[COMCTL32] Sync with Wine Staging 1.9.4. CORE-10912
[reactos.git] / reactos / dll / win32 / comctl32 / treeview.c
1 /* Treeview control
2 *
3 * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4 * Copyright 1998,1999 Alex Priem <alexp@sci.kun.nl>
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 CodeWeavers, Aric Stewart
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 *
22 * NOTES
23 *
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
25 *
26 * Note2: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
29 *
30 * TODO:
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO
33 *
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
35 *
36 * missing item styles: TVIS_EXPANDPARTIAL, TVIS_EX_FLAT,
37 * TVIS_EX_DISABLED
38 *
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
41 */
42
43 #include "comctl32.h"
44
45 #include <wine/exception.h>
46
47 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
48
49 /* internal structures */
50 typedef struct tagTREEVIEW_INFO
51 {
52 HWND hwnd;
53 HWND hwndNotify; /* Owner window to send notifications to */
54 DWORD dwStyle;
55 HTREEITEM root;
56 UINT uInternalStatus;
57 INT Timer;
58 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
59 INT cdmode; /* last custom draw setting */
60 UINT uScrollTime; /* max. time for scrolling in milliseconds */
61 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
62
63 UINT uItemHeight; /* item height */
64 BOOL bHeightSet;
65
66 LONG clientWidth; /* width of control window */
67 LONG clientHeight; /* height of control window */
68
69 LONG treeWidth; /* width of visible tree items */
70 LONG treeHeight; /* height of visible tree items */
71
72 UINT uIndent; /* indentation in pixels */
73 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
74 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
75 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
76 HTREEITEM editItem; /* item being edited with builtin edit box */
77
78 HTREEITEM firstVisible; /* handle to item whose top edge is at y = 0 */
79 LONG maxVisibleOrder;
80 HTREEITEM dropItem; /* handle to item selected by drag cursor */
81 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
82 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
83 HIMAGELIST dragList; /* Bitmap of dragged item */
84 LONG scrollX;
85 INT wheelRemainder;
86 COLORREF clrBk;
87 COLORREF clrText;
88 COLORREF clrLine;
89 COLORREF clrInsertMark;
90 HFONT hFont;
91 HFONT hDefaultFont;
92 HFONT hBoldFont;
93 HFONT hUnderlineFont;
94 HFONT hBoldUnderlineFont;
95 HCURSOR hcurHand;
96 HWND hwndToolTip;
97
98 HWND hwndEdit;
99 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
100 BOOL bIgnoreEditKillFocus;
101 BOOL bLabelChanged;
102
103 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
104 HIMAGELIST himlNormal;
105 int normalImageHeight;
106 int normalImageWidth;
107 HIMAGELIST himlState;
108 int stateImageHeight;
109 int stateImageWidth;
110 HDPA items;
111
112 DWORD lastKeyPressTimestamp;
113 WPARAM charCode;
114 INT nSearchParamLength;
115 WCHAR szSearchParam[ MAX_PATH ];
116 } TREEVIEW_INFO;
117
118 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
119 {
120 HTREEITEM parent; /* handle to parent or 0 if at root */
121 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
122 HTREEITEM firstChild; /* handle to first child or 0 if no child */
123
124 UINT callbackMask;
125 UINT state;
126 UINT stateMask;
127 LPWSTR pszText;
128 int cchTextMax;
129 int iImage;
130 int iSelectedImage;
131 int iExpandedImage;
132 int cChildren;
133 LPARAM lParam;
134 int iIntegral; /* item height multiplier (1 is normal) */
135 int iLevel; /* indentation level:0=root level */
136 HTREEITEM lastChild;
137 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
138 RECT rect;
139 LONG linesOffset;
140 LONG stateOffset;
141 LONG imageOffset;
142 LONG textOffset;
143 LONG textWidth; /* horizontal text extent for pszText */
144 LONG visibleOrder; /* Depth-first numbering of the items whose ancestors are all expanded,
145 corresponding to a top-to-bottom ordering in the tree view.
146 Each item takes up "item.iIntegral" spots in the visible order.
147 0 is the root's first child. */
148 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
149 } TREEVIEW_ITEM;
150
151 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
152 #define KEY_DELAY 450
153
154 /* bitflags for infoPtr->uInternalStatus */
155
156 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
157 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
158 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
159 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
160 #define TV_RDRAG 0x10 /* ditto Rbutton */
161 #define TV_RDRAGGING 0x20
162
163 /* bitflags for infoPtr->timer */
164
165 #define TV_EDIT_TIMER 2
166 #define TV_EDIT_TIMER_SET 2
167
168 #define TEXT_CALLBACK_SIZE 260
169
170 #define TREEVIEW_LEFT_MARGIN 8
171
172 #define MINIMUM_INDENT 19
173
174 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
175
176 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
177 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
178 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
179
180 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
181 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
182 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
183 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
184
185 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
186
187
188 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
189
190
191 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
192
193 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
194 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
195 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
196 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
197 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
198 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
199
200 /* Random Utilities *****************************************************/
201 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
202
203 /* Returns the treeview private data if hwnd is a treeview.
204 * Otherwise returns an undefined value. */
205 static inline TREEVIEW_INFO *
206 TREEVIEW_GetInfoPtr(HWND hwnd)
207 {
208 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
209 }
210
211 /* Don't call this. Nothing wants an item index. */
212 static inline int
213 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
214 {
215 return DPA_GetPtrIndex(infoPtr->items, handle);
216 }
217
218 /* Checks if item has changed and needs to be redrawn */
219 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
220 const TVITEMEXW *tvChange)
221 {
222 /* Number of children has changed */
223 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
224 return TRUE;
225
226 /* Image has changed and it's not a callback */
227 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
228 tiNew->iImage != I_IMAGECALLBACK)
229 return TRUE;
230
231 /* Selected image has changed and it's not a callback */
232 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
233 tiNew->iSelectedImage != I_IMAGECALLBACK)
234 return TRUE;
235
236 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
237 tiNew->iExpandedImage != I_IMAGECALLBACK)
238 return TRUE;
239
240 /* Text has changed and it's not a callback */
241 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
242 tiNew->pszText != LPSTR_TEXTCALLBACKW)
243 return TRUE;
244
245 /* Indent has changed */
246 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
247 return TRUE;
248
249 /* Item state has changed */
250 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
251 return TRUE;
252
253 return FALSE;
254 }
255
256 /***************************************************************************
257 * This method checks that handle is an item for this tree.
258 */
259 static BOOL
260 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
261 {
262 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
263 {
264 TRACE("invalid item %p\n", handle);
265 return FALSE;
266 }
267 else
268 return TRUE;
269 }
270
271 static HFONT
272 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
273 {
274 LOGFONTW font;
275
276 GetObjectW(hOrigFont, sizeof(font), &font);
277 font.lfWeight = FW_BOLD;
278 return CreateFontIndirectW(&font);
279 }
280
281 static HFONT
282 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
283 {
284 LOGFONTW font;
285
286 GetObjectW(hOrigFont, sizeof(font), &font);
287 font.lfUnderline = TRUE;
288 return CreateFontIndirectW(&font);
289 }
290
291 static HFONT
292 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont)
293 {
294 LOGFONTW font;
295
296 GetObjectW(hfont, sizeof(font), &font);
297 font.lfWeight = FW_BOLD;
298 font.lfUnderline = TRUE;
299 return CreateFontIndirectW(&font);
300 }
301
302 static inline HFONT
303 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
304 {
305 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
306 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
307 if (item->state & TVIS_BOLD)
308 return infoPtr->hBoldFont;
309 return infoPtr->hFont;
310 }
311
312 /* for trace/debugging purposes only */
313 static const char *
314 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
315 {
316 if (item == NULL) return "<null item>";
317 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
318 if (item->pszText == NULL) return "<null>";
319 return debugstr_w(item->pszText);
320 }
321
322 /* An item is not a child of itself. */
323 static BOOL
324 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
325 {
326 do
327 {
328 child = child->parent;
329 if (child == parent) return TRUE;
330 } while (child != NULL);
331
332 return FALSE;
333 }
334
335
336 /* Tree Traversal *******************************************************/
337
338 /***************************************************************************
339 * This method returns the last expanded sibling or child child item
340 * of a tree node
341 */
342 static TREEVIEW_ITEM *
343 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
344 {
345 if (!item) return NULL;
346
347 while (item->lastChild)
348 {
349 if (item->state & TVIS_EXPANDED)
350 item = item->lastChild;
351 else
352 break;
353 }
354
355 if (item == infoPtr->root)
356 return NULL;
357
358 return item;
359 }
360
361 /***************************************************************************
362 * This method returns the previous non-hidden item in the list not
363 * considering the tree hierarchy.
364 */
365 static TREEVIEW_ITEM *
366 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
367 {
368 if (tvItem->prevSibling)
369 {
370 /* This item has a prevSibling, get the last item in the sibling's tree. */
371 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
372
373 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
374 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
375 else
376 return upItem;
377 }
378 else
379 {
380 /* this item does not have a prevSibling, get the parent */
381 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
382 }
383 }
384
385
386 /***************************************************************************
387 * This method returns the next physical item in the treeview not
388 * considering the tree hierarchy.
389 */
390 static TREEVIEW_ITEM *
391 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
392 {
393 /*
394 * If this item has children and is expanded, return the first child
395 */
396 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
397 {
398 return tvItem->firstChild;
399 }
400
401
402 /*
403 * try to get the sibling
404 */
405 if (tvItem->nextSibling)
406 return tvItem->nextSibling;
407
408 /*
409 * Otherwise, get the parent's sibling.
410 */
411 while (tvItem->parent)
412 {
413 tvItem = tvItem->parent;
414
415 if (tvItem->nextSibling)
416 return tvItem->nextSibling;
417 }
418
419 return NULL;
420 }
421
422 /***************************************************************************
423 * This method returns the nth item starting at the given item. It returns
424 * the last item (or first) we we run out of items.
425 *
426 * Will scroll backward if count is <0.
427 * forward if count is >0.
428 */
429 static TREEVIEW_ITEM *
430 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
431 LONG count)
432 {
433 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
434 TREEVIEW_ITEM *previousItem;
435
436 assert(item != NULL);
437
438 if (count > 0)
439 {
440 next_item = TREEVIEW_GetNextListItem;
441 }
442 else if (count < 0)
443 {
444 count = -count;
445 next_item = TREEVIEW_GetPrevListItem;
446 }
447 else
448 return item;
449
450 do
451 {
452 previousItem = item;
453 item = next_item(infoPtr, item);
454
455 } while (--count && item != NULL);
456
457
458 return item ? item : previousItem;
459 }
460
461 /* Notifications ************************************************************/
462
463 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
464 {
465 if (!infoPtr->bNtfUnicode) {
466 switch (code) {
467 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
468 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
469 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
470 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
471 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
472 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
473 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
474 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
475 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
476 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
477 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
478 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
479 }
480 }
481 return code;
482 }
483
484 static inline BOOL
485 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, UINT code, NMHDR *hdr)
486 {
487 TRACE("code=%d, hdr=%p\n", code, hdr);
488
489 hdr->hwndFrom = infoPtr->hwnd;
490 hdr->idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
491 hdr->code = get_notifycode(infoPtr, code);
492
493 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, hdr->idFrom, (LPARAM)hdr);
494 }
495
496 static BOOL
497 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
498 {
499 NMHDR hdr;
500 return TREEVIEW_SendRealNotify(infoPtr, code, &hdr);
501 }
502
503 static VOID
504 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
505 {
506 tvItem->mask = mask;
507 tvItem->hItem = item;
508 tvItem->state = item->state;
509 tvItem->stateMask = 0;
510 tvItem->iImage = item->iImage;
511 tvItem->iSelectedImage = item->iSelectedImage;
512 tvItem->cChildren = item->cChildren;
513 tvItem->lParam = item->lParam;
514
515 if(mask & TVIF_TEXT)
516 {
517 if (!infoPtr->bNtfUnicode)
518 {
519 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
520 tvItem->pszText = Alloc (tvItem->cchTextMax);
521 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
522 }
523 else
524 {
525 tvItem->cchTextMax = item->cchTextMax;
526 tvItem->pszText = item->pszText;
527 }
528 }
529 else
530 {
531 tvItem->cchTextMax = 0;
532 tvItem->pszText = NULL;
533 }
534 }
535
536 static BOOL
537 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
538 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
539 {
540 NMTREEVIEWW nmhdr;
541 BOOL ret;
542
543 TRACE("code:%d action:0x%x olditem:%p newitem:%p\n",
544 code, action, oldItem, newItem);
545
546 memset(&nmhdr, 0, sizeof(NMTREEVIEWW));
547 nmhdr.action = action;
548
549 if (oldItem)
550 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
551
552 if (newItem)
553 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
554
555 nmhdr.ptDrag.x = 0;
556 nmhdr.ptDrag.y = 0;
557
558 ret = TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
559 if (!infoPtr->bNtfUnicode)
560 {
561 Free(nmhdr.itemOld.pszText);
562 Free(nmhdr.itemNew.pszText);
563 }
564 return ret;
565 }
566
567 static BOOL
568 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
569 HTREEITEM dragItem, POINT pt)
570 {
571 NMTREEVIEWW nmhdr;
572
573 TRACE("code:%d dragitem:%p\n", code, dragItem);
574
575 nmhdr.action = 0;
576 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
577 nmhdr.itemNew.hItem = dragItem;
578 nmhdr.itemNew.state = dragItem->state;
579 nmhdr.itemNew.lParam = dragItem->lParam;
580
581 nmhdr.ptDrag.x = pt.x;
582 nmhdr.ptDrag.y = pt.y;
583
584 return TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
585 }
586
587
588 static BOOL
589 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
590 HDC hdc, RECT rc)
591 {
592 NMTVCUSTOMDRAW nmcdhdr;
593 NMCUSTOMDRAW *nmcd;
594
595 TRACE("drawstage:0x%x hdc:%p\n", dwDrawStage, hdc);
596
597 nmcd = &nmcdhdr.nmcd;
598 nmcd->dwDrawStage = dwDrawStage;
599 nmcd->hdc = hdc;
600 nmcd->rc = rc;
601 nmcd->dwItemSpec = 0;
602 nmcd->uItemState = 0;
603 nmcd->lItemlParam = 0;
604 nmcdhdr.clrText = infoPtr->clrText;
605 nmcdhdr.clrTextBk = infoPtr->clrBk;
606 nmcdhdr.iLevel = 0;
607
608 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr.nmcd.hdr);
609 }
610
611 /* FIXME: need to find out when the flags in uItemState need to be set */
612
613 static BOOL
614 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
615 TREEVIEW_ITEM *item, UINT uItemDrawState,
616 NMTVCUSTOMDRAW *nmcdhdr)
617 {
618 NMCUSTOMDRAW *nmcd;
619 DWORD dwDrawStage;
620 DWORD_PTR dwItemSpec;
621 UINT uItemState;
622
623 dwDrawStage = CDDS_ITEM | uItemDrawState;
624 dwItemSpec = (DWORD_PTR)item;
625 uItemState = 0;
626 if (item->state & TVIS_SELECTED)
627 uItemState |= CDIS_SELECTED;
628 if (item == infoPtr->selectedItem)
629 uItemState |= CDIS_FOCUS;
630 if (item == infoPtr->hotItem)
631 uItemState |= CDIS_HOT;
632
633 nmcd = &nmcdhdr->nmcd;
634 nmcd->dwDrawStage = dwDrawStage;
635 nmcd->hdc = hdc;
636 nmcd->rc = item->rect;
637 nmcd->dwItemSpec = dwItemSpec;
638 nmcd->uItemState = uItemState;
639 nmcd->lItemlParam = item->lParam;
640 nmcdhdr->iLevel = item->iLevel;
641
642 TRACE("drawstage:0x%x hdc:%p item:%lx, itemstate:0x%x, lItemlParam:0x%lx\n",
643 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
644 nmcd->uItemState, nmcd->lItemlParam);
645
646 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr->nmcd.hdr);
647 }
648
649 static BOOL
650 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
651 {
652 NMTVDISPINFOW tvdi;
653 BOOL ret;
654
655 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
656 &tvdi.item, editItem);
657
658 ret = TREEVIEW_SendRealNotify(infoPtr, TVN_BEGINLABELEDITW, &tvdi.hdr);
659
660 if (!infoPtr->bNtfUnicode)
661 Free(tvdi.item.pszText);
662
663 return ret;
664 }
665
666 static void
667 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
668 UINT mask)
669 {
670 NMTVDISPINFOEXW callback;
671
672 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
673 mask &= item->callbackMask;
674
675 if (mask == 0) return;
676
677 /* 'state' always contains valid value, as well as 'lParam'.
678 * All other parameters are uninitialized.
679 */
680 callback.item.pszText = item->pszText;
681 callback.item.cchTextMax = item->cchTextMax;
682 callback.item.mask = mask;
683 callback.item.hItem = item;
684 callback.item.state = item->state;
685 callback.item.lParam = item->lParam;
686
687 /* If text is changed we need to recalculate textWidth */
688 if (mask & TVIF_TEXT)
689 item->textWidth = 0;
690
691 TREEVIEW_SendRealNotify(infoPtr, TVN_GETDISPINFOW, &callback.hdr);
692 TRACE("resulting code 0x%08x\n", callback.hdr.code);
693
694 /* It may have changed due to a call to SetItem. */
695 mask &= item->callbackMask;
696
697 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
698 {
699 /* Instead of copying text into our buffer user specified his own */
700 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
701 LPWSTR newText;
702 int buflen;
703 int len = MultiByteToWideChar( CP_ACP, 0,
704 (LPSTR)callback.item.pszText, -1,
705 NULL, 0);
706 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
707 newText = ReAlloc(item->pszText, buflen);
708
709 TRACE("returned str %s, len=%d, buflen=%d\n",
710 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
711
712 if (newText)
713 {
714 item->pszText = newText;
715 MultiByteToWideChar( CP_ACP, 0,
716 (LPSTR)callback.item.pszText, -1,
717 item->pszText, buflen/sizeof(WCHAR));
718 item->cchTextMax = buflen/sizeof(WCHAR);
719 }
720 /* If ReAlloc fails we have nothing to do, but keep original text */
721 }
722 else {
723 int len = max(lstrlenW(callback.item.pszText) + 1,
724 TEXT_CALLBACK_SIZE);
725 LPWSTR newText = ReAlloc(item->pszText, len);
726
727 TRACE("returned wstr %s, len=%d\n",
728 debugstr_w(callback.item.pszText), len);
729
730 if (newText)
731 {
732 item->pszText = newText;
733 strcpyW(item->pszText, callback.item.pszText);
734 item->cchTextMax = len;
735 }
736 /* If ReAlloc fails we have nothing to do, but keep original text */
737 }
738 }
739 else if (mask & TVIF_TEXT) {
740 /* User put text into our buffer, that is ok unless A string */
741 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
742 LPWSTR newText;
743 int buflen;
744 int len = MultiByteToWideChar( CP_ACP, 0,
745 (LPSTR)callback.item.pszText, -1,
746 NULL, 0);
747 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
748 newText = Alloc(buflen);
749
750 TRACE("same buffer str %s, len=%d, buflen=%d\n",
751 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
752
753 if (newText)
754 {
755 LPWSTR oldText = item->pszText;
756 item->pszText = newText;
757 MultiByteToWideChar( CP_ACP, 0,
758 (LPSTR)callback.item.pszText, -1,
759 item->pszText, buflen/sizeof(WCHAR));
760 item->cchTextMax = buflen/sizeof(WCHAR);
761 Free(oldText);
762 }
763 }
764 }
765
766 if (mask & TVIF_IMAGE)
767 item->iImage = callback.item.iImage;
768
769 if (mask & TVIF_SELECTEDIMAGE)
770 item->iSelectedImage = callback.item.iSelectedImage;
771
772 if (mask & TVIF_EXPANDEDIMAGE)
773 item->iExpandedImage = callback.item.iExpandedImage;
774
775 if (mask & TVIF_CHILDREN)
776 item->cChildren = callback.item.cChildren;
777
778 if (callback.item.mask & TVIF_STATE)
779 {
780 item->state &= ~callback.item.stateMask;
781 item->state |= (callback.item.state & callback.item.stateMask);
782 }
783
784 /* These members are now permanently set. */
785 if (callback.item.mask & TVIF_DI_SETITEM)
786 item->callbackMask &= ~callback.item.mask;
787 }
788
789 /***************************************************************************
790 * This function uses cChildren field to decide whether the item has
791 * children or not.
792 * Note: if this returns TRUE, the child items may not actually exist,
793 * they could be virtual.
794 *
795 * Just use item->firstChild to check for physical children.
796 */
797 static BOOL
798 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
799 {
800 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
801 /* Protect for a case when callback field is not changed by a host,
802 otherwise negative values trigger normal notifications. */
803 return item->cChildren != 0 && item->cChildren != I_CHILDRENCALLBACK;
804 }
805
806 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
807 {
808 INT format;
809
810 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
811
812 if (nCommand != NF_REQUERY) return 0;
813
814 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
815 TRACE("format=%d\n", format);
816
817 /* Invalid format returned by NF_QUERY defaults to ANSI*/
818 if (format != NFR_ANSI && format != NFR_UNICODE)
819 format = NFR_ANSI;
820
821 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
822
823 return format;
824 }
825
826 /* Item Position ********************************************************/
827
828 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
829 static VOID
830 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
831 {
832 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
833 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
834 > TVS_LINESATROOT);
835
836 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
837 - infoPtr->scrollX;
838 item->stateOffset = item->linesOffset + infoPtr->uIndent;
839 item->imageOffset = item->stateOffset
840 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
841 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
842 }
843
844 static VOID
845 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
846 {
847 HDC hdc;
848 HFONT hOldFont=0;
849 SIZE sz;
850
851 /* DRAW's OM docker creates items like this */
852 if (item->pszText == NULL)
853 {
854 item->textWidth = 0;
855 return;
856 }
857
858 if (hDC != 0)
859 {
860 hdc = hDC;
861 }
862 else
863 {
864 hdc = GetDC(infoPtr->hwnd);
865 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
866 }
867
868 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
869 item->textWidth = sz.cx;
870
871 if (hDC == 0)
872 {
873 SelectObject(hdc, hOldFont);
874 ReleaseDC(0, hdc);
875 }
876 }
877
878 static VOID
879 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
880 {
881 item->rect.top = infoPtr->uItemHeight *
882 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
883
884 item->rect.bottom = item->rect.top
885 + infoPtr->uItemHeight * item->iIntegral - 1;
886
887 item->rect.left = 0;
888 item->rect.right = infoPtr->clientWidth;
889 }
890
891 /* We know that only items after start need their order updated. */
892 static void
893 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
894 {
895 TREEVIEW_ITEM *item;
896 int order;
897
898 if (!start)
899 {
900 start = infoPtr->root->firstChild;
901 order = 0;
902 }
903 else
904 order = start->visibleOrder;
905
906 for (item = start; item != NULL;
907 item = TREEVIEW_GetNextListItem(infoPtr, item))
908 {
909 if (!ISVISIBLE(item) && order > 0)
910 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
911 item->visibleOrder = order;
912 order += item->iIntegral;
913 }
914
915 infoPtr->maxVisibleOrder = order;
916
917 for (item = start; item != NULL;
918 item = TREEVIEW_GetNextListItem(infoPtr, item))
919 {
920 TREEVIEW_ComputeItemRect(infoPtr, item);
921 }
922 }
923
924
925 /* Update metrics of all items in selected subtree.
926 * root must be expanded
927 */
928 static VOID
929 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
930 {
931 TREEVIEW_ITEM *sibling;
932 HDC hdc;
933 HFONT hOldFont;
934
935 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
936 return;
937
938 root->state &= ~TVIS_EXPANDED;
939 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
940 root->state |= TVIS_EXPANDED;
941
942 hdc = GetDC(infoPtr->hwnd);
943 hOldFont = SelectObject(hdc, infoPtr->hFont);
944
945 for (; root != sibling;
946 root = TREEVIEW_GetNextListItem(infoPtr, root))
947 {
948 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
949
950 if (root->callbackMask & TVIF_TEXT)
951 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
952
953 if (root->textWidth == 0)
954 {
955 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
956 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
957 }
958 }
959
960 SelectObject(hdc, hOldFont);
961 ReleaseDC(infoPtr->hwnd, hdc);
962 }
963
964 /* Item Allocation **********************************************************/
965
966 static TREEVIEW_ITEM *
967 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
968 {
969 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
970
971 if (!newItem)
972 return NULL;
973
974 /* I_IMAGENONE would make more sense but this is neither what is
975 * documented (MSDN doesn't specify) nor what Windows actually does
976 * (it sets it to zero)... and I can so imagine an application using
977 * inc/dec to toggle the images. */
978 newItem->iImage = 0;
979 newItem->iSelectedImage = 0;
980 newItem->iExpandedImage = (WORD)I_IMAGENONE;
981 newItem->infoPtr = infoPtr;
982
983 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
984 {
985 Free(newItem);
986 return NULL;
987 }
988
989 return newItem;
990 }
991
992 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
993 * free item->pszText. */
994 static void
995 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
996 {
997 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
998 if (infoPtr->selectedItem == item)
999 infoPtr->selectedItem = NULL;
1000 if (infoPtr->hotItem == item)
1001 infoPtr->hotItem = NULL;
1002 if (infoPtr->focusedItem == item)
1003 infoPtr->focusedItem = NULL;
1004 if (infoPtr->firstVisible == item)
1005 infoPtr->firstVisible = NULL;
1006 if (infoPtr->dropItem == item)
1007 infoPtr->dropItem = NULL;
1008 if (infoPtr->insertMarkItem == item)
1009 infoPtr->insertMarkItem = NULL;
1010 Free(item);
1011 }
1012
1013
1014 /* Item Insertion *******************************************************/
1015
1016 /***************************************************************************
1017 * This method inserts newItem before sibling as a child of parent.
1018 * sibling can be NULL, but only if parent has no children.
1019 */
1020 static void
1021 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1022 TREEVIEW_ITEM *parent)
1023 {
1024 assert(parent != NULL);
1025
1026 if (sibling != NULL)
1027 {
1028 assert(sibling->parent == parent);
1029
1030 if (sibling->prevSibling != NULL)
1031 sibling->prevSibling->nextSibling = newItem;
1032
1033 newItem->prevSibling = sibling->prevSibling;
1034 sibling->prevSibling = newItem;
1035 }
1036 else
1037 newItem->prevSibling = NULL;
1038
1039 newItem->nextSibling = sibling;
1040
1041 if (parent->firstChild == sibling)
1042 parent->firstChild = newItem;
1043
1044 if (parent->lastChild == NULL)
1045 parent->lastChild = newItem;
1046 }
1047
1048 /***************************************************************************
1049 * This method inserts newItem after sibling as a child of parent.
1050 * sibling can be NULL, but only if parent has no children.
1051 */
1052 static void
1053 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1054 TREEVIEW_ITEM *parent)
1055 {
1056 assert(parent != NULL);
1057
1058 if (sibling != NULL)
1059 {
1060 assert(sibling->parent == parent);
1061
1062 if (sibling->nextSibling != NULL)
1063 sibling->nextSibling->prevSibling = newItem;
1064
1065 newItem->nextSibling = sibling->nextSibling;
1066 sibling->nextSibling = newItem;
1067 }
1068 else
1069 newItem->nextSibling = NULL;
1070
1071 newItem->prevSibling = sibling;
1072
1073 if (parent->lastChild == sibling)
1074 parent->lastChild = newItem;
1075
1076 if (parent->firstChild == NULL)
1077 parent->firstChild = newItem;
1078 }
1079
1080 static BOOL
1081 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1082 const TVITEMEXW *tvItem, BOOL isW)
1083 {
1084 UINT callbackClear = 0;
1085 UINT callbackSet = 0;
1086
1087 TRACE("item %p\n", item);
1088 /* Do this first in case it fails. */
1089 if (tvItem->mask & TVIF_TEXT)
1090 {
1091 item->textWidth = 0; /* force width recalculation */
1092 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1093 {
1094 int len;
1095 LPWSTR newText;
1096 if (isW)
1097 len = lstrlenW(tvItem->pszText) + 1;
1098 else
1099 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1100
1101 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1102
1103 if (newText == NULL) return FALSE;
1104
1105 callbackClear |= TVIF_TEXT;
1106
1107 item->pszText = newText;
1108 item->cchTextMax = len;
1109 if (isW)
1110 lstrcpynW(item->pszText, tvItem->pszText, len);
1111 else
1112 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1113 item->pszText, len);
1114
1115 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1116 }
1117 else
1118 {
1119 callbackSet |= TVIF_TEXT;
1120
1121 item->pszText = ReAlloc(item->pszText,
1122 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1123 item->cchTextMax = TEXT_CALLBACK_SIZE;
1124 TRACE("setting callback, item %p\n", item);
1125 }
1126 }
1127
1128 if (tvItem->mask & TVIF_CHILDREN)
1129 {
1130 item->cChildren = tvItem->cChildren;
1131
1132 if (item->cChildren == I_CHILDRENCALLBACK)
1133 callbackSet |= TVIF_CHILDREN;
1134 else
1135 callbackClear |= TVIF_CHILDREN;
1136 }
1137
1138 if (tvItem->mask & TVIF_IMAGE)
1139 {
1140 item->iImage = tvItem->iImage;
1141
1142 if (item->iImage == I_IMAGECALLBACK)
1143 callbackSet |= TVIF_IMAGE;
1144 else
1145 callbackClear |= TVIF_IMAGE;
1146 }
1147
1148 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1149 {
1150 item->iSelectedImage = tvItem->iSelectedImage;
1151
1152 if (item->iSelectedImage == I_IMAGECALLBACK)
1153 callbackSet |= TVIF_SELECTEDIMAGE;
1154 else
1155 callbackClear |= TVIF_SELECTEDIMAGE;
1156 }
1157
1158 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1159 {
1160 item->iExpandedImage = tvItem->iExpandedImage;
1161
1162 if (item->iExpandedImage == I_IMAGECALLBACK)
1163 callbackSet |= TVIF_EXPANDEDIMAGE;
1164 else
1165 callbackClear |= TVIF_EXPANDEDIMAGE;
1166 }
1167
1168 if (tvItem->mask & TVIF_PARAM)
1169 item->lParam = tvItem->lParam;
1170
1171 /* If the application sets TVIF_INTEGRAL without
1172 * supplying a TVITEMEX structure, it's toast. */
1173 if (tvItem->mask & TVIF_INTEGRAL)
1174 item->iIntegral = tvItem->iIntegral;
1175
1176 if (tvItem->mask & TVIF_STATE)
1177 {
1178 TRACE("prevstate 0x%x, state 0x%x, mask 0x%x\n", item->state, tvItem->state,
1179 tvItem->stateMask);
1180 item->state &= ~tvItem->stateMask;
1181 item->state |= (tvItem->state & tvItem->stateMask);
1182 }
1183
1184 if (tvItem->mask & TVIF_STATEEX)
1185 {
1186 FIXME("New extended state: 0x%x\n", tvItem->uStateEx);
1187 }
1188
1189 item->callbackMask |= callbackSet;
1190 item->callbackMask &= ~callbackClear;
1191
1192 return TRUE;
1193 }
1194
1195 /* Note that the new item is pre-zeroed. */
1196 static LRESULT
1197 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1198 {
1199 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1200 HTREEITEM insertAfter;
1201 TREEVIEW_ITEM *newItem, *parentItem;
1202 BOOL bTextUpdated = FALSE;
1203
1204 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1205 {
1206 parentItem = infoPtr->root;
1207 }
1208 else
1209 {
1210 parentItem = ptdi->hParent;
1211
1212 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1213 {
1214 WARN("invalid parent %p\n", parentItem);
1215 return 0;
1216 }
1217 }
1218
1219 insertAfter = ptdi->hInsertAfter;
1220
1221 /* Validate this now for convenience. */
1222 switch ((DWORD_PTR)insertAfter)
1223 {
1224 case (DWORD_PTR)TVI_FIRST:
1225 case (DWORD_PTR)TVI_LAST:
1226 case (DWORD_PTR)TVI_SORT:
1227 break;
1228
1229 default:
1230 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1231 insertAfter->parent != parentItem)
1232 {
1233 WARN("invalid insert after %p\n", insertAfter);
1234 insertAfter = TVI_LAST;
1235 }
1236 }
1237
1238 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1239 (tvItem->mask & TVIF_TEXT)
1240 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1241 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1242 : "<no label>");
1243
1244 newItem = TREEVIEW_AllocateItem(infoPtr);
1245 if (newItem == NULL)
1246 return 0;
1247
1248 newItem->parent = parentItem;
1249 newItem->iIntegral = 1;
1250 newItem->visibleOrder = -1;
1251
1252 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1253 return 0;
1254
1255 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1256
1257 infoPtr->uNumItems++;
1258
1259 switch ((DWORD_PTR)insertAfter)
1260 {
1261 case (DWORD_PTR)TVI_FIRST:
1262 {
1263 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1264 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1265 if (infoPtr->firstVisible == originalFirst)
1266 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1267 }
1268 break;
1269
1270 case (DWORD_PTR)TVI_LAST:
1271 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1272 break;
1273
1274 /* hInsertAfter names a specific item we want to insert after */
1275 default:
1276 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1277 break;
1278
1279 case (DWORD_PTR)TVI_SORT:
1280 {
1281 TREEVIEW_ITEM *aChild;
1282 TREEVIEW_ITEM *previousChild = NULL;
1283 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1284 BOOL bItemInserted = FALSE;
1285
1286 aChild = parentItem->firstChild;
1287
1288 bTextUpdated = TRUE;
1289 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1290
1291 /* Iterate the parent children to see where we fit in */
1292 while (aChild != NULL)
1293 {
1294 INT comp;
1295
1296 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1297 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1298
1299 if (comp < 0) /* we are smaller than the current one */
1300 {
1301 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1302 if (infoPtr->firstVisible == originalFirst &&
1303 aChild == originalFirst)
1304 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1305 bItemInserted = TRUE;
1306 break;
1307 }
1308 else if (comp > 0) /* we are bigger than the current one */
1309 {
1310 previousChild = aChild;
1311
1312 /* This will help us to exit if there is no more sibling */
1313 aChild = (aChild->nextSibling == 0)
1314 ? NULL
1315 : aChild->nextSibling;
1316
1317 /* Look at the next item */
1318 continue;
1319 }
1320 else if (comp == 0)
1321 {
1322 /*
1323 * An item with this name is already existing, therefore,
1324 * we add after the one we found
1325 */
1326 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1327 bItemInserted = TRUE;
1328 break;
1329 }
1330 }
1331
1332 /*
1333 * we reach the end of the child list and the item has not
1334 * yet been inserted, therefore, insert it after the last child.
1335 */
1336 if ((!bItemInserted) && (aChild == NULL))
1337 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1338
1339 break;
1340 }
1341 }
1342
1343
1344 TRACE("new item %p; parent %p, mask 0x%x\n", newItem,
1345 newItem->parent, tvItem->mask);
1346
1347 newItem->iLevel = newItem->parent->iLevel + 1;
1348
1349 if (newItem->parent->cChildren == 0)
1350 newItem->parent->cChildren = 1;
1351
1352 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1353 {
1354 if (STATEIMAGEINDEX(newItem->state) == 0)
1355 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1356 }
1357
1358 if (infoPtr->firstVisible == NULL)
1359 infoPtr->firstVisible = newItem;
1360
1361 TREEVIEW_VerifyTree(infoPtr);
1362
1363 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1364
1365 if (parentItem == infoPtr->root ||
1366 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1367 {
1368 TREEVIEW_ITEM *item;
1369 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1370
1371 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1372 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1373
1374 if (!bTextUpdated)
1375 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1376
1377 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1378 TREEVIEW_UpdateScrollBars(infoPtr);
1379 /*
1380 * if the item was inserted in a visible part of the tree,
1381 * invalidate it, as well as those after it
1382 */
1383 for (item = newItem;
1384 item != NULL;
1385 item = TREEVIEW_GetNextListItem(infoPtr, item))
1386 TREEVIEW_Invalidate(infoPtr, item);
1387 }
1388 else
1389 {
1390 /* refresh treeview if newItem is the first item inserted under parentItem */
1391 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1392 {
1393 /* parent got '+' - update it */
1394 TREEVIEW_Invalidate(infoPtr, parentItem);
1395 }
1396 }
1397
1398 return (LRESULT)newItem;
1399 }
1400
1401 /* Item Deletion ************************************************************/
1402 static void
1403 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1404
1405 static void
1406 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1407 {
1408 TREEVIEW_ITEM *kill = parentItem->firstChild;
1409
1410 while (kill != NULL)
1411 {
1412 TREEVIEW_ITEM *next = kill->nextSibling;
1413
1414 TREEVIEW_RemoveItem(infoPtr, kill);
1415
1416 kill = next;
1417 }
1418
1419 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1420 assert(parentItem->firstChild == NULL);
1421 assert(parentItem->lastChild == NULL);
1422 }
1423
1424 static void
1425 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1426 {
1427 TREEVIEW_ITEM *parentItem;
1428
1429 assert(item != NULL);
1430 assert(item->parent != NULL); /* i.e. it must not be the root */
1431
1432 parentItem = item->parent;
1433
1434 if (parentItem->firstChild == item)
1435 parentItem->firstChild = item->nextSibling;
1436
1437 if (parentItem->lastChild == item)
1438 parentItem->lastChild = item->prevSibling;
1439
1440 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1441 && parentItem->cChildren > 0)
1442 parentItem->cChildren = 0;
1443
1444 if (item->prevSibling)
1445 item->prevSibling->nextSibling = item->nextSibling;
1446
1447 if (item->nextSibling)
1448 item->nextSibling->prevSibling = item->prevSibling;
1449 }
1450
1451 static void
1452 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1453 {
1454 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1455
1456 if (item->firstChild)
1457 TREEVIEW_RemoveAllChildren(infoPtr, item);
1458
1459 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1460 TVIF_HANDLE | TVIF_PARAM, item, 0);
1461
1462 TREEVIEW_UnlinkItem(item);
1463
1464 infoPtr->uNumItems--;
1465
1466 if (item->pszText != LPSTR_TEXTCALLBACKW)
1467 Free(item->pszText);
1468
1469 TREEVIEW_FreeItem(infoPtr, item);
1470 }
1471
1472
1473 /* Empty out the tree. */
1474 static void
1475 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1476 {
1477 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1478
1479 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1480 }
1481
1482 static LRESULT
1483 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1484 {
1485 TREEVIEW_ITEM *newSelection = NULL;
1486 TREEVIEW_ITEM *newFirstVisible = NULL;
1487 TREEVIEW_ITEM *parent, *prev = NULL;
1488 BOOL visible = FALSE;
1489
1490 if (item == TVI_ROOT || !item)
1491 {
1492 TRACE("TVI_ROOT\n");
1493 parent = infoPtr->root;
1494 newSelection = NULL;
1495 visible = TRUE;
1496 TREEVIEW_RemoveTree(infoPtr);
1497 }
1498 else
1499 {
1500 if (!TREEVIEW_ValidItem(infoPtr, item))
1501 return FALSE;
1502
1503 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1504 parent = item->parent;
1505
1506 if (ISVISIBLE(item))
1507 {
1508 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1509 visible = TRUE;
1510 }
1511
1512 if (infoPtr->selectedItem != NULL
1513 && (item == infoPtr->selectedItem
1514 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1515 {
1516 if (item->nextSibling)
1517 newSelection = item->nextSibling;
1518 else if (item->parent != infoPtr->root)
1519 newSelection = item->parent;
1520 else
1521 newSelection = item->prevSibling;
1522 TRACE("newSelection = %p\n", newSelection);
1523 }
1524
1525 if (infoPtr->firstVisible == item)
1526 {
1527 visible = TRUE;
1528 if (item->nextSibling)
1529 newFirstVisible = item->nextSibling;
1530 else if (item->prevSibling)
1531 newFirstVisible = item->prevSibling;
1532 else if (item->parent != infoPtr->root)
1533 newFirstVisible = item->parent;
1534 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1535 }
1536 else
1537 newFirstVisible = infoPtr->firstVisible;
1538
1539 TREEVIEW_RemoveItem(infoPtr, item);
1540 }
1541
1542 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1543 if (!infoPtr->selectedItem && newSelection)
1544 {
1545 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1546 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1547 }
1548
1549 /* Validate insertMark dropItem.
1550 * hotItem ??? - used for comparison only.
1551 */
1552 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1553 infoPtr->insertMarkItem = 0;
1554
1555 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1556 infoPtr->dropItem = 0;
1557
1558 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1559 newFirstVisible = infoPtr->root->firstChild;
1560
1561 TREEVIEW_VerifyTree(infoPtr);
1562
1563 if (visible)
1564 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1565
1566 if (!infoPtr->bRedraw) return TRUE;
1567
1568 if (visible)
1569 {
1570 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1571 TREEVIEW_UpdateScrollBars(infoPtr);
1572 TREEVIEW_Invalidate(infoPtr, NULL);
1573 }
1574 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1575 {
1576 /* parent lost '+/-' - update it */
1577 TREEVIEW_Invalidate(infoPtr, parent);
1578 }
1579
1580 return TRUE;
1581 }
1582
1583
1584 /* Get/Set Messages *********************************************************/
1585 static LRESULT
1586 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1587 {
1588 infoPtr->bRedraw = wParam != 0;
1589
1590 if (infoPtr->bRedraw)
1591 {
1592 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1593 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1594 TREEVIEW_UpdateScrollBars(infoPtr);
1595 TREEVIEW_Invalidate(infoPtr, NULL);
1596 }
1597 return 0;
1598 }
1599
1600 static LRESULT
1601 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1602 {
1603 TRACE("\n");
1604 return infoPtr->uIndent;
1605 }
1606
1607 static LRESULT
1608 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1609 {
1610 TRACE("\n");
1611
1612 if (newIndent < MINIMUM_INDENT)
1613 newIndent = MINIMUM_INDENT;
1614
1615 if (infoPtr->uIndent != newIndent)
1616 {
1617 infoPtr->uIndent = newIndent;
1618 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1619 TREEVIEW_UpdateScrollBars(infoPtr);
1620 TREEVIEW_Invalidate(infoPtr, NULL);
1621 }
1622
1623 return 0;
1624 }
1625
1626
1627 static LRESULT
1628 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1629 {
1630 TRACE("\n");
1631 return (LRESULT)infoPtr->hwndToolTip;
1632 }
1633
1634 static LRESULT
1635 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1636 {
1637 HWND prevToolTip;
1638
1639 TRACE("\n");
1640 prevToolTip = infoPtr->hwndToolTip;
1641 infoPtr->hwndToolTip = hwndTT;
1642
1643 return (LRESULT)prevToolTip;
1644 }
1645
1646 static LRESULT
1647 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1648 {
1649 BOOL rc = infoPtr->bNtfUnicode;
1650 infoPtr->bNtfUnicode = fUnicode;
1651 return rc;
1652 }
1653
1654 static LRESULT
1655 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1656 {
1657 return infoPtr->bNtfUnicode;
1658 }
1659
1660 static LRESULT
1661 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1662 {
1663 return infoPtr->uScrollTime;
1664 }
1665
1666 static LRESULT
1667 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1668 {
1669 UINT uOldScrollTime = infoPtr->uScrollTime;
1670
1671 infoPtr->uScrollTime = min(uScrollTime, 100);
1672
1673 return uOldScrollTime;
1674 }
1675
1676
1677 static LRESULT
1678 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1679 {
1680 TRACE("\n");
1681
1682 switch (wParam)
1683 {
1684 case TVSIL_NORMAL:
1685 return (LRESULT)infoPtr->himlNormal;
1686
1687 case TVSIL_STATE:
1688 return (LRESULT)infoPtr->himlState;
1689
1690 default:
1691 return 0;
1692 }
1693 }
1694
1695 #define TVHEIGHT_MIN 16
1696 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1697
1698 /* Compute the natural height for items. */
1699 static UINT
1700 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1701 {
1702 TEXTMETRICW tm;
1703 HDC hdc = GetDC(0);
1704 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1705 UINT height;
1706
1707 /* Height is the maximum of:
1708 * 16 (a hack because our fonts are tiny), and
1709 * The text height + border & margin, and
1710 * The size of the normal image list
1711 */
1712 GetTextMetricsW(hdc, &tm);
1713 SelectObject(hdc, hOldFont);
1714 ReleaseDC(0, hdc);
1715
1716 height = TVHEIGHT_MIN;
1717 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1718 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1719 if (height < infoPtr->normalImageHeight)
1720 height = infoPtr->normalImageHeight;
1721
1722 /* Round down, unless we support odd ("non even") heights. */
1723 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1724 height &= ~1;
1725
1726 return height;
1727 }
1728
1729 static LRESULT
1730 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1731 {
1732 HIMAGELIST himlOld = 0;
1733 int oldWidth = infoPtr->normalImageWidth;
1734 int oldHeight = infoPtr->normalImageHeight;
1735
1736 TRACE("%u,%p\n", type, himlNew);
1737
1738 switch (type)
1739 {
1740 case TVSIL_NORMAL:
1741 himlOld = infoPtr->himlNormal;
1742 infoPtr->himlNormal = himlNew;
1743
1744 if (himlNew)
1745 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1746 &infoPtr->normalImageHeight);
1747 else
1748 {
1749 infoPtr->normalImageWidth = 0;
1750 infoPtr->normalImageHeight = 0;
1751 }
1752
1753 break;
1754
1755 case TVSIL_STATE:
1756 himlOld = infoPtr->himlState;
1757 infoPtr->himlState = himlNew;
1758
1759 if (himlNew)
1760 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1761 &infoPtr->stateImageHeight);
1762 else
1763 {
1764 infoPtr->stateImageWidth = 0;
1765 infoPtr->stateImageHeight = 0;
1766 }
1767
1768 break;
1769
1770 default:
1771 ERR("unknown imagelist type %u\n", type);
1772 }
1773
1774 if (oldWidth != infoPtr->normalImageWidth ||
1775 oldHeight != infoPtr->normalImageHeight)
1776 {
1777 BOOL bRecalcVisible = FALSE;
1778
1779 if (oldHeight != infoPtr->normalImageHeight &&
1780 !infoPtr->bHeightSet)
1781 {
1782 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1783 bRecalcVisible = TRUE;
1784 }
1785
1786 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1787 infoPtr->normalImageWidth != infoPtr->uIndent)
1788 {
1789 infoPtr->uIndent = infoPtr->normalImageWidth;
1790 bRecalcVisible = TRUE;
1791 }
1792
1793 if (bRecalcVisible)
1794 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1795
1796 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1797 TREEVIEW_UpdateScrollBars(infoPtr);
1798 }
1799
1800 TREEVIEW_Invalidate(infoPtr, NULL);
1801
1802 return (LRESULT)himlOld;
1803 }
1804
1805 static LRESULT
1806 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1807 {
1808 INT prevHeight = infoPtr->uItemHeight;
1809
1810 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1811 if (newHeight == -1)
1812 {
1813 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1814 infoPtr->bHeightSet = FALSE;
1815 }
1816 else
1817 {
1818 if (newHeight == 0) newHeight = 1;
1819 infoPtr->uItemHeight = newHeight;
1820 infoPtr->bHeightSet = TRUE;
1821 }
1822
1823 /* Round down, unless we support odd ("non even") heights. */
1824 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1825 {
1826 infoPtr->uItemHeight &= ~1;
1827 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1828 }
1829
1830 if (infoPtr->uItemHeight != prevHeight)
1831 {
1832 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1833 TREEVIEW_UpdateScrollBars(infoPtr);
1834 TREEVIEW_Invalidate(infoPtr, NULL);
1835 }
1836
1837 return prevHeight;
1838 }
1839
1840 static LRESULT
1841 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1842 {
1843 TRACE("\n");
1844 return infoPtr->uItemHeight;
1845 }
1846
1847
1848 static LRESULT
1849 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1850 {
1851 TRACE("%p\n", infoPtr->hFont);
1852 return (LRESULT)infoPtr->hFont;
1853 }
1854
1855
1856 static INT CALLBACK
1857 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1858 {
1859 (void)unused;
1860
1861 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1862
1863 return 1;
1864 }
1865
1866 static LRESULT
1867 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1868 {
1869 UINT uHeight = infoPtr->uItemHeight;
1870
1871 TRACE("%p %i\n", hFont, bRedraw);
1872
1873 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1874
1875 DeleteObject(infoPtr->hBoldFont);
1876 DeleteObject(infoPtr->hUnderlineFont);
1877 DeleteObject(infoPtr->hBoldUnderlineFont);
1878 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1879 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1880 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
1881
1882 if (!infoPtr->bHeightSet)
1883 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1884
1885 if (uHeight != infoPtr->uItemHeight)
1886 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1887
1888 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1889
1890 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1891 TREEVIEW_UpdateScrollBars(infoPtr);
1892
1893 if (bRedraw)
1894 TREEVIEW_Invalidate(infoPtr, NULL);
1895
1896 return 0;
1897 }
1898
1899
1900 static LRESULT
1901 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1902 {
1903 TRACE("\n");
1904 return (LRESULT)infoPtr->clrLine;
1905 }
1906
1907 static LRESULT
1908 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1909 {
1910 COLORREF prevColor = infoPtr->clrLine;
1911
1912 TRACE("\n");
1913 infoPtr->clrLine = color;
1914 return (LRESULT)prevColor;
1915 }
1916
1917
1918 static LRESULT
1919 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1920 {
1921 TRACE("\n");
1922 return (LRESULT)infoPtr->clrText;
1923 }
1924
1925 static LRESULT
1926 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1927 {
1928 COLORREF prevColor = infoPtr->clrText;
1929
1930 TRACE("\n");
1931 infoPtr->clrText = color;
1932
1933 if (infoPtr->clrText != prevColor)
1934 TREEVIEW_Invalidate(infoPtr, NULL);
1935
1936 return (LRESULT)prevColor;
1937 }
1938
1939
1940 static LRESULT
1941 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1942 {
1943 TRACE("\n");
1944 return (LRESULT)infoPtr->clrBk;
1945 }
1946
1947 static LRESULT
1948 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1949 {
1950 COLORREF prevColor = infoPtr->clrBk;
1951
1952 TRACE("\n");
1953 infoPtr->clrBk = newColor;
1954
1955 if (newColor != prevColor)
1956 TREEVIEW_Invalidate(infoPtr, NULL);
1957
1958 return (LRESULT)prevColor;
1959 }
1960
1961
1962 static LRESULT
1963 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1964 {
1965 TRACE("\n");
1966 return (LRESULT)infoPtr->clrInsertMark;
1967 }
1968
1969 static LRESULT
1970 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1971 {
1972 COLORREF prevColor = infoPtr->clrInsertMark;
1973
1974 TRACE("0x%08x\n", color);
1975 infoPtr->clrInsertMark = color;
1976
1977 return (LRESULT)prevColor;
1978 }
1979
1980
1981 static LRESULT
1982 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1983 {
1984 TRACE("%d %p\n", wParam, item);
1985
1986 if (!TREEVIEW_ValidItem(infoPtr, item))
1987 return 0;
1988
1989 infoPtr->insertBeforeorAfter = wParam;
1990 infoPtr->insertMarkItem = item;
1991
1992 TREEVIEW_Invalidate(infoPtr, NULL);
1993
1994 return 1;
1995 }
1996
1997
1998 /************************************************************************
1999 * Some serious braindamage here. lParam is a pointer to both the
2000 * input HTREEITEM and the output RECT.
2001 */
2002 static LRESULT
2003 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2004 {
2005 TREEVIEW_ITEM *item;
2006 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2007
2008 TRACE("\n");
2009
2010 if (pItem == NULL)
2011 return FALSE;
2012
2013 item = *pItem;
2014 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2015 return FALSE;
2016
2017 /*
2018 * If wParam is TRUE return the text size otherwise return
2019 * the whole item size
2020 */
2021 if (fTextRect)
2022 {
2023 /* Windows does not send TVN_GETDISPINFO here. */
2024
2025 lpRect->top = item->rect.top;
2026 lpRect->bottom = item->rect.bottom;
2027
2028 lpRect->left = item->textOffset;
2029 if (!item->textWidth)
2030 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2031
2032 lpRect->right = item->textOffset + item->textWidth + 4;
2033 }
2034 else
2035 {
2036 *lpRect = item->rect;
2037 }
2038
2039 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2040
2041 return TRUE;
2042 }
2043
2044 static inline LRESULT
2045 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2046 {
2047 /* Surprise! This does not take integral height into account. */
2048 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2049 return infoPtr->clientHeight / infoPtr->uItemHeight;
2050 }
2051
2052
2053 static LRESULT
2054 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2055 {
2056 TREEVIEW_ITEM *item = tvItem->hItem;
2057
2058 if (!TREEVIEW_ValidItem(infoPtr, item))
2059 {
2060 BOOL valid_item = FALSE;
2061 if (!item) return FALSE;
2062
2063 __TRY
2064 {
2065 infoPtr = item->infoPtr;
2066 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2067 valid_item = TREEVIEW_ValidItem(infoPtr, item);
2068 }
2069 __EXCEPT_PAGE_FAULT
2070 {
2071 }
2072 __ENDTRY
2073 if (!valid_item) return FALSE;
2074 }
2075
2076 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2077
2078 if (tvItem->mask & TVIF_CHILDREN)
2079 {
2080 if (item->cChildren==I_CHILDRENCALLBACK)
2081 FIXME("I_CHILDRENCALLBACK not supported\n");
2082 tvItem->cChildren = item->cChildren;
2083 }
2084
2085 if (tvItem->mask & TVIF_HANDLE)
2086 tvItem->hItem = item;
2087
2088 if (tvItem->mask & TVIF_IMAGE)
2089 tvItem->iImage = item->iImage;
2090
2091 if (tvItem->mask & TVIF_INTEGRAL)
2092 tvItem->iIntegral = item->iIntegral;
2093
2094 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2095 tvItem->lParam = item->lParam;
2096
2097 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2098 tvItem->iSelectedImage = item->iSelectedImage;
2099
2100 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2101 tvItem->iExpandedImage = item->iExpandedImage;
2102
2103 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2104 tvItem->state = item->state;
2105
2106 if (tvItem->mask & TVIF_TEXT)
2107 {
2108 if (item->pszText == NULL)
2109 {
2110 if (tvItem->cchTextMax > 0)
2111 tvItem->pszText[0] = '\0';
2112 }
2113 else if (isW)
2114 {
2115 if (item->pszText == LPSTR_TEXTCALLBACKW)
2116 {
2117 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2118 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2119 }
2120 else
2121 {
2122 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2123 }
2124 }
2125 else
2126 {
2127 if (item->pszText == LPSTR_TEXTCALLBACKW)
2128 {
2129 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2130 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2131 }
2132 else
2133 {
2134 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2135 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2136 }
2137 }
2138 }
2139
2140 if (tvItem->mask & TVIF_STATEEX)
2141 {
2142 FIXME("Extended item state not supported, returning 0.\n");
2143 tvItem->uStateEx = 0;
2144 }
2145
2146 TRACE("item <%p>, txt %p, img %d, mask 0x%x\n",
2147 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2148
2149 return TRUE;
2150 }
2151
2152 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2153 * which is wrong. */
2154 static LRESULT
2155 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2156 {
2157 TREEVIEW_ITEM *item;
2158 TREEVIEW_ITEM originalItem;
2159
2160 item = tvItem->hItem;
2161
2162 TRACE("item %d, mask 0x%x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2163 tvItem->mask);
2164
2165 if (!TREEVIEW_ValidItem(infoPtr, item))
2166 return FALSE;
2167
2168 /* store the original item values */
2169 originalItem = *item;
2170
2171 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2172 return FALSE;
2173
2174 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2175 if ((tvItem->mask & TVIF_TEXT
2176 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2177 && ISVISIBLE(item))
2178 {
2179 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2180 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2181 }
2182
2183 if (tvItem->mask != 0 && ISVISIBLE(item))
2184 {
2185 /* The refresh updates everything, but we can't wait until then. */
2186 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2187
2188 /* if any of the item's values changed and it's not a callback, redraw the item */
2189 if (item_changed(&originalItem, item, tvItem))
2190 {
2191 if (tvItem->mask & TVIF_INTEGRAL)
2192 {
2193 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2194 TREEVIEW_UpdateScrollBars(infoPtr);
2195
2196 TREEVIEW_Invalidate(infoPtr, NULL);
2197 }
2198 else
2199 {
2200 TREEVIEW_UpdateScrollBars(infoPtr);
2201 TREEVIEW_Invalidate(infoPtr, item);
2202 }
2203 }
2204 }
2205
2206 return TRUE;
2207 }
2208
2209 static LRESULT
2210 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2211 {
2212 TRACE("\n");
2213
2214 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2215 return 0;
2216
2217 return (item->state & mask);
2218 }
2219
2220 static LRESULT
2221 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2222 {
2223 TREEVIEW_ITEM *retval;
2224
2225 retval = 0;
2226
2227 /* handle all the global data here */
2228 switch (which)
2229 {
2230 case TVGN_CHILD: /* Special case: child of 0 is root */
2231 if (item)
2232 break;
2233 /* fall through */
2234 case TVGN_ROOT:
2235 retval = infoPtr->root->firstChild;
2236 break;
2237
2238 case TVGN_CARET:
2239 retval = infoPtr->selectedItem;
2240 break;
2241
2242 case TVGN_FIRSTVISIBLE:
2243 retval = infoPtr->firstVisible;
2244 break;
2245
2246 case TVGN_DROPHILITE:
2247 retval = infoPtr->dropItem;
2248 break;
2249
2250 case TVGN_LASTVISIBLE:
2251 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2252 break;
2253 }
2254
2255 if (retval)
2256 {
2257 TRACE("flags:0x%x, returns %p\n", which, retval);
2258 return (LRESULT)retval;
2259 }
2260
2261 if (item == TVI_ROOT) item = infoPtr->root;
2262
2263 if (!TREEVIEW_ValidItem(infoPtr, item))
2264 return FALSE;
2265
2266 switch (which)
2267 {
2268 case TVGN_NEXT:
2269 retval = item->nextSibling;
2270 break;
2271 case TVGN_PREVIOUS:
2272 retval = item->prevSibling;
2273 break;
2274 case TVGN_PARENT:
2275 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2276 break;
2277 case TVGN_CHILD:
2278 retval = item->firstChild;
2279 break;
2280 case TVGN_NEXTVISIBLE:
2281 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2282 break;
2283 case TVGN_PREVIOUSVISIBLE:
2284 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2285 break;
2286 default:
2287 TRACE("Unknown msg 0x%x, item %p\n", which, item);
2288 break;
2289 }
2290
2291 TRACE("flags: 0x%x, item %p;returns %p\n", which, item, retval);
2292 return (LRESULT)retval;
2293 }
2294
2295
2296 static LRESULT
2297 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2298 {
2299 TRACE(" %d\n", infoPtr->uNumItems);
2300 return (LRESULT)infoPtr->uNumItems;
2301 }
2302
2303 static VOID
2304 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2305 {
2306 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2307 {
2308 static const unsigned int state_table[] = { 0, 2, 1 };
2309
2310 unsigned int state;
2311
2312 state = STATEIMAGEINDEX(item->state);
2313 TRACE("state: 0x%x\n", state);
2314 item->state &= ~TVIS_STATEIMAGEMASK;
2315
2316 if (state < 3)
2317 state = state_table[state];
2318
2319 item->state |= INDEXTOSTATEIMAGEMASK(state);
2320
2321 TRACE("state: 0x%x\n", state);
2322 TREEVIEW_Invalidate(infoPtr, item);
2323 }
2324 }
2325
2326
2327 /* Painting *************************************************************/
2328
2329 /* Draw the lines and expand button for an item. Also draws one section
2330 * of the line from item's parent to item's parent's next sibling. */
2331 static void
2332 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2333 {
2334 LONG centerx, centery;
2335 BOOL lar = ((infoPtr->dwStyle
2336 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2337 > TVS_LINESATROOT);
2338 HBRUSH hbr, hbrOld;
2339 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2340
2341 if (!lar && item->iLevel == 0)
2342 return;
2343
2344 hbr = CreateSolidBrush(clrBk);
2345 hbrOld = SelectObject(hdc, hbr);
2346
2347 centerx = (item->linesOffset + item->stateOffset) / 2;
2348 centery = (item->rect.top + item->rect.bottom) / 2;
2349
2350 if (infoPtr->dwStyle & TVS_HASLINES)
2351 {
2352 HPEN hOldPen, hNewPen;
2353 HTREEITEM parent;
2354 LOGBRUSH lb;
2355
2356 /* Get a dotted grey pen */
2357 lb.lbStyle = BS_SOLID;
2358 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2359 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2360 hOldPen = SelectObject(hdc, hNewPen);
2361
2362 /* Make sure the center is on a dot (using +2 instead
2363 * of +1 gives us pixel-by-pixel compat with native) */
2364 centery = (centery + 2) & ~1;
2365
2366 MoveToEx(hdc, item->stateOffset, centery, NULL);
2367 LineTo(hdc, centerx - 1, centery);
2368
2369 if (item->prevSibling || item->parent != infoPtr->root)
2370 {
2371 MoveToEx(hdc, centerx, item->rect.top, NULL);
2372 LineTo(hdc, centerx, centery);
2373 }
2374
2375 if (item->nextSibling)
2376 {
2377 MoveToEx(hdc, centerx, centery, NULL);
2378 LineTo(hdc, centerx, item->rect.bottom + 1);
2379 }
2380
2381 /* Draw the line from our parent to its next sibling. */
2382 parent = item->parent;
2383 while (parent != infoPtr->root)
2384 {
2385 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2386
2387 if (parent->nextSibling
2388 /* skip top-levels unless TVS_LINESATROOT */
2389 && parent->stateOffset > parent->linesOffset)
2390 {
2391 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2392 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2393 }
2394
2395 parent = parent->parent;
2396 }
2397
2398 SelectObject(hdc, hOldPen);
2399 DeleteObject(hNewPen);
2400 }
2401
2402 /*
2403 * Display the (+/-) signs
2404 */
2405
2406 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2407 {
2408 if (item->cChildren)
2409 {
2410 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2411 if (theme)
2412 {
2413 RECT glyphRect = item->rect;
2414 glyphRect.left = item->linesOffset;
2415 glyphRect.right = item->stateOffset;
2416 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2417 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2418 &glyphRect, NULL);
2419 }
2420 else
2421 {
2422 LONG height = item->rect.bottom - item->rect.top;
2423 LONG width = item->stateOffset - item->linesOffset;
2424 LONG rectsize = min(height, width) / 4;
2425 /* plussize = ceil(rectsize * 3/4) */
2426 LONG plussize = (rectsize + 1) * 3 / 4;
2427
2428 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2429 HPEN old_pen = SelectObject(hdc, new_pen);
2430
2431 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2432 centerx + rectsize + 2, centery + rectsize + 2);
2433
2434 SelectObject(hdc, old_pen);
2435 DeleteObject(new_pen);
2436
2437 /* draw +/- signs with current text color */
2438 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2439 old_pen = SelectObject(hdc, new_pen);
2440
2441 if (height < 18 || width < 18)
2442 {
2443 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2444 LineTo(hdc, centerx + plussize, centery);
2445
2446 if (!(item->state & TVIS_EXPANDED) ||
2447 (item->state & TVIS_EXPANDPARTIAL))
2448 {
2449 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2450 LineTo(hdc, centerx, centery + plussize);
2451 }
2452 }
2453 else
2454 {
2455 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2456 centerx + plussize, centery + 2);
2457
2458 if (!(item->state & TVIS_EXPANDED) ||
2459 (item->state & TVIS_EXPANDPARTIAL))
2460 {
2461 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2462 centerx + 2, centery + plussize);
2463 SetPixel(hdc, centerx - 1, centery, clrBk);
2464 SetPixel(hdc, centerx + 1, centery, clrBk);
2465 }
2466 }
2467
2468 SelectObject(hdc, old_pen);
2469 DeleteObject(new_pen);
2470 }
2471 }
2472 }
2473 SelectObject(hdc, hbrOld);
2474 DeleteObject(hbr);
2475 }
2476
2477 static void
2478 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2479 {
2480 INT cditem;
2481 HFONT hOldFont;
2482 COLORREF oldTextColor, oldTextBkColor;
2483 int centery;
2484 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2485 NMTVCUSTOMDRAW nmcdhdr;
2486
2487 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2488
2489 /* - If item is drop target or it is selected and window is in focus -
2490 * use blue background (COLOR_HIGHLIGHT).
2491 * - If item is selected, window is not in focus, but it has style
2492 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2493 * - Otherwise - use background color
2494 */
2495 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2496 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2497 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2498 {
2499 if ((item->state & TVIS_DROPHILITED) || inFocus)
2500 {
2501 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2502 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2503 }
2504 else
2505 {
2506 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2507 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2508 }
2509 }
2510 else
2511 {
2512 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2513 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2514 nmcdhdr.clrText = comctl32_color.clrHighlight;
2515 else
2516 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2517 }
2518
2519 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2520
2521 /* The custom draw handler can query the text rectangle,
2522 * so get ready. */
2523 /* should already be known, set to 0 when changed */
2524 if (!item->textWidth)
2525 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2526
2527 cditem = 0;
2528
2529 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2530 {
2531 cditem = TREEVIEW_SendCustomDrawItemNotify
2532 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2533 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2534
2535 if (cditem & CDRF_SKIPDEFAULT)
2536 {
2537 SelectObject(hdc, hOldFont);
2538 return;
2539 }
2540 }
2541
2542 if (cditem & CDRF_NEWFONT)
2543 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2544
2545 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2546
2547 /* Set colors. Custom draw handler can change these so we do this after it. */
2548 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2549 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2550
2551 centery = (item->rect.top + item->rect.bottom) / 2;
2552
2553 /*
2554 * Display the images associated with this item
2555 */
2556 {
2557 INT imageIndex;
2558
2559 /* State images are displayed to the left of the Normal image
2560 * image number is in state; zero should be `display no image'.
2561 */
2562 imageIndex = STATEIMAGEINDEX(item->state);
2563
2564 if (infoPtr->himlState && imageIndex)
2565 {
2566 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2567 item->stateOffset,
2568 centery - infoPtr->stateImageHeight / 2,
2569 ILD_NORMAL);
2570 }
2571
2572 /* Now, draw the normal image; can be either selected,
2573 * non-selected or expanded image.
2574 */
2575
2576 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2577 {
2578 /* The item is currently selected */
2579 imageIndex = item->iSelectedImage;
2580 }
2581 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2582 {
2583 /* The item is currently not selected but expanded */
2584 imageIndex = item->iExpandedImage;
2585 }
2586 else
2587 {
2588 /* The item is not selected and not expanded */
2589 imageIndex = item->iImage;
2590 }
2591
2592 if (infoPtr->himlNormal)
2593 {
2594 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2595
2596 style |= item->state & TVIS_OVERLAYMASK;
2597
2598 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2599 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2600 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2601 style);
2602 }
2603 }
2604
2605
2606 /*
2607 * Display the text associated with this item
2608 */
2609
2610 /* Don't paint item's text if it's being edited */
2611 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2612 {
2613 if (item->pszText)
2614 {
2615 RECT rcText;
2616 UINT align;
2617 SIZE sz;
2618
2619 rcText.top = item->rect.top;
2620 rcText.bottom = item->rect.bottom;
2621 rcText.left = item->textOffset;
2622 rcText.right = rcText.left + item->textWidth + 4;
2623
2624 TRACE("drawing text %s at (%s)\n",
2625 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2626
2627 /* Draw it */
2628 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2629
2630 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2631 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2632 ETO_CLIPPED | ETO_OPAQUE,
2633 &rcText,
2634 item->pszText,
2635 lstrlenW(item->pszText),
2636 NULL);
2637 SetTextAlign(hdc, align);
2638
2639 /* Draw focus box around the selected item */
2640 if ((item == infoPtr->selectedItem) && inFocus)
2641 {
2642 DrawFocusRect(hdc,&rcText);
2643 }
2644 }
2645 }
2646
2647 /* Draw insertion mark if necessary */
2648
2649 if (infoPtr->insertMarkItem)
2650 TRACE("item:%d,mark:%p\n",
2651 TREEVIEW_GetItemIndex(infoPtr, item),
2652 infoPtr->insertMarkItem);
2653
2654 if (item == infoPtr->insertMarkItem)
2655 {
2656 HPEN hNewPen, hOldPen;
2657 int offset;
2658 int left, right;
2659
2660 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2661 hOldPen = SelectObject(hdc, hNewPen);
2662
2663 if (infoPtr->insertBeforeorAfter)
2664 offset = item->rect.bottom - 1;
2665 else
2666 offset = item->rect.top + 1;
2667
2668 left = item->textOffset - 2;
2669 right = item->textOffset + item->textWidth + 2;
2670
2671 MoveToEx(hdc, left, offset - 3, NULL);
2672 LineTo(hdc, left, offset + 4);
2673
2674 MoveToEx(hdc, left, offset, NULL);
2675 LineTo(hdc, right + 1, offset);
2676
2677 MoveToEx(hdc, right, offset + 3, NULL);
2678 LineTo(hdc, right, offset - 4);
2679
2680 SelectObject(hdc, hOldPen);
2681 DeleteObject(hNewPen);
2682 }
2683
2684 /* Restore the hdc state */
2685 SetTextColor(hdc, oldTextColor);
2686 SetBkColor(hdc, oldTextBkColor);
2687 SelectObject(hdc, hOldFont);
2688
2689 if (cditem & CDRF_NOTIFYPOSTPAINT)
2690 {
2691 cditem = TREEVIEW_SendCustomDrawItemNotify
2692 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2693 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2694 }
2695 }
2696
2697 /* Computes treeHeight and treeWidth and updates the scroll bars.
2698 */
2699 static void
2700 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2701 {
2702 TREEVIEW_ITEM *item;
2703 HWND hwnd = infoPtr->hwnd;
2704 BOOL vert = FALSE;
2705 BOOL horz = FALSE;
2706 SCROLLINFO si;
2707 LONG scrollX = infoPtr->scrollX;
2708
2709 infoPtr->treeWidth = 0;
2710 infoPtr->treeHeight = 0;
2711
2712 /* We iterate through all visible items in order to get the tree height
2713 * and width */
2714 item = infoPtr->root->firstChild;
2715
2716 while (item != NULL)
2717 {
2718 if (ISVISIBLE(item))
2719 {
2720 /* actually we draw text at textOffset + 2 */
2721 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2722 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2723
2724 /* This is scroll-adjusted, but we fix this below. */
2725 infoPtr->treeHeight = item->rect.bottom;
2726 }
2727
2728 item = TREEVIEW_GetNextListItem(infoPtr, item);
2729 }
2730
2731 /* Fix the scroll adjusted treeHeight and treeWidth. */
2732 if (infoPtr->root->firstChild)
2733 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2734
2735 infoPtr->treeWidth += infoPtr->scrollX;
2736
2737 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2738
2739 /* Adding one scroll bar may take up enough space that it forces us
2740 * to add the other as well. */
2741 if (infoPtr->treeHeight > infoPtr->clientHeight)
2742 {
2743 vert = TRUE;
2744
2745 if (infoPtr->treeWidth
2746 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2747 horz = TRUE;
2748 }
2749 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2750 horz = TRUE;
2751
2752 if (!vert && horz && infoPtr->treeHeight
2753 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2754 vert = TRUE;
2755
2756 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2757
2758 si.cbSize = sizeof(SCROLLINFO);
2759 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2760 si.nMin = 0;
2761
2762 if (vert)
2763 {
2764 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2765 if ( si.nPage && NULL != infoPtr->firstVisible)
2766 {
2767 si.nPos = infoPtr->firstVisible->visibleOrder;
2768 si.nMax = infoPtr->maxVisibleOrder - 1;
2769
2770 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2771
2772 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2773 ShowScrollBar(hwnd, SB_VERT, TRUE);
2774 infoPtr->uInternalStatus |= TV_VSCROLL;
2775 }
2776 else
2777 {
2778 if (infoPtr->uInternalStatus & TV_VSCROLL)
2779 ShowScrollBar(hwnd, SB_VERT, FALSE);
2780 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2781 }
2782 }
2783 else
2784 {
2785 if (infoPtr->uInternalStatus & TV_VSCROLL)
2786 ShowScrollBar(hwnd, SB_VERT, FALSE);
2787 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2788 }
2789
2790 if (horz)
2791 {
2792 si.nPage = infoPtr->clientWidth;
2793 si.nPos = infoPtr->scrollX;
2794 si.nMax = infoPtr->treeWidth - 1;
2795
2796 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2797 {
2798 si.nPos = si.nMax - max( si.nPage-1, 0 );
2799 scrollX = si.nPos;
2800 }
2801
2802 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2803 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2804 infoPtr->uInternalStatus |= TV_HSCROLL;
2805
2806 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2807 TREEVIEW_HScroll(infoPtr,
2808 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2809 }
2810 else
2811 {
2812 if (infoPtr->uInternalStatus & TV_HSCROLL)
2813 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2814 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2815
2816 scrollX = 0;
2817 if (infoPtr->scrollX != 0)
2818 {
2819 TREEVIEW_HScroll(infoPtr,
2820 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2821 }
2822 }
2823
2824 if (!horz)
2825 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2826 }
2827
2828 static void
2829 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2830 {
2831 HBRUSH hBrush;
2832 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2833
2834 hBrush = CreateSolidBrush(clrBk);
2835 FillRect(hdc, rc, hBrush);
2836 DeleteObject(hBrush);
2837 }
2838
2839 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2840 static LRESULT
2841 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2842 {
2843 RECT rect;
2844
2845 TRACE("%p\n", infoPtr);
2846
2847 GetClientRect(infoPtr->hwnd, &rect);
2848 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2849
2850 return 1;
2851 }
2852
2853 static void
2854 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2855 {
2856 HWND hwnd = infoPtr->hwnd;
2857 RECT rect = *rc;
2858 TREEVIEW_ITEM *item;
2859
2860 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2861 {
2862 TRACE("empty window\n");
2863 return;
2864 }
2865
2866 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2867 hdc, rect);
2868
2869 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2870 {
2871 ReleaseDC(hwnd, hdc);
2872 return;
2873 }
2874
2875 for (item = infoPtr->root->firstChild;
2876 item != NULL;
2877 item = TREEVIEW_GetNextListItem(infoPtr, item))
2878 {
2879 if (ISVISIBLE(item))
2880 {
2881 /* Avoid unneeded calculations */
2882 if (item->rect.top > rect.bottom)
2883 break;
2884 if (item->rect.bottom < rect.top)
2885 continue;
2886
2887 TREEVIEW_DrawItem(infoPtr, hdc, item);
2888 }
2889 }
2890
2891 //
2892 // FIXME: This is correct, but is causes and infinite loop of WM_PAINT
2893 // messages, resulting in continuous painting of the scroll bar in reactos.
2894 // Comment out until the real bug is found. CORE-4912
2895 //
2896 #ifndef __REACTOS__
2897 TREEVIEW_UpdateScrollBars(infoPtr);
2898 #endif
2899
2900 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2901 infoPtr->cdmode =
2902 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2903 }
2904
2905 static inline void
2906 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2907 {
2908 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2909 }
2910
2911 static void
2912 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2913 {
2914 if (item)
2915 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2916 else
2917 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2918 }
2919
2920 static void
2921 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2922 {
2923 RECT rc;
2924 HBITMAP hbm, hbmOld;
2925 HDC hdc, hdcScreen;
2926 int nIndex;
2927
2928 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2929
2930 hdcScreen = GetDC(0);
2931
2932 hdc = CreateCompatibleDC(hdcScreen);
2933 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2934 hbmOld = SelectObject(hdc, hbm);
2935
2936 SetRect(&rc, 0, 0, 48, 16);
2937 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2938
2939 SetRect(&rc, 18, 2, 30, 14);
2940 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2941 DFCS_BUTTONCHECK|DFCS_FLAT);
2942
2943 SetRect(&rc, 34, 2, 46, 14);
2944 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2945 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2946
2947 SelectObject(hdc, hbmOld);
2948 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2949 comctl32_color.clrWindow);
2950 TRACE("checkbox index %d\n", nIndex);
2951
2952 DeleteObject(hbm);
2953 DeleteDC(hdc);
2954 ReleaseDC(0, hdcScreen);
2955
2956 infoPtr->stateImageWidth = 16;
2957 infoPtr->stateImageHeight = 16;
2958 }
2959
2960 static void
2961 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2962 {
2963 TREEVIEW_ITEM *child = item->firstChild;
2964
2965 item->state &= ~TVIS_STATEIMAGEMASK;
2966 item->state |= INDEXTOSTATEIMAGEMASK(1);
2967
2968 while (child)
2969 {
2970 TREEVIEW_ITEM *next = child->nextSibling;
2971 TREEVIEW_ResetImageStateIndex(infoPtr, child);
2972 child = next;
2973 }
2974 }
2975
2976 static LRESULT
2977 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2978 {
2979 HDC hdc;
2980 PAINTSTRUCT ps;
2981 RECT rc;
2982
2983 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2984
2985 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
2986 {
2987 TREEVIEW_InitCheckboxes(infoPtr);
2988 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
2989
2990 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
2991 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
2992 TREEVIEW_UpdateScrollBars(infoPtr);
2993 TREEVIEW_Invalidate(infoPtr, NULL);
2994 }
2995
2996 if (hdc_ref)
2997 {
2998 hdc = hdc_ref;
2999 GetClientRect(infoPtr->hwnd, &rc);
3000 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3001 }
3002 else
3003 {
3004 hdc = BeginPaint(infoPtr->hwnd, &ps);
3005 rc = ps.rcPaint;
3006 if(ps.fErase)
3007 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3008 }
3009
3010 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3011 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3012
3013 if (!hdc_ref)
3014 EndPaint(infoPtr->hwnd, &ps);
3015
3016 return 0;
3017 }
3018
3019 static LRESULT
3020 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3021 {
3022 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3023
3024 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3025 return 0;
3026
3027 if (options & PRF_ERASEBKGND)
3028 TREEVIEW_EraseBackground(infoPtr, hdc);
3029
3030 if (options & PRF_CLIENT)
3031 {
3032 RECT rc;
3033 GetClientRect(infoPtr->hwnd, &rc);
3034 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3035 }
3036
3037 return 0;
3038 }
3039
3040 /* Sorting **************************************************************/
3041
3042 /***************************************************************************
3043 * Forward the DPA local callback to the treeview owner callback
3044 */
3045 static INT WINAPI
3046 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3047 const TVSORTCB *pCallBackSort)
3048 {
3049 /* Forward the call to the client-defined callback */
3050 return pCallBackSort->lpfnCompare(first->lParam,
3051 second->lParam,
3052 pCallBackSort->lParam);
3053 }
3054
3055 /***************************************************************************
3056 * Treeview native sort routine: sort on item text.
3057 */
3058 static INT WINAPI
3059 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3060 const TREEVIEW_INFO *infoPtr)
3061 {
3062 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3063 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3064
3065 if(first->pszText && second->pszText)
3066 return lstrcmpiW(first->pszText, second->pszText);
3067 else if(first->pszText)
3068 return -1;
3069 else if(second->pszText)
3070 return 1;
3071 else
3072 return 0;
3073 }
3074
3075 /* Returns the number of physical children belonging to item. */
3076 static INT
3077 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3078 {
3079 INT cChildren = 0;
3080 HTREEITEM hti;
3081
3082 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3083 cChildren++;
3084
3085 return cChildren;
3086 }
3087
3088 /* Returns a DPA containing a pointer to each physical child of item in
3089 * sibling order. If item has no children, an empty DPA is returned. */
3090 static HDPA
3091 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3092 {
3093 HTREEITEM child;
3094
3095 HDPA list = DPA_Create(8);
3096 if (list == 0) return NULL;
3097
3098 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3099 {
3100 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3101 {
3102 DPA_Destroy(list);
3103 return NULL;
3104 }
3105 }
3106
3107 return list;
3108 }
3109
3110 /***************************************************************************
3111 * Setup the treeview structure with regards of the sort method
3112 * and sort the children of the TV item specified in lParam
3113 * fRecurse: currently unused. Should be zero.
3114 * parent: if pSort!=NULL, should equal pSort->hParent.
3115 * otherwise, item which child items are to be sorted.
3116 * pSort: sort method info. if NULL, sort on item text.
3117 * if non-NULL, sort on item's lParam content, and let the
3118 * application decide what that means. See also TVM_SORTCHILDRENCB.
3119 */
3120
3121 static LRESULT
3122 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3123 LPTVSORTCB pSort)
3124 {
3125 INT cChildren;
3126 PFNDPACOMPARE pfnCompare;
3127 LPARAM lpCompare;
3128
3129 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3130 if (parent == TVI_ROOT || parent == NULL)
3131 parent = infoPtr->root;
3132
3133 /* Check for a valid handle to the parent item */
3134 if (!TREEVIEW_ValidItem(infoPtr, parent))
3135 {
3136 ERR("invalid item hParent=%p\n", parent);
3137 return FALSE;
3138 }
3139
3140 if (pSort)
3141 {
3142 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3143 lpCompare = (LPARAM)pSort;
3144 }
3145 else
3146 {
3147 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3148 lpCompare = (LPARAM)infoPtr;
3149 }
3150
3151 cChildren = TREEVIEW_CountChildren(parent);
3152
3153 /* Make sure there is something to sort */
3154 if (cChildren > 1)
3155 {
3156 /* TREEVIEW_ITEM rechaining */
3157 INT count = 0;
3158 HTREEITEM item = 0;
3159 HTREEITEM nextItem = 0;
3160 HTREEITEM prevItem = 0;
3161
3162 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3163
3164 if (sortList == NULL)
3165 return FALSE;
3166
3167 /* let DPA sort the list */
3168 DPA_Sort(sortList, pfnCompare, lpCompare);
3169
3170 /* The order of DPA entries has been changed, so fixup the
3171 * nextSibling and prevSibling pointers. */
3172
3173 item = DPA_GetPtr(sortList, count++);
3174 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3175 {
3176 /* link the two current item together */
3177 item->nextSibling = nextItem;
3178 nextItem->prevSibling = item;
3179
3180 if (prevItem == NULL)
3181 {
3182 /* this is the first item, update the parent */
3183 parent->firstChild = item;
3184 item->prevSibling = NULL;
3185 }
3186 else
3187 {
3188 /* fix the back chaining */
3189 item->prevSibling = prevItem;
3190 }
3191
3192 /* get ready for the next one */
3193 prevItem = item;
3194 item = nextItem;
3195 }
3196
3197 /* the last item is pointed to by item and never has a sibling */
3198 item->nextSibling = NULL;
3199 parent->lastChild = item;
3200
3201 DPA_Destroy(sortList);
3202
3203 TREEVIEW_VerifyTree(infoPtr);
3204
3205 if (parent->state & TVIS_EXPANDED)
3206 {
3207 int visOrder = infoPtr->firstVisible->visibleOrder;
3208
3209 if (parent == infoPtr->root)
3210 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3211 else
3212 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3213
3214 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3215 {
3216 TREEVIEW_ITEM *item;
3217
3218 for (item = infoPtr->root->firstChild; item != NULL;
3219 item = TREEVIEW_GetNextListItem(infoPtr, item))
3220 {
3221 if (item->visibleOrder == visOrder)
3222 break;
3223 }
3224
3225 if (!item) item = parent->firstChild;
3226 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3227 }
3228
3229 TREEVIEW_Invalidate(infoPtr, NULL);
3230 }
3231
3232 return TRUE;
3233 }
3234 return FALSE;
3235 }
3236
3237
3238 /***************************************************************************
3239 * Setup the treeview structure with regards of the sort method
3240 * and sort the children of the TV item specified in lParam
3241 */
3242 static LRESULT
3243 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3244 {
3245 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3246 }
3247
3248
3249 /***************************************************************************
3250 * Sort the children of the TV item specified in lParam.
3251 */
3252 static LRESULT
3253 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3254 {
3255 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3256 }
3257
3258
3259 /* Expansion/Collapse ***************************************************/
3260
3261 static BOOL
3262 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3263 UINT action)
3264 {
3265 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3266 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3267 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3268 0, item);
3269 }
3270
3271 static VOID
3272 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3273 UINT action)
3274 {
3275 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3276 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3277 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3278 0, item);
3279 }
3280
3281
3282 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3283 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3284 static BOOL
3285 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3286 BOOL bRemoveChildren, BOOL bUser)
3287 {
3288 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3289 BOOL bSetSelection, bSetFirstVisible;
3290 RECT scrollRect;
3291 LONG scrollDist = 0;
3292 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3293 BOOL wasExpanded;
3294
3295 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3296
3297 if (!TREEVIEW_HasChildren(infoPtr, item))
3298 return FALSE;
3299
3300 if (bUser)
3301 TREEVIEW_SendExpanding(infoPtr, item, action);
3302
3303 if (item->firstChild == NULL)
3304 return FALSE;
3305
3306 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3307 item->state &= ~TVIS_EXPANDED;
3308
3309 if (wasExpanded && bUser)
3310 TREEVIEW_SendExpanded(infoPtr, item, action);
3311
3312 bSetSelection = (infoPtr->selectedItem != NULL
3313 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3314
3315 bSetFirstVisible = (infoPtr->firstVisible != NULL
3316 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3317
3318 tmpItem = item;
3319 while (tmpItem)
3320 {
3321 if (tmpItem->nextSibling)
3322 {
3323 nextItem = tmpItem->nextSibling;
3324 break;
3325 }
3326 tmpItem = tmpItem->parent;
3327 }
3328
3329 if (nextItem)
3330 scrollDist = nextItem->rect.top;
3331
3332 if (bRemoveChildren)
3333 {
3334 INT old_cChildren = item->cChildren;
3335 TRACE("TVE_COLLAPSERESET\n");
3336 item->state &= ~TVIS_EXPANDEDONCE;
3337 TREEVIEW_RemoveAllChildren(infoPtr, item);
3338 item->cChildren = old_cChildren;
3339 }
3340 if (!wasExpanded)
3341 return FALSE;
3342
3343 if (item->firstChild)
3344 {
3345 TREEVIEW_ITEM *i, *sibling;
3346
3347 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3348
3349 for (i = item->firstChild; i != sibling;
3350 i = TREEVIEW_GetNextListItem(infoPtr, i))
3351 {
3352 i->visibleOrder = -1;
3353 }
3354 }
3355
3356 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3357
3358 if (nextItem)
3359 scrollDist = -(scrollDist - nextItem->rect.top);
3360
3361 if (bSetSelection)
3362 {
3363 /* Don't call DoSelectItem, it sends notifications. */
3364 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3365 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3366 item->state |= TVIS_SELECTED;
3367 infoPtr->selectedItem = item;
3368 }
3369
3370 TREEVIEW_UpdateScrollBars(infoPtr);
3371
3372 scrollRect.left = 0;
3373 scrollRect.right = infoPtr->clientWidth;
3374 scrollRect.bottom = infoPtr->clientHeight;
3375
3376 if (nextItem)
3377 {
3378 scrollRect.top = nextItem->rect.top;
3379
3380 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3381 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3382 TREEVIEW_Invalidate(infoPtr, item);
3383 } else {
3384 scrollRect.top = item->rect.top;
3385 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3386 }
3387
3388 TREEVIEW_SetFirstVisible(infoPtr,
3389 bSetFirstVisible ? item : infoPtr->firstVisible,
3390 TRUE);
3391
3392 return wasExpanded;
3393 }
3394
3395 static BOOL
3396 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3397 BOOL partial, BOOL user)
3398 {
3399 LONG scrollDist;
3400 LONG orgNextTop = 0;
3401 RECT scrollRect;
3402 TREEVIEW_ITEM *nextItem, *tmpItem;
3403 BOOL sendsNotifications;
3404
3405 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3406
3407 if (!TREEVIEW_HasChildren(infoPtr, item))
3408 return FALSE;
3409
3410 tmpItem = item; nextItem = NULL;
3411 while (tmpItem)
3412 {
3413 if (tmpItem->nextSibling)
3414 {
3415 nextItem = tmpItem->nextSibling;
3416 break;
3417 }
3418 tmpItem = tmpItem->parent;
3419 }
3420
3421 if (nextItem)
3422 orgNextTop = nextItem->rect.top;
3423
3424 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3425
3426 sendsNotifications = user || ((item->cChildren != 0) &&
3427 !(item->state & TVIS_EXPANDEDONCE));
3428 if (sendsNotifications)
3429 {
3430 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3431 {
3432 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3433 return FALSE;
3434 }
3435 }
3436 if (!item->firstChild)
3437 return FALSE;
3438
3439 item->state |= TVIS_EXPANDED;
3440
3441 if (partial)
3442 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3443
3444 if (ISVISIBLE(item))
3445 {
3446 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3447 TREEVIEW_UpdateSubTree(infoPtr, item);
3448 TREEVIEW_UpdateScrollBars(infoPtr);
3449
3450 scrollRect.left = 0;
3451 scrollRect.bottom = infoPtr->treeHeight;
3452 scrollRect.right = infoPtr->clientWidth;
3453 if (nextItem)
3454 {
3455 scrollDist = nextItem->rect.top - orgNextTop;
3456 scrollRect.top = orgNextTop;
3457
3458 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3459 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3460 TREEVIEW_Invalidate (infoPtr, item);
3461 } else {
3462 scrollRect.top = item->rect.top;
3463 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3464 }
3465
3466 /* Scroll up so that as many children as possible are visible.
3467 * This fails when expanding causes an HScroll bar to appear, but we
3468 * don't know that yet, so the last item is obscured. */
3469 if (item->firstChild != NULL)
3470 {
3471 int nChildren = item->lastChild->visibleOrder
3472 - item->firstChild->visibleOrder + 1;
3473
3474 int visible_pos = item->visibleOrder
3475 - infoPtr->firstVisible->visibleOrder;
3476
3477 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3478
3479 if (visible_pos > 0 && nChildren > rows_below)
3480 {
3481 int scroll = nChildren - rows_below;
3482
3483 if (scroll > visible_pos)
3484 scroll = visible_pos;
3485
3486 if (scroll > 0)
3487 {
3488 TREEVIEW_ITEM *newFirstVisible
3489 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3490 scroll);
3491
3492
3493 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3494 }
3495 }
3496 }
3497 }
3498
3499 if (sendsNotifications) {
3500 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3501 item->state |= TVIS_EXPANDEDONCE;
3502 }
3503
3504 return TRUE;
3505 }
3506
3507 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3508 to mouse messages and TVM_SELECTITEM.
3509
3510 selection - previously selected item, used to collapse a part of a tree
3511 item - new selected item
3512 */
3513 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3514 HTREEITEM selection, HTREEITEM item)
3515 {
3516 TREEVIEW_ITEM *prev, *curr;
3517
3518 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3519
3520 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3521
3522 /*
3523 * Close the previous item and its ancestors as long as they are not
3524 * ancestors of the current item
3525 */
3526 for (prev = selection; prev && TREEVIEW_ValidItem(infoPtr, prev); prev = prev->parent)
3527 {
3528 for (curr = item; curr && TREEVIEW_ValidItem(infoPtr, curr); curr = curr->parent)
3529 {
3530 if (curr == prev)
3531 goto finish;
3532 }
3533 TREEVIEW_Collapse(infoPtr, prev, FALSE, TRUE);
3534 }
3535
3536 finish:
3537 /*
3538 * Expand the current item
3539 */
3540 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3541 }
3542
3543 static BOOL
3544 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3545 {
3546 TRACE("item=%p, user=%d\n", item, user);
3547
3548 if (item->state & TVIS_EXPANDED)
3549 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3550 else
3551 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3552 }
3553
3554 static VOID
3555 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3556 {
3557 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3558
3559 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3560 {
3561 if (TREEVIEW_HasChildren(infoPtr, item))
3562 TREEVIEW_ExpandAll(infoPtr, item);
3563 }
3564 }
3565
3566 /* Note:If the specified item is the child of a collapsed parent item,
3567 the parent's list of child items is (recursively) expanded to reveal the
3568 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3569 know if it also applies here.
3570 */
3571
3572 static LRESULT
3573 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3574 {
3575 if (!TREEVIEW_ValidItem(infoPtr, item))
3576 return 0;
3577
3578 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3579 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3580 flag, item->state);
3581
3582 switch (flag & TVE_TOGGLE)
3583 {
3584 case TVE_COLLAPSE:
3585 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3586 FALSE);
3587
3588 case TVE_EXPAND:
3589 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3590 FALSE);
3591
3592 case TVE_TOGGLE:
3593 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3594
3595 default:
3596 return 0;
3597 }
3598 }
3599
3600 /* Hit-Testing **********************************************************/
3601
3602 static TREEVIEW_ITEM *
3603 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3604 {
3605 TREEVIEW_ITEM *item;
3606 LONG row;
3607
3608 if (!infoPtr->firstVisible)
3609 return NULL;
3610
3611 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3612
3613 for (item = infoPtr->firstVisible; item != NULL;
3614 item = TREEVIEW_GetNextListItem(infoPtr, item))
3615 {
3616 if (row >= item->visibleOrder
3617 && row < item->visibleOrder + item->iIntegral)
3618 break;
3619 }
3620
3621 return item;
3622 }
3623
3624 static LRESULT
3625 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3626 {
3627 TREEVIEW_ITEM *item;
3628 RECT rect;
3629 UINT status;
3630 LONG x, y;
3631
3632 lpht->hItem = 0;
3633 GetClientRect(infoPtr->hwnd, &rect);
3634 status = 0;
3635 x = lpht->pt.x;
3636 y = lpht->pt.y;
3637
3638 if (x < rect.left)
3639 {
3640 status |= TVHT_TOLEFT;
3641 }
3642 else if (x > rect.right)
3643 {
3644 status |= TVHT_TORIGHT;
3645 }
3646
3647 if (y < rect.top)
3648 {
3649 status |= TVHT_ABOVE;
3650 }
3651 else if (y > rect.bottom)
3652 {
3653 status |= TVHT_BELOW;
3654 }
3655
3656 if (status)
3657 {
3658 lpht->flags = status;
3659 return 0;
3660 }
3661
3662 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3663 if (!item)
3664 {
3665 lpht->flags = TVHT_NOWHERE;
3666 return 0;
3667 }
3668
3669 if (x >= item->textOffset + item->textWidth)
3670 {
3671 lpht->flags = TVHT_ONITEMRIGHT;
3672 }
3673 else if (x >= item->textOffset)
3674 {
3675 lpht->flags = TVHT_ONITEMLABEL;
3676 }
3677 else if (x >= item->imageOffset)
3678 {
3679 lpht->flags = TVHT_ONITEMICON;
3680 }
3681 else if (x >= item->stateOffset)
3682 {
3683 lpht->flags = TVHT_ONITEMSTATEICON;
3684 }
3685 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3686 {
3687 lpht->flags = TVHT_ONITEMBUTTON;
3688 }
3689 else
3690 {
3691 lpht->flags = TVHT_ONITEMINDENT;
3692 }
3693
3694 lpht->hItem = item;
3695 TRACE("(%d,%d):result 0x%x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3696
3697 return (LRESULT)item;
3698 }
3699
3700 /* Item Label Editing ***************************************************/
3701
3702 static LRESULT
3703 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3704 {
3705 return (LRESULT)infoPtr->hwndEdit;
3706 }
3707
3708 static LRESULT CALLBACK
3709 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3710 {
3711 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3712 BOOL bCancel = FALSE;
3713 LRESULT rc;
3714
3715 switch (uMsg)
3716 {
3717 case WM_PAINT:
3718 TRACE("WM_PAINT start\n");
3719 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3720 lParam);
3721 TRACE("WM_PAINT done\n");
3722 return rc;
3723
3724 case WM_KILLFOCUS:
3725 if (infoPtr->bIgnoreEditKillFocus)
3726 return TRUE;
3727 break;
3728
3729 case WM_DESTROY:
3730 {
3731 WNDPROC editProc = infoPtr->wpEditOrig;
3732 infoPtr->wpEditOrig = 0;
3733 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3734 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3735 }
3736
3737 case WM_GETDLGCODE:
3738 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3739
3740 case WM_KEYDOWN:
3741 if (wParam == VK_ESCAPE)
3742 {
3743 bCancel = TRUE;
3744 break;
3745 }
3746 else if (wParam == VK_RETURN)
3747 {
3748 break;
3749 }
3750
3751 /* fall through */
3752 default:
3753 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3754 }
3755
3756 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3757 /* eg. Using a messagebox */
3758
3759 infoPtr->bIgnoreEditKillFocus = TRUE;
3760 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3761 infoPtr->bIgnoreEditKillFocus = FALSE;
3762
3763 return 0;
3764 }
3765
3766
3767 /* should handle edit control messages here */
3768
3769 static LRESULT
3770 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3771 {
3772 TRACE("code=0x%x, id=0x%x, handle=0x%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3773
3774 switch (HIWORD(wParam))
3775 {
3776 case EN_UPDATE:
3777 {
3778 /*
3779 * Adjust the edit window size
3780 */
3781 WCHAR buffer[1024];
3782 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3783 HDC hdc = GetDC(infoPtr->hwndEdit);
3784 SIZE sz;
3785 HFONT hFont, hOldFont = 0;
3786
3787 TRACE("edit=%p\n", infoPtr->hwndEdit);
3788
3789 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3790
3791 infoPtr->bLabelChanged = TRUE;
3792
3793 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3794
3795 /* Select font to get the right dimension of the string */
3796 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3797
3798 if (hFont != 0)
3799 {
3800 hOldFont = SelectObject(hdc, hFont);
3801 }
3802
3803 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3804 {
3805 TEXTMETRICW textMetric;
3806
3807 /* Add Extra spacing for the next character */
3808 GetTextMetricsW(hdc, &textMetric);
3809 sz.cx += (textMetric.tmMaxCharWidth * 2);
3810
3811 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3812 sz.cx = min(sz.cx,
3813 infoPtr->clientWidth - editItem->textOffset + 2);
3814
3815 SetWindowPos(infoPtr->hwndEdit,
3816 HWND_TOP,
3817 0,
3818 0,
3819 sz.cx,
3820 editItem->rect.bottom - editItem->rect.top + 3,
3821 SWP_NOMOVE | SWP_DRAWFRAME);
3822 }
3823
3824 if (hFont != 0)
3825 {
3826 SelectObject(hdc, hOldFont);
3827 }
3828
3829 ReleaseDC(infoPtr->hwnd, hdc);
3830 break;
3831 }
3832 case EN_KILLFOCUS:
3833 /* apparently we should respect passed handle value */
3834 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3835
3836 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3837 break;
3838
3839 default:
3840 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3841 }
3842
3843 return 0;
3844 }
3845
3846 static HWND
3847 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3848 {
3849 HWND hwnd = infoPtr->hwnd;
3850 HWND hwndEdit;
3851 SIZE sz;
3852 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3853 HDC hdc;
3854 HFONT hOldFont=0;
3855 TEXTMETRICW textMetric;
3856
3857 TRACE("%p %p\n", hwnd, hItem);
3858 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3859 return NULL;
3860
3861 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3862 return NULL;
3863
3864 if (infoPtr->hwndEdit)
3865 return infoPtr->hwndEdit;
3866
3867 infoPtr->bLabelChanged = FALSE;
3868
3869 /* make edit item visible */
3870 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3871
3872 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3873
3874 hdc = GetDC(hwnd);
3875 /* Select the font to get appropriate metric dimensions */
3876 if (infoPtr->hFont != 0)
3877 {
3878 hOldFont = SelectObject(hdc, infoPtr->hFont);
3879 }
3880
3881 /* Get string length in pixels */
3882 if (hItem->pszText)
3883 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3884 &sz);
3885 else
3886 GetTextExtentPoint32A(hdc, "", 0, &sz);
3887
3888 /* Add Extra spacing for the next character */
3889 GetTextMetricsW(hdc, &textMetric);
3890 sz.cx += (textMetric.tmMaxCharWidth * 2);
3891
3892 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3893 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3894
3895 if (infoPtr->hFont != 0)
3896 {
3897 SelectObject(hdc, hOldFont);
3898 }
3899
3900 ReleaseDC(hwnd, hdc);
3901
3902 infoPtr->editItem = hItem;
3903
3904 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3905 WC_EDITW,
3906 0,
3907 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3908 WS_CLIPSIBLINGS | ES_WANTRETURN |
3909 ES_LEFT, hItem->textOffset - 2,
3910 hItem->rect.top - 1, sz.cx + 3,
3911 hItem->rect.bottom -
3912 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3913 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3914
3915 infoPtr->hwndEdit = hwndEdit;
3916
3917 /* Get a 2D border. */
3918 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3919 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3920 SetWindowLongW(hwndEdit, GWL_STYLE,
3921 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3922
3923 SendMessageW(hwndEdit, WM_SETFONT,
3924 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3925
3926 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3927 (DWORD_PTR)
3928 TREEVIEW_Edit_SubclassProc);
3929 if (hItem->pszText)
3930 SetWindowTextW(hwndEdit, hItem->pszText);
3931
3932 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3933 {
3934 DestroyWindow(hwndEdit);
3935 infoPtr->hwndEdit = 0;
3936 infoPtr->editItem = NULL;
3937 return NULL;
3938 }
3939
3940 SetFocus(hwndEdit);
3941 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3942 ShowWindow(hwndEdit, SW_SHOW);
3943
3944 return hwndEdit;
3945 }
3946
3947
3948 static LRESULT
3949 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3950 {
3951 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3952 NMTVDISPINFOW tvdi;
3953 BOOL bCommit;
3954 WCHAR tmpText[1024] = { '\0' };
3955 WCHAR *newText = tmpText;
3956 int iLength = 0;
3957
3958 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3959
3960 tvdi.item.mask = 0;
3961 tvdi.item.hItem = editedItem;
3962 tvdi.item.state = editedItem->state;
3963 tvdi.item.lParam = editedItem->lParam;
3964
3965 if (!bCancel)
3966 {
3967 if (!infoPtr->bNtfUnicode)
3968 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3969 else
3970 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3971
3972 if (iLength >= 1023)
3973 {
3974 ERR("Insufficient space to retrieve new item label\n");
3975 }
3976
3977 tvdi.item.mask = TVIF_TEXT;
3978 tvdi.item.pszText = tmpText;
3979 tvdi.item.cchTextMax = iLength + 1;
3980 }
3981 else
3982 {
3983 tvdi.item.pszText = NULL;
3984 tvdi.item.cchTextMax = 0;
3985 }
3986
3987 bCommit = TREEVIEW_SendRealNotify(infoPtr, TVN_ENDLABELEDITW, &tvdi.hdr);
3988
3989 if (!bCancel && bCommit) /* Apply the changes */
3990 {
3991 if (!infoPtr->bNtfUnicode)
3992 {
3993 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3994 newText = Alloc(len * sizeof(WCHAR));
3995 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3996 iLength = len - 1;
3997 }
3998
3999 if (strcmpW(newText, editedItem->pszText) != 0)
4000 {
4001 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
4002 if (ptr == NULL)
4003 {
4004 ERR("OutOfMemory, cannot allocate space for label\n");
4005 if(newText != tmpText) Free(newText);
4006 DestroyWindow(infoPtr->hwndEdit);
4007 infoPtr->hwndEdit = 0;
4008 infoPtr->editItem = NULL;
4009 return FALSE;
4010 }
4011 else
4012 {
4013 editedItem->pszText = ptr;
4014 editedItem->cchTextMax = iLength + 1;
4015 strcpyW(editedItem->pszText, newText);
4016 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4017 }
4018 }
4019 if(newText != tmpText) Free(newText);
4020 }
4021
4022 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4023 DestroyWindow(infoPtr->hwndEdit);
4024 infoPtr->hwndEdit = 0;
4025 infoPtr->editItem = NULL;
4026 return TRUE;
4027 }
4028
4029 static LRESULT
4030 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4031 {
4032 if (wParam != TV_EDIT_TIMER)
4033 {
4034 ERR("got unknown timer\n");
4035 return 1;
4036 }
4037
4038 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4039 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4040
4041 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4042
4043 return 0;
4044 }
4045
4046
4047 /* Mouse Tracking/Drag **************************************************/
4048
4049 /***************************************************************************
4050 * This is quite unusual piece of code, but that's how it's implemented in
4051 * Windows.
4052 */
4053 static LRESULT
4054 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4055 {
4056 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4057 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4058 RECT r;
4059 MSG msg;
4060
4061 r.top = pt.y - cyDrag;
4062 r.left = pt.x - cxDrag;
4063 r.bottom = pt.y + cyDrag;
4064 r.right = pt.x + cxDrag;
4065
4066 SetCapture(infoPtr->hwnd);
4067
4068 while (1)
4069 {
4070 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4071 {
4072 if (msg.message == WM_MOUSEMOVE)
4073 {
4074 pt.x = (short)LOWORD(msg.lParam);
4075 pt.y = (short)HIWORD(msg.lParam);
4076 if (PtInRect(&r, pt))
4077 continue;
4078 else
4079 {
4080 ReleaseCapture();
4081 return 1;
4082 }
4083 }
4084 else if (msg.message >= WM_LBUTTONDOWN &&
4085 msg.message <= WM_RBUTTONDBLCLK)
4086 {
4087 break;
4088 }
4089
4090 DispatchMessageW(&msg);
4091 }
4092
4093 if (GetCapture() != infoPtr->hwnd)
4094 return 0;
4095 }
4096
4097 ReleaseCapture();
4098 return 0;
4099 }
4100
4101
4102 static LRESULT
4103 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4104 {
4105 TREEVIEW_ITEM *item;
4106 TVHITTESTINFO hit;
4107
4108 TRACE("\n");
4109 SetFocus(infoPtr->hwnd);
4110
4111 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4112 {
4113 /* If there is pending 'edit label' event - kill it now */
4114 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4115 }
4116
4117 hit.pt.x = (short)LOWORD(lParam);
4118 hit.pt.y = (short)HIWORD(lParam);
4119
4120 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4121 if (!item)
4122 return 0;
4123 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4124
4125 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4126 { /* FIXME! */
4127 switch (hit.flags)
4128 {
4129 case TVHT_ONITEMRIGHT:
4130 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4131 break;
4132
4133 case TVHT_ONITEMINDENT:
4134 if (!(infoPtr->dwStyle & TVS_HASLINES))
4135 {
4136 break;
4137 }
4138 else
4139 {
4140 int level = hit.pt.x / infoPtr->uIndent;
4141 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4142
4143 while (item->iLevel > level)
4144 {
4145 item = item->parent;
4146 }
4147
4148 /* fall through */
4149 }
4150
4151 case TVHT_ONITEMLABEL:
4152 case TVHT_ONITEMICON:
4153 case TVHT_ONITEMBUTTON:
4154 TREEVIEW_Toggle(infoPtr, item, TRUE);
4155 break;
4156
4157 case TVHT_ONITEMSTATEICON:
4158 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4159 TREEVIEW_ToggleItemState(infoPtr, item);
4160 else
4161 TREEVIEW_Toggle(infoPtr, item, TRUE);
4162 break;
4163 }
4164 }
4165 return TRUE;
4166 }
4167
4168
4169 static LRESULT
4170 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4171 {
4172 HWND hwnd = infoPtr->hwnd;
4173 TVHITTESTINFO ht;
4174 BOOL bTrack, bDoLabelEdit;
4175
4176 /* If Edit control is active - kill it and return.
4177 * The best way to do it is to set focus to itself.
4178 * Edit control subclassed procedure will automatically call
4179 * EndEditLabelNow.
4180 */
4181 if (infoPtr->hwndEdit)
4182 {
4183 SetFocus(hwnd);
4184 return 0;
4185 }
4186
4187 ht.pt.x = (short)LOWORD(lParam);
4188 ht.pt.y = (short)HIWORD(lParam);
4189
4190 TREEVIEW_HitTest(infoPtr, &ht);
4191 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4192
4193 /* update focusedItem and redraw both items */
4194 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4195 {
4196 infoPtr->focusedItem = ht.hItem;
4197 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4198 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4199 }
4200
4201 bTrack = (ht.flags & TVHT_ONITEM)
4202 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4203
4204 /*
4205 * If the style allows editing and the node is already selected
4206 * and the click occurred on the item label...
4207 */
4208 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4209 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4210
4211 /* Send NM_CLICK right away */
4212 if (!bTrack)
4213 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4214 goto setfocus;
4215
4216 if (ht.flags & TVHT_ONITEMBUTTON)
4217 {
4218 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4219 goto setfocus;
4220 }
4221 else if (bTrack)
4222 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4223 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4224 {
4225 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4226 infoPtr->dropItem = ht.hItem;
4227
4228 /* clean up focusedItem as we dragged and won't select this item */
4229 if(infoPtr->focusedItem)
4230 {
4231 /* refresh the item that was focused */
4232 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4233 infoPtr->focusedItem = NULL;
4234
4235 /* refresh the selected item to return the filled background */
4236 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4237 }
4238
4239 return 0;
4240 }
4241 }
4242
4243 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4244 goto setfocus;
4245
4246 if (bDoLabelEdit)
4247 {
4248 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4249 KillTimer(hwnd, TV_EDIT_TIMER);
4250
4251 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4252 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4253 }
4254 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4255 {
4256 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4257
4258 /* Select the current item */
4259 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4260 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4261 }
4262 else if (ht.flags & TVHT_ONITEMSTATEICON)
4263 {
4264 /* TVS_CHECKBOXES requires us to toggle the current state */
4265 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4266 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4267 }
4268
4269 setfocus:
4270 SetFocus(hwnd);
4271 return 0;
4272 }
4273
4274
4275 static LRESULT
4276 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4277 {
4278 TVHITTESTINFO ht;
4279
4280 if (infoPtr->hwndEdit)
4281 {
4282 SetFocus(infoPtr->hwnd);
4283 return 0;
4284 }
4285
4286 ht.pt.x = (short)LOWORD(lParam);
4287 ht.pt.y = (short)HIWORD(lParam);
4288
4289 TREEVIEW_HitTest(infoPtr, &ht);
4290
4291 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4292 {
4293 if (ht.hItem)
4294 {
4295 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4296 infoPtr->dropItem = ht.hItem;
4297 }
4298 }
4299 else
4300 {
4301 SetFocus(infoPtr->hwnd);
4302 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4303 {
4304 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4305 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4306 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4307 }
4308 }
4309
4310 return 0;
4311 }
4312
4313 static LRESULT
4314 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4315 {
4316 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4317 INT cx, cy;
4318 HDC hdc, htopdc;
4319 HWND hwtop;
4320 HBITMAP hbmp, hOldbmp;
4321 SIZE size;
4322 RECT rc;
4323 HFONT hOldFont;
4324
4325 TRACE("\n");
4326
4327 if (!(infoPtr->himlNormal))
4328 return 0;
4329
4330 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4331 return 0;
4332
4333 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4334
4335 hwtop = GetDesktopWindow();
4336 htopdc = GetDC(hwtop);
4337 hdc = CreateCompatibleDC(htopdc);
4338
4339 hOldFont = SelectObject(hdc, infoPtr->hFont);
4340
4341 if (dragItem->pszText)
4342 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4343 &size);
4344 else
4345 GetTextExtentPoint32A(hdc, "", 0, &size);
4346
4347 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4348 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4349 hOldbmp = SelectObject(hdc, hbmp);
4350
4351 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4352 size.cx += cx;
4353 if (cy > size.cy)
4354 size.cy = cy;
4355
4356 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4357 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4358 ILD_NORMAL);
4359
4360 /*
4361 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4362 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4363 */
4364
4365 /* draw item text */
4366
4367 SetRect(&rc, cx, 0, size.cx, size.cy);
4368
4369 if (dragItem->pszText)
4370 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4371 DT_LEFT);
4372
4373 SelectObject(hdc, hOldFont);
4374 SelectObject(hdc, hOldbmp);
4375
4376 ImageList_Add(infoPtr->dragList, hbmp, 0);
4377
4378 DeleteDC(hdc);
4379 DeleteObject(hbmp);
4380 ReleaseDC(hwtop, htopdc);
4381
4382 return (LRESULT)infoPtr->dragList;
4383 }
4384
4385 /* Selection ************************************************************/
4386
4387 static LRESULT
4388 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4389 INT cause)
4390 {
4391 TREEVIEW_ITEM *prevSelect;
4392
4393 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4394
4395 TRACE("Entering item %p (%s), flag 0x%x, cause 0x%x, state %d\n",
4396 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4397 newSelect ? newSelect->state : 0);
4398
4399 /* reset and redraw focusedItem if focusedItem was set so we don't */
4400 /* have to worry about the previously focused item when we set a new one */
4401 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4402 infoPtr->focusedItem = NULL;
4403
4404 switch (action)
4405 {
4406 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4407 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4408 /* Fall through */
4409 case TVGN_CARET:
4410 prevSelect = infoPtr->selectedItem;
4411
4412 if (prevSelect == newSelect) {
4413 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4414 break;
4415 }
4416
4417 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4418 TVN_SELCHANGINGW,
4419 cause,
4420 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4421 prevSelect,
4422 newSelect))
4423 return FALSE;
4424
4425 if (prevSelect)
4426 prevSelect->state &= ~TVIS_SELECTED;
4427 if (newSelect)
4428 newSelect->state |= TVIS_SELECTED;
4429
4430 infoPtr->selectedItem = newSelect;
4431
4432 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4433
4434 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4435 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4436
4437 TREEVIEW_SendTreeviewNotify(infoPtr,
4438 TVN_SELCHANGEDW,
4439 cause,
4440 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4441 prevSelect,
4442 newSelect);
4443 break;
4444
4445 case TVGN_DROPHILITE:
4446 prevSelect = infoPtr->dropItem;
4447
4448 if (prevSelect)
4449 prevSelect->state &= ~TVIS_DROPHILITED;
4450
4451 infoPtr->dropItem = newSelect;
4452
4453 if (newSelect)
4454 newSelect->state |= TVIS_DROPHILITED;
4455
4456 TREEVIEW_Invalidate(infoPtr, prevSelect);
4457 TREEVIEW_Invalidate(infoPtr, newSelect);
4458 break;
4459
4460 case TVGN_FIRSTVISIBLE:
4461 if (newSelect != NULL)
4462 {
4463 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4464 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4465 TREEVIEW_Invalidate(infoPtr, NULL);
4466 }
4467 break;
4468 }
4469
4470 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4471 return TRUE;
4472 }
4473
4474 /* FIXME: handle NM_KILLFOCUS etc */
4475 static LRESULT
4476 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4477 {
4478 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4479
4480 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4481 return FALSE;
4482
4483 if (item == infoPtr->selectedItem)
4484 return TRUE;
4485
4486 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4487
4488 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4489 return FALSE;
4490
4491 TREEVIEW_SingleExpand(infoPtr, selection, item);
4492
4493 return TRUE;
4494 }
4495
4496 /*************************************************************************
4497 * TREEVIEW_ProcessLetterKeys
4498 *
4499 * Processes keyboard messages generated by pressing the letter keys
4500 * on the keyboard.
4501 * What this does is perform a case insensitive search from the
4502 * current position with the following quirks:
4503 * - If two chars or more are pressed in quick succession we search
4504 * for the corresponding string (e.g. 'abc').
4505 * - If there is a delay we wipe away the current search string and
4506 * restart with just that char.
4507 * - If the user keeps pressing the same character, whether slowly or
4508 * fast, so that the search string is entirely composed of this
4509 * character ('aaaaa' for instance), then we search for first item
4510 * that starting with that character.
4511 * - If the user types the above character in quick succession, then
4512 * we must also search for the corresponding string ('aaaaa'), and
4513 * go to that string if there is a match.
4514 *
4515 * RETURNS
4516 *
4517 * Zero.
4518 *
4519 * BUGS
4520 *
4521 * - The current implementation has a list of characters it will
4522 * accept and it ignores everything else. In particular it will
4523 * ignore accentuated characters which seems to match what
4524 * Windows does. But I'm not sure it makes sense to follow
4525 * Windows there.
4526 * - We don't sound a beep when the search fails.
4527 * - The search should start from the focused item, not from the selected
4528 * item. One reason for this is to allow for multiple selections in trees.
4529 * But currently infoPtr->focusedItem does not seem very usable.
4530 *
4531 * SEE ALSO
4532 *
4533 * TREEVIEW_ProcessLetterKeys
4534 */
4535 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4536 {
4537 HTREEITEM nItem;
4538 HTREEITEM endidx,idx;
4539 TVITEMEXW item;
4540 WCHAR buffer[MAX_PATH];
4541 DWORD timestamp,elapsed;
4542
4543 /* simple parameter checking */
4544 if (!charCode || !keyData) return 0;
4545
4546 /* only allow the valid WM_CHARs through */
4547 if (!isalnum(charCode) &&
4548 charCode != '.' && charCode != '`' && charCode != '!' &&
4549 charCode != '@' && charCode != '#' && charCode != '$' &&
4550 charCode != '%' && charCode != '^' && charCode != '&' &&
4551 charCode != '*' && charCode != '(' && charCode != ')' &&
4552 charCode != '-' && charCode != '_' && charCode != '+' &&
4553 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4554 charCode != '}' && charCode != '[' && charCode != '{' &&
4555 charCode != '/' && charCode != '?' && charCode != '>' &&
4556 charCode != '<' && charCode != ',' && charCode != '~')
4557 return 0;
4558
4559 /* compute how much time elapsed since last keypress */
4560 timestamp = GetTickCount();
4561 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4562 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4563 } else {
4564 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4565 }
4566
4567 /* update the search parameters */
4568 infoPtr->lastKeyPressTimestamp=timestamp;
4569 if (elapsed < KEY_DELAY) {
4570 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4571 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4572 }
4573 if (infoPtr->charCode != charCode) {
4574 infoPtr->charCode=charCode=0;
4575 }
4576 } else {
4577 infoPtr->charCode=charCode;
4578 infoPtr->szSearchParam[0]=charCode;
4579 infoPtr->nSearchParamLength=1;
4580 /* Redundant with the 1 char string */
4581 charCode=0;
4582 }
4583
4584 /* and search from the current position */
4585 nItem=NULL;
4586 if (infoPtr->selectedItem != NULL) {
4587 endidx=infoPtr->selectedItem;
4588 /* if looking for single character match,
4589 * then we must always move forward
4590 */
4591 if (infoPtr->nSearchParamLength == 1)
4592 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4593 else
4594 idx=endidx;
4595 } else {
4596 endidx=NULL;
4597 idx=infoPtr->root->firstChild;
4598 }
4599 do {
4600 /* At the end point, sort out wrapping */
4601 if (idx == NULL) {
4602
4603 /* If endidx is null, stop at the last item (ie top to bottom) */
4604 if (endidx == NULL)
4605 break;
4606
4607 /* Otherwise, start again at the very beginning */
4608 idx=infoPtr->root->firstChild;
4609
4610 /* But if we are stopping on the first child, end now! */
4611 if (idx == endidx) break;
4612 }
4613
4614 /* get item */
4615 ZeroMemory(&item, sizeof(item));
4616 item.mask = TVIF_TEXT;
4617 item.hItem = idx;
4618 item.pszText = buffer;
4619 item.cchTextMax = sizeof(buffer);
4620 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4621
4622 /* check for a match */
4623 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4624 nItem=idx;
4625 break;
4626 } else if ( (charCode != 0) && (nItem == NULL) &&
4627 (nItem != infoPtr->selectedItem) &&
4628 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4629 /* This would work but we must keep looking for a longer match */
4630 nItem=idx;
4631 }
4632 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4633 } while (idx != endidx);
4634
4635 if (nItem != NULL) {
4636 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4637 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4638 }
4639 }
4640
4641 return 0;
4642 }
4643
4644 /* Scrolling ************************************************************/
4645
4646 static LRESULT
4647 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4648 {
4649 int viscount;
4650 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4651 HTREEITEM newFirstVisible = NULL;
4652 int visible_pos = -1;
4653
4654 if (!TREEVIEW_ValidItem(infoPtr, item))
4655 return FALSE;
4656
4657 if (!ISVISIBLE(item))
4658 {
4659 /* Expand parents as necessary. */
4660 HTREEITEM parent;
4661
4662 /* see if we are trying to ensure that root is visible */
4663 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4664 parent = item->parent;
4665 else
4666 parent = item; /* this item is the topmost item */
4667
4668 while (parent != infoPtr->root)
4669 {
4670 if (!(parent->state & TVIS_EXPANDED))
4671 TREEVIEW_Expand(infoPtr, parent, FALSE, TRUE);
4672
4673 parent = parent->parent;
4674 }
4675 }
4676
4677 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4678
4679 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4680 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4681
4682 if (hasFirstVisible)
4683 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4684
4685 if (visible_pos < 0)
4686 {
4687 /* item is before the start of the list: put it at the top. */
4688 newFirstVisible = item;
4689 }
4690 else if (visible_pos >= viscount
4691 /* Sometimes, before we are displayed, GVC is 0, causing us to
4692 * spuriously scroll up. */
4693 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4694 {
4695 /* item is past the end of the list. */
4696 int scroll = visible_pos - viscount;
4697
4698 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4699 scroll + 1);
4700 }
4701
4702 if (bHScroll)
4703 {
4704 /* Scroll window so item's text is visible as much as possible */
4705 /* Calculation of amount of extra space is taken from EditLabel code */
4706 INT pos, x;
4707 TEXTMETRICW textMetric;
4708 HDC hdc = GetWindowDC(infoPtr->hwnd);
4709
4710 x = item->textWidth;
4711
4712 GetTextMetricsW(hdc, &textMetric);
4713 ReleaseDC(infoPtr->hwnd, hdc);
4714
4715 x += (textMetric.tmMaxCharWidth * 2);
4716 x = max(x, textMetric.tmMaxCharWidth * 3);
4717
4718 if (item->textOffset < 0)
4719 pos = item->textOffset;
4720 else if (item->textOffset + x > infoPtr->clientWidth)
4721 {
4722 if (x > infoPtr->clientWidth)
4723 pos = item->textOffset;
4724 else
4725 pos = item->textOffset + x - infoPtr->clientWidth;
4726 }
4727 else
4728 pos = 0;
4729
4730 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4731 }
4732
4733 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4734 {
4735 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4736
4737 return TRUE;
4738 }
4739
4740 return FALSE;
4741 }
4742
4743 static VOID
4744 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4745 TREEVIEW_ITEM *newFirstVisible,
4746 BOOL bUpdateScrollPos)
4747 {
4748 int gap_size;
4749
4750 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4751
4752 if (newFirstVisible != NULL)
4753 {
4754 /* Prevent an empty gap from appearing at the bottom... */
4755 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4756 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4757
4758 if (gap_size > 0)
4759 {
4760 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4761 -gap_size);
4762
4763 /* ... unless we just don't have enough items. */
4764 if (newFirstVisible == NULL)
4765 newFirstVisible = infoPtr->root->firstChild;
4766 }
4767 }
4768
4769 if (infoPtr->firstVisible != newFirstVisible)
4770 {
4771 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4772 {
4773 infoPtr->firstVisible = newFirstVisible;
4774 TREEVIEW_Invalidate(infoPtr, NULL);
4775 }
4776 else
4777 {
4778 TREEVIEW_ITEM *item;
4779 int scroll = infoPtr->uItemHeight *
4780 (infoPtr->firstVisible->visibleOrder
4781 - newFirstVisible->visibleOrder);
4782
4783 infoPtr->firstVisible = newFirstVisible;
4784
4785 for (item = infoPtr->root->firstChild; item != NULL;
4786 item = TREEVIEW_GetNextListItem(infoPtr, item))
4787 {
4788 item->rect.top += scroll;
4789 item->rect.bottom += scroll;
4790 }
4791
4792 if (bUpdateScrollPos)
4793 SetScrollPos(infoPtr->hwnd, SB_VERT,
4794 newFirstVisible->visibleOrder, TRUE);
4795
4796 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4797 }
4798 }
4799 }
4800
4801 /************************************************************************
4802 * VScroll is always in units of visible items. i.e. we always have a
4803 * visible item aligned to the top of the control. (Unless we have no
4804 * items at all.)
4805 */
4806 static LRESULT
4807 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4808 {
4809 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4810 TREEVIEW_ITEM *newFirstVisible = NULL;
4811
4812 int nScrollCode = LOWORD(wParam);
4813
4814 TRACE("wp %lx\n", wParam);
4815
4816 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4817 return 0;
4818
4819 if (!oldFirstVisible)
4820 {
4821 assert(infoPtr->root->firstChild == NULL);
4822 return 0;
4823 }
4824
4825 switch (nScrollCode)
4826 {
4827 case SB_TOP:
4828 newFirstVisible = infoPtr->root->firstChild;
4829 break;
4830
4831 case SB_BOTTOM:
4832 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4833 break;
4834
4835 case SB_LINEUP:
4836 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4837 break;
4838
4839 case SB_LINEDOWN:
4840 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4841 break;
4842
4843 case SB_PAGEUP:
4844 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4845 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4846 break;
4847
4848 case SB_PAGEDOWN:
4849 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4850 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4851 break;
4852
4853 case SB_THUMBTRACK:
4854 case SB_THUMBPOSITION:
4855 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4856 infoPtr->root->firstChild,
4857 (LONG)(SHORT)HIWORD(wParam));
4858 break;
4859
4860 case SB_ENDSCROLL:
4861 return 0;
4862 }
4863
4864 if (newFirstVisible != NULL)
4865 {
4866 if (newFirstVisible != oldFirstVisible)
4867 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4868 nScrollCode != SB_THUMBTRACK);
4869 else if (nScrollCode == SB_THUMBPOSITION)
4870 SetScrollPos(infoPtr->hwnd, SB_VERT,
4871 newFirstVisible->visibleOrder, TRUE);
4872 }
4873
4874 return 0;
4875 }
4876
4877 static LRESULT
4878 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4879 {
4880 int maxWidth;
4881 int scrollX = infoPtr->scrollX;
4882 int nScrollCode = LOWORD(wParam);
4883
4884 TRACE("wp %lx\n", wParam);
4885
4886 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4887 return FALSE;
4888
4889 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4890 /* shall never occur */
4891 if (maxWidth <= 0)
4892 {
4893 scrollX = 0;
4894 goto scroll;
4895 }
4896
4897 switch (nScrollCode)
4898 {
4899 case SB_LINELEFT:
4900 scrollX -= infoPtr->uItemHeight;
4901 break;
4902 case SB_LINERIGHT:
4903 scrollX += infoPtr->uItemHeight;
4904 break;
4905 case SB_PAGELEFT:
4906 scrollX -= infoPtr->clientWidth;
4907 break;
4908 case SB_PAGERIGHT:
4909 scrollX += infoPtr->clientWidth;
4910 break;
4911
4912 case SB_THUMBTRACK:
4913 case SB_THUMBPOSITION:
4914 scrollX = (int)(SHORT)HIWORD(wParam);
4915 break;
4916
4917 case SB_ENDSCROLL:
4918 return 0;
4919 }
4920
4921 if (scrollX > maxWidth)
4922 scrollX = maxWidth;
4923 else if (scrollX < 0)
4924 scrollX = 0;
4925
4926 scroll:
4927 if (scrollX != infoPtr->scrollX)
4928 {
4929 TREEVIEW_ITEM *item;
4930 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4931
4932 for (item = infoPtr->root->firstChild; item != NULL;
4933 item = TREEVIEW_GetNextListItem(infoPtr, item))
4934 {
4935 item->linesOffset += scroll_pixels;
4936 item->stateOffset += scroll_pixels;
4937 item->imageOffset += scroll_pixels;
4938 item->textOffset += scroll_pixels;
4939 }
4940
4941 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4942 infoPtr->scrollX = scrollX;
4943 UpdateWindow(infoPtr->hwnd);
4944 }
4945
4946 if (nScrollCode != SB_THUMBTRACK)
4947 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4948
4949 return 0;
4950 }
4951
4952 static LRESULT
4953 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4954 {
4955 short wheelDelta;
4956 UINT pulScrollLines = 3;
4957
4958 if (wParam & (MK_SHIFT | MK_CONTROL))
4959 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4960
4961 if (infoPtr->firstVisible == NULL)
4962 return TRUE;
4963
4964 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4965
4966 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4967 /* if scrolling changes direction, ignore left overs */
4968 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
4969 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
4970 infoPtr->wheelRemainder += wheelDelta;
4971 else
4972 infoPtr->wheelRemainder = wheelDelta;
4973
4974 if (infoPtr->wheelRemainder && pulScrollLines)
4975 {
4976 int newDy;
4977 int maxDy;
4978 int lineScroll;
4979
4980 lineScroll = pulScrollLines * (float)infoPtr->wheelRemainder / WHEEL_DELTA;
4981 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / (int)pulScrollLines;
4982
4983 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
4984 maxDy = infoPtr->maxVisibleOrder;
4985
4986 if (newDy > maxDy)
4987 newDy = maxDy;
4988
4989 if (newDy < 0)
4990 newDy = 0;
4991
4992 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4993 }
4994 return TRUE;
4995 }
4996
4997 /* Create/Destroy *******************************************************/
4998
4999 static LRESULT
5000 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5001 {
5002 RECT rcClient;
5003 TREEVIEW_INFO *infoPtr;
5004 LOGFONTW lf;
5005
5006 TRACE("wnd %p, style 0x%x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5007
5008 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5009
5010 if (infoPtr == NULL)
5011 {
5012 ERR("could not allocate info memory!\n");
5013 return 0;
5014 }
5015
5016 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5017
5018 infoPtr->hwnd = hwnd;
5019 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5020 infoPtr->Timer = 0;
5021 infoPtr->uNumItems = 0;
5022 infoPtr->cdmode = 0;
5023 infoPtr->uScrollTime = 300; /* milliseconds */
5024 infoPtr->bRedraw = TRUE;
5025
5026 GetClientRect(hwnd, &rcClient);
5027
5028 /* No scroll bars yet. */
5029 infoPtr->clientWidth = rcClient.right;
5030 infoPtr->clientHeight = rcClient.bottom;
5031 infoPtr->uInternalStatus = 0;
5032
5033 infoPtr->treeWidth = 0;
5034 infoPtr->treeHeight = 0;
5035
5036 infoPtr->uIndent = MINIMUM_INDENT;
5037 infoPtr->selectedItem = NULL;
5038 infoPtr->focusedItem = NULL;
5039 infoPtr->hotItem = NULL;
5040 infoPtr->editItem = NULL;
5041 infoPtr->firstVisible = NULL;
5042 infoPtr->maxVisibleOrder = 0;
5043 infoPtr->dropItem = NULL;
5044 infoPtr->insertMarkItem = NULL;
5045 infoPtr->insertBeforeorAfter = 0;
5046 /* dragList */
5047
5048 infoPtr->scrollX = 0;
5049 infoPtr->wheelRemainder = 0;
5050
5051 infoPtr->clrBk = CLR_NONE; /* use system color */
5052 infoPtr->clrText = CLR_NONE; /* use system color */
5053 infoPtr->clrLine = CLR_DEFAULT;
5054 infoPtr->clrInsertMark = CLR_DEFAULT;
5055
5056 /* hwndToolTip */
5057
5058 infoPtr->hwndEdit = NULL;
5059 infoPtr->wpEditOrig = NULL;
5060 infoPtr->bIgnoreEditKillFocus = FALSE;
5061 infoPtr->bLabelChanged = FALSE;
5062
5063 infoPtr->himlNormal = NULL;
5064 infoPtr->himlState = NULL;
5065 infoPtr->normalImageWidth = 0;
5066 infoPtr->normalImageHeight = 0;
5067 infoPtr->stateImageWidth = 0;
5068 infoPtr->stateImageHeight = 0;
5069
5070 infoPtr->items = DPA_Create(16);
5071
5072 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5073 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5074 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5075 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5076 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5077 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5078
5079 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5080
5081 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5082 infoPtr->root->state = TVIS_EXPANDED;
5083 infoPtr->root->iLevel = -1;
5084 infoPtr->root->visibleOrder = -1;
5085
5086 infoPtr->hwndNotify = lpcs->hwndParent;
5087 infoPtr->hwndToolTip = 0;
5088
5089 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5090 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5091
5092 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5093 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5094 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5095 hwnd, 0, 0, 0);
5096
5097 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5098 ShowScrollBar(hwnd, SB_VERT, FALSE);
5099 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5100
5101 OpenThemeData (hwnd, themeClass);
5102
5103 return 0;
5104 }
5105
5106
5107 static LRESULT
5108 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5109 {
5110 TRACE("\n");
5111
5112 /* free item data */
5113 TREEVIEW_RemoveTree(infoPtr);
5114 /* root isn't freed with other items */
5115 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5116 DPA_Destroy(infoPtr->items);
5117
5118 /* tool tip is automatically destroyed: we are its owner */
5119
5120 /* Restore original wndproc */
5121 if (infoPtr->hwndEdit)
5122 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5123 (DWORD_PTR)infoPtr->wpEditOrig);
5124
5125 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5126
5127 /* Deassociate treeview from the window before doing anything drastic. */
5128 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5129
5130 DeleteObject(infoPtr->hDefaultFont);
5131 DeleteObject(infoPtr->hBoldFont);
5132 DeleteObject(infoPtr->hUnderlineFont);
5133 DeleteObject(infoPtr->hBoldUnderlineFont);
5134 Free(infoPtr);
5135
5136 return 0;
5137 }
5138
5139 /* Miscellaneous Messages ***********************************************/
5140
5141 static LRESULT
5142 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5143 {
5144 static const struct
5145 {
5146 unsigned char code;
5147 }
5148 scroll[] =
5149 {
5150 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5151 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5152 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5153 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5154 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5155 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5156 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5157 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5158 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5159 #undef SCROLL_ENTRY
5160 };
5161
5162 if (key >= VK_PRIOR && key <= VK_DOWN)
5163 {
5164 unsigned char code = scroll[key - VK_PRIOR].code;
5165
5166 (((code & (1 << 7)) == (SB_HORZ << 7))
5167 ? TREEVIEW_HScroll
5168 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5169 }
5170
5171 return 0;
5172 }
5173
5174 /************************************************************************
5175 * TREEVIEW_KeyDown
5176 *
5177 * VK_UP Move selection to the previous non-hidden item.
5178 * VK_DOWN Move selection to the next non-hidden item.
5179 * VK_HOME Move selection to the first item.
5180 * VK_END Move selection to the last item.
5181 * VK_LEFT If expanded then collapse, otherwise move to parent.
5182 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5183 * VK_ADD Expand.
5184 * VK_SUBTRACT Collapse.
5185 * VK_MULTIPLY Expand all.
5186 * VK_PRIOR Move up GetVisibleCount items.
5187 * VK_NEXT Move down GetVisibleCount items.
5188 * VK_BACK Move to parent.
5189 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5190 */
5191 static LRESULT
5192 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5193 {
5194 /* If it is non-NULL and different, it will be selected and visible. */
5195 TREEVIEW_ITEM *newSelection = NULL;
5196 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5197 NMTVKEYDOWN nmkeydown;
5198
5199 TRACE("%lx\n", wParam);
5200
5201 nmkeydown.wVKey = wParam;
5202 nmkeydown.flags = 0;
5203 TREEVIEW_SendRealNotify(infoPtr, TVN_KEYDOWN, &nmkeydown.hdr);
5204
5205 if (prevItem == NULL)
5206 return FALSE;
5207
5208 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5209 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5210
5211 switch (wParam)
5212 {
5213 case VK_UP:
5214 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5215 if (!newSelection)
5216 newSelection = infoPtr->root->firstChild;
5217 break;
5218
5219 case VK_DOWN:
5220 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5221 break;
5222
5223 case VK_RETURN:
5224 TREEVIEW_SendSimpleNotify(infoPtr, NM_RETURN);
5225 break;
5226
5227 case VK_HOME:
5228 newSelection = infoPtr->root->firstChild;
5229 break;
5230
5231 case VK_END:
5232 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5233 break;
5234
5235 case VK_LEFT:
5236 if (prevItem->state & TVIS_EXPANDED)
5237 {
5238 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5239 }
5240 else if (prevItem->parent != infoPtr->root)
5241 {
5242 newSelection = prevItem->parent;
5243 }
5244 break;
5245
5246 case VK_RIGHT:
5247 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5248 {
5249 if (!(prevItem->state & TVIS_EXPANDED))
5250 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5251 else
5252 {
5253 newSelection = prevItem->firstChild;
5254 }
5255 }
5256
5257 break;
5258
5259 case VK_MULTIPLY:
5260 TREEVIEW_ExpandAll(infoPtr, prevItem);
5261 break;
5262
5263 case VK_ADD:
5264 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5265 break;
5266
5267 case VK_SUBTRACT:
5268 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5269 break;
5270
5271 case VK_PRIOR:
5272 newSelection
5273 = TREEVIEW_GetListItem(infoPtr, prevItem,
5274 -TREEVIEW_GetVisibleCount(infoPtr));
5275 break;
5276
5277 case VK_NEXT:
5278 newSelection
5279 = TREEVIEW_GetListItem(infoPtr, prevItem,
5280 TREEVIEW_GetVisibleCount(infoPtr));
5281 break;
5282
5283 case VK_BACK:
5284 newSelection = prevItem->parent;
5285 if (newSelection == infoPtr->root)
5286 newSelection = NULL;
5287 break;
5288
5289 case VK_SPACE:
5290 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5291 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5292 break;
5293 }
5294
5295 if (newSelection && newSelection != prevItem)
5296 {
5297 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5298 TVC_BYKEYBOARD))
5299 {
5300 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5301 }
5302 }
5303
5304 return FALSE;
5305 }
5306
5307 static LRESULT
5308 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5309 {
5310 /* remove hot effect from item */
5311 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5312 infoPtr->hotItem = NULL;
5313
5314 return 0;
5315 }
5316
5317 static LRESULT
5318 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5319 {
5320 POINT pt;
5321 TRACKMOUSEEVENT trackinfo;
5322 TREEVIEW_ITEM * item;
5323
5324 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5325
5326 /* fill in the TRACKMOUSEEVENT struct */
5327 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5328 trackinfo.dwFlags = TME_QUERY;
5329 trackinfo.hwndTrack = infoPtr->hwnd;
5330
5331 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5332 _TrackMouseEvent(&trackinfo);
5333
5334 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5335 if(!(trackinfo.dwFlags & TME_LEAVE))
5336 {
5337 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5338 trackinfo.hwndTrack = infoPtr->hwnd;
5339 /* do it as fast as possible, minimal systimer latency will be used */
5340 trackinfo.dwHoverTime = 1;
5341
5342 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5343 /* and can properly deactivate the hot item */
5344 _TrackMouseEvent(&trackinfo);
5345 }
5346
5347 pt.x = (short)LOWORD(lParam);
5348 pt.y = (short)HIWORD(lParam);
5349
5350 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5351
5352 if (item != infoPtr->hotItem)
5353 {
5354 /* redraw old hot item */
5355 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5356 infoPtr->hotItem = item;
5357 /* redraw new hot item */
5358 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5359 }
5360
5361 return 0;
5362 }
5363
5364 /* Draw themed border */
5365 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5366 {
5367 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5368 HDC dc;
5369 RECT r;
5370 HRGN cliprgn;
5371 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5372 cyEdge = GetSystemMetrics (SM_CYEDGE);
5373
5374 if (!theme)
5375 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5376
5377 GetWindowRect(infoPtr->hwnd, &r);
5378
5379 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5380 r.right - cxEdge, r.bottom - cyEdge);
5381 if (region != (HRGN)1)
5382 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5383 OffsetRect(&r, -r.left, -r.top);
5384
5385 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5386 OffsetRect(&r, -r.left, -r.top);
5387
5388 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5389 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5390 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5391 ReleaseDC(infoPtr->hwnd, dc);
5392
5393 /* Call default proc to get the scrollbars etc. painted */
5394 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5395
5396 return TRUE;
5397 }
5398
5399 static LRESULT
5400 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5401 {
5402 LPNMHDR lpnmh = (LPNMHDR)lParam;
5403
5404 if (lpnmh->code == PGN_CALCSIZE) {
5405 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5406
5407 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5408 lppgc->iWidth = infoPtr->treeWidth;
5409 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5410 infoPtr->treeWidth, infoPtr->clientWidth);
5411 }
5412 else {
5413 lppgc->iHeight = infoPtr->treeHeight;
5414 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5415 infoPtr->treeHeight, infoPtr->clientHeight);
5416 }
5417 return 0;
5418 }
5419 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5420 }
5421
5422 static LRESULT
5423 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5424 {
5425 if (wParam == SIZE_RESTORED)
5426 {
5427 infoPtr->clientWidth = (short)LOWORD(lParam);
5428 infoPtr->clientHeight = (short)HIWORD(lParam);
5429
5430 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5431 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5432 TREEVIEW_UpdateScrollBars(infoPtr);
5433 }
5434 else
5435 {
5436 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5437 }
5438
5439 TREEVIEW_Invalidate(infoPtr, NULL);
5440 return 0;
5441 }
5442
5443 static LRESULT
5444 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5445 {
5446 TRACE("(%lx %lx)\n", wParam, lParam);
5447
5448 if (wParam == GWL_STYLE)
5449 {
5450 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5451
5452 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5453 {
5454 if (dwNewStyle & TVS_CHECKBOXES)
5455 {
5456 TREEVIEW_InitCheckboxes(infoPtr);
5457 TRACE("checkboxes enabled\n");
5458
5459 /* set all items to state image index 1 */
5460 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5461 }
5462 else
5463 {
5464 FIXME("tried to disable checkboxes\n");
5465 }
5466 }
5467
5468 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5469 {
5470 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5471 {
5472 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5473 TRACE("tooltips enabled\n");
5474 }
5475 else
5476 {
5477 DestroyWindow(infoPtr->hwndToolTip);
5478 infoPtr->hwndToolTip = 0;
5479 TRACE("tooltips disabled\n");
5480 }
5481 }
5482
5483 infoPtr->dwStyle = dwNewStyle;
5484 }
5485
5486 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
5487 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5488 TREEVIEW_UpdateScrollBars(infoPtr);
5489 TREEVIEW_Invalidate(infoPtr, NULL);
5490
5491 return 0;
5492 }
5493
5494 static LRESULT
5495 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5496 {
5497 POINT pt;
5498 TREEVIEW_ITEM * item;
5499 NMMOUSE nmmouse;
5500
5501 GetCursorPos(&pt);
5502 ScreenToClient(infoPtr->hwnd, &pt);
5503
5504 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5505
5506 memset(&nmmouse, 0, sizeof(nmmouse));
5507 if (item)
5508 {
5509 nmmouse.dwItemSpec = (DWORD_PTR)item;
5510 nmmouse.dwItemData = item->lParam;
5511 }
5512 nmmouse.pt.x = 0;
5513 nmmouse.pt.y = 0;
5514 nmmouse.dwHitInfo = lParam;
5515 if (TREEVIEW_SendRealNotify(infoPtr, NM_SETCURSOR, &nmmouse.hdr))
5516 return 0;
5517
5518 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5519 {
5520 SetCursor(infoPtr->hcurHand);
5521 return 0;
5522 }
5523 else
5524 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5525 }
5526
5527 static LRESULT
5528 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5529 {
5530 TRACE("\n");
5531
5532 if (!infoPtr->selectedItem)
5533 {
5534 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5535 TVC_UNKNOWN);
5536 }
5537
5538 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5539 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5540 return 0;
5541 }
5542
5543 static LRESULT
5544 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5545 {
5546 TRACE("\n");
5547
5548 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5549 UpdateWindow(infoPtr->hwnd);
5550 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5551 return 0;
5552 }
5553
5554 /* update theme after a WM_THEMECHANGED message */
5555 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5556 {
5557 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5558 CloseThemeData (theme);
5559 OpenThemeData (infoPtr->hwnd, themeClass);
5560 return 0;
5561 }
5562
5563
5564 static LRESULT WINAPI
5565 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5566 {
5567 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5568
5569 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5570
5571 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5572 else
5573 {
5574 if (uMsg == WM_CREATE)
5575 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5576 else
5577 goto def;
5578 }
5579
5580 switch (uMsg)
5581 {
5582 case TVM_CREATEDRAGIMAGE:
5583 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5584
5585 case TVM_DELETEITEM:
5586 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5587
5588 case TVM_EDITLABELA:
5589 case TVM_EDITLABELW:
5590 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5591
5592 case TVM_ENDEDITLABELNOW:
5593 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5594
5595 case TVM_ENSUREVISIBLE:
5596 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5597
5598 case TVM_EXPAND:
5599 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5600
5601 case TVM_GETBKCOLOR:
5602 return TREEVIEW_GetBkColor(infoPtr);
5603
5604 case TVM_GETCOUNT:
5605 return TREEVIEW_GetCount(infoPtr);
5606
5607 case TVM_GETEDITCONTROL:
5608 return TREEVIEW_GetEditControl(infoPtr);
5609
5610 case TVM_GETIMAGELIST:
5611 return TREEVIEW_GetImageList(infoPtr, wParam);
5612
5613 case TVM_GETINDENT:
5614 return TREEVIEW_GetIndent(infoPtr);
5615
5616 case TVM_GETINSERTMARKCOLOR:
5617 return TREEVIEW_GetInsertMarkColor(infoPtr);
5618
5619 case TVM_GETISEARCHSTRINGA:
5620 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5621 return 0;
5622
5623 case TVM_GETISEARCHSTRINGW:
5624 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5625 return 0;
5626
5627 case TVM_GETITEMA:
5628 case TVM_GETITEMW:
5629 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5630 uMsg == TVM_GETITEMW);
5631 case TVM_GETITEMHEIGHT:
5632 return TREEVIEW_GetItemHeight(infoPtr);
5633
5634 case TVM_GETITEMRECT:
5635 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5636
5637 case TVM_GETITEMSTATE:
5638 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5639
5640 case TVM_GETLINECOLOR:
5641 return TREEVIEW_GetLineColor(infoPtr);
5642
5643 case TVM_GETNEXTITEM:
5644 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5645
5646 case TVM_GETSCROLLTIME:
5647 return TREEVIEW_GetScrollTime(infoPtr);
5648
5649 case TVM_GETTEXTCOLOR:
5650 return TREEVIEW_GetTextColor(infoPtr);
5651
5652 case TVM_GETTOOLTIPS:
5653 return TREEVIEW_GetToolTips(infoPtr);
5654
5655 case TVM_GETUNICODEFORMAT:
5656 return TREEVIEW_GetUnicodeFormat(infoPtr);
5657
5658 case TVM_GETVISIBLECOUNT:
5659 return TREEVIEW_GetVisibleCount(infoPtr);
5660
5661 case TVM_HITTEST:
5662 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5663
5664 case TVM_INSERTITEMA:
5665 case TVM_INSERTITEMW:
5666 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5667 uMsg == TVM_INSERTITEMW);
5668 case TVM_SELECTITEM:
5669 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5670
5671 case TVM_SETBKCOLOR:
5672 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5673
5674 case TVM_SETIMAGELIST:
5675 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5676
5677 case TVM_SETINDENT:
5678 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5679
5680 case TVM_SETINSERTMARK:
5681 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5682
5683 case TVM_SETINSERTMARKCOLOR:
5684 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5685
5686 case TVM_SETITEMA:
5687 case TVM_SETITEMW:
5688 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5689 uMsg == TVM_SETITEMW);
5690 case TVM_SETLINECOLOR:
5691 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5692
5693 case TVM_SETITEMHEIGHT:
5694 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5695
5696 case TVM_SETSCROLLTIME:
5697 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5698
5699 case TVM_SETTEXTCOLOR:
5700 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5701
5702 case TVM_SETTOOLTIPS:
5703 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5704
5705 case TVM_SETUNICODEFORMAT:
5706 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5707
5708 case TVM_SORTCHILDREN:
5709 return TREEVIEW_SortChildren(infoPtr, lParam);
5710
5711 case TVM_SORTCHILDRENCB:
5712 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5713
5714 case WM_CHAR:
5715 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5716
5717 case WM_COMMAND:
5718 return TREEVIEW_Command(infoPtr, wParam, lParam);
5719
5720 case WM_DESTROY:
5721 return TREEVIEW_Destroy(infoPtr);
5722
5723 /* WM_ENABLE */
5724
5725 case WM_ERASEBKGND:
5726 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5727
5728 case WM_GETDLGCODE:
5729 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5730
5731 case WM_GETFONT:
5732 return TREEVIEW_GetFont(infoPtr);
5733
5734 case WM_HSCROLL:
5735 return TREEVIEW_HScroll(infoPtr, wParam);
5736
5737 case WM_KEYDOWN:
5738 return TREEVIEW_KeyDown(infoPtr, wParam);
5739
5740 case WM_KILLFOCUS:
5741 return TREEVIEW_KillFocus(infoPtr);
5742
5743 case WM_LBUTTONDBLCLK:
5744 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5745
5746 case WM_LBUTTONDOWN:
5747 return TREEVIEW_LButtonDown(infoPtr, lParam);
5748
5749 /* WM_MBUTTONDOWN */
5750
5751 case WM_MOUSELEAVE:
5752 return TREEVIEW_MouseLeave(infoPtr);
5753
5754 case WM_MOUSEMOVE:
5755 return TREEVIEW_MouseMove(infoPtr, lParam);
5756
5757 case WM_NCLBUTTONDOWN:
5758 if (infoPtr->hwndEdit)
5759 SetFocus(infoPtr->hwnd);
5760 goto def;
5761
5762 case WM_NCPAINT:
5763 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5764
5765 case WM_NOTIFY:
5766 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5767
5768 case WM_NOTIFYFORMAT:
5769 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5770
5771 case WM_PRINTCLIENT:
5772 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5773
5774 case WM_PAINT:
5775 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5776
5777 case WM_RBUTTONDOWN:
5778 return TREEVIEW_RButtonDown(infoPtr, lParam);
5779
5780 case WM_SETCURSOR:
5781 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5782
5783 case WM_SETFOCUS:
5784 return TREEVIEW_SetFocus(infoPtr);
5785
5786 case WM_SETFONT:
5787 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5788
5789 case WM_SETREDRAW:
5790 return TREEVIEW_SetRedraw(infoPtr, wParam);
5791
5792 case WM_SIZE:
5793 return TREEVIEW_Size(infoPtr, wParam, lParam);
5794
5795 case WM_STYLECHANGED:
5796 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5797
5798 case WM_SYSCOLORCHANGE:
5799 COMCTL32_RefreshSysColors();
5800 return 0;
5801
5802 /* WM_SYSKEYDOWN */
5803
5804 case WM_TIMER:
5805 return TREEVIEW_HandleTimer(infoPtr, wParam);
5806
5807 case WM_THEMECHANGED:
5808 return TREEVIEW_ThemeChanged (infoPtr);
5809
5810 case WM_VSCROLL:
5811 return TREEVIEW_VScroll(infoPtr, wParam);
5812
5813 /* WM_WININICHANGE */
5814
5815 case WM_MOUSEWHEEL:
5816 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5817
5818 case WM_DRAWITEM:
5819 TRACE("drawItem\n");
5820 goto def;
5821
5822 default:
5823 /* This mostly catches MFC and Delphi messages. :( */
5824 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5825 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5826 def:
5827 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5828 }
5829 }
5830
5831
5832 /* Class Registration ***************************************************/
5833
5834 VOID
5835 TREEVIEW_Register(void)
5836 {
5837 WNDCLASSW wndClass;
5838
5839 TRACE("\n");
5840
5841 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5842 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5843 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5844 wndClass.cbClsExtra = 0;
5845 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5846
5847 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5848 wndClass.hbrBackground = 0;
5849 wndClass.lpszClassName = WC_TREEVIEWW;
5850
5851 RegisterClassW(&wndClass);
5852 }
5853
5854
5855 VOID
5856 TREEVIEW_Unregister(void)
5857 {
5858 UnregisterClassW(WC_TREEVIEWW, NULL);
5859 }
5860
5861
5862 /* Tree Verification ****************************************************/
5863
5864 static inline void
5865 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5866
5867 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5868 const TREEVIEW_ITEM *item)
5869 {
5870 assert(infoPtr != NULL);
5871 assert(item != NULL);
5872
5873 /* both NULL, or both non-null */
5874 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5875
5876 assert(item->firstChild != item);
5877 assert(item->lastChild != item);
5878
5879 if (item->firstChild)
5880 {
5881 assert(item->firstChild->parent == item);
5882 assert(item->firstChild->prevSibling == NULL);
5883 }
5884
5885 if (item->lastChild)
5886 {
5887 assert(item->lastChild->parent == item);
5888 assert(item->lastChild->nextSibling == NULL);
5889 }
5890
5891 assert(item->nextSibling != item);
5892 if (item->nextSibling)
5893 {
5894 assert(item->nextSibling->parent == item->parent);
5895 assert(item->nextSibling->prevSibling == item);
5896 }
5897
5898 assert(item->prevSibling != item);
5899 if (item->prevSibling)
5900 {
5901 assert(item->prevSibling->parent == item->parent);
5902 assert(item->prevSibling->nextSibling == item);
5903 }
5904 }
5905
5906 static inline void
5907 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5908 {
5909 assert(item != NULL);
5910
5911 assert(item->parent != NULL);
5912 assert(item->parent != item);
5913 assert(item->iLevel == item->parent->iLevel + 1);
5914
5915 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5916
5917 TREEVIEW_VerifyItemCommon(infoPtr, item);
5918
5919 TREEVIEW_VerifyChildren(infoPtr, item);
5920 }
5921
5922 static inline void
5923 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5924 {
5925 const TREEVIEW_ITEM *child;
5926 assert(item != NULL);
5927
5928 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5929 TREEVIEW_VerifyItem(infoPtr, child);
5930 }
5931
5932 static inline void
5933 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5934 {
5935 TREEVIEW_ITEM *root = infoPtr->root;
5936
5937 assert(root != NULL);
5938 assert(root->iLevel == -1);
5939 assert(root->parent == NULL);
5940 assert(root->prevSibling == NULL);
5941
5942 TREEVIEW_VerifyItemCommon(infoPtr, root);
5943
5944 TREEVIEW_VerifyChildren(infoPtr, root);
5945 }
5946
5947 static void
5948 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5949 {
5950 if (!TRACE_ON(treeview)) return;
5951
5952 assert(infoPtr != NULL);
5953 TREEVIEW_VerifyRoot(infoPtr);
5954 }