home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mclb.zip / lb.pak / DMLB.C next >
Text File  |  1995-10-29  |  33KB  |  664 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. /*--------- Direct Manipulation ListBox (DMLB) Subclass Procs -------------*/
  19. /*                                                                         */
  20. /*  Author:            Mark McMillan                                       */
  21. /*                     IBM Corp.                                           */
  22. /*                     Research Triangle Park, NC                          */
  23. /*                     USA                                                 */
  24. /*                                                                         */
  25. /*  Original Concepts: Alan Warren,                                        */
  26. /*                     IBM Yorktown Research                               */
  27. /*                                                                         */
  28. /*  Copyright (c) IBM Corp. 1995                                           */
  29. /*-------------------------------------------------------------------------*/
  30.  
  31. /*===========================================================================
  32.  
  33.                      Direct Manipulation ListBox
  34.  
  35.    This is a set of functions to add direct-manipulation capability to a
  36.    standard PM listbox control.  Several DM functions are provided:
  37.  
  38.      (1) Drag/drop reordering of the list using the system-defined
  39.          drag button (usually button 2).
  40.  
  41.      (2) Drag/drop between different listboxes in the same application.
  42.          Application can control copy/move functions.
  43.  
  44.      (3) Context menu popup capability using the system-define context
  45.          menu action (usually click button 2).
  46.  
  47.    When dragging an item, zones are defined such that the item will
  48.    be inserted at the closest item boundry.  Moving the mouse outside of the
  49.    listbox to the north or south will initiate automatic scrolling in that  
  50.    direction.                                                               
  51.  
  52.    When the context-menu button is selected on a listbox item, the
  53.    application is notified so that it may present a popup context menu.
  54.  
  55.    The application will receive an extended set of WM_CONTROL messages
  56.    giving information about DM-related operations on the listbox and to
  57.    give the application control over inter-listbox drops. 
  58.  
  59.    These functions will also work with a Multi-Column listbox (MCLB).
  60.  
  61.    No additional application logic is required to use this function except
  62.    for a single call to initialize the DM functions for a specific listbox
  63.    control DMLBInitialize().  The window words of the listbox will be used
  64.    by these functions and are not available to the application.  However,
  65.    the after DMLBInitialize() the application may use the first pointer in
  66.    the structured pointed to by the window words.  E.g.,
  67.  
  68.      MyAppPtr = *((void *)WinQueryWindowPtr(LBHwnd, 0L));
  69.  
  70.    The application needs to define the following resources which may be
  71.    bound to the EXE or reside in a DLL (the .PTR files are supplied
  72.    with this toolkit):
  73.  
  74.      POINTER ID_DMLB_DRAGMOVE "lbdrag.ptr"    
  75.      POINTER ID_DMLB_DRAGCOPY "lbdragcp.ptr"  
  76.      POINTER ID_DMLB_DRAGNONE "nodrag.ptr"    
  77.      POINTER ID_DMLB_DRGNORTH "drgnorth.ptr"  
  78.      POINTER ID_DMLB_DRGSOUTH "drgsouth.ptr"  
  79.                                                                       
  80.    Known Limitations:                                                 
  81.  
  82.      - The listbox control window words are used to store per-instance  
  83.        data required by this routine.  The first pointer of that instance
  84.        data is available for application use (initialized to NULL).     
  85.        This data area is not available until after the application has  
  86.        called the DMLBInitialize() function.
  87.  
  88.      - Multiple-select listboxes are not supported.  Only a single item 
  89.        can be selected and moved at a time.  Some additional code logic 
  90.        could be added to overcome this if necessary.                    
  91.  
  92.      - Items can be dragged between listboxes only in the same process. 
  93.  
  94. ===========================================================================*/
  95.                                                                         
  96. /*-------------------------------------------------------------------------*/
  97. /* Updates:                                                                */
  98. /*   06/14/95 - Added capability to handle OWNERDRAW listbox style.        */
  99. /*   06/15/95 - Use system drag messages instead of drag on button 2 dn.   */
  100. /*   06/15/95 - Fixed some bounds checking when LB is empty.               */
  101. /*   06/15/95 - Added context menu capability.                             */
  102. /*   06/15/95 - Added ability to d/d between different list boxes.         */
  103. /*   07/22/95 - Removed need for app to setup subclass, provide all        */
  104. /*              setup/initialization in DMLBInitialize() API.              */
  105. /*   10/24/95 - Insure only one selection for LN_DMLB_INSERT_COPY/MOVE.    */
  106. /*-------------------------------------------------------------------------*/
  107.  
  108. #define  INCL_WIN
  109. #define  INCL_DOS
  110. #define  INCL_PM                               
  111. #define  INCL_DOSMEMMGR
  112. #define  INCL_ERRORS
  113. #include <os2.h>                             
  114.  
  115. /* C standard Library header files   */
  116. #include <string.h>
  117. #include <stdlib.h>
  118. #include <stdio.h>
  119.  
  120. #include "dmlb.h"             // Externalized definitions
  121.  
  122. #define LLI_UNDER       1     // Parameter values for DMLBLocateListboxItem()
  123. #define LLI_INSERTPOINT 2
  124.  
  125. #define POINT_INSIDE    1     /* Pointer is inside a drop-accepting listbox */
  126. #define POINT_OUTSIDE   2     /* Not in a drop-accepting listbox            */
  127. #define POINT_NORTH     3     /* Just north of good listbox, scroll it      */
  128. #define POINT_SOUTH     4     /* Just south of good listbox, scroll it      */
  129. #define DRAG_TIMERID     TID_USERMAX-1  /* Timer ID */
  130.  
  131. #define LISTBOX_CLASS_STRING "#7"       /* Requst of WinQueryClassName()   */
  132.  
  133. typedef struct DMLBDataStruct {          /* Per-instance listbox data       */
  134.        void     *AppData;               /* Place for application data      */
  135.        HPOINTER DragMIcon;              /* Pointer icon for dragging (move)*/
  136.        HPOINTER DragCIcon;              /* Pointer icon for dragging (copy)*/
  137.        HPOINTER DragNoDrp;              /* Pointer icon for dragging (none)*/
  138.        HPOINTER DeletIcon;              /* Pointer icon for draggind (del) */
  139.        HPOINTER NorthIcon, SouthIcon;   /* Up/down pointer icons           */
  140.        HMODULE  ResHMod;                /* Module with ptr resources       */
  141.        PFNWP    OldProcAddr;            /* Previous proc for this listbox  */
  142.        HWND     TargetHwnd;             /* Handle of drop-target listbox   */
  143.        USHORT   TargetDropMode;         /* Type of drop target requests    */
  144.        SHORT    PrevLocation;           /* Last known location of pointer  */
  145.        BOOL     Dragging;               /* Drag is currently in progress   */
  146.        } DMLBData, *PDMLBData;
  147.  
  148. /* Internal prototypes */
  149. void DMLBCheckTargetLocation(HWND SrcHwnd, DMLBData *InstData, SHORT X, SHORT Y);
  150. SHORT DMLBLocateListboxItem( HWND hwnd, HWND RelHwnd, SHORT Y, SHORT Option );
  151. MRESULT EXPENTRY DMLBSubclassListboxProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2);
  152.  
  153. /*----------------------------------------------------------------------*/
  154. BOOL EXPENTRY DMLBInitialize(HWND LBHwnd, HMODULE ResourceHMod)
  155. /*----------------------------------------------------------------------*/
  156. /* Setup direct-manipulation capabilites on given listbox.  This        */
  157. /* function takes care of creating the instance data needed by the      */
  158. /* subclass procedure, and subclassing the listbox window.  This is     */
  159. /* the only external API for DMLB.                                      */
  160. /*----------------------------------------------------------------------*/
  161. {
  162. DMLBData *InstData;
  163.  
  164.   /* Create and initialize listbox instance data.  Note that we use */
  165.   /* the listbox window words for a ptr to our instance data.  We   */
  166.   /* preserve the current value of the window words in the first    */
  167.   /* PVOID of our instance data where the app can use it.           */
  168.  
  169.   InstData = malloc(sizeof(DMLBData));
  170.   memset(InstData, 0x00, sizeof(DMLBData));
  171.   InstData->AppData      = WinQueryWindowPtr(LBHwnd, QWL_USER);
  172.   InstData->TargetHwnd   = LBHwnd;         // Inital target is self
  173.   InstData->TargetDropMode= DROPMODE_MOVE; // Default drop mode is MOVE
  174.   InstData->ResHMod      = ResourceHMod;   // Module to load ptr resources
  175.  
  176.   /* Note: must set this before subclassing since the subclass */
  177.   /* proc assumes it is set.                                   */
  178.  
  179.   WinSetWindowPtr(LBHwnd, QWL_USER, InstData);
  180.   InstData->OldProcAddr = WinSubclassWindow(LBHwnd, (PFNWP)DMLBSubclassListboxProc);
  181.   if (InstData->OldProcAddr == NULL) {
  182.     free(InstData);
  183.     return FALSE;
  184.   }
  185.   return TRUE;
  186. }
  187.  
  188. /*----------------------------------------------------------------------*/
  189. MRESULT EXPENTRY DMLBSubclassListboxProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  190. /*----------------------------------------------------------------------*/
  191. /* This window procedure is used to subclass a standard PM listbox      */
  192. /* control.  This procedure will intercept certain mouse events on the  */
  193. /* listbox to implement direct-manipulation functions.                  */
  194. /*----------------------------------------------------------------------*/
  195. {
  196. SHORT    Item;          /* Listbox item number                          */
  197. DMLBData  *InstData;     /* This instance-specific data (per listbox)    */
  198.  
  199.    /* The lisbox window pointer is to our instance data. */
  200.  
  201.    InstData = WinQueryWindowPtr(hwnd, QWL_USER);
  202.      
  203.    switch (msg) {
  204.       /* Since this is just a subclass setup after the listbox window */
  205.       /* is created, we never get a WM_CREATE message here.           */
  206.  
  207.       case WM_DESTROY:
  208.            /* The listbox window is being destroyed.  Cleanup any     */
  209.            /* resources we have allocated in this subclass.           */
  210.            if (InstData->DragMIcon != NULLHANDLE) {
  211.              WinDestroyPointer(InstData->DragMIcon);
  212.              WinDestroyPointer(InstData->DragCIcon);
  213.              WinDestroyPointer(InstData->NorthIcon);
  214.              WinDestroyPointer(InstData->SouthIcon);
  215.              WinDestroyPointer(InstData->DragNoDrp);
  216.              WinDestroyPointer(InstData->DeletIcon);
  217.              InstData->DragMIcon = NULLHANDLE;
  218.            }
  219.            /* Cleanup other resources */
  220.            if (InstData->Dragging) {
  221.              WinStopTimer(WinQueryAnchorBlock(hwnd),hwnd,DRAG_TIMERID);
  222.            }
  223.            /* Release instance data */
  224.            free(InstData);
  225.            WinSetWindowPtr(hwnd, 0L, NULL);
  226.            break;
  227.  
  228.       case WM_TIMER:
  229.            /* We get timer messages during dragging to implement */
  230.            /* auto-scrolling of listbox when pointer is placed   */
  231.            /* north or south of the listbox while dragging.      */
  232.  
  233.            if (!InstData->Dragging)  /* Ignore if not dragging   */
  234.              break;
  235.  
  236.            if (SHORT1FROMMP(mp1)==DRAG_TIMERID) {
  237.              switch (InstData->PrevLocation) { // Last known location of the pointer
  238.                case POINT_INSIDE:
  239.                case POINT_OUTSIDE:
  240.                  /* Do nothing */
  241.                  break;
  242.  
  243.                case POINT_NORTH:
  244.                  /* Scroll up one item */
  245.                  Item = (SHORT)WinSendMsg(InstData->TargetHwnd, LM_QUERYTOPINDEX,  0L, 0L);
  246.                  if ((Item != LIT_NONE) && (Item != 0))
  247.                     WinPostMsg(InstData->TargetHwnd, LM_SETTOPINDEX, MPFROMSHORT(Item-1), 0L);
  248.                  break;
  249.  
  250.                case POINT_SOUTH:
  251.                  /* Scroll down one item */
  252.                  Item = (SHORT)WinSendMsg(InstData->TargetHwnd, LM_QUERYTOPINDEX,  0L, 0L);
  253.                  if (Item != LIT_NONE)
  254.                     WinPostMsg(InstData->TargetHwnd, LM_SETTOPINDEX, MPFROMSHORT(Item+1), 0L);
  255.                  break;
  256.              } /* switch on PrevLocation */
  257.              return 0;
  258.            }
  259.            break;
  260.  
  261.       case WM_CONTEXTMENU: {
  262.            SHORT CursorIndx, Max;
  263.  
  264.            /* User requested context menu... notify our owner.          */
  265.  
  266.            /* First find out what item the pointer is over.             */
  267.            Max = (SHORT)WinSendMsg( hwnd, LM_QUERYITEMCOUNT, 0L, 0L );
  268.            CursorIndx = DMLBLocateListboxItem(hwnd, hwnd, SHORT2FROMMP(mp1), LLI_UNDER);
  269.            if ((Max == 0) || (CursorIndx+1 > Max))
  270.              CursorIndx = LIT_NONE;
  271.  
  272.            /* Tell our owner about it */
  273.            return WinSendMsg(WinQueryWindow(hwnd, QW_OWNER), WM_CONTROL,
  274.                       MPFROM2SHORT(WinQueryWindowUShort(hwnd, QWS_ID), LN_DMLB_CONTEXT),
  275.                       MPFROMSHORT(CursorIndx));
  276.            }
  277.  
  278.       case WM_MOUSEMOVE:
  279.            /* Monitor the position of the mouse relative to the listbox */
  280.            /* so we can set the pointer icon correctly and note the     */
  281.            /* position for use during WM_TIMER processing.              */
  282.  
  283.            if (!InstData->Dragging)   /* Ignore if not dragging */
  284.              break;
  285.  
  286.            DMLBCheckTargetLocation(hwnd, InstData, SHORT1FROMMP(mp1), SHORT2FROMMP(mp1));
  287.  
  288.            /* Set pointer icon appropriate for location */
  289.            if (InstData->DragMIcon == NULLHANDLE) {
  290.              // Load all the pointers (one time only)
  291.              InstData->DragMIcon= WinLoadPointer(HWND_DESKTOP, InstData->ResHMod, ID_DMLB_DRAGMOVE);
  292.              InstData->DragCIcon= WinLoadPointer(HWND_DESKTOP, InstData->ResHMod, ID_DMLB_DRAGCOPY);
  293.              InstData->DragNoDrp= WinLoadPointer(HWND_DESKTOP, InstData->ResHMod, ID_DMLB_DRAGNONE);
  294.              InstData->NorthIcon= WinLoadPointer(HWND_DESKTOP, InstData->ResHMod, ID_DMLB_DRGNORTH);
  295.              InstData->SouthIcon= WinLoadPointer(HWND_DESKTOP, InstData->ResHMod, ID_DMLB_DRGSOUTH);
  296.              InstData->DeletIcon= WinLoadPointer(HWND_DESKTOP, InstData->ResHMod, ID_DMLB_DRGDEL);
  297.            }
  298.            switch (InstData->PrevLocation) {
  299.              case POINT_INSIDE:
  300.                switch (InstData->TargetDropMode) {
  301.                case DROPMODE_MOVE:
  302.                  WinSetPointer(HWND_DESKTOP, InstData->DragMIcon); // Use MOVE pointer 
  303.                  break;
  304.                case DROPMODE_COPY:
  305.                  WinSetPointer(HWND_DESKTOP, InstData->DragCIcon); // Use COPY pointer
  306.                  break;
  307.                case DROPMODE_DELETE:
  308.                  WinSetPointer(HWND_DESKTOP, InstData->DeletIcon); // Use DELETE pointer
  309.                  break;
  310.                }
  311.                break;
  312.              case POINT_OUTSIDE:
  313.                WinSetPointer(HWND_DESKTOP, InstData->DragNoDrp);   // No-drop pointer
  314.                break;
  315.              case POINT_NORTH:
  316.                WinSetPointer(HWND_DESKTOP, InstData->NorthIcon);   // Scroll-up pointer
  317.                break;
  318.              case POINT_SOUTH:
  319.                WinSetPointer(HWND_DESKTOP, InstData->SouthIcon);   // Scroll-down poineter
  320.                break;
  321.            }
  322.  
  323.            return (MRESULT)TRUE;  /* Note we processed the message */
  324.  
  325.       case WM_BEGINDRAG:
  326.            {
  327.            SHORT  Max;
  328.            SHORT i, CursorIndx, hit;
  329.  
  330.            /* User started dragging with the pointer on our window */
  331.  
  332.            Max = (SHORT)WinSendMsg( hwnd, LM_QUERYITEMCOUNT, 0L, 0L );
  333.  
  334.            /* If we are currently dragging, cancel it (should not happen) */
  335.  
  336.            if ( InstData->Dragging ) {
  337.              InstData->Dragging = FALSE;
  338.              WinSetCapture( HWND_DESKTOP, NULLHANDLE );
  339.              return (MRESULT)FALSE;
  340.            }
  341.  
  342.            /* Get index of item under the mouse pointer and check */
  343.            /* for reasonable numeric bounds.                      */
  344.  
  345.            CursorIndx = DMLBLocateListboxItem(hwnd, hwnd, SHORT2FROMMP(mp1), LLI_UNDER);
  346.            if ((Max == 0) || (CursorIndx+1 > Max)) {
  347.              DosBeep( 440L, 50L );  // Don't allow drag if not on a listbox item
  348.              return (MRESULT)FALSE;
  349.            }
  350.  
  351.            /* Since we currently support dragging only a single item, */
  352.            /* de-select all items and just select the one under the   */
  353.            /* pointer.  To support multiple-drag we would probably    */
  354.            /* need to notify the owner so they could set the selection*/
  355.            /* status of all items to be dragged (which may or may not */
  356.            /* include the item under the pointer).                    */
  357.  
  358.            WinSendMsg(hwnd, LM_SELECTITEM, MPFROMSHORT(LIT_NONE), MPVOID);
  359.            WinSendMsg(hwnd, LM_SELECTITEM, MPFROMSHORT(CursorIndx), MPFROMSHORT(TRUE));
  360.  
  361.            /* Note we are now dragging and capture the pointer. */
  362.  
  363.            InstData->Dragging = TRUE;
  364.            WinSetCapture( HWND_DESKTOP, hwnd );
  365.            InstData->PrevLocation = POINT_INSIDE;
  366.            WinStartTimer(WinQueryAnchorBlock(hwnd),hwnd,DRAG_TIMERID, WinQuerySysValue(HWND_DESKTOP, SV_SCROLLRATE));
  367.            return (MRESULT)TRUE;
  368.            break;
  369.            }
  370.  
  371.       case WM_ENDDRAG:
  372.             {
  373.             SHORT DropIndx, CurrIndx;
  374.             SHORT SourceMax, TargetMax;      /* Num of items in source/target listbox */
  375.             char  *CopyText;                 /* Text to be copied/moved */
  376.             USHORT CopyTextLen;              /* Length of text */
  377.             void   *CopyHand;                /* Handle of item to be copied/moved */
  378.             BOOL  SameList = FALSE;          /* Source and target are same listbox */
  379.  
  380.             if (!InstData->Dragging)         /* Ignore if we are not dragging */
  381.               return (MRESULT)FALSE;
  382.  
  383.             /* Clear dragging indicators and release pointer */
  384.  
  385.             InstData->Dragging = FALSE;
  386.             WinSetCapture( HWND_DESKTOP, NULLHANDLE );
  387.             WinStopTimer(WinQueryAnchorBlock(hwnd),hwnd,DRAG_TIMERID);
  388.  
  389.             /* See if what is under the pointer will accept the drop */
  390.             DMLBCheckTargetLocation(hwnd, InstData, SHORT1FROMMP(mp1), SHORT2FROMMP(mp1));
  391.             if (InstData->PrevLocation != POINT_INSIDE)
  392.               return (MRESULT)TRUE;  /* Ignore drop outside a good listbox */
  393.  
  394.             if (hwnd == InstData->TargetHwnd)  // Source and target are same listbox
  395.               SameList = TRUE;
  396.  
  397.             SourceMax = (SHORT)WinSendMsg(hwnd, LM_QUERYITEMCOUNT, 0L, 0L ) -1;
  398.             TargetMax = (SHORT)WinSendMsg(InstData->TargetHwnd, LM_QUERYITEMCOUNT, 0L, 0L ) -1;
  399.  
  400.             /* Get drop point and original selected point */
  401.  
  402.             DropIndx = DMLBLocateListboxItem(InstData->TargetHwnd, hwnd, SHORT2FROMMP(mp1), LLI_INSERTPOINT);
  403.             CurrIndx = (SHORT)WinSendMsg(hwnd, LM_QUERYSELECTION, MPFROMSHORT(LIT_FIRST), 0L);
  404.  
  405.             /* Prevent move onto same item as source, in same listbox           */
  406.             /* being careful of DropIndx > SourceMax when drop after last item. */
  407.             if ((InstData->TargetDropMode==DROPMODE_MOVE) &&
  408.                 (SameList) &&
  409.                 ((min(DropIndx,SourceMax) == CurrIndx) || (DropIndx == CurrIndx+1))) {
  410.               DosBeep( 700L, 50L );  /* Don't drop before or after original */
  411.               return (MRESULT)TRUE;
  412.             }
  413.  
  414.             /* Make a copy of original to insert */
  415.  
  416.             CopyTextLen = (SHORT)WinSendMsg(hwnd,LM_QUERYITEMTEXTLENGTH,MPFROMSHORT(CurrIndx), 0L) + 1;
  417.             CopyText = malloc(CopyTextLen);
  418.             WinSendMsg(hwnd, LM_QUERYITEMTEXT, MPFROM2SHORT(CurrIndx, CopyTextLen), MPFROMP(CopyText));
  419.             CopyHand = WinSendMsg(hwnd, LM_QUERYITEMHANDLE, MPFROMSHORT(CurrIndx), 0L);
  420.  
  421.             /* Insert before insertion point, or at end of list */
  422.             if (DropIndx > TargetMax)
  423.               DropIndx = LIT_END;
  424.  
  425.             /* Disable update during insert/delete for smoother visual and */
  426.             /* prevent ownerdraw from occuring before new handles are set. */
  427.  
  428.             WinEnableWindowUpdate(hwnd, FALSE);
  429.             WinEnableWindowUpdate(InstData->TargetHwnd, FALSE);
  430.             
  431.             /* Insert into target list */
  432.             if (InstData->TargetDropMode != DROPMODE_DELETE) {
  433.               DropIndx = (SHORT)WinSendMsg(InstData->TargetHwnd, LM_INSERTITEM, MPFROMSHORT(DropIndx), MPFROMP(CopyText));
  434.               WinSendMsg(InstData->TargetHwnd, LM_SETITEMHANDLE, MPFROMSHORT(DropIndx), MPFROMP(CopyHand));
  435.             }
  436.             free(CopyText);
  437.  
  438.             /* Tell owner of originating listbox what we are doing.  We must notify */
  439.             /* the owner before we delete items because they may keep dynamic data  */
  440.             /* in the item handles that has to be freed.  The item in question is   */
  441.             /* the currently selected item in the listbox.                          */
  442.  
  443.             switch (InstData->TargetDropMode) {
  444.               case DROPMODE_MOVE:
  445.                  if (!SameList)
  446.                    WinSendMsg(WinQueryWindow(hwnd, QW_OWNER), WM_CONTROL,
  447.                            MPFROM2SHORT(WinQueryWindowUShort(hwnd, QWS_ID), LN_DMLB_DELETE_MOVE),
  448.                            MPFROMHWND(InstData->TargetHwnd));
  449.                  break;
  450.               case DROPMODE_DELETE:
  451.                  WinSendMsg(WinQueryWindow(hwnd, QW_OWNER), WM_CONTROL,
  452.                            MPFROM2SHORT(WinQueryWindowUShort(hwnd, QWS_ID), LN_DMLB_DELETE),
  453.                            MPFROMHWND(InstData->TargetHwnd));
  454.                  break;
  455.             }
  456.  
  457.             /* If this is a move, delete original.  If it is in the same  */
  458.             /* listbox as target, get new index since it may have         */
  459.             /* changed due to inserted copy.                              */
  460.  
  461.             if ((InstData->TargetDropMode == DROPMODE_MOVE) || (InstData->TargetDropMode == DROPMODE_DELETE)) {
  462.               CurrIndx = (SHORT)WinSendMsg(hwnd, LM_QUERYSELECTION, MPFROMSHORT(LIT_FIRST), 0L);
  463.               WinSendMsg(hwnd, LM_DELETEITEM, MPFROMSHORT(CurrIndx), 0L);
  464.             }
  465.  
  466.             /* Select the newly inserted item.  If the old copy was             */
  467.             /* above it in the same list, then the item number has changed by 1 */
  468.  
  469.             if ((DropIndx > CurrIndx) && (SameList) && (InstData->TargetDropMode==DROPMODE_MOVE))
  470.               DropIndx--;
  471.             if (InstData->TargetDropMode != DROPMODE_DELETE) {
  472.               WinSendMsg(InstData->TargetHwnd, LM_SELECTITEM, MPFROMSHORT(LIT_NONE), MPFROMSHORT(FALSE));
  473.               WinSendMsg(InstData->TargetHwnd, LM_SELECTITEM, MPFROMSHORT(DropIndx), MPFROMSHORT(TRUE));
  474.             }
  475.  
  476.             WinEnableWindowUpdate(hwnd, TRUE);
  477.             WinEnableWindowUpdate(InstData->TargetHwnd, TRUE);
  478.  
  479.             /* Notify target of inserted items if necessary */
  480.  
  481.             switch (InstData->TargetDropMode) {
  482.               case DROPMODE_MOVE:
  483.                 if (SameList)
  484.                    WinSendMsg(WinQueryWindow(InstData->TargetHwnd, QW_OWNER), WM_CONTROL,
  485.                          MPFROM2SHORT(WinQueryWindowUShort(InstData->TargetHwnd, QWS_ID), LN_DMLB_REORDERED),
  486.                          MPFROMHWND(hwnd));
  487.                 else
  488.                    WinSendMsg(WinQueryWindow(InstData->TargetHwnd, QW_OWNER), WM_CONTROL,
  489.                          MPFROM2SHORT(WinQueryWindowUShort(InstData->TargetHwnd, QWS_ID), LN_DMLB_INSERT_MOVE),
  490.                          MPFROMHWND(hwnd));
  491.                 break;
  492.               case DROPMODE_COPY:
  493.                 WinSendMsg(WinQueryWindow(InstData->TargetHwnd, QW_OWNER), WM_CONTROL,
  494.                          MPFROM2SHORT(WinQueryWindowUShort(InstData->TargetHwnd, QWS_ID), LN_DMLB_INSERT_COPY),
  495.                          MPFROMHWND(hwnd));
  496.                 break;
  497.             }
  498.  
  499.             return (MRESULT)TRUE;
  500.             }
  501.  
  502.       }
  503.  
  504.    /* Call previous window procedure to process this message */
  505.    return ( (*(InstData->OldProcAddr)) ( hwnd, msg, mp1, mp2 )  );
  506.    }
  507.  
  508.  
  509. /*----------------------------------------------------------------------*/
  510. SHORT DMLBLocateListboxItem( HWND hwnd, HWND RelHwnd, SHORT Y, SHORT Option )
  511. /*----------------------------------------------------------------------*/
  512. /* Parameters:                                                          */
  513. /*   hwnd         Listbox of interest                                   */
  514. /*   RelHwnd      Window to which Y is relative to                      */
  515. /*   Y            Y position of the pointer                             */
  516. /*   Option       LLI_UNDER to return index of item under pointer,      */
  517. /*                LLI_INSERTPOINT to index of item nearest insert point */
  518. /*----------------------------------------------------------------------*/
  519. /* Given a Y window coordinate, this function will calculate the item   */
  520. /* number of the listbox item at that position.  The LLI_UNDER option   */
  521. /* will return the item directly under the pointer.  The LLI_INSERTPOINT*/
  522. /* option will return the item number *before* which an insertion should*/
  523. /* be made (drop zones are calculated as the half-way points through    */
  524. /* each item).                                                          */
  525. /*----------------------------------------------------------------------*/
  526. {
  527.    RECTL   Rect;
  528.    POINTL  Points[2];
  529.    HPS     hps;
  530.    LONG    VertSize;
  531.    SHORT   ItemNum;
  532.    char    ClassName[10];
  533.  
  534.    /* Map coordinate from window it is relative to, to win of interst*/
  535.    Points[0].x = 0;
  536.    Points[0].y = Y;
  537.    WinMapWindowPoints(RelHwnd, hwnd, &(Points[0]), 1L);
  538.    Y = Points[0].y;
  539.  
  540.    /* If this is actually a MultiColumn ListBox, we must find the    */
  541.    /* real listbox which is the first column and use it for our      */
  542.    /* location calculations.  Since we only care about the Y         */
  543.    /* coordinate we only need to look at the 1st column of the MCLB. */
  544.  
  545.    WinQueryClassName(hwnd, sizeof(ClassName), ClassName);
  546.    if (!strcmp("MCLBCls", ClassName))
  547.      hwnd = WinWindowFromID(hwnd, 1);   /* Get handle of 1st column's listbox */
  548.  
  549.    /* If this is an OWNERDRAW listbox, ask owner how big items are.  */
  550.  
  551.    VertSize = 0;
  552.    if (WinQueryWindowULong(hwnd, QWL_STYLE) & LS_OWNERDRAW)
  553.      VertSize = SHORT1FROMMR(                               // Take returned Height
  554.                 WinSendMsg(WinQueryWindow(hwnd, QW_OWNER),  // Query owner of listbox
  555.                    WM_MEASUREITEM,                          // Ask owner the size
  556.                    MPFROMSHORT(WinQueryWindowUShort(hwnd, QWS_ID)),  // Listbox ID
  557.                    MPFROMSHORT(0)));                        // First item
  558.  
  559.    if (VertSize == 0) {
  560.      /* For a normal listbox, items are the size of the font.  To */
  561.      /* determine the size we get the bounding box of a space.    */
  562.      hps = WinGetPS(hwnd);
  563.      GpiQueryTextBox(hps, 1L, " ", 2, Points);
  564.      VertSize = Points[TXTBOX_TOPLEFT].y - Points[TXTBOX_BOTTOMLEFT].y;
  565.      WinReleasePS(hps);
  566.    }
  567.  
  568.    WinQueryWindowRect(hwnd, &Rect);
  569.    Rect.yTop = Rect.yTop-2;         /* Listbox frame is 2 pixels */
  570.  
  571.    /* Calculate item number of item under the pointer */
  572.    ItemNum = (SHORT)WinSendMsg(hwnd, LM_QUERYTOPINDEX, 0L, 0L)
  573.              + ((Rect.yTop-Y)/VertSize);
  574.  
  575.    /* Return item under pointer, or insertion point */
  576.  
  577.    switch (Option) {
  578.      case LLI_UNDER:
  579.        return ItemNum;
  580.      case LLI_INSERTPOINT:
  581.        if (((Rect.yTop-Y) % VertSize) <= (VertSize / 2))
  582.          return ItemNum;
  583.        return ItemNum+1;  /* Note: May exceed num of items in list */
  584.    }
  585. }
  586.  
  587.  
  588. /*----------------------------------------------------------------------*/
  589. void DMLBCheckTargetLocation(HWND SrcHwnd, DMLBData *InstData, SHORT X, SHORT Y)
  590. /*----------------------------------------------------------------------*/
  591. /* X,Y is in SrcHwnd window coordinates.                                */
  592. /* Given an Y,Y coordinate of the pointer, determine if the pointer is  */
  593. /* over a listbox that will accept a drop.  If it is, we save the       */
  594. /* handle of the listbox and set the PrevLocation to POINT_INSIDE in    */
  595. /* the instance data.  Otherwise, we see if the position is near the    */
  596. /* top/bottom edge of the last good target listbox we had.  If so, set  */
  597. /* the PrevLocation to POINT_NORTH/SOUTH as appropriate.  Finally, if   */
  598. /* none of the above, just set the PrevLocation to POINT_OUTSIDE.       */
  599. /*----------------------------------------------------------------------*/
  600. {
  601. RECTL Rect;
  602. POINTL ScreenPoint;
  603. HWND  OverHwnd;
  604. char  OverClass[10];
  605.  
  606.   InstData->PrevLocation = POINT_OUTSIDE; // Assume this until we know otherwise
  607.  
  608.   /* See if window under the pointer is a listbox.             */
  609.   ScreenPoint.x = X;
  610.   ScreenPoint.y = Y;
  611.   WinMapWindowPoints(SrcHwnd, HWND_DESKTOP, &ScreenPoint, 1L);
  612.   OverHwnd = WinWindowFromPoint(HWND_DESKTOP, &ScreenPoint, TRUE);
  613.   WinQueryClassName(OverHwnd, sizeof(OverClass), OverClass);
  614.  
  615.   if (!strcmp(OverClass, LISTBOX_CLASS_STRING)) {
  616.     /* Yes, it is a listbox -- see if it will accept our drop */
  617.     MRESULT Answer;
  618.  
  619.     /* If this is part of an MCLB, the MCLB is really the target */
  620.     WinQueryClassName(WinQueryWindow(OverHwnd, QW_OWNER), sizeof(OverClass), OverClass);
  621.     if (!strcmp(OverClass, "MCLBCls"))
  622.       OverHwnd = WinQueryWindow(OverHwnd, QW_OWNER);
  623.  
  624.     Answer = WinSendMsg(WinQueryWindow(OverHwnd, QW_OWNER),
  625.                         WM_CONTROL,
  626.                         MPFROM2SHORT(WinQueryWindowUShort(OverHwnd, QWS_ID), LN_DMLB_QRYDROP),
  627.                         MPFROMHWND(SrcHwnd));
  628.     if ((BOOL)SHORT1FROMMR(Answer)) {
  629.       /* Yes, it will accept the drop */
  630.       InstData->TargetHwnd = OverHwnd;                 // Note new target
  631.    // if (SrcHwnd == OverHwnd)
  632.    //   InstData->TargetDropMode = DROPMODE_MOVE;      // Force MOVE for same-listbox drops
  633.    // else
  634.         InstData->TargetDropMode = SHORT2FROMMR(Answer); // Copy, Move, or Delete
  635.       InstData->PrevLocation = POINT_INSIDE;           // We are inside the listbox now
  636.       return;
  637.     }
  638.     /* It will not accept a drop, proceed as point outside */
  639.     return;
  640.   }
  641.  
  642.   /* Not a listbox class window.  See if pointer is near north/south */
  643.   /* edge of last good target listbox.  ("Near" is defined as the    */
  644.   /* height of a menu bar).                                          */
  645.  
  646.   WinQueryWindowRect(InstData->TargetHwnd, &Rect);
  647.   /* Translate rectangle to screen coordinates */
  648.   WinMapWindowPoints(InstData->TargetHwnd, HWND_DESKTOP, (POINTL *)&Rect, 2L);
  649.  
  650.   if ((ScreenPoint.x >= Rect.xLeft) && (ScreenPoint.x <= Rect.xRight)) {
  651.     if ((ScreenPoint.y < Rect.yTop + WinQuerySysValue(HWND_DESKTOP, SV_CYMENU)) &&
  652.         (ScreenPoint.y > Rect.yBottom - WinQuerySysValue(HWND_DESKTOP, SV_CYMENU))) {
  653.        if (ScreenPoint.y > Rect.yTop)
  654.          InstData->PrevLocation = POINT_NORTH;
  655.        else if (ScreenPoint.y < Rect.yBottom)
  656.          InstData->PrevLocation = POINT_SOUTH;
  657.        return;
  658.     } // Within a menu-size distance of north/south edge
  659.   } // Within the horizontal boundries of the listbox
  660.  
  661.   /* Pointer is outside the range of interest */
  662.   return;
  663. }
  664.