wined3d: Use the bo binding in wined3d_context_gl_map_bo_address().
[wine.git] / dlls / comctl32 / treeview.c
blob06c4586fcd34e20d597ec90caef38dc9b39de710
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 <assert.h>
44 #include <ctype.h>
45 #include <stdarg.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <stdlib.h>
50 #define NONAMELESSUNION
52 #include "windef.h"
53 #include "winbase.h"
54 #include "wingdi.h"
55 #include "winuser.h"
56 #include "winnls.h"
57 #include "commctrl.h"
58 #include "comctl32.h"
59 #include "uxtheme.h"
60 #include "vssym32.h"
61 #include "wine/debug.h"
62 #include "wine/exception.h"
63 #include "wine/heap.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
67 /* internal structures */
68 typedef struct tagTREEVIEW_INFO
70 HWND hwnd;
71 HWND hwndNotify; /* Owner window to send notifications to */
72 DWORD dwStyle;
73 HTREEITEM root;
74 UINT uInternalStatus;
75 INT Timer;
76 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
77 INT cdmode; /* last custom draw setting */
78 UINT uScrollTime; /* max. time for scrolling in milliseconds */
79 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
81 UINT uItemHeight; /* item height */
82 BOOL bHeightSet;
84 LONG clientWidth; /* width of control window */
85 LONG clientHeight; /* height of control window */
87 LONG treeWidth; /* width of visible tree items */
88 LONG treeHeight; /* height of visible tree items */
90 UINT uIndent; /* indentation in pixels */
91 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
92 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
93 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
94 HTREEITEM editItem; /* item being edited with builtin edit box */
96 HTREEITEM firstVisible; /* handle to item whose top edge is at y = 0 */
97 LONG maxVisibleOrder;
98 HTREEITEM dropItem; /* handle to item selected by drag cursor */
99 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
100 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
101 HIMAGELIST dragList; /* Bitmap of dragged item */
102 LONG scrollX;
103 INT wheelRemainder;
104 COLORREF clrBk;
105 COLORREF clrText;
106 COLORREF clrLine;
107 COLORREF clrInsertMark;
108 HFONT hFont;
109 HFONT hDefaultFont;
110 HFONT hBoldFont;
111 HFONT hUnderlineFont;
112 HFONT hBoldUnderlineFont;
113 HCURSOR hcurHand;
114 HWND hwndToolTip;
116 HWND hwndEdit;
117 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
118 BOOL bIgnoreEditKillFocus;
119 BOOL bLabelChanged;
121 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
122 HIMAGELIST himlNormal;
123 int normalImageHeight;
124 int normalImageWidth;
125 HIMAGELIST himlState;
126 int stateImageHeight;
127 int stateImageWidth;
128 HDPA items;
130 DWORD lastKeyPressTimestamp;
131 WPARAM charCode;
132 INT nSearchParamLength;
133 WCHAR szSearchParam[ MAX_PATH ];
134 } TREEVIEW_INFO;
136 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
138 HTREEITEM parent; /* handle to parent or 0 if at root */
139 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
140 HTREEITEM firstChild; /* handle to first child or 0 if no child */
142 UINT callbackMask;
143 UINT state;
144 UINT stateMask;
145 LPWSTR pszText;
146 int cchTextMax;
147 int iImage;
148 int iSelectedImage;
149 int iExpandedImage;
150 int cChildren;
151 LPARAM lParam;
152 int iIntegral; /* item height multiplier (1 is normal) */
153 int iLevel; /* indentation level:0=root level */
154 HTREEITEM lastChild;
155 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
156 RECT rect;
157 LONG linesOffset;
158 LONG stateOffset;
159 LONG imageOffset;
160 LONG textOffset;
161 LONG textWidth; /* horizontal text extent for pszText */
162 LONG visibleOrder; /* Depth-first numbering of the items whose ancestors are all expanded,
163 corresponding to a top-to-bottom ordering in the tree view.
164 Each item takes up "item.iIntegral" spots in the visible order.
165 0 is the root's first child. */
166 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
167 } TREEVIEW_ITEM;
169 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
170 #define KEY_DELAY 450
172 /* bitflags for infoPtr->uInternalStatus */
174 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
175 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
176 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
177 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
178 #define TV_RDRAG 0x10 /* ditto Rbutton */
179 #define TV_RDRAGGING 0x20
181 /* bitflags for infoPtr->timer */
183 #define TV_EDIT_TIMER 2
184 #define TV_EDIT_TIMER_SET 2
186 #define TEXT_CALLBACK_SIZE 260
188 #define TREEVIEW_LEFT_MARGIN 8
190 #define MINIMUM_INDENT 19
192 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
194 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
195 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
196 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
198 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
199 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
200 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
201 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
203 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
206 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
209 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
211 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
212 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
213 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
214 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
215 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
216 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
218 /* Random Utilities *****************************************************/
219 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
221 /* Returns the treeview private data if hwnd is a treeview.
222 * Otherwise returns an undefined value. */
223 static inline TREEVIEW_INFO *
224 TREEVIEW_GetInfoPtr(HWND hwnd)
226 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
229 /* Don't call this. Nothing wants an item index. */
230 static inline int
231 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
233 return DPA_GetPtrIndex(infoPtr->items, handle);
236 /* Checks if item has changed and needs to be redrawn */
237 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
238 const TVITEMEXW *tvChange)
240 /* Number of children has changed */
241 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
242 return TRUE;
244 /* Image has changed and it's not a callback */
245 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
246 tiNew->iImage != I_IMAGECALLBACK)
247 return TRUE;
249 /* Selected image has changed and it's not a callback */
250 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
251 tiNew->iSelectedImage != I_IMAGECALLBACK)
252 return TRUE;
254 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
255 tiNew->iExpandedImage != I_IMAGECALLBACK)
256 return TRUE;
258 /* Text has changed and it's not a callback */
259 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
260 tiNew->pszText != LPSTR_TEXTCALLBACKW)
261 return TRUE;
263 /* Indent has changed */
264 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
265 return TRUE;
267 /* Item state has changed */
268 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
269 return TRUE;
271 return FALSE;
274 /***************************************************************************
275 * This method checks that handle is an item for this tree.
277 static BOOL
278 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
280 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
282 TRACE("invalid item %p\n", handle);
283 return FALSE;
285 else
286 return TRUE;
289 static HFONT
290 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
292 LOGFONTW font;
294 GetObjectW(hOrigFont, sizeof(font), &font);
295 font.lfWeight = FW_BOLD;
296 return CreateFontIndirectW(&font);
299 static HFONT
300 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
302 LOGFONTW font;
304 GetObjectW(hOrigFont, sizeof(font), &font);
305 font.lfUnderline = TRUE;
306 return CreateFontIndirectW(&font);
309 static HFONT
310 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont)
312 LOGFONTW font;
314 GetObjectW(hfont, sizeof(font), &font);
315 font.lfWeight = FW_BOLD;
316 font.lfUnderline = TRUE;
317 return CreateFontIndirectW(&font);
320 static inline HFONT
321 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
323 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
324 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
325 if (item->state & TVIS_BOLD)
326 return infoPtr->hBoldFont;
327 return infoPtr->hFont;
330 /* for trace/debugging purposes only */
331 static const char *
332 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
334 if (item == NULL) return "<null item>";
335 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
336 if (item->pszText == NULL) return "<null>";
337 return debugstr_w(item->pszText);
340 /* An item is not a child of itself. */
341 static BOOL
342 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
346 child = child->parent;
347 if (child == parent) return TRUE;
348 } while (child != NULL);
350 return FALSE;
353 static BOOL
354 TREEVIEW_IsFullRowSelect(const TREEVIEW_INFO *infoPtr)
356 return !(infoPtr->dwStyle & TVS_HASLINES) && (infoPtr->dwStyle & TVS_FULLROWSELECT);
359 static BOOL
360 TREEVIEW_IsItemHit(const TREEVIEW_INFO *infoPtr, const TVHITTESTINFO *ht)
362 if (TREEVIEW_IsFullRowSelect(infoPtr))
363 return ht->flags & (TVHT_ONITEMINDENT | TVHT_ONITEMBUTTON | TVHT_ONITEM | TVHT_ONITEMRIGHT);
364 else
365 return ht->flags & TVHT_ONITEM;
368 /* Tree Traversal *******************************************************/
370 /***************************************************************************
371 * This method returns the last expanded sibling or child child item
372 * of a tree node
374 static TREEVIEW_ITEM *
375 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
377 if (!item) return NULL;
379 while (item->lastChild)
381 if (item->state & TVIS_EXPANDED)
382 item = item->lastChild;
383 else
384 break;
387 if (item == infoPtr->root)
388 return NULL;
390 return item;
393 /***************************************************************************
394 * This method returns the previous non-hidden item in the list not
395 * considering the tree hierarchy.
397 static TREEVIEW_ITEM *
398 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
400 if (tvItem->prevSibling)
402 /* This item has a prevSibling, get the last item in the sibling's tree. */
403 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
405 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
406 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
407 else
408 return upItem;
410 else
412 /* this item does not have a prevSibling, get the parent */
413 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
418 /***************************************************************************
419 * This method returns the next physical item in the treeview not
420 * considering the tree hierarchy.
422 static TREEVIEW_ITEM *
423 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
426 * If this item has children and is expanded, return the first child
428 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
430 return tvItem->firstChild;
435 * try to get the sibling
437 if (tvItem->nextSibling)
438 return tvItem->nextSibling;
441 * Otherwise, get the parent's sibling.
443 while (tvItem->parent)
445 tvItem = tvItem->parent;
447 if (tvItem->nextSibling)
448 return tvItem->nextSibling;
451 return NULL;
454 /***************************************************************************
455 * This method returns the nth item starting at the given item. It returns
456 * the last item (or first) we we run out of items.
458 * Will scroll backward if count is <0.
459 * forward if count is >0.
461 static TREEVIEW_ITEM *
462 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
463 LONG count)
465 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
466 TREEVIEW_ITEM *previousItem;
468 assert(item != NULL);
470 if (count > 0)
472 next_item = TREEVIEW_GetNextListItem;
474 else if (count < 0)
476 count = -count;
477 next_item = TREEVIEW_GetPrevListItem;
479 else
480 return item;
484 previousItem = item;
485 item = next_item(infoPtr, item);
487 } while (--count && item != NULL);
490 return item ? item : previousItem;
493 /* Notifications ************************************************************/
495 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
497 if (!infoPtr->bNtfUnicode) {
498 switch (code) {
499 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
500 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
501 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
502 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
503 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
504 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
505 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
506 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
507 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
508 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
509 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
510 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
513 return code;
516 static inline BOOL
517 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, UINT code, NMHDR *hdr)
519 TRACE("code=%d, hdr=%p\n", code, hdr);
521 hdr->hwndFrom = infoPtr->hwnd;
522 hdr->idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
523 hdr->code = get_notifycode(infoPtr, code);
525 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, hdr->idFrom, (LPARAM)hdr);
528 static BOOL
529 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
531 NMHDR hdr;
532 return TREEVIEW_SendRealNotify(infoPtr, code, &hdr);
535 static VOID
536 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
538 tvItem->mask = mask;
539 tvItem->hItem = item;
540 tvItem->state = item->state;
541 tvItem->stateMask = 0;
542 tvItem->iImage = item->iImage;
543 tvItem->iSelectedImage = item->iSelectedImage;
544 tvItem->cChildren = item->cChildren;
545 tvItem->lParam = item->lParam;
547 if(mask & TVIF_TEXT)
549 if (!infoPtr->bNtfUnicode)
551 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
552 tvItem->pszText = heap_alloc (tvItem->cchTextMax);
553 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
555 else
557 tvItem->cchTextMax = item->cchTextMax;
558 tvItem->pszText = item->pszText;
561 else
563 tvItem->cchTextMax = 0;
564 tvItem->pszText = NULL;
568 static BOOL
569 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
570 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
572 NMTREEVIEWW nmhdr;
573 BOOL ret;
575 TRACE("code:%d action:0x%x olditem:%p newitem:%p\n",
576 code, action, oldItem, newItem);
578 memset(&nmhdr, 0, sizeof(NMTREEVIEWW));
579 nmhdr.action = action;
581 if (oldItem)
582 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
584 if (newItem)
585 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
587 nmhdr.ptDrag.x = 0;
588 nmhdr.ptDrag.y = 0;
590 ret = TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
591 if (!infoPtr->bNtfUnicode)
593 heap_free(nmhdr.itemOld.pszText);
594 heap_free(nmhdr.itemNew.pszText);
596 return ret;
599 static BOOL
600 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
601 HTREEITEM dragItem, POINT pt)
603 NMTREEVIEWW nmhdr;
605 TRACE("code:%d dragitem:%p\n", code, dragItem);
607 nmhdr.action = 0;
608 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
609 nmhdr.itemNew.hItem = dragItem;
610 nmhdr.itemNew.state = dragItem->state;
611 nmhdr.itemNew.lParam = dragItem->lParam;
613 nmhdr.ptDrag.x = pt.x;
614 nmhdr.ptDrag.y = pt.y;
616 return TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
620 static BOOL
621 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
622 HDC hdc, RECT rc)
624 NMTVCUSTOMDRAW nmcdhdr;
625 NMCUSTOMDRAW *nmcd;
627 TRACE("drawstage:0x%x hdc:%p\n", dwDrawStage, hdc);
629 nmcd = &nmcdhdr.nmcd;
630 nmcd->dwDrawStage = dwDrawStage;
631 nmcd->hdc = hdc;
632 nmcd->rc = rc;
633 nmcd->dwItemSpec = 0;
634 nmcd->uItemState = 0;
635 nmcd->lItemlParam = 0;
636 nmcdhdr.clrText = infoPtr->clrText;
637 nmcdhdr.clrTextBk = infoPtr->clrBk;
638 nmcdhdr.iLevel = 0;
640 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr.nmcd.hdr);
643 /* FIXME: need to find out when the flags in uItemState need to be set */
645 static BOOL
646 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
647 TREEVIEW_ITEM *item, UINT uItemDrawState,
648 NMTVCUSTOMDRAW *nmcdhdr)
650 NMCUSTOMDRAW *nmcd;
651 DWORD dwDrawStage;
652 DWORD_PTR dwItemSpec;
653 UINT uItemState;
655 dwDrawStage = CDDS_ITEM | uItemDrawState;
656 dwItemSpec = (DWORD_PTR)item;
657 uItemState = 0;
658 if (item->state & TVIS_SELECTED)
659 uItemState |= CDIS_SELECTED;
660 if (item == infoPtr->selectedItem)
661 uItemState |= CDIS_FOCUS;
662 if (item == infoPtr->hotItem)
663 uItemState |= CDIS_HOT;
665 nmcd = &nmcdhdr->nmcd;
666 nmcd->dwDrawStage = dwDrawStage;
667 nmcd->hdc = hdc;
668 nmcd->rc = item->rect;
669 nmcd->dwItemSpec = dwItemSpec;
670 nmcd->uItemState = uItemState;
671 nmcd->lItemlParam = item->lParam;
672 nmcdhdr->iLevel = item->iLevel;
674 TRACE("drawstage:0x%x hdc:%p item:%lx, itemstate:0x%x, lItemlParam:0x%lx\n",
675 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
676 nmcd->uItemState, nmcd->lItemlParam);
678 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr->nmcd.hdr);
681 static BOOL
682 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
684 NMTVDISPINFOW tvdi;
685 BOOL ret;
687 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
688 &tvdi.item, editItem);
690 ret = TREEVIEW_SendRealNotify(infoPtr, TVN_BEGINLABELEDITW, &tvdi.hdr);
692 if (!infoPtr->bNtfUnicode)
693 heap_free(tvdi.item.pszText);
695 return ret;
698 static void
699 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
700 UINT mask)
702 NMTVDISPINFOEXW callback;
704 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
705 mask &= item->callbackMask;
707 if (mask == 0) return;
709 /* 'state' always contains valid value, as well as 'lParam'.
710 * All other parameters are uninitialized.
712 callback.item.pszText = item->pszText;
713 callback.item.cchTextMax = item->cchTextMax;
714 callback.item.mask = mask;
715 callback.item.hItem = item;
716 callback.item.state = item->state;
717 callback.item.lParam = item->lParam;
719 /* If text is changed we need to recalculate textWidth */
720 if (mask & TVIF_TEXT)
721 item->textWidth = 0;
723 TREEVIEW_SendRealNotify(infoPtr, TVN_GETDISPINFOW, &callback.hdr);
724 TRACE("resulting code 0x%08x\n", callback.hdr.code);
726 /* It may have changed due to a call to SetItem. */
727 mask &= item->callbackMask;
729 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
731 /* Instead of copying text into our buffer user specified his own */
732 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
733 LPWSTR newText;
734 int buflen;
735 int len = MultiByteToWideChar( CP_ACP, 0,
736 (LPSTR)callback.item.pszText, -1,
737 NULL, 0);
738 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
739 newText = heap_realloc(item->pszText, buflen);
741 TRACE("returned str %s, len=%d, buflen=%d\n",
742 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
744 if (newText)
746 item->pszText = newText;
747 MultiByteToWideChar( CP_ACP, 0,
748 (LPSTR)callback.item.pszText, -1,
749 item->pszText, buflen/sizeof(WCHAR));
750 item->cchTextMax = buflen/sizeof(WCHAR);
752 /* If realloc fails we have nothing to do, but keep original text */
754 else {
755 int len = max(lstrlenW(callback.item.pszText) + 1,
756 TEXT_CALLBACK_SIZE);
757 LPWSTR newText = heap_realloc(item->pszText, len*sizeof(WCHAR));
759 TRACE("returned wstr %s, len=%d\n",
760 debugstr_w(callback.item.pszText), len);
762 if (newText)
764 item->pszText = newText;
765 lstrcpyW(item->pszText, callback.item.pszText);
766 item->cchTextMax = len;
768 /* If realloc fails we have nothing to do, but keep original text */
771 else if (mask & TVIF_TEXT) {
772 /* User put text into our buffer, that is ok unless A string */
773 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
774 LPWSTR newText;
775 int buflen;
776 int len = MultiByteToWideChar( CP_ACP, 0,
777 (LPSTR)callback.item.pszText, -1,
778 NULL, 0);
779 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
780 newText = heap_alloc(buflen);
782 TRACE("same buffer str %s, len=%d, buflen=%d\n",
783 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
785 if (newText)
787 LPWSTR oldText = item->pszText;
788 item->pszText = newText;
789 MultiByteToWideChar( CP_ACP, 0,
790 (LPSTR)callback.item.pszText, -1,
791 item->pszText, buflen/sizeof(WCHAR));
792 item->cchTextMax = buflen/sizeof(WCHAR);
793 heap_free(oldText);
798 if (mask & TVIF_IMAGE)
799 item->iImage = callback.item.iImage;
801 if (mask & TVIF_SELECTEDIMAGE)
802 item->iSelectedImage = callback.item.iSelectedImage;
804 if (mask & TVIF_EXPANDEDIMAGE)
805 item->iExpandedImage = callback.item.iExpandedImage;
807 if (mask & TVIF_CHILDREN)
808 item->cChildren = callback.item.cChildren;
810 if (callback.item.mask & TVIF_STATE)
812 item->state &= ~callback.item.stateMask;
813 item->state |= (callback.item.state & callback.item.stateMask);
816 /* These members are now permanently set. */
817 if (callback.item.mask & TVIF_DI_SETITEM)
818 item->callbackMask &= ~callback.item.mask;
821 /***************************************************************************
822 * This function uses cChildren field to decide whether the item has
823 * children or not.
824 * Note: if this returns TRUE, the child items may not actually exist,
825 * they could be virtual.
827 * Just use item->firstChild to check for physical children.
829 static BOOL
830 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
832 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
833 /* Protect for a case when callback field is not changed by a host,
834 otherwise negative values trigger normal notifications. */
835 return item->cChildren != 0 && item->cChildren != I_CHILDRENCALLBACK;
838 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
840 INT format;
842 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
844 if (nCommand != NF_REQUERY) return 0;
846 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
847 TRACE("format=%d\n", format);
849 /* Invalid format returned by NF_QUERY defaults to ANSI*/
850 if (format != NFR_ANSI && format != NFR_UNICODE)
851 format = NFR_ANSI;
853 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
855 return format;
858 /* Item Position ********************************************************/
860 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
861 static VOID
862 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
864 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
865 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
866 > TVS_LINESATROOT);
868 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
869 - infoPtr->scrollX;
870 item->stateOffset = item->linesOffset + infoPtr->uIndent;
871 item->imageOffset = item->stateOffset
872 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
873 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
876 static VOID
877 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
879 HDC hdc;
880 HFONT hOldFont=0;
881 SIZE sz;
883 /* DRAW's OM docker creates items like this */
884 if (item->pszText == NULL)
886 item->textWidth = 0;
887 return;
890 if (hDC != 0)
892 hdc = hDC;
894 else
896 hdc = GetDC(infoPtr->hwnd);
897 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
900 GetTextExtentPoint32W(hdc, item->pszText, lstrlenW(item->pszText), &sz);
901 item->textWidth = sz.cx;
903 if (hDC == 0)
905 SelectObject(hdc, hOldFont);
906 ReleaseDC(0, hdc);
910 static VOID
911 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
913 item->rect.top = infoPtr->uItemHeight *
914 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
916 item->rect.bottom = item->rect.top
917 + infoPtr->uItemHeight * item->iIntegral - 1;
919 item->rect.left = 0;
920 item->rect.right = infoPtr->clientWidth;
923 /* We know that only items after start need their order updated. */
924 static void
925 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
927 TREEVIEW_ITEM *item;
928 int order;
930 if (!start)
932 start = infoPtr->root->firstChild;
933 order = 0;
935 else
936 order = start->visibleOrder;
938 for (item = start; item != NULL;
939 item = TREEVIEW_GetNextListItem(infoPtr, item))
941 if (!ISVISIBLE(item) && order > 0)
942 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
943 item->visibleOrder = order;
944 order += item->iIntegral;
947 infoPtr->maxVisibleOrder = order;
949 for (item = infoPtr->root->firstChild; item != NULL;
950 item = TREEVIEW_GetNextListItem(infoPtr, item))
952 TREEVIEW_ComputeItemRect(infoPtr, item);
957 /* Update metrics of all items in selected subtree.
958 * root must be expanded
960 static VOID
961 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
963 TREEVIEW_ITEM *sibling;
964 HDC hdc;
965 HFONT hOldFont;
967 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
968 return;
970 root->state &= ~TVIS_EXPANDED;
971 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
972 root->state |= TVIS_EXPANDED;
974 hdc = GetDC(infoPtr->hwnd);
975 hOldFont = SelectObject(hdc, infoPtr->hFont);
977 for (; root != sibling;
978 root = TREEVIEW_GetNextListItem(infoPtr, root))
980 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
982 if (root->callbackMask & TVIF_TEXT)
983 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
985 if (root->textWidth == 0)
987 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
988 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
992 SelectObject(hdc, hOldFont);
993 ReleaseDC(infoPtr->hwnd, hdc);
996 /* Item Allocation **********************************************************/
998 static TREEVIEW_ITEM *
999 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1001 TREEVIEW_ITEM *newItem = heap_alloc_zero(sizeof(*newItem));
1003 if (!newItem)
1004 return NULL;
1006 /* I_IMAGENONE would make more sense but this is neither what is
1007 * documented (MSDN doesn't specify) nor what Windows actually does
1008 * (it sets it to zero)... and I can so imagine an application using
1009 * inc/dec to toggle the images. */
1010 newItem->iImage = 0;
1011 newItem->iSelectedImage = 0;
1012 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1013 newItem->infoPtr = infoPtr;
1015 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1017 heap_free(newItem);
1018 return NULL;
1021 return newItem;
1024 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1025 * free item->pszText. */
1026 static void
1027 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1029 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1030 if (infoPtr->selectedItem == item)
1031 infoPtr->selectedItem = NULL;
1032 if (infoPtr->hotItem == item)
1033 infoPtr->hotItem = NULL;
1034 if (infoPtr->focusedItem == item)
1035 infoPtr->focusedItem = NULL;
1036 if (infoPtr->firstVisible == item)
1037 infoPtr->firstVisible = NULL;
1038 if (infoPtr->dropItem == item)
1039 infoPtr->dropItem = NULL;
1040 if (infoPtr->insertMarkItem == item)
1041 infoPtr->insertMarkItem = NULL;
1042 heap_free(item);
1046 /* Item Insertion *******************************************************/
1048 /***************************************************************************
1049 * This method inserts newItem before sibling as a child of parent.
1050 * sibling can be NULL, but only if parent has no children.
1052 static void
1053 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1054 TREEVIEW_ITEM *parent)
1056 assert(parent != NULL);
1058 if (sibling != NULL)
1060 assert(sibling->parent == parent);
1062 if (sibling->prevSibling != NULL)
1063 sibling->prevSibling->nextSibling = newItem;
1065 newItem->prevSibling = sibling->prevSibling;
1066 sibling->prevSibling = newItem;
1068 else
1069 newItem->prevSibling = NULL;
1071 newItem->nextSibling = sibling;
1073 if (parent->firstChild == sibling)
1074 parent->firstChild = newItem;
1076 if (parent->lastChild == NULL)
1077 parent->lastChild = newItem;
1080 /***************************************************************************
1081 * This method inserts newItem after sibling as a child of parent.
1082 * sibling can be NULL, but only if parent has no children.
1084 static void
1085 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1086 TREEVIEW_ITEM *parent)
1088 assert(parent != NULL);
1090 if (sibling != NULL)
1092 assert(sibling->parent == parent);
1094 if (sibling->nextSibling != NULL)
1095 sibling->nextSibling->prevSibling = newItem;
1097 newItem->nextSibling = sibling->nextSibling;
1098 sibling->nextSibling = newItem;
1100 else
1101 newItem->nextSibling = NULL;
1103 newItem->prevSibling = sibling;
1105 if (parent->lastChild == sibling)
1106 parent->lastChild = newItem;
1108 if (parent->firstChild == NULL)
1109 parent->firstChild = newItem;
1112 static BOOL
1113 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1114 const TVITEMEXW *tvItem, BOOL isW)
1116 UINT callbackClear = 0;
1117 UINT callbackSet = 0;
1119 TRACE("item %p\n", item);
1120 /* Do this first in case it fails. */
1121 if (tvItem->mask & TVIF_TEXT)
1123 item->textWidth = 0; /* force width recalculation */
1125 /* Covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1126 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL)
1128 int len;
1129 LPWSTR newText;
1130 if (isW)
1131 len = lstrlenW(tvItem->pszText) + 1;
1132 else
1133 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1135 /* Allocate new block to make pointer comparison in item_changed() work. */
1136 newText = heap_alloc(len * sizeof(WCHAR));
1138 if (newText == NULL) return FALSE;
1140 callbackClear |= TVIF_TEXT;
1142 heap_free(item->pszText);
1143 item->pszText = newText;
1144 item->cchTextMax = len;
1145 if (isW)
1146 lstrcpynW(item->pszText, tvItem->pszText, len);
1147 else
1148 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1149 item->pszText, len);
1151 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1153 else
1155 callbackSet |= TVIF_TEXT;
1156 item->pszText = heap_realloc(item->pszText, TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1157 item->cchTextMax = TEXT_CALLBACK_SIZE;
1158 TRACE("setting callback, item %p\n", item);
1162 if (tvItem->mask & TVIF_CHILDREN)
1164 item->cChildren = tvItem->cChildren;
1166 if (item->cChildren == I_CHILDRENCALLBACK)
1167 callbackSet |= TVIF_CHILDREN;
1168 else
1169 callbackClear |= TVIF_CHILDREN;
1172 if (tvItem->mask & TVIF_IMAGE)
1174 item->iImage = tvItem->iImage;
1176 if (item->iImage == I_IMAGECALLBACK)
1177 callbackSet |= TVIF_IMAGE;
1178 else
1179 callbackClear |= TVIF_IMAGE;
1182 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1184 item->iSelectedImage = tvItem->iSelectedImage;
1186 if (item->iSelectedImage == I_IMAGECALLBACK)
1187 callbackSet |= TVIF_SELECTEDIMAGE;
1188 else
1189 callbackClear |= TVIF_SELECTEDIMAGE;
1192 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1194 item->iExpandedImage = tvItem->iExpandedImage;
1196 if (item->iExpandedImage == I_IMAGECALLBACK)
1197 callbackSet |= TVIF_EXPANDEDIMAGE;
1198 else
1199 callbackClear |= TVIF_EXPANDEDIMAGE;
1202 if (tvItem->mask & TVIF_PARAM)
1203 item->lParam = tvItem->lParam;
1205 /* If the application sets TVIF_INTEGRAL without
1206 * supplying a TVITEMEX structure, it's toast. */
1207 if (tvItem->mask & TVIF_INTEGRAL)
1208 item->iIntegral = tvItem->iIntegral;
1210 if (tvItem->mask & TVIF_STATE)
1212 TRACE("prevstate 0x%x, state 0x%x, mask 0x%x\n", item->state, tvItem->state,
1213 tvItem->stateMask);
1214 item->state &= ~tvItem->stateMask;
1215 item->state |= (tvItem->state & tvItem->stateMask);
1218 if (tvItem->mask & TVIF_STATEEX)
1220 FIXME("New extended state: 0x%x\n", tvItem->uStateEx);
1223 item->callbackMask |= callbackSet;
1224 item->callbackMask &= ~callbackClear;
1226 return TRUE;
1229 /* Note that the new item is pre-zeroed. */
1230 static LRESULT
1231 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1233 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1234 HTREEITEM insertAfter;
1235 TREEVIEW_ITEM *newItem, *parentItem;
1236 BOOL bTextUpdated = FALSE;
1238 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1240 parentItem = infoPtr->root;
1242 else
1244 parentItem = ptdi->hParent;
1246 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1248 WARN("invalid parent %p\n", parentItem);
1249 return 0;
1253 insertAfter = ptdi->hInsertAfter;
1255 /* Validate this now for convenience. */
1256 switch ((DWORD_PTR)insertAfter)
1258 case (DWORD_PTR)TVI_FIRST:
1259 case (DWORD_PTR)TVI_LAST:
1260 case (DWORD_PTR)TVI_SORT:
1261 break;
1263 default:
1264 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1265 insertAfter->parent != parentItem)
1267 WARN("invalid insert after %p\n", insertAfter);
1268 insertAfter = TVI_LAST;
1272 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1273 (tvItem->mask & TVIF_TEXT)
1274 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1275 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1276 : "<no label>");
1278 newItem = TREEVIEW_AllocateItem(infoPtr);
1279 if (newItem == NULL)
1280 return 0;
1282 newItem->parent = parentItem;
1283 newItem->iIntegral = 1;
1284 newItem->visibleOrder = -1;
1286 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1287 return 0;
1289 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1291 infoPtr->uNumItems++;
1293 switch ((DWORD_PTR)insertAfter)
1295 case (DWORD_PTR)TVI_FIRST:
1297 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1298 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1299 if (infoPtr->firstVisible == originalFirst)
1300 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1302 break;
1304 case (DWORD_PTR)TVI_LAST:
1305 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1306 break;
1308 /* hInsertAfter names a specific item we want to insert after */
1309 default:
1310 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1311 break;
1313 case (DWORD_PTR)TVI_SORT:
1315 TREEVIEW_ITEM *aChild;
1316 TREEVIEW_ITEM *previousChild = NULL;
1317 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1318 BOOL bItemInserted = FALSE;
1320 aChild = parentItem->firstChild;
1322 bTextUpdated = TRUE;
1323 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1325 /* Iterate the parent children to see where we fit in */
1326 while (aChild != NULL)
1328 INT comp;
1330 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1331 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1333 if (comp < 0) /* we are smaller than the current one */
1335 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1336 if (infoPtr->firstVisible == originalFirst &&
1337 aChild == originalFirst)
1338 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1339 bItemInserted = TRUE;
1340 break;
1342 else if (comp > 0) /* we are bigger than the current one */
1344 previousChild = aChild;
1346 /* This will help us to exit if there is no more sibling */
1347 aChild = (aChild->nextSibling == 0)
1348 ? NULL
1349 : aChild->nextSibling;
1351 /* Look at the next item */
1352 continue;
1354 else if (comp == 0)
1357 * An item with this name is already existing, therefore,
1358 * we add after the one we found
1360 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1361 bItemInserted = TRUE;
1362 break;
1367 * we reach the end of the child list and the item has not
1368 * yet been inserted, therefore, insert it after the last child.
1370 if ((!bItemInserted) && (aChild == NULL))
1371 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1373 break;
1378 TRACE("new item %p; parent %p, mask 0x%x\n", newItem,
1379 newItem->parent, tvItem->mask);
1381 newItem->iLevel = newItem->parent->iLevel + 1;
1383 if (newItem->parent->cChildren == 0)
1384 newItem->parent->cChildren = 1;
1386 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1388 if (STATEIMAGEINDEX(newItem->state) == 0)
1389 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1392 if (infoPtr->firstVisible == NULL)
1393 infoPtr->firstVisible = newItem;
1395 TREEVIEW_VerifyTree(infoPtr);
1397 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1399 if (parentItem == infoPtr->root ||
1400 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1402 TREEVIEW_ITEM *item;
1403 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1405 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1406 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1408 if (!bTextUpdated)
1409 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1411 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1412 TREEVIEW_UpdateScrollBars(infoPtr);
1414 * if the item was inserted in a visible part of the tree,
1415 * invalidate it, as well as those after it
1417 for (item = newItem;
1418 item != NULL;
1419 item = TREEVIEW_GetNextListItem(infoPtr, item))
1420 TREEVIEW_Invalidate(infoPtr, item);
1422 else
1424 /* refresh treeview if newItem is the first item inserted under parentItem */
1425 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1427 /* parent got '+' - update it */
1428 TREEVIEW_Invalidate(infoPtr, parentItem);
1432 return (LRESULT)newItem;
1435 /* Item Deletion ************************************************************/
1436 static void
1437 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1439 static void
1440 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1442 TREEVIEW_ITEM *kill = parentItem->firstChild;
1444 while (kill != NULL)
1446 TREEVIEW_ITEM *next = kill->nextSibling;
1448 TREEVIEW_RemoveItem(infoPtr, kill);
1450 kill = next;
1453 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1454 assert(parentItem->firstChild == NULL);
1455 assert(parentItem->lastChild == NULL);
1458 static void
1459 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1461 TREEVIEW_ITEM *parentItem;
1463 assert(item != NULL);
1464 assert(item->parent != NULL); /* i.e. it must not be the root */
1466 parentItem = item->parent;
1468 if (parentItem->firstChild == item)
1469 parentItem->firstChild = item->nextSibling;
1471 if (parentItem->lastChild == item)
1472 parentItem->lastChild = item->prevSibling;
1474 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1475 && parentItem->cChildren > 0)
1476 parentItem->cChildren = 0;
1478 if (item->prevSibling)
1479 item->prevSibling->nextSibling = item->nextSibling;
1481 if (item->nextSibling)
1482 item->nextSibling->prevSibling = item->prevSibling;
1485 static void
1486 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1488 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1490 if (item->firstChild)
1491 TREEVIEW_RemoveAllChildren(infoPtr, item);
1493 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1494 TVIF_HANDLE | TVIF_PARAM, item, 0);
1496 TREEVIEW_UnlinkItem(item);
1498 infoPtr->uNumItems--;
1500 if (item->pszText != LPSTR_TEXTCALLBACKW)
1501 heap_free(item->pszText);
1503 TREEVIEW_FreeItem(infoPtr, item);
1507 /* Empty out the tree. */
1508 static void
1509 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1511 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1513 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1516 static LRESULT
1517 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1519 TREEVIEW_ITEM *newSelection = NULL;
1520 TREEVIEW_ITEM *newFirstVisible = NULL;
1521 TREEVIEW_ITEM *parent, *prev = NULL;
1522 BOOL visible = FALSE;
1524 if (item == TVI_ROOT || !item)
1526 TRACE("TVI_ROOT\n");
1527 parent = infoPtr->root;
1528 newSelection = NULL;
1529 visible = TRUE;
1530 TREEVIEW_RemoveTree(infoPtr);
1532 else
1534 if (!TREEVIEW_ValidItem(infoPtr, item))
1535 return FALSE;
1537 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1538 parent = item->parent;
1540 if (ISVISIBLE(item))
1542 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1543 visible = TRUE;
1546 if (infoPtr->selectedItem != NULL
1547 && (item == infoPtr->selectedItem
1548 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1550 if (item->nextSibling)
1551 newSelection = item->nextSibling;
1552 else if (item->parent != infoPtr->root)
1553 newSelection = item->parent;
1554 else
1555 newSelection = item->prevSibling;
1556 TRACE("newSelection = %p\n", newSelection);
1559 if (infoPtr->firstVisible == item)
1561 visible = TRUE;
1562 if (item->nextSibling)
1563 newFirstVisible = item->nextSibling;
1564 else if (item->prevSibling)
1565 newFirstVisible = item->prevSibling;
1566 else if (item->parent != infoPtr->root)
1567 newFirstVisible = item->parent;
1568 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1570 else
1571 newFirstVisible = infoPtr->firstVisible;
1573 TREEVIEW_RemoveItem(infoPtr, item);
1576 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1577 if (!infoPtr->selectedItem && newSelection)
1579 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1580 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1583 /* Validate insertMark dropItem.
1584 * hotItem ??? - used for comparison only.
1586 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1587 infoPtr->insertMarkItem = 0;
1589 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1590 infoPtr->dropItem = 0;
1592 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1593 newFirstVisible = infoPtr->root->firstChild;
1595 TREEVIEW_VerifyTree(infoPtr);
1597 if (visible)
1598 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1600 if (!infoPtr->bRedraw) return TRUE;
1602 if (visible)
1604 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1605 TREEVIEW_UpdateScrollBars(infoPtr);
1606 TREEVIEW_Invalidate(infoPtr, NULL);
1608 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1610 /* parent lost '+/-' - update it */
1611 TREEVIEW_Invalidate(infoPtr, parent);
1614 return TRUE;
1618 /* Get/Set Messages *********************************************************/
1619 static LRESULT
1620 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1622 infoPtr->bRedraw = wParam != 0;
1624 if (infoPtr->bRedraw)
1626 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1627 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1628 TREEVIEW_UpdateScrollBars(infoPtr);
1629 TREEVIEW_Invalidate(infoPtr, NULL);
1631 return 0;
1634 static LRESULT
1635 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1637 TRACE("\n");
1638 return infoPtr->uIndent;
1641 static LRESULT
1642 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1644 TRACE("\n");
1646 if (newIndent < MINIMUM_INDENT)
1647 newIndent = MINIMUM_INDENT;
1649 if (infoPtr->uIndent != newIndent)
1651 infoPtr->uIndent = newIndent;
1652 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1653 TREEVIEW_UpdateScrollBars(infoPtr);
1654 TREEVIEW_Invalidate(infoPtr, NULL);
1657 return 0;
1661 static LRESULT
1662 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1664 TRACE("\n");
1665 return (LRESULT)infoPtr->hwndToolTip;
1668 static LRESULT
1669 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1671 HWND prevToolTip;
1673 TRACE("\n");
1674 prevToolTip = infoPtr->hwndToolTip;
1675 infoPtr->hwndToolTip = hwndTT;
1677 return (LRESULT)prevToolTip;
1680 static LRESULT
1681 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1683 BOOL rc = infoPtr->bNtfUnicode;
1684 infoPtr->bNtfUnicode = fUnicode;
1685 return rc;
1688 static LRESULT
1689 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1691 return infoPtr->bNtfUnicode;
1694 static LRESULT
1695 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1697 return infoPtr->uScrollTime;
1700 static LRESULT
1701 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1703 UINT uOldScrollTime = infoPtr->uScrollTime;
1705 infoPtr->uScrollTime = min(uScrollTime, 100);
1707 return uOldScrollTime;
1711 static LRESULT
1712 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1714 TRACE("\n");
1716 switch (wParam)
1718 case TVSIL_NORMAL:
1719 return (LRESULT)infoPtr->himlNormal;
1721 case TVSIL_STATE:
1722 return (LRESULT)infoPtr->himlState;
1724 default:
1725 return 0;
1729 #define TVHEIGHT_MIN 16
1730 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1732 /* Compute the natural height for items. */
1733 static UINT
1734 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1736 TEXTMETRICW tm;
1737 HDC hdc = GetDC(0);
1738 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1739 UINT height;
1741 /* Height is the maximum of:
1742 * 16 (a hack because our fonts are tiny), and
1743 * The text height + border & margin, and
1744 * The size of the normal image list
1746 GetTextMetricsW(hdc, &tm);
1747 SelectObject(hdc, hOldFont);
1748 ReleaseDC(0, hdc);
1750 height = TVHEIGHT_MIN;
1751 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1752 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1753 if (height < infoPtr->normalImageHeight)
1754 height = infoPtr->normalImageHeight;
1756 /* Round down, unless we support odd ("non even") heights. */
1757 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1758 height &= ~1;
1760 return height;
1763 static LRESULT
1764 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1766 HIMAGELIST himlOld = 0;
1767 int oldWidth = infoPtr->normalImageWidth;
1768 int oldHeight = infoPtr->normalImageHeight;
1770 TRACE("%u,%p\n", type, himlNew);
1772 switch (type)
1774 case TVSIL_NORMAL:
1775 himlOld = infoPtr->himlNormal;
1776 infoPtr->himlNormal = himlNew;
1778 if (himlNew)
1779 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1780 &infoPtr->normalImageHeight);
1781 else
1783 infoPtr->normalImageWidth = 0;
1784 infoPtr->normalImageHeight = 0;
1787 break;
1789 case TVSIL_STATE:
1790 himlOld = infoPtr->himlState;
1791 infoPtr->himlState = himlNew;
1793 if (himlNew)
1794 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1795 &infoPtr->stateImageHeight);
1796 else
1798 infoPtr->stateImageWidth = 0;
1799 infoPtr->stateImageHeight = 0;
1802 break;
1804 default:
1805 ERR("unknown imagelist type %u\n", type);
1808 if (oldWidth != infoPtr->normalImageWidth ||
1809 oldHeight != infoPtr->normalImageHeight)
1811 BOOL bRecalcVisible = FALSE;
1813 if (oldHeight != infoPtr->normalImageHeight &&
1814 !infoPtr->bHeightSet)
1816 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1817 bRecalcVisible = TRUE;
1820 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1821 infoPtr->normalImageWidth != infoPtr->uIndent)
1823 infoPtr->uIndent = infoPtr->normalImageWidth;
1824 bRecalcVisible = TRUE;
1827 if (bRecalcVisible)
1828 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1830 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1831 TREEVIEW_UpdateScrollBars(infoPtr);
1834 TREEVIEW_Invalidate(infoPtr, NULL);
1836 return (LRESULT)himlOld;
1839 static LRESULT
1840 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1842 INT prevHeight = infoPtr->uItemHeight;
1844 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1845 if (newHeight == -1)
1847 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1848 infoPtr->bHeightSet = FALSE;
1850 else
1852 if (newHeight == 0) newHeight = 1;
1853 infoPtr->uItemHeight = newHeight;
1854 infoPtr->bHeightSet = TRUE;
1857 /* Round down, unless we support odd ("non even") heights. */
1858 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1860 infoPtr->uItemHeight &= ~1;
1861 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1864 if (infoPtr->uItemHeight != prevHeight)
1866 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1867 TREEVIEW_UpdateScrollBars(infoPtr);
1868 TREEVIEW_Invalidate(infoPtr, NULL);
1871 return prevHeight;
1874 static LRESULT
1875 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1877 TRACE("\n");
1878 return infoPtr->uItemHeight;
1882 static LRESULT
1883 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1885 TRACE("%p\n", infoPtr->hFont);
1886 return (LRESULT)infoPtr->hFont;
1890 static INT CALLBACK
1891 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1893 (void)unused;
1895 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1897 return 1;
1900 static LRESULT
1901 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1903 UINT uHeight = infoPtr->uItemHeight;
1905 TRACE("%p %i\n", hFont, bRedraw);
1907 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1909 DeleteObject(infoPtr->hBoldFont);
1910 DeleteObject(infoPtr->hUnderlineFont);
1911 DeleteObject(infoPtr->hBoldUnderlineFont);
1912 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1913 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1914 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
1916 if (!infoPtr->bHeightSet)
1917 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1919 if (uHeight != infoPtr->uItemHeight)
1920 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1922 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1924 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1925 TREEVIEW_UpdateScrollBars(infoPtr);
1927 if (bRedraw)
1928 TREEVIEW_Invalidate(infoPtr, NULL);
1930 return 0;
1934 static LRESULT
1935 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1937 TRACE("\n");
1938 return (LRESULT)infoPtr->clrLine;
1941 static LRESULT
1942 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1944 COLORREF prevColor = infoPtr->clrLine;
1946 TRACE("\n");
1947 infoPtr->clrLine = color;
1948 return (LRESULT)prevColor;
1952 static LRESULT
1953 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1955 TRACE("\n");
1956 return (LRESULT)infoPtr->clrText;
1959 static LRESULT
1960 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1962 COLORREF prevColor = infoPtr->clrText;
1964 TRACE("\n");
1965 infoPtr->clrText = color;
1967 if (infoPtr->clrText != prevColor)
1968 TREEVIEW_Invalidate(infoPtr, NULL);
1970 return (LRESULT)prevColor;
1974 static LRESULT
1975 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1977 TRACE("\n");
1978 return (LRESULT)infoPtr->clrBk;
1981 static LRESULT
1982 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1984 COLORREF prevColor = infoPtr->clrBk;
1986 TRACE("\n");
1987 infoPtr->clrBk = newColor;
1989 if (newColor != prevColor)
1990 TREEVIEW_Invalidate(infoPtr, NULL);
1992 return (LRESULT)prevColor;
1996 static LRESULT
1997 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1999 TRACE("\n");
2000 return (LRESULT)infoPtr->clrInsertMark;
2003 static LRESULT
2004 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
2006 COLORREF prevColor = infoPtr->clrInsertMark;
2008 TRACE("0x%08x\n", color);
2009 infoPtr->clrInsertMark = color;
2011 return (LRESULT)prevColor;
2015 static LRESULT
2016 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2018 TRACE("%d %p\n", wParam, item);
2020 if (!TREEVIEW_ValidItem(infoPtr, item))
2021 return 0;
2023 infoPtr->insertBeforeorAfter = wParam;
2024 infoPtr->insertMarkItem = item;
2026 TREEVIEW_Invalidate(infoPtr, NULL);
2028 return 1;
2032 /************************************************************************
2033 * Some serious braindamage here. lParam is a pointer to both the
2034 * input HTREEITEM and the output RECT.
2036 static LRESULT
2037 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2039 TREEVIEW_ITEM *item;
2040 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2042 TRACE("\n");
2044 if (pItem == NULL)
2045 return FALSE;
2047 item = *pItem;
2048 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2049 return FALSE;
2052 * If wParam is TRUE return the text size otherwise return
2053 * the whole item size
2055 if (fTextRect)
2057 /* Windows does not send TVN_GETDISPINFO here. */
2059 lpRect->top = item->rect.top;
2060 lpRect->bottom = item->rect.bottom;
2062 lpRect->left = item->textOffset;
2063 if (!item->textWidth)
2064 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2066 lpRect->right = item->textOffset + item->textWidth + 4;
2068 else
2070 *lpRect = item->rect;
2073 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2075 return TRUE;
2078 static inline LRESULT
2079 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2081 /* Surprise! This does not take integral height into account. */
2082 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2083 return infoPtr->clientHeight / infoPtr->uItemHeight;
2087 static LRESULT
2088 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2090 TREEVIEW_ITEM *item = tvItem->hItem;
2092 if (!TREEVIEW_ValidItem(infoPtr, item))
2094 BOOL valid_item = FALSE;
2095 if (!item) return FALSE;
2097 __TRY
2099 infoPtr = item->infoPtr;
2100 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2101 valid_item = TREEVIEW_ValidItem(infoPtr, item);
2103 __EXCEPT_PAGE_FAULT
2106 __ENDTRY
2107 if (!valid_item) return FALSE;
2110 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2112 if (tvItem->mask & TVIF_CHILDREN)
2114 if (item->cChildren==I_CHILDRENCALLBACK)
2115 FIXME("I_CHILDRENCALLBACK not supported\n");
2116 tvItem->cChildren = item->cChildren;
2119 if (tvItem->mask & TVIF_HANDLE)
2120 tvItem->hItem = item;
2122 if (tvItem->mask & TVIF_IMAGE)
2123 tvItem->iImage = item->iImage;
2125 if (tvItem->mask & TVIF_INTEGRAL)
2126 tvItem->iIntegral = item->iIntegral;
2128 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2129 tvItem->lParam = item->lParam;
2131 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2132 tvItem->iSelectedImage = item->iSelectedImage;
2134 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2135 tvItem->iExpandedImage = item->iExpandedImage;
2137 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2138 tvItem->state = item->state;
2140 if (tvItem->mask & TVIF_TEXT)
2142 if (item->pszText == NULL)
2144 if (tvItem->cchTextMax > 0)
2145 tvItem->pszText[0] = '\0';
2147 else if (isW)
2149 if (item->pszText == LPSTR_TEXTCALLBACKW)
2151 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2152 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2154 else
2156 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2159 else
2161 if (item->pszText == LPSTR_TEXTCALLBACKW)
2163 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2164 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2166 else
2168 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2169 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2174 if (tvItem->mask & TVIF_STATEEX)
2176 FIXME("Extended item state not supported, returning 0.\n");
2177 tvItem->uStateEx = 0;
2180 TRACE("item <%p>, txt %p, img %d, mask 0x%x\n",
2181 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2183 return TRUE;
2186 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2187 * which is wrong. */
2188 static LRESULT
2189 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2191 TREEVIEW_ITEM *item;
2192 TREEVIEW_ITEM originalItem;
2194 item = tvItem->hItem;
2196 TRACE("item %d, mask 0x%x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2197 tvItem->mask);
2199 if (!TREEVIEW_ValidItem(infoPtr, item))
2200 return FALSE;
2202 /* Store the original item values. Text buffer will be freed in TREEVIEW_DoSetItemT() below. */
2203 originalItem = *item;
2205 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2206 return FALSE;
2208 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2209 if ((tvItem->mask & TVIF_TEXT
2210 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2211 && ISVISIBLE(item))
2213 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2214 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2217 if (tvItem->mask != 0 && ISVISIBLE(item))
2219 /* The refresh updates everything, but we can't wait until then. */
2220 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2222 /* if any of the item's values changed and it's not a callback, redraw the item */
2223 if (item_changed(&originalItem, item, tvItem))
2225 if (tvItem->mask & TVIF_INTEGRAL)
2227 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2228 TREEVIEW_UpdateScrollBars(infoPtr);
2230 TREEVIEW_Invalidate(infoPtr, NULL);
2232 else
2234 TREEVIEW_UpdateScrollBars(infoPtr);
2235 TREEVIEW_Invalidate(infoPtr, item);
2240 return TRUE;
2243 static LRESULT
2244 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2246 TRACE("\n");
2248 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2249 return 0;
2251 return (item->state & mask);
2254 static LRESULT
2255 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2257 TREEVIEW_ITEM *retval;
2259 retval = 0;
2261 /* handle all the global data here */
2262 switch (which)
2264 case TVGN_CHILD: /* Special case: child of 0 is root */
2265 if (item)
2266 break;
2267 /* fall through */
2268 case TVGN_ROOT:
2269 retval = infoPtr->root->firstChild;
2270 break;
2272 case TVGN_CARET:
2273 retval = infoPtr->selectedItem;
2274 break;
2276 case TVGN_FIRSTVISIBLE:
2277 retval = infoPtr->firstVisible;
2278 break;
2280 case TVGN_DROPHILITE:
2281 retval = infoPtr->dropItem;
2282 break;
2284 case TVGN_LASTVISIBLE:
2285 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2286 break;
2289 if (retval)
2291 TRACE("flags:0x%x, returns %p\n", which, retval);
2292 return (LRESULT)retval;
2295 if (item == TVI_ROOT) item = infoPtr->root;
2297 if (!TREEVIEW_ValidItem(infoPtr, item))
2298 return FALSE;
2300 switch (which)
2302 case TVGN_NEXT:
2303 retval = item->nextSibling;
2304 break;
2305 case TVGN_PREVIOUS:
2306 retval = item->prevSibling;
2307 break;
2308 case TVGN_PARENT:
2309 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2310 break;
2311 case TVGN_CHILD:
2312 retval = item->firstChild;
2313 break;
2314 case TVGN_NEXTVISIBLE:
2315 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2316 break;
2317 case TVGN_PREVIOUSVISIBLE:
2318 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2319 break;
2320 default:
2321 TRACE("Unknown msg 0x%x, item %p\n", which, item);
2322 break;
2325 TRACE("flags: 0x%x, item %p;returns %p\n", which, item, retval);
2326 return (LRESULT)retval;
2330 static LRESULT
2331 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2333 TRACE(" %d\n", infoPtr->uNumItems);
2334 return (LRESULT)infoPtr->uNumItems;
2337 static VOID
2338 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2340 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2342 static const unsigned int state_table[] = { 0, 2, 1 };
2344 unsigned int state;
2346 state = STATEIMAGEINDEX(item->state);
2347 TRACE("state: 0x%x\n", state);
2348 item->state &= ~TVIS_STATEIMAGEMASK;
2350 if (state < 3)
2351 state = state_table[state];
2353 item->state |= INDEXTOSTATEIMAGEMASK(state);
2355 TRACE("state: 0x%x\n", state);
2356 TREEVIEW_Invalidate(infoPtr, item);
2361 /* Painting *************************************************************/
2363 /* Draw the lines and expand button for an item. Also draws one section
2364 * of the line from item's parent to item's parent's next sibling. */
2365 static void
2366 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2368 LONG centerx, centery;
2369 BOOL lar = ((infoPtr->dwStyle
2370 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2371 > TVS_LINESATROOT);
2372 HBRUSH hbr, hbrOld;
2373 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2375 if (!lar && item->iLevel == 0)
2376 return;
2378 hbr = CreateSolidBrush(clrBk);
2379 hbrOld = SelectObject(hdc, hbr);
2381 centerx = (item->linesOffset + item->stateOffset) / 2;
2382 centery = (item->rect.top + item->rect.bottom) / 2;
2384 if (infoPtr->dwStyle & TVS_HASLINES)
2386 HPEN hOldPen, hNewPen;
2387 HTREEITEM parent;
2388 LOGBRUSH lb;
2390 /* Get a dotted grey pen */
2391 lb.lbStyle = BS_SOLID;
2392 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2393 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2394 hOldPen = SelectObject(hdc, hNewPen);
2396 /* Make sure the center is on a dot (using +2 instead
2397 * of +1 gives us pixel-by-pixel compat with native) */
2398 centery = (centery + 2) & ~1;
2400 MoveToEx(hdc, item->stateOffset, centery, NULL);
2401 LineTo(hdc, centerx - 1, centery);
2403 if (item->prevSibling || item->parent != infoPtr->root)
2405 MoveToEx(hdc, centerx, item->rect.top, NULL);
2406 LineTo(hdc, centerx, centery);
2409 if (item->nextSibling)
2411 MoveToEx(hdc, centerx, centery, NULL);
2412 LineTo(hdc, centerx, item->rect.bottom + 1);
2415 /* Draw the line from our parent to its next sibling. */
2416 parent = item->parent;
2417 while (parent != infoPtr->root)
2419 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2421 if (parent->nextSibling
2422 /* skip top-levels unless TVS_LINESATROOT */
2423 && parent->stateOffset > parent->linesOffset)
2425 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2426 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2429 parent = parent->parent;
2432 SelectObject(hdc, hOldPen);
2433 DeleteObject(hNewPen);
2437 * Display the (+/-) signs
2440 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2442 if (item->cChildren)
2444 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2445 if (theme)
2447 RECT glyphRect = item->rect;
2448 glyphRect.left = item->linesOffset;
2449 glyphRect.right = item->stateOffset;
2450 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2451 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2452 &glyphRect, NULL);
2454 else
2456 LONG height = item->rect.bottom - item->rect.top;
2457 LONG width = item->stateOffset - item->linesOffset;
2458 LONG rectsize = min(height, width) / 4;
2459 /* plussize = ceil(rectsize * 3/4) */
2460 LONG plussize = (rectsize + 1) * 3 / 4;
2462 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2463 HPEN old_pen = SelectObject(hdc, new_pen);
2465 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2466 centerx + rectsize + 2, centery + rectsize + 2);
2468 SelectObject(hdc, old_pen);
2469 DeleteObject(new_pen);
2471 /* draw +/- signs with current text color */
2472 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2473 old_pen = SelectObject(hdc, new_pen);
2475 if (height < 18 || width < 18)
2477 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2478 LineTo(hdc, centerx + plussize, centery);
2480 if (!(item->state & TVIS_EXPANDED) ||
2481 (item->state & TVIS_EXPANDPARTIAL))
2483 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2484 LineTo(hdc, centerx, centery + plussize);
2487 else
2489 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2490 centerx + plussize, centery + 2);
2492 if (!(item->state & TVIS_EXPANDED) ||
2493 (item->state & TVIS_EXPANDPARTIAL))
2495 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2496 centerx + 2, centery + plussize);
2497 SetPixel(hdc, centerx - 1, centery, clrBk);
2498 SetPixel(hdc, centerx + 1, centery, clrBk);
2502 SelectObject(hdc, old_pen);
2503 DeleteObject(new_pen);
2507 SelectObject(hdc, hbrOld);
2508 DeleteObject(hbr);
2511 static void
2512 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2514 INT cditem;
2515 HFONT hOldFont;
2516 COLORREF oldTextColor, oldTextBkColor;
2517 int centery;
2518 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2519 NMTVCUSTOMDRAW nmcdhdr;
2521 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2523 /* - If item is drop target or it is selected and window is in focus -
2524 * use blue background (COLOR_HIGHLIGHT).
2525 * - If item is selected, window is not in focus, but it has style
2526 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2527 * - Otherwise - use background color
2529 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2530 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2531 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2533 if ((item->state & TVIS_DROPHILITED) || inFocus)
2535 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2536 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2538 else
2540 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2541 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2544 else
2546 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2547 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2548 nmcdhdr.clrText = comctl32_color.clrHighlight;
2549 else
2550 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2553 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2554 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2555 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2557 /* The custom draw handler can query the text rectangle,
2558 * so get ready. */
2559 /* should already be known, set to 0 when changed */
2560 if (!item->textWidth)
2561 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2563 cditem = 0;
2565 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2567 cditem = TREEVIEW_SendCustomDrawItemNotify
2568 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2569 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2571 if (cditem & CDRF_SKIPDEFAULT)
2573 SelectObject(hdc, hOldFont);
2574 return;
2578 if (cditem & CDRF_NEWFONT)
2579 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2581 if (TREEVIEW_IsFullRowSelect(infoPtr))
2583 HBRUSH brush = CreateSolidBrush(nmcdhdr.clrTextBk);
2584 FillRect(hdc, &item->rect, brush);
2585 DeleteObject(brush);
2588 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2590 /* reset colors. Custom draw handler can change them */
2591 SetTextColor(hdc, nmcdhdr.clrText);
2592 SetBkColor(hdc, nmcdhdr.clrTextBk);
2594 centery = (item->rect.top + item->rect.bottom) / 2;
2597 * Display the images associated with this item
2600 INT imageIndex;
2602 /* State images are displayed to the left of the Normal image
2603 * image number is in state; zero should be `display no image'.
2605 imageIndex = STATEIMAGEINDEX(item->state);
2607 if (infoPtr->himlState && imageIndex)
2609 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2610 item->stateOffset,
2611 centery - infoPtr->stateImageHeight / 2,
2612 ILD_NORMAL);
2615 /* Now, draw the normal image; can be either selected,
2616 * non-selected or expanded image.
2619 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2621 /* The item is currently selected */
2622 imageIndex = item->iSelectedImage;
2624 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2626 /* The item is currently not selected but expanded */
2627 imageIndex = item->iExpandedImage;
2629 else
2631 /* The item is not selected and not expanded */
2632 imageIndex = item->iImage;
2635 if (infoPtr->himlNormal)
2637 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2639 style |= item->state & TVIS_OVERLAYMASK;
2641 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2642 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2643 0, 0,
2644 TREEVIEW_IsFullRowSelect(infoPtr) ? nmcdhdr.clrTextBk : infoPtr->clrBk,
2645 item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2646 style);
2652 * Display the text associated with this item
2655 /* Don't paint item's text if it's being edited */
2656 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2658 if (item->pszText)
2660 RECT rcText;
2661 UINT align;
2662 SIZE sz;
2664 rcText.top = item->rect.top;
2665 rcText.bottom = item->rect.bottom;
2666 rcText.left = item->textOffset;
2667 rcText.right = rcText.left + item->textWidth + 4;
2669 TRACE("drawing text %s at (%s)\n",
2670 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2672 /* Draw it */
2673 GetTextExtentPoint32W(hdc, item->pszText, lstrlenW(item->pszText), &sz);
2675 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2676 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2677 ETO_CLIPPED | ETO_OPAQUE,
2678 &rcText,
2679 item->pszText,
2680 lstrlenW(item->pszText),
2681 NULL);
2682 SetTextAlign(hdc, align);
2684 /* Draw focus box around the selected item */
2685 if ((item == infoPtr->selectedItem) && inFocus)
2687 DrawFocusRect(hdc,&rcText);
2692 /* Draw insertion mark if necessary */
2694 if (infoPtr->insertMarkItem)
2695 TRACE("item:%d,mark:%p\n",
2696 TREEVIEW_GetItemIndex(infoPtr, item),
2697 infoPtr->insertMarkItem);
2699 if (item == infoPtr->insertMarkItem)
2701 HPEN hNewPen, hOldPen;
2702 int offset;
2703 int left, right;
2705 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2706 hOldPen = SelectObject(hdc, hNewPen);
2708 if (infoPtr->insertBeforeorAfter)
2709 offset = item->rect.bottom - 1;
2710 else
2711 offset = item->rect.top + 1;
2713 left = item->textOffset - 2;
2714 right = item->textOffset + item->textWidth + 2;
2716 MoveToEx(hdc, left, offset - 3, NULL);
2717 LineTo(hdc, left, offset + 4);
2719 MoveToEx(hdc, left, offset, NULL);
2720 LineTo(hdc, right + 1, offset);
2722 MoveToEx(hdc, right, offset + 3, NULL);
2723 LineTo(hdc, right, offset - 4);
2725 SelectObject(hdc, hOldPen);
2726 DeleteObject(hNewPen);
2729 /* Restore the hdc state */
2730 SetTextColor(hdc, oldTextColor);
2731 SetBkColor(hdc, oldTextBkColor);
2732 SelectObject(hdc, hOldFont);
2734 if (cditem & CDRF_NOTIFYPOSTPAINT)
2736 cditem = TREEVIEW_SendCustomDrawItemNotify
2737 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2738 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2742 /* Computes treeHeight and treeWidth and updates the scroll bars.
2744 static void
2745 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2747 TREEVIEW_ITEM *item;
2748 HWND hwnd = infoPtr->hwnd;
2749 BOOL vert = FALSE;
2750 BOOL horz = FALSE;
2751 SCROLLINFO si;
2752 LONG scrollX = infoPtr->scrollX;
2754 infoPtr->treeWidth = 0;
2755 infoPtr->treeHeight = 0;
2757 /* We iterate through all visible items in order to get the tree height
2758 * and width */
2759 item = infoPtr->root->firstChild;
2761 while (item != NULL)
2763 if (ISVISIBLE(item))
2765 /* actually we draw text at textOffset + 2 */
2766 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2767 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2769 /* This is scroll-adjusted, but we fix this below. */
2770 infoPtr->treeHeight = item->rect.bottom;
2773 item = TREEVIEW_GetNextListItem(infoPtr, item);
2776 /* Fix the scroll adjusted treeHeight and treeWidth. */
2777 if (infoPtr->root->firstChild)
2778 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2780 infoPtr->treeWidth += infoPtr->scrollX;
2782 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2784 /* Adding one scroll bar may take up enough space that it forces us
2785 * to add the other as well. */
2786 if (infoPtr->treeHeight > infoPtr->clientHeight)
2788 vert = TRUE;
2790 if (infoPtr->treeWidth
2791 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2792 horz = TRUE;
2794 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2795 horz = TRUE;
2797 if (!vert && horz && infoPtr->treeHeight
2798 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2799 vert = TRUE;
2801 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2803 si.cbSize = sizeof(SCROLLINFO);
2804 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2805 si.nMin = 0;
2807 if (vert)
2809 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2810 if ( si.nPage && NULL != infoPtr->firstVisible)
2812 si.nPos = infoPtr->firstVisible->visibleOrder;
2813 si.nMax = infoPtr->maxVisibleOrder - 1;
2815 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2817 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2818 ShowScrollBar(hwnd, SB_VERT, TRUE);
2819 infoPtr->uInternalStatus |= TV_VSCROLL;
2821 else
2823 if (infoPtr->uInternalStatus & TV_VSCROLL)
2824 ShowScrollBar(hwnd, SB_VERT, FALSE);
2825 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2828 else
2830 if (infoPtr->uInternalStatus & TV_VSCROLL)
2831 ShowScrollBar(hwnd, SB_VERT, FALSE);
2832 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2835 if (horz)
2837 si.nPage = infoPtr->clientWidth;
2838 si.nPos = infoPtr->scrollX;
2839 si.nMax = infoPtr->treeWidth - 1;
2841 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2843 si.nPos = si.nMax - max( si.nPage-1, 0 );
2844 scrollX = si.nPos;
2847 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2848 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2849 infoPtr->uInternalStatus |= TV_HSCROLL;
2851 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2852 TREEVIEW_HScroll(infoPtr,
2853 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2855 else
2857 if (infoPtr->uInternalStatus & TV_HSCROLL)
2858 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2859 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2861 scrollX = 0;
2862 if (infoPtr->scrollX != 0)
2864 TREEVIEW_HScroll(infoPtr,
2865 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2869 if (!horz)
2870 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2873 static void
2874 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2876 HBRUSH hBrush;
2877 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2879 hBrush = CreateSolidBrush(clrBk);
2880 FillRect(hdc, rc, hBrush);
2881 DeleteObject(hBrush);
2884 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2885 static LRESULT
2886 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2888 RECT rect;
2890 TRACE("%p\n", infoPtr);
2892 GetClientRect(infoPtr->hwnd, &rect);
2893 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2895 return 1;
2898 static void
2899 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2901 HWND hwnd = infoPtr->hwnd;
2902 RECT rect = *rc;
2903 TREEVIEW_ITEM *item;
2905 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2907 TRACE("empty window\n");
2908 return;
2911 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2912 hdc, rect);
2914 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2916 ReleaseDC(hwnd, hdc);
2917 return;
2920 for (item = infoPtr->root->firstChild;
2921 item != NULL;
2922 item = TREEVIEW_GetNextListItem(infoPtr, item))
2924 if (ISVISIBLE(item))
2926 /* Avoid unneeded calculations */
2927 if (item->rect.top > rect.bottom)
2928 break;
2929 if (item->rect.bottom < rect.top)
2930 continue;
2932 TREEVIEW_DrawItem(infoPtr, hdc, item);
2936 TREEVIEW_UpdateScrollBars(infoPtr);
2938 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2939 infoPtr->cdmode =
2940 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2943 static inline void
2944 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2946 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2949 static void
2950 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2952 if (item)
2953 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2954 else
2955 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2958 static void
2959 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2961 RECT rc;
2962 HBITMAP hbm, hbmOld;
2963 HDC hdc, hdcScreen;
2964 int nIndex;
2966 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2968 hdcScreen = GetDC(0);
2970 hdc = CreateCompatibleDC(hdcScreen);
2971 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2972 hbmOld = SelectObject(hdc, hbm);
2974 SetRect(&rc, 0, 0, 48, 16);
2975 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2977 SetRect(&rc, 18, 2, 30, 14);
2978 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2979 DFCS_BUTTONCHECK|DFCS_FLAT);
2981 SetRect(&rc, 34, 2, 46, 14);
2982 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2983 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2985 SelectObject(hdc, hbmOld);
2986 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2987 comctl32_color.clrWindow);
2988 TRACE("checkbox index %d\n", nIndex);
2990 DeleteObject(hbm);
2991 DeleteDC(hdc);
2992 ReleaseDC(0, hdcScreen);
2994 infoPtr->stateImageWidth = 16;
2995 infoPtr->stateImageHeight = 16;
2998 static void
2999 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3001 TREEVIEW_ITEM *child = item->firstChild;
3003 item->state &= ~TVIS_STATEIMAGEMASK;
3004 item->state |= INDEXTOSTATEIMAGEMASK(1);
3006 while (child)
3008 TREEVIEW_ITEM *next = child->nextSibling;
3009 TREEVIEW_ResetImageStateIndex(infoPtr, child);
3010 child = next;
3014 static LRESULT
3015 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
3017 HDC hdc;
3018 PAINTSTRUCT ps;
3019 RECT rc;
3021 TRACE("(%p %p)\n", infoPtr, hdc_ref);
3023 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
3025 TREEVIEW_InitCheckboxes(infoPtr);
3026 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
3028 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
3029 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
3030 TREEVIEW_UpdateScrollBars(infoPtr);
3031 TREEVIEW_Invalidate(infoPtr, NULL);
3034 if (hdc_ref)
3036 hdc = hdc_ref;
3037 GetClientRect(infoPtr->hwnd, &rc);
3038 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3040 else
3042 hdc = BeginPaint(infoPtr->hwnd, &ps);
3043 rc = ps.rcPaint;
3044 if(ps.fErase)
3045 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3048 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3049 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3051 if (!hdc_ref)
3052 EndPaint(infoPtr->hwnd, &ps);
3054 return 0;
3057 static LRESULT
3058 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3060 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3062 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3063 return 0;
3065 if (options & PRF_ERASEBKGND)
3066 TREEVIEW_EraseBackground(infoPtr, hdc);
3068 if (options & PRF_CLIENT)
3070 RECT rc;
3071 GetClientRect(infoPtr->hwnd, &rc);
3072 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3075 return 0;
3078 /* Sorting **************************************************************/
3080 /***************************************************************************
3081 * Forward the DPA local callback to the treeview owner callback
3083 static INT WINAPI
3084 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3085 const TVSORTCB *pCallBackSort)
3087 /* Forward the call to the client-defined callback */
3088 return pCallBackSort->lpfnCompare(first->lParam,
3089 second->lParam,
3090 pCallBackSort->lParam);
3093 /***************************************************************************
3094 * Treeview native sort routine: sort on item text.
3096 static INT WINAPI
3097 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3098 const TREEVIEW_INFO *infoPtr)
3100 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3101 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3103 if(first->pszText && second->pszText)
3104 return lstrcmpiW(first->pszText, second->pszText);
3105 else if(first->pszText)
3106 return -1;
3107 else if(second->pszText)
3108 return 1;
3109 else
3110 return 0;
3113 /* Returns the number of physical children belonging to item. */
3114 static INT
3115 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3117 INT cChildren = 0;
3118 HTREEITEM hti;
3120 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3121 cChildren++;
3123 return cChildren;
3126 /* Returns a DPA containing a pointer to each physical child of item in
3127 * sibling order. If item has no children, an empty DPA is returned. */
3128 static HDPA
3129 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3131 HTREEITEM child;
3133 HDPA list = DPA_Create(8);
3134 if (list == 0) return NULL;
3136 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3138 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3140 DPA_Destroy(list);
3141 return NULL;
3145 return list;
3148 /***************************************************************************
3149 * Setup the treeview structure with regards of the sort method
3150 * and sort the children of the TV item specified in lParam
3151 * fRecurse: currently unused. Should be zero.
3152 * parent: if pSort!=NULL, should equal pSort->hParent.
3153 * otherwise, item which child items are to be sorted.
3154 * pSort: sort method info. if NULL, sort on item text.
3155 * if non-NULL, sort on item's lParam content, and let the
3156 * application decide what that means. See also TVM_SORTCHILDRENCB.
3159 static LRESULT
3160 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3161 LPTVSORTCB pSort)
3163 INT cChildren;
3164 PFNDPACOMPARE pfnCompare;
3165 LPARAM lpCompare;
3167 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3168 if (parent == TVI_ROOT || parent == NULL)
3169 parent = infoPtr->root;
3171 /* Check for a valid handle to the parent item */
3172 if (!TREEVIEW_ValidItem(infoPtr, parent))
3174 ERR("invalid item hParent=%p\n", parent);
3175 return FALSE;
3178 if (pSort)
3180 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3181 lpCompare = (LPARAM)pSort;
3183 else
3185 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3186 lpCompare = (LPARAM)infoPtr;
3189 cChildren = TREEVIEW_CountChildren(parent);
3191 /* Make sure there is something to sort */
3192 if (cChildren > 1)
3194 /* TREEVIEW_ITEM rechaining */
3195 INT count = 0;
3196 HTREEITEM item = 0;
3197 HTREEITEM nextItem = 0;
3198 HTREEITEM prevItem = 0;
3200 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3202 if (sortList == NULL)
3203 return FALSE;
3205 /* let DPA sort the list */
3206 DPA_Sort(sortList, pfnCompare, lpCompare);
3208 /* The order of DPA entries has been changed, so fixup the
3209 * nextSibling and prevSibling pointers. */
3211 item = DPA_GetPtr(sortList, count++);
3212 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3214 /* link the two current item together */
3215 item->nextSibling = nextItem;
3216 nextItem->prevSibling = item;
3218 if (prevItem == NULL)
3220 /* this is the first item, update the parent */
3221 parent->firstChild = item;
3222 item->prevSibling = NULL;
3224 else
3226 /* fix the back chaining */
3227 item->prevSibling = prevItem;
3230 /* get ready for the next one */
3231 prevItem = item;
3232 item = nextItem;
3235 /* the last item is pointed to by item and never has a sibling */
3236 item->nextSibling = NULL;
3237 parent->lastChild = item;
3239 DPA_Destroy(sortList);
3241 TREEVIEW_VerifyTree(infoPtr);
3243 if (parent->state & TVIS_EXPANDED)
3245 int visOrder = infoPtr->firstVisible->visibleOrder;
3247 if (parent == infoPtr->root)
3248 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3249 else
3250 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3252 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3254 TREEVIEW_ITEM *item;
3256 for (item = infoPtr->root->firstChild; item != NULL;
3257 item = TREEVIEW_GetNextListItem(infoPtr, item))
3259 if (item->visibleOrder == visOrder)
3260 break;
3263 if (!item) item = parent->firstChild;
3264 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3267 TREEVIEW_Invalidate(infoPtr, NULL);
3270 return TRUE;
3272 return FALSE;
3276 /***************************************************************************
3277 * Setup the treeview structure with regards of the sort method
3278 * and sort the children of the TV item specified in lParam
3280 static LRESULT
3281 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3283 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3287 /***************************************************************************
3288 * Sort the children of the TV item specified in lParam.
3290 static LRESULT
3291 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3293 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3297 /* Expansion/Collapse ***************************************************/
3299 static BOOL
3300 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3301 UINT action)
3303 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3304 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3305 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3306 0, item);
3309 static VOID
3310 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3311 UINT action)
3313 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3314 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3315 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3316 0, item);
3320 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3321 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3322 static BOOL
3323 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3324 BOOL bRemoveChildren, BOOL bUser)
3326 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3327 BOOL bSetSelection, bSetFirstVisible;
3328 RECT scrollRect;
3329 LONG scrollDist = 0;
3330 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3331 BOOL wasExpanded;
3333 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3335 if (!TREEVIEW_HasChildren(infoPtr, item))
3336 return FALSE;
3338 if (bUser)
3339 TREEVIEW_SendExpanding(infoPtr, item, action);
3341 if (item->firstChild == NULL)
3342 return FALSE;
3344 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3345 item->state &= ~TVIS_EXPANDED;
3347 if (wasExpanded && bUser)
3348 TREEVIEW_SendExpanded(infoPtr, item, action);
3350 bSetSelection = (infoPtr->selectedItem != NULL
3351 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3353 bSetFirstVisible = (infoPtr->firstVisible != NULL
3354 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3356 tmpItem = item;
3357 while (tmpItem)
3359 if (tmpItem->nextSibling)
3361 nextItem = tmpItem->nextSibling;
3362 break;
3364 tmpItem = tmpItem->parent;
3367 if (nextItem)
3368 scrollDist = nextItem->rect.top;
3370 if (bRemoveChildren)
3372 INT old_cChildren = item->cChildren;
3373 TRACE("TVE_COLLAPSERESET\n");
3374 item->state &= ~TVIS_EXPANDEDONCE;
3375 TREEVIEW_RemoveAllChildren(infoPtr, item);
3376 item->cChildren = old_cChildren;
3378 if (!wasExpanded)
3379 return FALSE;
3381 if (item->firstChild)
3383 TREEVIEW_ITEM *i, *sibling;
3385 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3387 for (i = item->firstChild; i != sibling;
3388 i = TREEVIEW_GetNextListItem(infoPtr, i))
3390 i->visibleOrder = -1;
3394 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3396 if (nextItem)
3397 scrollDist = -(scrollDist - nextItem->rect.top);
3399 if (bSetSelection)
3401 /* Don't call DoSelectItem, it sends notifications. */
3402 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3403 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3404 item->state |= TVIS_SELECTED;
3405 infoPtr->selectedItem = item;
3408 TREEVIEW_UpdateScrollBars(infoPtr);
3410 scrollRect.left = 0;
3411 scrollRect.right = infoPtr->clientWidth;
3412 scrollRect.bottom = infoPtr->clientHeight;
3414 if (nextItem)
3416 scrollRect.top = nextItem->rect.top;
3418 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3419 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3420 TREEVIEW_Invalidate(infoPtr, item);
3421 } else {
3422 scrollRect.top = item->rect.top;
3423 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3426 TREEVIEW_SetFirstVisible(infoPtr,
3427 bSetFirstVisible ? item : infoPtr->firstVisible,
3428 TRUE);
3430 return wasExpanded;
3433 static BOOL
3434 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3435 BOOL partial, BOOL user)
3437 LONG scrollDist;
3438 LONG orgNextTop = 0;
3439 RECT scrollRect;
3440 TREEVIEW_ITEM *nextItem, *tmpItem;
3441 BOOL sendsNotifications;
3443 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3445 if (!TREEVIEW_HasChildren(infoPtr, item))
3446 return FALSE;
3448 tmpItem = item; nextItem = NULL;
3449 while (tmpItem)
3451 if (tmpItem->nextSibling)
3453 nextItem = tmpItem->nextSibling;
3454 break;
3456 tmpItem = tmpItem->parent;
3459 if (nextItem)
3460 orgNextTop = nextItem->rect.top;
3462 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3464 sendsNotifications = user || ((item->cChildren != 0) &&
3465 !(item->state & TVIS_EXPANDEDONCE));
3466 if (sendsNotifications)
3468 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3470 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3471 return FALSE;
3474 if (!item->firstChild)
3475 return FALSE;
3477 item->state |= TVIS_EXPANDED;
3479 if (partial)
3480 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3482 if (ISVISIBLE(item))
3484 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3485 TREEVIEW_UpdateSubTree(infoPtr, item);
3486 TREEVIEW_UpdateScrollBars(infoPtr);
3488 scrollRect.left = 0;
3489 scrollRect.bottom = infoPtr->treeHeight;
3490 scrollRect.right = infoPtr->clientWidth;
3491 if (nextItem)
3493 scrollDist = nextItem->rect.top - orgNextTop;
3494 scrollRect.top = orgNextTop;
3496 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3497 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3498 TREEVIEW_Invalidate (infoPtr, item);
3499 } else {
3500 scrollRect.top = item->rect.top;
3501 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3504 /* Scroll up so that as many children as possible are visible.
3505 * This fails when expanding causes an HScroll bar to appear, but we
3506 * don't know that yet, so the last item is obscured. */
3507 if (item->firstChild != NULL)
3509 int nChildren = item->lastChild->visibleOrder
3510 - item->firstChild->visibleOrder + 1;
3512 int visible_pos = item->visibleOrder
3513 - infoPtr->firstVisible->visibleOrder;
3515 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3517 if (visible_pos > 0 && nChildren > rows_below)
3519 int scroll = nChildren - rows_below;
3521 if (scroll > visible_pos)
3522 scroll = visible_pos;
3524 if (scroll > 0)
3526 TREEVIEW_ITEM *newFirstVisible
3527 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3528 scroll);
3531 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3537 if (sendsNotifications) {
3538 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3539 item->state |= TVIS_EXPANDEDONCE;
3542 return TRUE;
3545 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3546 to mouse messages and TVM_SELECTITEM.
3548 selection - previously selected item, used to collapse a part of a tree
3549 item - new selected item
3551 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3552 HTREEITEM selection, HTREEITEM item)
3554 TREEVIEW_ITEM *prev, *curr;
3556 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3558 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3561 * Close the previous item and its ancestors as long as they are not
3562 * ancestors of the current item
3564 for (prev = selection; prev && TREEVIEW_ValidItem(infoPtr, prev); prev = prev->parent)
3566 for (curr = item; curr && TREEVIEW_ValidItem(infoPtr, curr); curr = curr->parent)
3568 if (curr == prev)
3569 goto finish;
3571 TREEVIEW_Collapse(infoPtr, prev, FALSE, TRUE);
3574 finish:
3576 * Expand the current item
3578 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3581 static BOOL
3582 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3584 TRACE("item=%p, user=%d\n", item, user);
3586 if (item->state & TVIS_EXPANDED)
3587 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3588 else
3589 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3592 static VOID
3593 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3595 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3597 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3599 if (TREEVIEW_HasChildren(infoPtr, item))
3600 TREEVIEW_ExpandAll(infoPtr, item);
3604 /* Note:If the specified item is the child of a collapsed parent item,
3605 the parent's list of child items is (recursively) expanded to reveal the
3606 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3607 know if it also applies here.
3610 static LRESULT
3611 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3613 if (!TREEVIEW_ValidItem(infoPtr, item))
3614 return 0;
3616 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3617 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3618 flag, item->state);
3620 switch (flag & TVE_TOGGLE)
3622 case TVE_COLLAPSE:
3623 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3624 FALSE);
3626 case TVE_EXPAND:
3627 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3628 FALSE);
3630 case TVE_TOGGLE:
3631 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3633 default:
3634 return 0;
3638 /* Hit-Testing **********************************************************/
3640 static TREEVIEW_ITEM *
3641 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3643 TREEVIEW_ITEM *item;
3644 LONG row;
3646 if (!infoPtr->firstVisible)
3647 return NULL;
3649 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3651 for (item = infoPtr->firstVisible; item != NULL;
3652 item = TREEVIEW_GetNextListItem(infoPtr, item))
3654 if (row >= item->visibleOrder
3655 && row < item->visibleOrder + item->iIntegral)
3656 break;
3659 return item;
3662 static TREEVIEW_ITEM *
3663 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3665 TREEVIEW_ITEM *item;
3666 RECT rect;
3667 UINT status;
3668 LONG x, y;
3670 lpht->hItem = 0;
3671 GetClientRect(infoPtr->hwnd, &rect);
3672 status = 0;
3673 x = lpht->pt.x;
3674 y = lpht->pt.y;
3676 if (x < rect.left)
3678 status |= TVHT_TOLEFT;
3680 else if (x > rect.right)
3682 status |= TVHT_TORIGHT;
3685 if (y < rect.top)
3687 status |= TVHT_ABOVE;
3689 else if (y > rect.bottom)
3691 status |= TVHT_BELOW;
3694 if (status)
3696 lpht->flags = status;
3697 return NULL;
3700 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3701 if (!item)
3703 lpht->flags = TVHT_NOWHERE;
3704 return NULL;
3707 if (!item->textWidth)
3708 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
3710 if (x >= item->textOffset + item->textWidth)
3712 lpht->flags = TVHT_ONITEMRIGHT;
3714 else if (x >= item->textOffset)
3716 lpht->flags = TVHT_ONITEMLABEL;
3718 else if (x >= item->imageOffset)
3720 lpht->flags = TVHT_ONITEMICON;
3722 else if (x >= item->stateOffset)
3724 lpht->flags = TVHT_ONITEMSTATEICON;
3726 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3728 lpht->flags = TVHT_ONITEMBUTTON;
3730 else
3732 lpht->flags = TVHT_ONITEMINDENT;
3735 lpht->hItem = item;
3736 TRACE("(%d,%d):result 0x%x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3738 return item;
3741 /* Item Label Editing ***************************************************/
3743 static LRESULT
3744 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3746 return (LRESULT)infoPtr->hwndEdit;
3749 static LRESULT CALLBACK
3750 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3752 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3753 BOOL bCancel = FALSE;
3754 LRESULT rc;
3756 switch (uMsg)
3758 case WM_PAINT:
3759 TRACE("WM_PAINT start\n");
3760 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3761 lParam);
3762 TRACE("WM_PAINT done\n");
3763 return rc;
3765 case WM_KILLFOCUS:
3766 if (infoPtr->bIgnoreEditKillFocus)
3767 return TRUE;
3768 break;
3770 case WM_DESTROY:
3772 WNDPROC editProc = infoPtr->wpEditOrig;
3773 infoPtr->wpEditOrig = 0;
3774 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3775 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3778 case WM_GETDLGCODE:
3779 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3781 case WM_KEYDOWN:
3782 if (wParam == VK_ESCAPE)
3784 bCancel = TRUE;
3785 break;
3787 else if (wParam == VK_RETURN)
3789 break;
3792 /* fall through */
3793 default:
3794 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3797 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3798 /* eg. Using a messagebox */
3800 infoPtr->bIgnoreEditKillFocus = TRUE;
3801 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3802 infoPtr->bIgnoreEditKillFocus = FALSE;
3804 return 0;
3808 /* should handle edit control messages here */
3810 static LRESULT
3811 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3813 TRACE("code=0x%x, id=0x%x, handle=0x%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3815 switch (HIWORD(wParam))
3817 case EN_UPDATE:
3820 * Adjust the edit window size
3822 WCHAR buffer[1024];
3823 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3824 HDC hdc = GetDC(infoPtr->hwndEdit);
3825 SIZE sz;
3826 HFONT hFont, hOldFont = 0;
3828 TRACE("edit=%p\n", infoPtr->hwndEdit);
3830 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3832 infoPtr->bLabelChanged = TRUE;
3834 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
3836 /* Select font to get the right dimension of the string */
3837 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3839 if (hFont != 0)
3841 hOldFont = SelectObject(hdc, hFont);
3844 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
3846 TEXTMETRICW textMetric;
3848 /* Add Extra spacing for the next character */
3849 GetTextMetricsW(hdc, &textMetric);
3850 sz.cx += (textMetric.tmMaxCharWidth * 2);
3852 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3853 sz.cx = min(sz.cx,
3854 infoPtr->clientWidth - editItem->textOffset + 2);
3856 SetWindowPos(infoPtr->hwndEdit,
3857 HWND_TOP,
3860 sz.cx,
3861 editItem->rect.bottom - editItem->rect.top + 3,
3862 SWP_NOMOVE | SWP_DRAWFRAME);
3865 if (hFont != 0)
3867 SelectObject(hdc, hOldFont);
3870 ReleaseDC(infoPtr->hwnd, hdc);
3871 break;
3873 case EN_KILLFOCUS:
3874 /* apparently we should respect passed handle value */
3875 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3877 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3878 break;
3880 default:
3881 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3884 return 0;
3887 static HWND
3888 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3890 HWND hwnd = infoPtr->hwnd;
3891 HWND hwndEdit;
3892 SIZE sz;
3893 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3894 HDC hdc;
3895 HFONT hOldFont=0;
3896 TEXTMETRICW textMetric;
3898 TRACE("%p %p\n", hwnd, hItem);
3899 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3900 return NULL;
3902 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3903 return NULL;
3905 if (infoPtr->hwndEdit)
3906 return infoPtr->hwndEdit;
3908 infoPtr->bLabelChanged = FALSE;
3910 /* make edit item visible */
3911 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3913 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3915 hdc = GetDC(hwnd);
3916 /* Select the font to get appropriate metric dimensions */
3917 if (infoPtr->hFont != 0)
3919 hOldFont = SelectObject(hdc, infoPtr->hFont);
3922 /* Get string length in pixels */
3923 if (hItem->pszText)
3924 GetTextExtentPoint32W(hdc, hItem->pszText, lstrlenW(hItem->pszText),
3925 &sz);
3926 else
3927 GetTextExtentPoint32A(hdc, "", 0, &sz);
3929 /* Add Extra spacing for the next character */
3930 GetTextMetricsW(hdc, &textMetric);
3931 sz.cx += (textMetric.tmMaxCharWidth * 2);
3933 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3934 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3936 if (infoPtr->hFont != 0)
3938 SelectObject(hdc, hOldFont);
3941 ReleaseDC(hwnd, hdc);
3943 infoPtr->editItem = hItem;
3945 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3946 WC_EDITW,
3948 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3949 WS_CLIPSIBLINGS | ES_WANTRETURN |
3950 ES_LEFT, hItem->textOffset - 2,
3951 hItem->rect.top - 1, sz.cx + 3,
3952 hItem->rect.bottom -
3953 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3954 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3956 infoPtr->hwndEdit = hwndEdit;
3958 /* Get a 2D border. */
3959 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3960 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3961 SetWindowLongW(hwndEdit, GWL_STYLE,
3962 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3964 SendMessageW(hwndEdit, WM_SETFONT,
3965 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3967 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3968 (DWORD_PTR)
3969 TREEVIEW_Edit_SubclassProc);
3970 SendMessageW(hwndEdit, EM_SETLIMITTEXT, MAX_PATH - 1, 0);
3971 if (hItem->pszText)
3972 SetWindowTextW(hwndEdit, hItem->pszText);
3974 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3976 DestroyWindow(hwndEdit);
3977 infoPtr->hwndEdit = 0;
3978 infoPtr->editItem = NULL;
3979 return NULL;
3982 SetFocus(hwndEdit);
3983 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3984 ShowWindow(hwndEdit, SW_SHOW);
3986 return hwndEdit;
3990 static LRESULT
3991 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3993 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3994 NMTVDISPINFOW tvdi;
3995 BOOL bCommit;
3996 WCHAR tmpText[MAX_PATH] = { '\0' };
3997 WCHAR *newText;
3998 int iLength = 0;
4000 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
4002 tvdi.item.mask = 0;
4003 tvdi.item.hItem = editedItem;
4004 tvdi.item.state = editedItem->state;
4005 tvdi.item.lParam = editedItem->lParam;
4007 if (!bCancel)
4009 if (!infoPtr->bNtfUnicode)
4010 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, ARRAY_SIZE(tmpText));
4011 else
4012 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, ARRAY_SIZE(tmpText));
4014 tvdi.item.mask = TVIF_TEXT;
4015 tvdi.item.pszText = tmpText;
4016 tvdi.item.cchTextMax = ARRAY_SIZE(tmpText);
4018 else
4020 tvdi.item.pszText = NULL;
4021 tvdi.item.cchTextMax = 0;
4024 bCommit = TREEVIEW_SendRealNotify(infoPtr, TVN_ENDLABELEDITW, &tvdi.hdr);
4026 if (!bCancel && bCommit) /* Apply the changes */
4028 if (!infoPtr->bNtfUnicode)
4030 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tvdi.item.pszText, -1, NULL, 0 );
4031 newText = heap_alloc(len * sizeof(WCHAR));
4032 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tvdi.item.pszText, -1, newText, len );
4033 iLength = len - 1;
4035 else
4036 newText = tvdi.item.pszText;
4038 if (lstrcmpW(newText, editedItem->pszText) != 0)
4040 WCHAR *ptr = heap_realloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
4041 if (ptr == NULL)
4043 ERR("OutOfMemory, cannot allocate space for label\n");
4044 if (newText != tmpText) heap_free(newText);
4045 DestroyWindow(infoPtr->hwndEdit);
4046 infoPtr->hwndEdit = 0;
4047 infoPtr->editItem = NULL;
4048 return FALSE;
4050 else
4052 editedItem->pszText = ptr;
4053 editedItem->cchTextMax = iLength + 1;
4054 lstrcpyW(editedItem->pszText, newText);
4055 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4058 if (newText != tmpText) heap_free(newText);
4061 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4062 DestroyWindow(infoPtr->hwndEdit);
4063 infoPtr->hwndEdit = 0;
4064 infoPtr->editItem = NULL;
4065 return TRUE;
4068 static LRESULT
4069 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4071 if (wParam != TV_EDIT_TIMER)
4073 ERR("got unknown timer\n");
4074 return 1;
4077 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4078 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4080 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4082 return 0;
4086 /* Mouse Tracking/Drag **************************************************/
4088 /***************************************************************************
4089 * This is quite unusual piece of code, but that's how it's implemented in
4090 * Windows.
4092 static LRESULT
4093 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4095 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4096 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4097 RECT r;
4098 MSG msg;
4100 r.top = pt.y - cyDrag;
4101 r.left = pt.x - cxDrag;
4102 r.bottom = pt.y + cyDrag;
4103 r.right = pt.x + cxDrag;
4105 SetCapture(infoPtr->hwnd);
4107 while (1)
4109 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4111 if (msg.message == WM_MOUSEMOVE)
4113 pt.x = (short)LOWORD(msg.lParam);
4114 pt.y = (short)HIWORD(msg.lParam);
4115 if (PtInRect(&r, pt))
4116 continue;
4117 else
4119 ReleaseCapture();
4120 return 1;
4123 else if (msg.message >= WM_LBUTTONDOWN &&
4124 msg.message <= WM_RBUTTONDBLCLK)
4126 break;
4129 DispatchMessageW(&msg);
4132 if (GetCapture() != infoPtr->hwnd)
4133 return 0;
4136 ReleaseCapture();
4137 return 0;
4141 static LRESULT
4142 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4144 TREEVIEW_ITEM *item;
4145 TVHITTESTINFO hit;
4147 TRACE("\n");
4148 SetFocus(infoPtr->hwnd);
4150 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4152 /* If there is pending 'edit label' event - kill it now */
4153 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4156 hit.pt.x = (short)LOWORD(lParam);
4157 hit.pt.y = (short)HIWORD(lParam);
4159 item = TREEVIEW_HitTest(infoPtr, &hit);
4160 if (!item)
4161 return 0;
4162 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4164 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4165 { /* FIXME! */
4166 switch (hit.flags)
4168 case TVHT_ONITEMRIGHT:
4169 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4170 break;
4172 case TVHT_ONITEMINDENT:
4173 if (!(infoPtr->dwStyle & TVS_HASLINES))
4175 break;
4177 else
4179 int level = hit.pt.x / infoPtr->uIndent;
4180 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4182 while (item->iLevel > level)
4184 item = item->parent;
4187 /* fall through */
4190 case TVHT_ONITEMLABEL:
4191 case TVHT_ONITEMICON:
4192 case TVHT_ONITEMBUTTON:
4193 TREEVIEW_Toggle(infoPtr, item, TRUE);
4194 break;
4196 case TVHT_ONITEMSTATEICON:
4197 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4198 TREEVIEW_ToggleItemState(infoPtr, item);
4199 else
4200 TREEVIEW_Toggle(infoPtr, item, TRUE);
4201 break;
4204 return TRUE;
4208 static LRESULT
4209 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4211 BOOL do_track, do_select, bDoLabelEdit;
4212 HWND hwnd = infoPtr->hwnd;
4213 TVHITTESTINFO ht;
4215 /* If Edit control is active - kill it and return.
4216 * The best way to do it is to set focus to itself.
4217 * Edit control subclassed procedure will automatically call
4218 * EndEditLabelNow.
4220 if (infoPtr->hwndEdit)
4222 SetFocus(hwnd);
4223 return 0;
4226 ht.pt.x = (short)LOWORD(lParam);
4227 ht.pt.y = (short)HIWORD(lParam);
4229 TREEVIEW_HitTest(infoPtr, &ht);
4230 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4232 /* update focusedItem and redraw both items */
4233 if (ht.hItem)
4235 BOOL do_focus;
4237 if (TREEVIEW_IsFullRowSelect(infoPtr))
4238 do_focus = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEM | TVHT_ONITEMRIGHT);
4239 else
4240 do_focus = ht.flags & TVHT_ONITEM;
4242 if (do_focus)
4244 infoPtr->focusedItem = ht.hItem;
4245 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4246 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4250 if (!(infoPtr->dwStyle & TVS_DISABLEDRAGDROP))
4252 if (TREEVIEW_IsFullRowSelect(infoPtr))
4253 do_track = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEM | TVHT_ONITEMRIGHT);
4254 else
4255 do_track = ht.flags & TVHT_ONITEM;
4257 else
4258 do_track = FALSE;
4261 * If the style allows editing and the node is already selected
4262 * and the click occurred on the item label...
4264 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4265 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4267 /* Send NM_CLICK right away */
4268 if (!do_track && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4269 goto setfocus;
4271 if (ht.flags & TVHT_ONITEMBUTTON)
4273 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4274 goto setfocus;
4276 else if (do_track)
4277 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4278 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4280 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4281 infoPtr->dropItem = ht.hItem;
4283 /* clean up focusedItem as we dragged and won't select this item */
4284 if(infoPtr->focusedItem)
4286 /* refresh the item that was focused */
4287 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4288 infoPtr->focusedItem = NULL;
4290 /* refresh the selected item to return the filled background */
4291 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4294 return 0;
4298 if (do_track && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4299 goto setfocus;
4301 if (TREEVIEW_IsFullRowSelect(infoPtr))
4302 do_select = ht.flags & (TVHT_ONITEMINDENT | TVHT_ONITEMICON | TVHT_ONITEMLABEL | TVHT_ONITEMRIGHT);
4303 else
4304 do_select = ht.flags & (TVHT_ONITEMICON | TVHT_ONITEMLABEL);
4306 if (bDoLabelEdit)
4308 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4309 KillTimer(hwnd, TV_EDIT_TIMER);
4311 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4312 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4314 else if (do_select)
4316 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4318 /* Select the current item */
4319 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4320 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4322 else if (ht.flags & TVHT_ONITEMSTATEICON)
4324 /* TVS_CHECKBOXES requires us to toggle the current state */
4325 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4326 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4329 setfocus:
4330 SetFocus(hwnd);
4331 return 0;
4335 static LRESULT
4336 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4338 TVHITTESTINFO ht;
4340 if (infoPtr->hwndEdit)
4342 SetFocus(infoPtr->hwnd);
4343 return 0;
4346 ht.pt.x = (short)LOWORD(lParam);
4347 ht.pt.y = (short)HIWORD(lParam);
4349 if (TREEVIEW_HitTest(infoPtr, &ht))
4351 infoPtr->focusedItem = ht.hItem;
4352 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4353 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4356 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4358 if (ht.hItem)
4360 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4361 infoPtr->dropItem = ht.hItem;
4364 else
4366 SetFocus(infoPtr->hwnd);
4367 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4369 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4370 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4371 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4375 if (ht.hItem)
4377 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4378 infoPtr->focusedItem = infoPtr->selectedItem;
4379 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4382 return 0;
4385 static LRESULT
4386 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4388 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4389 INT cx, cy;
4390 HDC hdc, htopdc;
4391 HWND hwtop;
4392 HBITMAP hbmp, hOldbmp;
4393 SIZE size;
4394 RECT rc;
4395 HFONT hOldFont;
4397 TRACE("\n");
4399 if (!(infoPtr->himlNormal))
4400 return 0;
4402 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4403 return 0;
4405 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4407 hwtop = GetDesktopWindow();
4408 htopdc = GetDC(hwtop);
4409 hdc = CreateCompatibleDC(htopdc);
4411 hOldFont = SelectObject(hdc, infoPtr->hFont);
4413 if (dragItem->pszText)
4414 GetTextExtentPoint32W(hdc, dragItem->pszText, lstrlenW(dragItem->pszText),
4415 &size);
4416 else
4417 GetTextExtentPoint32A(hdc, "", 0, &size);
4419 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4420 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4421 hOldbmp = SelectObject(hdc, hbmp);
4423 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4424 size.cx += cx;
4425 if (cy > size.cy)
4426 size.cy = cy;
4428 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4429 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4430 ILD_NORMAL);
4433 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4434 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4437 /* draw item text */
4439 SetRect(&rc, cx, 0, size.cx, size.cy);
4441 if (dragItem->pszText)
4442 DrawTextW(hdc, dragItem->pszText, lstrlenW(dragItem->pszText), &rc,
4443 DT_LEFT);
4445 SelectObject(hdc, hOldFont);
4446 SelectObject(hdc, hOldbmp);
4448 ImageList_Add(infoPtr->dragList, hbmp, 0);
4450 DeleteDC(hdc);
4451 DeleteObject(hbmp);
4452 ReleaseDC(hwtop, htopdc);
4454 return (LRESULT)infoPtr->dragList;
4457 /* Selection ************************************************************/
4459 static LRESULT
4460 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4461 INT cause)
4463 TREEVIEW_ITEM *prevSelect;
4465 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4467 TRACE("Entering item %p (%s), flag 0x%x, cause 0x%x, state 0x%x\n",
4468 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4469 newSelect ? newSelect->state : 0);
4471 /* reset and redraw focusedItem if focusedItem was set so we don't */
4472 /* have to worry about the previously focused item when we set a new one */
4473 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4474 infoPtr->focusedItem = NULL;
4476 switch (action)
4478 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4479 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4480 /* Fall through */
4481 case TVGN_CARET:
4482 prevSelect = infoPtr->selectedItem;
4484 if (prevSelect == newSelect) {
4485 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4486 break;
4489 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4490 TVN_SELCHANGINGW,
4491 cause,
4492 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4493 prevSelect,
4494 newSelect))
4495 return FALSE;
4497 if (prevSelect)
4498 prevSelect->state &= ~TVIS_SELECTED;
4499 if (newSelect)
4500 newSelect->state |= TVIS_SELECTED;
4502 infoPtr->selectedItem = newSelect;
4504 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4506 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4507 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4509 TREEVIEW_SendTreeviewNotify(infoPtr,
4510 TVN_SELCHANGEDW,
4511 cause,
4512 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4513 prevSelect,
4514 newSelect);
4515 break;
4517 case TVGN_DROPHILITE:
4518 prevSelect = infoPtr->dropItem;
4520 if (prevSelect)
4521 prevSelect->state &= ~TVIS_DROPHILITED;
4523 infoPtr->dropItem = newSelect;
4525 if (newSelect)
4526 newSelect->state |= TVIS_DROPHILITED;
4528 TREEVIEW_Invalidate(infoPtr, prevSelect);
4529 TREEVIEW_Invalidate(infoPtr, newSelect);
4530 break;
4532 case TVGN_FIRSTVISIBLE:
4533 if (newSelect != NULL)
4535 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4536 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4537 TREEVIEW_Invalidate(infoPtr, NULL);
4539 break;
4542 TRACE("Leaving state 0x%x\n", newSelect ? newSelect->state : 0);
4543 return TRUE;
4546 /* FIXME: handle NM_KILLFOCUS etc */
4547 static LRESULT
4548 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4550 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4552 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4553 return FALSE;
4555 if (item == infoPtr->selectedItem)
4556 return TRUE;
4558 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4560 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4561 return FALSE;
4563 TREEVIEW_SingleExpand(infoPtr, selection, item);
4565 return TRUE;
4568 /*************************************************************************
4569 * TREEVIEW_ProcessLetterKeys
4571 * Processes keyboard messages generated by pressing the letter keys
4572 * on the keyboard.
4573 * What this does is perform a case insensitive search from the
4574 * current position with the following quirks:
4575 * - If two chars or more are pressed in quick succession we search
4576 * for the corresponding string (e.g. 'abc').
4577 * - If there is a delay we wipe away the current search string and
4578 * restart with just that char.
4579 * - If the user keeps pressing the same character, whether slowly or
4580 * fast, so that the search string is entirely composed of this
4581 * character ('aaaaa' for instance), then we search for first item
4582 * that starting with that character.
4583 * - If the user types the above character in quick succession, then
4584 * we must also search for the corresponding string ('aaaaa'), and
4585 * go to that string if there is a match.
4587 * RETURNS
4589 * Zero.
4591 * BUGS
4593 * - The current implementation has a list of characters it will
4594 * accept and it ignores everything else. In particular it will
4595 * ignore accentuated characters which seems to match what
4596 * Windows does. But I'm not sure it makes sense to follow
4597 * Windows there.
4598 * - We don't sound a beep when the search fails.
4599 * - The search should start from the focused item, not from the selected
4600 * item. One reason for this is to allow for multiple selections in trees.
4601 * But currently infoPtr->focusedItem does not seem very usable.
4603 * SEE ALSO
4605 * TREEVIEW_ProcessLetterKeys
4607 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4609 HTREEITEM nItem;
4610 HTREEITEM endidx,idx;
4611 TVITEMEXW item;
4612 WCHAR buffer[MAX_PATH];
4613 DWORD timestamp,elapsed;
4615 /* simple parameter checking */
4616 if (!charCode || !keyData) return 0;
4618 /* only allow the valid WM_CHARs through */
4619 if (!isalnum(charCode) &&
4620 charCode != '.' && charCode != '`' && charCode != '!' &&
4621 charCode != '@' && charCode != '#' && charCode != '$' &&
4622 charCode != '%' && charCode != '^' && charCode != '&' &&
4623 charCode != '*' && charCode != '(' && charCode != ')' &&
4624 charCode != '-' && charCode != '_' && charCode != '+' &&
4625 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4626 charCode != '}' && charCode != '[' && charCode != '{' &&
4627 charCode != '/' && charCode != '?' && charCode != '>' &&
4628 charCode != '<' && charCode != ',' && charCode != '~')
4629 return 0;
4631 /* compute how much time elapsed since last keypress */
4632 timestamp = GetTickCount();
4633 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4634 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4635 } else {
4636 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4639 /* update the search parameters */
4640 infoPtr->lastKeyPressTimestamp=timestamp;
4641 if (elapsed < KEY_DELAY) {
4642 if (infoPtr->nSearchParamLength < ARRAY_SIZE(infoPtr->szSearchParam)) {
4643 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4645 if (infoPtr->charCode != charCode) {
4646 infoPtr->charCode=charCode=0;
4648 } else {
4649 infoPtr->charCode=charCode;
4650 infoPtr->szSearchParam[0]=charCode;
4651 infoPtr->nSearchParamLength=1;
4652 /* Redundant with the 1 char string */
4653 charCode=0;
4656 /* and search from the current position */
4657 nItem=NULL;
4658 if (infoPtr->selectedItem != NULL) {
4659 endidx=infoPtr->selectedItem;
4660 /* if looking for single character match,
4661 * then we must always move forward
4663 if (infoPtr->nSearchParamLength == 1)
4664 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4665 else
4666 idx=endidx;
4667 } else {
4668 endidx=NULL;
4669 idx=infoPtr->root->firstChild;
4671 do {
4672 /* At the end point, sort out wrapping */
4673 if (idx == NULL) {
4675 /* If endidx is null, stop at the last item (ie top to bottom) */
4676 if (endidx == NULL)
4677 break;
4679 /* Otherwise, start again at the very beginning */
4680 idx=infoPtr->root->firstChild;
4682 /* But if we are stopping on the first child, end now! */
4683 if (idx == endidx) break;
4686 /* get item */
4687 ZeroMemory(&item, sizeof(item));
4688 item.mask = TVIF_TEXT;
4689 item.hItem = idx;
4690 item.pszText = buffer;
4691 item.cchTextMax = ARRAY_SIZE(buffer);
4692 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4694 /* check for a match */
4695 if (wcsnicmp(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4696 nItem=idx;
4697 break;
4698 } else if ( (charCode != 0) && (nItem == NULL) &&
4699 (nItem != infoPtr->selectedItem) &&
4700 (wcsnicmp(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4701 /* This would work but we must keep looking for a longer match */
4702 nItem=idx;
4704 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4705 } while (idx != endidx);
4707 if (nItem != NULL) {
4708 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4709 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4713 return 0;
4716 /* Scrolling ************************************************************/
4718 static LRESULT
4719 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4721 int viscount;
4722 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4723 HTREEITEM newFirstVisible = NULL;
4724 int visible_pos = -1;
4726 if (!TREEVIEW_ValidItem(infoPtr, item))
4727 return FALSE;
4729 if (!ISVISIBLE(item))
4731 /* Expand parents as necessary. */
4732 HTREEITEM parent;
4734 /* see if we are trying to ensure that root is visible */
4735 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4736 parent = item->parent;
4737 else
4738 parent = item; /* this item is the topmost item */
4740 while (parent != infoPtr->root)
4742 if (!(parent->state & TVIS_EXPANDED))
4743 TREEVIEW_Expand(infoPtr, parent, FALSE, TRUE);
4745 parent = parent->parent;
4749 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4751 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4752 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4754 if (hasFirstVisible)
4755 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4757 if (visible_pos < 0)
4759 /* item is before the start of the list: put it at the top. */
4760 newFirstVisible = item;
4762 else if (visible_pos >= viscount
4763 /* Sometimes, before we are displayed, GVC is 0, causing us to
4764 * spuriously scroll up. */
4765 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4767 /* item is past the end of the list. */
4768 int scroll = visible_pos - viscount;
4770 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4771 scroll + 1);
4774 if (bHScroll)
4776 /* Scroll window so item's text is visible as much as possible */
4777 /* Calculation of amount of extra space is taken from EditLabel code */
4778 INT pos, x;
4779 TEXTMETRICW textMetric;
4780 HDC hdc = GetWindowDC(infoPtr->hwnd);
4782 x = item->textWidth;
4784 GetTextMetricsW(hdc, &textMetric);
4785 ReleaseDC(infoPtr->hwnd, hdc);
4787 x += (textMetric.tmMaxCharWidth * 2);
4788 x = max(x, textMetric.tmMaxCharWidth * 3);
4790 if (item->textOffset < 0)
4791 pos = item->textOffset;
4792 else if (item->textOffset + x > infoPtr->clientWidth)
4794 if (x > infoPtr->clientWidth)
4795 pos = item->textOffset;
4796 else
4797 pos = item->textOffset + x - infoPtr->clientWidth;
4799 else
4800 pos = 0;
4802 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4805 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4807 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4809 return TRUE;
4812 return FALSE;
4815 static VOID
4816 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4817 TREEVIEW_ITEM *newFirstVisible,
4818 BOOL bUpdateScrollPos)
4820 int gap_size;
4822 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4824 if (newFirstVisible != NULL)
4826 /* Prevent an empty gap from appearing at the bottom... */
4827 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4828 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4830 if (gap_size > 0)
4832 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4833 -gap_size);
4835 /* ... unless we just don't have enough items. */
4836 if (newFirstVisible == NULL)
4837 newFirstVisible = infoPtr->root->firstChild;
4841 if (infoPtr->firstVisible != newFirstVisible)
4843 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4845 infoPtr->firstVisible = newFirstVisible;
4846 TREEVIEW_Invalidate(infoPtr, NULL);
4848 else
4850 TREEVIEW_ITEM *item;
4851 int scroll = infoPtr->uItemHeight *
4852 (infoPtr->firstVisible->visibleOrder
4853 - newFirstVisible->visibleOrder);
4855 infoPtr->firstVisible = newFirstVisible;
4857 for (item = infoPtr->root->firstChild; item != NULL;
4858 item = TREEVIEW_GetNextListItem(infoPtr, item))
4860 item->rect.top += scroll;
4861 item->rect.bottom += scroll;
4864 if (bUpdateScrollPos)
4865 SetScrollPos(infoPtr->hwnd, SB_VERT,
4866 newFirstVisible->visibleOrder, TRUE);
4868 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4873 /************************************************************************
4874 * VScroll is always in units of visible items. i.e. we always have a
4875 * visible item aligned to the top of the control. (Unless we have no
4876 * items at all.)
4878 static LRESULT
4879 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4881 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4882 TREEVIEW_ITEM *newFirstVisible = NULL;
4884 int nScrollCode = LOWORD(wParam);
4886 TRACE("wp %lx\n", wParam);
4888 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4889 return 0;
4891 if (!oldFirstVisible)
4893 assert(infoPtr->root->firstChild == NULL);
4894 return 0;
4897 switch (nScrollCode)
4899 case SB_TOP:
4900 newFirstVisible = infoPtr->root->firstChild;
4901 break;
4903 case SB_BOTTOM:
4904 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4905 break;
4907 case SB_LINEUP:
4908 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4909 break;
4911 case SB_LINEDOWN:
4912 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4913 break;
4915 case SB_PAGEUP:
4916 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4917 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4918 break;
4920 case SB_PAGEDOWN:
4921 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4922 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4923 break;
4925 case SB_THUMBTRACK:
4926 case SB_THUMBPOSITION:
4927 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4928 infoPtr->root->firstChild,
4929 (LONG)(SHORT)HIWORD(wParam));
4930 break;
4932 case SB_ENDSCROLL:
4933 return 0;
4936 if (newFirstVisible != NULL)
4938 if (newFirstVisible != oldFirstVisible)
4939 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4940 nScrollCode != SB_THUMBTRACK);
4941 else if (nScrollCode == SB_THUMBPOSITION)
4942 SetScrollPos(infoPtr->hwnd, SB_VERT,
4943 newFirstVisible->visibleOrder, TRUE);
4946 return 0;
4949 static LRESULT
4950 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4952 int maxWidth;
4953 int scrollX = infoPtr->scrollX;
4954 int nScrollCode = LOWORD(wParam);
4956 TRACE("wp %lx\n", wParam);
4958 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4959 return FALSE;
4961 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4962 /* shall never occur */
4963 if (maxWidth <= 0)
4965 scrollX = 0;
4966 goto scroll;
4969 switch (nScrollCode)
4971 case SB_LINELEFT:
4972 scrollX -= infoPtr->uItemHeight;
4973 break;
4974 case SB_LINERIGHT:
4975 scrollX += infoPtr->uItemHeight;
4976 break;
4977 case SB_PAGELEFT:
4978 scrollX -= infoPtr->clientWidth;
4979 break;
4980 case SB_PAGERIGHT:
4981 scrollX += infoPtr->clientWidth;
4982 break;
4984 case SB_THUMBTRACK:
4985 case SB_THUMBPOSITION:
4986 scrollX = (int)(SHORT)HIWORD(wParam);
4987 break;
4989 case SB_ENDSCROLL:
4990 return 0;
4993 if (scrollX > maxWidth)
4994 scrollX = maxWidth;
4995 else if (scrollX < 0)
4996 scrollX = 0;
4998 scroll:
4999 if (scrollX != infoPtr->scrollX)
5001 TREEVIEW_ITEM *item;
5002 LONG scroll_pixels = infoPtr->scrollX - scrollX;
5004 for (item = infoPtr->root->firstChild; item != NULL;
5005 item = TREEVIEW_GetNextListItem(infoPtr, item))
5007 item->linesOffset += scroll_pixels;
5008 item->stateOffset += scroll_pixels;
5009 item->imageOffset += scroll_pixels;
5010 item->textOffset += scroll_pixels;
5013 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
5014 infoPtr->scrollX = scrollX;
5015 UpdateWindow(infoPtr->hwnd);
5018 if (nScrollCode != SB_THUMBTRACK)
5019 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
5021 return 0;
5024 static LRESULT
5025 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5027 short wheelDelta;
5028 INT pulScrollLines = 3;
5030 if (wParam & (MK_SHIFT | MK_CONTROL))
5031 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
5033 if (infoPtr->firstVisible == NULL)
5034 return TRUE;
5036 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
5038 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5039 /* if scrolling changes direction, ignore left overs */
5040 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
5041 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
5042 infoPtr->wheelRemainder += wheelDelta;
5043 else
5044 infoPtr->wheelRemainder = wheelDelta;
5046 if (infoPtr->wheelRemainder && pulScrollLines)
5048 int newDy;
5049 int maxDy;
5050 int lineScroll;
5052 lineScroll = pulScrollLines * infoPtr->wheelRemainder / WHEEL_DELTA;
5053 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / pulScrollLines;
5055 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
5056 maxDy = infoPtr->maxVisibleOrder;
5058 if (newDy > maxDy)
5059 newDy = maxDy;
5061 if (newDy < 0)
5062 newDy = 0;
5064 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
5066 return TRUE;
5069 /* Create/Destroy *******************************************************/
5071 static LRESULT
5072 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5074 RECT rcClient;
5075 TREEVIEW_INFO *infoPtr;
5076 LOGFONTW lf;
5078 TRACE("wnd %p, style 0x%x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5080 infoPtr = heap_alloc_zero(sizeof(TREEVIEW_INFO));
5082 if (infoPtr == NULL)
5084 ERR("could not allocate info memory!\n");
5085 return 0;
5088 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5090 infoPtr->hwnd = hwnd;
5091 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5092 infoPtr->Timer = 0;
5093 infoPtr->uNumItems = 0;
5094 infoPtr->cdmode = 0;
5095 infoPtr->uScrollTime = 300; /* milliseconds */
5096 infoPtr->bRedraw = TRUE;
5098 GetClientRect(hwnd, &rcClient);
5100 /* No scroll bars yet. */
5101 infoPtr->clientWidth = rcClient.right;
5102 infoPtr->clientHeight = rcClient.bottom;
5103 infoPtr->uInternalStatus = 0;
5105 infoPtr->treeWidth = 0;
5106 infoPtr->treeHeight = 0;
5108 infoPtr->uIndent = MINIMUM_INDENT;
5109 infoPtr->selectedItem = NULL;
5110 infoPtr->focusedItem = NULL;
5111 infoPtr->hotItem = NULL;
5112 infoPtr->editItem = NULL;
5113 infoPtr->firstVisible = NULL;
5114 infoPtr->maxVisibleOrder = 0;
5115 infoPtr->dropItem = NULL;
5116 infoPtr->insertMarkItem = NULL;
5117 infoPtr->insertBeforeorAfter = 0;
5118 /* dragList */
5120 infoPtr->scrollX = 0;
5121 infoPtr->wheelRemainder = 0;
5123 infoPtr->clrBk = CLR_NONE; /* use system color */
5124 infoPtr->clrText = CLR_NONE; /* use system color */
5125 infoPtr->clrLine = CLR_DEFAULT;
5126 infoPtr->clrInsertMark = CLR_DEFAULT;
5128 /* hwndToolTip */
5130 infoPtr->hwndEdit = NULL;
5131 infoPtr->wpEditOrig = NULL;
5132 infoPtr->bIgnoreEditKillFocus = FALSE;
5133 infoPtr->bLabelChanged = FALSE;
5135 infoPtr->himlNormal = NULL;
5136 infoPtr->himlState = NULL;
5137 infoPtr->normalImageWidth = 0;
5138 infoPtr->normalImageHeight = 0;
5139 infoPtr->stateImageWidth = 0;
5140 infoPtr->stateImageHeight = 0;
5142 infoPtr->items = DPA_Create(16);
5144 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5145 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5146 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5147 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5148 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5149 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5151 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5153 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5154 infoPtr->root->state = TVIS_EXPANDED;
5155 infoPtr->root->iLevel = -1;
5156 infoPtr->root->visibleOrder = -1;
5158 infoPtr->hwndNotify = lpcs->hwndParent;
5159 infoPtr->hwndToolTip = 0;
5161 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5162 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5164 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5165 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5166 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5167 hwnd, 0, 0, 0);
5169 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5170 ShowScrollBar(hwnd, SB_VERT, FALSE);
5171 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5173 OpenThemeData (hwnd, themeClass);
5175 return 0;
5179 static LRESULT
5180 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5182 TRACE("\n");
5184 /* free item data */
5185 TREEVIEW_RemoveTree(infoPtr);
5186 /* root isn't freed with other items */
5187 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5188 DPA_Destroy(infoPtr->items);
5190 /* Restore original wndproc */
5191 if (infoPtr->hwndEdit)
5192 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5193 (DWORD_PTR)infoPtr->wpEditOrig);
5195 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5197 /* Deassociate treeview from the window before doing anything drastic. */
5198 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5200 DeleteObject(infoPtr->hDefaultFont);
5201 DeleteObject(infoPtr->hBoldFont);
5202 DeleteObject(infoPtr->hUnderlineFont);
5203 DeleteObject(infoPtr->hBoldUnderlineFont);
5204 DestroyWindow(infoPtr->hwndToolTip);
5205 heap_free(infoPtr);
5207 return 0;
5210 /* Miscellaneous Messages ***********************************************/
5212 static LRESULT
5213 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5215 static const struct
5217 unsigned char code;
5219 scroll[] =
5221 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5222 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5223 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5224 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5225 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5226 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5227 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5228 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5229 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5230 #undef SCROLL_ENTRY
5233 if (key >= VK_PRIOR && key <= VK_DOWN)
5235 unsigned char code = scroll[key - VK_PRIOR].code;
5237 (((code & (1 << 7)) == (SB_HORZ << 7))
5238 ? TREEVIEW_HScroll
5239 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5242 return 0;
5245 /************************************************************************
5246 * TREEVIEW_KeyDown
5248 * VK_UP Move selection to the previous non-hidden item.
5249 * VK_DOWN Move selection to the next non-hidden item.
5250 * VK_HOME Move selection to the first item.
5251 * VK_END Move selection to the last item.
5252 * VK_LEFT If expanded then collapse, otherwise move to parent.
5253 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5254 * VK_ADD Expand.
5255 * VK_SUBTRACT Collapse.
5256 * VK_MULTIPLY Expand all.
5257 * VK_PRIOR Move up GetVisibleCount items.
5258 * VK_NEXT Move down GetVisibleCount items.
5259 * VK_BACK Move to parent.
5260 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5262 static LRESULT
5263 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5265 /* If it is non-NULL and different, it will be selected and visible. */
5266 TREEVIEW_ITEM *newSelection = NULL;
5267 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5268 NMTVKEYDOWN nmkeydown;
5270 TRACE("%lx\n", wParam);
5272 nmkeydown.wVKey = wParam;
5273 nmkeydown.flags = 0;
5274 TREEVIEW_SendRealNotify(infoPtr, TVN_KEYDOWN, &nmkeydown.hdr);
5276 if (prevItem == NULL)
5277 return FALSE;
5279 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5280 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5282 switch (wParam)
5284 case VK_UP:
5285 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5286 if (!newSelection)
5287 newSelection = infoPtr->root->firstChild;
5288 break;
5290 case VK_DOWN:
5291 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5292 break;
5294 case VK_RETURN:
5295 TREEVIEW_SendSimpleNotify(infoPtr, NM_RETURN);
5296 break;
5298 case VK_HOME:
5299 newSelection = infoPtr->root->firstChild;
5300 break;
5302 case VK_END:
5303 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5304 break;
5306 case VK_LEFT:
5307 if (prevItem->state & TVIS_EXPANDED)
5309 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5311 else if (prevItem->parent != infoPtr->root)
5313 newSelection = prevItem->parent;
5315 break;
5317 case VK_RIGHT:
5318 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5320 if (!(prevItem->state & TVIS_EXPANDED))
5321 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5322 else
5324 newSelection = prevItem->firstChild;
5328 break;
5330 case VK_MULTIPLY:
5331 TREEVIEW_ExpandAll(infoPtr, prevItem);
5332 break;
5334 case VK_ADD:
5335 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5336 break;
5338 case VK_SUBTRACT:
5339 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5340 break;
5342 case VK_PRIOR:
5343 newSelection
5344 = TREEVIEW_GetListItem(infoPtr, prevItem,
5345 -TREEVIEW_GetVisibleCount(infoPtr));
5346 break;
5348 case VK_NEXT:
5349 newSelection
5350 = TREEVIEW_GetListItem(infoPtr, prevItem,
5351 TREEVIEW_GetVisibleCount(infoPtr));
5352 break;
5354 case VK_BACK:
5355 newSelection = prevItem->parent;
5356 if (newSelection == infoPtr->root)
5357 newSelection = NULL;
5358 break;
5360 case VK_SPACE:
5361 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5362 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5363 break;
5366 if (newSelection && newSelection != prevItem)
5368 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5369 TVC_BYKEYBOARD))
5371 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5375 return FALSE;
5378 static LRESULT
5379 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5381 /* remove hot effect from item */
5382 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5383 infoPtr->hotItem = NULL;
5385 return 0;
5388 static LRESULT
5389 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5391 TRACKMOUSEEVENT trackinfo;
5392 TREEVIEW_ITEM * item;
5393 TVHITTESTINFO ht;
5394 BOOL item_hit;
5396 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5398 /* fill in the TRACKMOUSEEVENT struct */
5399 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5400 trackinfo.dwFlags = TME_QUERY;
5401 trackinfo.hwndTrack = infoPtr->hwnd;
5403 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5404 _TrackMouseEvent(&trackinfo);
5406 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5407 if(!(trackinfo.dwFlags & TME_LEAVE))
5409 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5410 trackinfo.hwndTrack = infoPtr->hwnd;
5411 /* do it as fast as possible, minimal systimer latency will be used */
5412 trackinfo.dwHoverTime = 1;
5414 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5415 /* and can properly deactivate the hot item */
5416 _TrackMouseEvent(&trackinfo);
5419 ht.pt.x = (short)LOWORD(lParam);
5420 ht.pt.y = (short)HIWORD(lParam);
5422 item = TREEVIEW_HitTest(infoPtr, &ht);
5423 item_hit = TREEVIEW_IsItemHit(infoPtr, &ht);
5424 if ((item != infoPtr->hotItem) || !item_hit)
5426 /* redraw old hot item */
5427 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5428 infoPtr->hotItem = NULL;
5429 if (item && item_hit)
5431 infoPtr->hotItem = item;
5432 /* redraw new hot item */
5433 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5437 return 0;
5440 /* Draw themed border */
5441 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5443 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5444 HDC dc;
5445 RECT r;
5446 HRGN cliprgn;
5447 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5448 cyEdge = GetSystemMetrics (SM_CYEDGE);
5450 if (!theme)
5451 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5453 GetWindowRect(infoPtr->hwnd, &r);
5455 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5456 r.right - cxEdge, r.bottom - cyEdge);
5457 if (region != (HRGN)1)
5458 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5459 OffsetRect(&r, -r.left, -r.top);
5461 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5462 OffsetRect(&r, -r.left, -r.top);
5464 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5465 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5466 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5467 ReleaseDC(infoPtr->hwnd, dc);
5469 /* Call default proc to get the scrollbars etc. painted */
5470 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5472 return TRUE;
5475 static LRESULT
5476 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5478 LPNMHDR lpnmh = (LPNMHDR)lParam;
5480 if (lpnmh->code == PGN_CALCSIZE) {
5481 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5483 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5484 lppgc->iWidth = infoPtr->treeWidth;
5485 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5486 infoPtr->treeWidth, infoPtr->clientWidth);
5488 else {
5489 lppgc->iHeight = infoPtr->treeHeight;
5490 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5491 infoPtr->treeHeight, infoPtr->clientHeight);
5493 return 0;
5495 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5498 static LRESULT
5499 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5501 if (wParam == SIZE_RESTORED)
5503 infoPtr->clientWidth = (short)LOWORD(lParam);
5504 infoPtr->clientHeight = (short)HIWORD(lParam);
5506 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5507 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5508 TREEVIEW_UpdateScrollBars(infoPtr);
5510 else
5512 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5515 TREEVIEW_Invalidate(infoPtr, NULL);
5516 return 0;
5519 static LRESULT
5520 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5522 TRACE("(%lx %lx)\n", wParam, lParam);
5524 if (wParam == GWL_STYLE)
5526 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5528 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5530 if (dwNewStyle & TVS_CHECKBOXES)
5532 TREEVIEW_InitCheckboxes(infoPtr);
5533 TRACE("checkboxes enabled\n");
5535 /* set all items to state image index 1 */
5536 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5538 else
5540 FIXME("tried to disable checkboxes\n");
5544 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5546 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5548 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5549 TRACE("tooltips enabled\n");
5551 else
5553 DestroyWindow(infoPtr->hwndToolTip);
5554 infoPtr->hwndToolTip = 0;
5555 TRACE("tooltips disabled\n");
5559 infoPtr->dwStyle = dwNewStyle;
5562 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
5563 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5564 TREEVIEW_UpdateScrollBars(infoPtr);
5565 TREEVIEW_Invalidate(infoPtr, NULL);
5567 return 0;
5570 static LRESULT
5571 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5573 TREEVIEW_ITEM * item;
5574 TVHITTESTINFO ht;
5575 NMMOUSE nmmouse;
5577 GetCursorPos(&ht.pt);
5578 ScreenToClient(infoPtr->hwnd, &ht.pt);
5580 item = TREEVIEW_HitTest(infoPtr, &ht);
5582 memset(&nmmouse, 0, sizeof(nmmouse));
5583 if (item)
5585 nmmouse.dwItemSpec = (DWORD_PTR)item;
5586 nmmouse.dwItemData = item->lParam;
5588 nmmouse.pt.x = 0;
5589 nmmouse.pt.y = 0;
5590 nmmouse.dwHitInfo = lParam;
5591 if (TREEVIEW_SendRealNotify(infoPtr, NM_SETCURSOR, &nmmouse.hdr))
5592 return 0;
5594 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT) && TREEVIEW_IsItemHit(infoPtr, &ht))
5596 SetCursor(infoPtr->hcurHand);
5597 return 0;
5599 else
5600 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5603 static LRESULT
5604 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5606 TRACE("\n");
5608 if (!infoPtr->selectedItem)
5610 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5611 TVC_UNKNOWN);
5614 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5615 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5616 return 0;
5619 static LRESULT
5620 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5622 TRACE("\n");
5624 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5625 UpdateWindow(infoPtr->hwnd);
5626 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5627 return 0;
5630 /* update theme after a WM_THEMECHANGED message */
5631 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5633 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5634 CloseThemeData (theme);
5635 OpenThemeData (infoPtr->hwnd, themeClass);
5636 return 0;
5640 static LRESULT WINAPI
5641 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5643 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5645 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5647 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5648 else
5650 if (uMsg == WM_CREATE)
5651 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5652 else
5653 goto def;
5656 switch (uMsg)
5658 case TVM_CREATEDRAGIMAGE:
5659 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5661 case TVM_DELETEITEM:
5662 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5664 case TVM_EDITLABELA:
5665 case TVM_EDITLABELW:
5666 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5668 case TVM_ENDEDITLABELNOW:
5669 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5671 case TVM_ENSUREVISIBLE:
5672 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5674 case TVM_EXPAND:
5675 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5677 case TVM_GETBKCOLOR:
5678 return TREEVIEW_GetBkColor(infoPtr);
5680 case TVM_GETCOUNT:
5681 return TREEVIEW_GetCount(infoPtr);
5683 case TVM_GETEDITCONTROL:
5684 return TREEVIEW_GetEditControl(infoPtr);
5686 case TVM_GETIMAGELIST:
5687 return TREEVIEW_GetImageList(infoPtr, wParam);
5689 case TVM_GETINDENT:
5690 return TREEVIEW_GetIndent(infoPtr);
5692 case TVM_GETINSERTMARKCOLOR:
5693 return TREEVIEW_GetInsertMarkColor(infoPtr);
5695 case TVM_GETISEARCHSTRINGA:
5696 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5697 return 0;
5699 case TVM_GETISEARCHSTRINGW:
5700 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5701 return 0;
5703 case TVM_GETITEMA:
5704 case TVM_GETITEMW:
5705 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5706 uMsg == TVM_GETITEMW);
5707 case TVM_GETITEMHEIGHT:
5708 return TREEVIEW_GetItemHeight(infoPtr);
5710 case TVM_GETITEMRECT:
5711 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5713 case TVM_GETITEMSTATE:
5714 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5716 case TVM_GETLINECOLOR:
5717 return TREEVIEW_GetLineColor(infoPtr);
5719 case TVM_GETNEXTITEM:
5720 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5722 case TVM_GETSCROLLTIME:
5723 return TREEVIEW_GetScrollTime(infoPtr);
5725 case TVM_GETTEXTCOLOR:
5726 return TREEVIEW_GetTextColor(infoPtr);
5728 case TVM_GETTOOLTIPS:
5729 return TREEVIEW_GetToolTips(infoPtr);
5731 case TVM_GETUNICODEFORMAT:
5732 return TREEVIEW_GetUnicodeFormat(infoPtr);
5734 case TVM_GETVISIBLECOUNT:
5735 return TREEVIEW_GetVisibleCount(infoPtr);
5737 case TVM_HITTEST:
5738 return (LRESULT)TREEVIEW_HitTest(infoPtr, (TVHITTESTINFO*)lParam);
5740 case TVM_INSERTITEMA:
5741 case TVM_INSERTITEMW:
5742 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5743 uMsg == TVM_INSERTITEMW);
5744 case TVM_SELECTITEM:
5745 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5747 case TVM_SETBKCOLOR:
5748 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5750 case TVM_SETIMAGELIST:
5751 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5753 case TVM_SETINDENT:
5754 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5756 case TVM_SETINSERTMARK:
5757 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5759 case TVM_SETINSERTMARKCOLOR:
5760 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5762 case TVM_SETITEMA:
5763 case TVM_SETITEMW:
5764 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5765 uMsg == TVM_SETITEMW);
5766 case TVM_SETLINECOLOR:
5767 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5769 case TVM_SETITEMHEIGHT:
5770 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5772 case TVM_SETSCROLLTIME:
5773 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5775 case TVM_SETTEXTCOLOR:
5776 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5778 case TVM_SETTOOLTIPS:
5779 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5781 case TVM_SETUNICODEFORMAT:
5782 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5784 case TVM_SORTCHILDREN:
5785 return TREEVIEW_SortChildren(infoPtr, lParam);
5787 case TVM_SORTCHILDRENCB:
5788 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5790 case WM_CHAR:
5791 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5793 case WM_COMMAND:
5794 return TREEVIEW_Command(infoPtr, wParam, lParam);
5796 case WM_DESTROY:
5797 return TREEVIEW_Destroy(infoPtr);
5799 /* WM_ENABLE */
5801 case WM_ERASEBKGND:
5802 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5804 case WM_GETDLGCODE:
5805 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5807 case WM_GETFONT:
5808 return TREEVIEW_GetFont(infoPtr);
5810 case WM_HSCROLL:
5811 return TREEVIEW_HScroll(infoPtr, wParam);
5813 case WM_KEYDOWN:
5814 case WM_SYSKEYDOWN:
5815 return TREEVIEW_KeyDown(infoPtr, wParam);
5817 case WM_KILLFOCUS:
5818 return TREEVIEW_KillFocus(infoPtr);
5820 case WM_LBUTTONDBLCLK:
5821 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5823 case WM_LBUTTONDOWN:
5824 return TREEVIEW_LButtonDown(infoPtr, lParam);
5826 /* WM_MBUTTONDOWN */
5828 case WM_MOUSELEAVE:
5829 return TREEVIEW_MouseLeave(infoPtr);
5831 case WM_MOUSEMOVE:
5832 return TREEVIEW_MouseMove(infoPtr, lParam);
5834 case WM_NCLBUTTONDOWN:
5835 if (infoPtr->hwndEdit)
5836 SetFocus(infoPtr->hwnd);
5837 goto def;
5839 case WM_NCPAINT:
5840 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5842 case WM_NOTIFY:
5843 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5845 case WM_NOTIFYFORMAT:
5846 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5848 case WM_PRINTCLIENT:
5849 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5851 case WM_PAINT:
5852 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5854 case WM_RBUTTONDOWN:
5855 return TREEVIEW_RButtonDown(infoPtr, lParam);
5857 case WM_SETCURSOR:
5858 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5860 case WM_SETFOCUS:
5861 return TREEVIEW_SetFocus(infoPtr);
5863 case WM_SETFONT:
5864 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5866 case WM_SETREDRAW:
5867 return TREEVIEW_SetRedraw(infoPtr, wParam);
5869 case WM_SIZE:
5870 return TREEVIEW_Size(infoPtr, wParam, lParam);
5872 case WM_STYLECHANGED:
5873 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5875 case WM_SYSCOLORCHANGE:
5876 COMCTL32_RefreshSysColors();
5877 return 0;
5879 case WM_TIMER:
5880 return TREEVIEW_HandleTimer(infoPtr, wParam);
5882 case WM_THEMECHANGED:
5883 return TREEVIEW_ThemeChanged (infoPtr);
5885 case WM_VSCROLL:
5886 return TREEVIEW_VScroll(infoPtr, wParam);
5888 /* WM_WININICHANGE */
5890 case WM_MOUSEWHEEL:
5891 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5893 case WM_DRAWITEM:
5894 TRACE("drawItem\n");
5895 goto def;
5897 default:
5898 /* This mostly catches MFC and Delphi messages. :( */
5899 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5900 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5901 def:
5902 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5907 /* Class Registration ***************************************************/
5909 VOID
5910 TREEVIEW_Register(void)
5912 WNDCLASSW wndClass;
5914 TRACE("\n");
5916 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5917 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5918 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5919 wndClass.cbClsExtra = 0;
5920 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5922 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5923 wndClass.hbrBackground = 0;
5924 wndClass.lpszClassName = WC_TREEVIEWW;
5926 RegisterClassW(&wndClass);
5930 VOID
5931 TREEVIEW_Unregister(void)
5933 UnregisterClassW(WC_TREEVIEWW, NULL);
5937 /* Tree Verification ****************************************************/
5939 static inline void
5940 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5942 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5943 const TREEVIEW_ITEM *item)
5945 assert(infoPtr != NULL);
5946 assert(item != NULL);
5948 /* both NULL, or both non-null */
5949 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5951 assert(item->firstChild != item);
5952 assert(item->lastChild != item);
5954 if (item->firstChild)
5956 assert(item->firstChild->parent == item);
5957 assert(item->firstChild->prevSibling == NULL);
5960 if (item->lastChild)
5962 assert(item->lastChild->parent == item);
5963 assert(item->lastChild->nextSibling == NULL);
5966 assert(item->nextSibling != item);
5967 if (item->nextSibling)
5969 assert(item->nextSibling->parent == item->parent);
5970 assert(item->nextSibling->prevSibling == item);
5973 assert(item->prevSibling != item);
5974 if (item->prevSibling)
5976 assert(item->prevSibling->parent == item->parent);
5977 assert(item->prevSibling->nextSibling == item);
5981 static inline void
5982 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5984 assert(item != NULL);
5986 assert(item->parent != NULL);
5987 assert(item->parent != item);
5988 assert(item->iLevel == item->parent->iLevel + 1);
5990 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5992 TREEVIEW_VerifyItemCommon(infoPtr, item);
5994 TREEVIEW_VerifyChildren(infoPtr, item);
5997 static inline void
5998 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
6000 const TREEVIEW_ITEM *child;
6001 assert(item != NULL);
6003 for (child = item->firstChild; child != NULL; child = child->nextSibling)
6004 TREEVIEW_VerifyItem(infoPtr, child);
6007 static inline void
6008 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
6010 TREEVIEW_ITEM *root = infoPtr->root;
6012 assert(root != NULL);
6013 assert(root->iLevel == -1);
6014 assert(root->parent == NULL);
6015 assert(root->prevSibling == NULL);
6017 TREEVIEW_VerifyItemCommon(infoPtr, root);
6019 TREEVIEW_VerifyChildren(infoPtr, root);
6022 static void
6023 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
6025 if (!TRACE_ON(treeview)) return;
6027 assert(infoPtr != NULL);
6028 TREEVIEW_VerifyRoot(infoPtr);