home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mclb.zip / lb.pak / SAMPLES / SAMP1 / SAMP1.C < prev    next >
Text File  |  1995-08-17  |  15KB  |  267 lines

  1. /***************************************************************************/
  2. /***************************************************************************/
  3. /*                        DISCLAIMER OF WARRANTIES.                        */
  4. /***************************************************************************/
  5. /***************************************************************************/
  6. /*                                                                         */
  7. /*  Copyright (C) 1995 IBM Corporation                                     */
  8. /*                                                                         */
  9. /*      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is        */
  10. /*      sample code created by IBM Corporation. This sample code is not    */
  11. /*      part of any standard or IBM product and is provided to you solely  */
  12. /*      for  the purpose of assisting you in the development of your       */
  13. /*      applications.  The code is provided "AS IS", without               */
  14. /*      warranty of any kind.  IBM shall not be liable for any damages     */
  15. /*      arising out of your use of the sample code, even if they have been */
  16. /*      advised of the possibility of such damages.                        */
  17. /***************************************************************************/
  18. /*                                                                         */
  19. /* Author:         Mark McMillan                                           */
  20. /*                 IBM Corporation                                         */
  21. /*                                                                         */
  22. /***************************************************************************/
  23.  
  24. /*---------------------------------------------------------------------------
  25.                              Sample #1
  26.  
  27.   This is just a basic sample that shows how to:
  28.  
  29.      - Setup the MCLBINFO structure
  30.      - Create an MCLB control
  31.      - Insert data into the rows and columns
  32.      - Respond to MCLB control (WM_CONTROL) messages
  33.      - Resize an MCLB in a sizable window
  34.  
  35. ---------------------------------------------------------------------------*/
  36.  
  37.  
  38. #define  INCL_BASE        
  39. #define  INCL_WIN         
  40. #define  INCL_DOS         
  41. #define  INCL_WINSTDSPIN  
  42.                           
  43. #include <os2.h>          
  44. #include <string.h>       
  45. #include <stdio.h>        
  46. #include <stdlib.h>       
  47.  
  48. #include "mclb.h"               // MCLB definitions
  49.                           
  50. #include "DIALOG.H"       
  51.  
  52. #define  ID_MCLB   500          // ID of MCLB control
  53.                           
  54. /* General Dialog Helper Macros */    
  55. #define CONTROLID               SHORT1FROMMP(mp1)                                                  
  56. #define CONTROLCODE             SHORT2FROMMP(mp1)                                                  
  57. #define CONTROLHWND(ID)         WinWindowFromID(hwnd,ID)                                           
  58.  
  59. HAB hab;                        // Application anchor block
  60. HMQ MyQ;                        // Application message queue
  61. HWND MCLBHwnd;                  // Handle of MCLB control
  62. char Buff[100];                 // Msg buffer
  63. SHORT Item;                     // Item index
  64.  
  65. MRESULT EXPENTRY ID_MAINDLG_Proc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  66.  
  67. /*----------------------------------------------------------------------------*/
  68. MRESULT EXPENTRY ID_MAINDLG_Proc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) 
  69. /*----------------------------------------------------------------------------*/
  70. /* Dialog procedure for main window dialog.                                   */
  71. /*----------------------------------------------------------------------------*/
  72. {                                                                               
  73.                                                                                 
  74.   switch (msg) {                                                                
  75.                                                                                 
  76.     case WM_INITDLG: {
  77.       /* Create the MCLB before the dialog window is displayed.  We don't */
  78.       /* have to worry about sizing it now since we will get and process  */
  79.       /* a WM_WINDOWPOSCHANGED message before the window becomes visible. */
  80.  
  81.       MCLBINFO InitData;
  82.       LONG InitSizeList[3]= {2L, 1L, 1L};  // Make col 1 twice as big as 2 and 3
  83.  
  84.       /* Initialize MCLB create data structure */
  85.  
  86.       memset(&InitData, 0x00, sizeof(MCLBINFO));
  87.       // These are the only required initialization values:
  88.       InitData.Size = sizeof(MCLBINFO);
  89.       InitData.Cols = 3;
  90.       InitData.TabChar = '!';
  91.       InitData.Titles = "Title!Author!Publisher";
  92.       InitData.InitSizes= InitSizeList;  // Initial sizes (proportions)
  93.  
  94.       // Play with these for colors and fonts:
  95.       // InitData.TitleBColor = 0x00FFFF00;     
  96.       // InitData.TitleFColor = 0x00000000;
  97.       // InitData.ListBColor = 0x0000FFFF;
  98.       // InitData.ListFColor = 0x00000000;
  99.       // strcpy(InitData.ListFont, "8.Helv");
  100.       // strcpy(InitData.TitleFont, "10.Helvetica Bold Italic");
  101.  
  102.       /* Now create the MCLB.  The dialog window is the parent (so it */
  103.       /* draws on the dialog), and owner (so this dialog proc will    */
  104.       /* get messages from it).                                       */
  105.  
  106.       MCLBHwnd = MCLBCreateWindow(
  107.                  hwnd,                    // Parent window
  108.                  hwnd,                    // Owner to recv messages
  109.                  WS_VISIBLE|              // Styles: Make it visible
  110.                    WS_TABSTOP|              // Let user TAB to it
  111.                    MCLBS_SIZEMETHOD_PROP|   // Proportional sizing when window is sized
  112.                    LS_HORZSCROLL,           // Give each column a horizontal scroll bar
  113.                  0,0,100,100,             // Will set size later, but must have large horz size now
  114.                  HWND_TOP,                // Put on top of any sibling windows
  115.                  ID_MCLB,                 // Window ID
  116.                  &InitData);              // MCLB create structure
  117.  
  118.       /* Insert data into the MCLB.  Each LM_INSERTITEM inserts a single */
  119.       /* row.  Columns are separated with the TabChar supplied above     */
  120.       /* (an "!" for our sample here).                                   */
  121.  
  122.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("OS/2 Programmer's Guide!Iacobucci!McGraw-Hill"));
  123.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("Client/Server Programming with OS/2!Orfali, Harkey!VNR"));
  124.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("The REXX Language!Cowlishaw!Prentice Hall"));
  125.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("Inside OS/2!Minasi, Little, Semple, Camarda!New Riders Publishing"));
  126.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("Developing Multimedia Applications under OS/2!Lawton, Noe, Lopez!John Wiley & Sons"));
  127.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("OS/2 Warp for Dummies!Rathbone!IDG"));
  128.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("REXX Reference Summary Handbook!Goran!C F S Nevada"));
  129.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("Progamming the OS/2 Warp GPI!!John Wiley and Sons"));
  130.       WinSendMsg(MCLBHwnd, LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), MPFROMP("Inside OS/2 Warp!Minasi!New Riders Publishing"));
  131.  
  132.       return FALSE;                                                             
  133.       } // end of WM_INITDLG
  134.                                                                                 
  135.     case WM_COMMAND:
  136.       switch (SHORT1FROMMP(mp1)) {                                              
  137.         case DID_OK: /*----------~OK (PUSHBUTTON)----------*/                   
  138.           /* Since our main window is a dialog, don't let default dialog */
  139.           /* proc dismiss us or focus will not always return to the      */
  140.           /* proper application.  Instead, destroy ourselves.  This is   */
  141.           /* a trick to properly using a dialog for a main window.       */
  142.           WinDestroyWindow(hwnd);
  143.           return 0;
  144.       }                                                                         
  145.       break;                                                                    
  146.                                                                                 
  147.     case WM_CONTROL:                                                            
  148.       switch SHORT1FROMMP(mp1) {                                                
  149.         case ID_MCLB: 
  150.  
  151.           /* Process control messages from the MCLB.  Most of them are */
  152.           /* standard listbox messages with the same meaning as usual. */
  153.           /* There are also a few MCLB-specific control messages.      */
  154.  
  155.           switch SHORT2FROMMP(mp1) {
  156.             case LN_SELECT:
  157.  
  158.               /* User selected or unselected an entry in the MCLB.  Our   */
  159.               /* sample uses a single-select listbox.                     */
  160.  
  161.               Item = SHORT1FROMMR(WinSendMsg(MCLBHwnd, LM_QUERYSELECTION, MPFROMSHORT(LIT_FIRST), MPVOID));
  162.               if (Item >= 0)
  163.                 sprintf(Buff, "Item %i selected", Item);
  164.               else
  165.                 strcpy(Buff, "Unable to query selection.");
  166.               WinSetDlgItemText(hwnd, ID_MSG, Buff);
  167.               return 0;
  168.  
  169.             case LN_ENTER:
  170.  
  171.               /* User double-clicked or pressed ENTER in the MCLB. */
  172.  
  173.               Item = SHORT1FROMMR(WinSendMsg(MCLBHwnd, LM_QUERYSELECTION, MPFROMSHORT(LIT_FIRST), MPVOID));
  174.               if (Item >= 0)
  175.                 sprintf(Buff, "Item %i entered", Item);
  176.               else
  177.                 strcpy(Buff, "Unable to query selection.");
  178.               WinSetDlgItemText(hwnd, ID_MSG, Buff);
  179.               return 0;
  180.  
  181.             case MCLBN_COLSIZED: {
  182.  
  183.               /* The user has changed the relative column sizings using one */
  184.               /* of the split bars.  Note we do not get this message when   */
  185.               /* the entire MCLB is resized with the window, only when the  */
  186.               /* user moves a splitbar.                                     */
  187.  
  188.               LONG SizeList[3];
  189.               WinSendMsg(MCLBHwnd, MCLB_QUERYCOLSIZES, MPFROMP(SizeList), MPVOID);
  190.               sprintf(Buff, "Columns resized to (%li, %li, %li)", SizeList[0], SizeList[1], SizeList[2]);
  191.               WinSetDlgItemText(hwnd, ID_MSG, Buff);
  192.               return 0;
  193.               }
  194.           } // switch on MCLB notify code
  195.           break;
  196.       }                                                                         
  197.       break; /* End of WM_CONTROL */                                            
  198.  
  199.     case WM_WINDOWPOSCHANGED: {
  200.  
  201.       /* Dialog was resized, so we resize/move the dialog controls as needed. */
  202.       /* When we resize the MCLB, it will adjust it's column widths according */
  203.       /* the the MCLBS_SIZEMETHOD_* algorithm we specified in the style bits. */
  204.       /* Resizing the control does NOT cause a MCLBN_COLSIZED control message.*/
  205.  
  206.       SWP  Pos1, Pos2;
  207.  
  208.       /* Let def dlg proc set frame controls */
  209.       WinDefDlgProc(hwnd, msg, mp1, mp2);
  210.  
  211.       /* Size/place MCLB above OK button, centered in dialog.  Note that the */
  212.       /* dialog window is the frame, so we must account for frame controls.  */
  213.  
  214.       WinQueryWindowPos(CONTROLHWND(DID_OK), &Pos1);
  215.       WinQueryWindowPos(hwnd, &Pos2);
  216.       Pos2.cy = Pos2.cy - WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR) - WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER);
  217.       Pos2.cx = Pos2.cx - (2 * Pos1.x),
  218.       WinSetWindowPos(MCLBHwnd, HWND_TOP,
  219.          Pos1.x,
  220.          Pos1.y+Pos1.cy+5,
  221.          Pos2.cx,
  222.          Pos2.cy - (Pos1.y+Pos1.cy+7+Pos1.y),
  223.          SWP_MOVE|SWP_SIZE);
  224.  
  225.       /* Size/place message control */
  226.       WinQueryWindowPos(CONTROLHWND(ID_MSG), &Pos1);
  227.       WinQueryWindowPos(hwnd, &Pos2);
  228.       WinSetWindowPos(CONTROLHWND(ID_MSG), HWND_TOP,
  229.          0,0,
  230.          Pos2.cx-Pos1.x-(2*WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER))-3,
  231.          Pos1.cy,
  232.          SWP_SIZE);
  233.  
  234.       return 0;  // Already called default proc, so just return.
  235.       }
  236.                                                                                 
  237.   } /* End of (msg) switch */                                                   
  238.                                                                                 
  239.   return WinDefDlgProc(hwnd, msg, mp1, mp2);                                    
  240.                                                                                 
  241. } /* End dialog procedure */                                                    
  242.  
  243. /*----------------------------------------------------------------------------*/
  244. int main(int argc,char **argv, char **envp)                                     
  245. /*----------------------------------------------------------------------------*/
  246. /*  Main routine just runs a dialog box (no main window).                     */
  247. /*----------------------------------------------------------------------------*/
  248. {                                                                               
  249.   /* Initialize PM window environment, get a message queue */                   
  250.                                                                                 
  251.   hab = WinInitialize(0L);                                                      
  252.   MyQ = WinCreateMsgQueue(hab, 0) ;                                             
  253.                                                                                 
  254.   WinDlgBox(HWND_DESKTOP, HWND_DESKTOP, (PFNWP)                                 
  255.             ID_MAINDLG_Proc,                                                    
  256.             NULLHANDLE,                                                         
  257.             ID_MAINDLG,                                                         
  258.             NULL);                                                              
  259.                                                                                 
  260.   /* Cleanup when dialog terminates */                                          
  261.                                                                                 
  262.   WinDestroyMsgQueue(MyQ);                                                      
  263.   WinTerminate(hab);                                                            
  264.  
  265.   return 0;
  266. }                                                                               
  267.