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