maintainers: Move WineHQ URLs to https.
[wine.git] / dlls / comctl32 / treeview.c
blob9a37cf331b499887ddde6ab3c5cc64a172485484
1 /* Treeview control
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
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.
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.
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
22 * NOTES
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
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.
30 * TODO:
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO
34 * missing styles: TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_EXPANDPARTIAL, TVIS_EX_FLAT,
37 * TVIS_EX_DISABLED
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
43 #include "config.h"
44 #include "wine/port.h"
46 #include <assert.h>
47 #include <ctype.h>
48 #include <stdarg.h>
49 #include <string.h>
50 #include <limits.h>
51 #include <stdlib.h>
53 #define NONAMELESSUNION
55 #include "windef.h"
56 #include "winbase.h"
57 #include "wingdi.h"
58 #include "winuser.h"
59 #include "winnls.h"
60 #include "commctrl.h"
61 #include "comctl32.h"
62 #include "uxtheme.h"
63 #include "vssym32.h"
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
66 #include "wine/exception.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
70 /* internal structures */
71 typedef struct tagTREEVIEW_INFO
73 HWND hwnd;
74 HWND hwndNotify; /* Owner window to send notifications to */
75 DWORD dwStyle;
76 HTREEITEM root;
77 UINT uInternalStatus;
78 INT Timer;
79 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
80 INT cdmode; /* last custom draw setting */
81 UINT uScrollTime; /* max. time for scrolling in milliseconds */
82 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
84 UINT uItemHeight; /* item height */
85 BOOL bHeightSet;
87 LONG clientWidth; /* width of control window */
88 LONG clientHeight; /* height of control window */
90 LONG treeWidth; /* width of visible tree items */
91 LONG treeHeight; /* height of visible tree items */
93 UINT uIndent; /* indentation in pixels */
94 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
95 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
96 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
97 HTREEITEM editItem; /* item being edited with builtin edit box */
99 HTREEITEM firstVisible; /* handle to item whose top edge is at y = 0 */
100 LONG maxVisibleOrder;
101 HTREEITEM dropItem; /* handle to item selected by drag cursor */
102 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
103 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
104 HIMAGELIST dragList; /* Bitmap of dragged item */
105 LONG scrollX;
106 INT wheelRemainder;
107 COLORREF clrBk;
108 COLORREF clrText;
109 COLORREF clrLine;
110 COLORREF clrInsertMark;
111 HFONT hFont;
112 HFONT hDefaultFont;
113 HFONT hBoldFont;
114 HFONT hUnderlineFont;
115 HFONT hBoldUnderlineFont;
116 HCURSOR hcurHand;
117 HWND hwndToolTip;
119 HWND hwndEdit;
120 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
121 BOOL bIgnoreEditKillFocus;
122 BOOL bLabelChanged;
124 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
125 HIMAGELIST himlNormal;
126 int normalImageHeight;
127 int normalImageWidth;
128 HIMAGELIST himlState;
129 int stateImageHeight;
130 int stateImageWidth;
131 HDPA items;
133 DWORD lastKeyPressTimestamp;
134 WPARAM charCode;
135 INT nSearchParamLength;
136 WCHAR szSearchParam[ MAX_PATH ];
137 } TREEVIEW_INFO;
139 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
141 HTREEITEM parent; /* handle to parent or 0 if at root */
142 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
143 HTREEITEM firstChild; /* handle to first child or 0 if no child */
145 UINT callbackMask;
146 UINT state;
147 UINT stateMask;
148 LPWSTR pszText;
149 int cchTextMax;
150 int iImage;
151 int iSelectedImage;
152 int iExpandedImage;
153 int cChildren;
154 LPARAM lParam;
155 int iIntegral; /* item height multiplier (1 is normal) */
156 int iLevel; /* indentation level:0=root level */
157 HTREEITEM lastChild;
158 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
159 RECT rect;
160 LONG linesOffset;
161 LONG stateOffset;
162 LONG imageOffset;
163 LONG textOffset;
164 LONG textWidth; /* horizontal text extent for pszText */
165 LONG visibleOrder; /* Depth-first numbering of the items whose ancestors are all expanded,
166 corresponding to a top-to-bottom ordering in the tree view.
167 Each item takes up "item.iIntegral" spots in the visible order.
168 0 is the root's first child. */
169 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
170 } TREEVIEW_ITEM;
172 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
173 #define KEY_DELAY 450
175 /* bitflags for infoPtr->uInternalStatus */
177 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
178 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
179 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
180 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
181 #define TV_RDRAG 0x10 /* ditto Rbutton */
182 #define TV_RDRAGGING 0x20
184 /* bitflags for infoPtr->timer */
186 #define TV_EDIT_TIMER 2
187 #define TV_EDIT_TIMER_SET 2
189 #define TEXT_CALLBACK_SIZE 260
191 #define TREEVIEW_LEFT_MARGIN 8
193 #define MINIMUM_INDENT 19
195 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
197 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
198 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
199 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
201 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
202 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
203 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
204 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
206 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
209 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
212 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
214 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
215 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
216 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
217 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
218 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
219 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
221 /* Random Utilities *****************************************************/
222 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
224 /* Returns the treeview private data if hwnd is a treeview.
225 * Otherwise returns an undefined value. */
226 static inline TREEVIEW_INFO *
227 TREEVIEW_GetInfoPtr(HWND hwnd)
229 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
232 /* Don't call this. Nothing wants an item index. */
233 static inline int
234 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
236 return DPA_GetPtrIndex(infoPtr->items, handle);
239 /* Checks if item has changed and needs to be redrawn */
240 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
241 const TVITEMEXW *tvChange)
243 /* Number of children has changed */
244 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
245 return TRUE;
247 /* Image has changed and it's not a callback */
248 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
249 tiNew->iImage != I_IMAGECALLBACK)
250 return TRUE;
252 /* Selected image has changed and it's not a callback */
253 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
254 tiNew->iSelectedImage != I_IMAGECALLBACK)
255 return TRUE;
257 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
258 tiNew->iExpandedImage != I_IMAGECALLBACK)
259 return TRUE;
261 /* Text has changed and it's not a callback */
262 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
263 tiNew->pszText != LPSTR_TEXTCALLBACKW)
264 return TRUE;
266 /* Indent has changed */
267 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
268 return TRUE;
270 /* Item state has changed */
271 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
272 return TRUE;
274 return FALSE;
277 /***************************************************************************
278 * This method checks that handle is an item for this tree.
280 static BOOL
281 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
283 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
285 TRACE("invalid item %p\n", handle);
286 return FALSE;
288 else
289 return TRUE;
292 static HFONT
293 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
295 LOGFONTW font;
297 GetObjectW(hOrigFont, sizeof(font), &font);
298 font.lfWeight = FW_BOLD;
299 return CreateFontIndirectW(&font);
302 static HFONT
303 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
305 LOGFONTW font;
307 GetObjectW(hOrigFont, sizeof(font), &font);
308 font.lfUnderline = TRUE;
309 return CreateFontIndirectW(&font);
312 static HFONT
313 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont)
315 LOGFONTW font;
317 GetObjectW(hfont, sizeof(font), &font);
318 font.lfWeight = FW_BOLD;
319 font.lfUnderline = TRUE;
320 return CreateFontIndirectW(&font);
323 static inline HFONT
324 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
326 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
327 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
328 if (item->state & TVIS_BOLD)
329 return infoPtr->hBoldFont;
330 return infoPtr->hFont;
333 /* for trace/debugging purposes only */
334 static const char *
335 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
337 if (item == NULL) return "<null item>";
338 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
339 if (item->pszText == NULL) return "<null>";
340 return debugstr_w(item->pszText);
343 /* An item is not a child of itself. */
344 static BOOL
345 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
349 child = child->parent;
350 if (child == parent) return TRUE;
351 } while (child != NULL);
353 return FALSE;
356 static BOOL
357 TREEVIEW_IsFullRowSelect(const TREEVIEW_INFO *infoPtr)
359 return !(infoPtr->dwStyle & TVS_HASLINES) && (infoPtr->dwStyle & TVS_FULLROWSELECT);
362 static BOOL
363 TREEVIEW_IsItemHit(const TREEVIEW_INFO *infoPtr, const TVHITTESTINFO *ht)
365 if (TREEVIEW_IsFullRowSelect(infoPtr))
366 return ht->flags & (TVHT_ONITEMINDENT | TVHT_ONITEMBUTTON | TVHT_ONITEM | TVHT_ONITEMRIGHT);
367 else
368 return ht->flags & TVHT_ONITEM;
371 /* Tree Traversal *******************************************************/
373 /***************************************************************************
374 * This method returns the last expanded sibling or child child item
375 * of a tree node
377 static TREEVIEW_ITEM *
378 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
380 if (!item) return NULL;
382 while (item->lastChild)
384 if (item->state & TVIS_EXPANDED)
385 item = item->lastChild;
386 else
387 break;
390 if (item == infoPtr->root)
391 return NULL;
393 return item;
396 /***************************************************************************
397 * This method returns the previous non-hidden item in the list not
398 * considering the tree hierarchy.
400 static TREEVIEW_ITEM *
401 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
403 if (tvItem->prevSibling)
405 /* This item has a prevSibling, get the last item in the sibling's tree. */
406 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
408 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
409 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
410 else
411 return upItem;
413 else
415 /* this item does not have a prevSibling, get the parent */
416 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
421 /***************************************************************************
422 * This method returns the next physical item in the treeview not
423 * considering the tree hierarchy.
425 static TREEVIEW_ITEM *
426 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
429 * If this item has children and is expanded, return the first child
431 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
433 return tvItem->firstChild;
438 * try to get the sibling
440 if (tvItem->nextSibling)
441 return tvItem->nextSibling;
444 * Otherwise, get the parent's sibling.
446 while (tvItem->parent)
448 tvItem = tvItem->parent;
450 if (tvItem->nextSibling)
451 return tvItem->nextSibling;
454 return NULL;
457 /***************************************************************************
458 * This method returns the nth item starting at the given item. It returns
459 * the last item (or first) we we run out of items.
461 * Will scroll backward if count is <0.
462 * forward if count is >0.
464 static TREEVIEW_ITEM *
465 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
466 LONG count)
468 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
469 TREEVIEW_ITEM *previousItem;
471 assert(item != NULL);
473 if (count > 0)
475 next_item = TREEVIEW_GetNextListItem;
477 else if (count < 0)
479 count = -count;
480 next_item = TREEVIEW_GetPrevListItem;
482 else
483 return item;
487 previousItem = item;
488 item = next_item(infoPtr, item);
490 } while (--count && item != NULL);
493 return item ? item : previousItem;
496 /* Notifications ************************************************************/
498 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
500 if (!infoPtr->bNtfUnicode) {
501 switch (code) {
502 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
503 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
504 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
505 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
506 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
507 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
508 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
509 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
510 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
511 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
512 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
513 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
516 return code;
519 static inline BOOL
520 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, UINT code, NMHDR *hdr)
522 TRACE("code=%d, hdr=%p\n", code, hdr);
524 hdr->hwndFrom = infoPtr->hwnd;
525 hdr->idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
526 hdr->code = get_notifycode(infoPtr, code);
528 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, hdr->idFrom, (LPARAM)hdr);
531 static BOOL
532 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
534 NMHDR hdr;
535 return TREEVIEW_SendRealNotify(infoPtr, code, &hdr);
538 static VOID
539 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
541 tvItem->mask = mask;
542 tvItem->hItem = item;
543 tvItem->state = item->state;
544 tvItem->stateMask = 0;
545 tvItem->iImage = item->iImage;
546 tvItem->iSelectedImage = item->iSelectedImage;
547 tvItem->cChildren = item->cChildren;
548 tvItem->lParam = item->lParam;
550 if(mask & TVIF_TEXT)
552 if (!infoPtr->bNtfUnicode)
554 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
555 tvItem->pszText = Alloc (tvItem->cchTextMax);
556 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
558 else
560 tvItem->cchTextMax = item->cchTextMax;
561 tvItem->pszText = item->pszText;
564 else
566 tvItem->cchTextMax = 0;
567 tvItem->pszText = NULL;
571 static BOOL
572 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
573 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
575 NMTREEVIEWW nmhdr;
576 BOOL ret;
578 TRACE("code:%d action:0x%x olditem:%p newitem:%p\n",
579 code, action, oldItem, newItem);
581 memset(&nmhdr, 0, sizeof(NMTREEVIEWW));
582 nmhdr.action = action;
584 if (oldItem)
585 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
587 if (newItem)
588 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
590 nmhdr.ptDrag.x = 0;
591 nmhdr.ptDrag.y = 0;
593 ret = TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
594 if (!infoPtr->bNtfUnicode)
596 Free(nmhdr.itemOld.pszText);
597 Free(nmhdr.itemNew.pszText);
599 return ret;
602 static BOOL
603 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
604 HTREEITEM dragItem, POINT pt)
606 NMTREEVIEWW nmhdr;
608 TRACE("code:%d dragitem:%p\n", code, dragItem);
610 nmhdr.action = 0;
611 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
612 nmhdr.itemNew.hItem = dragItem;
613 nmhdr.itemNew.state = dragItem->state;
614 nmhdr.itemNew.lParam = dragItem->lParam;
616 nmhdr.ptDrag.x = pt.x;
617 nmhdr.ptDrag.y = pt.y;
619 return TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
623 static BOOL
624 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
625 HDC hdc, RECT rc)
627 NMTVCUSTOMDRAW nmcdhdr;
628 NMCUSTOMDRAW *nmcd;
630 TRACE("drawstage:0x%x hdc:%p\n", dwDrawStage, hdc);
632 nmcd = &nmcdhdr.nmcd;
633 nmcd->dwDrawStage = dwDrawStage;
634 nmcd->hdc = hdc;
635 nmcd->rc = rc;
636 nmcd->dwItemSpec = 0;
637 nmcd->uItemState = 0;
638 nmcd->lItemlParam = 0;
639 nmcdhdr.clrText = infoPtr->clrText;
640 nmcdhdr.clrTextBk = infoPtr->clrBk;
641 nmcdhdr.iLevel = 0;
643 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr.nmcd.hdr);
646 /* FIXME: need to find out when the flags in uItemState need to be set */
648 static BOOL
649 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
650 TREEVIEW_ITEM *item, UINT uItemDrawState,
651 NMTVCUSTOMDRAW *nmcdhdr)
653 NMCUSTOMDRAW *nmcd;
654 DWORD dwDrawStage;
655 DWORD_PTR dwItemSpec;
656 UINT uItemState;
658 dwDrawStage = CDDS_ITEM | uItemDrawState;
659 dwItemSpec = (DWORD_PTR)item;
660 uItemState = 0;
661 if (item->state & TVIS_SELECTED)
662 uItemState |= CDIS_SELECTED;
663 if (item == infoPtr->selectedItem)
664 uItemState |= CDIS_FOCUS;
665 if (item == infoPtr->hotItem)
666 uItemState |= CDIS_HOT;
668 nmcd = &nmcdhdr->nmcd;
669 nmcd->dwDrawStage = dwDrawStage;
670 nmcd->hdc = hdc;
671 nmcd->rc = item->rect;
672 nmcd->dwItemSpec = dwItemSpec;
673 nmcd->uItemState = uItemState;
674 nmcd->lItemlParam = item->lParam;
675 nmcdhdr->iLevel = item->iLevel;
677 TRACE("drawstage:0x%x hdc:%p item:%lx, itemstate:0x%x, lItemlParam:0x%lx\n",
678 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
679 nmcd->uItemState, nmcd->lItemlParam);
681 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr->nmcd.hdr);
684 static BOOL
685 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
687 NMTVDISPINFOW tvdi;
688 BOOL ret;
690 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
691 &tvdi.item, editItem);
693 ret = TREEVIEW_SendRealNotify(infoPtr, TVN_BEGINLABELEDITW, &tvdi.hdr);
695 if (!infoPtr->bNtfUnicode)
696 Free(tvdi.item.pszText);
698 return ret;
701 static void
702 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
703 UINT mask)
705 NMTVDISPINFOEXW callback;
707 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
708 mask &= item->callbackMask;
710 if (mask == 0) return;
712 /* 'state' always contains valid value, as well as 'lParam'.
713 * All other parameters are uninitialized.
715 callback.item.pszText = item->pszText;
716 callback.item.cchTextMax = item->cchTextMax;
717 callback.item.mask = mask;
718 callback.item.hItem = item;
719 callback.item.state = item->state;
720 callback.item.lParam = item->lParam;
722 /* If text is changed we need to recalculate textWidth */
723 if (mask & TVIF_TEXT)
724 item->textWidth = 0;
726 TREEVIEW_SendRealNotify(infoPtr, TVN_GETDISPINFOW, &callback.hdr);
727 TRACE("resulting code 0x%08x\n", callback.hdr.code);
729 /* It may have changed due to a call to SetItem. */
730 mask &= item->callbackMask;
732 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
734 /* Instead of copying text into our buffer user specified his own */
735 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
736 LPWSTR newText;
737 int buflen;
738 int len = MultiByteToWideChar( CP_ACP, 0,
739 (LPSTR)callback.item.pszText, -1,
740 NULL, 0);
741 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
742 newText = ReAlloc(item->pszText, buflen);
744 TRACE("returned str %s, len=%d, buflen=%d\n",
745 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
747 if (newText)
749 item->pszText = newText;
750 MultiByteToWideChar( CP_ACP, 0,
751 (LPSTR)callback.item.pszText, -1,
752 item->pszText, buflen/sizeof(WCHAR));
753 item->cchTextMax = buflen/sizeof(WCHAR);
755 /* If ReAlloc fails we have nothing to do, but keep original text */
757 else {
758 int len = max(lstrlenW(callback.item.pszText) + 1,
759 TEXT_CALLBACK_SIZE);
760 LPWSTR newText = ReAlloc(item->pszText, len);
762 TRACE("returned wstr %s, len=%d\n",
763 debugstr_w(callback.item.pszText), len);
765 if (newText)
767 item->pszText = newText;
768 strcpyW(item->pszText, callback.item.pszText);
769 item->cchTextMax = len;
771 /* If ReAlloc fails we have nothing to do, but keep original text */
774 else if (mask & TVIF_TEXT) {
775 /* User put text into our buffer, that is ok unless A string */
776 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
777 LPWSTR newText;
778 int buflen;
779 int len = MultiByteToWideChar( CP_ACP, 0,
780 (LPSTR)callback.item.pszText, -1,
781 NULL, 0);
782 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
783 newText = Alloc(buflen);
785 TRACE("same buffer str %s, len=%d, buflen=%d\n",
786 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
788 if (newText)
790 LPWSTR oldText = item->pszText;
791 item->pszText = newText;
792 MultiByteToWideChar( CP_ACP, 0,
793 (LPSTR)callback.item.pszText, -1,
794 item->pszText, buflen/sizeof(WCHAR));
795 item->cchTextMax = buflen/sizeof(WCHAR);
796 Free(oldText);
801 if (mask & TVIF_IMAGE)
802 item->iImage = callback.item.iImage;
804 if (mask & TVIF_SELECTEDIMAGE)
805 item->iSelectedImage = callback.item.iSelectedImage;
807 if (mask & TVIF_EXPANDEDIMAGE)
808 item->iExpandedImage = callback.item.iExpandedImage;
810 if (mask & TVIF_CHILDREN)
811 item->cChildren = callback.item.cChildren;
813 if (callback.item.mask & TVIF_STATE)
815 item->state &= ~callback.item.stateMask;
816 item->state |= (callback.item.state & callback.item.stateMask);
819 /* These members are now permanently set. */
820 if (callback.item.mask & TVIF_DI_SETITEM)
821 item->callbackMask &= ~callback.item.mask;
824 /***************************************************************************
825 * This function uses cChildren field to decide whether the item has
826 * children or not.
827 * Note: if this returns TRUE, the child items may not actually exist,
828 * they could be virtual.
830 * Just use item->firstChild to check for physical children.
832 static BOOL
833 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
835 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
836 /* Protect for a case when callback field is not changed by a host,
837 otherwise negative values trigger normal notifications. */
838 return item->cChildren != 0 && item->cChildren != I_CHILDRENCALLBACK;
841 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
843 INT format;
845 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
847 if (nCommand != NF_REQUERY) return 0;
849 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
850 TRACE("format=%d\n", format);
852 /* Invalid format returned by NF_QUERY defaults to ANSI*/
853 if (format != NFR_ANSI && format != NFR_UNICODE)
854 format = NFR_ANSI;
856 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
858 return format;
861 /* Item Position ********************************************************/
863 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
864 static VOID
865 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
867 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
868 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
869 > TVS_LINESATROOT);
871 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
872 - infoPtr->scrollX;
873 item->stateOffset = item->linesOffset + infoPtr->uIndent;
874 item->imageOffset = item->stateOffset
875 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
876 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
879 static VOID
880 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
882 HDC hdc;
883 HFONT hOldFont=0;
884 SIZE sz;
886 /* DRAW's OM docker creates items like this */
887 if (item->pszText == NULL)
889 item->textWidth = 0;
890 return;
893 if (hDC != 0)
895 hdc = hDC;
897 else
899 hdc = GetDC(infoPtr->hwnd);
900 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
903 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
904 item->textWidth = sz.cx;
906 if (hDC == 0)
908 SelectObject(hdc, hOldFont);
909 ReleaseDC(0, hdc);
913 static VOID
914 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
916 item->rect.top = infoPtr->uItemHeight *
917 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
919 item->rect.bottom = item->rect.top
920 + infoPtr->uItemHeight * item->iIntegral - 1;
922 item->rect.left = 0;
923 item->rect.right = infoPtr->clientWidth;
926 /* We know that only items after start need their order updated. */
927 static void
928 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
930 TREEVIEW_ITEM *item;
931 int order;
933 if (!start)
935 start = infoPtr->root->firstChild;
936 order = 0;
938 else
939 order = start->visibleOrder;
941 for (item = start; item != NULL;
942 item = TREEVIEW_GetNextListItem(infoPtr, item))
944 if (!ISVISIBLE(item) && order > 0)
945 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
946 item->visibleOrder = order;
947 order += item->iIntegral;
950 infoPtr->maxVisibleOrder = order;
952 for (item = start; item != NULL;
953 item = TREEVIEW_GetNextListItem(infoPtr, item))
955 TREEVIEW_ComputeItemRect(infoPtr, item);
960 /* Update metrics of all items in selected subtree.
961 * root must be expanded
963 static VOID
964 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
966 TREEVIEW_ITEM *sibling;
967 HDC hdc;
968 HFONT hOldFont;
970 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
971 return;
973 root->state &= ~TVIS_EXPANDED;
974 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
975 root->state |= TVIS_EXPANDED;
977 hdc = GetDC(infoPtr->hwnd);
978 hOldFont = SelectObject(hdc, infoPtr->hFont);
980 for (; root != sibling;
981 root = TREEVIEW_GetNextListItem(infoPtr, root))
983 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
985 if (root->callbackMask & TVIF_TEXT)
986 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
988 if (root->textWidth == 0)
990 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
991 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
995 SelectObject(hdc, hOldFont);
996 ReleaseDC(infoPtr->hwnd, hdc);
999 /* Item Allocation **********************************************************/
1001 static TREEVIEW_ITEM *
1002 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1004 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1006 if (!newItem)
1007 return NULL;
1009 /* I_IMAGENONE would make more sense but this is neither what is
1010 * documented (MSDN doesn't specify) nor what Windows actually does
1011 * (it sets it to zero)... and I can so imagine an application using
1012 * inc/dec to toggle the images. */
1013 newItem->iImage = 0;
1014 newItem->iSelectedImage = 0;
1015 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1016 newItem->infoPtr = infoPtr;
1018 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1020 Free(newItem);
1021 return NULL;
1024 return newItem;
1027 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1028 * free item->pszText. */
1029 static void
1030 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1032 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1033 if (infoPtr->selectedItem == item)
1034 infoPtr->selectedItem = NULL;
1035 if (infoPtr->hotItem == item)
1036 infoPtr->hotItem = NULL;
1037 if (infoPtr->focusedItem == item)
1038 infoPtr->focusedItem = NULL;
1039 if (infoPtr->firstVisible == item)
1040 infoPtr->firstVisible = NULL;
1041 if (infoPtr->dropItem == item)
1042 infoPtr->dropItem = NULL;
1043 if (infoPtr->insertMarkItem == item)
1044 infoPtr->insertMarkItem = NULL;
1045 Free(item);
1049 /* Item Insertion *******************************************************/
1051 /***************************************************************************
1052 * This method inserts newItem before sibling as a child of parent.
1053 * sibling can be NULL, but only if parent has no children.
1055 static void
1056 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1057 TREEVIEW_ITEM *parent)
1059 assert(parent != NULL);
1061 if (sibling != NULL)
1063 assert(sibling->parent == parent);
1065 if (sibling->prevSibling != NULL)
1066 sibling->prevSibling->nextSibling = newItem;
1068 newItem->prevSibling = sibling->prevSibling;
1069 sibling->prevSibling = newItem;
1071 else
1072 newItem->prevSibling = NULL;
1074 newItem->nextSibling = sibling;
1076 if (parent->firstChild == sibling)
1077 parent->firstChild = newItem;
1079 if (parent->lastChild == NULL)
1080 parent->lastChild = newItem;
1083 /***************************************************************************
1084 * This method inserts newItem after sibling as a child of parent.
1085 * sibling can be NULL, but only if parent has no children.
1087 static void
1088 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1089 TREEVIEW_ITEM *parent)
1091 assert(parent != NULL);
1093 if (sibling != NULL)
1095 assert(sibling->parent == parent);
1097 if (sibling->nextSibling != NULL)
1098 sibling->nextSibling->prevSibling = newItem;
1100 newItem->nextSibling = sibling->nextSibling;
1101 sibling->nextSibling = newItem;
1103 else
1104 newItem->nextSibling = NULL;
1106 newItem->prevSibling = sibling;
1108 if (parent->lastChild == sibling)
1109 parent->lastChild = newItem;
1111 if (parent->firstChild == NULL)
1112 parent->firstChild = newItem;
1115 static BOOL
1116 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1117 const TVITEMEXW *tvItem, BOOL isW)
1119 UINT callbackClear = 0;
1120 UINT callbackSet = 0;
1122 TRACE("item %p\n", item);
1123 /* Do this first in case it fails. */
1124 if (tvItem->mask & TVIF_TEXT)
1126 item->textWidth = 0; /* force width recalculation */
1127 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1129 int len;
1130 LPWSTR newText;
1131 if (isW)
1132 len = lstrlenW(tvItem->pszText) + 1;
1133 else
1134 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1136 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1138 if (newText == NULL) return FALSE;
1140 callbackClear |= TVIF_TEXT;
1142 item->pszText = newText;
1143 item->cchTextMax = len;
1144 if (isW)
1145 lstrcpynW(item->pszText, tvItem->pszText, len);
1146 else
1147 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1148 item->pszText, len);
1150 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1152 else
1154 callbackSet |= TVIF_TEXT;
1156 item->pszText = ReAlloc(item->pszText,
1157 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1158 item->cchTextMax = TEXT_CALLBACK_SIZE;
1159 TRACE("setting callback, item %p\n", item);
1163 if (tvItem->mask & TVIF_CHILDREN)
1165 item->cChildren = tvItem->cChildren;
1167 if (item->cChildren == I_CHILDRENCALLBACK)
1168 callbackSet |= TVIF_CHILDREN;
1169 else
1170 callbackClear |= TVIF_CHILDREN;
1173 if (tvItem->mask & TVIF_IMAGE)
1175 item->iImage = tvItem->iImage;
1177 if (item->iImage == I_IMAGECALLBACK)
1178 callbackSet |= TVIF_IMAGE;
1179 else
1180 callbackClear |= TVIF_IMAGE;
1183 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1185 item->iSelectedImage = tvItem->iSelectedImage;
1187 if (item->iSelectedImage == I_IMAGECALLBACK)
1188 callbackSet |= TVIF_SELECTEDIMAGE;
1189 else
1190 callbackClear |= TVIF_SELECTEDIMAGE;
1193 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1195 item->iExpandedImage = tvItem->iExpandedImage;
1197 if (item->iExpandedImage == I_IMAGECALLBACK)
1198 callbackSet |= TVIF_EXPANDEDIMAGE;
1199 else
1200 callbackClear |= TVIF_EXPANDEDIMAGE;
1203 if (tvItem->mask & TVIF_PARAM)
1204 item->lParam = tvItem->lParam;
1206 /* If the application sets TVIF_INTEGRAL without
1207 * supplying a TVITEMEX structure, it's toast. */
1208 if (tvItem->mask & TVIF_INTEGRAL)
1209 item->iIntegral = tvItem->iIntegral;
1211 if (tvItem->mask & TVIF_STATE)
1213 TRACE("prevstate 0x%x, state 0x%x, mask 0x%x\n", item->state, tvItem->state,
1214 tvItem->stateMask);
1215 item->state &= ~tvItem->stateMask;
1216 item->state |= (tvItem->state & tvItem->stateMask);
1219 if (tvItem->mask & TVIF_STATEEX)
1221 FIXME("New extended state: 0x%x\n", tvItem->uStateEx);
1224 item->callbackMask |= callbackSet;
1225 item->callbackMask &= ~callbackClear;
1227 return TRUE;
1230 /* Note that the new item is pre-zeroed. */
1231 static LRESULT
1232 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1234 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1235 HTREEITEM insertAfter;
1236 TREEVIEW_ITEM *newItem, *parentItem;
1237 BOOL bTextUpdated = FALSE;
1239 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1241 parentItem = infoPtr->root;
1243 else
1245 parentItem = ptdi->hParent;
1247 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1249 WARN("invalid parent %p\n", parentItem);
1250 return 0;
1254 insertAfter = ptdi->hInsertAfter;
1256 /* Validate this now for convenience. */
1257 switch ((DWORD_PTR)insertAfter)
1259 case (DWORD_PTR)TVI_FIRST:
1260 case (DWORD_PTR)TVI_LAST:
1261 case (DWORD_PTR)TVI_SORT:
1262 break;
1264 default:
1265 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1266 insertAfter->parent != parentItem)
1268 WARN("invalid insert after %p\n", insertAfter);
1269 insertAfter = TVI_LAST;
1273 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1274 (tvItem->mask & TVIF_TEXT)
1275 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1276 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1277 : "<no label>");
1279 newItem = TREEVIEW_AllocateItem(infoPtr);
1280 if (newItem == NULL)
1281 return 0;
1283 newItem->parent = parentItem;
1284 newItem->iIntegral = 1;
1285 newItem->visibleOrder = -1;
1287 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1288 return 0;
1290 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1292 infoPtr->uNumItems++;
1294 switch ((DWORD_PTR)insertAfter)
1296 case (DWORD_PTR)TVI_FIRST:
1298 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1299 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1300 if (infoPtr->firstVisible == originalFirst)
1301 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1303 break;
1305 case (DWORD_PTR)TVI_LAST:
1306 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1307 break;
1309 /* hInsertAfter names a specific item we want to insert after */
1310 default:
1311 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1312 break;
1314 case (DWORD_PTR)TVI_SORT:
1316 TREEVIEW_ITEM *aChild;
1317 TREEVIEW_ITEM *previousChild = NULL;
1318 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1319 BOOL bItemInserted = FALSE;
1321 aChild = parentItem->firstChild;
1323 bTextUpdated = TRUE;
1324 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1326 /* Iterate the parent children to see where we fit in */
1327 while (aChild != NULL)
1329 INT comp;
1331 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1332 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1334 if (comp < 0) /* we are smaller than the current one */
1336 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1337 if (infoPtr->firstVisible == originalFirst &&
1338 aChild == originalFirst)
1339 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1340 bItemInserted = TRUE;
1341 break;
1343 else if (comp > 0) /* we are bigger than the current one */
1345 previousChild = aChild;
1347 /* This will help us to exit if there is no more sibling */
1348 aChild = (aChild->nextSibling == 0)
1349 ? NULL
1350 : aChild->nextSibling;
1352 /* Look at the next item */
1353 continue;
1355 else if (comp == 0)
1358 * An item with this name is already existing, therefore,
1359 * we add after the one we found
1361 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1362 bItemInserted = TRUE;
1363 break;
1368 * we reach the end of the child list and the item has not
1369 * yet been inserted, therefore, insert it after the last child.
1371 if ((!bItemInserted) && (aChild == NULL))
1372 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1374 break;
1379 TRACE("new item %p; parent %p, mask 0x%x\n", newItem,
1380 newItem->parent, tvItem->mask);
1382 newItem->iLevel = newItem->parent->iLevel + 1;
1384 if (newItem->parent->cChildren == 0)
1385 newItem->parent->cChildren = 1;
1387 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1389 if (STATEIMAGEINDEX(newItem->state) == 0)
1390 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1393 if (infoPtr->firstVisible == NULL)
1394 infoPtr->firstVisible = newItem;
1396 TREEVIEW_VerifyTree(infoPtr);
1398 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1400 if (parentItem == infoPtr->root ||
1401 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1403 TREEVIEW_ITEM *item;
1404 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1406 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1407 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1409 if (!bTextUpdated)
1410 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1412 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1413 TREEVIEW_UpdateScrollBars(infoPtr);
1415 * if the item was inserted in a visible part of the tree,
1416 * invalidate it, as well as those after it
1418 for (item = newItem;
1419 item != NULL;
1420 item = TREEVIEW_GetNextListItem(infoPtr, item))
1421 TREEVIEW_Invalidate(infoPtr, item);
1423 else
1425 /* refresh treeview if newItem is the first item inserted under parentItem */
1426 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1428 /* parent got '+' - update it */
1429 TREEVIEW_Invalidate(infoPtr, parentItem);
1433 return (LRESULT)newItem;
1436 /* Item Deletion ************************************************************/
1437 static void
1438 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1440 static void
1441 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1443 TREEVIEW_ITEM *kill = parentItem->firstChild;
1445 while (kill != NULL)
1447 TREEVIEW_ITEM *next = kill->nextSibling;
1449 TREEVIEW_RemoveItem(infoPtr, kill);
1451 kill = next;
1454 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1455 assert(parentItem->firstChild == NULL);
1456 assert(parentItem->lastChild == NULL);
1459 static void
1460 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1462 TREEVIEW_ITEM *parentItem;
1464 assert(item != NULL);
1465 assert(item->parent != NULL); /* i.e. it must not be the root */
1467 parentItem = item->parent;
1469 if (parentItem->firstChild == item)
1470 parentItem->firstChild = item->nextSibling;
1472 if (parentItem->lastChild == item)
1473 parentItem->lastChild = item->prevSibling;
1475 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1476 && parentItem->cChildren > 0)
1477 parentItem->cChildren = 0;
1479 if (item->prevSibling)
1480 item->prevSibling->nextSibling = item->nextSibling;
1482 if (item->nextSibling)
1483 item->nextSibling->prevSibling = item->prevSibling;
1486 static void
1487 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1489 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1491 if (item->firstChild)
1492 TREEVIEW_RemoveAllChildren(infoPtr, item);
1494 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1495 TVIF_HANDLE | TVIF_PARAM, item, 0);
1497 TREEVIEW_UnlinkItem(item);
1499 infoPtr->uNumItems--;
1501 if (item->pszText != LPSTR_TEXTCALLBACKW)
1502 Free(item->pszText);
1504 TREEVIEW_FreeItem(infoPtr, item);
1508 /* Empty out the tree. */
1509 static void
1510 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1512 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1514 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1517 static LRESULT
1518 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1520 TREEVIEW_ITEM *newSelection = NULL;
1521 TREEVIEW_ITEM *newFirstVisible = NULL;
1522 TREEVIEW_ITEM *parent, *prev = NULL;
1523 BOOL visible = FALSE;
1525 if (item == TVI_ROOT || !item)
1527 TRACE("TVI_ROOT\n");
1528 parent = infoPtr->root;
1529 newSelection = NULL;
1530 visible = TRUE;
1531 TREEVIEW_RemoveTree(infoPtr);
1533 else
1535 if (!TREEVIEW_ValidItem(infoPtr, item))
1536 return FALSE;
1538 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1539 parent = item->parent;
1541 if (ISVISIBLE(item))
1543 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1544 visible = TRUE;
1547 if (infoPtr->selectedItem != NULL
1548 && (item == infoPtr->selectedItem
1549 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1551 if (item->nextSibling)
1552 newSelection = item->nextSibling;
1553 else if (item->parent != infoPtr->root)
1554 newSelection = item->parent;
1555 else
1556 newSelection = item->prevSibling;
1557 TRACE("newSelection = %p\n", newSelection);
1560 if (infoPtr->firstVisible == item)
1562 visible = TRUE;
1563 if (item->nextSibling)
1564 newFirstVisible = item->nextSibling;
1565 else if (item->prevSibling)
1566 newFirstVisible = item->prevSibling;
1567 else if (item->parent != infoPtr->root)
1568 newFirstVisible = item->parent;
1569 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1571 else
1572 newFirstVisible = infoPtr->firstVisible;
1574 TREEVIEW_RemoveItem(infoPtr, item);
1577 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1578 if (!infoPtr->selectedItem && newSelection)
1580 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1581 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1584 /* Validate insertMark dropItem.
1585 * hotItem ??? - used for comparison only.
1587 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1588 infoPtr->insertMarkItem = 0;
1590 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1591 infoPtr->dropItem = 0;
1593 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1594 newFirstVisible = infoPtr->root->firstChild;
1596 TREEVIEW_VerifyTree(infoPtr);
1598 if (visible)
1599 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1601 if (!infoPtr->bRedraw) return TRUE;
1603 if (visible)
1605 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1606 TREEVIEW_UpdateScrollBars(infoPtr);
1607 TREEVIEW_Invalidate(infoPtr, NULL);
1609 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1611 /* parent lost '+/-' - update it */
1612 TREEVIEW_Invalidate(infoPtr, parent);
1615 return TRUE;
1619 /* Get/Set Messages *********************************************************/
1620 static LRESULT
1621 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1623 infoPtr->bRedraw = wParam != 0;
1625 if (infoPtr->bRedraw)
1627 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1628 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1629 TREEVIEW_UpdateScrollBars(infoPtr);
1630 TREEVIEW_Invalidate(infoPtr, NULL);
1632 return 0;
1635 static LRESULT
1636 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1638 TRACE("\n");
1639 return infoPtr->uIndent;
1642 static LRESULT
1643 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1645 TRACE("\n");
1647 if (newIndent < MINIMUM_INDENT)
1648 newIndent = MINIMUM_INDENT;
1650 if (infoPtr->uIndent != newIndent)
1652 infoPtr->uIndent = newIndent;
1653 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1654 TREEVIEW_UpdateScrollBars(infoPtr);
1655 TREEVIEW_Invalidate(infoPtr, NULL);
1658 return 0;
1662 static LRESULT
1663 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1665 TRACE("\n");
1666 return (LRESULT)infoPtr->hwndToolTip;
1669 static LRESULT
1670 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1672 HWND prevToolTip;
1674 TRACE("\n");
1675 prevToolTip = infoPtr->hwndToolTip;
1676 infoPtr->hwndToolTip = hwndTT;
1678 return (LRESULT)prevToolTip;
1681 static LRESULT
1682 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1684 BOOL rc = infoPtr->bNtfUnicode;
1685 infoPtr->bNtfUnicode = fUnicode;
1686 return rc;
1689 static LRESULT
1690 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1692 return infoPtr->bNtfUnicode;
1695 static LRESULT
1696 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1698 return infoPtr->uScrollTime;
1701 static LRESULT
1702 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1704 UINT uOldScrollTime = infoPtr->uScrollTime;
1706 infoPtr->uScrollTime = min(uScrollTime, 100);
1708 return uOldScrollTime;
1712 static LRESULT
1713 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1715 TRACE("\n");
1717 switch (wParam)
1719 case TVSIL_NORMAL:
1720 return (LRESULT)infoPtr->himlNormal;
1722 case TVSIL_STATE:
1723 return (LRESULT)infoPtr->himlState;
1725 default:
1726 return 0;
1730 #define TVHEIGHT_MIN 16
1731 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1733 /* Compute the natural height for items. */
1734 static UINT
1735 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1737 TEXTMETRICW tm;
1738 HDC hdc = GetDC(0);
1739 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1740 UINT height;
1742 /* Height is the maximum of:
1743 * 16 (a hack because our fonts are tiny), and
1744 * The text height + border & margin, and
1745 * The size of the normal image list
1747 GetTextMetricsW(hdc, &tm);
1748 SelectObject(hdc, hOldFont);
1749 ReleaseDC(0, hdc);
1751 height = TVHEIGHT_MIN;
1752 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1753 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1754 if (height < infoPtr->normalImageHeight)
1755 height = infoPtr->normalImageHeight;
1757 /* Round down, unless we support odd ("non even") heights. */
1758 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1759 height &= ~1;
1761 return height;
1764 static LRESULT
1765 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1767 HIMAGELIST himlOld = 0;
1768 int oldWidth = infoPtr->normalImageWidth;
1769 int oldHeight = infoPtr->normalImageHeight;
1771 TRACE("%u,%p\n", type, himlNew);
1773 switch (type)
1775 case TVSIL_NORMAL:
1776 himlOld = infoPtr->himlNormal;
1777 infoPtr->himlNormal = himlNew;
1779 if (himlNew)
1780 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1781 &infoPtr->normalImageHeight);
1782 else
1784 infoPtr->normalImageWidth = 0;
1785 infoPtr->normalImageHeight = 0;
1788 break;
1790 case TVSIL_STATE:
1791 himlOld = infoPtr->himlState;
1792 infoPtr->himlState = himlNew;
1794 if (himlNew)
1795 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1796 &infoPtr->stateImageHeight);
1797 else
1799 infoPtr->stateImageWidth = 0;
1800 infoPtr->stateImageHeight = 0;
1803 break;
1805 default:
1806 ERR("unknown imagelist type %u\n", type);
1809 if (oldWidth != infoPtr->normalImageWidth ||
1810 oldHeight != infoPtr->normalImageHeight)
1812 BOOL bRecalcVisible = FALSE;
1814 if (oldHeight != infoPtr->normalImageHeight &&
1815 !infoPtr->bHeightSet)
1817 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1818 bRecalcVisible = TRUE;
1821 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1822 infoPtr->normalImageWidth != infoPtr->uIndent)
1824 infoPtr->uIndent = infoPtr->normalImageWidth;
1825 bRecalcVisible = TRUE;
1828 if (bRecalcVisible)
1829 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1831 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1832 TREEVIEW_UpdateScrollBars(infoPtr);
1835 TREEVIEW_Invalidate(infoPtr, NULL);
1837 return (LRESULT)himlOld;
1840 static LRESULT
1841 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1843 INT prevHeight = infoPtr->uItemHeight;
1845 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1846 if (newHeight == -1)
1848 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1849 infoPtr->bHeightSet = FALSE;
1851 else
1853 if (newHeight == 0) newHeight = 1;
1854 infoPtr->uItemHeight = newHeight;
1855 infoPtr->bHeightSet = TRUE;
1858 /* Round down, unless we support odd ("non even") heights. */
1859 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1861 infoPtr->uItemHeight &= ~1;
1862 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1865 if (infoPtr->uItemHeight != prevHeight)
1867 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1868 TREEVIEW_UpdateScrollBars(infoPtr);
1869 TREEVIEW_Invalidate(infoPtr, NULL);
1872 return prevHeight;
1875 static LRESULT
1876 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1878 TRACE("\n");
1879 return infoPtr->uItemHeight;
1883 static LRESULT
1884 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1886 TRACE("%p\n", infoPtr->hFont);
1887 return (LRESULT)infoPtr->hFont;
1891 static INT CALLBACK
1892 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1894 (void)unused;
1896 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1898 return 1;
1901 static LRESULT
1902 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1904 UINT uHeight = infoPtr->uItemHeight;
1906 TRACE("%p %i\n", hFont, bRedraw);
1908 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1910 DeleteObject(infoPtr->hBoldFont);
1911 DeleteObject(infoPtr->hUnderlineFont);
1912 DeleteObject(infoPtr->hBoldUnderlineFont);
1913 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1914 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1915 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
1917 if (!infoPtr->bHeightSet)
1918 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1920 if (uHeight != infoPtr->uItemHeight)
1921 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1923 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1925 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1926 TREEVIEW_UpdateScrollBars(infoPtr);
1928 if (bRedraw)
1929 TREEVIEW_Invalidate(infoPtr, NULL);
1931 return 0;
1935 static LRESULT
1936 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1938 TRACE("\n");
1939 return (LRESULT)infoPtr->clrLine;
1942 static LRESULT
1943 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1945 COLORREF prevColor = infoPtr->clrLine;
1947 TRACE("\n");
1948 infoPtr->clrLine = color;
1949 return (LRESULT)prevColor;
1953 static LRESULT
1954 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1956 TRACE("\n");
1957 return (LRESULT)infoPtr->clrText;
1960 static LRESULT
1961 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1963 COLORREF prevColor = infoPtr->clrText;
1965 TRACE("\n");
1966 infoPtr->clrText = color;
1968 if (infoPtr->clrText != prevColor)
1969 TREEVIEW_Invalidate(infoPtr, NULL);
1971 return (LRESULT)prevColor;
1975 static LRESULT
1976 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1978 TRACE("\n");
1979 return (LRESULT)infoPtr->clrBk;
1982 static LRESULT
1983 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1985 COLORREF prevColor = infoPtr->clrBk;
1987 TRACE("\n");
1988 infoPtr->clrBk = newColor;
1990 if (newColor != prevColor)
1991 TREEVIEW_Invalidate(infoPtr, NULL);
1993 return (LRESULT)prevColor;
1997 static LRESULT
1998 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
2000 TRACE("\n");
2001 return (LRESULT)infoPtr->clrInsertMark;
2004 static LRESULT
2005 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
2007 COLORREF prevColor = infoPtr->clrInsertMark;
2009 TRACE("0x%08x\n", color);
2010 infoPtr->clrInsertMark = color;
2012 return (LRESULT)prevColor;
2016 static LRESULT
2017 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2019 TRACE("%d %p\n", wParam, item);
2021 if (!TREEVIEW_ValidItem(infoPtr, item))
2022 return 0;
2024 infoPtr->insertBeforeorAfter = wParam;
2025 infoPtr->insertMarkItem = item;
2027 TREEVIEW_Invalidate(infoPtr, NULL);
2029 return 1;
2033 /************************************************************************
2034 * Some serious braindamage here. lParam is a pointer to both the
2035 * input HTREEITEM and the output RECT.
2037 static LRESULT
2038 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2040 TREEVIEW_ITEM *item;
2041 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2043 TRACE("\n");
2045 if (pItem == NULL)
2046 return FALSE;
2048 item = *pItem;
2049 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2050 return FALSE;
2053 * If wParam is TRUE return the text size otherwise return
2054 * the whole item size
2056 if (fTextRect)
2058 /* Windows does not send TVN_GETDISPINFO here. */
2060 lpRect->top = item->rect.top;
2061 lpRect->bottom = item->rect.bottom;
2063 lpRect->left = item->textOffset;
2064 if (!item->textWidth)
2065 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2067 lpRect->right = item->textOffset + item->textWidth + 4;
2069 else
2071 *lpRect = item->rect;
2074 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2076 return TRUE;
2079 static inline LRESULT
2080 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2082 /* Surprise! This does not take integral height into account. */
2083 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2084 return infoPtr->clientHeight / infoPtr->uItemHeight;
2088 static LRESULT
2089 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2091 TREEVIEW_ITEM *item = tvItem->hItem;
2093 if (!TREEVIEW_ValidItem(infoPtr, item))
2095 BOOL valid_item = FALSE;
2096 if (!item) return FALSE;
2098 __TRY
2100 infoPtr = item->infoPtr;
2101 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2102 valid_item = TREEVIEW_ValidItem(infoPtr, item);
2104 __EXCEPT_PAGE_FAULT
2107 __ENDTRY
2108 if (!valid_item) return FALSE;
2111 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2113 if (tvItem->mask & TVIF_CHILDREN)
2115 if (item->cChildren==I_CHILDRENCALLBACK)
2116 FIXME("I_CHILDRENCALLBACK not supported\n");
2117 tvItem->cChildren = item->cChildren;
2120 if (tvItem->mask & TVIF_HANDLE)
2121 tvItem->hItem = item;
2123 if (tvItem->mask & TVIF_IMAGE)
2124 tvItem->iImage = item->iImage;
2126 if (tvItem->mask & TVIF_INTEGRAL)
2127 tvItem->iIntegral = item->iIntegral;
2129 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2130 tvItem->lParam = item->lParam;
2132 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2133 tvItem->iSelectedImage = item->iSelectedImage;
2135 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2136 tvItem->iExpandedImage = item->iExpandedImage;
2138 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2139 tvItem->state = item->state;
2141 if (tvItem->mask & TVIF_TEXT)
2143 if (item->pszText == NULL)
2145 if (tvItem->cchTextMax > 0)
2146 tvItem->pszText[0] = '\0';
2148 else if (isW)
2150 if (item->pszText == LPSTR_TEXTCALLBACKW)
2152 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2153 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2155 else
2157 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2160 else
2162 if (item->pszText == LPSTR_TEXTCALLBACKW)
2164 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2165 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2167 else
2169 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2170 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2175 if (tvItem->mask & TVIF_STATEEX)
2177 FIXME("Extended item state not supported, returning 0.\n");
2178 tvItem->uStateEx = 0;
2181 TRACE("item <%p>, txt %p, img %d, mask 0x%x\n",
2182 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2184 return TRUE;
2187 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2188 * which is wrong. */
2189 static LRESULT
2190 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2192 TREEVIEW_ITEM *item;
2193 TREEVIEW_ITEM originalItem;
2195 item = tvItem->hItem;
2197 TRACE("item %d, mask 0x%x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2198 tvItem->mask);
2200 if (!TREEVIEW_ValidItem(infoPtr, item))
2201 return FALSE;
2203 /* store the original item values */
2204 originalItem = *item;
2206 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2207 return FALSE;
2209 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2210 if ((tvItem->mask & TVIF_TEXT
2211 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2212 && ISVISIBLE(item))
2214 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2215 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2218 if (tvItem->mask != 0 && ISVISIBLE(item))
2220 /* The refresh updates everything, but we can't wait until then. */
2221 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2223 /* if any of the item's values changed and it's not a callback, redraw the item */
2224 if (item_changed(&originalItem, item, tvItem))
2226 if (tvItem->mask & TVIF_INTEGRAL)
2228 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2229 TREEVIEW_UpdateScrollBars(infoPtr);
2231 TREEVIEW_Invalidate(infoPtr, NULL);
2233 else
2235 TREEVIEW_UpdateScrollBars(infoPtr);
2236 TREEVIEW_Invalidate(infoPtr, item);
2241 return TRUE;
2244 static LRESULT
2245 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2247 TRACE("\n");
2249 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2250 return 0;
2252 return (item->state & mask);
2255 static LRESULT
2256 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2258 TREEVIEW_ITEM *retval;
2260 retval = 0;
2262 /* handle all the global data here */
2263 switch (which)
2265 case TVGN_CHILD: /* Special case: child of 0 is root */
2266 if (item)
2267 break;
2268 /* fall through */
2269 case TVGN_ROOT:
2270 retval = infoPtr->root->firstChild;
2271 break;
2273 case TVGN_CARET:
2274 retval = infoPtr->selectedItem;
2275 break;
2277 case TVGN_FIRSTVISIBLE:
2278 retval = infoPtr->firstVisible;
2279 break;
2281 case TVGN_DROPHILITE:
2282 retval = infoPtr->dropItem;
2283 break;
2285 case TVGN_LASTVISIBLE:
2286 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2287 break;
2290 if (retval)
2292 TRACE("flags:0x%x, returns %p\n", which, retval);
2293 return (LRESULT)retval;
2296 if (item == TVI_ROOT) item = infoPtr->root;
2298 if (!TREEVIEW_ValidItem(infoPtr, item))
2299 return FALSE;
2301 switch (which)
2303 case TVGN_NEXT:
2304 retval = item->nextSibling;
2305 break;
2306 case TVGN_PREVIOUS:
2307 retval = item->prevSibling;
2308 break;
2309 case TVGN_PARENT:
2310 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2311 break;
2312 case TVGN_CHILD:
2313 retval = item->firstChild;
2314 break;
2315 case TVGN_NEXTVISIBLE:
2316 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2317 break;
2318 case TVGN_PREVIOUSVISIBLE:
2319 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2320 break;
2321 default:
2322 TRACE("Unknown msg 0x%x, item %p\n", which, item);
2323 break;
2326 TRACE("flags: 0x%x, item %p;returns %p\n", which, item, retval);
2327 return (LRESULT)retval;
2331 static LRESULT
2332 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2334 TRACE(" %d\n", infoPtr->uNumItems);
2335 return (LRESULT)infoPtr->uNumItems;
2338 static VOID
2339 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2341 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2343 static const unsigned int state_table[] = { 0, 2, 1 };
2345 unsigned int state;
2347 state = STATEIMAGEINDEX(item->state);
2348 TRACE("state: 0x%x\n", state);
2349 item->state &= ~TVIS_STATEIMAGEMASK;
2351 if (state < 3)
2352 state = state_table[state];
2354 item->state |= INDEXTOSTATEIMAGEMASK(state);
2356 TRACE("state: 0x%x\n", state);
2357 TREEVIEW_Invalidate(infoPtr, item);
2362 /* Painting *************************************************************/
2364 /* Draw the lines and expand button for an item. Also draws one section
2365 * of the line from item's parent to item's parent's next sibling. */
2366 static void
2367 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2369 LONG centerx, centery;
2370 BOOL lar = ((infoPtr->dwStyle
2371 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2372 > TVS_LINESATROOT);
2373 HBRUSH hbr, hbrOld;
2374 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2376 if (!lar && item->iLevel == 0)
2377 return;
2379 hbr = CreateSolidBrush(clrBk);
2380 hbrOld = SelectObject(hdc, hbr);
2382 centerx = (item->linesOffset + item->stateOffset) / 2;
2383 centery = (item->rect.top + item->rect.bottom) / 2;
2385 if (infoPtr->dwStyle & TVS_HASLINES)
2387 HPEN hOldPen, hNewPen;
2388 HTREEITEM parent;
2389 LOGBRUSH lb;
2391 /* Get a dotted grey pen */
2392 lb.lbStyle = BS_SOLID;
2393 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2394 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2395 hOldPen = SelectObject(hdc, hNewPen);
2397 /* Make sure the center is on a dot (using +2 instead
2398 * of +1 gives us pixel-by-pixel compat with native) */
2399 centery = (centery + 2) & ~1;
2401 MoveToEx(hdc, item->stateOffset, centery, NULL);
2402 LineTo(hdc, centerx - 1, centery);
2404 if (item->prevSibling || item->parent != infoPtr->root)
2406 MoveToEx(hdc, centerx, item->rect.top, NULL);
2407 LineTo(hdc, centerx, centery);
2410 if (item->nextSibling)
2412 MoveToEx(hdc, centerx, centery, NULL);
2413 LineTo(hdc, centerx, item->rect.bottom + 1);
2416 /* Draw the line from our parent to its next sibling. */
2417 parent = item->parent;
2418 while (parent != infoPtr->root)
2420 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2422 if (parent->nextSibling
2423 /* skip top-levels unless TVS_LINESATROOT */
2424 && parent->stateOffset > parent->linesOffset)
2426 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2427 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2430 parent = parent->parent;
2433 SelectObject(hdc, hOldPen);
2434 DeleteObject(hNewPen);
2438 * Display the (+/-) signs
2441 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2443 if (item->cChildren)
2445 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2446 if (theme)
2448 RECT glyphRect = item->rect;
2449 glyphRect.left = item->linesOffset;
2450 glyphRect.right = item->stateOffset;
2451 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2452 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2453 &glyphRect, NULL);
2455 else
2457 LONG height = item->rect.bottom - item->rect.top;
2458 LONG width = item->stateOffset - item->linesOffset;
2459 LONG rectsize = min(height, width) / 4;
2460 /* plussize = ceil(rectsize * 3/4) */
2461 LONG plussize = (rectsize + 1) * 3 / 4;
2463 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2464 HPEN old_pen = SelectObject(hdc, new_pen);
2466 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2467 centerx + rectsize + 2, centery + rectsize + 2);
2469 SelectObject(hdc, old_pen);
2470 DeleteObject(new_pen);
2472 /* draw +/- signs with current text color */
2473 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2474 old_pen = SelectObject(hdc, new_pen);
2476 if (height < 18 || width < 18)
2478 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2479 LineTo(hdc, centerx + plussize, centery);
2481 if (!(item->state & TVIS_EXPANDED) ||
2482 (item->state & TVIS_EXPANDPARTIAL))
2484 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2485 LineTo(hdc, centerx, centery + plussize);
2488 else
2490 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2491 centerx + plussize, centery + 2);
2493 if (!(item->state & TVIS_EXPANDED) ||
2494 (item->state & TVIS_EXPANDPARTIAL))
2496 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2497 centerx + 2, centery + plussize);
2498 SetPixel(hdc, centerx - 1, centery, clrBk);
2499 SetPixel(hdc, centerx + 1, centery, clrBk);
2503 SelectObject(hdc, old_pen);
2504 DeleteObject(new_pen);
2508 SelectObject(hdc, hbrOld);
2509 DeleteObject(hbr);
2512 static void
2513 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2515 INT cditem;
2516 HFONT hOldFont;
2517 COLORREF oldTextColor, oldTextBkColor;
2518 int centery;
2519 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2520 NMTVCUSTOMDRAW nmcdhdr;
2522 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2524 /* - If item is drop target or it is selected and window is in focus -
2525 * use blue background (COLOR_HIGHLIGHT).
2526 * - If item is selected, window is not in focus, but it has style
2527 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2528 * - Otherwise - use background color
2530 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2531 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2532 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2534 if ((item->state & TVIS_DROPHILITED) || inFocus)
2536 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2537 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2539 else
2541 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2542 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2545 else
2547 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2548 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2549 nmcdhdr.clrText = comctl32_color.clrHighlight;
2550 else
2551 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2554 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2555 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2556 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2558 /* The custom draw handler can query the text rectangle,
2559 * so get ready. */
2560 /* should already be known, set to 0 when changed */
2561 if (!item->textWidth)
2562 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2564 cditem = 0;
2566 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2568 cditem = TREEVIEW_SendCustomDrawItemNotify
2569 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2570 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2572 if (cditem & CDRF_SKIPDEFAULT)
2574 SelectObject(hdc, hOldFont);
2575 return;
2579 if (cditem & CDRF_NEWFONT)
2580 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2582 if (TREEVIEW_IsFullRowSelect(infoPtr))
2584 HBRUSH brush = CreateSolidBrush(nmcdhdr.clrTextBk);
2585 FillRect(hdc, &item->rect, brush);
2586 DeleteObject(brush);
2589 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2591 /* reset colors. Custom draw handler can change them */
2592 SetTextColor(hdc, nmcdhdr.clrText);
2593 SetBkColor(hdc, nmcdhdr.clrTextBk);
2595 centery = (item->rect.top + item->rect.bottom) / 2;
2598 * Display the images associated with this item
2601 INT imageIndex;
2603 /* State images are displayed to the left of the Normal image
2604 * image number is in state; zero should be `display no image'.
2606 imageIndex = STATEIMAGEINDEX(item->state);
2608 if (infoPtr->himlState && imageIndex)
2610 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2611 item->stateOffset,
2612 centery - infoPtr->stateImageHeight / 2,
2613 ILD_NORMAL);
2616 /* Now, draw the normal image; can be either selected,
2617 * non-selected or expanded image.
2620 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2622 /* The item is currently selected */
2623 imageIndex = item->iSelectedImage;
2625 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2627 /* The item is currently not selected but expanded */
2628 imageIndex = item->iExpandedImage;
2630 else
2632 /* The item is not selected and not expanded */
2633 imageIndex = item->iImage;
2636 if (infoPtr->himlNormal)
2638 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2640 style |= item->state & TVIS_OVERLAYMASK;
2642 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2643 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2644 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2645 style);
2651 * Display the text associated with this item
2654 /* Don't paint item's text if it's being edited */
2655 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2657 if (item->pszText)
2659 RECT rcText;
2660 UINT align;
2661 SIZE sz;
2663 rcText.top = item->rect.top;
2664 rcText.bottom = item->rect.bottom;
2665 rcText.left = item->textOffset;
2666 rcText.right = rcText.left + item->textWidth + 4;
2668 TRACE("drawing text %s at (%s)\n",
2669 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2671 /* Draw it */
2672 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2674 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2675 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2676 ETO_CLIPPED | ETO_OPAQUE,
2677 &rcText,
2678 item->pszText,
2679 lstrlenW(item->pszText),
2680 NULL);
2681 SetTextAlign(hdc, align);
2683 /* Draw focus box around the selected item */
2684 if ((item == infoPtr->selectedItem) && inFocus)
2686 DrawFocusRect(hdc,&rcText);
2691 /* Draw insertion mark if necessary */
2693 if (infoPtr->insertMarkItem)
2694 TRACE("item:%d,mark:%p\n",
2695 TREEVIEW_GetItemIndex(infoPtr, item),
2696 infoPtr->insertMarkItem);
2698 if (item == infoPtr->insertMarkItem)
2700 HPEN hNewPen, hOldPen;
2701 int offset;
2702 int left, right;
2704 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2705 hOldPen = SelectObject(hdc, hNewPen);
2707 if (infoPtr->insertBeforeorAfter)
2708 offset = item->rect.bottom - 1;
2709 else
2710 offset = item->rect.top + 1;
2712 left = item->textOffset - 2;
2713 right = item->textOffset + item->textWidth + 2;
2715 MoveToEx(hdc, left, offset - 3, NULL);
2716 LineTo(hdc, left, offset + 4);
2718 MoveToEx(hdc, left, offset, NULL);
2719 LineTo(hdc, right + 1, offset);
2721 MoveToEx(hdc, right, offset + 3, NULL);
2722 LineTo(hdc, right, offset - 4);
2724 SelectObject(hdc, hOldPen);
2725 DeleteObject(hNewPen);
2728 /* Restore the hdc state */
2729 SetTextColor(hdc, oldTextColor);
2730 SetBkColor(hdc, oldTextBkColor);
2731 SelectObject(hdc, hOldFont);
2733 if (cditem & CDRF_NOTIFYPOSTPAINT)
2735 cditem = TREEVIEW_SendCustomDrawItemNotify
2736 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2737 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2741 /* Computes treeHeight and treeWidth and updates the scroll bars.
2743 static void
2744 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2746 TREEVIEW_ITEM *item;
2747 HWND hwnd = infoPtr->hwnd;
2748 BOOL vert = FALSE;
2749 BOOL horz = FALSE;
2750 SCROLLINFO si;
2751 LONG scrollX = infoPtr->scrollX;
2753 infoPtr->treeWidth = 0;
2754 infoPtr->treeHeight = 0;
2756 /* We iterate through all visible items in order to get the tree height
2757 * and width */
2758 item = infoPtr->root->firstChild;
2760 while (item != NULL)
2762 if (ISVISIBLE(item))
2764 /* actually we draw text at textOffset + 2 */
2765 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2766 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2768 /* This is scroll-adjusted, but we fix this below. */
2769 infoPtr->treeHeight = item->rect.bottom;
2772 item = TREEVIEW_GetNextListItem(infoPtr, item);
2775 /* Fix the scroll adjusted treeHeight and treeWidth. */
2776 if (infoPtr->root->firstChild)
2777 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2779 infoPtr->treeWidth += infoPtr->scrollX;
2781 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2783 /* Adding one scroll bar may take up enough space that it forces us
2784 * to add the other as well. */
2785 if (infoPtr->treeHeight > infoPtr->clientHeight)
2787 vert = TRUE;
2789 if (infoPtr->treeWidth
2790 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2791 horz = TRUE;
2793 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2794 horz = TRUE;
2796 if (!vert && horz && infoPtr->treeHeight
2797 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2798 vert = TRUE;
2800 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2802 si.cbSize = sizeof(SCROLLINFO);
2803 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2804 si.nMin = 0;
2806 if (vert)
2808 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2809 if ( si.nPage && NULL != infoPtr->firstVisible)
2811 si.nPos = infoPtr->firstVisible->visibleOrder;
2812 si.nMax = infoPtr->maxVisibleOrder - 1;
2814 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2816 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2817 ShowScrollBar(hwnd, SB_VERT, TRUE);
2818 infoPtr->uInternalStatus |= TV_VSCROLL;
2820 else
2822 if (infoPtr->uInternalStatus & TV_VSCROLL)
2823 ShowScrollBar(hwnd, SB_VERT, FALSE);
2824 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2827 else
2829 if (infoPtr->uInternalStatus & TV_VSCROLL)
2830 ShowScrollBar(hwnd, SB_VERT, FALSE);
2831 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2834 if (horz)
2836 si.nPage = infoPtr->clientWidth;
2837 si.nPos = infoPtr->scrollX;
2838 si.nMax = infoPtr->treeWidth - 1;
2840 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2842 si.nPos = si.nMax - max( si.nPage-1, 0 );
2843 scrollX = si.nPos;
2846 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2847 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2848 infoPtr->uInternalStatus |= TV_HSCROLL;
2850 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2851 TREEVIEW_HScroll(infoPtr,
2852 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2854 else
2856 if (infoPtr->uInternalStatus & TV_HSCROLL)
2857 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2858 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2860 scrollX = 0;
2861 if (infoPtr->scrollX != 0)
2863 TREEVIEW_HScroll(infoPtr,
2864 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2868 if (!horz)
2869 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2872 static void
2873 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2875 HBRUSH hBrush;
2876 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2878 hBrush = CreateSolidBrush(clrBk);
2879 FillRect(hdc, rc, hBrush);
2880 DeleteObject(hBrush);
2883 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2884 static LRESULT
2885 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2887 RECT rect;
2889 TRACE("%p\n", infoPtr);
2891 GetClientRect(infoPtr->hwnd, &rect);
2892 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2894 return 1;
2897 static void
2898 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2900 HWND hwnd = infoPtr->hwnd;
2901 RECT rect = *rc;
2902 TREEVIEW_ITEM *item;
2904 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2906 TRACE("empty window\n");
2907 return;
2910 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2911 hdc, rect);
2913 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2915 ReleaseDC(hwnd, hdc);
2916 return;
2919 for (item = infoPtr->root->firstChild;
2920 item != NULL;
2921 item = TREEVIEW_GetNextListItem(infoPtr, item))
2923 if (ISVISIBLE(item))
2925 /* Avoid unneeded calculations */
2926 if (item->rect.top > rect.bottom)
2927 break;
2928 if (item->rect.bottom < rect.top)
2929 continue;
2931 TREEVIEW_DrawItem(infoPtr, hdc, item);
2935 TREEVIEW_UpdateScrollBars(infoPtr);
2937 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2938 infoPtr->cdmode =
2939 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2942 static inline void
2943 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2945 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2948 static void
2949 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2951 if (item)
2952 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2953 else
2954 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2957 static void
2958 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2960 RECT rc;
2961 HBITMAP hbm, hbmOld;
2962 HDC hdc, hdcScreen;
2963 int nIndex;
2965 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2967 hdcScreen = GetDC(0);
2969 hdc = CreateCompatibleDC(hdcScreen);
2970 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2971 hbmOld = SelectObject(hdc, hbm);
2973 SetRect(&rc, 0, 0, 48, 16);
2974 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2976 SetRect(&rc, 18, 2, 30, 14);
2977 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2978 DFCS_BUTTONCHECK|DFCS_FLAT);
2980 SetRect(&rc, 34, 2, 46, 14);
2981 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2982 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2984 SelectObject(hdc, hbmOld);
2985 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2986 comctl32_color.clrWindow);
2987 TRACE("checkbox index %d\n", nIndex);
2989 DeleteObject(hbm);
2990 DeleteDC(hdc);
2991 ReleaseDC(0, hdcScreen);
2993 infoPtr->stateImageWidth = 16;
2994 infoPtr->stateImageHeight = 16;
2997 static void
2998 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3000 TREEVIEW_ITEM *child = item->firstChild;
3002 item->state &= ~TVIS_STATEIMAGEMASK;
3003 item->state |= INDEXTOSTATEIMAGEMASK(1);
3005 while (child)
3007 TREEVIEW_ITEM *next = child->nextSibling;
3008 TREEVIEW_ResetImageStateIndex(infoPtr, child);
3009 child = next;
3013 static LRESULT
3014 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
3016 HDC hdc;
3017 PAINTSTRUCT ps;
3018 RECT rc;
3020 TRACE("(%p %p)\n", infoPtr, hdc_ref);
3022 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
3024 TREEVIEW_InitCheckboxes(infoPtr);
3025 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
3027 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
3028 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
3029 TREEVIEW_UpdateScrollBars(infoPtr);
3030 TREEVIEW_Invalidate(infoPtr, NULL);
3033 if (hdc_ref)
3035 hdc = hdc_ref;
3036 GetClientRect(infoPtr->hwnd, &rc);
3037 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3039 else
3041 hdc = BeginPaint(infoPtr->hwnd, &ps);
3042 rc = ps.rcPaint;
3043 if(ps.fErase)
3044 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3047 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3048 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3050 if (!hdc_ref)
3051 EndPaint(infoPtr->hwnd, &ps);
3053 return 0;
3056 static LRESULT
3057 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3059 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3061 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3062 return 0;
3064 if (options & PRF_ERASEBKGND)
3065 TREEVIEW_EraseBackground(infoPtr, hdc);
3067 if (options & PRF_CLIENT)
3069 RECT rc;
3070 GetClientRect(infoPtr->hwnd, &rc);
3071 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3074 return 0;
3077 /* Sorting **************************************************************/
3079 /***************************************************************************
3080 * Forward the DPA local callback to the treeview owner callback
3082 static INT WINAPI
3083 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3084 const TVSORTCB *pCallBackSort)
3086 /* Forward the call to the client-defined callback */
3087 return pCallBackSort->lpfnCompare(first->lParam,
3088 second->lParam,
3089 pCallBackSort->lParam);
3092 /***************************************************************************
3093 * Treeview native sort routine: sort on item text.
3095 static INT WINAPI
3096 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3097 const TREEVIEW_INFO *infoPtr)
3099 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3100 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3102 if(first->pszText && second->pszText)
3103 return lstrcmpiW(first->pszText, second->pszText);
3104 else if(first->pszText)
3105 return -1;
3106 else if(second->pszText)
3107 return 1;
3108 else
3109 return 0;
3112 /* Returns the number of physical children belonging to item. */
3113 static INT
3114 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3116 INT cChildren = 0;
3117 HTREEITEM hti;
3119 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3120 cChildren++;
3122 return cChildren;
3125 /* Returns a DPA containing a pointer to each physical child of item in
3126 * sibling order. If item has no children, an empty DPA is returned. */
3127 static HDPA
3128 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3130 HTREEITEM child;
3132 HDPA list = DPA_Create(8);
3133 if (list == 0) return NULL;
3135 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3137 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3139 DPA_Destroy(list);
3140 return NULL;
3144 return list;
3147 /***************************************************************************
3148 * Setup the treeview structure with regards of the sort method
3149 * and sort the children of the TV item specified in lParam
3150 * fRecurse: currently unused. Should be zero.
3151 * parent: if pSort!=NULL, should equal pSort->hParent.
3152 * otherwise, item which child items are to be sorted.
3153 * pSort: sort method info. if NULL, sort on item text.
3154 * if non-NULL, sort on item's lParam content, and let the
3155 * application decide what that means. See also TVM_SORTCHILDRENCB.
3158 static LRESULT
3159 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3160 LPTVSORTCB pSort)
3162 INT cChildren;
3163 PFNDPACOMPARE pfnCompare;
3164 LPARAM lpCompare;
3166 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3167 if (parent == TVI_ROOT || parent == NULL)
3168 parent = infoPtr->root;
3170 /* Check for a valid handle to the parent item */
3171 if (!TREEVIEW_ValidItem(infoPtr, parent))
3173 ERR("invalid item hParent=%p\n", parent);
3174 return FALSE;
3177 if (pSort)
3179 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3180 lpCompare = (LPARAM)pSort;
3182 else
3184 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3185 lpCompare = (LPARAM)infoPtr;
3188 cChildren = TREEVIEW_CountChildren(parent);
3190 /* Make sure there is something to sort */
3191 if (cChildren > 1)
3193 /* TREEVIEW_ITEM rechaining */
3194 INT count = 0;
3195 HTREEITEM item = 0;
3196 HTREEITEM nextItem = 0;
3197 HTREEITEM prevItem = 0;
3199 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3201 if (sortList == NULL)
3202 return FALSE;
3204 /* let DPA sort the list */
3205 DPA_Sort(sortList, pfnCompare, lpCompare);
3207 /* The order of DPA entries has been changed, so fixup the
3208 * nextSibling and prevSibling pointers. */
3210 item = DPA_GetPtr(sortList, count++);
3211 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3213 /* link the two current item together */
3214 item->nextSibling = nextItem;
3215 nextItem->prevSibling = item;
3217 if (prevItem == NULL)
3219 /* this is the first item, update the parent */
3220 parent->firstChild = item;
3221 item->prevSibling = NULL;
3223 else
3225 /* fix the back chaining */
3226 item->prevSibling = prevItem;
3229 /* get ready for the next one */
3230 prevItem = item;
3231 item = nextItem;
3234 /* the last item is pointed to by item and never has a sibling */
3235 item->nextSibling = NULL;
3236 parent->lastChild = item;
3238 DPA_Destroy(sortList);
3240 TREEVIEW_VerifyTree(infoPtr);
3242 if (parent->state & TVIS_EXPANDED)
3244 int visOrder = infoPtr->firstVisible->visibleOrder;
3246 if (parent == infoPtr->root)
3247 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3248 else
3249 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3251 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3253 TREEVIEW_ITEM *item;
3255 for (item = infoPtr->root->firstChild; item != NULL;
3256 item = TREEVIEW_GetNextListItem(infoPtr, item))
3258 if (item->visibleOrder == visOrder)
3259 break;
3262 if (!item) item = parent->firstChild;
3263 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3266 TREEVIEW_Invalidate(infoPtr, NULL);
3269 return TRUE;
3271 return FALSE;
3275 /***************************************************************************
3276 * Setup the treeview structure with regards of the sort method
3277 * and sort the children of the TV item specified in lParam
3279 static LRESULT
3280 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3282 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3286 /***************************************************************************
3287 * Sort the children of the TV item specified in lParam.
3289 static LRESULT
3290 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3292 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3296 /* Expansion/Collapse ***************************************************/
3298 static BOOL
3299 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3300 UINT action)
3302 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3303 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3304 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3305 0, item);
3308 static VOID
3309 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3310 UINT action)
3312 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3313 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3314 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3315 0, item);
3319 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3320 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3321 static BOOL
3322 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3323 BOOL bRemoveChildren, BOOL bUser)
3325 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3326 BOOL bSetSelection, bSetFirstVisible;
3327 RECT scrollRect;
3328 LONG scrollDist = 0;
3329 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3330 BOOL wasExpanded;
3332 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3334 if (!TREEVIEW_HasChildren(infoPtr, item))
3335 return FALSE;
3337 if (bUser)
3338 TREEVIEW_SendExpanding(infoPtr, item, action);
3340 if (item->firstChild == NULL)
3341 return FALSE;
3343 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3344 item->state &= ~TVIS_EXPANDED;
3346 if (wasExpanded && bUser)
3347 TREEVIEW_SendExpanded(infoPtr, item, action);
3349 bSetSelection = (infoPtr->selectedItem != NULL
3350 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3352 bSetFirstVisible = (infoPtr->firstVisible != NULL
3353 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3355 tmpItem = item;
3356 while (tmpItem)
3358 if (tmpItem->nextSibling)
3360 nextItem = tmpItem->nextSibling;
3361 break;
3363 tmpItem = tmpItem->parent;
3366 if (nextItem)
3367 scrollDist = nextItem->rect.top;
3369 if (bRemoveChildren)
3371 INT old_cChildren = item->cChildren;
3372 TRACE("TVE_COLLAPSERESET\n");
3373 item->state &= ~TVIS_EXPANDEDONCE;
3374 TREEVIEW_RemoveAllChildren(infoPtr, item);
3375 item->cChildren = old_cChildren;
3377 if (!wasExpanded)
3378 return FALSE;
3380 if (item->firstChild)
3382 TREEVIEW_ITEM *i, *sibling;
3384 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3386 for (i = item->firstChild; i != sibling;
3387 i = TREEVIEW_GetNextListItem(infoPtr, i))
3389 i->visibleOrder = -1;
3393 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3395 if (nextItem)
3396 scrollDist = -(scrollDist - nextItem->rect.top);
3398 if (bSetSelection)
3400 /* Don't call DoSelectItem, it sends notifications. */
3401 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3402 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3403 item->state |= TVIS_SELECTED;
3404 infoPtr->selectedItem = item;
3407 TREEVIEW_UpdateScrollBars(infoPtr);
3409 scrollRect.left = 0;
3410 scrollRect.right = infoPtr->clientWidth;
3411 scrollRect.bottom = infoPtr->clientHeight;
3413 if (nextItem)
3415 scrollRect.top = nextItem->rect.top;
3417 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3418 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3419 TREEVIEW_Invalidate(infoPtr, item);
3420 } else {
3421 scrollRect.top = item->rect.top;
3422 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3425 TREEVIEW_SetFirstVisible(infoPtr,
3426 bSetFirstVisible ? item : infoPtr->firstVisible,
3427 TRUE);
3429 return wasExpanded;
3432 static BOOL
3433 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3434 BOOL partial, BOOL user)
3436 LONG scrollDist;
3437 LONG orgNextTop = 0;
3438 RECT scrollRect;
3439 TREEVIEW_ITEM *nextItem, *tmpItem;
3440 BOOL sendsNotifications;
3442 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3444 if (!TREEVIEW_HasChildren(infoPtr, item))
3445 return FALSE;
3447 tmpItem = item; nextItem = NULL;
3448 while (tmpItem)
3450 if (tmpItem->nextSibling)
3452 nextItem = tmpItem->nextSibling;
3453 break;
3455 tmpItem = tmpItem->parent;
3458 if (nextItem)
3459 orgNextTop = nextItem->rect.top;
3461 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3463 sendsNotifications = user || ((item->cChildren != 0) &&
3464 !(item->state & TVIS_EXPANDEDONCE));
3465 if (sendsNotifications)
3467 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3469 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3470 return FALSE;
3473 if (!item->firstChild)
3474 return FALSE;
3476 item->state |= TVIS_EXPANDED;
3478 if (partial)
3479 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3481 if (ISVISIBLE(item))
3483 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3484 TREEVIEW_UpdateSubTree(infoPtr, item);
3485 TREEVIEW_UpdateScrollBars(infoPtr);
3487 scrollRect.left = 0;
3488 scrollRect.bottom = infoPtr->treeHeight;
3489 scrollRect.right = infoPtr->clientWidth;
3490 if (nextItem)
3492 scrollDist = nextItem->rect.top - orgNextTop;
3493 scrollRect.top = orgNextTop;
3495 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3496 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3497 TREEVIEW_Invalidate (infoPtr, item);
3498 } else {
3499 scrollRect.top = item->rect.top;
3500 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3503 /* Scroll up so that as many children as possible are visible.
3504 * This fails when expanding causes an HScroll bar to appear, but we
3505 * don't know that yet, so the last item is obscured. */
3506 if (item->firstChild != NULL)
3508 int nChildren = item->lastChild->visibleOrder
3509 - item->firstChild->visibleOrder + 1;
3511 int visible_pos = item->visibleOrder
3512 - infoPtr->firstVisible->visibleOrder;
3514 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3516 if (visible_pos > 0 && nChildren > rows_below)
3518 int scroll = nChildren - rows_below;
3520 if (scroll > visible_pos)
3521 scroll = visible_pos;
3523 if (scroll > 0)
3525 TREEVIEW_ITEM *newFirstVisible
3526 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3527 scroll);
3530 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3536 if (sendsNotifications) {
3537 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3538 item->state |= TVIS_EXPANDEDONCE;
3541 return TRUE;
3544 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3545 to mouse messages and TVM_SELECTITEM.
3547 selection - previously selected item, used to collapse a part of a tree
3548 item - new selected item
3550 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3551 HTREEITEM selection, HTREEITEM item)
3553 TREEVIEW_ITEM *prev, *curr;
3555 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3557 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3560 * Close the previous item and its ancestors as long as they are not
3561 * ancestors of the current item
3563 for (prev = selection; prev && TREEVIEW_ValidItem(infoPtr, prev); prev = prev->parent)
3565 for (curr = item; curr && TREEVIEW_ValidItem(infoPtr, curr); curr = curr->parent)
3567 if (curr == prev)
3568 goto finish;
3570 TREEVIEW_Collapse(infoPtr, prev, FALSE, TRUE);
3573 finish:
3575 * Expand the current item
3577 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3580 static BOOL
3581 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3583 TRACE("item=%p, user=%d\n", item, user);
3585 if (item->state & TVIS_EXPANDED)
3586 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3587 else
3588 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3591 static VOID
3592 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3594 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3596 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3598 if (TREEVIEW_HasChildren(infoPtr, item))
3599 TREEVIEW_ExpandAll(infoPtr, item);
3603 /* Note:If the specified item is the child of a collapsed parent item,
3604 the parent's list of child items is (recursively) expanded to reveal the
3605 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3606 know if it also applies here.
3609 static LRESULT
3610 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3612 if (!TREEVIEW_ValidItem(infoPtr, item))
3613 return 0;
3615 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3616 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3617 flag, item->state);
3619 switch (flag & TVE_TOGGLE)
3621 case TVE_COLLAPSE:
3622 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3623 FALSE);
3625 case TVE_EXPAND:
3626 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3627 FALSE);
3629 case TVE_TOGGLE:
3630 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3632 default:
3633 return 0;
3637 /* Hit-Testing **********************************************************/
3639 static TREEVIEW_ITEM *
3640 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3642 TREEVIEW_ITEM *item;
3643 LONG row;
3645 if (!infoPtr->firstVisible)
3646 return NULL;
3648 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3650 for (item = infoPtr->firstVisible; item != NULL;
3651 item = TREEVIEW_GetNextListItem(infoPtr, item))
3653 if (row >= item->visibleOrder
3654 && row < item->visibleOrder + item->iIntegral)
3655 break;
3658 return item;
3661 static TREEVIEW_ITEM *
3662 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3664 TREEVIEW_ITEM *item;
3665 RECT rect;
3666 UINT status;
3667 LONG x, y;
3669 lpht->hItem = 0;
3670 GetClientRect(infoPtr->hwnd, &rect);
3671 status = 0;
3672 x = lpht->pt.x;
3673 y = lpht->pt.y;
3675 if (x < rect.left)
3677 status |= TVHT_TOLEFT;
3679 else if (x > rect.right)
3681 status |= TVHT_TORIGHT;
3684 if (y < rect.top)
3686 status |= TVHT_ABOVE;
3688 else if (y > rect.bottom)
3690 status |= TVHT_BELOW;
3693 if (status)
3695 lpht->flags = status;
3696 return NULL;
3699 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3700 if (!item)
3702 lpht->flags = TVHT_NOWHERE;
3703 return NULL;
3706 if (!item->textWidth)
3707 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
3709 if (x >= item->textOffset + item->textWidth)
3711 lpht->flags = TVHT_ONITEMRIGHT;
3713 else if (x >= item->textOffset)
3715 lpht->flags = TVHT_ONITEMLABEL;
3717 else if (x >= item->imageOffset)
3719 lpht->flags = TVHT_ONITEMICON;
3721 else if (x >= item->stateOffset)
3723 lpht->flags = TVHT_ONITEMSTATEICON;
3725 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3727 lpht->flags = TVHT_ONITEMBUTTON;
3729 else
3731 lpht->flags = TVHT_ONITEMINDENT;
3734 lpht->hItem = item;
3735 TRACE("(%d,%d):result 0x%x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3737 return item;
3740 /* Item Label Editing ***************************************************/
3742 static LRESULT
3743 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3745 return (LRESULT)infoPtr->hwndEdit;
3748 static LRESULT CALLBACK
3749 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3751 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3752 BOOL bCancel = FALSE;
3753 LRESULT rc;
3755 switch (uMsg)
3757 case WM_PAINT:
3758 TRACE("WM_PAINT start\n");
3759 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3760 lParam);
3761 TRACE("WM_PAINT done\n");
3762 return rc;
3764 case WM_KILLFOCUS:
3765 if (infoPtr->bIgnoreEditKillFocus)
3766 return TRUE;
3767 break;
3769 case WM_DESTROY:
3771 WNDPROC editProc = infoPtr->wpEditOrig;
3772 infoPtr->wpEditOrig = 0;
3773 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3774 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3777 case WM_GETDLGCODE:
3778 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3780 case WM_KEYDOWN:
3781 if (wParam == VK_ESCAPE)
3783 bCancel = TRUE;
3784 break;
3786 else if (wParam == VK_RETURN)
3788 break;
3791 /* fall through */
3792 default:
3793 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3796 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3797 /* eg. Using a messagebox */
3799 infoPtr->bIgnoreEditKillFocus = TRUE;
3800 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3801 infoPtr->bIgnoreEditKillFocus = FALSE;
3803 return 0;
3807 /* should handle edit control messages here */
3809 static LRESULT
3810 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3812 TRACE("code=0x%x, id=0x%x, handle=0x%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3814 switch (HIWORD(wParam))
3816 case EN_UPDATE:
3819 * Adjust the edit window size
3821 WCHAR buffer[1024];
3822 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3823 HDC hdc = GetDC(infoPtr->hwndEdit);
3824 SIZE sz;
3825 HFONT hFont, hOldFont = 0;
3827 TRACE("edit=%p\n", infoPtr->hwndEdit);
3829 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3831 infoPtr->bLabelChanged = TRUE;
3833 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3835 /* Select font to get the right dimension of the string */
3836 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3838 if (hFont != 0)
3840 hOldFont = SelectObject(hdc, hFont);
3843 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3845 TEXTMETRICW textMetric;
3847 /* Add Extra spacing for the next character */
3848 GetTextMetricsW(hdc, &textMetric);
3849 sz.cx += (textMetric.tmMaxCharWidth * 2);
3851 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3852 sz.cx = min(sz.cx,
3853 infoPtr->clientWidth - editItem->textOffset + 2);
3855 SetWindowPos(infoPtr->hwndEdit,
3856 HWND_TOP,
3859 sz.cx,
3860 editItem->rect.bottom - editItem->rect.top + 3,
3861 SWP_NOMOVE | SWP_DRAWFRAME);
3864 if (hFont != 0)
3866 SelectObject(hdc, hOldFont);
3869 ReleaseDC(infoPtr->hwnd, hdc);
3870 break;
3872 case EN_KILLFOCUS:
3873 /* apparently we should respect passed handle value */
3874 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3876 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3877 break;
3879 default:
3880 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3883 return 0;
3886 static HWND
3887 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3889 HWND hwnd = infoPtr->hwnd;
3890 HWND hwndEdit;
3891 SIZE sz;
3892 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3893 HDC hdc;
3894 HFONT hOldFont=0;
3895 TEXTMETRICW textMetric;
3897 TRACE("%p %p\n", hwnd, hItem);
3898 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3899 return NULL;
3901 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3902 return NULL;
3904 if (infoPtr->hwndEdit)
3905 return infoPtr->hwndEdit;
3907 infoPtr->bLabelChanged = FALSE;
3909 /* make edit item visible */
3910 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3912 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3914 hdc = GetDC(hwnd);
3915 /* Select the font to get appropriate metric dimensions */
3916 if (infoPtr->hFont != 0)
3918 hOldFont = SelectObject(hdc, infoPtr->hFont);
3921 /* Get string length in pixels */
3922 if (hItem->pszText)
3923 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3924 &sz);
3925 else
3926 GetTextExtentPoint32A(hdc, "", 0, &sz);
3928 /* Add Extra spacing for the next character */
3929 GetTextMetricsW(hdc, &textMetric);
3930 sz.cx += (textMetric.tmMaxCharWidth * 2);
3932 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3933 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3935 if (infoPtr->hFont != 0)
3937 SelectObject(hdc, hOldFont);
3940 ReleaseDC(hwnd, hdc);
3942 infoPtr->editItem = hItem;
3944 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3945 WC_EDITW,
3947 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3948 WS_CLIPSIBLINGS | ES_WANTRETURN |
3949 ES_LEFT, hItem->textOffset - 2,
3950 hItem->rect.top - 1, sz.cx + 3,
3951 hItem->rect.bottom -
3952 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3953 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3955 infoPtr->hwndEdit = hwndEdit;
3957 /* Get a 2D border. */
3958 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3959 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3960 SetWindowLongW(hwndEdit, GWL_STYLE,
3961 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3963 SendMessageW(hwndEdit, WM_SETFONT,
3964 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3966 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3967 (DWORD_PTR)
3968 TREEVIEW_Edit_SubclassProc);
3969 if (hItem->pszText)
3970 SetWindowTextW(hwndEdit, hItem->pszText);
3972 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3974 DestroyWindow(hwndEdit);
3975 infoPtr->hwndEdit = 0;
3976 infoPtr->editItem = NULL;
3977 return NULL;
3980 SetFocus(hwndEdit);
3981 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3982 ShowWindow(hwndEdit, SW_SHOW);
3984 return hwndEdit;
3988 static LRESULT
3989 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3991 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3992 NMTVDISPINFOW tvdi;
3993 BOOL bCommit;
3994 WCHAR tmpText[1024] = { '\0' };
3995 WCHAR *newText = tmpText;
3996 int iLength = 0;
3998 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
4000 tvdi.item.mask = 0;
4001 tvdi.item.hItem = editedItem;
4002 tvdi.item.state = editedItem->state;
4003 tvdi.item.lParam = editedItem->lParam;
4005 if (!bCancel)
4007 if (!infoPtr->bNtfUnicode)
4008 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
4009 else
4010 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
4012 if (iLength >= 1023)
4014 ERR("Insufficient space to retrieve new item label\n");
4017 tvdi.item.mask = TVIF_TEXT;
4018 tvdi.item.pszText = tmpText;
4019 tvdi.item.cchTextMax = iLength + 1;
4021 else
4023 tvdi.item.pszText = NULL;
4024 tvdi.item.cchTextMax = 0;
4027 bCommit = TREEVIEW_SendRealNotify(infoPtr, TVN_ENDLABELEDITW, &tvdi.hdr);
4029 if (!bCancel && bCommit) /* Apply the changes */
4031 if (!infoPtr->bNtfUnicode)
4033 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
4034 newText = Alloc(len * sizeof(WCHAR));
4035 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
4036 iLength = len - 1;
4039 if (strcmpW(newText, editedItem->pszText) != 0)
4041 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
4042 if (ptr == NULL)
4044 ERR("OutOfMemory, cannot allocate space for label\n");
4045 if(newText != tmpText) Free(newText);
4046 DestroyWindow(infoPtr->hwndEdit);
4047 infoPtr->hwndEdit = 0;
4048 infoPtr->editItem = NULL;
4049 return FALSE;
4051 else
4053 editedItem->pszText = ptr;
4054 editedItem->cchTextMax = iLength + 1;
4055 strcpyW(editedItem->pszText, newText);
4056 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4059 if(newText != tmpText) Free(newText);
4062 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4063 DestroyWindow(infoPtr->hwndEdit);
4064 infoPtr->hwndEdit = 0;
4065 infoPtr->editItem = NULL;
4066 return TRUE;
4069 static LRESULT
4070 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4072 if (wParam != TV_EDIT_TIMER)
4074 ERR("got unknown timer\n");
4075 return 1;
4078 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4079 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4081 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4083 return 0;
4087 /* Mouse Tracking/Drag **************************************************/
4089 /***************************************************************************
4090 * This is quite unusual piece of code, but that's how it's implemented in
4091 * Windows.
4093 static LRESULT
4094 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4096 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4097 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4098 RECT r;
4099 MSG msg;
4101 r.top = pt.y - cyDrag;
4102 r.left = pt.x - cxDrag;
4103 r.bottom = pt.y + cyDrag;
4104 r.right = pt.x + cxDrag;
4106 SetCapture(infoPtr->hwnd);
4108 while (1)
4110 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4112 if (msg.message == WM_MOUSEMOVE)
4114 pt.x = (short)LOWORD(msg.lParam);
4115 pt.y = (short)HIWORD(msg.lParam);
4116 if (PtInRect(&r, pt))
4117 continue;
4118 else
4120 ReleaseCapture();
4121 return 1;
4124 else if (msg.message >= WM_LBUTTONDOWN &&
4125 msg.message <= WM_RBUTTONDBLCLK)
4127 break;
4130 DispatchMessageW(&msg);
4133 if (GetCapture() != infoPtr->hwnd)
4134 return 0;
4137 ReleaseCapture();
4138 return 0;
4142 static LRESULT
4143 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4145 TREEVIEW_ITEM *item;
4146 TVHITTESTINFO hit;
4148 TRACE("\n");
4149 SetFocus(infoPtr->hwnd);
4151 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4153 /* If there is pending 'edit label' event - kill it now */
4154 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4157 hit.pt.x = (short)LOWORD(lParam);
4158 hit.pt.y = (short)HIWORD(lParam);
4160 item = TREEVIEW_HitTest(infoPtr, &hit);
4161 if (!item)
4162 return 0;
4163 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4165 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4166 { /* FIXME! */
4167 switch (hit.flags)
4169 case TVHT_ONITEMRIGHT:
4170 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4171 break;
4173 case TVHT_ONITEMINDENT:
4174 if (!(infoPtr->dwStyle & TVS_HASLINES))
4176 break;
4178 else
4180 int level = hit.pt.x / infoPtr->uIndent;
4181 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4183 while (item->iLevel > level)
4185 item = item->parent;
4188 /* fall through */
4191 case TVHT_ONITEMLABEL:
4192 case TVHT_ONITEMICON:
4193 case TVHT_ONITEMBUTTON:
4194 TREEVIEW_Toggle(infoPtr, item, TRUE);
4195 break;
4197 case TVHT_ONITEMSTATEICON:
4198 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4199 TREEVIEW_ToggleItemState(infoPtr, item);
4200 else
4201 TREEVIEW_Toggle(infoPtr, item, TRUE);
4202 break;
4205 return TRUE;
4209 static LRESULT
4210 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4212 BOOL do_track, do_select, bDoLabelEdit;
4213 HWND hwnd = infoPtr->hwnd;
4214 TVHITTESTINFO ht;
4216 /* If Edit control is active - kill it and return.
4217 * The best way to do it is to set focus to itself.
4218 * Edit control subclassed procedure will automatically call
4219 * EndEditLabelNow.
4221 if (infoPtr->hwndEdit)
4223 SetFocus(hwnd);
4224 return 0;
4227 ht.pt.x = (short)LOWORD(lParam);
4228 ht.pt.y = (short)HIWORD(lParam);
4230 TREEVIEW_HitTest(infoPtr, &ht);
4231 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4233 /* update focusedItem and redraw both items */
4234 if (ht.hItem)
4236 BOOL do_focus;
4238 if (TREEVIEW_IsFullRowSelect(infoPtr))
4239 do_focus = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEM | TVHT_ONITEMRIGHT);
4240 else
4241 do_focus = ht.flags & TVHT_ONITEM;
4243 if (do_focus)
4245 infoPtr->focusedItem = ht.hItem;
4246 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4247 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4251 if (!(infoPtr->dwStyle & TVS_DISABLEDRAGDROP))
4253 if (TREEVIEW_IsFullRowSelect(infoPtr))
4254 do_track = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEM | TVHT_ONITEMRIGHT);
4255 else
4256 do_track = ht.flags & TVHT_ONITEM;
4258 else
4259 do_track = FALSE;
4262 * If the style allows editing and the node is already selected
4263 * and the click occurred on the item label...
4265 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4266 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4268 /* Send NM_CLICK right away */
4269 if (!do_track && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4270 goto setfocus;
4272 if (ht.flags & TVHT_ONITEMBUTTON)
4274 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4275 goto setfocus;
4277 else if (do_track)
4278 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4279 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4281 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4282 infoPtr->dropItem = ht.hItem;
4284 /* clean up focusedItem as we dragged and won't select this item */
4285 if(infoPtr->focusedItem)
4287 /* refresh the item that was focused */
4288 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4289 infoPtr->focusedItem = NULL;
4291 /* refresh the selected item to return the filled background */
4292 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4295 return 0;
4299 if (do_track && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4300 goto setfocus;
4302 if (TREEVIEW_IsFullRowSelect(infoPtr))
4303 do_select = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEMICON | TVHT_ONITEMLABEL | TVHT_ONITEMRIGHT);
4304 else
4305 do_select = ht.flags & (TVHT_ONITEMICON | TVHT_ONITEMLABEL);
4307 if (bDoLabelEdit)
4309 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4310 KillTimer(hwnd, TV_EDIT_TIMER);
4312 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4313 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4315 else if (do_select)
4317 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4319 /* Select the current item */
4320 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4321 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4323 else if (ht.flags & TVHT_ONITEMSTATEICON)
4325 /* TVS_CHECKBOXES requires us to toggle the current state */
4326 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4327 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4330 setfocus:
4331 SetFocus(hwnd);
4332 return 0;
4336 static LRESULT
4337 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4339 TVHITTESTINFO ht;
4341 if (infoPtr->hwndEdit)
4343 SetFocus(infoPtr->hwnd);
4344 return 0;
4347 ht.pt.x = (short)LOWORD(lParam);
4348 ht.pt.y = (short)HIWORD(lParam);
4350 if (TREEVIEW_HitTest(infoPtr, &ht))
4352 infoPtr->focusedItem = ht.hItem;
4353 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4354 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4357 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4359 if (ht.hItem)
4361 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4362 infoPtr->dropItem = ht.hItem;
4365 else
4367 SetFocus(infoPtr->hwnd);
4368 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4370 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4371 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4372 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4376 if (ht.hItem)
4378 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4379 infoPtr->focusedItem = infoPtr->selectedItem;
4380 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4383 return 0;
4386 static LRESULT
4387 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4389 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4390 INT cx, cy;
4391 HDC hdc, htopdc;
4392 HWND hwtop;
4393 HBITMAP hbmp, hOldbmp;
4394 SIZE size;
4395 RECT rc;
4396 HFONT hOldFont;
4398 TRACE("\n");
4400 if (!(infoPtr->himlNormal))
4401 return 0;
4403 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4404 return 0;
4406 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4408 hwtop = GetDesktopWindow();
4409 htopdc = GetDC(hwtop);
4410 hdc = CreateCompatibleDC(htopdc);
4412 hOldFont = SelectObject(hdc, infoPtr->hFont);
4414 if (dragItem->pszText)
4415 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4416 &size);
4417 else
4418 GetTextExtentPoint32A(hdc, "", 0, &size);
4420 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4421 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4422 hOldbmp = SelectObject(hdc, hbmp);
4424 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4425 size.cx += cx;
4426 if (cy > size.cy)
4427 size.cy = cy;
4429 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4430 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4431 ILD_NORMAL);
4434 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4435 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4438 /* draw item text */
4440 SetRect(&rc, cx, 0, size.cx, size.cy);
4442 if (dragItem->pszText)
4443 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4444 DT_LEFT);
4446 SelectObject(hdc, hOldFont);
4447 SelectObject(hdc, hOldbmp);
4449 ImageList_Add(infoPtr->dragList, hbmp, 0);
4451 DeleteDC(hdc);
4452 DeleteObject(hbmp);
4453 ReleaseDC(hwtop, htopdc);
4455 return (LRESULT)infoPtr->dragList;
4458 /* Selection ************************************************************/
4460 static LRESULT
4461 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4462 INT cause)
4464 TREEVIEW_ITEM *prevSelect;
4466 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4468 TRACE("Entering item %p (%s), flag 0x%x, cause 0x%x, state 0x%x\n",
4469 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4470 newSelect ? newSelect->state : 0);
4472 /* reset and redraw focusedItem if focusedItem was set so we don't */
4473 /* have to worry about the previously focused item when we set a new one */
4474 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4475 infoPtr->focusedItem = NULL;
4477 switch (action)
4479 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4480 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4481 /* Fall through */
4482 case TVGN_CARET:
4483 prevSelect = infoPtr->selectedItem;
4485 if (prevSelect == newSelect) {
4486 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4487 break;
4490 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4491 TVN_SELCHANGINGW,
4492 cause,
4493 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4494 prevSelect,
4495 newSelect))
4496 return FALSE;
4498 if (prevSelect)
4499 prevSelect->state &= ~TVIS_SELECTED;
4500 if (newSelect)
4501 newSelect->state |= TVIS_SELECTED;
4503 infoPtr->selectedItem = newSelect;
4505 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4507 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4508 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4510 TREEVIEW_SendTreeviewNotify(infoPtr,
4511 TVN_SELCHANGEDW,
4512 cause,
4513 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4514 prevSelect,
4515 newSelect);
4516 break;
4518 case TVGN_DROPHILITE:
4519 prevSelect = infoPtr->dropItem;
4521 if (prevSelect)
4522 prevSelect->state &= ~TVIS_DROPHILITED;
4524 infoPtr->dropItem = newSelect;
4526 if (newSelect)
4527 newSelect->state |= TVIS_DROPHILITED;
4529 TREEVIEW_Invalidate(infoPtr, prevSelect);
4530 TREEVIEW_Invalidate(infoPtr, newSelect);
4531 break;
4533 case TVGN_FIRSTVISIBLE:
4534 if (newSelect != NULL)
4536 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4537 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4538 TREEVIEW_Invalidate(infoPtr, NULL);
4540 break;
4543 TRACE("Leaving state 0x%x\n", newSelect ? newSelect->state : 0);
4544 return TRUE;
4547 /* FIXME: handle NM_KILLFOCUS etc */
4548 static LRESULT
4549 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4551 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4553 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4554 return FALSE;
4556 if (item == infoPtr->selectedItem)
4557 return TRUE;
4559 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4561 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4562 return FALSE;
4564 TREEVIEW_SingleExpand(infoPtr, selection, item);
4566 return TRUE;
4569 /*************************************************************************
4570 * TREEVIEW_ProcessLetterKeys
4572 * Processes keyboard messages generated by pressing the letter keys
4573 * on the keyboard.
4574 * What this does is perform a case insensitive search from the
4575 * current position with the following quirks:
4576 * - If two chars or more are pressed in quick succession we search
4577 * for the corresponding string (e.g. 'abc').
4578 * - If there is a delay we wipe away the current search string and
4579 * restart with just that char.
4580 * - If the user keeps pressing the same character, whether slowly or
4581 * fast, so that the search string is entirely composed of this
4582 * character ('aaaaa' for instance), then we search for first item
4583 * that starting with that character.
4584 * - If the user types the above character in quick succession, then
4585 * we must also search for the corresponding string ('aaaaa'), and
4586 * go to that string if there is a match.
4588 * RETURNS
4590 * Zero.
4592 * BUGS
4594 * - The current implementation has a list of characters it will
4595 * accept and it ignores everything else. In particular it will
4596 * ignore accentuated characters which seems to match what
4597 * Windows does. But I'm not sure it makes sense to follow
4598 * Windows there.
4599 * - We don't sound a beep when the search fails.
4600 * - The search should start from the focused item, not from the selected
4601 * item. One reason for this is to allow for multiple selections in trees.
4602 * But currently infoPtr->focusedItem does not seem very usable.
4604 * SEE ALSO
4606 * TREEVIEW_ProcessLetterKeys
4608 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4610 HTREEITEM nItem;
4611 HTREEITEM endidx,idx;
4612 TVITEMEXW item;
4613 WCHAR buffer[MAX_PATH];
4614 DWORD timestamp,elapsed;
4616 /* simple parameter checking */
4617 if (!charCode || !keyData) return 0;
4619 /* only allow the valid WM_CHARs through */
4620 if (!isalnum(charCode) &&
4621 charCode != '.' && charCode != '`' && charCode != '!' &&
4622 charCode != '@' && charCode != '#' && charCode != '$' &&
4623 charCode != '%' && charCode != '^' && charCode != '&' &&
4624 charCode != '*' && charCode != '(' && charCode != ')' &&
4625 charCode != '-' && charCode != '_' && charCode != '+' &&
4626 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4627 charCode != '}' && charCode != '[' && charCode != '{' &&
4628 charCode != '/' && charCode != '?' && charCode != '>' &&
4629 charCode != '<' && charCode != ',' && charCode != '~')
4630 return 0;
4632 /* compute how much time elapsed since last keypress */
4633 timestamp = GetTickCount();
4634 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4635 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4636 } else {
4637 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4640 /* update the search parameters */
4641 infoPtr->lastKeyPressTimestamp=timestamp;
4642 if (elapsed < KEY_DELAY) {
4643 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4644 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4646 if (infoPtr->charCode != charCode) {
4647 infoPtr->charCode=charCode=0;
4649 } else {
4650 infoPtr->charCode=charCode;
4651 infoPtr->szSearchParam[0]=charCode;
4652 infoPtr->nSearchParamLength=1;
4653 /* Redundant with the 1 char string */
4654 charCode=0;
4657 /* and search from the current position */
4658 nItem=NULL;
4659 if (infoPtr->selectedItem != NULL) {
4660 endidx=infoPtr->selectedItem;
4661 /* if looking for single character match,
4662 * then we must always move forward
4664 if (infoPtr->nSearchParamLength == 1)
4665 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4666 else
4667 idx=endidx;
4668 } else {
4669 endidx=NULL;
4670 idx=infoPtr->root->firstChild;
4672 do {
4673 /* At the end point, sort out wrapping */
4674 if (idx == NULL) {
4676 /* If endidx is null, stop at the last item (ie top to bottom) */
4677 if (endidx == NULL)
4678 break;
4680 /* Otherwise, start again at the very beginning */
4681 idx=infoPtr->root->firstChild;
4683 /* But if we are stopping on the first child, end now! */
4684 if (idx == endidx) break;
4687 /* get item */
4688 ZeroMemory(&item, sizeof(item));
4689 item.mask = TVIF_TEXT;
4690 item.hItem = idx;
4691 item.pszText = buffer;
4692 item.cchTextMax = sizeof(buffer);
4693 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4695 /* check for a match */
4696 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4697 nItem=idx;
4698 break;
4699 } else if ( (charCode != 0) && (nItem == NULL) &&
4700 (nItem != infoPtr->selectedItem) &&
4701 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4702 /* This would work but we must keep looking for a longer match */
4703 nItem=idx;
4705 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4706 } while (idx != endidx);
4708 if (nItem != NULL) {
4709 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4710 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4714 return 0;
4717 /* Scrolling ************************************************************/
4719 static LRESULT
4720 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4722 int viscount;
4723 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4724 HTREEITEM newFirstVisible = NULL;
4725 int visible_pos = -1;
4727 if (!TREEVIEW_ValidItem(infoPtr, item))
4728 return FALSE;
4730 if (!ISVISIBLE(item))
4732 /* Expand parents as necessary. */
4733 HTREEITEM parent;
4735 /* see if we are trying to ensure that root is visible */
4736 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4737 parent = item->parent;
4738 else
4739 parent = item; /* this item is the topmost item */
4741 while (parent != infoPtr->root)
4743 if (!(parent->state & TVIS_EXPANDED))
4744 TREEVIEW_Expand(infoPtr, parent, FALSE, TRUE);
4746 parent = parent->parent;
4750 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4752 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4753 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4755 if (hasFirstVisible)
4756 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4758 if (visible_pos < 0)
4760 /* item is before the start of the list: put it at the top. */
4761 newFirstVisible = item;
4763 else if (visible_pos >= viscount
4764 /* Sometimes, before we are displayed, GVC is 0, causing us to
4765 * spuriously scroll up. */
4766 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4768 /* item is past the end of the list. */
4769 int scroll = visible_pos - viscount;
4771 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4772 scroll + 1);
4775 if (bHScroll)
4777 /* Scroll window so item's text is visible as much as possible */
4778 /* Calculation of amount of extra space is taken from EditLabel code */
4779 INT pos, x;
4780 TEXTMETRICW textMetric;
4781 HDC hdc = GetWindowDC(infoPtr->hwnd);
4783 x = item->textWidth;
4785 GetTextMetricsW(hdc, &textMetric);
4786 ReleaseDC(infoPtr->hwnd, hdc);
4788 x += (textMetric.tmMaxCharWidth * 2);
4789 x = max(x, textMetric.tmMaxCharWidth * 3);
4791 if (item->textOffset < 0)
4792 pos = item->textOffset;
4793 else if (item->textOffset + x > infoPtr->clientWidth)
4795 if (x > infoPtr->clientWidth)
4796 pos = item->textOffset;
4797 else
4798 pos = item->textOffset + x - infoPtr->clientWidth;
4800 else
4801 pos = 0;
4803 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4806 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4808 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4810 return TRUE;
4813 return FALSE;
4816 static VOID
4817 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4818 TREEVIEW_ITEM *newFirstVisible,
4819 BOOL bUpdateScrollPos)
4821 int gap_size;
4823 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4825 if (newFirstVisible != NULL)
4827 /* Prevent an empty gap from appearing at the bottom... */
4828 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4829 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4831 if (gap_size > 0)
4833 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4834 -gap_size);
4836 /* ... unless we just don't have enough items. */
4837 if (newFirstVisible == NULL)
4838 newFirstVisible = infoPtr->root->firstChild;
4842 if (infoPtr->firstVisible != newFirstVisible)
4844 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4846 infoPtr->firstVisible = newFirstVisible;
4847 TREEVIEW_Invalidate(infoPtr, NULL);
4849 else
4851 TREEVIEW_ITEM *item;
4852 int scroll = infoPtr->uItemHeight *
4853 (infoPtr->firstVisible->visibleOrder
4854 - newFirstVisible->visibleOrder);
4856 infoPtr->firstVisible = newFirstVisible;
4858 for (item = infoPtr->root->firstChild; item != NULL;
4859 item = TREEVIEW_GetNextListItem(infoPtr, item))
4861 item->rect.top += scroll;
4862 item->rect.bottom += scroll;
4865 if (bUpdateScrollPos)
4866 SetScrollPos(infoPtr->hwnd, SB_VERT,
4867 newFirstVisible->visibleOrder, TRUE);
4869 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4874 /************************************************************************
4875 * VScroll is always in units of visible items. i.e. we always have a
4876 * visible item aligned to the top of the control. (Unless we have no
4877 * items at all.)
4879 static LRESULT
4880 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4882 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4883 TREEVIEW_ITEM *newFirstVisible = NULL;
4885 int nScrollCode = LOWORD(wParam);
4887 TRACE("wp %lx\n", wParam);
4889 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4890 return 0;
4892 if (!oldFirstVisible)
4894 assert(infoPtr->root->firstChild == NULL);
4895 return 0;
4898 switch (nScrollCode)
4900 case SB_TOP:
4901 newFirstVisible = infoPtr->root->firstChild;
4902 break;
4904 case SB_BOTTOM:
4905 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4906 break;
4908 case SB_LINEUP:
4909 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4910 break;
4912 case SB_LINEDOWN:
4913 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4914 break;
4916 case SB_PAGEUP:
4917 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4918 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4919 break;
4921 case SB_PAGEDOWN:
4922 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4923 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4924 break;
4926 case SB_THUMBTRACK:
4927 case SB_THUMBPOSITION:
4928 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4929 infoPtr->root->firstChild,
4930 (LONG)(SHORT)HIWORD(wParam));
4931 break;
4933 case SB_ENDSCROLL:
4934 return 0;
4937 if (newFirstVisible != NULL)
4939 if (newFirstVisible != oldFirstVisible)
4940 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4941 nScrollCode != SB_THUMBTRACK);
4942 else if (nScrollCode == SB_THUMBPOSITION)
4943 SetScrollPos(infoPtr->hwnd, SB_VERT,
4944 newFirstVisible->visibleOrder, TRUE);
4947 return 0;
4950 static LRESULT
4951 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4953 int maxWidth;
4954 int scrollX = infoPtr->scrollX;
4955 int nScrollCode = LOWORD(wParam);
4957 TRACE("wp %lx\n", wParam);
4959 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4960 return FALSE;
4962 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4963 /* shall never occur */
4964 if (maxWidth <= 0)
4966 scrollX = 0;
4967 goto scroll;
4970 switch (nScrollCode)
4972 case SB_LINELEFT:
4973 scrollX -= infoPtr->uItemHeight;
4974 break;
4975 case SB_LINERIGHT:
4976 scrollX += infoPtr->uItemHeight;
4977 break;
4978 case SB_PAGELEFT:
4979 scrollX -= infoPtr->clientWidth;
4980 break;
4981 case SB_PAGERIGHT:
4982 scrollX += infoPtr->clientWidth;
4983 break;
4985 case SB_THUMBTRACK:
4986 case SB_THUMBPOSITION:
4987 scrollX = (int)(SHORT)HIWORD(wParam);
4988 break;
4990 case SB_ENDSCROLL:
4991 return 0;
4994 if (scrollX > maxWidth)
4995 scrollX = maxWidth;
4996 else if (scrollX < 0)
4997 scrollX = 0;
4999 scroll:
5000 if (scrollX != infoPtr->scrollX)
5002 TREEVIEW_ITEM *item;
5003 LONG scroll_pixels = infoPtr->scrollX - scrollX;
5005 for (item = infoPtr->root->firstChild; item != NULL;
5006 item = TREEVIEW_GetNextListItem(infoPtr, item))
5008 item->linesOffset += scroll_pixels;
5009 item->stateOffset += scroll_pixels;
5010 item->imageOffset += scroll_pixels;
5011 item->textOffset += scroll_pixels;
5014 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
5015 infoPtr->scrollX = scrollX;
5016 UpdateWindow(infoPtr->hwnd);
5019 if (nScrollCode != SB_THUMBTRACK)
5020 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
5022 return 0;
5025 static LRESULT
5026 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5028 short wheelDelta;
5029 UINT pulScrollLines = 3;
5031 if (wParam & (MK_SHIFT | MK_CONTROL))
5032 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
5034 if (infoPtr->firstVisible == NULL)
5035 return TRUE;
5037 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
5039 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5040 /* if scrolling changes direction, ignore left overs */
5041 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
5042 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
5043 infoPtr->wheelRemainder += wheelDelta;
5044 else
5045 infoPtr->wheelRemainder = wheelDelta;
5047 if (infoPtr->wheelRemainder && pulScrollLines)
5049 int newDy;
5050 int maxDy;
5051 int lineScroll;
5053 lineScroll = pulScrollLines * (float)infoPtr->wheelRemainder / WHEEL_DELTA;
5054 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / (int)pulScrollLines;
5056 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
5057 maxDy = infoPtr->maxVisibleOrder;
5059 if (newDy > maxDy)
5060 newDy = maxDy;
5062 if (newDy < 0)
5063 newDy = 0;
5065 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
5067 return TRUE;
5070 /* Create/Destroy *******************************************************/
5072 static LRESULT
5073 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5075 RECT rcClient;
5076 TREEVIEW_INFO *infoPtr;
5077 LOGFONTW lf;
5079 TRACE("wnd %p, style 0x%x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5081 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5083 if (infoPtr == NULL)
5085 ERR("could not allocate info memory!\n");
5086 return 0;
5089 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5091 infoPtr->hwnd = hwnd;
5092 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5093 infoPtr->Timer = 0;
5094 infoPtr->uNumItems = 0;
5095 infoPtr->cdmode = 0;
5096 infoPtr->uScrollTime = 300; /* milliseconds */
5097 infoPtr->bRedraw = TRUE;
5099 GetClientRect(hwnd, &rcClient);
5101 /* No scroll bars yet. */
5102 infoPtr->clientWidth = rcClient.right;
5103 infoPtr->clientHeight = rcClient.bottom;
5104 infoPtr->uInternalStatus = 0;
5106 infoPtr->treeWidth = 0;
5107 infoPtr->treeHeight = 0;
5109 infoPtr->uIndent = MINIMUM_INDENT;
5110 infoPtr->selectedItem = NULL;
5111 infoPtr->focusedItem = NULL;
5112 infoPtr->hotItem = NULL;
5113 infoPtr->editItem = NULL;
5114 infoPtr->firstVisible = NULL;
5115 infoPtr->maxVisibleOrder = 0;
5116 infoPtr->dropItem = NULL;
5117 infoPtr->insertMarkItem = NULL;
5118 infoPtr->insertBeforeorAfter = 0;
5119 /* dragList */
5121 infoPtr->scrollX = 0;
5122 infoPtr->wheelRemainder = 0;
5124 infoPtr->clrBk = CLR_NONE; /* use system color */
5125 infoPtr->clrText = CLR_NONE; /* use system color */
5126 infoPtr->clrLine = CLR_DEFAULT;
5127 infoPtr->clrInsertMark = CLR_DEFAULT;
5129 /* hwndToolTip */
5131 infoPtr->hwndEdit = NULL;
5132 infoPtr->wpEditOrig = NULL;
5133 infoPtr->bIgnoreEditKillFocus = FALSE;
5134 infoPtr->bLabelChanged = FALSE;
5136 infoPtr->himlNormal = NULL;
5137 infoPtr->himlState = NULL;
5138 infoPtr->normalImageWidth = 0;
5139 infoPtr->normalImageHeight = 0;
5140 infoPtr->stateImageWidth = 0;
5141 infoPtr->stateImageHeight = 0;
5143 infoPtr->items = DPA_Create(16);
5145 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5146 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5147 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5148 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5149 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5150 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5152 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5154 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5155 infoPtr->root->state = TVIS_EXPANDED;
5156 infoPtr->root->iLevel = -1;
5157 infoPtr->root->visibleOrder = -1;
5159 infoPtr->hwndNotify = lpcs->hwndParent;
5160 infoPtr->hwndToolTip = 0;
5162 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5163 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5165 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5166 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5167 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5168 hwnd, 0, 0, 0);
5170 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5171 ShowScrollBar(hwnd, SB_VERT, FALSE);
5172 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5174 OpenThemeData (hwnd, themeClass);
5176 return 0;
5180 static LRESULT
5181 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5183 TRACE("\n");
5185 /* free item data */
5186 TREEVIEW_RemoveTree(infoPtr);
5187 /* root isn't freed with other items */
5188 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5189 DPA_Destroy(infoPtr->items);
5191 /* tool tip is automatically destroyed: we are its owner */
5193 /* Restore original wndproc */
5194 if (infoPtr->hwndEdit)
5195 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5196 (DWORD_PTR)infoPtr->wpEditOrig);
5198 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5200 /* Deassociate treeview from the window before doing anything drastic. */
5201 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5203 DeleteObject(infoPtr->hDefaultFont);
5204 DeleteObject(infoPtr->hBoldFont);
5205 DeleteObject(infoPtr->hUnderlineFont);
5206 DeleteObject(infoPtr->hBoldUnderlineFont);
5207 Free(infoPtr);
5209 return 0;
5212 /* Miscellaneous Messages ***********************************************/
5214 static LRESULT
5215 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5217 static const struct
5219 unsigned char code;
5221 scroll[] =
5223 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5224 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5225 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5226 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5227 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5228 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5229 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5230 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5231 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5232 #undef SCROLL_ENTRY
5235 if (key >= VK_PRIOR && key <= VK_DOWN)
5237 unsigned char code = scroll[key - VK_PRIOR].code;
5239 (((code & (1 << 7)) == (SB_HORZ << 7))
5240 ? TREEVIEW_HScroll
5241 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5244 return 0;
5247 /************************************************************************
5248 * TREEVIEW_KeyDown
5250 * VK_UP Move selection to the previous non-hidden item.
5251 * VK_DOWN Move selection to the next non-hidden item.
5252 * VK_HOME Move selection to the first item.
5253 * VK_END Move selection to the last item.
5254 * VK_LEFT If expanded then collapse, otherwise move to parent.
5255 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5256 * VK_ADD Expand.
5257 * VK_SUBTRACT Collapse.
5258 * VK_MULTIPLY Expand all.
5259 * VK_PRIOR Move up GetVisibleCount items.
5260 * VK_NEXT Move down GetVisibleCount items.
5261 * VK_BACK Move to parent.
5262 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5264 static LRESULT
5265 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5267 /* If it is non-NULL and different, it will be selected and visible. */
5268 TREEVIEW_ITEM *newSelection = NULL;
5269 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5270 NMTVKEYDOWN nmkeydown;
5272 TRACE("%lx\n", wParam);
5274 nmkeydown.wVKey = wParam;
5275 nmkeydown.flags = 0;
5276 TREEVIEW_SendRealNotify(infoPtr, TVN_KEYDOWN, &nmkeydown.hdr);
5278 if (prevItem == NULL)
5279 return FALSE;
5281 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5282 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5284 switch (wParam)
5286 case VK_UP:
5287 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5288 if (!newSelection)
5289 newSelection = infoPtr->root->firstChild;
5290 break;
5292 case VK_DOWN:
5293 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5294 break;
5296 case VK_RETURN:
5297 TREEVIEW_SendSimpleNotify(infoPtr, NM_RETURN);
5298 break;
5300 case VK_HOME:
5301 newSelection = infoPtr->root->firstChild;
5302 break;
5304 case VK_END:
5305 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5306 break;
5308 case VK_LEFT:
5309 if (prevItem->state & TVIS_EXPANDED)
5311 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5313 else if (prevItem->parent != infoPtr->root)
5315 newSelection = prevItem->parent;
5317 break;
5319 case VK_RIGHT:
5320 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5322 if (!(prevItem->state & TVIS_EXPANDED))
5323 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5324 else
5326 newSelection = prevItem->firstChild;
5330 break;
5332 case VK_MULTIPLY:
5333 TREEVIEW_ExpandAll(infoPtr, prevItem);
5334 break;
5336 case VK_ADD:
5337 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5338 break;
5340 case VK_SUBTRACT:
5341 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5342 break;
5344 case VK_PRIOR:
5345 newSelection
5346 = TREEVIEW_GetListItem(infoPtr, prevItem,
5347 -TREEVIEW_GetVisibleCount(infoPtr));
5348 break;
5350 case VK_NEXT:
5351 newSelection
5352 = TREEVIEW_GetListItem(infoPtr, prevItem,
5353 TREEVIEW_GetVisibleCount(infoPtr));
5354 break;
5356 case VK_BACK:
5357 newSelection = prevItem->parent;
5358 if (newSelection == infoPtr->root)
5359 newSelection = NULL;
5360 break;
5362 case VK_SPACE:
5363 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5364 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5365 break;
5368 if (newSelection && newSelection != prevItem)
5370 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5371 TVC_BYKEYBOARD))
5373 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5377 return FALSE;
5380 static LRESULT
5381 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5383 /* remove hot effect from item */
5384 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5385 infoPtr->hotItem = NULL;
5387 return 0;
5390 static LRESULT
5391 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5393 TRACKMOUSEEVENT trackinfo;
5394 TREEVIEW_ITEM * item;
5395 TVHITTESTINFO ht;
5396 BOOL item_hit;
5398 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5400 /* fill in the TRACKMOUSEEVENT struct */
5401 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5402 trackinfo.dwFlags = TME_QUERY;
5403 trackinfo.hwndTrack = infoPtr->hwnd;
5405 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5406 _TrackMouseEvent(&trackinfo);
5408 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5409 if(!(trackinfo.dwFlags & TME_LEAVE))
5411 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5412 trackinfo.hwndTrack = infoPtr->hwnd;
5413 /* do it as fast as possible, minimal systimer latency will be used */
5414 trackinfo.dwHoverTime = 1;
5416 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5417 /* and can properly deactivate the hot item */
5418 _TrackMouseEvent(&trackinfo);
5421 ht.pt.x = (short)LOWORD(lParam);
5422 ht.pt.y = (short)HIWORD(lParam);
5424 item = TREEVIEW_HitTest(infoPtr, &ht);
5425 item_hit = TREEVIEW_IsItemHit(infoPtr, &ht);
5426 if ((item != infoPtr->hotItem) || !item_hit)
5428 /* redraw old hot item */
5429 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5430 infoPtr->hotItem = NULL;
5431 if (item && item_hit)
5433 infoPtr->hotItem = item;
5434 /* redraw new hot item */
5435 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5439 return 0;
5442 /* Draw themed border */
5443 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5445 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5446 HDC dc;
5447 RECT r;
5448 HRGN cliprgn;
5449 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5450 cyEdge = GetSystemMetrics (SM_CYEDGE);
5452 if (!theme)
5453 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5455 GetWindowRect(infoPtr->hwnd, &r);
5457 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5458 r.right - cxEdge, r.bottom - cyEdge);
5459 if (region != (HRGN)1)
5460 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5461 OffsetRect(&r, -r.left, -r.top);
5463 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5464 OffsetRect(&r, -r.left, -r.top);
5466 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5467 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5468 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5469 ReleaseDC(infoPtr->hwnd, dc);
5471 /* Call default proc to get the scrollbars etc. painted */
5472 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5474 return TRUE;
5477 static LRESULT
5478 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5480 LPNMHDR lpnmh = (LPNMHDR)lParam;
5482 if (lpnmh->code == PGN_CALCSIZE) {
5483 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5485 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5486 lppgc->iWidth = infoPtr->treeWidth;
5487 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5488 infoPtr->treeWidth, infoPtr->clientWidth);
5490 else {
5491 lppgc->iHeight = infoPtr->treeHeight;
5492 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5493 infoPtr->treeHeight, infoPtr->clientHeight);
5495 return 0;
5497 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5500 static LRESULT
5501 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5503 if (wParam == SIZE_RESTORED)
5505 infoPtr->clientWidth = (short)LOWORD(lParam);
5506 infoPtr->clientHeight = (short)HIWORD(lParam);
5508 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5509 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5510 TREEVIEW_UpdateScrollBars(infoPtr);
5512 else
5514 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5517 TREEVIEW_Invalidate(infoPtr, NULL);
5518 return 0;
5521 static LRESULT
5522 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5524 TRACE("(%lx %lx)\n", wParam, lParam);
5526 if (wParam == GWL_STYLE)
5528 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5530 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5532 if (dwNewStyle & TVS_CHECKBOXES)
5534 TREEVIEW_InitCheckboxes(infoPtr);
5535 TRACE("checkboxes enabled\n");
5537 /* set all items to state image index 1 */
5538 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5540 else
5542 FIXME("tried to disable checkboxes\n");
5546 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5548 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5550 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5551 TRACE("tooltips enabled\n");
5553 else
5555 DestroyWindow(infoPtr->hwndToolTip);
5556 infoPtr->hwndToolTip = 0;
5557 TRACE("tooltips disabled\n");
5561 infoPtr->dwStyle = dwNewStyle;
5564 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
5565 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5566 TREEVIEW_UpdateScrollBars(infoPtr);
5567 TREEVIEW_Invalidate(infoPtr, NULL);
5569 return 0;
5572 static LRESULT
5573 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5575 TREEVIEW_ITEM * item;
5576 TVHITTESTINFO ht;
5577 NMMOUSE nmmouse;
5579 GetCursorPos(&ht.pt);
5580 ScreenToClient(infoPtr->hwnd, &ht.pt);
5582 item = TREEVIEW_HitTest(infoPtr, &ht);
5584 memset(&nmmouse, 0, sizeof(nmmouse));
5585 if (item)
5587 nmmouse.dwItemSpec = (DWORD_PTR)item;
5588 nmmouse.dwItemData = item->lParam;
5590 nmmouse.pt.x = 0;
5591 nmmouse.pt.y = 0;
5592 nmmouse.dwHitInfo = lParam;
5593 if (TREEVIEW_SendRealNotify(infoPtr, NM_SETCURSOR, &nmmouse.hdr))
5594 return 0;
5596 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT) && TREEVIEW_IsItemHit(infoPtr, &ht))
5598 SetCursor(infoPtr->hcurHand);
5599 return 0;
5601 else
5602 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5605 static LRESULT
5606 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5608 TRACE("\n");
5610 if (!infoPtr->selectedItem)
5612 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5613 TVC_UNKNOWN);
5616 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5617 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5618 return 0;
5621 static LRESULT
5622 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5624 TRACE("\n");
5626 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5627 UpdateWindow(infoPtr->hwnd);
5628 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5629 return 0;
5632 /* update theme after a WM_THEMECHANGED message */
5633 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5635 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5636 CloseThemeData (theme);
5637 OpenThemeData (infoPtr->hwnd, themeClass);
5638 return 0;
5642 static LRESULT WINAPI
5643 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5645 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5647 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5649 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5650 else
5652 if (uMsg == WM_CREATE)
5653 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5654 else
5655 goto def;
5658 switch (uMsg)
5660 case TVM_CREATEDRAGIMAGE:
5661 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5663 case TVM_DELETEITEM:
5664 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5666 case TVM_EDITLABELA:
5667 case TVM_EDITLABELW:
5668 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5670 case TVM_ENDEDITLABELNOW:
5671 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5673 case TVM_ENSUREVISIBLE:
5674 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5676 case TVM_EXPAND:
5677 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5679 case TVM_GETBKCOLOR:
5680 return TREEVIEW_GetBkColor(infoPtr);
5682 case TVM_GETCOUNT:
5683 return TREEVIEW_GetCount(infoPtr);
5685 case TVM_GETEDITCONTROL:
5686 return TREEVIEW_GetEditControl(infoPtr);
5688 case TVM_GETIMAGELIST:
5689 return TREEVIEW_GetImageList(infoPtr, wParam);
5691 case TVM_GETINDENT:
5692 return TREEVIEW_GetIndent(infoPtr);
5694 case TVM_GETINSERTMARKCOLOR:
5695 return TREEVIEW_GetInsertMarkColor(infoPtr);
5697 case TVM_GETISEARCHSTRINGA:
5698 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5699 return 0;
5701 case TVM_GETISEARCHSTRINGW:
5702 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5703 return 0;
5705 case TVM_GETITEMA:
5706 case TVM_GETITEMW:
5707 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5708 uMsg == TVM_GETITEMW);
5709 case TVM_GETITEMHEIGHT:
5710 return TREEVIEW_GetItemHeight(infoPtr);
5712 case TVM_GETITEMRECT:
5713 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5715 case TVM_GETITEMSTATE:
5716 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5718 case TVM_GETLINECOLOR:
5719 return TREEVIEW_GetLineColor(infoPtr);
5721 case TVM_GETNEXTITEM:
5722 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5724 case TVM_GETSCROLLTIME:
5725 return TREEVIEW_GetScrollTime(infoPtr);
5727 case TVM_GETTEXTCOLOR:
5728 return TREEVIEW_GetTextColor(infoPtr);
5730 case TVM_GETTOOLTIPS:
5731 return TREEVIEW_GetToolTips(infoPtr);
5733 case TVM_GETUNICODEFORMAT:
5734 return TREEVIEW_GetUnicodeFormat(infoPtr);
5736 case TVM_GETVISIBLECOUNT:
5737 return TREEVIEW_GetVisibleCount(infoPtr);
5739 case TVM_HITTEST:
5740 return (LRESULT)TREEVIEW_HitTest(infoPtr, (TVHITTESTINFO*)lParam);
5742 case TVM_INSERTITEMA:
5743 case TVM_INSERTITEMW:
5744 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5745 uMsg == TVM_INSERTITEMW);
5746 case TVM_SELECTITEM:
5747 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5749 case TVM_SETBKCOLOR:
5750 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5752 case TVM_SETIMAGELIST:
5753 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5755 case TVM_SETINDENT:
5756 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5758 case TVM_SETINSERTMARK:
5759 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5761 case TVM_SETINSERTMARKCOLOR:
5762 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5764 case TVM_SETITEMA:
5765 case TVM_SETITEMW:
5766 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5767 uMsg == TVM_SETITEMW);
5768 case TVM_SETLINECOLOR:
5769 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5771 case TVM_SETITEMHEIGHT:
5772 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5774 case TVM_SETSCROLLTIME:
5775 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5777 case TVM_SETTEXTCOLOR:
5778 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5780 case TVM_SETTOOLTIPS:
5781 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5783 case TVM_SETUNICODEFORMAT:
5784 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5786 case TVM_SORTCHILDREN:
5787 return TREEVIEW_SortChildren(infoPtr, lParam);
5789 case TVM_SORTCHILDRENCB:
5790 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5792 case WM_CHAR:
5793 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5795 case WM_COMMAND:
5796 return TREEVIEW_Command(infoPtr, wParam, lParam);
5798 case WM_DESTROY:
5799 return TREEVIEW_Destroy(infoPtr);
5801 /* WM_ENABLE */
5803 case WM_ERASEBKGND:
5804 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5806 case WM_GETDLGCODE:
5807 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5809 case WM_GETFONT:
5810 return TREEVIEW_GetFont(infoPtr);
5812 case WM_HSCROLL:
5813 return TREEVIEW_HScroll(infoPtr, wParam);
5815 case WM_KEYDOWN:
5816 case WM_SYSKEYDOWN:
5817 return TREEVIEW_KeyDown(infoPtr, wParam);
5819 case WM_KILLFOCUS:
5820 return TREEVIEW_KillFocus(infoPtr);
5822 case WM_LBUTTONDBLCLK:
5823 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5825 case WM_LBUTTONDOWN:
5826 return TREEVIEW_LButtonDown(infoPtr, lParam);
5828 /* WM_MBUTTONDOWN */
5830 case WM_MOUSELEAVE:
5831 return TREEVIEW_MouseLeave(infoPtr);
5833 case WM_MOUSEMOVE:
5834 return TREEVIEW_MouseMove(infoPtr, lParam);
5836 case WM_NCLBUTTONDOWN:
5837 if (infoPtr->hwndEdit)
5838 SetFocus(infoPtr->hwnd);
5839 goto def;
5841 case WM_NCPAINT:
5842 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5844 case WM_NOTIFY:
5845 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5847 case WM_NOTIFYFORMAT:
5848 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5850 case WM_PRINTCLIENT:
5851 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5853 case WM_PAINT:
5854 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5856 case WM_RBUTTONDOWN:
5857 return TREEVIEW_RButtonDown(infoPtr, lParam);
5859 case WM_SETCURSOR:
5860 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5862 case WM_SETFOCUS:
5863 return TREEVIEW_SetFocus(infoPtr);
5865 case WM_SETFONT:
5866 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5868 case WM_SETREDRAW:
5869 return TREEVIEW_SetRedraw(infoPtr, wParam);
5871 case WM_SIZE:
5872 return TREEVIEW_Size(infoPtr, wParam, lParam);
5874 case WM_STYLECHANGED:
5875 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5877 case WM_SYSCOLORCHANGE:
5878 COMCTL32_RefreshSysColors();
5879 return 0;
5881 case WM_TIMER:
5882 return TREEVIEW_HandleTimer(infoPtr, wParam);
5884 case WM_THEMECHANGED:
5885 return TREEVIEW_ThemeChanged (infoPtr);
5887 case WM_VSCROLL:
5888 return TREEVIEW_VScroll(infoPtr, wParam);
5890 /* WM_WININICHANGE */
5892 case WM_MOUSEWHEEL:
5893 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5895 case WM_DRAWITEM:
5896 TRACE("drawItem\n");
5897 goto def;
5899 default:
5900 /* This mostly catches MFC and Delphi messages. :( */
5901 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5902 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5903 def:
5904 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5909 /* Class Registration ***************************************************/
5911 VOID
5912 TREEVIEW_Register(void)
5914 WNDCLASSW wndClass;
5916 TRACE("\n");
5918 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5919 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5920 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5921 wndClass.cbClsExtra = 0;
5922 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5924 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5925 wndClass.hbrBackground = 0;
5926 wndClass.lpszClassName = WC_TREEVIEWW;
5928 RegisterClassW(&wndClass);
5932 VOID
5933 TREEVIEW_Unregister(void)
5935 UnregisterClassW(WC_TREEVIEWW, NULL);
5939 /* Tree Verification ****************************************************/
5941 static inline void
5942 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5944 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5945 const TREEVIEW_ITEM *item)
5947 assert(infoPtr != NULL);
5948 assert(item != NULL);
5950 /* both NULL, or both non-null */
5951 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5953 assert(item->firstChild != item);
5954 assert(item->lastChild != item);
5956 if (item->firstChild)
5958 assert(item->firstChild->parent == item);
5959 assert(item->firstChild->prevSibling == NULL);
5962 if (item->lastChild)
5964 assert(item->lastChild->parent == item);
5965 assert(item->lastChild->nextSibling == NULL);
5968 assert(item->nextSibling != item);
5969 if (item->nextSibling)
5971 assert(item->nextSibling->parent == item->parent);
5972 assert(item->nextSibling->prevSibling == item);
5975 assert(item->prevSibling != item);
5976 if (item->prevSibling)
5978 assert(item->prevSibling->parent == item->parent);
5979 assert(item->prevSibling->nextSibling == item);
5983 static inline void
5984 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5986 assert(item != NULL);
5988 assert(item->parent != NULL);
5989 assert(item->parent != item);
5990 assert(item->iLevel == item->parent->iLevel + 1);
5992 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5994 TREEVIEW_VerifyItemCommon(infoPtr, item);
5996 TREEVIEW_VerifyChildren(infoPtr, item);
5999 static inline void
6000 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
6002 const TREEVIEW_ITEM *child;
6003 assert(item != NULL);
6005 for (child = item->firstChild; child != NULL; child = child->nextSibling)
6006 TREEVIEW_VerifyItem(infoPtr, child);
6009 static inline void
6010 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
6012 TREEVIEW_ITEM *root = infoPtr->root;
6014 assert(root != NULL);
6015 assert(root->iLevel == -1);
6016 assert(root->parent == NULL);
6017 assert(root->prevSibling == NULL);
6019 TREEVIEW_VerifyItemCommon(infoPtr, root);
6021 TREEVIEW_VerifyChildren(infoPtr, root);
6024 static void
6025 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
6027 if (!TRACE_ON(treeview)) return;
6029 assert(infoPtr != NULL);
6030 TREEVIEW_VerifyRoot(infoPtr);