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