home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mclb.zip / lb.pak / SAMPLES / samp5 / SAMP5.C < prev    next >
Text File  |  1995-08-04  |  16KB  |  273 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 #5
  26.  
  27.   This sample demonstrates drag/drop of listbox items between different
  28.   listbox controls.
  29.  
  30. ---------------------------------------------------------------------------*/
  31. #define  INCL_BASE                   
  32. #define  INCL_WIN                    
  33. #define  INCL_DOS                    
  34. #define  INCL_WINSTDSPIN             
  35.                                      
  36. #include <os2.h>                     
  37. #include <string.h>                  
  38. #include <stdio.h>                   
  39. #include <stdlib.h>                  
  40.                                      
  41. #include "DIALOG.H"                  // IDs for dialogs
  42. #include "dmlb.h"                    // DMLB definitions
  43. #define  MSG_UPDATE_SAMPLE           (WM_USER+200)
  44.  
  45. /* General Dialog Helper Macros */
  46. #define CONTROLHWND(ID)         WinWindowFromID(hwnd,ID)                                           
  47. #define INSERTITEM(ID,Text)     (SHORT)WinSendDlgItemMsg(hwnd,ID,LM_INSERTITEM,MPFROMSHORT(LIT_END),Text)
  48. #define INSERTITEMAT(ID,At,Text) (SHORT)WinSendDlgItemMsg(hwnd,ID,LM_INSERTITEM,MPFROMSHORT(At),Text) 
  49. #define DELETEITEM(ID,Index)    WinSendDlgItemMsg(hwnd,ID,LM_DELETEITEM,MPFROMSHORT(Index),0L)     
  50. #define QUERYSELECTION(ID)      (SHORT)WinSendDlgItemMsg(hwnd,ID,LM_QUERYSELECTION,MPFROMSHORT(LIT_FIRST),0L)
  51. #define QUERYITEMCOUNT(ID)      (SHORT)WinSendDlgItemMsg(hwnd,ID,LM_QUERYITEMCOUNT,0L,0L)     
  52. #define QUERYITEMTEXT(ID,Index,Buff,Size) (SHORT)WinSendDlgItemMsg(hwnd,ID,LM_QUERYITEMTEXT,MPFROM2SHORT(Index,Size),MPFROMP(Buff))
  53. #define QUERYITEMTEXTLEN(ID,Index) (SHORT)WinSendDlgItemMsg(hwnd,ID,LM_QUERYITEMTEXTLENGTH,MPFROM2SHORT(Index),0L)
  54. #define SETITEMHANDLE(ID,Index,Hand)  WinSendDlgItemMsg(hwnd,ID,LM_SETITEMHANDLE,MPFROMSHORT(Index),MPFROMLONG(Hand))
  55. #define QUERYITEMHANDLE(ID,Index)  (ULONG)WinSendDlgItemMsg(hwnd,ID,LM_QUERYITEMHANDLE,MPFROMSHORT(Index),0L)
  56. #define SETSELECTION(ID,Index)  WinSendDlgItemMsg(hwnd,ID,LM_SELECTITEM,MPFROMSHORT(Index),MPFROMSHORT(TRUE))
  57. #define SETITEMTEXT(ID,Index,Text)  WinSendDlgItemMsg(hwnd,ID,LM_SETITEMTEXT,MPFROMSHORT(Index),MPFROMP(Text))
  58. #define SETTEXTLIMIT(ID,Size)   WinSendDlgItemMsg(hwnd,ID,EM_SETTEXTLIMIT,MPFROMSHORT(Size),0L)
  59. #define SETTEXT(ID,Buff)        WinSetWindowText(WinWindowFromID(hwnd,ID),Buff)                
  60. #define QUERYTEXT(ID,Buff,Size) WinQueryWindowText(WinWindowFromID(hwnd,ID),Size,Buff)         
  61. #define QUERYTEXTLEN(ID)        WinQueryWindowTextLength(WinWindowFromID(hwnd,ID))             
  62.                                                                                                    
  63. MRESULT EXPENTRY DLG_MAIN_Proc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);                   
  64.  
  65. /*----------------------------------------------------------------------------*/                   
  66. MRESULT EXPENTRY DLG_MAIN_Proc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)                    
  67. /*----------------------------------------------------------------------------*/                   
  68. /* This dialog proc initializes and handles all messages for the main         */
  69. /* dialog.                                                                    */
  70. /*----------------------------------------------------------------------------*/                   
  71. {                                                                                                  
  72. SHORT Item;
  73.  
  74. // If the following is TRUE we generate code to support multiple copies of
  75. // the database fields in the export list.  In this mode:
  76. //   - Items are COPIED when dragged from the database list to the export list
  77. //   - Items are DELETED when dragged from the export list to the database list
  78. //
  79. // If this is FALSE, we generate code such that only one copy of any given
  80. // field is allowed in the export list:
  81. //   - Items are MOVED when dragged from the database to the export list
  82. //   - Items are MOVED when dragged from the export list to the database
  83.  
  84. static BOOL AllowMultiples = TRUE;
  85.                                                                                                    
  86.   switch (msg) {                                                                                   
  87.                                                                                                    
  88.     case WM_INITDLG:                                                                               
  89.  
  90.       /* Build some starter data in both listboxes.  The text in the */
  91.       /* listbox is the field name, and we set the handle of each    */
  92.       /* item to the sample data for that field.                     */
  93.  
  94.       Item = INSERTITEM(LIST2, "Last name");
  95.       SETITEMHANDLE    (LIST2, Item, "McMillan");
  96.       Item = INSERTITEM(LIST2, "First name");
  97.       SETITEMHANDLE    (LIST2, Item, "Mark");
  98.       Item = INSERTITEM(LIST2, "Middle initial");
  99.       SETITEMHANDLE    (LIST2, Item, "A");
  100.       Item = INSERTITEM(LIST2, "Street");
  101.       SETITEMHANDLE    (LIST2, Item, "4922 Appletree Lane");
  102.       Item = INSERTITEM(LIST2, "City");
  103.       SETITEMHANDLE    (LIST2, Item, "Raleigh");
  104.       Item = INSERTITEM(LIST2, "State");
  105.       SETITEMHANDLE    (LIST2, Item, "NC");
  106.       Item = INSERTITEM(LIST2, "Zip code");
  107.       SETITEMHANDLE    (LIST2, Item, "27462");
  108.       Item = INSERTITEM(LIST2, "Customer number");
  109.       SETITEMHANDLE    (LIST2, Item, "382F-3922");
  110.       Item = INSERTITEM(LIST2, "Date of entry");
  111.       SETITEMHANDLE    (LIST2, Item, "03/21/95");
  112.       Item = INSERTITEM(LIST2, "License ID");
  113.       SETITEMHANDLE    (LIST2, Item, "22F84EC1");
  114.       Item = INSERTITEM(LIST2, "Contact method");
  115.       SETITEMHANDLE    (LIST2, Item, "Phone");
  116.       Item = INSERTITEM(LIST2, "E-mail");
  117.       SETITEMHANDLE    (LIST2, Item, "nobody@nowhere.net");
  118.       Item = INSERTITEM(LIST2, "Version");
  119.       SETITEMHANDLE    (LIST2, Item, "1.4.1");
  120.       Item = INSERTITEM(LIST2, "Last upgrade");
  121.       SETITEMHANDLE    (LIST2, Item, "00/00/00");
  122.  
  123.       /* Add direct-manipulation capability to both listboxs */
  124.       DMLBInitialize(CONTROLHWND(LIST1), NULLHANDLE);
  125.       DMLBInitialize(CONTROLHWND(LIST2), NULLHANDLE);
  126.  
  127.       /* Initialize sample display */
  128.       WinSendMsg(hwnd, MSG_UPDATE_SAMPLE, MPVOID, MPVOID);
  129.  
  130.       return FALSE;                                           
  131.  
  132.     case WM_DESTROY:
  133.       /* Free resources */
  134.       break;
  135.  
  136.     case MSG_UPDATE_SAMPLE: {
  137.       /* Update the sample text by extracting the sample data from */
  138.       /* the left listbox item handles.                            */
  139.  
  140.       char BigBuff[400];
  141.       SHORT i;
  142.  
  143.       BigBuff[0] = '\0';
  144.       for (i=0; i<QUERYITEMCOUNT(LIST1); i++) {  // Build sample record
  145.         strcat(BigBuff, (char *)QUERYITEMHANDLE(LIST1, i));
  146.         strcat(BigBuff, " ,");
  147.       }
  148.  
  149.       if (BigBuff[0] != '\0')
  150.         BigBuff[strlen(BigBuff)-1] = '\0';  // Remove trailing delimiter
  151.  
  152.       SETTEXT(SAMPLE, BigBuff);  // Update display
  153.       return 0;
  154.       }
  155.                                                               
  156.     case WM_COMMAND:                                          
  157.       switch (SHORT1FROMMP(mp1)) {                            
  158.         case DID_OK: /*----------~OK (PUSHBUTTON)----------*/ 
  159.           // Let default dialog proc terminate us
  160.           break;                                              
  161.       }                                                       
  162.       break;                                                  
  163.                                                               
  164.     case WM_CONTROL:                                          
  165.       switch SHORT1FROMMP(mp1) {                              
  166.  
  167.         /*******************************************************************/
  168.         /* Notification messages from the listboxs come here.  These are   */
  169.         /* the usual PM listbox messages, plus some new ones for DMLB.     */
  170.         /*******************************************************************/
  171.         case LIST1: /*----------LIST1 (LISTBOX)----------*/
  172.  
  173.           /* Messages from the "export fields" listbox */
  174.  
  175.           switch (SHORT2FROMMP(mp1)) {
  176.             case LN_SELECT:           
  177.             case LN_ENTER:            
  178.               /* User dbl-clicked an entry or pressed ENTER.  For this sample       */
  179.               /* we do nothing.                                                     */
  180.               return 0;
  181.  
  182.             case LN_DMLB_QRYDROP:
  183.               /* A listbox item is about to be dropped on this listbox.  We must    */
  184.               /* return an Ok-to-drop indicator, and tell what to do with the       */
  185.               /* original item (copy or move).                                      */
  186.  
  187.               if (HWNDFROMMP(mp2) == CONTROLHWND(LIST1))  // Always MOVE in my own list
  188.                 return MRFROM2SHORT(TRUE, DROPMODE_MOVE);
  189.  
  190.               if (AllowMultiples)
  191.                 return MRFROM2SHORT(TRUE, DROPMODE_COPY); // Copy from field list
  192.               else
  193.                 return MRFROM2SHORT(TRUE, DROPMODE_MOVE); // Move from field list
  194.  
  195.             case LN_DMLB_REORDERED:
  196.             case LN_DMLB_DELETE:
  197.             case LN_DMLB_DELETE_MOVE:
  198.             case LN_DMLB_INSERT_MOVE:
  199.             case LN_DMLB_INSERT_COPY:
  200.               /* The list of fields has been updated by drag/drop (an item has      */
  201.               /* been moved in the list, dragged to the other list and deleted from */
  202.               /* this one, or dragged from the other list and inserted here).  When */
  203.               /* any of these occure we must update the sample display.  Note we    */
  204.               /* must POST this because DELETE messages are sent before the delete  */
  205.               /* actually occures (so we use POST to delay our update).             */
  206.               WinPostMsg(hwnd, MSG_UPDATE_SAMPLE, MPVOID, MPVOID);
  207.               return 0;
  208.           }                           
  209.           break;                      
  210.  
  211.         case LIST2: /*----------LIST2 (LISTBOX)----------*/
  212.  
  213.           /* Messages from the "all fields" listbox */
  214.  
  215.           switch (SHORT2FROMMP(mp1)) {
  216.             case LN_SELECT:           
  217.             case LN_ENTER:            
  218.               /* User dbl-clicked an entry or pressed ENTER.  For this sample       */
  219.               /* we do nothing.                                                     */
  220.               return 0;
  221.  
  222.             case LN_DMLB_QRYDROP:
  223.               /* A listbox item is about to be dropped on this listbox.  We must    */
  224.               /* return an Ok-to-drop indicator,   and tell what to do with the     */
  225.               /* original item (copy or move).                                      */
  226.  
  227.               if (HWNDFROMMP(mp2) == CONTROLHWND(LIST2))   // Drop on self?
  228.                 return MRFROM2SHORT(FALSE, 0);             // Don't allow
  229.               else {
  230.                 if (AllowMultiples)  // Just delete if our list stays full
  231.                   return MRFROM2SHORT(TRUE, DROPMODE_DELETE);
  232.                 else                 // Move field back to this list
  233.                   return MRFROM2SHORT(TRUE, DROPMODE_MOVE);
  234.               }
  235.           }                           
  236.           break;                      
  237.       }                               
  238.       break; /* End of WM_CONTROL */          
  239.                                               
  240.   } /* End of (msg) switch */                 
  241.                                               
  242.   return WinDefDlgProc(hwnd, msg, mp1, mp2);  
  243.                                               
  244. } /* End dialog procedure */                  
  245.  
  246. /*----------------------------------------------------------------------------*/
  247. int      main(int argc,char **argv, char **envp)                                
  248. /*----------------------------------------------------------------------------*/
  249. /*  Main routine just runs a dialog box (no main window).                     */
  250. /*----------------------------------------------------------------------------*/
  251. {                                                                               
  252. HAB hab;                                                                        
  253. HMQ MyQ;                                                                        
  254.                                                                                 
  255.   /* Initialize PM window environment, get a message queue */                   
  256.                                                                                 
  257.   hab = WinInitialize(0L);                                                      
  258.   MyQ = WinCreateMsgQueue(hab, 0) ;                                             
  259.                                                                                 
  260.   WinDlgBox(HWND_DESKTOP, HWND_DESKTOP, (PFNWP)                                 
  261.             DLG_MAIN_Proc,                                                      
  262.             NULLHANDLE,                                                         
  263.             DLG_MAIN,                                                           
  264.             NULL);                                                              
  265.                                                                                 
  266.   /* Cleanup when dialog terminates */                                          
  267.                                                                                 
  268.   WinDestroyMsgQueue(MyQ);                                                      
  269.   WinTerminate(hab);                                                            
  270.  
  271.   return 0;
  272. }                                                                               
  273.