4 * Copyright 1999 Eric Kohl
5 * Copyright 2004 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Mar. 10, 2004, by Robert Shearman.
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features or bugs please note them below.
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(commctrl);
45 #define DRAGLIST_SUBCLASSID 0
46 #define DRAGLIST_SCROLLPERIOD 200
47 #define DRAGLIST_TIMERID 666
49 /* properties relating to IDI_DRAGICON */
50 #define DRAGICON_HOTSPOT_X 17
51 #define DRAGICON_HOTSPOT_Y 7
52 #define DRAGICON_HEIGHT 32
54 /* internal Wine specific data for the drag list control */
55 typedef struct _DRAGLISTDATA
57 /* are we currently in dragging mode? */
60 /* cursor to use as determined by DL_DRAGGING notification.
61 * NOTE: as we use LoadCursor we don't have to use DeleteCursor
62 * when we are finished with it */
65 /* optimisation so that we don't have to load the cursor
66 * all of the time whilst dragging */
67 LRESULT last_dragging_response;
69 /* prevents flicker with drawing drag arrow */
70 RECT last_drag_icon_rect;
73 UINT uDragListMessage = 0; /* registered window message code */
74 static DWORD dwLastScrollTime = 0;
75 static HICON hDragArrow = NULL;
77 /***********************************************************************
78 * DragList_Notify (internal)
80 * Sends notification messages to the parent control. Note that it
81 * does not use WM_NOTIFY like the rest of the controls, but a registered
84 static LRESULT DragList_Notify(HWND hwndLB, UINT uNotification)
88 dli.uNotification = uNotification;
89 GetCursorPos(&dli.ptCursor);
90 return SendMessageW(GetParent(hwndLB), uDragListMessage, GetDlgCtrlID(hwndLB), (LPARAM)&dli);
93 /* cleans up after dragging */
94 static void DragList_EndDrag(HWND hwnd, DRAGLISTDATA * data)
96 KillTimer(hwnd, DRAGLIST_TIMERID);
98 /* clear any drag insert icon present */
99 InvalidateRect(GetParent(hwnd), &data->last_drag_icon_rect, TRUE);
100 /* clear data for next use */
101 memset(data, 0, sizeof(*data));
104 /***********************************************************************
105 * DragList_SubclassWindowProc (internal)
107 * Handles certain messages to enable dragging for the ListBox and forwards
108 * the rest to the ListBox.
110 static LRESULT CALLBACK
111 DragList_SubclassWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
113 DRAGLISTDATA * data = (DRAGLISTDATA*)dwRefData;
118 data->dragging = DragList_Notify(hwnd, DL_BEGINDRAG);
122 SetTimer(hwnd, DRAGLIST_TIMERID, DRAGLIST_SCROLLPERIOD, NULL);
124 /* note that we don't absorb this message to let the list box
125 * do its thing (normally selecting an item) */
130 /* user cancelled drag by either right clicking or
131 * by pressing the escape key */
132 if ((data->dragging) &&
133 ((uMsg == WM_RBUTTONDOWN) || (wParam == VK_ESCAPE)))
135 /* clean up and absorb message */
136 DragList_EndDrag(hwnd, data);
137 DragList_Notify(hwnd, DL_CANCELDRAG);
146 LRESULT cursor = DragList_Notify(hwnd, DL_DRAGGING);
147 /* optimisation so that we don't have to load the cursor
148 * all of the time whilst dragging */
149 if (data->last_dragging_response != cursor)
154 data->cursor = LoadCursorW(NULL, (LPCWSTR)IDC_NO);
155 SetCursor(data->cursor);
158 data->cursor = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_COPY);
159 SetCursor(data->cursor);
162 data->cursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
163 SetCursor(data->cursor);
166 data->last_dragging_response = cursor;
168 /* don't pass this message on to List Box */
176 DragList_EndDrag(hwnd, data);
177 DragList_Notify(hwnd, DL_DROPPED);
182 /* tell dialog boxes that we want to receive WM_KEYDOWN events
183 * for keys like VK_ESCAPE */
185 return DLGC_WANTALLKEYS;
188 RemoveWindowSubclass(hwnd, DragList_SubclassWindowProc, DRAGLIST_SUBCLASSID);
192 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
195 /***********************************************************************
196 * MakeDragList (COMCTL32.13)
198 * Makes a normal ListBox into a DragList by subclassing it.
204 BOOL WINAPI MakeDragList (HWND hwndLB)
206 DRAGLISTDATA *data = Alloc(sizeof(DRAGLISTDATA));
208 TRACE("(%p)\n", hwndLB);
210 if (!uDragListMessage)
211 uDragListMessage = RegisterWindowMessageW(DRAGLISTMSGSTRINGW);
213 return SetWindowSubclass(hwndLB, DragList_SubclassWindowProc, DRAGLIST_SUBCLASSID, (DWORD_PTR)data);
216 /***********************************************************************
217 * DrawInsert (COMCTL32.15)
219 * Draws insert arrow by the side of the ListBox item in the parent window.
224 VOID WINAPI DrawInsert (HWND hwndParent, HWND hwndLB, INT nItem)
226 RECT rcItem, rcListBox, rcDragIcon;
230 TRACE("(%p %p %d)\n", hwndParent, hwndLB, nItem);
233 hDragArrow = LoadIconW(COMCTL32_hModule, (LPCWSTR)IDI_DRAGARROW);
235 if (LB_ERR == SendMessageW(hwndLB, LB_GETITEMRECT, nItem, (LPARAM)&rcItem))
238 if (!GetWindowRect(hwndLB, &rcListBox))
241 /* convert item rect to parent co-ordinates */
242 if (!MapWindowPoints(hwndLB, hwndParent, (LPPOINT)&rcItem, 2))
245 /* convert list box rect to parent co-ordinates */
246 if (!MapWindowPoints(HWND_DESKTOP, hwndParent, (LPPOINT)&rcListBox, 2))
249 rcDragIcon.left = rcListBox.left - DRAGICON_HOTSPOT_X;
250 rcDragIcon.top = rcItem.top - DRAGICON_HOTSPOT_Y;
251 rcDragIcon.right = rcListBox.left;
252 rcDragIcon.bottom = rcDragIcon.top + DRAGICON_HEIGHT;
254 if (!GetWindowSubclass(hwndLB, DragList_SubclassWindowProc, DRAGLIST_SUBCLASSID, (DWORD_PTR*)&data))
258 SetRectEmpty(&rcDragIcon);
260 /* prevent flicker by only redrawing when necessary */
261 if (!EqualRect(&rcDragIcon, &data->last_drag_icon_rect))
263 /* get rid of any previous inserts drawn */
264 RedrawWindow(hwndParent, &data->last_drag_icon_rect, NULL,
265 RDW_INTERNALPAINT | RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
267 CopyRect(&data->last_drag_icon_rect, &rcDragIcon);
271 hdc = GetDC(hwndParent);
273 DrawIcon(hdc, rcDragIcon.left, rcDragIcon.top, hDragArrow);
275 ReleaseDC(hwndParent, hdc);
280 /***********************************************************************
281 * LBItemFromPt (COMCTL32.14)
283 * Gets the index of the ListBox item under the specified point,
284 * scrolling if bAutoScroll is TRUE and pt is outside of the ListBox.
287 * The ListBox item ID if pt is over a list item or -1 otherwise.
289 INT WINAPI LBItemFromPt (HWND hwndLB, POINT pt, BOOL bAutoScroll)
295 TRACE("(%p %d x %d %s)\n",
296 hwndLB, pt.x, pt.y, bAutoScroll ? "TRUE" : "FALSE");
298 ScreenToClient (hwndLB, &pt);
299 GetClientRect (hwndLB, &rcClient);
300 nIndex = (INT)SendMessageW (hwndLB, LB_GETTOPINDEX, 0, 0);
302 if (PtInRect (&rcClient, pt))
304 /* point is inside -- get the item index */
307 if (SendMessageW (hwndLB, LB_GETITEMRECT, nIndex, (LPARAM)&rcClient) == LB_ERR)
310 if (PtInRect (&rcClient, pt))
318 /* point is outside */
322 if ((pt.x > rcClient.right) || (pt.x < rcClient.left))
330 dwScrollTime = GetTickCount ();
332 if ((dwScrollTime - dwLastScrollTime) < DRAGLIST_SCROLLPERIOD)
335 dwLastScrollTime = dwScrollTime;
337 SendMessageW (hwndLB, LB_SETTOPINDEX, nIndex, 0);