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