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