wordpad: Add a dropdown menu to the bullet button.
[wine.git] / dlls / comctl32 / treeview.c
blob117b30942732a95614faa86b44b33b979a7245ed
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));
2556 /* The custom draw handler can query the text rectangle,
2557 * so get ready. */
2558 /* should already be known, set to 0 when changed */
2559 if (!item->textWidth)
2560 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2562 cditem = 0;
2564 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2566 cditem = TREEVIEW_SendCustomDrawItemNotify
2567 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2568 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2570 if (cditem & CDRF_SKIPDEFAULT)
2572 SelectObject(hdc, hOldFont);
2573 return;
2577 if (cditem & CDRF_NEWFONT)
2578 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2580 if (TREEVIEW_IsFullRowSelect(infoPtr))
2582 HBRUSH brush = CreateSolidBrush(nmcdhdr.clrTextBk);
2583 FillRect(hdc, &item->rect, brush);
2584 DeleteObject(brush);
2587 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2589 /* Set colors. Custom draw handler can change these so we do this after it. */
2590 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2591 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2593 centery = (item->rect.top + item->rect.bottom) / 2;
2596 * Display the images associated with this item
2599 INT imageIndex;
2601 /* State images are displayed to the left of the Normal image
2602 * image number is in state; zero should be `display no image'.
2604 imageIndex = STATEIMAGEINDEX(item->state);
2606 if (infoPtr->himlState && imageIndex)
2608 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2609 item->stateOffset,
2610 centery - infoPtr->stateImageHeight / 2,
2611 ILD_NORMAL);
2614 /* Now, draw the normal image; can be either selected,
2615 * non-selected or expanded image.
2618 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2620 /* The item is currently selected */
2621 imageIndex = item->iSelectedImage;
2623 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2625 /* The item is currently not selected but expanded */
2626 imageIndex = item->iExpandedImage;
2628 else
2630 /* The item is not selected and not expanded */
2631 imageIndex = item->iImage;
2634 if (infoPtr->himlNormal)
2636 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2638 style |= item->state & TVIS_OVERLAYMASK;
2640 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2641 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2642 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2643 style);
2649 * Display the text associated with this item
2652 /* Don't paint item's text if it's being edited */
2653 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2655 if (item->pszText)
2657 RECT rcText;
2658 UINT align;
2659 SIZE sz;
2661 rcText.top = item->rect.top;
2662 rcText.bottom = item->rect.bottom;
2663 rcText.left = item->textOffset;
2664 rcText.right = rcText.left + item->textWidth + 4;
2666 TRACE("drawing text %s at (%s)\n",
2667 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2669 /* Draw it */
2670 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2672 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2673 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2674 ETO_CLIPPED | ETO_OPAQUE,
2675 &rcText,
2676 item->pszText,
2677 lstrlenW(item->pszText),
2678 NULL);
2679 SetTextAlign(hdc, align);
2681 /* Draw focus box around the selected item */
2682 if ((item == infoPtr->selectedItem) && inFocus)
2684 DrawFocusRect(hdc,&rcText);
2689 /* Draw insertion mark if necessary */
2691 if (infoPtr->insertMarkItem)
2692 TRACE("item:%d,mark:%p\n",
2693 TREEVIEW_GetItemIndex(infoPtr, item),
2694 infoPtr->insertMarkItem);
2696 if (item == infoPtr->insertMarkItem)
2698 HPEN hNewPen, hOldPen;
2699 int offset;
2700 int left, right;
2702 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2703 hOldPen = SelectObject(hdc, hNewPen);
2705 if (infoPtr->insertBeforeorAfter)
2706 offset = item->rect.bottom - 1;
2707 else
2708 offset = item->rect.top + 1;
2710 left = item->textOffset - 2;
2711 right = item->textOffset + item->textWidth + 2;
2713 MoveToEx(hdc, left, offset - 3, NULL);
2714 LineTo(hdc, left, offset + 4);
2716 MoveToEx(hdc, left, offset, NULL);
2717 LineTo(hdc, right + 1, offset);
2719 MoveToEx(hdc, right, offset + 3, NULL);
2720 LineTo(hdc, right, offset - 4);
2722 SelectObject(hdc, hOldPen);
2723 DeleteObject(hNewPen);
2726 /* Restore the hdc state */
2727 SetTextColor(hdc, oldTextColor);
2728 SetBkColor(hdc, oldTextBkColor);
2729 SelectObject(hdc, hOldFont);
2731 if (cditem & CDRF_NOTIFYPOSTPAINT)
2733 cditem = TREEVIEW_SendCustomDrawItemNotify
2734 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2735 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2739 /* Computes treeHeight and treeWidth and updates the scroll bars.
2741 static void
2742 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2744 TREEVIEW_ITEM *item;
2745 HWND hwnd = infoPtr->hwnd;
2746 BOOL vert = FALSE;
2747 BOOL horz = FALSE;
2748 SCROLLINFO si;
2749 LONG scrollX = infoPtr->scrollX;
2751 infoPtr->treeWidth = 0;
2752 infoPtr->treeHeight = 0;
2754 /* We iterate through all visible items in order to get the tree height
2755 * and width */
2756 item = infoPtr->root->firstChild;
2758 while (item != NULL)
2760 if (ISVISIBLE(item))
2762 /* actually we draw text at textOffset + 2 */
2763 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2764 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2766 /* This is scroll-adjusted, but we fix this below. */
2767 infoPtr->treeHeight = item->rect.bottom;
2770 item = TREEVIEW_GetNextListItem(infoPtr, item);
2773 /* Fix the scroll adjusted treeHeight and treeWidth. */
2774 if (infoPtr->root->firstChild)
2775 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2777 infoPtr->treeWidth += infoPtr->scrollX;
2779 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2781 /* Adding one scroll bar may take up enough space that it forces us
2782 * to add the other as well. */
2783 if (infoPtr->treeHeight > infoPtr->clientHeight)
2785 vert = TRUE;
2787 if (infoPtr->treeWidth
2788 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2789 horz = TRUE;
2791 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2792 horz = TRUE;
2794 if (!vert && horz && infoPtr->treeHeight
2795 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2796 vert = TRUE;
2798 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2800 si.cbSize = sizeof(SCROLLINFO);
2801 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2802 si.nMin = 0;
2804 if (vert)
2806 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2807 if ( si.nPage && NULL != infoPtr->firstVisible)
2809 si.nPos = infoPtr->firstVisible->visibleOrder;
2810 si.nMax = infoPtr->maxVisibleOrder - 1;
2812 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2814 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2815 ShowScrollBar(hwnd, SB_VERT, TRUE);
2816 infoPtr->uInternalStatus |= TV_VSCROLL;
2818 else
2820 if (infoPtr->uInternalStatus & TV_VSCROLL)
2821 ShowScrollBar(hwnd, SB_VERT, FALSE);
2822 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2825 else
2827 if (infoPtr->uInternalStatus & TV_VSCROLL)
2828 ShowScrollBar(hwnd, SB_VERT, FALSE);
2829 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2832 if (horz)
2834 si.nPage = infoPtr->clientWidth;
2835 si.nPos = infoPtr->scrollX;
2836 si.nMax = infoPtr->treeWidth - 1;
2838 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2840 si.nPos = si.nMax - max( si.nPage-1, 0 );
2841 scrollX = si.nPos;
2844 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2845 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2846 infoPtr->uInternalStatus |= TV_HSCROLL;
2848 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2849 TREEVIEW_HScroll(infoPtr,
2850 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2852 else
2854 if (infoPtr->uInternalStatus & TV_HSCROLL)
2855 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2856 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2858 scrollX = 0;
2859 if (infoPtr->scrollX != 0)
2861 TREEVIEW_HScroll(infoPtr,
2862 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2866 if (!horz)
2867 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2870 static void
2871 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2873 HBRUSH hBrush;
2874 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2876 hBrush = CreateSolidBrush(clrBk);
2877 FillRect(hdc, rc, hBrush);
2878 DeleteObject(hBrush);
2881 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2882 static LRESULT
2883 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2885 RECT rect;
2887 TRACE("%p\n", infoPtr);
2889 GetClientRect(infoPtr->hwnd, &rect);
2890 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2892 return 1;
2895 static void
2896 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2898 HWND hwnd = infoPtr->hwnd;
2899 RECT rect = *rc;
2900 TREEVIEW_ITEM *item;
2902 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2904 TRACE("empty window\n");
2905 return;
2908 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2909 hdc, rect);
2911 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2913 ReleaseDC(hwnd, hdc);
2914 return;
2917 for (item = infoPtr->root->firstChild;
2918 item != NULL;
2919 item = TREEVIEW_GetNextListItem(infoPtr, item))
2921 if (ISVISIBLE(item))
2923 /* Avoid unneeded calculations */
2924 if (item->rect.top > rect.bottom)
2925 break;
2926 if (item->rect.bottom < rect.top)
2927 continue;
2929 TREEVIEW_DrawItem(infoPtr, hdc, item);
2933 TREEVIEW_UpdateScrollBars(infoPtr);
2935 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2936 infoPtr->cdmode =
2937 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2940 static inline void
2941 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2943 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2946 static void
2947 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2949 if (item)
2950 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2951 else
2952 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2955 static void
2956 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2958 RECT rc;
2959 HBITMAP hbm, hbmOld;
2960 HDC hdc, hdcScreen;
2961 int nIndex;
2963 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2965 hdcScreen = GetDC(0);
2967 hdc = CreateCompatibleDC(hdcScreen);
2968 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2969 hbmOld = SelectObject(hdc, hbm);
2971 SetRect(&rc, 0, 0, 48, 16);
2972 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2974 SetRect(&rc, 18, 2, 30, 14);
2975 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2976 DFCS_BUTTONCHECK|DFCS_FLAT);
2978 SetRect(&rc, 34, 2, 46, 14);
2979 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2980 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2982 SelectObject(hdc, hbmOld);
2983 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2984 comctl32_color.clrWindow);
2985 TRACE("checkbox index %d\n", nIndex);
2987 DeleteObject(hbm);
2988 DeleteDC(hdc);
2989 ReleaseDC(0, hdcScreen);
2991 infoPtr->stateImageWidth = 16;
2992 infoPtr->stateImageHeight = 16;
2995 static void
2996 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2998 TREEVIEW_ITEM *child = item->firstChild;
3000 item->state &= ~TVIS_STATEIMAGEMASK;
3001 item->state |= INDEXTOSTATEIMAGEMASK(1);
3003 while (child)
3005 TREEVIEW_ITEM *next = child->nextSibling;
3006 TREEVIEW_ResetImageStateIndex(infoPtr, child);
3007 child = next;
3011 static LRESULT
3012 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
3014 HDC hdc;
3015 PAINTSTRUCT ps;
3016 RECT rc;
3018 TRACE("(%p %p)\n", infoPtr, hdc_ref);
3020 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
3022 TREEVIEW_InitCheckboxes(infoPtr);
3023 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
3025 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
3026 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
3027 TREEVIEW_UpdateScrollBars(infoPtr);
3028 TREEVIEW_Invalidate(infoPtr, NULL);
3031 if (hdc_ref)
3033 hdc = hdc_ref;
3034 GetClientRect(infoPtr->hwnd, &rc);
3035 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3037 else
3039 hdc = BeginPaint(infoPtr->hwnd, &ps);
3040 rc = ps.rcPaint;
3041 if(ps.fErase)
3042 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3045 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3046 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3048 if (!hdc_ref)
3049 EndPaint(infoPtr->hwnd, &ps);
3051 return 0;
3054 static LRESULT
3055 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3057 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3059 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3060 return 0;
3062 if (options & PRF_ERASEBKGND)
3063 TREEVIEW_EraseBackground(infoPtr, hdc);
3065 if (options & PRF_CLIENT)
3067 RECT rc;
3068 GetClientRect(infoPtr->hwnd, &rc);
3069 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3072 return 0;
3075 /* Sorting **************************************************************/
3077 /***************************************************************************
3078 * Forward the DPA local callback to the treeview owner callback
3080 static INT WINAPI
3081 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3082 const TVSORTCB *pCallBackSort)
3084 /* Forward the call to the client-defined callback */
3085 return pCallBackSort->lpfnCompare(first->lParam,
3086 second->lParam,
3087 pCallBackSort->lParam);
3090 /***************************************************************************
3091 * Treeview native sort routine: sort on item text.
3093 static INT WINAPI
3094 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3095 const TREEVIEW_INFO *infoPtr)
3097 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3098 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3100 if(first->pszText && second->pszText)
3101 return lstrcmpiW(first->pszText, second->pszText);
3102 else if(first->pszText)
3103 return -1;
3104 else if(second->pszText)
3105 return 1;
3106 else
3107 return 0;
3110 /* Returns the number of physical children belonging to item. */
3111 static INT
3112 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3114 INT cChildren = 0;
3115 HTREEITEM hti;
3117 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3118 cChildren++;
3120 return cChildren;
3123 /* Returns a DPA containing a pointer to each physical child of item in
3124 * sibling order. If item has no children, an empty DPA is returned. */
3125 static HDPA
3126 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3128 HTREEITEM child;
3130 HDPA list = DPA_Create(8);
3131 if (list == 0) return NULL;
3133 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3135 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3137 DPA_Destroy(list);
3138 return NULL;
3142 return list;
3145 /***************************************************************************
3146 * Setup the treeview structure with regards of the sort method
3147 * and sort the children of the TV item specified in lParam
3148 * fRecurse: currently unused. Should be zero.
3149 * parent: if pSort!=NULL, should equal pSort->hParent.
3150 * otherwise, item which child items are to be sorted.
3151 * pSort: sort method info. if NULL, sort on item text.
3152 * if non-NULL, sort on item's lParam content, and let the
3153 * application decide what that means. See also TVM_SORTCHILDRENCB.
3156 static LRESULT
3157 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3158 LPTVSORTCB pSort)
3160 INT cChildren;
3161 PFNDPACOMPARE pfnCompare;
3162 LPARAM lpCompare;
3164 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3165 if (parent == TVI_ROOT || parent == NULL)
3166 parent = infoPtr->root;
3168 /* Check for a valid handle to the parent item */
3169 if (!TREEVIEW_ValidItem(infoPtr, parent))
3171 ERR("invalid item hParent=%p\n", parent);
3172 return FALSE;
3175 if (pSort)
3177 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3178 lpCompare = (LPARAM)pSort;
3180 else
3182 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3183 lpCompare = (LPARAM)infoPtr;
3186 cChildren = TREEVIEW_CountChildren(parent);
3188 /* Make sure there is something to sort */
3189 if (cChildren > 1)
3191 /* TREEVIEW_ITEM rechaining */
3192 INT count = 0;
3193 HTREEITEM item = 0;
3194 HTREEITEM nextItem = 0;
3195 HTREEITEM prevItem = 0;
3197 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3199 if (sortList == NULL)
3200 return FALSE;
3202 /* let DPA sort the list */
3203 DPA_Sort(sortList, pfnCompare, lpCompare);
3205 /* The order of DPA entries has been changed, so fixup the
3206 * nextSibling and prevSibling pointers. */
3208 item = DPA_GetPtr(sortList, count++);
3209 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3211 /* link the two current item together */
3212 item->nextSibling = nextItem;
3213 nextItem->prevSibling = item;
3215 if (prevItem == NULL)
3217 /* this is the first item, update the parent */
3218 parent->firstChild = item;
3219 item->prevSibling = NULL;
3221 else
3223 /* fix the back chaining */
3224 item->prevSibling = prevItem;
3227 /* get ready for the next one */
3228 prevItem = item;
3229 item = nextItem;
3232 /* the last item is pointed to by item and never has a sibling */
3233 item->nextSibling = NULL;
3234 parent->lastChild = item;
3236 DPA_Destroy(sortList);
3238 TREEVIEW_VerifyTree(infoPtr);
3240 if (parent->state & TVIS_EXPANDED)
3242 int visOrder = infoPtr->firstVisible->visibleOrder;
3244 if (parent == infoPtr->root)
3245 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3246 else
3247 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3249 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3251 TREEVIEW_ITEM *item;
3253 for (item = infoPtr->root->firstChild; item != NULL;
3254 item = TREEVIEW_GetNextListItem(infoPtr, item))
3256 if (item->visibleOrder == visOrder)
3257 break;
3260 if (!item) item = parent->firstChild;
3261 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3264 TREEVIEW_Invalidate(infoPtr, NULL);
3267 return TRUE;
3269 return FALSE;
3273 /***************************************************************************
3274 * Setup the treeview structure with regards of the sort method
3275 * and sort the children of the TV item specified in lParam
3277 static LRESULT
3278 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3280 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3284 /***************************************************************************
3285 * Sort the children of the TV item specified in lParam.
3287 static LRESULT
3288 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3290 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3294 /* Expansion/Collapse ***************************************************/
3296 static BOOL
3297 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3298 UINT action)
3300 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3301 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3302 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3303 0, item);
3306 static VOID
3307 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3308 UINT action)
3310 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3311 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3312 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3313 0, item);
3317 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3318 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3319 static BOOL
3320 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3321 BOOL bRemoveChildren, BOOL bUser)
3323 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3324 BOOL bSetSelection, bSetFirstVisible;
3325 RECT scrollRect;
3326 LONG scrollDist = 0;
3327 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3328 BOOL wasExpanded;
3330 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3332 if (!TREEVIEW_HasChildren(infoPtr, item))
3333 return FALSE;
3335 if (bUser)
3336 TREEVIEW_SendExpanding(infoPtr, item, action);
3338 if (item->firstChild == NULL)
3339 return FALSE;
3341 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3342 item->state &= ~TVIS_EXPANDED;
3344 if (wasExpanded && bUser)
3345 TREEVIEW_SendExpanded(infoPtr, item, action);
3347 bSetSelection = (infoPtr->selectedItem != NULL
3348 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3350 bSetFirstVisible = (infoPtr->firstVisible != NULL
3351 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3353 tmpItem = item;
3354 while (tmpItem)
3356 if (tmpItem->nextSibling)
3358 nextItem = tmpItem->nextSibling;
3359 break;
3361 tmpItem = tmpItem->parent;
3364 if (nextItem)
3365 scrollDist = nextItem->rect.top;
3367 if (bRemoveChildren)
3369 INT old_cChildren = item->cChildren;
3370 TRACE("TVE_COLLAPSERESET\n");
3371 item->state &= ~TVIS_EXPANDEDONCE;
3372 TREEVIEW_RemoveAllChildren(infoPtr, item);
3373 item->cChildren = old_cChildren;
3375 if (!wasExpanded)
3376 return FALSE;
3378 if (item->firstChild)
3380 TREEVIEW_ITEM *i, *sibling;
3382 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3384 for (i = item->firstChild; i != sibling;
3385 i = TREEVIEW_GetNextListItem(infoPtr, i))
3387 i->visibleOrder = -1;
3391 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3393 if (nextItem)
3394 scrollDist = -(scrollDist - nextItem->rect.top);
3396 if (bSetSelection)
3398 /* Don't call DoSelectItem, it sends notifications. */
3399 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3400 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3401 item->state |= TVIS_SELECTED;
3402 infoPtr->selectedItem = item;
3405 TREEVIEW_UpdateScrollBars(infoPtr);
3407 scrollRect.left = 0;
3408 scrollRect.right = infoPtr->clientWidth;
3409 scrollRect.bottom = infoPtr->clientHeight;
3411 if (nextItem)
3413 scrollRect.top = nextItem->rect.top;
3415 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3416 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3417 TREEVIEW_Invalidate(infoPtr, item);
3418 } else {
3419 scrollRect.top = item->rect.top;
3420 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3423 TREEVIEW_SetFirstVisible(infoPtr,
3424 bSetFirstVisible ? item : infoPtr->firstVisible,
3425 TRUE);
3427 return wasExpanded;
3430 static BOOL
3431 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3432 BOOL partial, BOOL user)
3434 LONG scrollDist;
3435 LONG orgNextTop = 0;
3436 RECT scrollRect;
3437 TREEVIEW_ITEM *nextItem, *tmpItem;
3438 BOOL sendsNotifications;
3440 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3442 if (!TREEVIEW_HasChildren(infoPtr, item))
3443 return FALSE;
3445 tmpItem = item; nextItem = NULL;
3446 while (tmpItem)
3448 if (tmpItem->nextSibling)
3450 nextItem = tmpItem->nextSibling;
3451 break;
3453 tmpItem = tmpItem->parent;
3456 if (nextItem)
3457 orgNextTop = nextItem->rect.top;
3459 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3461 sendsNotifications = user || ((item->cChildren != 0) &&
3462 !(item->state & TVIS_EXPANDEDONCE));
3463 if (sendsNotifications)
3465 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3467 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3468 return FALSE;
3471 if (!item->firstChild)
3472 return FALSE;
3474 item->state |= TVIS_EXPANDED;
3476 if (partial)
3477 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3479 if (ISVISIBLE(item))
3481 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3482 TREEVIEW_UpdateSubTree(infoPtr, item);
3483 TREEVIEW_UpdateScrollBars(infoPtr);
3485 scrollRect.left = 0;
3486 scrollRect.bottom = infoPtr->treeHeight;
3487 scrollRect.right = infoPtr->clientWidth;
3488 if (nextItem)
3490 scrollDist = nextItem->rect.top - orgNextTop;
3491 scrollRect.top = orgNextTop;
3493 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3494 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3495 TREEVIEW_Invalidate (infoPtr, item);
3496 } else {
3497 scrollRect.top = item->rect.top;
3498 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3501 /* Scroll up so that as many children as possible are visible.
3502 * This fails when expanding causes an HScroll bar to appear, but we
3503 * don't know that yet, so the last item is obscured. */
3504 if (item->firstChild != NULL)
3506 int nChildren = item->lastChild->visibleOrder
3507 - item->firstChild->visibleOrder + 1;
3509 int visible_pos = item->visibleOrder
3510 - infoPtr->firstVisible->visibleOrder;
3512 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3514 if (visible_pos > 0 && nChildren > rows_below)
3516 int scroll = nChildren - rows_below;
3518 if (scroll > visible_pos)
3519 scroll = visible_pos;
3521 if (scroll > 0)
3523 TREEVIEW_ITEM *newFirstVisible
3524 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3525 scroll);
3528 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3534 if (sendsNotifications) {
3535 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3536 item->state |= TVIS_EXPANDEDONCE;
3539 return TRUE;
3542 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3543 to mouse messages and TVM_SELECTITEM.
3545 selection - previously selected item, used to collapse a part of a tree
3546 item - new selected item
3548 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3549 HTREEITEM selection, HTREEITEM item)
3551 TREEVIEW_ITEM *prev, *curr;
3553 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3555 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3558 * Close the previous item and its ancestors as long as they are not
3559 * ancestors of the current item
3561 for (prev = selection; prev && TREEVIEW_ValidItem(infoPtr, prev); prev = prev->parent)
3563 for (curr = item; curr && TREEVIEW_ValidItem(infoPtr, curr); curr = curr->parent)
3565 if (curr == prev)
3566 goto finish;
3568 TREEVIEW_Collapse(infoPtr, prev, FALSE, TRUE);
3571 finish:
3573 * Expand the current item
3575 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3578 static BOOL
3579 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3581 TRACE("item=%p, user=%d\n", item, user);
3583 if (item->state & TVIS_EXPANDED)
3584 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3585 else
3586 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3589 static VOID
3590 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3592 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3594 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3596 if (TREEVIEW_HasChildren(infoPtr, item))
3597 TREEVIEW_ExpandAll(infoPtr, item);
3601 /* Note:If the specified item is the child of a collapsed parent item,
3602 the parent's list of child items is (recursively) expanded to reveal the
3603 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3604 know if it also applies here.
3607 static LRESULT
3608 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3610 if (!TREEVIEW_ValidItem(infoPtr, item))
3611 return 0;
3613 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3614 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3615 flag, item->state);
3617 switch (flag & TVE_TOGGLE)
3619 case TVE_COLLAPSE:
3620 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3621 FALSE);
3623 case TVE_EXPAND:
3624 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3625 FALSE);
3627 case TVE_TOGGLE:
3628 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3630 default:
3631 return 0;
3635 /* Hit-Testing **********************************************************/
3637 static TREEVIEW_ITEM *
3638 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3640 TREEVIEW_ITEM *item;
3641 LONG row;
3643 if (!infoPtr->firstVisible)
3644 return NULL;
3646 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3648 for (item = infoPtr->firstVisible; item != NULL;
3649 item = TREEVIEW_GetNextListItem(infoPtr, item))
3651 if (row >= item->visibleOrder
3652 && row < item->visibleOrder + item->iIntegral)
3653 break;
3656 return item;
3659 static TREEVIEW_ITEM *
3660 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3662 TREEVIEW_ITEM *item;
3663 RECT rect;
3664 UINT status;
3665 LONG x, y;
3667 lpht->hItem = 0;
3668 GetClientRect(infoPtr->hwnd, &rect);
3669 status = 0;
3670 x = lpht->pt.x;
3671 y = lpht->pt.y;
3673 if (x < rect.left)
3675 status |= TVHT_TOLEFT;
3677 else if (x > rect.right)
3679 status |= TVHT_TORIGHT;
3682 if (y < rect.top)
3684 status |= TVHT_ABOVE;
3686 else if (y > rect.bottom)
3688 status |= TVHT_BELOW;
3691 if (status)
3693 lpht->flags = status;
3694 return NULL;
3697 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3698 if (!item)
3700 lpht->flags = TVHT_NOWHERE;
3701 return NULL;
3704 if (x >= item->textOffset + item->textWidth)
3706 lpht->flags = TVHT_ONITEMRIGHT;
3708 else if (x >= item->textOffset)
3710 lpht->flags = TVHT_ONITEMLABEL;
3712 else if (x >= item->imageOffset)
3714 lpht->flags = TVHT_ONITEMICON;
3716 else if (x >= item->stateOffset)
3718 lpht->flags = TVHT_ONITEMSTATEICON;
3720 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3722 lpht->flags = TVHT_ONITEMBUTTON;
3724 else
3726 lpht->flags = TVHT_ONITEMINDENT;
3729 lpht->hItem = item;
3730 TRACE("(%d,%d):result 0x%x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3732 return item;
3735 /* Item Label Editing ***************************************************/
3737 static LRESULT
3738 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3740 return (LRESULT)infoPtr->hwndEdit;
3743 static LRESULT CALLBACK
3744 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3746 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3747 BOOL bCancel = FALSE;
3748 LRESULT rc;
3750 switch (uMsg)
3752 case WM_PAINT:
3753 TRACE("WM_PAINT start\n");
3754 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3755 lParam);
3756 TRACE("WM_PAINT done\n");
3757 return rc;
3759 case WM_KILLFOCUS:
3760 if (infoPtr->bIgnoreEditKillFocus)
3761 return TRUE;
3762 break;
3764 case WM_DESTROY:
3766 WNDPROC editProc = infoPtr->wpEditOrig;
3767 infoPtr->wpEditOrig = 0;
3768 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3769 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3772 case WM_GETDLGCODE:
3773 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3775 case WM_KEYDOWN:
3776 if (wParam == VK_ESCAPE)
3778 bCancel = TRUE;
3779 break;
3781 else if (wParam == VK_RETURN)
3783 break;
3786 /* fall through */
3787 default:
3788 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3791 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3792 /* eg. Using a messagebox */
3794 infoPtr->bIgnoreEditKillFocus = TRUE;
3795 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3796 infoPtr->bIgnoreEditKillFocus = FALSE;
3798 return 0;
3802 /* should handle edit control messages here */
3804 static LRESULT
3805 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3807 TRACE("code=0x%x, id=0x%x, handle=0x%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3809 switch (HIWORD(wParam))
3811 case EN_UPDATE:
3814 * Adjust the edit window size
3816 WCHAR buffer[1024];
3817 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3818 HDC hdc = GetDC(infoPtr->hwndEdit);
3819 SIZE sz;
3820 HFONT hFont, hOldFont = 0;
3822 TRACE("edit=%p\n", infoPtr->hwndEdit);
3824 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3826 infoPtr->bLabelChanged = TRUE;
3828 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3830 /* Select font to get the right dimension of the string */
3831 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3833 if (hFont != 0)
3835 hOldFont = SelectObject(hdc, hFont);
3838 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3840 TEXTMETRICW textMetric;
3842 /* Add Extra spacing for the next character */
3843 GetTextMetricsW(hdc, &textMetric);
3844 sz.cx += (textMetric.tmMaxCharWidth * 2);
3846 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3847 sz.cx = min(sz.cx,
3848 infoPtr->clientWidth - editItem->textOffset + 2);
3850 SetWindowPos(infoPtr->hwndEdit,
3851 HWND_TOP,
3854 sz.cx,
3855 editItem->rect.bottom - editItem->rect.top + 3,
3856 SWP_NOMOVE | SWP_DRAWFRAME);
3859 if (hFont != 0)
3861 SelectObject(hdc, hOldFont);
3864 ReleaseDC(infoPtr->hwnd, hdc);
3865 break;
3867 case EN_KILLFOCUS:
3868 /* apparently we should respect passed handle value */
3869 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3871 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3872 break;
3874 default:
3875 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3878 return 0;
3881 static HWND
3882 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3884 HWND hwnd = infoPtr->hwnd;
3885 HWND hwndEdit;
3886 SIZE sz;
3887 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3888 HDC hdc;
3889 HFONT hOldFont=0;
3890 TEXTMETRICW textMetric;
3892 TRACE("%p %p\n", hwnd, hItem);
3893 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3894 return NULL;
3896 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3897 return NULL;
3899 if (infoPtr->hwndEdit)
3900 return infoPtr->hwndEdit;
3902 infoPtr->bLabelChanged = FALSE;
3904 /* make edit item visible */
3905 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3907 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3909 hdc = GetDC(hwnd);
3910 /* Select the font to get appropriate metric dimensions */
3911 if (infoPtr->hFont != 0)
3913 hOldFont = SelectObject(hdc, infoPtr->hFont);
3916 /* Get string length in pixels */
3917 if (hItem->pszText)
3918 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3919 &sz);
3920 else
3921 GetTextExtentPoint32A(hdc, "", 0, &sz);
3923 /* Add Extra spacing for the next character */
3924 GetTextMetricsW(hdc, &textMetric);
3925 sz.cx += (textMetric.tmMaxCharWidth * 2);
3927 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3928 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3930 if (infoPtr->hFont != 0)
3932 SelectObject(hdc, hOldFont);
3935 ReleaseDC(hwnd, hdc);
3937 infoPtr->editItem = hItem;
3939 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3940 WC_EDITW,
3942 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3943 WS_CLIPSIBLINGS | ES_WANTRETURN |
3944 ES_LEFT, hItem->textOffset - 2,
3945 hItem->rect.top - 1, sz.cx + 3,
3946 hItem->rect.bottom -
3947 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3948 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3950 infoPtr->hwndEdit = hwndEdit;
3952 /* Get a 2D border. */
3953 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3954 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3955 SetWindowLongW(hwndEdit, GWL_STYLE,
3956 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3958 SendMessageW(hwndEdit, WM_SETFONT,
3959 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3961 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3962 (DWORD_PTR)
3963 TREEVIEW_Edit_SubclassProc);
3964 if (hItem->pszText)
3965 SetWindowTextW(hwndEdit, hItem->pszText);
3967 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3969 DestroyWindow(hwndEdit);
3970 infoPtr->hwndEdit = 0;
3971 infoPtr->editItem = NULL;
3972 return NULL;
3975 SetFocus(hwndEdit);
3976 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3977 ShowWindow(hwndEdit, SW_SHOW);
3979 return hwndEdit;
3983 static LRESULT
3984 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3986 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3987 NMTVDISPINFOW tvdi;
3988 BOOL bCommit;
3989 WCHAR tmpText[1024] = { '\0' };
3990 WCHAR *newText = tmpText;
3991 int iLength = 0;
3993 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3995 tvdi.item.mask = 0;
3996 tvdi.item.hItem = editedItem;
3997 tvdi.item.state = editedItem->state;
3998 tvdi.item.lParam = editedItem->lParam;
4000 if (!bCancel)
4002 if (!infoPtr->bNtfUnicode)
4003 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
4004 else
4005 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
4007 if (iLength >= 1023)
4009 ERR("Insufficient space to retrieve new item label\n");
4012 tvdi.item.mask = TVIF_TEXT;
4013 tvdi.item.pszText = tmpText;
4014 tvdi.item.cchTextMax = iLength + 1;
4016 else
4018 tvdi.item.pszText = NULL;
4019 tvdi.item.cchTextMax = 0;
4022 bCommit = TREEVIEW_SendRealNotify(infoPtr, TVN_ENDLABELEDITW, &tvdi.hdr);
4024 if (!bCancel && bCommit) /* Apply the changes */
4026 if (!infoPtr->bNtfUnicode)
4028 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
4029 newText = Alloc(len * sizeof(WCHAR));
4030 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
4031 iLength = len - 1;
4034 if (strcmpW(newText, editedItem->pszText) != 0)
4036 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
4037 if (ptr == NULL)
4039 ERR("OutOfMemory, cannot allocate space for label\n");
4040 if(newText != tmpText) Free(newText);
4041 DestroyWindow(infoPtr->hwndEdit);
4042 infoPtr->hwndEdit = 0;
4043 infoPtr->editItem = NULL;
4044 return FALSE;
4046 else
4048 editedItem->pszText = ptr;
4049 editedItem->cchTextMax = iLength + 1;
4050 strcpyW(editedItem->pszText, newText);
4051 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4054 if(newText != tmpText) Free(newText);
4057 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4058 DestroyWindow(infoPtr->hwndEdit);
4059 infoPtr->hwndEdit = 0;
4060 infoPtr->editItem = NULL;
4061 return TRUE;
4064 static LRESULT
4065 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4067 if (wParam != TV_EDIT_TIMER)
4069 ERR("got unknown timer\n");
4070 return 1;
4073 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4074 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4076 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4078 return 0;
4082 /* Mouse Tracking/Drag **************************************************/
4084 /***************************************************************************
4085 * This is quite unusual piece of code, but that's how it's implemented in
4086 * Windows.
4088 static LRESULT
4089 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4091 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4092 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4093 RECT r;
4094 MSG msg;
4096 r.top = pt.y - cyDrag;
4097 r.left = pt.x - cxDrag;
4098 r.bottom = pt.y + cyDrag;
4099 r.right = pt.x + cxDrag;
4101 SetCapture(infoPtr->hwnd);
4103 while (1)
4105 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4107 if (msg.message == WM_MOUSEMOVE)
4109 pt.x = (short)LOWORD(msg.lParam);
4110 pt.y = (short)HIWORD(msg.lParam);
4111 if (PtInRect(&r, pt))
4112 continue;
4113 else
4115 ReleaseCapture();
4116 return 1;
4119 else if (msg.message >= WM_LBUTTONDOWN &&
4120 msg.message <= WM_RBUTTONDBLCLK)
4122 break;
4125 DispatchMessageW(&msg);
4128 if (GetCapture() != infoPtr->hwnd)
4129 return 0;
4132 ReleaseCapture();
4133 return 0;
4137 static LRESULT
4138 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4140 TREEVIEW_ITEM *item;
4141 TVHITTESTINFO hit;
4143 TRACE("\n");
4144 SetFocus(infoPtr->hwnd);
4146 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4148 /* If there is pending 'edit label' event - kill it now */
4149 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4152 hit.pt.x = (short)LOWORD(lParam);
4153 hit.pt.y = (short)HIWORD(lParam);
4155 item = TREEVIEW_HitTest(infoPtr, &hit);
4156 if (!item)
4157 return 0;
4158 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4160 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4161 { /* FIXME! */
4162 switch (hit.flags)
4164 case TVHT_ONITEMRIGHT:
4165 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4166 break;
4168 case TVHT_ONITEMINDENT:
4169 if (!(infoPtr->dwStyle & TVS_HASLINES))
4171 break;
4173 else
4175 int level = hit.pt.x / infoPtr->uIndent;
4176 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4178 while (item->iLevel > level)
4180 item = item->parent;
4183 /* fall through */
4186 case TVHT_ONITEMLABEL:
4187 case TVHT_ONITEMICON:
4188 case TVHT_ONITEMBUTTON:
4189 TREEVIEW_Toggle(infoPtr, item, TRUE);
4190 break;
4192 case TVHT_ONITEMSTATEICON:
4193 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4194 TREEVIEW_ToggleItemState(infoPtr, item);
4195 else
4196 TREEVIEW_Toggle(infoPtr, item, TRUE);
4197 break;
4200 return TRUE;
4204 static LRESULT
4205 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4207 BOOL do_track, do_select, bDoLabelEdit;
4208 HWND hwnd = infoPtr->hwnd;
4209 TVHITTESTINFO ht;
4211 /* If Edit control is active - kill it and return.
4212 * The best way to do it is to set focus to itself.
4213 * Edit control subclassed procedure will automatically call
4214 * EndEditLabelNow.
4216 if (infoPtr->hwndEdit)
4218 SetFocus(hwnd);
4219 return 0;
4222 ht.pt.x = (short)LOWORD(lParam);
4223 ht.pt.y = (short)HIWORD(lParam);
4225 TREEVIEW_HitTest(infoPtr, &ht);
4226 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4228 /* update focusedItem and redraw both items */
4229 if (ht.hItem)
4231 BOOL do_focus;
4233 if (TREEVIEW_IsFullRowSelect(infoPtr))
4234 do_focus = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEM | TVHT_ONITEMRIGHT);
4235 else
4236 do_focus = ht.flags & TVHT_ONITEM;
4238 if (do_focus)
4240 infoPtr->focusedItem = ht.hItem;
4241 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4242 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4246 if (!(infoPtr->dwStyle & TVS_DISABLEDRAGDROP))
4248 if (TREEVIEW_IsFullRowSelect(infoPtr))
4249 do_track = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEM | TVHT_ONITEMRIGHT);
4250 else
4251 do_track = ht.flags & TVHT_ONITEM;
4253 else
4254 do_track = FALSE;
4257 * If the style allows editing and the node is already selected
4258 * and the click occurred on the item label...
4260 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4261 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4263 /* Send NM_CLICK right away */
4264 if (!do_track && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4265 goto setfocus;
4267 if (ht.flags & TVHT_ONITEMBUTTON)
4269 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4270 goto setfocus;
4272 else if (do_track)
4273 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4274 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4276 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4277 infoPtr->dropItem = ht.hItem;
4279 /* clean up focusedItem as we dragged and won't select this item */
4280 if(infoPtr->focusedItem)
4282 /* refresh the item that was focused */
4283 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4284 infoPtr->focusedItem = NULL;
4286 /* refresh the selected item to return the filled background */
4287 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4290 return 0;
4294 if (do_track && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4295 goto setfocus;
4297 if (TREEVIEW_IsFullRowSelect(infoPtr))
4298 do_select = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEMICON | TVHT_ONITEMLABEL | TVHT_ONITEMRIGHT);
4299 else
4300 do_select = ht.flags & (TVHT_ONITEMICON | TVHT_ONITEMLABEL);
4302 if (bDoLabelEdit)
4304 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4305 KillTimer(hwnd, TV_EDIT_TIMER);
4307 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4308 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4310 else if (do_select)
4312 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4314 /* Select the current item */
4315 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4316 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4318 else if (ht.flags & TVHT_ONITEMSTATEICON)
4320 /* TVS_CHECKBOXES requires us to toggle the current state */
4321 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4322 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4325 setfocus:
4326 SetFocus(hwnd);
4327 return 0;
4331 static LRESULT
4332 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4334 TVHITTESTINFO ht;
4336 if (infoPtr->hwndEdit)
4338 SetFocus(infoPtr->hwnd);
4339 return 0;
4342 ht.pt.x = (short)LOWORD(lParam);
4343 ht.pt.y = (short)HIWORD(lParam);
4345 TREEVIEW_HitTest(infoPtr, &ht);
4347 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4349 if (ht.hItem)
4351 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4352 infoPtr->dropItem = ht.hItem;
4355 else
4357 SetFocus(infoPtr->hwnd);
4358 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4360 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4361 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4362 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4366 return 0;
4369 static LRESULT
4370 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4372 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4373 INT cx, cy;
4374 HDC hdc, htopdc;
4375 HWND hwtop;
4376 HBITMAP hbmp, hOldbmp;
4377 SIZE size;
4378 RECT rc;
4379 HFONT hOldFont;
4381 TRACE("\n");
4383 if (!(infoPtr->himlNormal))
4384 return 0;
4386 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4387 return 0;
4389 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4391 hwtop = GetDesktopWindow();
4392 htopdc = GetDC(hwtop);
4393 hdc = CreateCompatibleDC(htopdc);
4395 hOldFont = SelectObject(hdc, infoPtr->hFont);
4397 if (dragItem->pszText)
4398 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4399 &size);
4400 else
4401 GetTextExtentPoint32A(hdc, "", 0, &size);
4403 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4404 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4405 hOldbmp = SelectObject(hdc, hbmp);
4407 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4408 size.cx += cx;
4409 if (cy > size.cy)
4410 size.cy = cy;
4412 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4413 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4414 ILD_NORMAL);
4417 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4418 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4421 /* draw item text */
4423 SetRect(&rc, cx, 0, size.cx, size.cy);
4425 if (dragItem->pszText)
4426 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4427 DT_LEFT);
4429 SelectObject(hdc, hOldFont);
4430 SelectObject(hdc, hOldbmp);
4432 ImageList_Add(infoPtr->dragList, hbmp, 0);
4434 DeleteDC(hdc);
4435 DeleteObject(hbmp);
4436 ReleaseDC(hwtop, htopdc);
4438 return (LRESULT)infoPtr->dragList;
4441 /* Selection ************************************************************/
4443 static LRESULT
4444 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4445 INT cause)
4447 TREEVIEW_ITEM *prevSelect;
4449 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4451 TRACE("Entering item %p (%s), flag 0x%x, cause 0x%x, state %d\n",
4452 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4453 newSelect ? newSelect->state : 0);
4455 /* reset and redraw focusedItem if focusedItem was set so we don't */
4456 /* have to worry about the previously focused item when we set a new one */
4457 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4458 infoPtr->focusedItem = NULL;
4460 switch (action)
4462 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4463 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4464 /* Fall through */
4465 case TVGN_CARET:
4466 prevSelect = infoPtr->selectedItem;
4468 if (prevSelect == newSelect) {
4469 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4470 break;
4473 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4474 TVN_SELCHANGINGW,
4475 cause,
4476 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4477 prevSelect,
4478 newSelect))
4479 return FALSE;
4481 if (prevSelect)
4482 prevSelect->state &= ~TVIS_SELECTED;
4483 if (newSelect)
4484 newSelect->state |= TVIS_SELECTED;
4486 infoPtr->selectedItem = newSelect;
4488 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4490 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4491 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4493 TREEVIEW_SendTreeviewNotify(infoPtr,
4494 TVN_SELCHANGEDW,
4495 cause,
4496 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4497 prevSelect,
4498 newSelect);
4499 break;
4501 case TVGN_DROPHILITE:
4502 prevSelect = infoPtr->dropItem;
4504 if (prevSelect)
4505 prevSelect->state &= ~TVIS_DROPHILITED;
4507 infoPtr->dropItem = newSelect;
4509 if (newSelect)
4510 newSelect->state |= TVIS_DROPHILITED;
4512 TREEVIEW_Invalidate(infoPtr, prevSelect);
4513 TREEVIEW_Invalidate(infoPtr, newSelect);
4514 break;
4516 case TVGN_FIRSTVISIBLE:
4517 if (newSelect != NULL)
4519 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4520 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4521 TREEVIEW_Invalidate(infoPtr, NULL);
4523 break;
4526 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4527 return TRUE;
4530 /* FIXME: handle NM_KILLFOCUS etc */
4531 static LRESULT
4532 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4534 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4536 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4537 return FALSE;
4539 if (item == infoPtr->selectedItem)
4540 return TRUE;
4542 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4544 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4545 return FALSE;
4547 TREEVIEW_SingleExpand(infoPtr, selection, item);
4549 return TRUE;
4552 /*************************************************************************
4553 * TREEVIEW_ProcessLetterKeys
4555 * Processes keyboard messages generated by pressing the letter keys
4556 * on the keyboard.
4557 * What this does is perform a case insensitive search from the
4558 * current position with the following quirks:
4559 * - If two chars or more are pressed in quick succession we search
4560 * for the corresponding string (e.g. 'abc').
4561 * - If there is a delay we wipe away the current search string and
4562 * restart with just that char.
4563 * - If the user keeps pressing the same character, whether slowly or
4564 * fast, so that the search string is entirely composed of this
4565 * character ('aaaaa' for instance), then we search for first item
4566 * that starting with that character.
4567 * - If the user types the above character in quick succession, then
4568 * we must also search for the corresponding string ('aaaaa'), and
4569 * go to that string if there is a match.
4571 * RETURNS
4573 * Zero.
4575 * BUGS
4577 * - The current implementation has a list of characters it will
4578 * accept and it ignores everything else. In particular it will
4579 * ignore accentuated characters which seems to match what
4580 * Windows does. But I'm not sure it makes sense to follow
4581 * Windows there.
4582 * - We don't sound a beep when the search fails.
4583 * - The search should start from the focused item, not from the selected
4584 * item. One reason for this is to allow for multiple selections in trees.
4585 * But currently infoPtr->focusedItem does not seem very usable.
4587 * SEE ALSO
4589 * TREEVIEW_ProcessLetterKeys
4591 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4593 HTREEITEM nItem;
4594 HTREEITEM endidx,idx;
4595 TVITEMEXW item;
4596 WCHAR buffer[MAX_PATH];
4597 DWORD timestamp,elapsed;
4599 /* simple parameter checking */
4600 if (!charCode || !keyData) return 0;
4602 /* only allow the valid WM_CHARs through */
4603 if (!isalnum(charCode) &&
4604 charCode != '.' && charCode != '`' && charCode != '!' &&
4605 charCode != '@' && charCode != '#' && charCode != '$' &&
4606 charCode != '%' && charCode != '^' && charCode != '&' &&
4607 charCode != '*' && charCode != '(' && charCode != ')' &&
4608 charCode != '-' && charCode != '_' && charCode != '+' &&
4609 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4610 charCode != '}' && charCode != '[' && charCode != '{' &&
4611 charCode != '/' && charCode != '?' && charCode != '>' &&
4612 charCode != '<' && charCode != ',' && charCode != '~')
4613 return 0;
4615 /* compute how much time elapsed since last keypress */
4616 timestamp = GetTickCount();
4617 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4618 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4619 } else {
4620 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4623 /* update the search parameters */
4624 infoPtr->lastKeyPressTimestamp=timestamp;
4625 if (elapsed < KEY_DELAY) {
4626 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4627 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4629 if (infoPtr->charCode != charCode) {
4630 infoPtr->charCode=charCode=0;
4632 } else {
4633 infoPtr->charCode=charCode;
4634 infoPtr->szSearchParam[0]=charCode;
4635 infoPtr->nSearchParamLength=1;
4636 /* Redundant with the 1 char string */
4637 charCode=0;
4640 /* and search from the current position */
4641 nItem=NULL;
4642 if (infoPtr->selectedItem != NULL) {
4643 endidx=infoPtr->selectedItem;
4644 /* if looking for single character match,
4645 * then we must always move forward
4647 if (infoPtr->nSearchParamLength == 1)
4648 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4649 else
4650 idx=endidx;
4651 } else {
4652 endidx=NULL;
4653 idx=infoPtr->root->firstChild;
4655 do {
4656 /* At the end point, sort out wrapping */
4657 if (idx == NULL) {
4659 /* If endidx is null, stop at the last item (ie top to bottom) */
4660 if (endidx == NULL)
4661 break;
4663 /* Otherwise, start again at the very beginning */
4664 idx=infoPtr->root->firstChild;
4666 /* But if we are stopping on the first child, end now! */
4667 if (idx == endidx) break;
4670 /* get item */
4671 ZeroMemory(&item, sizeof(item));
4672 item.mask = TVIF_TEXT;
4673 item.hItem = idx;
4674 item.pszText = buffer;
4675 item.cchTextMax = sizeof(buffer);
4676 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4678 /* check for a match */
4679 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4680 nItem=idx;
4681 break;
4682 } else if ( (charCode != 0) && (nItem == NULL) &&
4683 (nItem != infoPtr->selectedItem) &&
4684 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4685 /* This would work but we must keep looking for a longer match */
4686 nItem=idx;
4688 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4689 } while (idx != endidx);
4691 if (nItem != NULL) {
4692 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4693 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4697 return 0;
4700 /* Scrolling ************************************************************/
4702 static LRESULT
4703 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4705 int viscount;
4706 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4707 HTREEITEM newFirstVisible = NULL;
4708 int visible_pos = -1;
4710 if (!TREEVIEW_ValidItem(infoPtr, item))
4711 return FALSE;
4713 if (!ISVISIBLE(item))
4715 /* Expand parents as necessary. */
4716 HTREEITEM parent;
4718 /* see if we are trying to ensure that root is visible */
4719 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4720 parent = item->parent;
4721 else
4722 parent = item; /* this item is the topmost item */
4724 while (parent != infoPtr->root)
4726 if (!(parent->state & TVIS_EXPANDED))
4727 TREEVIEW_Expand(infoPtr, parent, FALSE, TRUE);
4729 parent = parent->parent;
4733 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4735 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4736 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4738 if (hasFirstVisible)
4739 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4741 if (visible_pos < 0)
4743 /* item is before the start of the list: put it at the top. */
4744 newFirstVisible = item;
4746 else if (visible_pos >= viscount
4747 /* Sometimes, before we are displayed, GVC is 0, causing us to
4748 * spuriously scroll up. */
4749 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4751 /* item is past the end of the list. */
4752 int scroll = visible_pos - viscount;
4754 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4755 scroll + 1);
4758 if (bHScroll)
4760 /* Scroll window so item's text is visible as much as possible */
4761 /* Calculation of amount of extra space is taken from EditLabel code */
4762 INT pos, x;
4763 TEXTMETRICW textMetric;
4764 HDC hdc = GetWindowDC(infoPtr->hwnd);
4766 x = item->textWidth;
4768 GetTextMetricsW(hdc, &textMetric);
4769 ReleaseDC(infoPtr->hwnd, hdc);
4771 x += (textMetric.tmMaxCharWidth * 2);
4772 x = max(x, textMetric.tmMaxCharWidth * 3);
4774 if (item->textOffset < 0)
4775 pos = item->textOffset;
4776 else if (item->textOffset + x > infoPtr->clientWidth)
4778 if (x > infoPtr->clientWidth)
4779 pos = item->textOffset;
4780 else
4781 pos = item->textOffset + x - infoPtr->clientWidth;
4783 else
4784 pos = 0;
4786 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4789 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4791 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4793 return TRUE;
4796 return FALSE;
4799 static VOID
4800 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4801 TREEVIEW_ITEM *newFirstVisible,
4802 BOOL bUpdateScrollPos)
4804 int gap_size;
4806 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4808 if (newFirstVisible != NULL)
4810 /* Prevent an empty gap from appearing at the bottom... */
4811 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4812 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4814 if (gap_size > 0)
4816 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4817 -gap_size);
4819 /* ... unless we just don't have enough items. */
4820 if (newFirstVisible == NULL)
4821 newFirstVisible = infoPtr->root->firstChild;
4825 if (infoPtr->firstVisible != newFirstVisible)
4827 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4829 infoPtr->firstVisible = newFirstVisible;
4830 TREEVIEW_Invalidate(infoPtr, NULL);
4832 else
4834 TREEVIEW_ITEM *item;
4835 int scroll = infoPtr->uItemHeight *
4836 (infoPtr->firstVisible->visibleOrder
4837 - newFirstVisible->visibleOrder);
4839 infoPtr->firstVisible = newFirstVisible;
4841 for (item = infoPtr->root->firstChild; item != NULL;
4842 item = TREEVIEW_GetNextListItem(infoPtr, item))
4844 item->rect.top += scroll;
4845 item->rect.bottom += scroll;
4848 if (bUpdateScrollPos)
4849 SetScrollPos(infoPtr->hwnd, SB_VERT,
4850 newFirstVisible->visibleOrder, TRUE);
4852 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4857 /************************************************************************
4858 * VScroll is always in units of visible items. i.e. we always have a
4859 * visible item aligned to the top of the control. (Unless we have no
4860 * items at all.)
4862 static LRESULT
4863 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4865 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4866 TREEVIEW_ITEM *newFirstVisible = NULL;
4868 int nScrollCode = LOWORD(wParam);
4870 TRACE("wp %lx\n", wParam);
4872 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4873 return 0;
4875 if (!oldFirstVisible)
4877 assert(infoPtr->root->firstChild == NULL);
4878 return 0;
4881 switch (nScrollCode)
4883 case SB_TOP:
4884 newFirstVisible = infoPtr->root->firstChild;
4885 break;
4887 case SB_BOTTOM:
4888 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4889 break;
4891 case SB_LINEUP:
4892 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4893 break;
4895 case SB_LINEDOWN:
4896 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4897 break;
4899 case SB_PAGEUP:
4900 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4901 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4902 break;
4904 case SB_PAGEDOWN:
4905 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4906 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4907 break;
4909 case SB_THUMBTRACK:
4910 case SB_THUMBPOSITION:
4911 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4912 infoPtr->root->firstChild,
4913 (LONG)(SHORT)HIWORD(wParam));
4914 break;
4916 case SB_ENDSCROLL:
4917 return 0;
4920 if (newFirstVisible != NULL)
4922 if (newFirstVisible != oldFirstVisible)
4923 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4924 nScrollCode != SB_THUMBTRACK);
4925 else if (nScrollCode == SB_THUMBPOSITION)
4926 SetScrollPos(infoPtr->hwnd, SB_VERT,
4927 newFirstVisible->visibleOrder, TRUE);
4930 return 0;
4933 static LRESULT
4934 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4936 int maxWidth;
4937 int scrollX = infoPtr->scrollX;
4938 int nScrollCode = LOWORD(wParam);
4940 TRACE("wp %lx\n", wParam);
4942 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4943 return FALSE;
4945 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4946 /* shall never occur */
4947 if (maxWidth <= 0)
4949 scrollX = 0;
4950 goto scroll;
4953 switch (nScrollCode)
4955 case SB_LINELEFT:
4956 scrollX -= infoPtr->uItemHeight;
4957 break;
4958 case SB_LINERIGHT:
4959 scrollX += infoPtr->uItemHeight;
4960 break;
4961 case SB_PAGELEFT:
4962 scrollX -= infoPtr->clientWidth;
4963 break;
4964 case SB_PAGERIGHT:
4965 scrollX += infoPtr->clientWidth;
4966 break;
4968 case SB_THUMBTRACK:
4969 case SB_THUMBPOSITION:
4970 scrollX = (int)(SHORT)HIWORD(wParam);
4971 break;
4973 case SB_ENDSCROLL:
4974 return 0;
4977 if (scrollX > maxWidth)
4978 scrollX = maxWidth;
4979 else if (scrollX < 0)
4980 scrollX = 0;
4982 scroll:
4983 if (scrollX != infoPtr->scrollX)
4985 TREEVIEW_ITEM *item;
4986 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4988 for (item = infoPtr->root->firstChild; item != NULL;
4989 item = TREEVIEW_GetNextListItem(infoPtr, item))
4991 item->linesOffset += scroll_pixels;
4992 item->stateOffset += scroll_pixels;
4993 item->imageOffset += scroll_pixels;
4994 item->textOffset += scroll_pixels;
4997 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4998 infoPtr->scrollX = scrollX;
4999 UpdateWindow(infoPtr->hwnd);
5002 if (nScrollCode != SB_THUMBTRACK)
5003 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
5005 return 0;
5008 static LRESULT
5009 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5011 short wheelDelta;
5012 UINT pulScrollLines = 3;
5014 if (wParam & (MK_SHIFT | MK_CONTROL))
5015 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
5017 if (infoPtr->firstVisible == NULL)
5018 return TRUE;
5020 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
5022 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5023 /* if scrolling changes direction, ignore left overs */
5024 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
5025 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
5026 infoPtr->wheelRemainder += wheelDelta;
5027 else
5028 infoPtr->wheelRemainder = wheelDelta;
5030 if (infoPtr->wheelRemainder && pulScrollLines)
5032 int newDy;
5033 int maxDy;
5034 int lineScroll;
5036 lineScroll = pulScrollLines * (float)infoPtr->wheelRemainder / WHEEL_DELTA;
5037 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / (int)pulScrollLines;
5039 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
5040 maxDy = infoPtr->maxVisibleOrder;
5042 if (newDy > maxDy)
5043 newDy = maxDy;
5045 if (newDy < 0)
5046 newDy = 0;
5048 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
5050 return TRUE;
5053 /* Create/Destroy *******************************************************/
5055 static LRESULT
5056 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5058 RECT rcClient;
5059 TREEVIEW_INFO *infoPtr;
5060 LOGFONTW lf;
5062 TRACE("wnd %p, style 0x%x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5064 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5066 if (infoPtr == NULL)
5068 ERR("could not allocate info memory!\n");
5069 return 0;
5072 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5074 infoPtr->hwnd = hwnd;
5075 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5076 infoPtr->Timer = 0;
5077 infoPtr->uNumItems = 0;
5078 infoPtr->cdmode = 0;
5079 infoPtr->uScrollTime = 300; /* milliseconds */
5080 infoPtr->bRedraw = TRUE;
5082 GetClientRect(hwnd, &rcClient);
5084 /* No scroll bars yet. */
5085 infoPtr->clientWidth = rcClient.right;
5086 infoPtr->clientHeight = rcClient.bottom;
5087 infoPtr->uInternalStatus = 0;
5089 infoPtr->treeWidth = 0;
5090 infoPtr->treeHeight = 0;
5092 infoPtr->uIndent = MINIMUM_INDENT;
5093 infoPtr->selectedItem = NULL;
5094 infoPtr->focusedItem = NULL;
5095 infoPtr->hotItem = NULL;
5096 infoPtr->editItem = NULL;
5097 infoPtr->firstVisible = NULL;
5098 infoPtr->maxVisibleOrder = 0;
5099 infoPtr->dropItem = NULL;
5100 infoPtr->insertMarkItem = NULL;
5101 infoPtr->insertBeforeorAfter = 0;
5102 /* dragList */
5104 infoPtr->scrollX = 0;
5105 infoPtr->wheelRemainder = 0;
5107 infoPtr->clrBk = CLR_NONE; /* use system color */
5108 infoPtr->clrText = CLR_NONE; /* use system color */
5109 infoPtr->clrLine = CLR_DEFAULT;
5110 infoPtr->clrInsertMark = CLR_DEFAULT;
5112 /* hwndToolTip */
5114 infoPtr->hwndEdit = NULL;
5115 infoPtr->wpEditOrig = NULL;
5116 infoPtr->bIgnoreEditKillFocus = FALSE;
5117 infoPtr->bLabelChanged = FALSE;
5119 infoPtr->himlNormal = NULL;
5120 infoPtr->himlState = NULL;
5121 infoPtr->normalImageWidth = 0;
5122 infoPtr->normalImageHeight = 0;
5123 infoPtr->stateImageWidth = 0;
5124 infoPtr->stateImageHeight = 0;
5126 infoPtr->items = DPA_Create(16);
5128 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5129 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5130 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5131 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5132 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5133 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5135 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5137 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5138 infoPtr->root->state = TVIS_EXPANDED;
5139 infoPtr->root->iLevel = -1;
5140 infoPtr->root->visibleOrder = -1;
5142 infoPtr->hwndNotify = lpcs->hwndParent;
5143 infoPtr->hwndToolTip = 0;
5145 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5146 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5148 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5149 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5150 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5151 hwnd, 0, 0, 0);
5153 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5154 ShowScrollBar(hwnd, SB_VERT, FALSE);
5155 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5157 OpenThemeData (hwnd, themeClass);
5159 return 0;
5163 static LRESULT
5164 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5166 TRACE("\n");
5168 /* free item data */
5169 TREEVIEW_RemoveTree(infoPtr);
5170 /* root isn't freed with other items */
5171 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5172 DPA_Destroy(infoPtr->items);
5174 /* tool tip is automatically destroyed: we are its owner */
5176 /* Restore original wndproc */
5177 if (infoPtr->hwndEdit)
5178 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5179 (DWORD_PTR)infoPtr->wpEditOrig);
5181 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5183 /* Deassociate treeview from the window before doing anything drastic. */
5184 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5186 DeleteObject(infoPtr->hDefaultFont);
5187 DeleteObject(infoPtr->hBoldFont);
5188 DeleteObject(infoPtr->hUnderlineFont);
5189 DeleteObject(infoPtr->hBoldUnderlineFont);
5190 Free(infoPtr);
5192 return 0;
5195 /* Miscellaneous Messages ***********************************************/
5197 static LRESULT
5198 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5200 static const struct
5202 unsigned char code;
5204 scroll[] =
5206 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5207 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5208 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5209 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5210 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5211 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5212 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5213 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5214 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5215 #undef SCROLL_ENTRY
5218 if (key >= VK_PRIOR && key <= VK_DOWN)
5220 unsigned char code = scroll[key - VK_PRIOR].code;
5222 (((code & (1 << 7)) == (SB_HORZ << 7))
5223 ? TREEVIEW_HScroll
5224 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5227 return 0;
5230 /************************************************************************
5231 * TREEVIEW_KeyDown
5233 * VK_UP Move selection to the previous non-hidden item.
5234 * VK_DOWN Move selection to the next non-hidden item.
5235 * VK_HOME Move selection to the first item.
5236 * VK_END Move selection to the last item.
5237 * VK_LEFT If expanded then collapse, otherwise move to parent.
5238 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5239 * VK_ADD Expand.
5240 * VK_SUBTRACT Collapse.
5241 * VK_MULTIPLY Expand all.
5242 * VK_PRIOR Move up GetVisibleCount items.
5243 * VK_NEXT Move down GetVisibleCount items.
5244 * VK_BACK Move to parent.
5245 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5247 static LRESULT
5248 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5250 /* If it is non-NULL and different, it will be selected and visible. */
5251 TREEVIEW_ITEM *newSelection = NULL;
5252 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5253 NMTVKEYDOWN nmkeydown;
5255 TRACE("%lx\n", wParam);
5257 nmkeydown.wVKey = wParam;
5258 nmkeydown.flags = 0;
5259 TREEVIEW_SendRealNotify(infoPtr, TVN_KEYDOWN, &nmkeydown.hdr);
5261 if (prevItem == NULL)
5262 return FALSE;
5264 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5265 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5267 switch (wParam)
5269 case VK_UP:
5270 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5271 if (!newSelection)
5272 newSelection = infoPtr->root->firstChild;
5273 break;
5275 case VK_DOWN:
5276 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5277 break;
5279 case VK_RETURN:
5280 TREEVIEW_SendSimpleNotify(infoPtr, NM_RETURN);
5281 break;
5283 case VK_HOME:
5284 newSelection = infoPtr->root->firstChild;
5285 break;
5287 case VK_END:
5288 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5289 break;
5291 case VK_LEFT:
5292 if (prevItem->state & TVIS_EXPANDED)
5294 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5296 else if (prevItem->parent != infoPtr->root)
5298 newSelection = prevItem->parent;
5300 break;
5302 case VK_RIGHT:
5303 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5305 if (!(prevItem->state & TVIS_EXPANDED))
5306 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5307 else
5309 newSelection = prevItem->firstChild;
5313 break;
5315 case VK_MULTIPLY:
5316 TREEVIEW_ExpandAll(infoPtr, prevItem);
5317 break;
5319 case VK_ADD:
5320 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5321 break;
5323 case VK_SUBTRACT:
5324 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5325 break;
5327 case VK_PRIOR:
5328 newSelection
5329 = TREEVIEW_GetListItem(infoPtr, prevItem,
5330 -TREEVIEW_GetVisibleCount(infoPtr));
5331 break;
5333 case VK_NEXT:
5334 newSelection
5335 = TREEVIEW_GetListItem(infoPtr, prevItem,
5336 TREEVIEW_GetVisibleCount(infoPtr));
5337 break;
5339 case VK_BACK:
5340 newSelection = prevItem->parent;
5341 if (newSelection == infoPtr->root)
5342 newSelection = NULL;
5343 break;
5345 case VK_SPACE:
5346 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5347 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5348 break;
5351 if (newSelection && newSelection != prevItem)
5353 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5354 TVC_BYKEYBOARD))
5356 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5360 return FALSE;
5363 static LRESULT
5364 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5366 /* remove hot effect from item */
5367 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5368 infoPtr->hotItem = NULL;
5370 return 0;
5373 static LRESULT
5374 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5376 TRACKMOUSEEVENT trackinfo;
5377 TREEVIEW_ITEM * item;
5378 TVHITTESTINFO ht;
5379 BOOL item_hit;
5381 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5383 /* fill in the TRACKMOUSEEVENT struct */
5384 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5385 trackinfo.dwFlags = TME_QUERY;
5386 trackinfo.hwndTrack = infoPtr->hwnd;
5388 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5389 _TrackMouseEvent(&trackinfo);
5391 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5392 if(!(trackinfo.dwFlags & TME_LEAVE))
5394 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5395 trackinfo.hwndTrack = infoPtr->hwnd;
5396 /* do it as fast as possible, minimal systimer latency will be used */
5397 trackinfo.dwHoverTime = 1;
5399 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5400 /* and can properly deactivate the hot item */
5401 _TrackMouseEvent(&trackinfo);
5404 ht.pt.x = (short)LOWORD(lParam);
5405 ht.pt.y = (short)HIWORD(lParam);
5407 item = TREEVIEW_HitTest(infoPtr, &ht);
5408 item_hit = TREEVIEW_IsItemHit(infoPtr, &ht);
5409 if ((item != infoPtr->hotItem) || !item_hit)
5411 /* redraw old hot item */
5412 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5413 infoPtr->hotItem = NULL;
5414 if (item && item_hit)
5416 infoPtr->hotItem = item;
5417 /* redraw new hot item */
5418 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5422 return 0;
5425 /* Draw themed border */
5426 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5428 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5429 HDC dc;
5430 RECT r;
5431 HRGN cliprgn;
5432 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5433 cyEdge = GetSystemMetrics (SM_CYEDGE);
5435 if (!theme)
5436 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5438 GetWindowRect(infoPtr->hwnd, &r);
5440 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5441 r.right - cxEdge, r.bottom - cyEdge);
5442 if (region != (HRGN)1)
5443 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5444 OffsetRect(&r, -r.left, -r.top);
5446 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5447 OffsetRect(&r, -r.left, -r.top);
5449 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5450 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5451 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5452 ReleaseDC(infoPtr->hwnd, dc);
5454 /* Call default proc to get the scrollbars etc. painted */
5455 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5457 return TRUE;
5460 static LRESULT
5461 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5463 LPNMHDR lpnmh = (LPNMHDR)lParam;
5465 if (lpnmh->code == PGN_CALCSIZE) {
5466 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5468 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5469 lppgc->iWidth = infoPtr->treeWidth;
5470 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5471 infoPtr->treeWidth, infoPtr->clientWidth);
5473 else {
5474 lppgc->iHeight = infoPtr->treeHeight;
5475 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5476 infoPtr->treeHeight, infoPtr->clientHeight);
5478 return 0;
5480 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5483 static LRESULT
5484 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5486 if (wParam == SIZE_RESTORED)
5488 infoPtr->clientWidth = (short)LOWORD(lParam);
5489 infoPtr->clientHeight = (short)HIWORD(lParam);
5491 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5492 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5493 TREEVIEW_UpdateScrollBars(infoPtr);
5495 else
5497 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5500 TREEVIEW_Invalidate(infoPtr, NULL);
5501 return 0;
5504 static LRESULT
5505 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5507 TRACE("(%lx %lx)\n", wParam, lParam);
5509 if (wParam == GWL_STYLE)
5511 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5513 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5515 if (dwNewStyle & TVS_CHECKBOXES)
5517 TREEVIEW_InitCheckboxes(infoPtr);
5518 TRACE("checkboxes enabled\n");
5520 /* set all items to state image index 1 */
5521 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5523 else
5525 FIXME("tried to disable checkboxes\n");
5529 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5531 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5533 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5534 TRACE("tooltips enabled\n");
5536 else
5538 DestroyWindow(infoPtr->hwndToolTip);
5539 infoPtr->hwndToolTip = 0;
5540 TRACE("tooltips disabled\n");
5544 infoPtr->dwStyle = dwNewStyle;
5547 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
5548 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5549 TREEVIEW_UpdateScrollBars(infoPtr);
5550 TREEVIEW_Invalidate(infoPtr, NULL);
5552 return 0;
5555 static LRESULT
5556 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5558 TREEVIEW_ITEM * item;
5559 TVHITTESTINFO ht;
5560 NMMOUSE nmmouse;
5562 GetCursorPos(&ht.pt);
5563 ScreenToClient(infoPtr->hwnd, &ht.pt);
5565 item = TREEVIEW_HitTest(infoPtr, &ht);
5567 memset(&nmmouse, 0, sizeof(nmmouse));
5568 if (item)
5570 nmmouse.dwItemSpec = (DWORD_PTR)item;
5571 nmmouse.dwItemData = item->lParam;
5573 nmmouse.pt.x = 0;
5574 nmmouse.pt.y = 0;
5575 nmmouse.dwHitInfo = lParam;
5576 if (TREEVIEW_SendRealNotify(infoPtr, NM_SETCURSOR, &nmmouse.hdr))
5577 return 0;
5579 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT) && TREEVIEW_IsItemHit(infoPtr, &ht))
5581 SetCursor(infoPtr->hcurHand);
5582 return 0;
5584 else
5585 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5588 static LRESULT
5589 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5591 TRACE("\n");
5593 if (!infoPtr->selectedItem)
5595 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5596 TVC_UNKNOWN);
5599 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5600 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5601 return 0;
5604 static LRESULT
5605 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5607 TRACE("\n");
5609 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5610 UpdateWindow(infoPtr->hwnd);
5611 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5612 return 0;
5615 /* update theme after a WM_THEMECHANGED message */
5616 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5618 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5619 CloseThemeData (theme);
5620 OpenThemeData (infoPtr->hwnd, themeClass);
5621 return 0;
5625 static LRESULT WINAPI
5626 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5628 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5630 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5632 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5633 else
5635 if (uMsg == WM_CREATE)
5636 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5637 else
5638 goto def;
5641 switch (uMsg)
5643 case TVM_CREATEDRAGIMAGE:
5644 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5646 case TVM_DELETEITEM:
5647 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5649 case TVM_EDITLABELA:
5650 case TVM_EDITLABELW:
5651 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5653 case TVM_ENDEDITLABELNOW:
5654 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5656 case TVM_ENSUREVISIBLE:
5657 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5659 case TVM_EXPAND:
5660 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5662 case TVM_GETBKCOLOR:
5663 return TREEVIEW_GetBkColor(infoPtr);
5665 case TVM_GETCOUNT:
5666 return TREEVIEW_GetCount(infoPtr);
5668 case TVM_GETEDITCONTROL:
5669 return TREEVIEW_GetEditControl(infoPtr);
5671 case TVM_GETIMAGELIST:
5672 return TREEVIEW_GetImageList(infoPtr, wParam);
5674 case TVM_GETINDENT:
5675 return TREEVIEW_GetIndent(infoPtr);
5677 case TVM_GETINSERTMARKCOLOR:
5678 return TREEVIEW_GetInsertMarkColor(infoPtr);
5680 case TVM_GETISEARCHSTRINGA:
5681 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5682 return 0;
5684 case TVM_GETISEARCHSTRINGW:
5685 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5686 return 0;
5688 case TVM_GETITEMA:
5689 case TVM_GETITEMW:
5690 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5691 uMsg == TVM_GETITEMW);
5692 case TVM_GETITEMHEIGHT:
5693 return TREEVIEW_GetItemHeight(infoPtr);
5695 case TVM_GETITEMRECT:
5696 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5698 case TVM_GETITEMSTATE:
5699 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5701 case TVM_GETLINECOLOR:
5702 return TREEVIEW_GetLineColor(infoPtr);
5704 case TVM_GETNEXTITEM:
5705 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5707 case TVM_GETSCROLLTIME:
5708 return TREEVIEW_GetScrollTime(infoPtr);
5710 case TVM_GETTEXTCOLOR:
5711 return TREEVIEW_GetTextColor(infoPtr);
5713 case TVM_GETTOOLTIPS:
5714 return TREEVIEW_GetToolTips(infoPtr);
5716 case TVM_GETUNICODEFORMAT:
5717 return TREEVIEW_GetUnicodeFormat(infoPtr);
5719 case TVM_GETVISIBLECOUNT:
5720 return TREEVIEW_GetVisibleCount(infoPtr);
5722 case TVM_HITTEST:
5723 return (LRESULT)TREEVIEW_HitTest(infoPtr, (TVHITTESTINFO*)lParam);
5725 case TVM_INSERTITEMA:
5726 case TVM_INSERTITEMW:
5727 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5728 uMsg == TVM_INSERTITEMW);
5729 case TVM_SELECTITEM:
5730 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5732 case TVM_SETBKCOLOR:
5733 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5735 case TVM_SETIMAGELIST:
5736 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5738 case TVM_SETINDENT:
5739 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5741 case TVM_SETINSERTMARK:
5742 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5744 case TVM_SETINSERTMARKCOLOR:
5745 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5747 case TVM_SETITEMA:
5748 case TVM_SETITEMW:
5749 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5750 uMsg == TVM_SETITEMW);
5751 case TVM_SETLINECOLOR:
5752 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5754 case TVM_SETITEMHEIGHT:
5755 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5757 case TVM_SETSCROLLTIME:
5758 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5760 case TVM_SETTEXTCOLOR:
5761 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5763 case TVM_SETTOOLTIPS:
5764 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5766 case TVM_SETUNICODEFORMAT:
5767 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5769 case TVM_SORTCHILDREN:
5770 return TREEVIEW_SortChildren(infoPtr, lParam);
5772 case TVM_SORTCHILDRENCB:
5773 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5775 case WM_CHAR:
5776 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5778 case WM_COMMAND:
5779 return TREEVIEW_Command(infoPtr, wParam, lParam);
5781 case WM_DESTROY:
5782 return TREEVIEW_Destroy(infoPtr);
5784 /* WM_ENABLE */
5786 case WM_ERASEBKGND:
5787 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5789 case WM_GETDLGCODE:
5790 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5792 case WM_GETFONT:
5793 return TREEVIEW_GetFont(infoPtr);
5795 case WM_HSCROLL:
5796 return TREEVIEW_HScroll(infoPtr, wParam);
5798 case WM_KEYDOWN:
5799 case WM_SYSKEYDOWN:
5800 return TREEVIEW_KeyDown(infoPtr, wParam);
5802 case WM_KILLFOCUS:
5803 return TREEVIEW_KillFocus(infoPtr);
5805 case WM_LBUTTONDBLCLK:
5806 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5808 case WM_LBUTTONDOWN:
5809 return TREEVIEW_LButtonDown(infoPtr, lParam);
5811 /* WM_MBUTTONDOWN */
5813 case WM_MOUSELEAVE:
5814 return TREEVIEW_MouseLeave(infoPtr);
5816 case WM_MOUSEMOVE:
5817 return TREEVIEW_MouseMove(infoPtr, lParam);
5819 case WM_NCLBUTTONDOWN:
5820 if (infoPtr->hwndEdit)
5821 SetFocus(infoPtr->hwnd);
5822 goto def;
5824 case WM_NCPAINT:
5825 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5827 case WM_NOTIFY:
5828 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5830 case WM_NOTIFYFORMAT:
5831 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5833 case WM_PRINTCLIENT:
5834 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5836 case WM_PAINT:
5837 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5839 case WM_RBUTTONDOWN:
5840 return TREEVIEW_RButtonDown(infoPtr, lParam);
5842 case WM_SETCURSOR:
5843 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5845 case WM_SETFOCUS:
5846 return TREEVIEW_SetFocus(infoPtr);
5848 case WM_SETFONT:
5849 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5851 case WM_SETREDRAW:
5852 return TREEVIEW_SetRedraw(infoPtr, wParam);
5854 case WM_SIZE:
5855 return TREEVIEW_Size(infoPtr, wParam, lParam);
5857 case WM_STYLECHANGED:
5858 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5860 case WM_SYSCOLORCHANGE:
5861 COMCTL32_RefreshSysColors();
5862 return 0;
5864 case WM_TIMER:
5865 return TREEVIEW_HandleTimer(infoPtr, wParam);
5867 case WM_THEMECHANGED:
5868 return TREEVIEW_ThemeChanged (infoPtr);
5870 case WM_VSCROLL:
5871 return TREEVIEW_VScroll(infoPtr, wParam);
5873 /* WM_WININICHANGE */
5875 case WM_MOUSEWHEEL:
5876 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5878 case WM_DRAWITEM:
5879 TRACE("drawItem\n");
5880 goto def;
5882 default:
5883 /* This mostly catches MFC and Delphi messages. :( */
5884 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5885 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5886 def:
5887 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5892 /* Class Registration ***************************************************/
5894 VOID
5895 TREEVIEW_Register(void)
5897 WNDCLASSW wndClass;
5899 TRACE("\n");
5901 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5902 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5903 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5904 wndClass.cbClsExtra = 0;
5905 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5907 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5908 wndClass.hbrBackground = 0;
5909 wndClass.lpszClassName = WC_TREEVIEWW;
5911 RegisterClassW(&wndClass);
5915 VOID
5916 TREEVIEW_Unregister(void)
5918 UnregisterClassW(WC_TREEVIEWW, NULL);
5922 /* Tree Verification ****************************************************/
5924 static inline void
5925 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5927 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5928 const TREEVIEW_ITEM *item)
5930 assert(infoPtr != NULL);
5931 assert(item != NULL);
5933 /* both NULL, or both non-null */
5934 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5936 assert(item->firstChild != item);
5937 assert(item->lastChild != item);
5939 if (item->firstChild)
5941 assert(item->firstChild->parent == item);
5942 assert(item->firstChild->prevSibling == NULL);
5945 if (item->lastChild)
5947 assert(item->lastChild->parent == item);
5948 assert(item->lastChild->nextSibling == NULL);
5951 assert(item->nextSibling != item);
5952 if (item->nextSibling)
5954 assert(item->nextSibling->parent == item->parent);
5955 assert(item->nextSibling->prevSibling == item);
5958 assert(item->prevSibling != item);
5959 if (item->prevSibling)
5961 assert(item->prevSibling->parent == item->parent);
5962 assert(item->prevSibling->nextSibling == item);
5966 static inline void
5967 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5969 assert(item != NULL);
5971 assert(item->parent != NULL);
5972 assert(item->parent != item);
5973 assert(item->iLevel == item->parent->iLevel + 1);
5975 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5977 TREEVIEW_VerifyItemCommon(infoPtr, item);
5979 TREEVIEW_VerifyChildren(infoPtr, item);
5982 static inline void
5983 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5985 const TREEVIEW_ITEM *child;
5986 assert(item != NULL);
5988 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5989 TREEVIEW_VerifyItem(infoPtr, child);
5992 static inline void
5993 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5995 TREEVIEW_ITEM *root = infoPtr->root;
5997 assert(root != NULL);
5998 assert(root->iLevel == -1);
5999 assert(root->parent == NULL);
6000 assert(root->prevSibling == NULL);
6002 TREEVIEW_VerifyItemCommon(infoPtr, root);
6004 TREEVIEW_VerifyChildren(infoPtr, root);
6007 static void
6008 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
6010 if (!TRACE_ON(treeview)) return;
6012 assert(infoPtr != NULL);
6013 TREEVIEW_VerifyRoot(infoPtr);