home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / SLIDER.ZIP / SLIDER.C next >
Text File  |  1992-01-17  |  16KB  |  441 lines

  1. /***************************************************************
  2.  * Author:  Myron McCloud
  3.  *
  4.  * Date:   1/17/92
  5.  *
  6.  * File Name:  Slider.c
  7.  *
  8.  * Description: Example of a Slider.
  9.  *
  10.  * Purpose:
  11.  *
  12.  * Parameter:  None.
  13.  *
  14.  * Returns:
  15.  *
  16.  * Comments:
  17.  *
  18.  ***************************************************************/
  19. #define INCL_WIN
  20. #define INCL_PM
  21. #define INCL_WINSTDSLIDER      // slider control class
  22. #define INCL_WINWINDOWMGR      // editfield control class
  23. #include <os2.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <process.h>
  29. #include "slider.h"
  30.  
  31. main (void)
  32. {
  33.    HAB      hab;
  34.    HMQ      hmq;
  35.    QMSG     qmsg;
  36.    HWND     hwndParent;
  37.    HWND     hwndClient;
  38.    HWND     hwndSlider;
  39.  
  40.    static CHAR szClientClass [] = "Client Window";
  41.  
  42.    ULONG flCreateFlags = FCF_TITLEBAR
  43.                          | FCF_SYSMENU
  44.                          | FCF_SIZEBORDER
  45.                          | FCF_MINMAX
  46.                          | FCF_SHELLPOSITION
  47.                          | FCF_TASKLIST
  48.                          | FCF_MENU
  49.                          ;
  50.  
  51.    hab = WinInitialize (0);
  52.  
  53.    hmq = WinCreateMsgQueue (hab, 0);
  54.  
  55.    WinRegisterClass ((HAB)    hab
  56.                     ,(PSZ)    szClientClass
  57.                     ,(PFNWP)  ClientWinProc
  58.                     ,(ULONG)  0L
  59.                     ,(USHORT) 0
  60.                     );
  61.  
  62.    hwndParent = WinCreateStdWindow (HWND_DESKTOP
  63.                                    ,WS_VISIBLE
  64.                                    ,&flCreateFlags
  65.                                    ,szClientClass
  66.                                    ,""
  67.                                    ,0L
  68.                                    ,0
  69.                                    ,ID_FRAMERC
  70.                                    ,&hwndClient
  71.                                    );
  72.  
  73.    while (WinGetMsg (hab, &qmsg, NULL, 0, 0) )
  74.    {
  75.       WinDispatchMsg (hab, &qmsg);
  76.    } /* endwhile */
  77.  
  78.    WinDestroyWindow (hwndSlider);
  79.    WinDestroyWindow (hwndParent);
  80.    WinDestroyMsgQueue (hmq);
  81.    WinTerminate (hab);
  82.    return 0;
  83. }
  84.  
  85. /***************************************************************
  86.  * Function Name:  ClientWinProc
  87.  *
  88.  * Description: Window procedure for the Client
  89.  *
  90.  * Purpose:
  91.  *
  92.  * Parameter      Description
  93.  * --------------------------------------------------------------
  94.  * hwnd           HWND
  95.  * msg            USHORT
  96.  * mp1            MPARAM1
  97.  * mp2            MPARAM2
  98.  *
  99.  * Returns:
  100.  *
  101.  * Comments:
  102.  *
  103.  ***************************************************************/
  104.  
  105. MRESULT ClientWinProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  106. {
  107.  
  108.    #define TICK  10
  109.    #define SPACE 10
  110.  
  111.    BOOL       rc;
  112.    USHORT     usIndex;
  113.    SHORT      usArrows;                // Fudge Factor
  114.    ENTRYFDATA entryfdata;
  115.    HPS        hps;
  116.    RECTL      rcl;
  117.    CHAR       buff[4];
  118.    CHAR       *cData;
  119.    static     SLDCDATA   sldcdata;     // slider control data structure
  120.    static     WNDPARAMS  wndparams;    // slider window parameters structure
  121.    static     PARAM      param;        // slider presentation data structure
  122.    static     HWND       hwndSlider;   // handle to slider window
  123.    static     HWND       hwndDisplay;  // handle to entryfield window
  124.    static     HWND       hwndMenu;
  125.    static     SHORT      ClientCx;
  126.    static     SHORT      Clientcy;
  127.    static     SHORT      SliderWindowCx;
  128.    static     SHORT      NewShaftCx;
  129.    static     SHORT      PixelsBetweenTicks;
  130.    static     ULONG      ulArmPosition;
  131.    static     ULONG      color = CLR_RED;
  132.  
  133.    ULONG SliderCreateFlags = SLS_HORIZONTAL
  134.                            | SLS_BUTTONSLEFT
  135.                            | SLS_HOMELEFT
  136.                            | SLS_PRIMARYSCALE1
  137.                            | SLS_RIBBONSTRIP
  138.                            | WS_VISIBLE
  139.                            ;
  140.    ULONG EntryFieldCreateFlags = ES_LEFT
  141.                                | WS_VISIBLE
  142.                                | ES_MARGIN
  143.                                ;
  144.  
  145.    switch (msg)
  146.    {
  147.       case WM_COMMAND:
  148.          switch (SHORT1FROMMP (mp1))
  149.          {
  150.             case ID_HSLIDER:
  151.                /**
  152.                 *
  153.                 * Create window with horizonal slider
  154.                 *
  155.                 **/
  156.                hwndMenu=WinWindowFromID (WinQueryWindow (hwnd,QW_PARENT)
  157.                                         ,FID_MENU);
  158.  
  159.                WinEnableMenuItem (hwndMenu, ID_HSLIDER, FALSE);
  160.                WinEnableMenuItem (hwndMenu, ID_STOP, TRUE);
  161.  
  162.                sldcdata.cbSize = sizeof (sldcdata); // Size of control block
  163.                sldcdata.usScale1Increments = TICK ; // # of divisions on scale
  164.                sldcdata.usScale1Spacing    = SPACE; // pels between increments
  165.                sldcdata.usScale2Increments = TICK ; // # of divisions on scale
  166.                sldcdata.usScale2Spacing    = SPACE; // pels between increments
  167.                hwndSlider = WinCreateWindow (hwnd               // parent window
  168.                                             ,WC_SLIDER          // Class
  169.                                             ,""                 // window text
  170.                                             ,SliderCreateFlags  // create style
  171.                                             ,0                  // x = position
  172.                                             ,35                 // y = position
  173.                                             ,120                // cx= width
  174.                                             ,35                 // cy= height
  175.                                             ,hwnd               // window owner
  176.                                             ,HWND_TOP           // where to ins
  177.                                             ,WINDOW_ID          // window id
  178.                                             ,(PVOID) &sldcdata  // ctl data
  179.                                             ,(PVOID) NULL       // pres param
  180.                                             );
  181.  
  182.                // Set background color of slider window
  183.                WinSetPresParam (hwndSlider
  184.                                ,PP_BACKGROUNDCOLORINDEX
  185.                                ,sizeof(ULONG),&color
  186.                                );
  187.                /**
  188.                 *
  189.                 * place shaft window flush against left side of slider window
  190.                 * (x=0)place shaft window bottom flush against bottom of slider
  191.                 * window (y=0)
  192.                 *
  193.                 **/
  194.                WinSendMsg (hwndSlider
  195.                           ,SLM_SETSLIDERINFO
  196.                           ,MPFROMSHORT  (SMA_SHAFTPOSITION)
  197.                           ,MPFROM2SHORT (0,0)
  198.                           );
  199.  
  200.                /**
  201.                 *
  202.                 * Create entryfield window to display slider arm position
  203.                 *
  204.                 **/
  205.                entryfdata.cb           = sizeof (entryfdata);
  206.                entryfdata.cchEditLimit = 256                ;
  207.                entryfdata.ichMinSel    = 0                  ;
  208.                entryfdata.ichMaxSel    = 256                ;
  209.                hwndDisplay = WinCreateWindow (hwnd
  210.                                              ,WC_ENTRYFIELD
  211.                                              ,"Arm Position"
  212.                                              ,EntryFieldCreateFlags
  213.                                              ,100
  214.                                              ,160
  215.                                              ,85
  216.                                              ,20
  217.                                              ,hwnd
  218.                                              ,HWND_TOP
  219.                                              ,WINDOW_ID + 1
  220.                                              ,(PVOID) &entryfdata
  221.                                              ,NULL
  222.                                              );
  223.                /**
  224.                 *
  225.                 * This message sets the size of the a tick mark for the
  226.                 * primary scale. NOTE: all tick marks are initially set to
  227.                 * a size of 0 (INVISIBLE).
  228.                 *
  229.                 **/
  230.                WinSendMsg (hwndSlider
  231.                           ,SLM_SETTICKSIZE
  232.                           ,MPFROM2SHORT (SMA_SETALLTICKS, 5)
  233.                           ,NULL
  234.                           );
  235.                /**
  236.                 *
  237.                 * For each tick mark set text above the tick mark for the
  238.                 * primary scale. Tick mark does not have to be visible to
  239.                 * have text. Text is centered on the tick mark.
  240.                 *
  241.                 **/
  242.                for (usIndex = 0; usIndex < TICK; usIndex ++)
  243.                {
  244.                   _itoa (usIndex, buff, 10);
  245.  
  246.                   WinSendMsg (hwndSlider
  247.                              ,SLM_SETSCALETEXT
  248.                              ,MPFROMSHORT (usIndex)
  249.                              ,MPFROMP (buff)
  250.                              );
  251.                }
  252.  
  253.                /**
  254.                 *
  255.                 * place shaft window flush against left side of slider window
  256.                 * (x=0)place shaft window bottom flush against bottom of slider
  257.                 * window (y=0)
  258.                 *
  259.                 **/
  260.                 WinSendMsg (hwndSlider
  261.                            ,SLM_SETSLIDERINFO
  262.                            ,MPFROMSHORT  (SMA_SHAFTPOSITION)
  263.                            ,MPFROM2SHORT (0,0)
  264.                            );
  265.  
  266.              break;
  267.  
  268.  
  269.            case ID_VSLIDER:
  270.                /**
  271.                 *
  272.                 * Create window with vertical slider.
  273.                 * This is left as an exercise for the student.
  274.                 *
  275.                 **/
  276.                WinMessageBox (HWND_DESKTOP
  277.                              ,hwnd
  278.                              ,"Vertical slider not implemented."
  279.                              ,"Information"
  280.                              ,WINDOW_ID + 2
  281.                              ,MB_CANCEL
  282.                              );
  283.                break;
  284.  
  285.            case ID_STOP:
  286.                /**
  287.                 *
  288.                 * Close Slider window.
  289.                 *
  290.                 **/
  291.                WinEnableMenuItem (hwndMenu, ID_STOP, FALSE);
  292.                WinEnableMenuItem (hwndMenu, ID_HSLIDER, TRUE);
  293.                WinDestroyWindow (hwndSlider);
  294.                WinDestroyWindow (hwndDisplay);
  295.                break;
  296.  
  297.            case ID_EXIT:                 // Exit selected
  298.                WinPostMsg (hwnd, WM_QUIT, NULL, NULL);
  299.                break;
  300.  
  301.          } /* endswitch on WM_COMMAND messages */
  302.          break;
  303.  
  304.  
  305.       case WM_PAINT:
  306.          hps = WinBeginPaint (hwnd, 0, 0);
  307.          GpiErase (hps);
  308.          WinQueryWindowRect (hwnd, &rcl);
  309.          WinFillRect ((HPS) hps, (PRECTL) &rcl, (LONG) CLR_BLUE);
  310.          WinEndPaint (hps);
  311.          break;
  312.  
  313.  
  314.       case WM_SIZE:
  315.          /**
  316.           *
  317.           * Save current slider arm position
  318.           *
  319.           **/
  320.           ulArmPosition =(LONG) WinSendMsg (hwndSlider
  321.                                            ,SLM_QUERYSLIDERINFO
  322.                                            ,MPFROM2SHORT (SMA_SLIDERARMPOSITION
  323.                                            ,SMA_INCREMENTVALUE)
  324.                                            ,NULL
  325.                                            );
  326.          /**
  327.           *
  328.           * Calulates the new (Cx) size of SliderWindow and ShaftWindow
  329.           * in addition determine the distance between the ShaftWindow
  330.           * and the Arrow window in pixels.
  331.           *
  332.           **/
  333.          ClientCx           = SHORT1FROMMP (mp2)        ;
  334.          Clientcy           = SHORT2FROMMP (mp2)        ;
  335.          SliderWindowCx     = ClientCx                  ;
  336.          usArrows           = 20                        ;
  337.          NewShaftCx         = SliderWindowCx - usArrows ;
  338.          PixelsBetweenTicks = NewShaftCx / TICK         ;
  339.  
  340.          sldcdata.usScale1Spacing = PixelsBetweenTicks; // space between ticks
  341.          sldcdata.usScale2Spacing = PixelsBetweenTicks; // space between ticks
  342.  
  343.          wndparams.fsStatus       = WPM_CTLDATA       ; // param selection
  344.          wndparams.cchText        = 0                 ; // len of window text
  345.          wndparams.pszText        = NULL              ; // window text
  346.          wndparams.cbPresParams   = 0                 ; // len of pres param
  347.          wndparams.pPresParams    = NULL              ; // specific pres param
  348.          wndparams.cbCtlData      = sizeof (sldcdata) ; // len of specific data
  349.          wndparams.pCtlData       = &sldcdata         ; // window specific data
  350.  
  351.          // This call allow the general positioning of a window
  352.          WinSetWindowPos (hwndSlider
  353.                          ,HWND_TOP
  354.                          ,0
  355.                          ,35
  356.                          ,SliderWindowCx
  357.                          ,35
  358.                          ,SWP_MOVE | SWP_SIZE | SWP_SHOW
  359.                          );
  360.  
  361.          // This message is sent to set or change the sliders window parameters
  362.          WinSendMsg (hwndSlider
  363.                     ,WM_SETWINDOWPARAMS
  364.                     ,&wndparams
  365.                     ,NULL
  366.                     );
  367.          /**
  368.           *
  369.           * place shaft window flush against left side slider window (x=0)
  370.           * place shaft window bottom flush against bottom of slider window
  371.           * (y=0)
  372.           *
  373.           **/
  374.           WinSendMsg (hwndSlider
  375.                      ,SLM_SETSLIDERINFO
  376.                      ,MPFROMSHORT  (SMA_SHAFTPOSITION)
  377.                      ,MPFROM2SHORT (0,0)
  378.                      );
  379.  
  380.          /**
  381.           *
  382.           * Restore slider arm position
  383.           *
  384.           **/
  385.           rc = (BOOL) WinSendMsg (hwndSlider
  386.                                  ,SLM_SETSLIDERINFO
  387.                                  ,MPFROM2SHORT (SMA_SLIDERARMPOSITION, SMA_INCREMENTVALUE)
  388.                                  ,MPFROMSHORT ((USHORT) ulArmPosition)
  389.                                  );
  390.  
  391.          // This call allow the general positioning of editfield window
  392.          WinSetWindowPos (hwndDisplay
  393.                          ,HWND_TOP
  394.                          ,0
  395.                          ,Clientcy - 35
  396.                          ,SliderWindowCx
  397.                          ,35
  398.                          ,SWP_MOVE | SWP_SIZE | SWP_SHOW
  399.                          );
  400.          break;
  401.  
  402.  
  403.      case WM_CONTROL:
  404.          /**
  405.           * Process the WM_CONTROL messages for the slider
  406.           **/
  407.           switch (SHORT2FROMMP (mp1))
  408.           {
  409.             case SLN_CHANGE:
  410.             {
  411.                LONG ulValue;
  412.                ulValue = (LONG) WinSendMsg ((HWND) WinWindowFromID
  413.                                            (hwnd, WINDOW_ID)
  414.                                            ,SLM_QUERYSLIDERINFO
  415.                                            ,MPFROM2SHORT (SMA_SLIDERARMPOSITION
  416.                                            ,SMA_INCREMENTVALUE)
  417.                                            ,NULL
  418.                                            );
  419.  
  420.                /**
  421.                 * Convert slider arm position to ascii and place
  422.                 * ascii text into entryfield window
  423.                 **/
  424.                cData = _ltoa (ulValue, buff, 10);
  425.                WinSetWindowText ((HWND) hwndDisplay, (PSZ) cData);
  426.                break;
  427.             }
  428.  
  429.           } /* endswitch on WM_CONTROL notifcation messages */
  430.           break;
  431.  
  432.       default:
  433.          return (WinDefWindowProc (hwnd, msg, mp1, mp2));
  434.          break;
  435.  
  436.    } /* endswitch on msg */
  437.  
  438.    return (MRESULT) TRUE;
  439.  
  440. } /* end of ClientWinProc */
  441.