home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mclb.zip / lb.pak / SAMPLES / samp4 / samp4.C < prev    next >
Text File  |  1995-08-04  |  15KB  |  274 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 #4
  26.  
  27.   This sample combines a multicolumn listbox and a drag/drop listbox to
  28.   create a multicolumn drag/drop listbox.  This is a just combining the
  29.   functions of sample #1 and sample #3.
  30.  
  31. ---------------------------------------------------------------------------*/
  32.  
  33.  
  34. #define  INCL_BASE        
  35. #define  INCL_WIN         
  36. #define  INCL_DOS         
  37. #define  INCL_WINSTDSPIN  
  38.                           
  39. #include <os2.h>          
  40. #include <string.h>       
  41. #include <stdio.h>        
  42. #include <stdlib.h>       
  43.  
  44. #include "mclb.h"               // MCLB definitions
  45. #include "dmlb.h"               // DMLB definitions
  46.                           
  47. #include "DIALOG.H"       
  48.  
  49. #define  ID_MCLB   500          // ID of MCLB control
  50.                           
  51. /* General Dialog Helper Macros */    
  52. #define CONTROLID               SHORT1FROMMP(mp1)                                                  
  53. #define CONTROLCODE             SHORT2FROMMP(mp1)                                                  
  54. #define CONTROLHWND(ID)         WinWindowFromID(hwnd,ID)                                           
  55.  
  56. HAB hab;                        // Application anchor block
  57. HMQ MyQ;                        // Application message queue
  58. HWND MCLBHwnd;                  // Handle of MCLB control
  59. char Buff[100];                 // Msg buffer
  60. SHORT Item;                     // Item index
  61.  
  62. MRESULT EXPENTRY ID_MAINDLG_Proc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  63.  
  64. /*----------------------------------------------------------------------------*/
  65. MRESULT EXPENTRY ID_MAINDLG_Proc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) 
  66. /*----------------------------------------------------------------------------*/
  67. /* Dialog procedure for main window dialog.                                   */
  68. /*----------------------------------------------------------------------------*/
  69. {                                                                               
  70.                                                                                 
  71.   switch (msg) {                                                                
  72.                                                                                 
  73.     case WM_INITDLG: {
  74.       /* Create the MCLB before the dialog window is displayed.  We don't */
  75.       /* have to worry about sizing it now since we will get and process  */
  76.       /* a WM_WINDOWPOSCHANGED message before the window becomes visible. */
  77.  
  78.       MCLBINFO InitData;
  79.       LONG InitSizeList[3]= {2L, 1L, 1L};  // Make col 1 twice as big as 2 and 3
  80.  
  81.       /* Initialize MCLB create data structure */
  82.  
  83.       memset(&InitData, 0x00, sizeof(MCLBINFO));
  84.       // These are the only required initialization values:
  85.       InitData.Size = sizeof(MCLBINFO);
  86.       InitData.Cols = 3;
  87.       InitData.TabChar = '!';
  88.       InitData.Titles = "Title!Author!Publisher";
  89.       InitData.InitSizes= InitSizeList;  // Initial sizes (proportions)
  90.  
  91.       // Play with these for colors and fonts:
  92.       // InitData.TitleBColor = 0x00FFFF00;     
  93.       // InitData.TitleFColor = 0x00000000;
  94.       // InitData.ListBColor = 0x0000FFFF;
  95.       // InitData.ListFColor = 0x00000000;
  96.       // strcpy(InitData.ListFont, "8.Helv");
  97.       // strcpy(InitData.TitleFont, "10.Helvetica Bold Italic");
  98.  
  99.       /* Now create the MCLB.  The dialog window is the parent (so it */
  100.       /* draws on the dialog), and owner (so this dialog proc will    */
  101.       /* get messages from it).                                       */
  102.  
  103.       MCLBHwnd = MCLBCreateWindow(
  104.                  hwnd,                    // Parent window
  105.                  hwnd,                    // Owner to recv messages
  106.                  WS_VISIBLE|              // Styles: Make it visible
  107.                    WS_TABSTOP|              // Let user TAB to it
  108.                    MCLBS_SIZEMETHOD_PROP|   // Proportional sizing when window is sized
  109.                    LS_HORZSCROLL,           // Give each column a horizontal scroll bar
  110.                  0,0,100,100,             // Will set size later, but must have large horz size now
  111.                  HWND_TOP,                // Put on top of any sibling windows
  112.                  ID_MCLB,                 // Window ID
  113.                  &InitData);              // MCLB create structure
  114.  
  115.       /* Add direct-manipulation capability to the listbox */
  116.       DMLBInitialize(MCLBHwnd, NULLHANDLE);
  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.  
  195.             case LN_DMLB_QRYDROP:
  196.               /* A listbox item is about to be dropped on this listbox.  We must    */
  197.               /* return a Ok-to-drop  indicator, and tell what to do with the       */
  198.               /* original item (copy or move).  Since we have just one listbox in   */
  199.               /* this sample, MOVE is the only thing that makes sense.              */
  200.               return MRFROM2SHORT(TRUE, DROPMODE_MOVE);
  201.           } // switch on MCLB notify code
  202.           break;
  203.       }                                                                         
  204.       break; /* End of WM_CONTROL */                                            
  205.  
  206.     case WM_WINDOWPOSCHANGED: {
  207.  
  208.       /* Dialog was resized, so we resize/move the dialog controls as needed. */
  209.       /* When we resize the MCLB, it will adjust it's column widths according */
  210.       /* the the MCLBS_SIZEMETHOD_* algorithm we specified in the style bits. */
  211.       /* Resizing the control does NOT cause a MCLBN_COLSIZED control message.*/
  212.  
  213.       SWP  Pos1, Pos2;
  214.  
  215.       /* Let def dlg proc set frame controls */
  216.       WinDefDlgProc(hwnd, msg, mp1, mp2);
  217.  
  218.       /* Size/place MCLB above OK button, centered in dialog.  Note that the */
  219.       /* dialog window is the frame, so we must account for frame controls.  */
  220.  
  221.       WinQueryWindowPos(CONTROLHWND(DID_OK), &Pos1);
  222.       WinQueryWindowPos(hwnd, &Pos2);
  223.       Pos2.cy = Pos2.cy - WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR) - WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER);
  224.       Pos2.cx = Pos2.cx - (2 * Pos1.x),
  225.       WinSetWindowPos(MCLBHwnd, HWND_TOP,
  226.          Pos1.x,
  227.          Pos1.y+Pos1.cy+5,
  228.          Pos2.cx,
  229.          Pos2.cy - (Pos1.y+Pos1.cy+7+Pos1.y),
  230.          SWP_MOVE|SWP_SIZE);
  231.  
  232.       /* Size/place message control */
  233.       WinQueryWindowPos(CONTROLHWND(ID_MSG), &Pos1);
  234.       WinQueryWindowPos(hwnd, &Pos2);
  235.       WinSetWindowPos(CONTROLHWND(ID_MSG), HWND_TOP,
  236.          0,0,
  237.          Pos2.cx-Pos1.x-(2*WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER))-3,
  238.          Pos1.cy,
  239.          SWP_SIZE);
  240.  
  241.       return 0;  // Already called default proc, so just return.
  242.       }
  243.                                                                                 
  244.   } /* End of (msg) switch */                                                   
  245.                                                                                 
  246.   return WinDefDlgProc(hwnd, msg, mp1, mp2);                                    
  247.                                                                                 
  248. } /* End dialog procedure */                                                    
  249.  
  250. /*----------------------------------------------------------------------------*/
  251. int main(int argc,char **argv, char **envp)                                     
  252. /*----------------------------------------------------------------------------*/
  253. /*  Main routine just runs a dialog box (no main window).                     */
  254. /*----------------------------------------------------------------------------*/
  255. {                                                                               
  256.   /* Initialize PM window environment, get a message queue */                   
  257.                                                                                 
  258.   hab = WinInitialize(0L);                                                      
  259.   MyQ = WinCreateMsgQueue(hab, 0) ;                                             
  260.                                                                                 
  261.   WinDlgBox(HWND_DESKTOP, HWND_DESKTOP, (PFNWP)                                 
  262.             ID_MAINDLG_Proc,                                                    
  263.             NULLHANDLE,                                                         
  264.             ID_MAINDLG,                                                         
  265.             NULL);                                                              
  266.                                                                                 
  267.   /* Cleanup when dialog terminates */                                          
  268.                                                                                 
  269.   WinDestroyMsgQueue(MyQ);                                                      
  270.   WinTerminate(hab);                                                            
  271.  
  272.   return 0;
  273. }                                                                               
  274.