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