home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / list1.zip / LIST1.TXT
Text File  |  1993-08-23  |  14KB  |  400 lines

  1.  
  2.  
  3. LIST BOXES
  4.  
  5. The LIST1 example program shows a very introductory list box program. This 
  6. list box has a LS_MULTIPLESEL style, and communicates with the client area 
  7. to have the selections displayed in the window.
  8.  
  9. ***********************************************************
  10. *                       LIST1.C                           *
  11. ***********************************************************
  12.  
  13. #define INCL_WIN
  14. #define INCL_GPI
  15.  
  16. #include <os2.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "list1.h"
  21.  
  22. MRESULT EXPENTRY ClientWndProc ( HWND hwndClient,
  23.                                  ULONG ulMsg,
  24.                                  MPARAM mpParm1,
  25.                                  MPARAM mpParm2 ) ;
  26.  
  27. MRESULT EXPENTRY DlgProc ( HWND hwndDlg,
  28.                            ULONG ulMsg,
  29.                            MPARAM mpParm1,
  30.                            MPARAM mpParm2 ) ;
  31.  
  32.  
  33.  
  34.  
  35. #define CLS_CLIENT               "MyClass"
  36. int main ( void ) ;
  37.  
  38. typedef struct {
  39.     BOOL   fSelectedItems ;
  40.     USHORT ausListBoxSel [10] ;
  41. } LISTBOXINFO, *PLISTBOXINFO ;
  42.  
  43. int main ( )
  44. {
  45.     HMQ hmqQueue ;
  46.     HAB habAnchor ;
  47.     ULONG ulFlags ;
  48.     HWND hwndFrame ;
  49.     HWND hwndClient ;
  50.     QMSG qmMsg ;
  51.  
  52.     /* initialization stuff */
  53.     habAnchor = WinInitialize ( 0 ) ;
  54.     hmqQueue = WinCreateMsgQueue ( habAnchor, 0 ) ;
  55.  
  56.     WinRegisterClass ( habAnchor,
  57.                        CLS_CLIENT,
  58.                        ClientWndProc,
  59.                        CS_SYNCPAINT,
  60.                        sizeof ( PVOID )) ;
  61.  
  62.     ulFlags = FCF_TITLEBAR | FCF_SYSMENU |
  63.               FCF_SIZEBORDER | FCF_MINMAX | FCF_SHELLPOSITION ;
  64.  
  65.  
  66.     hwndFrame = WinCreateStdWindow ( HWND_DESKTOP,
  67.                                    WS_VISIBLE,
  68.                                    &ulFlags,
  69.                                    CLS_CLIENT,
  70.                                    "Plain Listbox Example",
  71.                                    0,
  72.                                    ( HMODULE ) 0,
  73.                                    IDM_MENU,
  74.                                    &hwndClient ) ;
  75.  
  76.  
  77.     if ( hwndFrame != NULLHANDLE ) {
  78.         while ( WinGetMsg ( habAnchor, &qmMsg, NULLHANDLE, 0, 0 ))
  79.             WinDispatchMsg ( habAnchor, &qmMsg ) ;
  80.         WinDestroyWindow ( hwndFrame ) ;
  81.     } /* endif */
  82.  
  83.     /* clean - up */
  84.     WinDestroyMsgQueue ( hmqQueue ) ;
  85.     WinTerminate ( habAnchor ) ;
  86.     return 0 ;
  87. }
  88.  
  89. MRESULT EXPENTRY ClientWndProc ( HWND hwndClient, ULONG ulMsg,
  90.                                  MPARAM mpParm1, MPARAM mpParm2 )
  91.  
  92. {
  93.     PLISTBOXINFO pliInfo ;
  94.  
  95.  
  96.     pliInfo = WinQueryWindowPtr ( hwndClient, 0 ) ;
  97.  
  98.     switch ( ulMsg ) {
  99.         case WM_ERASEBACKGROUND:
  100.             return ( MRFROMLONG ( TRUE ) ) ;
  101.         case WM_CREATE:
  102.             pliInfo =
  103.             ( PLISTBOXINFO ) malloc ( sizeof ( LISTBOXINFO )) ;
  104.             if ( pliInfo == ( PLISTBOXINFO ) 0 ) {
  105.                 WinAlarm ( HWND_DESKTOP,
  106.                            WA_ERROR ) ;
  107.                 return (( MRESULT ) TRUE ) ;
  108.             } /* endif */
  109.             WinSetWindowPtr ( hwndClient,
  110.                 0,
  111.                 pliInfo ) ;
  112.             pliInfo -> fSelectedItems = FALSE ;
  113.             WinPostMsg ( hwndClient,
  114.                          UM_LOADDLG,
  115.                          ( MPARAM ) 0,
  116.                          ( MPARAM ) 0 ) ;
  117.             break ;
  118.         case WM_DESTROY:
  119.             if ( pliInfo )
  120.                 free ( pliInfo ) ;
  121.             break ;
  122.         case WM_PAINT:
  123.             {
  124.                 HPS hpsPresentationSpace ;
  125.                 RECTL rectInvalidRect, rclPaintRegion ;
  126.                 USHORT i ;
  127.  
  128.                 hpsPresentationSpace = WinBeginPaint (
  129.                                           hwndClient,
  130.                                           NULLHANDLE,
  131.                                           &rectInvalidRect ) ;
  132.                 rclPaintRegion.xLeft = rectInvalidRect.xLeft ;
  133.                 rclPaintRegion.xRight = rectInvalidRect.xRight ;
  134.                 rclPaintRegion.yBottom = rectInvalidRect.yBottom ;
  135.                 rclPaintRegion.yTop = rectInvalidRect.yTop ;
  136.  
  137.                 WinFillRect ( hpsPresentationSpace,
  138.                               &rectInvalidRect,
  139.                               CLR_WHITE ) ;
  140.                 if ( pliInfo -> fSelectedItems == TRUE ) {
  141.  
  142.                     rclPaintRegion.yTop -= 15 ;
  143.                     WinDrawText ( hpsPresentationSpace,
  144.                                   - 1,
  145.                                   "You have selected:",
  146.                                   &rclPaintRegion,
  147.                                   0,
  148.                                   0,
  149.                                   DT_LEFT | DT_TEXTATTRS ) ;
  150.  
  151.                     for ( i = 0 ; i < 10 ; i ++ )
  152.                         if ( pliInfo -> ausListBoxSel [i] ==
  153.                               TRUE ) {
  154.  
  155.                             rclPaintRegion.yTop -= 15 ;
  156.                             WinDrawText ( hpsPresentationSpace,
  157.                                           - 1,
  158.                                           pszListBoxEntry [i] ,
  159.                                           &rclPaintRegion,
  160.                                           0,
  161.                                           0,
  162.                                           DT_LEFT |
  163.                                           DT_TEXTATTRS ) ;
  164.  
  165.                             pliInfo -> ausListBoxSel [i] = FALSE ;
  166.                         } /* end if selected */
  167.                     pliInfo -> fSelectedItems = FALSE ;
  168.                 } /* end if */
  169.                 WinEndPaint ( hpsPresentationSpace ) ;
  170.                 break ;
  171.  
  172.             }
  173.         case UM_LOADDLG:
  174.             WinDlgBox ( hwndClient,
  175.                         hwndClient,
  176.                         DlgProc,
  177.                         ( HMODULE ) 0,
  178.                         IDD_LISTBOX,
  179.                         NULL ) ;
  180.             return ( MRFROMLONG ( TRUE ) ) ;
  181.         case WM_SIZE:
  182.             WinPostMsg ( hwndClient,
  183.                          UM_LISTBOXSEL ,
  184.                          ( MPARAM ) 0,
  185.                          ( MPARAM ) 0 ) ;
  186.             break ;
  187.         case UM_LISTBOXSEL:
  188.             {
  189.  
  190.                 SHORT sSelect = 0 ;
  191.                 USHORT usIndex = LIT_FIRST ;
  192.                 RECTL rclInvalidRegion ;
  193.  
  194.                 while ( sSelect != LIT_NONE ) {
  195.                     sSelect = ( SHORT ) WinSendDlgItemMsg (
  196.                         WinWindowFromID (
  197.                         hwndClient, IDD_LISTBOX ) ,
  198.                         IDL_LISTBOX,
  199.                         LM_QUERYSELECTION,
  200.                         MPFROMSHORT ( usIndex ) ,
  201.                         ( MPARAM ) 0 ) ;
  202.                     pliInfo -> ausListBoxSel [sSelect] = TRUE ;
  203.                     usIndex = sSelect ;
  204.                  }
  205.                 pliInfo -> fSelectedItems = TRUE ;
  206.  
  207.                 WinQueryWindowRect ( hwndClient,
  208.                     &rclInvalidRegion ) ;
  209.  
  210.                 rclInvalidRegion.xLeft =
  211.                    ( rclInvalidRegion.xRight -
  212.                    rclInvalidRegion.xLeft ) / 3 * 2 ;
  213.  
  214.                 WinInvalidateRect ( hwndClient,
  215.                     &rclInvalidRegion, FALSE ) ;
  216.                 break ;
  217.             }
  218.         default:
  219.             return WinDefWindowProc ( hwndClient,
  220.                 ulMsg, mpParm1, mpParm2 ) ;
  221.          } /* endswitch */
  222.  
  223.     return ( MRFROMLONG ( FALSE )) ;
  224.     }
  225.  
  226. MRESULT EXPENTRY DlgProc ( HWND hwndDlg, ULONG ulMsg,
  227.      MPARAM mpParm1, MPARAM mpParm2 )
  228.  
  229. {
  230.     USHORT usNumItems = 10 ;
  231.     USHORT i ;
  232.  
  233.     switch ( ulMsg ) {
  234.  
  235.         case WM_INITDLG:
  236.             {
  237.                 HWND hwndListBox ;
  238.  
  239.                 hwndListBox = WinWindowFromID ( hwndDlg,
  240.                      IDL_LISTBOX ) ;
  241.                 for ( i = 0 ; i < usNumItems ; i ++ )
  242.                     WinInsertLboxItem ( hwndListBox,
  243.                                         LIT_END,
  244.                                         pszListBoxEntry [i] ) ;
  245.  
  246.                 WinSendDlgItemMsg ( hwndDlg,
  247.                                     IDL_LISTBOX,
  248.                                     LM_SELECTITEM,
  249.                                     MPFROMSHORT ( 0 ) ,
  250.                                     MPFROMSHORT ( TRUE )) ;
  251.             }
  252.             break ;
  253.  
  254.         case WM_COMMAND:
  255.             {
  256.                 SHORT sCommand ;
  257.  
  258.                 HWND hwndClient ;
  259.                 RECTL rclRectangle ;
  260.  
  261.                 sCommand = SHORT1FROMMP ( mpParm1 ) ;
  262.                 switch ( sCommand ) {
  263.                     case DID_OK:
  264.                         hwndClient = WinQueryWindow ( hwndDlg,
  265.                                                       QW_PARENT ) ;
  266.                         WinPostMsg ( hwndClient,
  267.                                      UM_LISTBOXSEL,
  268.                                      ( MPARAM ) 0,
  269.                                      ( MPARAM ) 0 ) ;
  270.                         return (MRFROMLONG ( TRUE )) ;
  271.  
  272.                     case DID_CANCEL:
  273.                         hwndClient = WinQueryWindow ( hwndDlg,
  274.                                                       QW_PARENT ) ;
  275.                         WinQueryWindowRect ( hwndClient,
  276.                                              &rclRectangle ) ;
  277.                         WinInvalidateRect ( hwndClient,
  278.                                             &rclRectangle,
  279.                                             FALSE ) ;
  280.                         WinDismissDlg ( hwndDlg, sCommand ) ;
  281.                         break ;
  282.  
  283.                  } /* end switch sCommand */
  284.                break ;
  285.             }
  286.         default:
  287.             return ( WinDefDlgProc ( hwndDlg, ulMsg,
  288.                                      mpParm1, mpParm2 )) ;
  289.    } /* endswitch */
  290.  
  291.    return ( MRFROMLONG ( FALSE ) ) ;
  292. }
  293.  
  294. ***********************************************************
  295. *                       LIST1.RC                          *
  296. ***********************************************************
  297.  
  298. #include <os2.h>
  299. #include "LIST1.H"
  300.  
  301. DLGTEMPLATE IDD_LISTBOX LOADONCALL MOVEABLE DISCARDABLE
  302. BEGIN
  303.     DIALOG  "Listbox Example", IDD_LISTBOX, 12, 6, 170, 107, WS_VISIBLE,
  304.             FCF_SYSMENU | FCF_TITLEBAR
  305.             PRESPARAMS PP_BACKGROUNDCOLORINDEX, CLR_WHITE
  306.     BEGIN
  307.         LISTBOX         IDL_LISTBOX, 14, 28, 135, 63, LS_MULTIPLESEL
  308.             PRESPARAMS PP_BACKGROUNDCOLORINDEX, CLR_WHITE
  309.         PUSHBUTTON      "OK", DID_OK, 3, 1, 40, 14
  310.         PUSHBUTTON      "Cancel", DID_CANCEL, 48, 1, 40, 14
  311.     END
  312. END
  313.  
  314. ***********************************************************
  315. *                       LIST1.H                           *
  316. ***********************************************************
  317.  
  318. #define UM_LOADDLG                 (WM_USER+1)
  319. #define UM_LISTBOXSEL              (WM_USER+2)
  320. #define IDD_LISTBOX                 200
  321. #define IDL_LISTBOX                 201
  322. #define IDM_MENU                    202
  323.  
  324.  
  325.  
  326. CHAR *pszListBoxEntry[] = {
  327.          "Cowboys",
  328.          "Bengals",
  329.          "Oilers",
  330.          "Bears",
  331.          "Broncos",
  332.          "Jets",
  333.          "Raiders",
  334.          "Rams",
  335.          "Giants",
  336.          "Redskins" };
  337.  
  338. ***********************************************************
  339. *                       LIST1.MAK                         *
  340. *********************************************************** 
  341.  
  342. LIST1.EXE:                    LIST1.OBJ \
  343.                                 LIST1.RES
  344.         LINK386 @<<
  345. LIST1
  346. LIST1
  347. LIST1
  348. OS2386
  349. LIST1
  350. <<
  351.         RC LIST1.RES LIST1.EXE
  352.  
  353. LIST1.RES:                    LIST1.RC \
  354.                                 LIST1.H
  355.         RC -r LIST1.RC LIST1.RES
  356.  
  357. LIST1.OBJ:                    LIST1.C \
  358.                                 LIST1.H
  359.         ICC -C+ -Kb+ -Ss+ LIST1.C
  360.  
  361. ***********************************************************
  362. *                       LIST1.DEF                         *
  363. ***********************************************************
  364.  
  365. NAME LISTBOX WINDOWAPI
  366. DESCRIPTION 'Listbox example.
  367.              Copyright (c) 1992 by Kathleen Panov.
  368.              All rights reserved.'
  369.  
  370. ***********************************************************
  371.  
  372. In the LIST1 sample program, the dialog box will post a message, 
  373. UM_LISTBOXSEL, to the client area wwhen the OK button is pressed. 
  374. When the client area receives this message, it queries the list 
  375. box to determine which items have been selected. These items are 
  376. stored in the user-defined window word area for the client window. 
  377. Also, a flag, fSelectedItems is set to indicate items have been 
  378. selected.
  379.  
  380. The UM_LISTBOXSEL is known as a user-defined message. All user-defined 
  381. messages must use a message ID between 0x1000 (WM_USER) and 0xBFFF. 
  382. These messages give the programmer the opportunity to create a message for 
  383. an event that is not a system-defined event.
  384.  
  385. When the WM_PAINT message is received, the client area is cleared. If the 
  386. flag, fSelectedItems, is set, the items in the window word are written 
  387. to the client area.
  388.  
  389. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  390. +                       NOTICE                            +
  391. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  392.  
  393. The LIST1 example is from The Art of OS/2 C Programming
  394. by Kathleen Panov, Arthur Panov, and Larry Salomon, Jr.,
  395. August 1993, published by QED Publishing Group, ISBN 
  396. 0-89435-446-9. The example was uploaded by the publisher 
  397. with the assistance of the authors for use by Forum 
  398. members.
  399.  
  400.