api-ms-win-core-comm-l1-1-0: Add dll.
[wine.git] / dlls / comctl32 / treeview.c
blobbbef651597cd1f42286498649b4973166f593dd3
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"
67 #include "wine/heap.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
71 /* internal structures */
72 typedef struct tagTREEVIEW_INFO
74 HWND hwnd;
75 HWND hwndNotify; /* Owner window to send notifications to */
76 DWORD dwStyle;
77 HTREEITEM root;
78 UINT uInternalStatus;
79 INT Timer;
80 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
81 INT cdmode; /* last custom draw setting */
82 UINT uScrollTime; /* max. time for scrolling in milliseconds */
83 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
85 UINT uItemHeight; /* item height */
86 BOOL bHeightSet;
88 LONG clientWidth; /* width of control window */
89 LONG clientHeight; /* height of control window */
91 LONG treeWidth; /* width of visible tree items */
92 LONG treeHeight; /* height of visible tree items */
94 UINT uIndent; /* indentation in pixels */
95 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
96 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
97 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
98 HTREEITEM editItem; /* item being edited with builtin edit box */
100 HTREEITEM firstVisible; /* handle to item whose top edge is at y = 0 */
101 LONG maxVisibleOrder;
102 HTREEITEM dropItem; /* handle to item selected by drag cursor */
103 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
104 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
105 HIMAGELIST dragList; /* Bitmap of dragged item */
106 LONG scrollX;
107 INT wheelRemainder;
108 COLORREF clrBk;
109 COLORREF clrText;
110 COLORREF clrLine;
111 COLORREF clrInsertMark;
112 HFONT hFont;
113 HFONT hDefaultFont;
114 HFONT hBoldFont;
115 HFONT hUnderlineFont;
116 HFONT hBoldUnderlineFont;
117 HCURSOR hcurHand;
118 HWND hwndToolTip;
120 HWND hwndEdit;
121 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
122 BOOL bIgnoreEditKillFocus;
123 BOOL bLabelChanged;
125 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
126 HIMAGELIST himlNormal;
127 int normalImageHeight;
128 int normalImageWidth;
129 HIMAGELIST himlState;
130 int stateImageHeight;
131 int stateImageWidth;
132 HDPA items;
134 DWORD lastKeyPressTimestamp;
135 WPARAM charCode;
136 INT nSearchParamLength;
137 WCHAR szSearchParam[ MAX_PATH ];
138 } TREEVIEW_INFO;
140 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
142 HTREEITEM parent; /* handle to parent or 0 if at root */
143 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
144 HTREEITEM firstChild; /* handle to first child or 0 if no child */
146 UINT callbackMask;
147 UINT state;
148 UINT stateMask;
149 LPWSTR pszText;
150 int cchTextMax;
151 int iImage;
152 int iSelectedImage;
153 int iExpandedImage;
154 int cChildren;
155 LPARAM lParam;
156 int iIntegral; /* item height multiplier (1 is normal) */
157 int iLevel; /* indentation level:0=root level */
158 HTREEITEM lastChild;
159 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
160 RECT rect;
161 LONG linesOffset;
162 LONG stateOffset;
163 LONG imageOffset;
164 LONG textOffset;
165 LONG textWidth; /* horizontal text extent for pszText */
166 LONG visibleOrder; /* Depth-first numbering of the items whose ancestors are all expanded,
167 corresponding to a top-to-bottom ordering in the tree view.
168 Each item takes up "item.iIntegral" spots in the visible order.
169 0 is the root's first child. */
170 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
171 } TREEVIEW_ITEM;
173 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
174 #define KEY_DELAY 450
176 /* bitflags for infoPtr->uInternalStatus */
178 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
179 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
180 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
181 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
182 #define TV_RDRAG 0x10 /* ditto Rbutton */
183 #define TV_RDRAGGING 0x20
185 /* bitflags for infoPtr->timer */
187 #define TV_EDIT_TIMER 2
188 #define TV_EDIT_TIMER_SET 2
190 #define TEXT_CALLBACK_SIZE 260
192 #define TREEVIEW_LEFT_MARGIN 8
194 #define MINIMUM_INDENT 19
196 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
198 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
199 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
200 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
202 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
203 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
204 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
205 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
207 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
210 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
213 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
215 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
216 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
217 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
218 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
219 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
220 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
222 /* Random Utilities *****************************************************/
223 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
225 /* Returns the treeview private data if hwnd is a treeview.
226 * Otherwise returns an undefined value. */
227 static inline TREEVIEW_INFO *
228 TREEVIEW_GetInfoPtr(HWND hwnd)
230 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
233 /* Don't call this. Nothing wants an item index. */
234 static inline int
235 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
237 return DPA_GetPtrIndex(infoPtr->items, handle);
240 /* Checks if item has changed and needs to be redrawn */
241 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
242 const TVITEMEXW *tvChange)
244 /* Number of children has changed */
245 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
246 return TRUE;
248 /* Image has changed and it's not a callback */
249 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
250 tiNew->iImage != I_IMAGECALLBACK)
251 return TRUE;
253 /* Selected image has changed and it's not a callback */
254 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
255 tiNew->iSelectedImage != I_IMAGECALLBACK)
256 return TRUE;
258 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
259 tiNew->iExpandedImage != I_IMAGECALLBACK)
260 return TRUE;
262 /* Text has changed and it's not a callback */
263 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
264 tiNew->pszText != LPSTR_TEXTCALLBACKW)
265 return TRUE;
267 /* Indent has changed */
268 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
269 return TRUE;
271 /* Item state has changed */
272 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
273 return TRUE;
275 return FALSE;
278 /***************************************************************************
279 * This method checks that handle is an item for this tree.
281 static BOOL
282 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
284 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
286 TRACE("invalid item %p\n", handle);
287 return FALSE;
289 else
290 return TRUE;
293 static HFONT
294 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
296 LOGFONTW font;
298 GetObjectW(hOrigFont, sizeof(font), &font);
299 font.lfWeight = FW_BOLD;
300 return CreateFontIndirectW(&font);
303 static HFONT
304 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
306 LOGFONTW font;
308 GetObjectW(hOrigFont, sizeof(font), &font);
309 font.lfUnderline = TRUE;
310 return CreateFontIndirectW(&font);
313 static HFONT
314 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont)
316 LOGFONTW font;
318 GetObjectW(hfont, sizeof(font), &font);
319 font.lfWeight = FW_BOLD;
320 font.lfUnderline = TRUE;
321 return CreateFontIndirectW(&font);
324 static inline HFONT
325 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
327 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
328 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
329 if (item->state & TVIS_BOLD)
330 return infoPtr->hBoldFont;
331 return infoPtr->hFont;
334 /* for trace/debugging purposes only */
335 static const char *
336 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
338 if (item == NULL) return "<null item>";
339 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
340 if (item->pszText == NULL) return "<null>";
341 return debugstr_w(item->pszText);
344 /* An item is not a child of itself. */
345 static BOOL
346 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
350 child = child->parent;
351 if (child == parent) return TRUE;
352 } while (child != NULL);
354 return FALSE;
357 static BOOL
358 TREEVIEW_IsFullRowSelect(const TREEVIEW_INFO *infoPtr)
360 return !(infoPtr->dwStyle & TVS_HASLINES) && (infoPtr->dwStyle & TVS_FULLROWSELECT);
363 static BOOL
364 TREEVIEW_IsItemHit(const TREEVIEW_INFO *infoPtr, const TVHITTESTINFO *ht)
366 if (TREEVIEW_IsFullRowSelect(infoPtr))
367 return ht->flags & (TVHT_ONITEMINDENT | TVHT_ONITEMBUTTON | TVHT_ONITEM | TVHT_ONITEMRIGHT);
368 else
369 return ht->flags & TVHT_ONITEM;
372 /* Tree Traversal *******************************************************/
374 /***************************************************************************
375 * This method returns the last expanded sibling or child child item
376 * of a tree node
378 static TREEVIEW_ITEM *
379 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
381 if (!item) return NULL;
383 while (item->lastChild)
385 if (item->state & TVIS_EXPANDED)
386 item = item->lastChild;
387 else
388 break;
391 if (item == infoPtr->root)
392 return NULL;
394 return item;
397 /***************************************************************************
398 * This method returns the previous non-hidden item in the list not
399 * considering the tree hierarchy.
401 static TREEVIEW_ITEM *
402 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
404 if (tvItem->prevSibling)
406 /* This item has a prevSibling, get the last item in the sibling's tree. */
407 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
409 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
410 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
411 else
412 return upItem;
414 else
416 /* this item does not have a prevSibling, get the parent */
417 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
422 /***************************************************************************
423 * This method returns the next physical item in the treeview not
424 * considering the tree hierarchy.
426 static TREEVIEW_ITEM *
427 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
430 * If this item has children and is expanded, return the first child
432 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
434 return tvItem->firstChild;
439 * try to get the sibling
441 if (tvItem->nextSibling)
442 return tvItem->nextSibling;
445 * Otherwise, get the parent's sibling.
447 while (tvItem->parent)
449 tvItem = tvItem->parent;
451 if (tvItem->nextSibling)
452 return tvItem->nextSibling;
455 return NULL;
458 /***************************************************************************
459 * This method returns the nth item starting at the given item. It returns
460 * the last item (or first) we we run out of items.
462 * Will scroll backward if count is <0.
463 * forward if count is >0.
465 static TREEVIEW_ITEM *
466 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
467 LONG count)
469 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
470 TREEVIEW_ITEM *previousItem;
472 assert(item != NULL);
474 if (count > 0)
476 next_item = TREEVIEW_GetNextListItem;
478 else if (count < 0)
480 count = -count;
481 next_item = TREEVIEW_GetPrevListItem;
483 else
484 return item;
488 previousItem = item;
489 item = next_item(infoPtr, item);
491 } while (--count && item != NULL);
494 return item ? item : previousItem;
497 /* Notifications ************************************************************/
499 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
501 if (!infoPtr->bNtfUnicode) {
502 switch (code) {
503 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
504 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
505 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
506 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
507 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
508 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
509 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
510 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
511 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
512 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
513 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
514 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
517 return code;
520 static inline BOOL
521 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, UINT code, NMHDR *hdr)
523 TRACE("code=%d, hdr=%p\n", code, hdr);
525 hdr->hwndFrom = infoPtr->hwnd;
526 hdr->idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
527 hdr->code = get_notifycode(infoPtr, code);
529 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, hdr->idFrom, (LPARAM)hdr);
532 static BOOL
533 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
535 NMHDR hdr;
536 return TREEVIEW_SendRealNotify(infoPtr, code, &hdr);
539 static VOID
540 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
542 tvItem->mask = mask;
543 tvItem->hItem = item;
544 tvItem->state = item->state;
545 tvItem->stateMask = 0;
546 tvItem->iImage = item->iImage;
547 tvItem->iSelectedImage = item->iSelectedImage;
548 tvItem->cChildren = item->cChildren;
549 tvItem->lParam = item->lParam;
551 if(mask & TVIF_TEXT)
553 if (!infoPtr->bNtfUnicode)
555 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
556 tvItem->pszText = heap_alloc (tvItem->cchTextMax);
557 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
559 else
561 tvItem->cchTextMax = item->cchTextMax;
562 tvItem->pszText = item->pszText;
565 else
567 tvItem->cchTextMax = 0;
568 tvItem->pszText = NULL;
572 static BOOL
573 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
574 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
576 NMTREEVIEWW nmhdr;
577 BOOL ret;
579 TRACE("code:%d action:0x%x olditem:%p newitem:%p\n",
580 code, action, oldItem, newItem);
582 memset(&nmhdr, 0, sizeof(NMTREEVIEWW));
583 nmhdr.action = action;
585 if (oldItem)
586 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
588 if (newItem)
589 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
591 nmhdr.ptDrag.x = 0;
592 nmhdr.ptDrag.y = 0;
594 ret = TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
595 if (!infoPtr->bNtfUnicode)
597 heap_free(nmhdr.itemOld.pszText);
598 heap_free(nmhdr.itemNew.pszText);
600 return ret;
603 static BOOL
604 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
605 HTREEITEM dragItem, POINT pt)
607 NMTREEVIEWW nmhdr;
609 TRACE("code:%d dragitem:%p\n", code, dragItem);
611 nmhdr.action = 0;
612 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
613 nmhdr.itemNew.hItem = dragItem;
614 nmhdr.itemNew.state = dragItem->state;
615 nmhdr.itemNew.lParam = dragItem->lParam;
617 nmhdr.ptDrag.x = pt.x;
618 nmhdr.ptDrag.y = pt.y;
620 return TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
624 static BOOL
625 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
626 HDC hdc, RECT rc)
628 NMTVCUSTOMDRAW nmcdhdr;
629 NMCUSTOMDRAW *nmcd;
631 TRACE("drawstage:0x%x hdc:%p\n", dwDrawStage, hdc);
633 nmcd = &nmcdhdr.nmcd;
634 nmcd->dwDrawStage = dwDrawStage;
635 nmcd->hdc = hdc;
636 nmcd->rc = rc;
637 nmcd->dwItemSpec = 0;
638 nmcd->uItemState = 0;
639 nmcd->lItemlParam = 0;
640 nmcdhdr.clrText = infoPtr->clrText;
641 nmcdhdr.clrTextBk = infoPtr->clrBk;
642 nmcdhdr.iLevel = 0;
644 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr.nmcd.hdr);
647 /* FIXME: need to find out when the flags in uItemState need to be set */
649 static BOOL
650 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
651 TREEVIEW_ITEM *item, UINT uItemDrawState,
652 NMTVCUSTOMDRAW *nmcdhdr)
654 NMCUSTOMDRAW *nmcd;
655 DWORD dwDrawStage;
656 DWORD_PTR dwItemSpec;
657 UINT uItemState;
659 dwDrawStage = CDDS_ITEM | uItemDrawState;
660 dwItemSpec = (DWORD_PTR)item;
661 uItemState = 0;
662 if (item->state & TVIS_SELECTED)
663 uItemState |= CDIS_SELECTED;
664 if (item == infoPtr->selectedItem)
665 uItemState |= CDIS_FOCUS;
666 if (item == infoPtr->hotItem)
667 uItemState |= CDIS_HOT;
669 nmcd = &nmcdhdr->nmcd;
670 nmcd->dwDrawStage = dwDrawStage;
671 nmcd->hdc = hdc;
672 nmcd->rc = item->rect;
673 nmcd->dwItemSpec = dwItemSpec;
674 nmcd->uItemState = uItemState;
675 nmcd->lItemlParam = item->lParam;
676 nmcdhdr->iLevel = item->iLevel;
678 TRACE("drawstage:0x%x hdc:%p item:%lx, itemstate:0x%x, lItemlParam:0x%lx\n",
679 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
680 nmcd->uItemState, nmcd->lItemlParam);
682 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr->nmcd.hdr);
685 static BOOL
686 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
688 NMTVDISPINFOW tvdi;
689 BOOL ret;
691 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
692 &tvdi.item, editItem);
694 ret = TREEVIEW_SendRealNotify(infoPtr, TVN_BEGINLABELEDITW, &tvdi.hdr);
696 if (!infoPtr->bNtfUnicode)
697 heap_free(tvdi.item.pszText);
699 return ret;
702 static void
703 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
704 UINT mask)
706 NMTVDISPINFOEXW callback;
708 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
709 mask &= item->callbackMask;
711 if (mask == 0) return;
713 /* 'state' always contains valid value, as well as 'lParam'.
714 * All other parameters are uninitialized.
716 callback.item.pszText = item->pszText;
717 callback.item.cchTextMax = item->cchTextMax;
718 callback.item.mask = mask;
719 callback.item.hItem = item;
720 callback.item.state = item->state;
721 callback.item.lParam = item->lParam;
723 /* If text is changed we need to recalculate textWidth */
724 if (mask & TVIF_TEXT)
725 item->textWidth = 0;
727 TREEVIEW_SendRealNotify(infoPtr, TVN_GETDISPINFOW, &callback.hdr);
728 TRACE("resulting code 0x%08x\n", callback.hdr.code);
730 /* It may have changed due to a call to SetItem. */
731 mask &= item->callbackMask;
733 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
735 /* Instead of copying text into our buffer user specified his own */
736 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
737 LPWSTR newText;
738 int buflen;
739 int len = MultiByteToWideChar( CP_ACP, 0,
740 (LPSTR)callback.item.pszText, -1,
741 NULL, 0);
742 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
743 newText = heap_realloc(item->pszText, buflen);
745 TRACE("returned str %s, len=%d, buflen=%d\n",
746 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
748 if (newText)
750 item->pszText = newText;
751 MultiByteToWideChar( CP_ACP, 0,
752 (LPSTR)callback.item.pszText, -1,
753 item->pszText, buflen/sizeof(WCHAR));
754 item->cchTextMax = buflen/sizeof(WCHAR);
756 /* If realloc fails we have nothing to do, but keep original text */
758 else {
759 int len = max(lstrlenW(callback.item.pszText) + 1,
760 TEXT_CALLBACK_SIZE);
761 LPWSTR newText = heap_realloc(item->pszText, len);
763 TRACE("returned wstr %s, len=%d\n",
764 debugstr_w(callback.item.pszText), len);
766 if (newText)
768 item->pszText = newText;
769 strcpyW(item->pszText, callback.item.pszText);
770 item->cchTextMax = len;
772 /* If realloc fails we have nothing to do, but keep original text */
775 else if (mask & TVIF_TEXT) {
776 /* User put text into our buffer, that is ok unless A string */
777 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
778 LPWSTR newText;
779 int buflen;
780 int len = MultiByteToWideChar( CP_ACP, 0,
781 (LPSTR)callback.item.pszText, -1,
782 NULL, 0);
783 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
784 newText = heap_alloc(buflen);
786 TRACE("same buffer str %s, len=%d, buflen=%d\n",
787 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
789 if (newText)
791 LPWSTR oldText = item->pszText;
792 item->pszText = newText;
793 MultiByteToWideChar( CP_ACP, 0,
794 (LPSTR)callback.item.pszText, -1,
795 item->pszText, buflen/sizeof(WCHAR));
796 item->cchTextMax = buflen/sizeof(WCHAR);
797 heap_free(oldText);
802 if (mask & TVIF_IMAGE)
803 item->iImage = callback.item.iImage;
805 if (mask & TVIF_SELECTEDIMAGE)
806 item->iSelectedImage = callback.item.iSelectedImage;
808 if (mask & TVIF_EXPANDEDIMAGE)
809 item->iExpandedImage = callback.item.iExpandedImage;
811 if (mask & TVIF_CHILDREN)
812 item->cChildren = callback.item.cChildren;
814 if (callback.item.mask & TVIF_STATE)
816 item->state &= ~callback.item.stateMask;
817 item->state |= (callback.item.state & callback.item.stateMask);
820 /* These members are now permanently set. */
821 if (callback.item.mask & TVIF_DI_SETITEM)
822 item->callbackMask &= ~callback.item.mask;
825 /***************************************************************************
826 * This function uses cChildren field to decide whether the item has
827 * children or not.
828 * Note: if this returns TRUE, the child items may not actually exist,
829 * they could be virtual.
831 * Just use item->firstChild to check for physical children.
833 static BOOL
834 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
836 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
837 /* Protect for a case when callback field is not changed by a host,
838 otherwise negative values trigger normal notifications. */
839 return item->cChildren != 0 && item->cChildren != I_CHILDRENCALLBACK;
842 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
844 INT format;
846 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
848 if (nCommand != NF_REQUERY) return 0;
850 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
851 TRACE("format=%d\n", format);
853 /* Invalid format returned by NF_QUERY defaults to ANSI*/
854 if (format != NFR_ANSI && format != NFR_UNICODE)
855 format = NFR_ANSI;
857 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
859 return format;
862 /* Item Position ********************************************************/
864 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
865 static VOID
866 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
868 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
869 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
870 > TVS_LINESATROOT);
872 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
873 - infoPtr->scrollX;
874 item->stateOffset = item->linesOffset + infoPtr->uIndent;
875 item->imageOffset = item->stateOffset
876 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
877 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
880 static VOID
881 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
883 HDC hdc;
884 HFONT hOldFont=0;
885 SIZE sz;
887 /* DRAW's OM docker creates items like this */
888 if (item->pszText == NULL)
890 item->textWidth = 0;
891 return;
894 if (hDC != 0)
896 hdc = hDC;
898 else
900 hdc = GetDC(infoPtr->hwnd);
901 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
904 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
905 item->textWidth = sz.cx;
907 if (hDC == 0)
909 SelectObject(hdc, hOldFont);
910 ReleaseDC(0, hdc);
914 static VOID
915 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
917 item->rect.top = infoPtr->uItemHeight *
918 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
920 item->rect.bottom = item->rect.top
921 + infoPtr->uItemHeight * item->iIntegral - 1;
923 item->rect.left = 0;
924 item->rect.right = infoPtr->clientWidth;
927 /* We know that only items after start need their order updated. */
928 static void
929 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
931 TREEVIEW_ITEM *item;
932 int order;
934 if (!start)
936 start = infoPtr->root->firstChild;
937 order = 0;
939 else
940 order = start->visibleOrder;
942 for (item = start; item != NULL;
943 item = TREEVIEW_GetNextListItem(infoPtr, item))
945 if (!ISVISIBLE(item) && order > 0)
946 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
947 item->visibleOrder = order;
948 order += item->iIntegral;
951 infoPtr->maxVisibleOrder = order;
953 for (item = infoPtr->root->firstChild; item != NULL;
954 item = TREEVIEW_GetNextListItem(infoPtr, item))
956 TREEVIEW_ComputeItemRect(infoPtr, item);
961 /* Update metrics of all items in selected subtree.
962 * root must be expanded
964 static VOID
965 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
967 TREEVIEW_ITEM *sibling;
968 HDC hdc;
969 HFONT hOldFont;
971 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
972 return;
974 root->state &= ~TVIS_EXPANDED;
975 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
976 root->state |= TVIS_EXPANDED;
978 hdc = GetDC(infoPtr->hwnd);
979 hOldFont = SelectObject(hdc, infoPtr->hFont);
981 for (; root != sibling;
982 root = TREEVIEW_GetNextListItem(infoPtr, root))
984 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
986 if (root->callbackMask & TVIF_TEXT)
987 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
989 if (root->textWidth == 0)
991 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
992 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
996 SelectObject(hdc, hOldFont);
997 ReleaseDC(infoPtr->hwnd, hdc);
1000 /* Item Allocation **********************************************************/
1002 static TREEVIEW_ITEM *
1003 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1005 TREEVIEW_ITEM *newItem = heap_alloc_zero(sizeof(*newItem));
1007 if (!newItem)
1008 return NULL;
1010 /* I_IMAGENONE would make more sense but this is neither what is
1011 * documented (MSDN doesn't specify) nor what Windows actually does
1012 * (it sets it to zero)... and I can so imagine an application using
1013 * inc/dec to toggle the images. */
1014 newItem->iImage = 0;
1015 newItem->iSelectedImage = 0;
1016 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1017 newItem->infoPtr = infoPtr;
1019 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1021 heap_free(newItem);
1022 return NULL;
1025 return newItem;
1028 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1029 * free item->pszText. */
1030 static void
1031 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1033 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1034 if (infoPtr->selectedItem == item)
1035 infoPtr->selectedItem = NULL;
1036 if (infoPtr->hotItem == item)
1037 infoPtr->hotItem = NULL;
1038 if (infoPtr->focusedItem == item)
1039 infoPtr->focusedItem = NULL;
1040 if (infoPtr->firstVisible == item)
1041 infoPtr->firstVisible = NULL;
1042 if (infoPtr->dropItem == item)
1043 infoPtr->dropItem = NULL;
1044 if (infoPtr->insertMarkItem == item)
1045 infoPtr->insertMarkItem = NULL;
1046 heap_free(item);
1050 /* Item Insertion *******************************************************/
1052 /***************************************************************************
1053 * This method inserts newItem before sibling as a child of parent.
1054 * sibling can be NULL, but only if parent has no children.
1056 static void
1057 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1058 TREEVIEW_ITEM *parent)
1060 assert(parent != NULL);
1062 if (sibling != NULL)
1064 assert(sibling->parent == parent);
1066 if (sibling->prevSibling != NULL)
1067 sibling->prevSibling->nextSibling = newItem;
1069 newItem->prevSibling = sibling->prevSibling;
1070 sibling->prevSibling = newItem;
1072 else
1073 newItem->prevSibling = NULL;
1075 newItem->nextSibling = sibling;
1077 if (parent->firstChild == sibling)
1078 parent->firstChild = newItem;
1080 if (parent->lastChild == NULL)
1081 parent->lastChild = newItem;
1084 /***************************************************************************
1085 * This method inserts newItem after sibling as a child of parent.
1086 * sibling can be NULL, but only if parent has no children.
1088 static void
1089 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1090 TREEVIEW_ITEM *parent)
1092 assert(parent != NULL);
1094 if (sibling != NULL)
1096 assert(sibling->parent == parent);
1098 if (sibling->nextSibling != NULL)
1099 sibling->nextSibling->prevSibling = newItem;
1101 newItem->nextSibling = sibling->nextSibling;
1102 sibling->nextSibling = newItem;
1104 else
1105 newItem->nextSibling = NULL;
1107 newItem->prevSibling = sibling;
1109 if (parent->lastChild == sibling)
1110 parent->lastChild = newItem;
1112 if (parent->firstChild == NULL)
1113 parent->firstChild = newItem;
1116 static BOOL
1117 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1118 const TVITEMEXW *tvItem, BOOL isW)
1120 UINT callbackClear = 0;
1121 UINT callbackSet = 0;
1123 TRACE("item %p\n", item);
1124 /* Do this first in case it fails. */
1125 if (tvItem->mask & TVIF_TEXT)
1127 item->textWidth = 0; /* force width recalculation */
1129 /* Covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1130 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL)
1132 int len;
1133 LPWSTR newText;
1134 if (isW)
1135 len = lstrlenW(tvItem->pszText) + 1;
1136 else
1137 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1139 /* Allocate new block to make pointer comparison in item_changed() work. */
1140 newText = heap_alloc(len * sizeof(WCHAR));
1142 if (newText == NULL) return FALSE;
1144 callbackClear |= TVIF_TEXT;
1146 heap_free(item->pszText);
1147 item->pszText = newText;
1148 item->cchTextMax = len;
1149 if (isW)
1150 lstrcpynW(item->pszText, tvItem->pszText, len);
1151 else
1152 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1153 item->pszText, len);
1155 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1157 else
1159 callbackSet |= TVIF_TEXT;
1160 item->pszText = heap_realloc(item->pszText, TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1161 item->cchTextMax = TEXT_CALLBACK_SIZE;
1162 TRACE("setting callback, item %p\n", item);
1166 if (tvItem->mask & TVIF_CHILDREN)
1168 item->cChildren = tvItem->cChildren;
1170 if (item->cChildren == I_CHILDRENCALLBACK)
1171 callbackSet |= TVIF_CHILDREN;
1172 else
1173 callbackClear |= TVIF_CHILDREN;
1176 if (tvItem->mask & TVIF_IMAGE)
1178 item->iImage = tvItem->iImage;
1180 if (item->iImage == I_IMAGECALLBACK)
1181 callbackSet |= TVIF_IMAGE;
1182 else
1183 callbackClear |= TVIF_IMAGE;
1186 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1188 item->iSelectedImage = tvItem->iSelectedImage;
1190 if (item->iSelectedImage == I_IMAGECALLBACK)
1191 callbackSet |= TVIF_SELECTEDIMAGE;
1192 else
1193 callbackClear |= TVIF_SELECTEDIMAGE;
1196 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1198 item->iExpandedImage = tvItem->iExpandedImage;
1200 if (item->iExpandedImage == I_IMAGECALLBACK)
1201 callbackSet |= TVIF_EXPANDEDIMAGE;
1202 else
1203 callbackClear |= TVIF_EXPANDEDIMAGE;
1206 if (tvItem->mask & TVIF_PARAM)
1207 item->lParam = tvItem->lParam;
1209 /* If the application sets TVIF_INTEGRAL without
1210 * supplying a TVITEMEX structure, it's toast. */
1211 if (tvItem->mask & TVIF_INTEGRAL)
1212 item->iIntegral = tvItem->iIntegral;
1214 if (tvItem->mask & TVIF_STATE)
1216 TRACE("prevstate 0x%x, state 0x%x, mask 0x%x\n", item->state, tvItem->state,
1217 tvItem->stateMask);
1218 item->state &= ~tvItem->stateMask;
1219 item->state |= (tvItem->state & tvItem->stateMask);
1222 if (tvItem->mask & TVIF_STATEEX)
1224 FIXME("New extended state: 0x%x\n", tvItem->uStateEx);
1227 item->callbackMask |= callbackSet;
1228 item->callbackMask &= ~callbackClear;
1230 return TRUE;
1233 /* Note that the new item is pre-zeroed. */
1234 static LRESULT
1235 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1237 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1238 HTREEITEM insertAfter;
1239 TREEVIEW_ITEM *newItem, *parentItem;
1240 BOOL bTextUpdated = FALSE;
1242 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1244 parentItem = infoPtr->root;
1246 else
1248 parentItem = ptdi->hParent;
1250 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1252 WARN("invalid parent %p\n", parentItem);
1253 return 0;
1257 insertAfter = ptdi->hInsertAfter;
1259 /* Validate this now for convenience. */
1260 switch ((DWORD_PTR)insertAfter)
1262 case (DWORD_PTR)TVI_FIRST:
1263 case (DWORD_PTR)TVI_LAST:
1264 case (DWORD_PTR)TVI_SORT:
1265 break;
1267 default:
1268 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1269 insertAfter->parent != parentItem)
1271 WARN("invalid insert after %p\n", insertAfter);
1272 insertAfter = TVI_LAST;
1276 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1277 (tvItem->mask & TVIF_TEXT)
1278 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1279 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1280 : "<no label>");
1282 newItem = TREEVIEW_AllocateItem(infoPtr);
1283 if (newItem == NULL)
1284 return 0;
1286 newItem->parent = parentItem;
1287 newItem->iIntegral = 1;
1288 newItem->visibleOrder = -1;
1290 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1291 return 0;
1293 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1295 infoPtr->uNumItems++;
1297 switch ((DWORD_PTR)insertAfter)
1299 case (DWORD_PTR)TVI_FIRST:
1301 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1302 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1303 if (infoPtr->firstVisible == originalFirst)
1304 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1306 break;
1308 case (DWORD_PTR)TVI_LAST:
1309 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1310 break;
1312 /* hInsertAfter names a specific item we want to insert after */
1313 default:
1314 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1315 break;
1317 case (DWORD_PTR)TVI_SORT:
1319 TREEVIEW_ITEM *aChild;
1320 TREEVIEW_ITEM *previousChild = NULL;
1321 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1322 BOOL bItemInserted = FALSE;
1324 aChild = parentItem->firstChild;
1326 bTextUpdated = TRUE;
1327 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1329 /* Iterate the parent children to see where we fit in */
1330 while (aChild != NULL)
1332 INT comp;
1334 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1335 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1337 if (comp < 0) /* we are smaller than the current one */
1339 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1340 if (infoPtr->firstVisible == originalFirst &&
1341 aChild == originalFirst)
1342 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1343 bItemInserted = TRUE;
1344 break;
1346 else if (comp > 0) /* we are bigger than the current one */
1348 previousChild = aChild;
1350 /* This will help us to exit if there is no more sibling */
1351 aChild = (aChild->nextSibling == 0)
1352 ? NULL
1353 : aChild->nextSibling;
1355 /* Look at the next item */
1356 continue;
1358 else if (comp == 0)
1361 * An item with this name is already existing, therefore,
1362 * we add after the one we found
1364 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1365 bItemInserted = TRUE;
1366 break;
1371 * we reach the end of the child list and the item has not
1372 * yet been inserted, therefore, insert it after the last child.
1374 if ((!bItemInserted) && (aChild == NULL))
1375 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1377 break;
1382 TRACE("new item %p; parent %p, mask 0x%x\n", newItem,
1383 newItem->parent, tvItem->mask);
1385 newItem->iLevel = newItem->parent->iLevel + 1;
1387 if (newItem->parent->cChildren == 0)
1388 newItem->parent->cChildren = 1;
1390 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1392 if (STATEIMAGEINDEX(newItem->state) == 0)
1393 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1396 if (infoPtr->firstVisible == NULL)
1397 infoPtr->firstVisible = newItem;
1399 TREEVIEW_VerifyTree(infoPtr);
1401 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1403 if (parentItem == infoPtr->root ||
1404 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1406 TREEVIEW_ITEM *item;
1407 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1409 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1410 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1412 if (!bTextUpdated)
1413 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1415 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1416 TREEVIEW_UpdateScrollBars(infoPtr);
1418 * if the item was inserted in a visible part of the tree,
1419 * invalidate it, as well as those after it
1421 for (item = newItem;
1422 item != NULL;
1423 item = TREEVIEW_GetNextListItem(infoPtr, item))
1424 TREEVIEW_Invalidate(infoPtr, item);
1426 else
1428 /* refresh treeview if newItem is the first item inserted under parentItem */
1429 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1431 /* parent got '+' - update it */
1432 TREEVIEW_Invalidate(infoPtr, parentItem);
1436 return (LRESULT)newItem;
1439 /* Item Deletion ************************************************************/
1440 static void
1441 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1443 static void
1444 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1446 TREEVIEW_ITEM *kill = parentItem->firstChild;
1448 while (kill != NULL)
1450 TREEVIEW_ITEM *next = kill->nextSibling;
1452 TREEVIEW_RemoveItem(infoPtr, kill);
1454 kill = next;
1457 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1458 assert(parentItem->firstChild == NULL);
1459 assert(parentItem->lastChild == NULL);
1462 static void
1463 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1465 TREEVIEW_ITEM *parentItem;
1467 assert(item != NULL);
1468 assert(item->parent != NULL); /* i.e. it must not be the root */
1470 parentItem = item->parent;
1472 if (parentItem->firstChild == item)
1473 parentItem->firstChild = item->nextSibling;
1475 if (parentItem->lastChild == item)
1476 parentItem->lastChild = item->prevSibling;
1478 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1479 && parentItem->cChildren > 0)
1480 parentItem->cChildren = 0;
1482 if (item->prevSibling)
1483 item->prevSibling->nextSibling = item->nextSibling;
1485 if (item->nextSibling)
1486 item->nextSibling->prevSibling = item->prevSibling;
1489 static void
1490 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1492 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1494 if (item->firstChild)
1495 TREEVIEW_RemoveAllChildren(infoPtr, item);
1497 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1498 TVIF_HANDLE | TVIF_PARAM, item, 0);
1500 TREEVIEW_UnlinkItem(item);
1502 infoPtr->uNumItems--;
1504 if (item->pszText != LPSTR_TEXTCALLBACKW)
1505 heap_free(item->pszText);
1507 TREEVIEW_FreeItem(infoPtr, item);
1511 /* Empty out the tree. */
1512 static void
1513 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1515 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1517 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1520 static LRESULT
1521 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1523 TREEVIEW_ITEM *newSelection = NULL;
1524 TREEVIEW_ITEM *newFirstVisible = NULL;
1525 TREEVIEW_ITEM *parent, *prev = NULL;
1526 BOOL visible = FALSE;
1528 if (item == TVI_ROOT || !item)
1530 TRACE("TVI_ROOT\n");
1531 parent = infoPtr->root;
1532 newSelection = NULL;
1533 visible = TRUE;
1534 TREEVIEW_RemoveTree(infoPtr);
1536 else
1538 if (!TREEVIEW_ValidItem(infoPtr, item))
1539 return FALSE;
1541 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1542 parent = item->parent;
1544 if (ISVISIBLE(item))
1546 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1547 visible = TRUE;
1550 if (infoPtr->selectedItem != NULL
1551 && (item == infoPtr->selectedItem
1552 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1554 if (item->nextSibling)
1555 newSelection = item->nextSibling;
1556 else if (item->parent != infoPtr->root)
1557 newSelection = item->parent;
1558 else
1559 newSelection = item->prevSibling;
1560 TRACE("newSelection = %p\n", newSelection);
1563 if (infoPtr->firstVisible == item)
1565 visible = TRUE;
1566 if (item->nextSibling)
1567 newFirstVisible = item->nextSibling;
1568 else if (item->prevSibling)
1569 newFirstVisible = item->prevSibling;
1570 else if (item->parent != infoPtr->root)
1571 newFirstVisible = item->parent;
1572 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1574 else
1575 newFirstVisible = infoPtr->firstVisible;
1577 TREEVIEW_RemoveItem(infoPtr, item);
1580 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1581 if (!infoPtr->selectedItem && newSelection)
1583 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1584 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1587 /* Validate insertMark dropItem.
1588 * hotItem ??? - used for comparison only.
1590 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1591 infoPtr->insertMarkItem = 0;
1593 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1594 infoPtr->dropItem = 0;
1596 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1597 newFirstVisible = infoPtr->root->firstChild;
1599 TREEVIEW_VerifyTree(infoPtr);
1601 if (visible)
1602 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1604 if (!infoPtr->bRedraw) return TRUE;
1606 if (visible)
1608 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1609 TREEVIEW_UpdateScrollBars(infoPtr);
1610 TREEVIEW_Invalidate(infoPtr, NULL);
1612 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1614 /* parent lost '+/-' - update it */
1615 TREEVIEW_Invalidate(infoPtr, parent);
1618 return TRUE;
1622 /* Get/Set Messages *********************************************************/
1623 static LRESULT
1624 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1626 infoPtr->bRedraw = wParam != 0;
1628 if (infoPtr->bRedraw)
1630 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1631 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1632 TREEVIEW_UpdateScrollBars(infoPtr);
1633 TREEVIEW_Invalidate(infoPtr, NULL);
1635 return 0;
1638 static LRESULT
1639 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1641 TRACE("\n");
1642 return infoPtr->uIndent;
1645 static LRESULT
1646 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1648 TRACE("\n");
1650 if (newIndent < MINIMUM_INDENT)
1651 newIndent = MINIMUM_INDENT;
1653 if (infoPtr->uIndent != newIndent)
1655 infoPtr->uIndent = newIndent;
1656 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1657 TREEVIEW_UpdateScrollBars(infoPtr);
1658 TREEVIEW_Invalidate(infoPtr, NULL);
1661 return 0;
1665 static LRESULT
1666 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1668 TRACE("\n");
1669 return (LRESULT)infoPtr->hwndToolTip;
1672 static LRESULT
1673 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1675 HWND prevToolTip;
1677 TRACE("\n");
1678 prevToolTip = infoPtr->hwndToolTip;
1679 infoPtr->hwndToolTip = hwndTT;
1681 return (LRESULT)prevToolTip;
1684 static LRESULT
1685 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1687 BOOL rc = infoPtr->bNtfUnicode;
1688 infoPtr->bNtfUnicode = fUnicode;
1689 return rc;
1692 static LRESULT
1693 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1695 return infoPtr->bNtfUnicode;
1698 static LRESULT
1699 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1701 return infoPtr->uScrollTime;
1704 static LRESULT
1705 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1707 UINT uOldScrollTime = infoPtr->uScrollTime;
1709 infoPtr->uScrollTime = min(uScrollTime, 100);
1711 return uOldScrollTime;
1715 static LRESULT
1716 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1718 TRACE("\n");
1720 switch (wParam)
1722 case TVSIL_NORMAL:
1723 return (LRESULT)infoPtr->himlNormal;
1725 case TVSIL_STATE:
1726 return (LRESULT)infoPtr->himlState;
1728 default:
1729 return 0;
1733 #define TVHEIGHT_MIN 16
1734 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1736 /* Compute the natural height for items. */
1737 static UINT
1738 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1740 TEXTMETRICW tm;
1741 HDC hdc = GetDC(0);
1742 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1743 UINT height;
1745 /* Height is the maximum of:
1746 * 16 (a hack because our fonts are tiny), and
1747 * The text height + border & margin, and
1748 * The size of the normal image list
1750 GetTextMetricsW(hdc, &tm);
1751 SelectObject(hdc, hOldFont);
1752 ReleaseDC(0, hdc);
1754 height = TVHEIGHT_MIN;
1755 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1756 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1757 if (height < infoPtr->normalImageHeight)
1758 height = infoPtr->normalImageHeight;
1760 /* Round down, unless we support odd ("non even") heights. */
1761 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1762 height &= ~1;
1764 return height;
1767 static LRESULT
1768 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1770 HIMAGELIST himlOld = 0;
1771 int oldWidth = infoPtr->normalImageWidth;
1772 int oldHeight = infoPtr->normalImageHeight;
1774 TRACE("%u,%p\n", type, himlNew);
1776 switch (type)
1778 case TVSIL_NORMAL:
1779 himlOld = infoPtr->himlNormal;
1780 infoPtr->himlNormal = himlNew;
1782 if (himlNew)
1783 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1784 &infoPtr->normalImageHeight);
1785 else
1787 infoPtr->normalImageWidth = 0;
1788 infoPtr->normalImageHeight = 0;
1791 break;
1793 case TVSIL_STATE:
1794 himlOld = infoPtr->himlState;
1795 infoPtr->himlState = himlNew;
1797 if (himlNew)
1798 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1799 &infoPtr->stateImageHeight);
1800 else
1802 infoPtr->stateImageWidth = 0;
1803 infoPtr->stateImageHeight = 0;
1806 break;
1808 default:
1809 ERR("unknown imagelist type %u\n", type);
1812 if (oldWidth != infoPtr->normalImageWidth ||
1813 oldHeight != infoPtr->normalImageHeight)
1815 BOOL bRecalcVisible = FALSE;
1817 if (oldHeight != infoPtr->normalImageHeight &&
1818 !infoPtr->bHeightSet)
1820 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1821 bRecalcVisible = TRUE;
1824 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1825 infoPtr->normalImageWidth != infoPtr->uIndent)
1827 infoPtr->uIndent = infoPtr->normalImageWidth;
1828 bRecalcVisible = TRUE;
1831 if (bRecalcVisible)
1832 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1834 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1835 TREEVIEW_UpdateScrollBars(infoPtr);
1838 TREEVIEW_Invalidate(infoPtr, NULL);
1840 return (LRESULT)himlOld;
1843 static LRESULT
1844 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1846 INT prevHeight = infoPtr->uItemHeight;
1848 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1849 if (newHeight == -1)
1851 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1852 infoPtr->bHeightSet = FALSE;
1854 else
1856 if (newHeight == 0) newHeight = 1;
1857 infoPtr->uItemHeight = newHeight;
1858 infoPtr->bHeightSet = TRUE;
1861 /* Round down, unless we support odd ("non even") heights. */
1862 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1864 infoPtr->uItemHeight &= ~1;
1865 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1868 if (infoPtr->uItemHeight != prevHeight)
1870 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1871 TREEVIEW_UpdateScrollBars(infoPtr);
1872 TREEVIEW_Invalidate(infoPtr, NULL);
1875 return prevHeight;
1878 static LRESULT
1879 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1881 TRACE("\n");
1882 return infoPtr->uItemHeight;
1886 static LRESULT
1887 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1889 TRACE("%p\n", infoPtr->hFont);
1890 return (LRESULT)infoPtr->hFont;
1894 static INT CALLBACK
1895 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1897 (void)unused;
1899 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1901 return 1;
1904 static LRESULT
1905 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1907 UINT uHeight = infoPtr->uItemHeight;
1909 TRACE("%p %i\n", hFont, bRedraw);
1911 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1913 DeleteObject(infoPtr->hBoldFont);
1914 DeleteObject(infoPtr->hUnderlineFont);
1915 DeleteObject(infoPtr->hBoldUnderlineFont);
1916 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1917 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1918 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
1920 if (!infoPtr->bHeightSet)
1921 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1923 if (uHeight != infoPtr->uItemHeight)
1924 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1926 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1928 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1929 TREEVIEW_UpdateScrollBars(infoPtr);
1931 if (bRedraw)
1932 TREEVIEW_Invalidate(infoPtr, NULL);
1934 return 0;
1938 static LRESULT
1939 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1941 TRACE("\n");
1942 return (LRESULT)infoPtr->clrLine;
1945 static LRESULT
1946 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1948 COLORREF prevColor = infoPtr->clrLine;
1950 TRACE("\n");
1951 infoPtr->clrLine = color;
1952 return (LRESULT)prevColor;
1956 static LRESULT
1957 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1959 TRACE("\n");
1960 return (LRESULT)infoPtr->clrText;
1963 static LRESULT
1964 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1966 COLORREF prevColor = infoPtr->clrText;
1968 TRACE("\n");
1969 infoPtr->clrText = color;
1971 if (infoPtr->clrText != prevColor)
1972 TREEVIEW_Invalidate(infoPtr, NULL);
1974 return (LRESULT)prevColor;
1978 static LRESULT
1979 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1981 TRACE("\n");
1982 return (LRESULT)infoPtr->clrBk;
1985 static LRESULT
1986 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1988 COLORREF prevColor = infoPtr->clrBk;
1990 TRACE("\n");
1991 infoPtr->clrBk = newColor;
1993 if (newColor != prevColor)
1994 TREEVIEW_Invalidate(infoPtr, NULL);
1996 return (LRESULT)prevColor;
2000 static LRESULT
2001 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
2003 TRACE("\n");
2004 return (LRESULT)infoPtr->clrInsertMark;
2007 static LRESULT
2008 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
2010 COLORREF prevColor = infoPtr->clrInsertMark;
2012 TRACE("0x%08x\n", color);
2013 infoPtr->clrInsertMark = color;
2015 return (LRESULT)prevColor;
2019 static LRESULT
2020 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2022 TRACE("%d %p\n", wParam, item);
2024 if (!TREEVIEW_ValidItem(infoPtr, item))
2025 return 0;
2027 infoPtr->insertBeforeorAfter = wParam;
2028 infoPtr->insertMarkItem = item;
2030 TREEVIEW_Invalidate(infoPtr, NULL);
2032 return 1;
2036 /************************************************************************
2037 * Some serious braindamage here. lParam is a pointer to both the
2038 * input HTREEITEM and the output RECT.
2040 static LRESULT
2041 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2043 TREEVIEW_ITEM *item;
2044 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2046 TRACE("\n");
2048 if (pItem == NULL)
2049 return FALSE;
2051 item = *pItem;
2052 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2053 return FALSE;
2056 * If wParam is TRUE return the text size otherwise return
2057 * the whole item size
2059 if (fTextRect)
2061 /* Windows does not send TVN_GETDISPINFO here. */
2063 lpRect->top = item->rect.top;
2064 lpRect->bottom = item->rect.bottom;
2066 lpRect->left = item->textOffset;
2067 if (!item->textWidth)
2068 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2070 lpRect->right = item->textOffset + item->textWidth + 4;
2072 else
2074 *lpRect = item->rect;
2077 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2079 return TRUE;
2082 static inline LRESULT
2083 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2085 /* Surprise! This does not take integral height into account. */
2086 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2087 return infoPtr->clientHeight / infoPtr->uItemHeight;
2091 static LRESULT
2092 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2094 TREEVIEW_ITEM *item = tvItem->hItem;
2096 if (!TREEVIEW_ValidItem(infoPtr, item))
2098 BOOL valid_item = FALSE;
2099 if (!item) return FALSE;
2101 __TRY
2103 infoPtr = item->infoPtr;
2104 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2105 valid_item = TREEVIEW_ValidItem(infoPtr, item);
2107 __EXCEPT_PAGE_FAULT
2110 __ENDTRY
2111 if (!valid_item) return FALSE;
2114 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2116 if (tvItem->mask & TVIF_CHILDREN)
2118 if (item->cChildren==I_CHILDRENCALLBACK)
2119 FIXME("I_CHILDRENCALLBACK not supported\n");
2120 tvItem->cChildren = item->cChildren;
2123 if (tvItem->mask & TVIF_HANDLE)
2124 tvItem->hItem = item;
2126 if (tvItem->mask & TVIF_IMAGE)
2127 tvItem->iImage = item->iImage;
2129 if (tvItem->mask & TVIF_INTEGRAL)
2130 tvItem->iIntegral = item->iIntegral;
2132 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2133 tvItem->lParam = item->lParam;
2135 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2136 tvItem->iSelectedImage = item->iSelectedImage;
2138 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2139 tvItem->iExpandedImage = item->iExpandedImage;
2141 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2142 tvItem->state = item->state;
2144 if (tvItem->mask & TVIF_TEXT)
2146 if (item->pszText == NULL)
2148 if (tvItem->cchTextMax > 0)
2149 tvItem->pszText[0] = '\0';
2151 else if (isW)
2153 if (item->pszText == LPSTR_TEXTCALLBACKW)
2155 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2156 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2158 else
2160 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2163 else
2165 if (item->pszText == LPSTR_TEXTCALLBACKW)
2167 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2168 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2170 else
2172 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2173 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2178 if (tvItem->mask & TVIF_STATEEX)
2180 FIXME("Extended item state not supported, returning 0.\n");
2181 tvItem->uStateEx = 0;
2184 TRACE("item <%p>, txt %p, img %d, mask 0x%x\n",
2185 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2187 return TRUE;
2190 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2191 * which is wrong. */
2192 static LRESULT
2193 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2195 TREEVIEW_ITEM *item;
2196 TREEVIEW_ITEM originalItem;
2198 item = tvItem->hItem;
2200 TRACE("item %d, mask 0x%x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2201 tvItem->mask);
2203 if (!TREEVIEW_ValidItem(infoPtr, item))
2204 return FALSE;
2206 /* Store the original item values. Text buffer will be freed in TREEVIEW_DoSetItemT() below. */
2207 originalItem = *item;
2209 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2210 return FALSE;
2212 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2213 if ((tvItem->mask & TVIF_TEXT
2214 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2215 && ISVISIBLE(item))
2217 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2218 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2221 if (tvItem->mask != 0 && ISVISIBLE(item))
2223 /* The refresh updates everything, but we can't wait until then. */
2224 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2226 /* if any of the item's values changed and it's not a callback, redraw the item */
2227 if (item_changed(&originalItem, item, tvItem))
2229 if (tvItem->mask & TVIF_INTEGRAL)
2231 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2232 TREEVIEW_UpdateScrollBars(infoPtr);
2234 TREEVIEW_Invalidate(infoPtr, NULL);
2236 else
2238 TREEVIEW_UpdateScrollBars(infoPtr);
2239 TREEVIEW_Invalidate(infoPtr, item);
2244 return TRUE;
2247 static LRESULT
2248 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2250 TRACE("\n");
2252 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2253 return 0;
2255 return (item->state & mask);
2258 static LRESULT
2259 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2261 TREEVIEW_ITEM *retval;
2263 retval = 0;
2265 /* handle all the global data here */
2266 switch (which)
2268 case TVGN_CHILD: /* Special case: child of 0 is root */
2269 if (item)
2270 break;
2271 /* fall through */
2272 case TVGN_ROOT:
2273 retval = infoPtr->root->firstChild;
2274 break;
2276 case TVGN_CARET:
2277 retval = infoPtr->selectedItem;
2278 break;
2280 case TVGN_FIRSTVISIBLE:
2281 retval = infoPtr->firstVisible;
2282 break;
2284 case TVGN_DROPHILITE:
2285 retval = infoPtr->dropItem;
2286 break;
2288 case TVGN_LASTVISIBLE:
2289 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2290 break;
2293 if (retval)
2295 TRACE("flags:0x%x, returns %p\n", which, retval);
2296 return (LRESULT)retval;
2299 if (item == TVI_ROOT) item = infoPtr->root;
2301 if (!TREEVIEW_ValidItem(infoPtr, item))
2302 return FALSE;
2304 switch (which)
2306 case TVGN_NEXT:
2307 retval = item->nextSibling;
2308 break;
2309 case TVGN_PREVIOUS:
2310 retval = item->prevSibling;
2311 break;
2312 case TVGN_PARENT:
2313 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2314 break;
2315 case TVGN_CHILD:
2316 retval = item->firstChild;
2317 break;
2318 case TVGN_NEXTVISIBLE:
2319 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2320 break;
2321 case TVGN_PREVIOUSVISIBLE:
2322 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2323 break;
2324 default:
2325 TRACE("Unknown msg 0x%x, item %p\n", which, item);
2326 break;
2329 TRACE("flags: 0x%x, item %p;returns %p\n", which, item, retval);
2330 return (LRESULT)retval;
2334 static LRESULT
2335 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2337 TRACE(" %d\n", infoPtr->uNumItems);
2338 return (LRESULT)infoPtr->uNumItems;
2341 static VOID
2342 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2344 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2346 static const unsigned int state_table[] = { 0, 2, 1 };
2348 unsigned int state;
2350 state = STATEIMAGEINDEX(item->state);
2351 TRACE("state: 0x%x\n", state);
2352 item->state &= ~TVIS_STATEIMAGEMASK;
2354 if (state < 3)
2355 state = state_table[state];
2357 item->state |= INDEXTOSTATEIMAGEMASK(state);
2359 TRACE("state: 0x%x\n", state);
2360 TREEVIEW_Invalidate(infoPtr, item);
2365 /* Painting *************************************************************/
2367 /* Draw the lines and expand button for an item. Also draws one section
2368 * of the line from item's parent to item's parent's next sibling. */
2369 static void
2370 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2372 LONG centerx, centery;
2373 BOOL lar = ((infoPtr->dwStyle
2374 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2375 > TVS_LINESATROOT);
2376 HBRUSH hbr, hbrOld;
2377 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2379 if (!lar && item->iLevel == 0)
2380 return;
2382 hbr = CreateSolidBrush(clrBk);
2383 hbrOld = SelectObject(hdc, hbr);
2385 centerx = (item->linesOffset + item->stateOffset) / 2;
2386 centery = (item->rect.top + item->rect.bottom) / 2;
2388 if (infoPtr->dwStyle & TVS_HASLINES)
2390 HPEN hOldPen, hNewPen;
2391 HTREEITEM parent;
2392 LOGBRUSH lb;
2394 /* Get a dotted grey pen */
2395 lb.lbStyle = BS_SOLID;
2396 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2397 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2398 hOldPen = SelectObject(hdc, hNewPen);
2400 /* Make sure the center is on a dot (using +2 instead
2401 * of +1 gives us pixel-by-pixel compat with native) */
2402 centery = (centery + 2) & ~1;
2404 MoveToEx(hdc, item->stateOffset, centery, NULL);
2405 LineTo(hdc, centerx - 1, centery);
2407 if (item->prevSibling || item->parent != infoPtr->root)
2409 MoveToEx(hdc, centerx, item->rect.top, NULL);
2410 LineTo(hdc, centerx, centery);
2413 if (item->nextSibling)
2415 MoveToEx(hdc, centerx, centery, NULL);
2416 LineTo(hdc, centerx, item->rect.bottom + 1);
2419 /* Draw the line from our parent to its next sibling. */
2420 parent = item->parent;
2421 while (parent != infoPtr->root)
2423 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2425 if (parent->nextSibling
2426 /* skip top-levels unless TVS_LINESATROOT */
2427 && parent->stateOffset > parent->linesOffset)
2429 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2430 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2433 parent = parent->parent;
2436 SelectObject(hdc, hOldPen);
2437 DeleteObject(hNewPen);
2441 * Display the (+/-) signs
2444 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2446 if (item->cChildren)
2448 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2449 if (theme)
2451 RECT glyphRect = item->rect;
2452 glyphRect.left = item->linesOffset;
2453 glyphRect.right = item->stateOffset;
2454 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2455 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2456 &glyphRect, NULL);
2458 else
2460 LONG height = item->rect.bottom - item->rect.top;
2461 LONG width = item->stateOffset - item->linesOffset;
2462 LONG rectsize = min(height, width) / 4;
2463 /* plussize = ceil(rectsize * 3/4) */
2464 LONG plussize = (rectsize + 1) * 3 / 4;
2466 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2467 HPEN old_pen = SelectObject(hdc, new_pen);
2469 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2470 centerx + rectsize + 2, centery + rectsize + 2);
2472 SelectObject(hdc, old_pen);
2473 DeleteObject(new_pen);
2475 /* draw +/- signs with current text color */
2476 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2477 old_pen = SelectObject(hdc, new_pen);
2479 if (height < 18 || width < 18)
2481 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2482 LineTo(hdc, centerx + plussize, centery);
2484 if (!(item->state & TVIS_EXPANDED) ||
2485 (item->state & TVIS_EXPANDPARTIAL))
2487 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2488 LineTo(hdc, centerx, centery + plussize);
2491 else
2493 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2494 centerx + plussize, centery + 2);
2496 if (!(item->state & TVIS_EXPANDED) ||
2497 (item->state & TVIS_EXPANDPARTIAL))
2499 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2500 centerx + 2, centery + plussize);
2501 SetPixel(hdc, centerx - 1, centery, clrBk);
2502 SetPixel(hdc, centerx + 1, centery, clrBk);
2506 SelectObject(hdc, old_pen);
2507 DeleteObject(new_pen);
2511 SelectObject(hdc, hbrOld);
2512 DeleteObject(hbr);
2515 static void
2516 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2518 INT cditem;
2519 HFONT hOldFont;
2520 COLORREF oldTextColor, oldTextBkColor;
2521 int centery;
2522 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2523 NMTVCUSTOMDRAW nmcdhdr;
2525 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2527 /* - If item is drop target or it is selected and window is in focus -
2528 * use blue background (COLOR_HIGHLIGHT).
2529 * - If item is selected, window is not in focus, but it has style
2530 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2531 * - Otherwise - use background color
2533 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2534 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2535 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2537 if ((item->state & TVIS_DROPHILITED) || inFocus)
2539 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2540 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2542 else
2544 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2545 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2548 else
2550 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2551 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2552 nmcdhdr.clrText = comctl32_color.clrHighlight;
2553 else
2554 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2557 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2558 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2559 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2561 /* The custom draw handler can query the text rectangle,
2562 * so get ready. */
2563 /* should already be known, set to 0 when changed */
2564 if (!item->textWidth)
2565 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2567 cditem = 0;
2569 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2571 cditem = TREEVIEW_SendCustomDrawItemNotify
2572 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2573 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2575 if (cditem & CDRF_SKIPDEFAULT)
2577 SelectObject(hdc, hOldFont);
2578 return;
2582 if (cditem & CDRF_NEWFONT)
2583 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2585 if (TREEVIEW_IsFullRowSelect(infoPtr))
2587 HBRUSH brush = CreateSolidBrush(nmcdhdr.clrTextBk);
2588 FillRect(hdc, &item->rect, brush);
2589 DeleteObject(brush);
2592 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2594 /* reset colors. Custom draw handler can change them */
2595 SetTextColor(hdc, nmcdhdr.clrText);
2596 SetBkColor(hdc, nmcdhdr.clrTextBk);
2598 centery = (item->rect.top + item->rect.bottom) / 2;
2601 * Display the images associated with this item
2604 INT imageIndex;
2606 /* State images are displayed to the left of the Normal image
2607 * image number is in state; zero should be `display no image'.
2609 imageIndex = STATEIMAGEINDEX(item->state);
2611 if (infoPtr->himlState && imageIndex)
2613 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2614 item->stateOffset,
2615 centery - infoPtr->stateImageHeight / 2,
2616 ILD_NORMAL);
2619 /* Now, draw the normal image; can be either selected,
2620 * non-selected or expanded image.
2623 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2625 /* The item is currently selected */
2626 imageIndex = item->iSelectedImage;
2628 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2630 /* The item is currently not selected but expanded */
2631 imageIndex = item->iExpandedImage;
2633 else
2635 /* The item is not selected and not expanded */
2636 imageIndex = item->iImage;
2639 if (infoPtr->himlNormal)
2641 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2643 style |= item->state & TVIS_OVERLAYMASK;
2645 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2646 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2647 0, 0,
2648 TREEVIEW_IsFullRowSelect(infoPtr) ? nmcdhdr.clrTextBk : infoPtr->clrBk,
2649 item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2650 style);
2656 * Display the text associated with this item
2659 /* Don't paint item's text if it's being edited */
2660 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2662 if (item->pszText)
2664 RECT rcText;
2665 UINT align;
2666 SIZE sz;
2668 rcText.top = item->rect.top;
2669 rcText.bottom = item->rect.bottom;
2670 rcText.left = item->textOffset;
2671 rcText.right = rcText.left + item->textWidth + 4;
2673 TRACE("drawing text %s at (%s)\n",
2674 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2676 /* Draw it */
2677 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2679 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2680 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2681 ETO_CLIPPED | ETO_OPAQUE,
2682 &rcText,
2683 item->pszText,
2684 lstrlenW(item->pszText),
2685 NULL);
2686 SetTextAlign(hdc, align);
2688 /* Draw focus box around the selected item */
2689 if ((item == infoPtr->selectedItem) && inFocus)
2691 DrawFocusRect(hdc,&rcText);
2696 /* Draw insertion mark if necessary */
2698 if (infoPtr->insertMarkItem)
2699 TRACE("item:%d,mark:%p\n",
2700 TREEVIEW_GetItemIndex(infoPtr, item),
2701 infoPtr->insertMarkItem);
2703 if (item == infoPtr->insertMarkItem)
2705 HPEN hNewPen, hOldPen;
2706 int offset;
2707 int left, right;
2709 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2710 hOldPen = SelectObject(hdc, hNewPen);
2712 if (infoPtr->insertBeforeorAfter)
2713 offset = item->rect.bottom - 1;
2714 else
2715 offset = item->rect.top + 1;
2717 left = item->textOffset - 2;
2718 right = item->textOffset + item->textWidth + 2;
2720 MoveToEx(hdc, left, offset - 3, NULL);
2721 LineTo(hdc, left, offset + 4);
2723 MoveToEx(hdc, left, offset, NULL);
2724 LineTo(hdc, right + 1, offset);
2726 MoveToEx(hdc, right, offset + 3, NULL);
2727 LineTo(hdc, right, offset - 4);
2729 SelectObject(hdc, hOldPen);
2730 DeleteObject(hNewPen);
2733 /* Restore the hdc state */
2734 SetTextColor(hdc, oldTextColor);
2735 SetBkColor(hdc, oldTextBkColor);
2736 SelectObject(hdc, hOldFont);
2738 if (cditem & CDRF_NOTIFYPOSTPAINT)
2740 cditem = TREEVIEW_SendCustomDrawItemNotify
2741 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2742 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2746 /* Computes treeHeight and treeWidth and updates the scroll bars.
2748 static void
2749 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2751 TREEVIEW_ITEM *item;
2752 HWND hwnd = infoPtr->hwnd;
2753 BOOL vert = FALSE;
2754 BOOL horz = FALSE;
2755 SCROLLINFO si;
2756 LONG scrollX = infoPtr->scrollX;
2758 infoPtr->treeWidth = 0;
2759 infoPtr->treeHeight = 0;
2761 /* We iterate through all visible items in order to get the tree height
2762 * and width */
2763 item = infoPtr->root->firstChild;
2765 while (item != NULL)
2767 if (ISVISIBLE(item))
2769 /* actually we draw text at textOffset + 2 */
2770 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2771 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2773 /* This is scroll-adjusted, but we fix this below. */
2774 infoPtr->treeHeight = item->rect.bottom;
2777 item = TREEVIEW_GetNextListItem(infoPtr, item);
2780 /* Fix the scroll adjusted treeHeight and treeWidth. */
2781 if (infoPtr->root->firstChild)
2782 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2784 infoPtr->treeWidth += infoPtr->scrollX;
2786 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2788 /* Adding one scroll bar may take up enough space that it forces us
2789 * to add the other as well. */
2790 if (infoPtr->treeHeight > infoPtr->clientHeight)
2792 vert = TRUE;
2794 if (infoPtr->treeWidth
2795 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2796 horz = TRUE;
2798 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2799 horz = TRUE;
2801 if (!vert && horz && infoPtr->treeHeight
2802 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2803 vert = TRUE;
2805 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2807 si.cbSize = sizeof(SCROLLINFO);
2808 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2809 si.nMin = 0;
2811 if (vert)
2813 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2814 if ( si.nPage && NULL != infoPtr->firstVisible)
2816 si.nPos = infoPtr->firstVisible->visibleOrder;
2817 si.nMax = infoPtr->maxVisibleOrder - 1;
2819 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2821 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2822 ShowScrollBar(hwnd, SB_VERT, TRUE);
2823 infoPtr->uInternalStatus |= TV_VSCROLL;
2825 else
2827 if (infoPtr->uInternalStatus & TV_VSCROLL)
2828 ShowScrollBar(hwnd, SB_VERT, FALSE);
2829 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2832 else
2834 if (infoPtr->uInternalStatus & TV_VSCROLL)
2835 ShowScrollBar(hwnd, SB_VERT, FALSE);
2836 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2839 if (horz)
2841 si.nPage = infoPtr->clientWidth;
2842 si.nPos = infoPtr->scrollX;
2843 si.nMax = infoPtr->treeWidth - 1;
2845 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2847 si.nPos = si.nMax - max( si.nPage-1, 0 );
2848 scrollX = si.nPos;
2851 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2852 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2853 infoPtr->uInternalStatus |= TV_HSCROLL;
2855 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2856 TREEVIEW_HScroll(infoPtr,
2857 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2859 else
2861 if (infoPtr->uInternalStatus & TV_HSCROLL)
2862 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2863 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2865 scrollX = 0;
2866 if (infoPtr->scrollX != 0)
2868 TREEVIEW_HScroll(infoPtr,
2869 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2873 if (!horz)
2874 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2877 static void
2878 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2880 HBRUSH hBrush;
2881 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2883 hBrush = CreateSolidBrush(clrBk);
2884 FillRect(hdc, rc, hBrush);
2885 DeleteObject(hBrush);
2888 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2889 static LRESULT
2890 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2892 RECT rect;
2894 TRACE("%p\n", infoPtr);
2896 GetClientRect(infoPtr->hwnd, &rect);
2897 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2899 return 1;
2902 static void
2903 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2905 HWND hwnd = infoPtr->hwnd;
2906 RECT rect = *rc;
2907 TREEVIEW_ITEM *item;
2909 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2911 TRACE("empty window\n");
2912 return;
2915 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2916 hdc, rect);
2918 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2920 ReleaseDC(hwnd, hdc);
2921 return;
2924 for (item = infoPtr->root->firstChild;
2925 item != NULL;
2926 item = TREEVIEW_GetNextListItem(infoPtr, item))
2928 if (ISVISIBLE(item))
2930 /* Avoid unneeded calculations */
2931 if (item->rect.top > rect.bottom)
2932 break;
2933 if (item->rect.bottom < rect.top)
2934 continue;
2936 TREEVIEW_DrawItem(infoPtr, hdc, item);
2940 TREEVIEW_UpdateScrollBars(infoPtr);
2942 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2943 infoPtr->cdmode =
2944 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2947 static inline void
2948 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2950 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2953 static void
2954 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2956 if (item)
2957 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2958 else
2959 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2962 static void
2963 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2965 RECT rc;
2966 HBITMAP hbm, hbmOld;
2967 HDC hdc, hdcScreen;
2968 int nIndex;
2970 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2972 hdcScreen = GetDC(0);
2974 hdc = CreateCompatibleDC(hdcScreen);
2975 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2976 hbmOld = SelectObject(hdc, hbm);
2978 SetRect(&rc, 0, 0, 48, 16);
2979 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2981 SetRect(&rc, 18, 2, 30, 14);
2982 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2983 DFCS_BUTTONCHECK|DFCS_FLAT);
2985 SetRect(&rc, 34, 2, 46, 14);
2986 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2987 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2989 SelectObject(hdc, hbmOld);
2990 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2991 comctl32_color.clrWindow);
2992 TRACE("checkbox index %d\n", nIndex);
2994 DeleteObject(hbm);
2995 DeleteDC(hdc);
2996 ReleaseDC(0, hdcScreen);
2998 infoPtr->stateImageWidth = 16;
2999 infoPtr->stateImageHeight = 16;
3002 static void
3003 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3005 TREEVIEW_ITEM *child = item->firstChild;
3007 item->state &= ~TVIS_STATEIMAGEMASK;
3008 item->state |= INDEXTOSTATEIMAGEMASK(1);
3010 while (child)
3012 TREEVIEW_ITEM *next = child->nextSibling;
3013 TREEVIEW_ResetImageStateIndex(infoPtr, child);
3014 child = next;
3018 static LRESULT
3019 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
3021 HDC hdc;
3022 PAINTSTRUCT ps;
3023 RECT rc;
3025 TRACE("(%p %p)\n", infoPtr, hdc_ref);
3027 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
3029 TREEVIEW_InitCheckboxes(infoPtr);
3030 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
3032 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
3033 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
3034 TREEVIEW_UpdateScrollBars(infoPtr);
3035 TREEVIEW_Invalidate(infoPtr, NULL);
3038 if (hdc_ref)
3040 hdc = hdc_ref;
3041 GetClientRect(infoPtr->hwnd, &rc);
3042 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3044 else
3046 hdc = BeginPaint(infoPtr->hwnd, &ps);
3047 rc = ps.rcPaint;
3048 if(ps.fErase)
3049 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3052 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3053 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3055 if (!hdc_ref)
3056 EndPaint(infoPtr->hwnd, &ps);
3058 return 0;
3061 static LRESULT
3062 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3064 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3066 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3067 return 0;
3069 if (options & PRF_ERASEBKGND)
3070 TREEVIEW_EraseBackground(infoPtr, hdc);
3072 if (options & PRF_CLIENT)
3074 RECT rc;
3075 GetClientRect(infoPtr->hwnd, &rc);
3076 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3079 return 0;
3082 /* Sorting **************************************************************/
3084 /***************************************************************************
3085 * Forward the DPA local callback to the treeview owner callback
3087 static INT WINAPI
3088 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3089 const TVSORTCB *pCallBackSort)
3091 /* Forward the call to the client-defined callback */
3092 return pCallBackSort->lpfnCompare(first->lParam,
3093 second->lParam,
3094 pCallBackSort->lParam);
3097 /***************************************************************************
3098 * Treeview native sort routine: sort on item text.
3100 static INT WINAPI
3101 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3102 const TREEVIEW_INFO *infoPtr)
3104 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3105 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3107 if(first->pszText && second->pszText)
3108 return lstrcmpiW(first->pszText, second->pszText);
3109 else if(first->pszText)
3110 return -1;
3111 else if(second->pszText)
3112 return 1;
3113 else
3114 return 0;
3117 /* Returns the number of physical children belonging to item. */
3118 static INT
3119 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3121 INT cChildren = 0;
3122 HTREEITEM hti;
3124 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3125 cChildren++;
3127 return cChildren;
3130 /* Returns a DPA containing a pointer to each physical child of item in
3131 * sibling order. If item has no children, an empty DPA is returned. */
3132 static HDPA
3133 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3135 HTREEITEM child;
3137 HDPA list = DPA_Create(8);
3138 if (list == 0) return NULL;
3140 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3142 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3144 DPA_Destroy(list);
3145 return NULL;
3149 return list;
3152 /***************************************************************************
3153 * Setup the treeview structure with regards of the sort method
3154 * and sort the children of the TV item specified in lParam
3155 * fRecurse: currently unused. Should be zero.
3156 * parent: if pSort!=NULL, should equal pSort->hParent.
3157 * otherwise, item which child items are to be sorted.
3158 * pSort: sort method info. if NULL, sort on item text.
3159 * if non-NULL, sort on item's lParam content, and let the
3160 * application decide what that means. See also TVM_SORTCHILDRENCB.
3163 static LRESULT
3164 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3165 LPTVSORTCB pSort)
3167 INT cChildren;
3168 PFNDPACOMPARE pfnCompare;
3169 LPARAM lpCompare;
3171 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3172 if (parent == TVI_ROOT || parent == NULL)
3173 parent = infoPtr->root;
3175 /* Check for a valid handle to the parent item */
3176 if (!TREEVIEW_ValidItem(infoPtr, parent))
3178 ERR("invalid item hParent=%p\n", parent);
3179 return FALSE;
3182 if (pSort)
3184 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3185 lpCompare = (LPARAM)pSort;
3187 else
3189 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3190 lpCompare = (LPARAM)infoPtr;
3193 cChildren = TREEVIEW_CountChildren(parent);
3195 /* Make sure there is something to sort */
3196 if (cChildren > 1)
3198 /* TREEVIEW_ITEM rechaining */
3199 INT count = 0;
3200 HTREEITEM item = 0;
3201 HTREEITEM nextItem = 0;
3202 HTREEITEM prevItem = 0;
3204 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3206 if (sortList == NULL)
3207 return FALSE;
3209 /* let DPA sort the list */
3210 DPA_Sort(sortList, pfnCompare, lpCompare);
3212 /* The order of DPA entries has been changed, so fixup the
3213 * nextSibling and prevSibling pointers. */
3215 item = DPA_GetPtr(sortList, count++);
3216 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3218 /* link the two current item together */
3219 item->nextSibling = nextItem;
3220 nextItem->prevSibling = item;
3222 if (prevItem == NULL)
3224 /* this is the first item, update the parent */
3225 parent->firstChild = item;
3226 item->prevSibling = NULL;
3228 else
3230 /* fix the back chaining */
3231 item->prevSibling = prevItem;
3234 /* get ready for the next one */
3235 prevItem = item;
3236 item = nextItem;
3239 /* the last item is pointed to by item and never has a sibling */
3240 item->nextSibling = NULL;
3241 parent->lastChild = item;
3243 DPA_Destroy(sortList);
3245 TREEVIEW_VerifyTree(infoPtr);
3247 if (parent->state & TVIS_EXPANDED)
3249 int visOrder = infoPtr->firstVisible->visibleOrder;
3251 if (parent == infoPtr->root)
3252 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3253 else
3254 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3256 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3258 TREEVIEW_ITEM *item;
3260 for (item = infoPtr->root->firstChild; item != NULL;
3261 item = TREEVIEW_GetNextListItem(infoPtr, item))
3263 if (item->visibleOrder == visOrder)
3264 break;
3267 if (!item) item = parent->firstChild;
3268 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3271 TREEVIEW_Invalidate(infoPtr, NULL);
3274 return TRUE;
3276 return FALSE;
3280 /***************************************************************************
3281 * Setup the treeview structure with regards of the sort method
3282 * and sort the children of the TV item specified in lParam
3284 static LRESULT
3285 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3287 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3291 /***************************************************************************
3292 * Sort the children of the TV item specified in lParam.
3294 static LRESULT
3295 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3297 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3301 /* Expansion/Collapse ***************************************************/
3303 static BOOL
3304 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3305 UINT action)
3307 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3308 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3309 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3310 0, item);
3313 static VOID
3314 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3315 UINT action)
3317 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3318 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3319 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3320 0, item);
3324 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3325 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3326 static BOOL
3327 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3328 BOOL bRemoveChildren, BOOL bUser)
3330 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3331 BOOL bSetSelection, bSetFirstVisible;
3332 RECT scrollRect;
3333 LONG scrollDist = 0;
3334 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3335 BOOL wasExpanded;
3337 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3339 if (!TREEVIEW_HasChildren(infoPtr, item))
3340 return FALSE;
3342 if (bUser)
3343 TREEVIEW_SendExpanding(infoPtr, item, action);
3345 if (item->firstChild == NULL)
3346 return FALSE;
3348 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3349 item->state &= ~TVIS_EXPANDED;
3351 if (wasExpanded && bUser)
3352 TREEVIEW_SendExpanded(infoPtr, item, action);
3354 bSetSelection = (infoPtr->selectedItem != NULL
3355 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3357 bSetFirstVisible = (infoPtr->firstVisible != NULL
3358 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3360 tmpItem = item;
3361 while (tmpItem)
3363 if (tmpItem->nextSibling)
3365 nextItem = tmpItem->nextSibling;
3366 break;
3368 tmpItem = tmpItem->parent;
3371 if (nextItem)
3372 scrollDist = nextItem->rect.top;
3374 if (bRemoveChildren)
3376 INT old_cChildren = item->cChildren;
3377 TRACE("TVE_COLLAPSERESET\n");
3378 item->state &= ~TVIS_EXPANDEDONCE;
3379 TREEVIEW_RemoveAllChildren(infoPtr, item);
3380 item->cChildren = old_cChildren;
3382 if (!wasExpanded)
3383 return FALSE;
3385 if (item->firstChild)
3387 TREEVIEW_ITEM *i, *sibling;
3389 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3391 for (i = item->firstChild; i != sibling;
3392 i = TREEVIEW_GetNextListItem(infoPtr, i))
3394 i->visibleOrder = -1;
3398 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3400 if (nextItem)
3401 scrollDist = -(scrollDist - nextItem->rect.top);
3403 if (bSetSelection)
3405 /* Don't call DoSelectItem, it sends notifications. */
3406 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3407 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3408 item->state |= TVIS_SELECTED;
3409 infoPtr->selectedItem = item;
3412 TREEVIEW_UpdateScrollBars(infoPtr);
3414 scrollRect.left = 0;
3415 scrollRect.right = infoPtr->clientWidth;
3416 scrollRect.bottom = infoPtr->clientHeight;
3418 if (nextItem)
3420 scrollRect.top = nextItem->rect.top;
3422 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3423 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3424 TREEVIEW_Invalidate(infoPtr, item);
3425 } else {
3426 scrollRect.top = item->rect.top;
3427 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3430 TREEVIEW_SetFirstVisible(infoPtr,
3431 bSetFirstVisible ? item : infoPtr->firstVisible,
3432 TRUE);
3434 return wasExpanded;
3437 static BOOL
3438 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3439 BOOL partial, BOOL user)
3441 LONG scrollDist;
3442 LONG orgNextTop = 0;
3443 RECT scrollRect;
3444 TREEVIEW_ITEM *nextItem, *tmpItem;
3445 BOOL sendsNotifications;
3447 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3449 if (!TREEVIEW_HasChildren(infoPtr, item))
3450 return FALSE;
3452 tmpItem = item; nextItem = NULL;
3453 while (tmpItem)
3455 if (tmpItem->nextSibling)
3457 nextItem = tmpItem->nextSibling;
3458 break;
3460 tmpItem = tmpItem->parent;
3463 if (nextItem)
3464 orgNextTop = nextItem->rect.top;
3466 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3468 sendsNotifications = user || ((item->cChildren != 0) &&
3469 !(item->state & TVIS_EXPANDEDONCE));
3470 if (sendsNotifications)
3472 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3474 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3475 return FALSE;
3478 if (!item->firstChild)
3479 return FALSE;
3481 item->state |= TVIS_EXPANDED;
3483 if (partial)
3484 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3486 if (ISVISIBLE(item))
3488 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3489 TREEVIEW_UpdateSubTree(infoPtr, item);
3490 TREEVIEW_UpdateScrollBars(infoPtr);
3492 scrollRect.left = 0;
3493 scrollRect.bottom = infoPtr->treeHeight;
3494 scrollRect.right = infoPtr->clientWidth;
3495 if (nextItem)
3497 scrollDist = nextItem->rect.top - orgNextTop;
3498 scrollRect.top = orgNextTop;
3500 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3501 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3502 TREEVIEW_Invalidate (infoPtr, item);
3503 } else {
3504 scrollRect.top = item->rect.top;
3505 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3508 /* Scroll up so that as many children as possible are visible.
3509 * This fails when expanding causes an HScroll bar to appear, but we
3510 * don't know that yet, so the last item is obscured. */
3511 if (item->firstChild != NULL)
3513 int nChildren = item->lastChild->visibleOrder
3514 - item->firstChild->visibleOrder + 1;
3516 int visible_pos = item->visibleOrder
3517 - infoPtr->firstVisible->visibleOrder;
3519 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3521 if (visible_pos > 0 && nChildren > rows_below)
3523 int scroll = nChildren - rows_below;
3525 if (scroll > visible_pos)
3526 scroll = visible_pos;
3528 if (scroll > 0)
3530 TREEVIEW_ITEM *newFirstVisible
3531 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3532 scroll);
3535 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3541 if (sendsNotifications) {
3542 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3543 item->state |= TVIS_EXPANDEDONCE;
3546 return TRUE;
3549 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3550 to mouse messages and TVM_SELECTITEM.
3552 selection - previously selected item, used to collapse a part of a tree
3553 item - new selected item
3555 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3556 HTREEITEM selection, HTREEITEM item)
3558 TREEVIEW_ITEM *prev, *curr;
3560 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3562 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3565 * Close the previous item and its ancestors as long as they are not
3566 * ancestors of the current item
3568 for (prev = selection; prev && TREEVIEW_ValidItem(infoPtr, prev); prev = prev->parent)
3570 for (curr = item; curr && TREEVIEW_ValidItem(infoPtr, curr); curr = curr->parent)
3572 if (curr == prev)
3573 goto finish;
3575 TREEVIEW_Collapse(infoPtr, prev, FALSE, TRUE);
3578 finish:
3580 * Expand the current item
3582 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3585 static BOOL
3586 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3588 TRACE("item=%p, user=%d\n", item, user);
3590 if (item->state & TVIS_EXPANDED)
3591 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3592 else
3593 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3596 static VOID
3597 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3599 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3601 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3603 if (TREEVIEW_HasChildren(infoPtr, item))
3604 TREEVIEW_ExpandAll(infoPtr, item);
3608 /* Note:If the specified item is the child of a collapsed parent item,
3609 the parent's list of child items is (recursively) expanded to reveal the
3610 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3611 know if it also applies here.
3614 static LRESULT
3615 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3617 if (!TREEVIEW_ValidItem(infoPtr, item))
3618 return 0;
3620 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3621 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3622 flag, item->state);
3624 switch (flag & TVE_TOGGLE)
3626 case TVE_COLLAPSE:
3627 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3628 FALSE);
3630 case TVE_EXPAND:
3631 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3632 FALSE);
3634 case TVE_TOGGLE:
3635 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3637 default:
3638 return 0;
3642 /* Hit-Testing **********************************************************/
3644 static TREEVIEW_ITEM *
3645 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3647 TREEVIEW_ITEM *item;
3648 LONG row;
3650 if (!infoPtr->firstVisible)
3651 return NULL;
3653 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3655 for (item = infoPtr->firstVisible; item != NULL;
3656 item = TREEVIEW_GetNextListItem(infoPtr, item))
3658 if (row >= item->visibleOrder
3659 && row < item->visibleOrder + item->iIntegral)
3660 break;
3663 return item;
3666 static TREEVIEW_ITEM *
3667 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3669 TREEVIEW_ITEM *item;
3670 RECT rect;
3671 UINT status;
3672 LONG x, y;
3674 lpht->hItem = 0;
3675 GetClientRect(infoPtr->hwnd, &rect);
3676 status = 0;
3677 x = lpht->pt.x;
3678 y = lpht->pt.y;
3680 if (x < rect.left)
3682 status |= TVHT_TOLEFT;
3684 else if (x > rect.right)
3686 status |= TVHT_TORIGHT;
3689 if (y < rect.top)
3691 status |= TVHT_ABOVE;
3693 else if (y > rect.bottom)
3695 status |= TVHT_BELOW;
3698 if (status)
3700 lpht->flags = status;
3701 return NULL;
3704 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3705 if (!item)
3707 lpht->flags = TVHT_NOWHERE;
3708 return NULL;
3711 if (!item->textWidth)
3712 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
3714 if (x >= item->textOffset + item->textWidth)
3716 lpht->flags = TVHT_ONITEMRIGHT;
3718 else if (x >= item->textOffset)
3720 lpht->flags = TVHT_ONITEMLABEL;
3722 else if (x >= item->imageOffset)
3724 lpht->flags = TVHT_ONITEMICON;
3726 else if (x >= item->stateOffset)
3728 lpht->flags = TVHT_ONITEMSTATEICON;
3730 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3732 lpht->flags = TVHT_ONITEMBUTTON;
3734 else
3736 lpht->flags = TVHT_ONITEMINDENT;
3739 lpht->hItem = item;
3740 TRACE("(%d,%d):result 0x%x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3742 return item;
3745 /* Item Label Editing ***************************************************/
3747 static LRESULT
3748 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3750 return (LRESULT)infoPtr->hwndEdit;
3753 static LRESULT CALLBACK
3754 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3756 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3757 BOOL bCancel = FALSE;
3758 LRESULT rc;
3760 switch (uMsg)
3762 case WM_PAINT:
3763 TRACE("WM_PAINT start\n");
3764 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3765 lParam);
3766 TRACE("WM_PAINT done\n");
3767 return rc;
3769 case WM_KILLFOCUS:
3770 if (infoPtr->bIgnoreEditKillFocus)
3771 return TRUE;
3772 break;
3774 case WM_DESTROY:
3776 WNDPROC editProc = infoPtr->wpEditOrig;
3777 infoPtr->wpEditOrig = 0;
3778 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3779 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3782 case WM_GETDLGCODE:
3783 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3785 case WM_KEYDOWN:
3786 if (wParam == VK_ESCAPE)
3788 bCancel = TRUE;
3789 break;
3791 else if (wParam == VK_RETURN)
3793 break;
3796 /* fall through */
3797 default:
3798 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3801 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3802 /* eg. Using a messagebox */
3804 infoPtr->bIgnoreEditKillFocus = TRUE;
3805 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3806 infoPtr->bIgnoreEditKillFocus = FALSE;
3808 return 0;
3812 /* should handle edit control messages here */
3814 static LRESULT
3815 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3817 TRACE("code=0x%x, id=0x%x, handle=0x%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3819 switch (HIWORD(wParam))
3821 case EN_UPDATE:
3824 * Adjust the edit window size
3826 WCHAR buffer[1024];
3827 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3828 HDC hdc = GetDC(infoPtr->hwndEdit);
3829 SIZE sz;
3830 HFONT hFont, hOldFont = 0;
3832 TRACE("edit=%p\n", infoPtr->hwndEdit);
3834 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3836 infoPtr->bLabelChanged = TRUE;
3838 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
3840 /* Select font to get the right dimension of the string */
3841 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3843 if (hFont != 0)
3845 hOldFont = SelectObject(hdc, hFont);
3848 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3850 TEXTMETRICW textMetric;
3852 /* Add Extra spacing for the next character */
3853 GetTextMetricsW(hdc, &textMetric);
3854 sz.cx += (textMetric.tmMaxCharWidth * 2);
3856 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3857 sz.cx = min(sz.cx,
3858 infoPtr->clientWidth - editItem->textOffset + 2);
3860 SetWindowPos(infoPtr->hwndEdit,
3861 HWND_TOP,
3864 sz.cx,
3865 editItem->rect.bottom - editItem->rect.top + 3,
3866 SWP_NOMOVE | SWP_DRAWFRAME);
3869 if (hFont != 0)
3871 SelectObject(hdc, hOldFont);
3874 ReleaseDC(infoPtr->hwnd, hdc);
3875 break;
3877 case EN_KILLFOCUS:
3878 /* apparently we should respect passed handle value */
3879 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3881 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3882 break;
3884 default:
3885 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3888 return 0;
3891 static HWND
3892 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3894 HWND hwnd = infoPtr->hwnd;
3895 HWND hwndEdit;
3896 SIZE sz;
3897 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3898 HDC hdc;
3899 HFONT hOldFont=0;
3900 TEXTMETRICW textMetric;
3902 TRACE("%p %p\n", hwnd, hItem);
3903 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3904 return NULL;
3906 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3907 return NULL;
3909 if (infoPtr->hwndEdit)
3910 return infoPtr->hwndEdit;
3912 infoPtr->bLabelChanged = FALSE;
3914 /* make edit item visible */
3915 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3917 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3919 hdc = GetDC(hwnd);
3920 /* Select the font to get appropriate metric dimensions */
3921 if (infoPtr->hFont != 0)
3923 hOldFont = SelectObject(hdc, infoPtr->hFont);
3926 /* Get string length in pixels */
3927 if (hItem->pszText)
3928 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3929 &sz);
3930 else
3931 GetTextExtentPoint32A(hdc, "", 0, &sz);
3933 /* Add Extra spacing for the next character */
3934 GetTextMetricsW(hdc, &textMetric);
3935 sz.cx += (textMetric.tmMaxCharWidth * 2);
3937 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3938 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3940 if (infoPtr->hFont != 0)
3942 SelectObject(hdc, hOldFont);
3945 ReleaseDC(hwnd, hdc);
3947 infoPtr->editItem = hItem;
3949 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3950 WC_EDITW,
3952 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3953 WS_CLIPSIBLINGS | ES_WANTRETURN |
3954 ES_LEFT, hItem->textOffset - 2,
3955 hItem->rect.top - 1, sz.cx + 3,
3956 hItem->rect.bottom -
3957 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3958 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3960 infoPtr->hwndEdit = hwndEdit;
3962 /* Get a 2D border. */
3963 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3964 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3965 SetWindowLongW(hwndEdit, GWL_STYLE,
3966 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3968 SendMessageW(hwndEdit, WM_SETFONT,
3969 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3971 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3972 (DWORD_PTR)
3973 TREEVIEW_Edit_SubclassProc);
3974 if (hItem->pszText)
3975 SetWindowTextW(hwndEdit, hItem->pszText);
3977 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3979 DestroyWindow(hwndEdit);
3980 infoPtr->hwndEdit = 0;
3981 infoPtr->editItem = NULL;
3982 return NULL;
3985 SetFocus(hwndEdit);
3986 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3987 ShowWindow(hwndEdit, SW_SHOW);
3989 return hwndEdit;
3993 static LRESULT
3994 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3996 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3997 NMTVDISPINFOW tvdi;
3998 BOOL bCommit;
3999 WCHAR tmpText[1024] = { '\0' };
4000 WCHAR *newText = tmpText;
4001 int iLength = 0;
4003 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
4005 tvdi.item.mask = 0;
4006 tvdi.item.hItem = editedItem;
4007 tvdi.item.state = editedItem->state;
4008 tvdi.item.lParam = editedItem->lParam;
4010 if (!bCancel)
4012 if (!infoPtr->bNtfUnicode)
4013 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
4014 else
4015 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
4017 if (iLength >= 1023)
4019 ERR("Insufficient space to retrieve new item label\n");
4022 tvdi.item.mask = TVIF_TEXT;
4023 tvdi.item.pszText = tmpText;
4024 tvdi.item.cchTextMax = iLength + 1;
4026 else
4028 tvdi.item.pszText = NULL;
4029 tvdi.item.cchTextMax = 0;
4032 bCommit = TREEVIEW_SendRealNotify(infoPtr, TVN_ENDLABELEDITW, &tvdi.hdr);
4034 if (!bCancel && bCommit) /* Apply the changes */
4036 if (!infoPtr->bNtfUnicode)
4038 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
4039 newText = heap_alloc(len * sizeof(WCHAR));
4040 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
4041 iLength = len - 1;
4044 if (strcmpW(newText, editedItem->pszText) != 0)
4046 WCHAR *ptr = heap_realloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
4047 if (ptr == NULL)
4049 ERR("OutOfMemory, cannot allocate space for label\n");
4050 if (newText != tmpText) heap_free(newText);
4051 DestroyWindow(infoPtr->hwndEdit);
4052 infoPtr->hwndEdit = 0;
4053 infoPtr->editItem = NULL;
4054 return FALSE;
4056 else
4058 editedItem->pszText = ptr;
4059 editedItem->cchTextMax = iLength + 1;
4060 strcpyW(editedItem->pszText, newText);
4061 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4064 if (newText != tmpText) heap_free(newText);
4067 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4068 DestroyWindow(infoPtr->hwndEdit);
4069 infoPtr->hwndEdit = 0;
4070 infoPtr->editItem = NULL;
4071 return TRUE;
4074 static LRESULT
4075 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4077 if (wParam != TV_EDIT_TIMER)
4079 ERR("got unknown timer\n");
4080 return 1;
4083 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4084 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4086 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4088 return 0;
4092 /* Mouse Tracking/Drag **************************************************/
4094 /***************************************************************************
4095 * This is quite unusual piece of code, but that's how it's implemented in
4096 * Windows.
4098 static LRESULT
4099 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4101 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4102 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4103 RECT r;
4104 MSG msg;
4106 r.top = pt.y - cyDrag;
4107 r.left = pt.x - cxDrag;
4108 r.bottom = pt.y + cyDrag;
4109 r.right = pt.x + cxDrag;
4111 SetCapture(infoPtr->hwnd);
4113 while (1)
4115 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4117 if (msg.message == WM_MOUSEMOVE)
4119 pt.x = (short)LOWORD(msg.lParam);
4120 pt.y = (short)HIWORD(msg.lParam);
4121 if (PtInRect(&r, pt))
4122 continue;
4123 else
4125 ReleaseCapture();
4126 return 1;
4129 else if (msg.message >= WM_LBUTTONDOWN &&
4130 msg.message <= WM_RBUTTONDBLCLK)
4132 break;
4135 DispatchMessageW(&msg);
4138 if (GetCapture() != infoPtr->hwnd)
4139 return 0;
4142 ReleaseCapture();
4143 return 0;
4147 static LRESULT
4148 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4150 TREEVIEW_ITEM *item;
4151 TVHITTESTINFO hit;
4153 TRACE("\n");
4154 SetFocus(infoPtr->hwnd);
4156 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4158 /* If there is pending 'edit label' event - kill it now */
4159 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4162 hit.pt.x = (short)LOWORD(lParam);
4163 hit.pt.y = (short)HIWORD(lParam);
4165 item = TREEVIEW_HitTest(infoPtr, &hit);
4166 if (!item)
4167 return 0;
4168 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4170 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4171 { /* FIXME! */
4172 switch (hit.flags)
4174 case TVHT_ONITEMRIGHT:
4175 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4176 break;
4178 case TVHT_ONITEMINDENT:
4179 if (!(infoPtr->dwStyle & TVS_HASLINES))
4181 break;
4183 else
4185 int level = hit.pt.x / infoPtr->uIndent;
4186 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4188 while (item->iLevel > level)
4190 item = item->parent;
4193 /* fall through */
4196 case TVHT_ONITEMLABEL:
4197 case TVHT_ONITEMICON:
4198 case TVHT_ONITEMBUTTON:
4199 TREEVIEW_Toggle(infoPtr, item, TRUE);
4200 break;
4202 case TVHT_ONITEMSTATEICON:
4203 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4204 TREEVIEW_ToggleItemState(infoPtr, item);
4205 else
4206 TREEVIEW_Toggle(infoPtr, item, TRUE);
4207 break;
4210 return TRUE;
4214 static LRESULT
4215 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4217 BOOL do_track, do_select, bDoLabelEdit;
4218 HWND hwnd = infoPtr->hwnd;
4219 TVHITTESTINFO ht;
4221 /* If Edit control is active - kill it and return.
4222 * The best way to do it is to set focus to itself.
4223 * Edit control subclassed procedure will automatically call
4224 * EndEditLabelNow.
4226 if (infoPtr->hwndEdit)
4228 SetFocus(hwnd);
4229 return 0;
4232 ht.pt.x = (short)LOWORD(lParam);
4233 ht.pt.y = (short)HIWORD(lParam);
4235 TREEVIEW_HitTest(infoPtr, &ht);
4236 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4238 /* update focusedItem and redraw both items */
4239 if (ht.hItem)
4241 BOOL do_focus;
4243 if (TREEVIEW_IsFullRowSelect(infoPtr))
4244 do_focus = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEM | TVHT_ONITEMRIGHT);
4245 else
4246 do_focus = ht.flags & TVHT_ONITEM;
4248 if (do_focus)
4250 infoPtr->focusedItem = ht.hItem;
4251 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4252 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4256 if (!(infoPtr->dwStyle & TVS_DISABLEDRAGDROP))
4258 if (TREEVIEW_IsFullRowSelect(infoPtr))
4259 do_track = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEM | TVHT_ONITEMRIGHT);
4260 else
4261 do_track = ht.flags & TVHT_ONITEM;
4263 else
4264 do_track = FALSE;
4267 * If the style allows editing and the node is already selected
4268 * and the click occurred on the item label...
4270 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4271 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4273 /* Send NM_CLICK right away */
4274 if (!do_track && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4275 goto setfocus;
4277 if (ht.flags & TVHT_ONITEMBUTTON)
4279 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4280 goto setfocus;
4282 else if (do_track)
4283 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4284 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4286 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4287 infoPtr->dropItem = ht.hItem;
4289 /* clean up focusedItem as we dragged and won't select this item */
4290 if(infoPtr->focusedItem)
4292 /* refresh the item that was focused */
4293 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4294 infoPtr->focusedItem = NULL;
4296 /* refresh the selected item to return the filled background */
4297 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4300 return 0;
4304 if (do_track && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4305 goto setfocus;
4307 if (TREEVIEW_IsFullRowSelect(infoPtr))
4308 do_select = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEMICON | TVHT_ONITEMLABEL | TVHT_ONITEMRIGHT);
4309 else
4310 do_select = ht.flags & (TVHT_ONITEMICON | TVHT_ONITEMLABEL);
4312 if (bDoLabelEdit)
4314 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4315 KillTimer(hwnd, TV_EDIT_TIMER);
4317 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4318 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4320 else if (do_select)
4322 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4324 /* Select the current item */
4325 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4326 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4328 else if (ht.flags & TVHT_ONITEMSTATEICON)
4330 /* TVS_CHECKBOXES requires us to toggle the current state */
4331 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4332 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4335 setfocus:
4336 SetFocus(hwnd);
4337 return 0;
4341 static LRESULT
4342 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4344 TVHITTESTINFO ht;
4346 if (infoPtr->hwndEdit)
4348 SetFocus(infoPtr->hwnd);
4349 return 0;
4352 ht.pt.x = (short)LOWORD(lParam);
4353 ht.pt.y = (short)HIWORD(lParam);
4355 if (TREEVIEW_HitTest(infoPtr, &ht))
4357 infoPtr->focusedItem = ht.hItem;
4358 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4359 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4362 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4364 if (ht.hItem)
4366 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4367 infoPtr->dropItem = ht.hItem;
4370 else
4372 SetFocus(infoPtr->hwnd);
4373 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4375 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4376 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4377 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4381 if (ht.hItem)
4383 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4384 infoPtr->focusedItem = infoPtr->selectedItem;
4385 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4388 return 0;
4391 static LRESULT
4392 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4394 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4395 INT cx, cy;
4396 HDC hdc, htopdc;
4397 HWND hwtop;
4398 HBITMAP hbmp, hOldbmp;
4399 SIZE size;
4400 RECT rc;
4401 HFONT hOldFont;
4403 TRACE("\n");
4405 if (!(infoPtr->himlNormal))
4406 return 0;
4408 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4409 return 0;
4411 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4413 hwtop = GetDesktopWindow();
4414 htopdc = GetDC(hwtop);
4415 hdc = CreateCompatibleDC(htopdc);
4417 hOldFont = SelectObject(hdc, infoPtr->hFont);
4419 if (dragItem->pszText)
4420 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4421 &size);
4422 else
4423 GetTextExtentPoint32A(hdc, "", 0, &size);
4425 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4426 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4427 hOldbmp = SelectObject(hdc, hbmp);
4429 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4430 size.cx += cx;
4431 if (cy > size.cy)
4432 size.cy = cy;
4434 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4435 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4436 ILD_NORMAL);
4439 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4440 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4443 /* draw item text */
4445 SetRect(&rc, cx, 0, size.cx, size.cy);
4447 if (dragItem->pszText)
4448 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4449 DT_LEFT);
4451 SelectObject(hdc, hOldFont);
4452 SelectObject(hdc, hOldbmp);
4454 ImageList_Add(infoPtr->dragList, hbmp, 0);
4456 DeleteDC(hdc);
4457 DeleteObject(hbmp);
4458 ReleaseDC(hwtop, htopdc);
4460 return (LRESULT)infoPtr->dragList;
4463 /* Selection ************************************************************/
4465 static LRESULT
4466 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4467 INT cause)
4469 TREEVIEW_ITEM *prevSelect;
4471 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4473 TRACE("Entering item %p (%s), flag 0x%x, cause 0x%x, state 0x%x\n",
4474 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4475 newSelect ? newSelect->state : 0);
4477 /* reset and redraw focusedItem if focusedItem was set so we don't */
4478 /* have to worry about the previously focused item when we set a new one */
4479 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4480 infoPtr->focusedItem = NULL;
4482 switch (action)
4484 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4485 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4486 /* Fall through */
4487 case TVGN_CARET:
4488 prevSelect = infoPtr->selectedItem;
4490 if (prevSelect == newSelect) {
4491 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4492 break;
4495 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4496 TVN_SELCHANGINGW,
4497 cause,
4498 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4499 prevSelect,
4500 newSelect))
4501 return FALSE;
4503 if (prevSelect)
4504 prevSelect->state &= ~TVIS_SELECTED;
4505 if (newSelect)
4506 newSelect->state |= TVIS_SELECTED;
4508 infoPtr->selectedItem = newSelect;
4510 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4512 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4513 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4515 TREEVIEW_SendTreeviewNotify(infoPtr,
4516 TVN_SELCHANGEDW,
4517 cause,
4518 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4519 prevSelect,
4520 newSelect);
4521 break;
4523 case TVGN_DROPHILITE:
4524 prevSelect = infoPtr->dropItem;
4526 if (prevSelect)
4527 prevSelect->state &= ~TVIS_DROPHILITED;
4529 infoPtr->dropItem = newSelect;
4531 if (newSelect)
4532 newSelect->state |= TVIS_DROPHILITED;
4534 TREEVIEW_Invalidate(infoPtr, prevSelect);
4535 TREEVIEW_Invalidate(infoPtr, newSelect);
4536 break;
4538 case TVGN_FIRSTVISIBLE:
4539 if (newSelect != NULL)
4541 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4542 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4543 TREEVIEW_Invalidate(infoPtr, NULL);
4545 break;
4548 TRACE("Leaving state 0x%x\n", newSelect ? newSelect->state : 0);
4549 return TRUE;
4552 /* FIXME: handle NM_KILLFOCUS etc */
4553 static LRESULT
4554 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4556 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4558 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4559 return FALSE;
4561 if (item == infoPtr->selectedItem)
4562 return TRUE;
4564 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4566 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4567 return FALSE;
4569 TREEVIEW_SingleExpand(infoPtr, selection, item);
4571 return TRUE;
4574 /*************************************************************************
4575 * TREEVIEW_ProcessLetterKeys
4577 * Processes keyboard messages generated by pressing the letter keys
4578 * on the keyboard.
4579 * What this does is perform a case insensitive search from the
4580 * current position with the following quirks:
4581 * - If two chars or more are pressed in quick succession we search
4582 * for the corresponding string (e.g. 'abc').
4583 * - If there is a delay we wipe away the current search string and
4584 * restart with just that char.
4585 * - If the user keeps pressing the same character, whether slowly or
4586 * fast, so that the search string is entirely composed of this
4587 * character ('aaaaa' for instance), then we search for first item
4588 * that starting with that character.
4589 * - If the user types the above character in quick succession, then
4590 * we must also search for the corresponding string ('aaaaa'), and
4591 * go to that string if there is a match.
4593 * RETURNS
4595 * Zero.
4597 * BUGS
4599 * - The current implementation has a list of characters it will
4600 * accept and it ignores everything else. In particular it will
4601 * ignore accentuated characters which seems to match what
4602 * Windows does. But I'm not sure it makes sense to follow
4603 * Windows there.
4604 * - We don't sound a beep when the search fails.
4605 * - The search should start from the focused item, not from the selected
4606 * item. One reason for this is to allow for multiple selections in trees.
4607 * But currently infoPtr->focusedItem does not seem very usable.
4609 * SEE ALSO
4611 * TREEVIEW_ProcessLetterKeys
4613 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4615 HTREEITEM nItem;
4616 HTREEITEM endidx,idx;
4617 TVITEMEXW item;
4618 WCHAR buffer[MAX_PATH];
4619 DWORD timestamp,elapsed;
4621 /* simple parameter checking */
4622 if (!charCode || !keyData) return 0;
4624 /* only allow the valid WM_CHARs through */
4625 if (!isalnum(charCode) &&
4626 charCode != '.' && charCode != '`' && charCode != '!' &&
4627 charCode != '@' && charCode != '#' && charCode != '$' &&
4628 charCode != '%' && charCode != '^' && charCode != '&' &&
4629 charCode != '*' && charCode != '(' && charCode != ')' &&
4630 charCode != '-' && charCode != '_' && charCode != '+' &&
4631 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4632 charCode != '}' && charCode != '[' && charCode != '{' &&
4633 charCode != '/' && charCode != '?' && charCode != '>' &&
4634 charCode != '<' && charCode != ',' && charCode != '~')
4635 return 0;
4637 /* compute how much time elapsed since last keypress */
4638 timestamp = GetTickCount();
4639 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4640 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4641 } else {
4642 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4645 /* update the search parameters */
4646 infoPtr->lastKeyPressTimestamp=timestamp;
4647 if (elapsed < KEY_DELAY) {
4648 if (infoPtr->nSearchParamLength < ARRAY_SIZE(infoPtr->szSearchParam)) {
4649 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4651 if (infoPtr->charCode != charCode) {
4652 infoPtr->charCode=charCode=0;
4654 } else {
4655 infoPtr->charCode=charCode;
4656 infoPtr->szSearchParam[0]=charCode;
4657 infoPtr->nSearchParamLength=1;
4658 /* Redundant with the 1 char string */
4659 charCode=0;
4662 /* and search from the current position */
4663 nItem=NULL;
4664 if (infoPtr->selectedItem != NULL) {
4665 endidx=infoPtr->selectedItem;
4666 /* if looking for single character match,
4667 * then we must always move forward
4669 if (infoPtr->nSearchParamLength == 1)
4670 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4671 else
4672 idx=endidx;
4673 } else {
4674 endidx=NULL;
4675 idx=infoPtr->root->firstChild;
4677 do {
4678 /* At the end point, sort out wrapping */
4679 if (idx == NULL) {
4681 /* If endidx is null, stop at the last item (ie top to bottom) */
4682 if (endidx == NULL)
4683 break;
4685 /* Otherwise, start again at the very beginning */
4686 idx=infoPtr->root->firstChild;
4688 /* But if we are stopping on the first child, end now! */
4689 if (idx == endidx) break;
4692 /* get item */
4693 ZeroMemory(&item, sizeof(item));
4694 item.mask = TVIF_TEXT;
4695 item.hItem = idx;
4696 item.pszText = buffer;
4697 item.cchTextMax = ARRAY_SIZE(buffer);
4698 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4700 /* check for a match */
4701 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4702 nItem=idx;
4703 break;
4704 } else if ( (charCode != 0) && (nItem == NULL) &&
4705 (nItem != infoPtr->selectedItem) &&
4706 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4707 /* This would work but we must keep looking for a longer match */
4708 nItem=idx;
4710 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4711 } while (idx != endidx);
4713 if (nItem != NULL) {
4714 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4715 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4719 return 0;
4722 /* Scrolling ************************************************************/
4724 static LRESULT
4725 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4727 int viscount;
4728 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4729 HTREEITEM newFirstVisible = NULL;
4730 int visible_pos = -1;
4732 if (!TREEVIEW_ValidItem(infoPtr, item))
4733 return FALSE;
4735 if (!ISVISIBLE(item))
4737 /* Expand parents as necessary. */
4738 HTREEITEM parent;
4740 /* see if we are trying to ensure that root is visible */
4741 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4742 parent = item->parent;
4743 else
4744 parent = item; /* this item is the topmost item */
4746 while (parent != infoPtr->root)
4748 if (!(parent->state & TVIS_EXPANDED))
4749 TREEVIEW_Expand(infoPtr, parent, FALSE, TRUE);
4751 parent = parent->parent;
4755 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4757 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4758 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4760 if (hasFirstVisible)
4761 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4763 if (visible_pos < 0)
4765 /* item is before the start of the list: put it at the top. */
4766 newFirstVisible = item;
4768 else if (visible_pos >= viscount
4769 /* Sometimes, before we are displayed, GVC is 0, causing us to
4770 * spuriously scroll up. */
4771 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4773 /* item is past the end of the list. */
4774 int scroll = visible_pos - viscount;
4776 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4777 scroll + 1);
4780 if (bHScroll)
4782 /* Scroll window so item's text is visible as much as possible */
4783 /* Calculation of amount of extra space is taken from EditLabel code */
4784 INT pos, x;
4785 TEXTMETRICW textMetric;
4786 HDC hdc = GetWindowDC(infoPtr->hwnd);
4788 x = item->textWidth;
4790 GetTextMetricsW(hdc, &textMetric);
4791 ReleaseDC(infoPtr->hwnd, hdc);
4793 x += (textMetric.tmMaxCharWidth * 2);
4794 x = max(x, textMetric.tmMaxCharWidth * 3);
4796 if (item->textOffset < 0)
4797 pos = item->textOffset;
4798 else if (item->textOffset + x > infoPtr->clientWidth)
4800 if (x > infoPtr->clientWidth)
4801 pos = item->textOffset;
4802 else
4803 pos = item->textOffset + x - infoPtr->clientWidth;
4805 else
4806 pos = 0;
4808 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4811 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4813 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4815 return TRUE;
4818 return FALSE;
4821 static VOID
4822 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4823 TREEVIEW_ITEM *newFirstVisible,
4824 BOOL bUpdateScrollPos)
4826 int gap_size;
4828 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4830 if (newFirstVisible != NULL)
4832 /* Prevent an empty gap from appearing at the bottom... */
4833 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4834 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4836 if (gap_size > 0)
4838 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4839 -gap_size);
4841 /* ... unless we just don't have enough items. */
4842 if (newFirstVisible == NULL)
4843 newFirstVisible = infoPtr->root->firstChild;
4847 if (infoPtr->firstVisible != newFirstVisible)
4849 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4851 infoPtr->firstVisible = newFirstVisible;
4852 TREEVIEW_Invalidate(infoPtr, NULL);
4854 else
4856 TREEVIEW_ITEM *item;
4857 int scroll = infoPtr->uItemHeight *
4858 (infoPtr->firstVisible->visibleOrder
4859 - newFirstVisible->visibleOrder);
4861 infoPtr->firstVisible = newFirstVisible;
4863 for (item = infoPtr->root->firstChild; item != NULL;
4864 item = TREEVIEW_GetNextListItem(infoPtr, item))
4866 item->rect.top += scroll;
4867 item->rect.bottom += scroll;
4870 if (bUpdateScrollPos)
4871 SetScrollPos(infoPtr->hwnd, SB_VERT,
4872 newFirstVisible->visibleOrder, TRUE);
4874 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4879 /************************************************************************
4880 * VScroll is always in units of visible items. i.e. we always have a
4881 * visible item aligned to the top of the control. (Unless we have no
4882 * items at all.)
4884 static LRESULT
4885 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4887 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4888 TREEVIEW_ITEM *newFirstVisible = NULL;
4890 int nScrollCode = LOWORD(wParam);
4892 TRACE("wp %lx\n", wParam);
4894 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4895 return 0;
4897 if (!oldFirstVisible)
4899 assert(infoPtr->root->firstChild == NULL);
4900 return 0;
4903 switch (nScrollCode)
4905 case SB_TOP:
4906 newFirstVisible = infoPtr->root->firstChild;
4907 break;
4909 case SB_BOTTOM:
4910 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4911 break;
4913 case SB_LINEUP:
4914 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4915 break;
4917 case SB_LINEDOWN:
4918 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4919 break;
4921 case SB_PAGEUP:
4922 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4923 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4924 break;
4926 case SB_PAGEDOWN:
4927 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4928 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4929 break;
4931 case SB_THUMBTRACK:
4932 case SB_THUMBPOSITION:
4933 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4934 infoPtr->root->firstChild,
4935 (LONG)(SHORT)HIWORD(wParam));
4936 break;
4938 case SB_ENDSCROLL:
4939 return 0;
4942 if (newFirstVisible != NULL)
4944 if (newFirstVisible != oldFirstVisible)
4945 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4946 nScrollCode != SB_THUMBTRACK);
4947 else if (nScrollCode == SB_THUMBPOSITION)
4948 SetScrollPos(infoPtr->hwnd, SB_VERT,
4949 newFirstVisible->visibleOrder, TRUE);
4952 return 0;
4955 static LRESULT
4956 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4958 int maxWidth;
4959 int scrollX = infoPtr->scrollX;
4960 int nScrollCode = LOWORD(wParam);
4962 TRACE("wp %lx\n", wParam);
4964 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4965 return FALSE;
4967 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4968 /* shall never occur */
4969 if (maxWidth <= 0)
4971 scrollX = 0;
4972 goto scroll;
4975 switch (nScrollCode)
4977 case SB_LINELEFT:
4978 scrollX -= infoPtr->uItemHeight;
4979 break;
4980 case SB_LINERIGHT:
4981 scrollX += infoPtr->uItemHeight;
4982 break;
4983 case SB_PAGELEFT:
4984 scrollX -= infoPtr->clientWidth;
4985 break;
4986 case SB_PAGERIGHT:
4987 scrollX += infoPtr->clientWidth;
4988 break;
4990 case SB_THUMBTRACK:
4991 case SB_THUMBPOSITION:
4992 scrollX = (int)(SHORT)HIWORD(wParam);
4993 break;
4995 case SB_ENDSCROLL:
4996 return 0;
4999 if (scrollX > maxWidth)
5000 scrollX = maxWidth;
5001 else if (scrollX < 0)
5002 scrollX = 0;
5004 scroll:
5005 if (scrollX != infoPtr->scrollX)
5007 TREEVIEW_ITEM *item;
5008 LONG scroll_pixels = infoPtr->scrollX - scrollX;
5010 for (item = infoPtr->root->firstChild; item != NULL;
5011 item = TREEVIEW_GetNextListItem(infoPtr, item))
5013 item->linesOffset += scroll_pixels;
5014 item->stateOffset += scroll_pixels;
5015 item->imageOffset += scroll_pixels;
5016 item->textOffset += scroll_pixels;
5019 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
5020 infoPtr->scrollX = scrollX;
5021 UpdateWindow(infoPtr->hwnd);
5024 if (nScrollCode != SB_THUMBTRACK)
5025 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
5027 return 0;
5030 static LRESULT
5031 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5033 short wheelDelta;
5034 UINT pulScrollLines = 3;
5036 if (wParam & (MK_SHIFT | MK_CONTROL))
5037 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
5039 if (infoPtr->firstVisible == NULL)
5040 return TRUE;
5042 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
5044 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5045 /* if scrolling changes direction, ignore left overs */
5046 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
5047 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
5048 infoPtr->wheelRemainder += wheelDelta;
5049 else
5050 infoPtr->wheelRemainder = wheelDelta;
5052 if (infoPtr->wheelRemainder && pulScrollLines)
5054 int newDy;
5055 int maxDy;
5056 int lineScroll;
5058 lineScroll = pulScrollLines * (float)infoPtr->wheelRemainder / WHEEL_DELTA;
5059 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / (int)pulScrollLines;
5061 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
5062 maxDy = infoPtr->maxVisibleOrder;
5064 if (newDy > maxDy)
5065 newDy = maxDy;
5067 if (newDy < 0)
5068 newDy = 0;
5070 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
5072 return TRUE;
5075 /* Create/Destroy *******************************************************/
5077 static LRESULT
5078 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5080 RECT rcClient;
5081 TREEVIEW_INFO *infoPtr;
5082 LOGFONTW lf;
5084 TRACE("wnd %p, style 0x%x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5086 infoPtr = heap_alloc_zero(sizeof(TREEVIEW_INFO));
5088 if (infoPtr == NULL)
5090 ERR("could not allocate info memory!\n");
5091 return 0;
5094 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5096 infoPtr->hwnd = hwnd;
5097 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5098 infoPtr->Timer = 0;
5099 infoPtr->uNumItems = 0;
5100 infoPtr->cdmode = 0;
5101 infoPtr->uScrollTime = 300; /* milliseconds */
5102 infoPtr->bRedraw = TRUE;
5104 GetClientRect(hwnd, &rcClient);
5106 /* No scroll bars yet. */
5107 infoPtr->clientWidth = rcClient.right;
5108 infoPtr->clientHeight = rcClient.bottom;
5109 infoPtr->uInternalStatus = 0;
5111 infoPtr->treeWidth = 0;
5112 infoPtr->treeHeight = 0;
5114 infoPtr->uIndent = MINIMUM_INDENT;
5115 infoPtr->selectedItem = NULL;
5116 infoPtr->focusedItem = NULL;
5117 infoPtr->hotItem = NULL;
5118 infoPtr->editItem = NULL;
5119 infoPtr->firstVisible = NULL;
5120 infoPtr->maxVisibleOrder = 0;
5121 infoPtr->dropItem = NULL;
5122 infoPtr->insertMarkItem = NULL;
5123 infoPtr->insertBeforeorAfter = 0;
5124 /* dragList */
5126 infoPtr->scrollX = 0;
5127 infoPtr->wheelRemainder = 0;
5129 infoPtr->clrBk = CLR_NONE; /* use system color */
5130 infoPtr->clrText = CLR_NONE; /* use system color */
5131 infoPtr->clrLine = CLR_DEFAULT;
5132 infoPtr->clrInsertMark = CLR_DEFAULT;
5134 /* hwndToolTip */
5136 infoPtr->hwndEdit = NULL;
5137 infoPtr->wpEditOrig = NULL;
5138 infoPtr->bIgnoreEditKillFocus = FALSE;
5139 infoPtr->bLabelChanged = FALSE;
5141 infoPtr->himlNormal = NULL;
5142 infoPtr->himlState = NULL;
5143 infoPtr->normalImageWidth = 0;
5144 infoPtr->normalImageHeight = 0;
5145 infoPtr->stateImageWidth = 0;
5146 infoPtr->stateImageHeight = 0;
5148 infoPtr->items = DPA_Create(16);
5150 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5151 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5152 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5153 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5154 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5155 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5157 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5159 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5160 infoPtr->root->state = TVIS_EXPANDED;
5161 infoPtr->root->iLevel = -1;
5162 infoPtr->root->visibleOrder = -1;
5164 infoPtr->hwndNotify = lpcs->hwndParent;
5165 infoPtr->hwndToolTip = 0;
5167 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5168 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5170 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5171 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5172 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5173 hwnd, 0, 0, 0);
5175 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5176 ShowScrollBar(hwnd, SB_VERT, FALSE);
5177 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5179 OpenThemeData (hwnd, themeClass);
5181 return 0;
5185 static LRESULT
5186 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5188 TRACE("\n");
5190 /* free item data */
5191 TREEVIEW_RemoveTree(infoPtr);
5192 /* root isn't freed with other items */
5193 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5194 DPA_Destroy(infoPtr->items);
5196 /* Restore original wndproc */
5197 if (infoPtr->hwndEdit)
5198 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5199 (DWORD_PTR)infoPtr->wpEditOrig);
5201 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5203 /* Deassociate treeview from the window before doing anything drastic. */
5204 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5206 DeleteObject(infoPtr->hDefaultFont);
5207 DeleteObject(infoPtr->hBoldFont);
5208 DeleteObject(infoPtr->hUnderlineFont);
5209 DeleteObject(infoPtr->hBoldUnderlineFont);
5210 DestroyWindow(infoPtr->hwndToolTip);
5211 heap_free(infoPtr);
5213 return 0;
5216 /* Miscellaneous Messages ***********************************************/
5218 static LRESULT
5219 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5221 static const struct
5223 unsigned char code;
5225 scroll[] =
5227 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5228 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5229 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5230 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5231 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5232 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5233 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5234 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5235 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5236 #undef SCROLL_ENTRY
5239 if (key >= VK_PRIOR && key <= VK_DOWN)
5241 unsigned char code = scroll[key - VK_PRIOR].code;
5243 (((code & (1 << 7)) == (SB_HORZ << 7))
5244 ? TREEVIEW_HScroll
5245 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5248 return 0;
5251 /************************************************************************
5252 * TREEVIEW_KeyDown
5254 * VK_UP Move selection to the previous non-hidden item.
5255 * VK_DOWN Move selection to the next non-hidden item.
5256 * VK_HOME Move selection to the first item.
5257 * VK_END Move selection to the last item.
5258 * VK_LEFT If expanded then collapse, otherwise move to parent.
5259 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5260 * VK_ADD Expand.
5261 * VK_SUBTRACT Collapse.
5262 * VK_MULTIPLY Expand all.
5263 * VK_PRIOR Move up GetVisibleCount items.
5264 * VK_NEXT Move down GetVisibleCount items.
5265 * VK_BACK Move to parent.
5266 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5268 static LRESULT
5269 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5271 /* If it is non-NULL and different, it will be selected and visible. */
5272 TREEVIEW_ITEM *newSelection = NULL;
5273 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5274 NMTVKEYDOWN nmkeydown;
5276 TRACE("%lx\n", wParam);
5278 nmkeydown.wVKey = wParam;
5279 nmkeydown.flags = 0;
5280 TREEVIEW_SendRealNotify(infoPtr, TVN_KEYDOWN, &nmkeydown.hdr);
5282 if (prevItem == NULL)
5283 return FALSE;
5285 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5286 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5288 switch (wParam)
5290 case VK_UP:
5291 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5292 if (!newSelection)
5293 newSelection = infoPtr->root->firstChild;
5294 break;
5296 case VK_DOWN:
5297 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5298 break;
5300 case VK_RETURN:
5301 TREEVIEW_SendSimpleNotify(infoPtr, NM_RETURN);
5302 break;
5304 case VK_HOME:
5305 newSelection = infoPtr->root->firstChild;
5306 break;
5308 case VK_END:
5309 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5310 break;
5312 case VK_LEFT:
5313 if (prevItem->state & TVIS_EXPANDED)
5315 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5317 else if (prevItem->parent != infoPtr->root)
5319 newSelection = prevItem->parent;
5321 break;
5323 case VK_RIGHT:
5324 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5326 if (!(prevItem->state & TVIS_EXPANDED))
5327 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5328 else
5330 newSelection = prevItem->firstChild;
5334 break;
5336 case VK_MULTIPLY:
5337 TREEVIEW_ExpandAll(infoPtr, prevItem);
5338 break;
5340 case VK_ADD:
5341 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5342 break;
5344 case VK_SUBTRACT:
5345 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5346 break;
5348 case VK_PRIOR:
5349 newSelection
5350 = TREEVIEW_GetListItem(infoPtr, prevItem,
5351 -TREEVIEW_GetVisibleCount(infoPtr));
5352 break;
5354 case VK_NEXT:
5355 newSelection
5356 = TREEVIEW_GetListItem(infoPtr, prevItem,
5357 TREEVIEW_GetVisibleCount(infoPtr));
5358 break;
5360 case VK_BACK:
5361 newSelection = prevItem->parent;
5362 if (newSelection == infoPtr->root)
5363 newSelection = NULL;
5364 break;
5366 case VK_SPACE:
5367 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5368 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5369 break;
5372 if (newSelection && newSelection != prevItem)
5374 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5375 TVC_BYKEYBOARD))
5377 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5381 return FALSE;
5384 static LRESULT
5385 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5387 /* remove hot effect from item */
5388 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5389 infoPtr->hotItem = NULL;
5391 return 0;
5394 static LRESULT
5395 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5397 TRACKMOUSEEVENT trackinfo;
5398 TREEVIEW_ITEM * item;
5399 TVHITTESTINFO ht;
5400 BOOL item_hit;
5402 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5404 /* fill in the TRACKMOUSEEVENT struct */
5405 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5406 trackinfo.dwFlags = TME_QUERY;
5407 trackinfo.hwndTrack = infoPtr->hwnd;
5409 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5410 _TrackMouseEvent(&trackinfo);
5412 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5413 if(!(trackinfo.dwFlags & TME_LEAVE))
5415 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5416 trackinfo.hwndTrack = infoPtr->hwnd;
5417 /* do it as fast as possible, minimal systimer latency will be used */
5418 trackinfo.dwHoverTime = 1;
5420 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5421 /* and can properly deactivate the hot item */
5422 _TrackMouseEvent(&trackinfo);
5425 ht.pt.x = (short)LOWORD(lParam);
5426 ht.pt.y = (short)HIWORD(lParam);
5428 item = TREEVIEW_HitTest(infoPtr, &ht);
5429 item_hit = TREEVIEW_IsItemHit(infoPtr, &ht);
5430 if ((item != infoPtr->hotItem) || !item_hit)
5432 /* redraw old hot item */
5433 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5434 infoPtr->hotItem = NULL;
5435 if (item && item_hit)
5437 infoPtr->hotItem = item;
5438 /* redraw new hot item */
5439 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5443 return 0;
5446 /* Draw themed border */
5447 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5449 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5450 HDC dc;
5451 RECT r;
5452 HRGN cliprgn;
5453 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5454 cyEdge = GetSystemMetrics (SM_CYEDGE);
5456 if (!theme)
5457 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5459 GetWindowRect(infoPtr->hwnd, &r);
5461 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5462 r.right - cxEdge, r.bottom - cyEdge);
5463 if (region != (HRGN)1)
5464 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5465 OffsetRect(&r, -r.left, -r.top);
5467 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5468 OffsetRect(&r, -r.left, -r.top);
5470 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5471 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5472 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5473 ReleaseDC(infoPtr->hwnd, dc);
5475 /* Call default proc to get the scrollbars etc. painted */
5476 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5478 return TRUE;
5481 static LRESULT
5482 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5484 LPNMHDR lpnmh = (LPNMHDR)lParam;
5486 if (lpnmh->code == PGN_CALCSIZE) {
5487 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5489 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5490 lppgc->iWidth = infoPtr->treeWidth;
5491 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5492 infoPtr->treeWidth, infoPtr->clientWidth);
5494 else {
5495 lppgc->iHeight = infoPtr->treeHeight;
5496 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5497 infoPtr->treeHeight, infoPtr->clientHeight);
5499 return 0;
5501 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5504 static LRESULT
5505 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5507 if (wParam == SIZE_RESTORED)
5509 infoPtr->clientWidth = (short)LOWORD(lParam);
5510 infoPtr->clientHeight = (short)HIWORD(lParam);
5512 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5513 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5514 TREEVIEW_UpdateScrollBars(infoPtr);
5516 else
5518 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5521 TREEVIEW_Invalidate(infoPtr, NULL);
5522 return 0;
5525 static LRESULT
5526 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5528 TRACE("(%lx %lx)\n", wParam, lParam);
5530 if (wParam == GWL_STYLE)
5532 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5534 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5536 if (dwNewStyle & TVS_CHECKBOXES)
5538 TREEVIEW_InitCheckboxes(infoPtr);
5539 TRACE("checkboxes enabled\n");
5541 /* set all items to state image index 1 */
5542 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5544 else
5546 FIXME("tried to disable checkboxes\n");
5550 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5552 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5554 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5555 TRACE("tooltips enabled\n");
5557 else
5559 DestroyWindow(infoPtr->hwndToolTip);
5560 infoPtr->hwndToolTip = 0;
5561 TRACE("tooltips disabled\n");
5565 infoPtr->dwStyle = dwNewStyle;
5568 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
5569 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5570 TREEVIEW_UpdateScrollBars(infoPtr);
5571 TREEVIEW_Invalidate(infoPtr, NULL);
5573 return 0;
5576 static LRESULT
5577 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5579 TREEVIEW_ITEM * item;
5580 TVHITTESTINFO ht;
5581 NMMOUSE nmmouse;
5583 GetCursorPos(&ht.pt);
5584 ScreenToClient(infoPtr->hwnd, &ht.pt);
5586 item = TREEVIEW_HitTest(infoPtr, &ht);
5588 memset(&nmmouse, 0, sizeof(nmmouse));
5589 if (item)
5591 nmmouse.dwItemSpec = (DWORD_PTR)item;
5592 nmmouse.dwItemData = item->lParam;
5594 nmmouse.pt.x = 0;
5595 nmmouse.pt.y = 0;
5596 nmmouse.dwHitInfo = lParam;
5597 if (TREEVIEW_SendRealNotify(infoPtr, NM_SETCURSOR, &nmmouse.hdr))
5598 return 0;
5600 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT) && TREEVIEW_IsItemHit(infoPtr, &ht))
5602 SetCursor(infoPtr->hcurHand);
5603 return 0;
5605 else
5606 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5609 static LRESULT
5610 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5612 TRACE("\n");
5614 if (!infoPtr->selectedItem)
5616 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5617 TVC_UNKNOWN);
5620 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5621 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5622 return 0;
5625 static LRESULT
5626 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5628 TRACE("\n");
5630 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5631 UpdateWindow(infoPtr->hwnd);
5632 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5633 return 0;
5636 /* update theme after a WM_THEMECHANGED message */
5637 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5639 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5640 CloseThemeData (theme);
5641 OpenThemeData (infoPtr->hwnd, themeClass);
5642 return 0;
5646 static LRESULT WINAPI
5647 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5649 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5651 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5653 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5654 else
5656 if (uMsg == WM_CREATE)
5657 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5658 else
5659 goto def;
5662 switch (uMsg)
5664 case TVM_CREATEDRAGIMAGE:
5665 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5667 case TVM_DELETEITEM:
5668 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5670 case TVM_EDITLABELA:
5671 case TVM_EDITLABELW:
5672 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5674 case TVM_ENDEDITLABELNOW:
5675 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5677 case TVM_ENSUREVISIBLE:
5678 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5680 case TVM_EXPAND:
5681 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5683 case TVM_GETBKCOLOR:
5684 return TREEVIEW_GetBkColor(infoPtr);
5686 case TVM_GETCOUNT:
5687 return TREEVIEW_GetCount(infoPtr);
5689 case TVM_GETEDITCONTROL:
5690 return TREEVIEW_GetEditControl(infoPtr);
5692 case TVM_GETIMAGELIST:
5693 return TREEVIEW_GetImageList(infoPtr, wParam);
5695 case TVM_GETINDENT:
5696 return TREEVIEW_GetIndent(infoPtr);
5698 case TVM_GETINSERTMARKCOLOR:
5699 return TREEVIEW_GetInsertMarkColor(infoPtr);
5701 case TVM_GETISEARCHSTRINGA:
5702 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5703 return 0;
5705 case TVM_GETISEARCHSTRINGW:
5706 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5707 return 0;
5709 case TVM_GETITEMA:
5710 case TVM_GETITEMW:
5711 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5712 uMsg == TVM_GETITEMW);
5713 case TVM_GETITEMHEIGHT:
5714 return TREEVIEW_GetItemHeight(infoPtr);
5716 case TVM_GETITEMRECT:
5717 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5719 case TVM_GETITEMSTATE:
5720 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5722 case TVM_GETLINECOLOR:
5723 return TREEVIEW_GetLineColor(infoPtr);
5725 case TVM_GETNEXTITEM:
5726 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5728 case TVM_GETSCROLLTIME:
5729 return TREEVIEW_GetScrollTime(infoPtr);
5731 case TVM_GETTEXTCOLOR:
5732 return TREEVIEW_GetTextColor(infoPtr);
5734 case TVM_GETTOOLTIPS:
5735 return TREEVIEW_GetToolTips(infoPtr);
5737 case TVM_GETUNICODEFORMAT:
5738 return TREEVIEW_GetUnicodeFormat(infoPtr);
5740 case TVM_GETVISIBLECOUNT:
5741 return TREEVIEW_GetVisibleCount(infoPtr);
5743 case TVM_HITTEST:
5744 return (LRESULT)TREEVIEW_HitTest(infoPtr, (TVHITTESTINFO*)lParam);
5746 case TVM_INSERTITEMA:
5747 case TVM_INSERTITEMW:
5748 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5749 uMsg == TVM_INSERTITEMW);
5750 case TVM_SELECTITEM:
5751 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5753 case TVM_SETBKCOLOR:
5754 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5756 case TVM_SETIMAGELIST:
5757 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5759 case TVM_SETINDENT:
5760 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5762 case TVM_SETINSERTMARK:
5763 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5765 case TVM_SETINSERTMARKCOLOR:
5766 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5768 case TVM_SETITEMA:
5769 case TVM_SETITEMW:
5770 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5771 uMsg == TVM_SETITEMW);
5772 case TVM_SETLINECOLOR:
5773 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5775 case TVM_SETITEMHEIGHT:
5776 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5778 case TVM_SETSCROLLTIME:
5779 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5781 case TVM_SETTEXTCOLOR:
5782 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5784 case TVM_SETTOOLTIPS:
5785 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5787 case TVM_SETUNICODEFORMAT:
5788 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5790 case TVM_SORTCHILDREN:
5791 return TREEVIEW_SortChildren(infoPtr, lParam);
5793 case TVM_SORTCHILDRENCB:
5794 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5796 case WM_CHAR:
5797 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5799 case WM_COMMAND:
5800 return TREEVIEW_Command(infoPtr, wParam, lParam);
5802 case WM_DESTROY:
5803 return TREEVIEW_Destroy(infoPtr);
5805 /* WM_ENABLE */
5807 case WM_ERASEBKGND:
5808 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5810 case WM_GETDLGCODE:
5811 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5813 case WM_GETFONT:
5814 return TREEVIEW_GetFont(infoPtr);
5816 case WM_HSCROLL:
5817 return TREEVIEW_HScroll(infoPtr, wParam);
5819 case WM_KEYDOWN:
5820 case WM_SYSKEYDOWN:
5821 return TREEVIEW_KeyDown(infoPtr, wParam);
5823 case WM_KILLFOCUS:
5824 return TREEVIEW_KillFocus(infoPtr);
5826 case WM_LBUTTONDBLCLK:
5827 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5829 case WM_LBUTTONDOWN:
5830 return TREEVIEW_LButtonDown(infoPtr, lParam);
5832 /* WM_MBUTTONDOWN */
5834 case WM_MOUSELEAVE:
5835 return TREEVIEW_MouseLeave(infoPtr);
5837 case WM_MOUSEMOVE:
5838 return TREEVIEW_MouseMove(infoPtr, lParam);
5840 case WM_NCLBUTTONDOWN:
5841 if (infoPtr->hwndEdit)
5842 SetFocus(infoPtr->hwnd);
5843 goto def;
5845 case WM_NCPAINT:
5846 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5848 case WM_NOTIFY:
5849 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5851 case WM_NOTIFYFORMAT:
5852 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5854 case WM_PRINTCLIENT:
5855 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5857 case WM_PAINT:
5858 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5860 case WM_RBUTTONDOWN:
5861 return TREEVIEW_RButtonDown(infoPtr, lParam);
5863 case WM_SETCURSOR:
5864 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5866 case WM_SETFOCUS:
5867 return TREEVIEW_SetFocus(infoPtr);
5869 case WM_SETFONT:
5870 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5872 case WM_SETREDRAW:
5873 return TREEVIEW_SetRedraw(infoPtr, wParam);
5875 case WM_SIZE:
5876 return TREEVIEW_Size(infoPtr, wParam, lParam);
5878 case WM_STYLECHANGED:
5879 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5881 case WM_SYSCOLORCHANGE:
5882 COMCTL32_RefreshSysColors();
5883 return 0;
5885 case WM_TIMER:
5886 return TREEVIEW_HandleTimer(infoPtr, wParam);
5888 case WM_THEMECHANGED:
5889 return TREEVIEW_ThemeChanged (infoPtr);
5891 case WM_VSCROLL:
5892 return TREEVIEW_VScroll(infoPtr, wParam);
5894 /* WM_WININICHANGE */
5896 case WM_MOUSEWHEEL:
5897 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5899 case WM_DRAWITEM:
5900 TRACE("drawItem\n");
5901 goto def;
5903 default:
5904 /* This mostly catches MFC and Delphi messages. :( */
5905 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5906 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5907 def:
5908 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5913 /* Class Registration ***************************************************/
5915 VOID
5916 TREEVIEW_Register(void)
5918 WNDCLASSW wndClass;
5920 TRACE("\n");
5922 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5923 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5924 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5925 wndClass.cbClsExtra = 0;
5926 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5928 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5929 wndClass.hbrBackground = 0;
5930 wndClass.lpszClassName = WC_TREEVIEWW;
5932 RegisterClassW(&wndClass);
5936 VOID
5937 TREEVIEW_Unregister(void)
5939 UnregisterClassW(WC_TREEVIEWW, NULL);
5943 /* Tree Verification ****************************************************/
5945 static inline void
5946 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5948 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5949 const TREEVIEW_ITEM *item)
5951 assert(infoPtr != NULL);
5952 assert(item != NULL);
5954 /* both NULL, or both non-null */
5955 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5957 assert(item->firstChild != item);
5958 assert(item->lastChild != item);
5960 if (item->firstChild)
5962 assert(item->firstChild->parent == item);
5963 assert(item->firstChild->prevSibling == NULL);
5966 if (item->lastChild)
5968 assert(item->lastChild->parent == item);
5969 assert(item->lastChild->nextSibling == NULL);
5972 assert(item->nextSibling != item);
5973 if (item->nextSibling)
5975 assert(item->nextSibling->parent == item->parent);
5976 assert(item->nextSibling->prevSibling == item);
5979 assert(item->prevSibling != item);
5980 if (item->prevSibling)
5982 assert(item->prevSibling->parent == item->parent);
5983 assert(item->prevSibling->nextSibling == item);
5987 static inline void
5988 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5990 assert(item != NULL);
5992 assert(item->parent != NULL);
5993 assert(item->parent != item);
5994 assert(item->iLevel == item->parent->iLevel + 1);
5996 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5998 TREEVIEW_VerifyItemCommon(infoPtr, item);
6000 TREEVIEW_VerifyChildren(infoPtr, item);
6003 static inline void
6004 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
6006 const TREEVIEW_ITEM *child;
6007 assert(item != NULL);
6009 for (child = item->firstChild; child != NULL; child = child->nextSibling)
6010 TREEVIEW_VerifyItem(infoPtr, child);
6013 static inline void
6014 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
6016 TREEVIEW_ITEM *root = infoPtr->root;
6018 assert(root != NULL);
6019 assert(root->iLevel == -1);
6020 assert(root->parent == NULL);
6021 assert(root->prevSibling == NULL);
6023 TREEVIEW_VerifyItemCommon(infoPtr, root);
6025 TREEVIEW_VerifyChildren(infoPtr, root);
6028 static void
6029 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
6031 if (!TRACE_ON(treeview)) return;
6033 assert(infoPtr != NULL);
6034 TREEVIEW_VerifyRoot(infoPtr);