d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / comctl32 / treeview.c
blob40673234a2a445930afef5009856cc8e8c2ecf5c
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, TVN_SINGLEEXPAND
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_EXPANDPARTIAL, TVIS_EX_FLAT,
37 * TVIS_EX_DISABLED
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
43 #include "config.h"
44 #include "wine/port.h"
46 #include <assert.h>
47 #include <ctype.h>
48 #include <stdarg.h>
49 #include <string.h>
50 #include <limits.h>
51 #include <stdlib.h>
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
55 #include "windef.h"
56 #include "winbase.h"
57 #include "wingdi.h"
58 #include "winuser.h"
59 #include "winnls.h"
60 #include "commctrl.h"
61 #include "comctl32.h"
62 #include "uxtheme.h"
63 #include "vssym32.h"
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
69 /* internal structures */
70 typedef struct tagTREEVIEW_INFO
72 HWND hwnd;
73 HWND hwndNotify; /* Owner window to send notifications to */
74 DWORD dwStyle;
75 HTREEITEM root;
76 UINT uInternalStatus;
77 INT Timer;
78 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
79 INT cdmode; /* last custom draw setting */
80 UINT uScrollTime; /* max. time for scrolling in milliseconds */
81 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
83 UINT uItemHeight; /* item height */
84 BOOL bHeightSet;
86 LONG clientWidth; /* width of control window */
87 LONG clientHeight; /* height of control window */
89 LONG treeWidth; /* width of visible tree items */
90 LONG treeHeight; /* height of visible tree items */
92 UINT uIndent; /* indentation in pixels */
93 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
94 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
95 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
96 HTREEITEM editItem; /* item being edited with builtin edit box */
98 HTREEITEM firstVisible; /* handle to first visible item */
99 LONG maxVisibleOrder;
100 HTREEITEM dropItem; /* handle to item selected by drag cursor */
101 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
102 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
103 HIMAGELIST dragList; /* Bitmap of dragged item */
104 LONG scrollX;
105 INT wheelRemainder;
106 COLORREF clrBk;
107 COLORREF clrText;
108 COLORREF clrLine;
109 COLORREF clrInsertMark;
110 HFONT hFont;
111 HFONT hDefaultFont;
112 HFONT hBoldFont;
113 HFONT hUnderlineFont;
114 HFONT hBoldUnderlineFont;
115 HCURSOR hcurHand;
116 HWND hwndToolTip;
118 HWND hwndEdit;
119 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
120 BOOL bIgnoreEditKillFocus;
121 BOOL bLabelChanged;
123 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
124 HIMAGELIST himlNormal;
125 int normalImageHeight;
126 int normalImageWidth;
127 HIMAGELIST himlState;
128 int stateImageHeight;
129 int stateImageWidth;
130 HDPA items;
132 DWORD lastKeyPressTimestamp;
133 WPARAM charCode;
134 INT nSearchParamLength;
135 WCHAR szSearchParam[ MAX_PATH ];
136 } TREEVIEW_INFO;
138 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
140 HTREEITEM parent; /* handle to parent or 0 if at root */
141 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
142 HTREEITEM firstChild; /* handle to first child or 0 if no child */
144 UINT callbackMask;
145 UINT state;
146 UINT stateMask;
147 LPWSTR pszText;
148 int cchTextMax;
149 int iImage;
150 int iSelectedImage;
151 int iExpandedImage;
152 int cChildren;
153 LPARAM lParam;
154 int iIntegral; /* item height multiplier (1 is normal) */
155 int iLevel; /* indentation level:0=root level */
156 HTREEITEM lastChild;
157 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
158 RECT rect;
159 LONG linesOffset;
160 LONG stateOffset;
161 LONG imageOffset;
162 LONG textOffset;
163 LONG textWidth; /* horizontal text extent for pszText */
164 LONG visibleOrder; /* visible ordering, 0 is first visible item */
165 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
166 } TREEVIEW_ITEM;
168 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
169 #define KEY_DELAY 450
171 /* bitflags for infoPtr->uInternalStatus */
173 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
174 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
175 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
176 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
177 #define TV_RDRAG 0x10 /* ditto Rbutton */
178 #define TV_RDRAGGING 0x20
180 /* bitflags for infoPtr->timer */
182 #define TV_EDIT_TIMER 2
183 #define TV_EDIT_TIMER_SET 2
185 #define TEXT_CALLBACK_SIZE 260
187 #define TREEVIEW_LEFT_MARGIN 8
189 #define MINIMUM_INDENT 19
191 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
193 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
194 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
195 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
197 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
198 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
199 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
200 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
202 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
205 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
208 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
210 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
211 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
212 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
213 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
214 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
215 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
217 /* Random Utilities *****************************************************/
218 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
220 /* Returns the treeview private data if hwnd is a treeview.
221 * Otherwise returns an undefined value. */
222 static inline TREEVIEW_INFO *
223 TREEVIEW_GetInfoPtr(HWND hwnd)
225 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
228 /* Don't call this. Nothing wants an item index. */
229 static inline int
230 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
232 return DPA_GetPtrIndex(infoPtr->items, handle);
235 /* Checks if item has changed and needs to be redrawn */
236 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
237 const TVITEMEXW *tvChange)
239 /* Number of children has changed */
240 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
241 return TRUE;
243 /* Image has changed and it's not a callback */
244 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
245 tiNew->iImage != I_IMAGECALLBACK)
246 return TRUE;
248 /* Selected image has changed and it's not a callback */
249 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
250 tiNew->iSelectedImage != I_IMAGECALLBACK)
251 return TRUE;
253 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
254 tiNew->iExpandedImage != I_IMAGECALLBACK)
255 return TRUE;
257 /* Text has changed and it's not a callback */
258 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
259 tiNew->pszText != LPSTR_TEXTCALLBACKW)
260 return TRUE;
262 /* Indent has changed */
263 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
264 return TRUE;
266 /* Item state has changed */
267 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
268 return TRUE;
270 return FALSE;
273 /***************************************************************************
274 * This method checks that handle is an item for this tree.
276 static BOOL
277 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
279 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
281 TRACE("invalid item %p\n", handle);
282 return FALSE;
284 else
285 return TRUE;
288 static HFONT
289 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
291 LOGFONTW font;
293 GetObjectW(hOrigFont, sizeof(font), &font);
294 font.lfWeight = FW_BOLD;
295 return CreateFontIndirectW(&font);
298 static HFONT
299 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
301 LOGFONTW font;
303 GetObjectW(hOrigFont, sizeof(font), &font);
304 font.lfUnderline = TRUE;
305 return CreateFontIndirectW(&font);
308 static HFONT
309 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont)
311 LOGFONTW font;
313 GetObjectW(hfont, sizeof(font), &font);
314 font.lfWeight = FW_BOLD;
315 font.lfUnderline = TRUE;
316 return CreateFontIndirectW(&font);
319 static inline HFONT
320 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
322 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
323 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
324 if (item->state & TVIS_BOLD)
325 return infoPtr->hBoldFont;
326 return infoPtr->hFont;
329 /* for trace/debugging purposes only */
330 static const char *
331 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
333 if (item == NULL) return "<null item>";
334 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
335 if (item->pszText == NULL) return "<null>";
336 return debugstr_w(item->pszText);
339 /* An item is not a child of itself. */
340 static BOOL
341 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
345 child = child->parent;
346 if (child == parent) return TRUE;
347 } while (child != NULL);
349 return FALSE;
353 /* Tree Traversal *******************************************************/
355 /***************************************************************************
356 * This method returns the last expanded sibling or child child item
357 * of a tree node
359 static TREEVIEW_ITEM *
360 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
362 if (!item) return NULL;
364 while (item->lastChild)
366 if (item->state & TVIS_EXPANDED)
367 item = item->lastChild;
368 else
369 break;
372 if (item == infoPtr->root)
373 return NULL;
375 return item;
378 /***************************************************************************
379 * This method returns the previous non-hidden item in the list not
380 * considering the tree hierarchy.
382 static TREEVIEW_ITEM *
383 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
385 if (tvItem->prevSibling)
387 /* This item has a prevSibling, get the last item in the sibling's tree. */
388 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
390 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
391 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
392 else
393 return upItem;
395 else
397 /* this item does not have a prevSibling, get the parent */
398 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
403 /***************************************************************************
404 * This method returns the next physical item in the treeview not
405 * considering the tree hierarchy.
407 static TREEVIEW_ITEM *
408 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
411 * If this item has children and is expanded, return the first child
413 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
415 return tvItem->firstChild;
420 * try to get the sibling
422 if (tvItem->nextSibling)
423 return tvItem->nextSibling;
426 * Otherwise, get the parent's sibling.
428 while (tvItem->parent)
430 tvItem = tvItem->parent;
432 if (tvItem->nextSibling)
433 return tvItem->nextSibling;
436 return NULL;
439 /***************************************************************************
440 * This method returns the nth item starting at the given item. It returns
441 * the last item (or first) we we run out of items.
443 * Will scroll backward if count is <0.
444 * forward if count is >0.
446 static TREEVIEW_ITEM *
447 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
448 LONG count)
450 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
451 TREEVIEW_ITEM *previousItem;
453 assert(item != NULL);
455 if (count > 0)
457 next_item = TREEVIEW_GetNextListItem;
459 else if (count < 0)
461 count = -count;
462 next_item = TREEVIEW_GetPrevListItem;
464 else
465 return item;
469 previousItem = item;
470 item = next_item(infoPtr, item);
472 } while (--count && item != NULL);
475 return item ? item : previousItem;
478 /* Notifications ************************************************************/
480 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
482 if (!infoPtr->bNtfUnicode) {
483 switch (code) {
484 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
485 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
486 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
487 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
488 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
489 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
490 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
491 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
492 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
493 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
494 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
495 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
498 return code;
501 static inline BOOL
502 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, UINT code, NMHDR *hdr)
504 TRACE("code=%d, hdr=%p\n", code, hdr);
506 hdr->hwndFrom = infoPtr->hwnd;
507 hdr->idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
508 hdr->code = get_notifycode(infoPtr, code);
510 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, hdr->idFrom, (LPARAM)hdr);
513 static BOOL
514 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
516 NMHDR hdr;
517 return TREEVIEW_SendRealNotify(infoPtr, code, &hdr);
520 static VOID
521 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
523 tvItem->mask = mask;
524 tvItem->hItem = item;
525 tvItem->state = item->state;
526 tvItem->stateMask = 0;
527 tvItem->iImage = item->iImage;
528 tvItem->iSelectedImage = item->iSelectedImage;
529 tvItem->cChildren = item->cChildren;
530 tvItem->lParam = item->lParam;
532 if(mask & TVIF_TEXT)
534 if (!infoPtr->bNtfUnicode)
536 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
537 tvItem->pszText = Alloc (tvItem->cchTextMax);
538 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
540 else
542 tvItem->cchTextMax = item->cchTextMax;
543 tvItem->pszText = item->pszText;
546 else
548 tvItem->cchTextMax = 0;
549 tvItem->pszText = NULL;
553 static BOOL
554 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
555 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
557 NMTREEVIEWW nmhdr;
558 BOOL ret;
560 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
561 code, action, oldItem, newItem);
563 memset(&nmhdr, 0, sizeof(NMTREEVIEWW));
564 nmhdr.action = action;
566 if (oldItem)
567 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
569 if (newItem)
570 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
572 nmhdr.ptDrag.x = 0;
573 nmhdr.ptDrag.y = 0;
575 ret = TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
576 if (!infoPtr->bNtfUnicode)
578 Free(nmhdr.itemOld.pszText);
579 Free(nmhdr.itemNew.pszText);
581 return ret;
584 static BOOL
585 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
586 HTREEITEM dragItem, POINT pt)
588 NMTREEVIEWW nmhdr;
590 TRACE("code:%d dragitem:%p\n", code, dragItem);
592 nmhdr.action = 0;
593 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
594 nmhdr.itemNew.hItem = dragItem;
595 nmhdr.itemNew.state = dragItem->state;
596 nmhdr.itemNew.lParam = dragItem->lParam;
598 nmhdr.ptDrag.x = pt.x;
599 nmhdr.ptDrag.y = pt.y;
601 return TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
605 static BOOL
606 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
607 HDC hdc, RECT rc)
609 NMTVCUSTOMDRAW nmcdhdr;
610 NMCUSTOMDRAW *nmcd;
612 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
614 nmcd = &nmcdhdr.nmcd;
615 nmcd->dwDrawStage = dwDrawStage;
616 nmcd->hdc = hdc;
617 nmcd->rc = rc;
618 nmcd->dwItemSpec = 0;
619 nmcd->uItemState = 0;
620 nmcd->lItemlParam = 0;
621 nmcdhdr.clrText = infoPtr->clrText;
622 nmcdhdr.clrTextBk = infoPtr->clrBk;
623 nmcdhdr.iLevel = 0;
625 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr.nmcd.hdr);
628 /* FIXME: need to find out when the flags in uItemState need to be set */
630 static BOOL
631 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
632 TREEVIEW_ITEM *item, UINT uItemDrawState,
633 NMTVCUSTOMDRAW *nmcdhdr)
635 NMCUSTOMDRAW *nmcd;
636 DWORD dwDrawStage;
637 DWORD_PTR dwItemSpec;
638 UINT uItemState;
640 dwDrawStage = CDDS_ITEM | uItemDrawState;
641 dwItemSpec = (DWORD_PTR)item;
642 uItemState = 0;
643 if (item->state & TVIS_SELECTED)
644 uItemState |= CDIS_SELECTED;
645 if (item == infoPtr->selectedItem)
646 uItemState |= CDIS_FOCUS;
647 if (item == infoPtr->hotItem)
648 uItemState |= CDIS_HOT;
650 nmcd = &nmcdhdr->nmcd;
651 nmcd->dwDrawStage = dwDrawStage;
652 nmcd->hdc = hdc;
653 nmcd->rc = item->rect;
654 nmcd->dwItemSpec = dwItemSpec;
655 nmcd->uItemState = uItemState;
656 nmcd->lItemlParam = item->lParam;
657 nmcdhdr->iLevel = item->iLevel;
659 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
660 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
661 nmcd->uItemState, nmcd->lItemlParam);
663 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr->nmcd.hdr);
666 static BOOL
667 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
669 NMTVDISPINFOW tvdi;
670 BOOL ret;
672 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
673 &tvdi.item, editItem);
675 ret = TREEVIEW_SendRealNotify(infoPtr, TVN_BEGINLABELEDITW, &tvdi.hdr);
677 if (!infoPtr->bNtfUnicode)
678 Free(tvdi.item.pszText);
680 return ret;
683 static void
684 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
685 UINT mask)
687 NMTVDISPINFOEXW callback;
689 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
690 mask &= item->callbackMask;
692 if (mask == 0) return;
694 /* 'state' always contains valid value, as well as 'lParam'.
695 * All other parameters are uninitialized.
697 callback.item.pszText = item->pszText;
698 callback.item.cchTextMax = item->cchTextMax;
699 callback.item.mask = mask;
700 callback.item.hItem = item;
701 callback.item.state = item->state;
702 callback.item.lParam = item->lParam;
704 /* If text is changed we need to recalculate textWidth */
705 if (mask & TVIF_TEXT)
706 item->textWidth = 0;
708 TREEVIEW_SendRealNotify(infoPtr, TVN_GETDISPINFOW, &callback.hdr);
709 TRACE("resulting code 0x%08x\n", callback.hdr.code);
711 /* It may have changed due to a call to SetItem. */
712 mask &= item->callbackMask;
714 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
716 /* Instead of copying text into our buffer user specified his own */
717 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
718 LPWSTR newText;
719 int buflen;
720 int len = MultiByteToWideChar( CP_ACP, 0,
721 (LPSTR)callback.item.pszText, -1,
722 NULL, 0);
723 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
724 newText = ReAlloc(item->pszText, buflen);
726 TRACE("returned str %s, len=%d, buflen=%d\n",
727 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
729 if (newText)
731 item->pszText = newText;
732 MultiByteToWideChar( CP_ACP, 0,
733 (LPSTR)callback.item.pszText, -1,
734 item->pszText, buflen/sizeof(WCHAR));
735 item->cchTextMax = buflen/sizeof(WCHAR);
737 /* If ReAlloc fails we have nothing to do, but keep original text */
739 else {
740 int len = max(lstrlenW(callback.item.pszText) + 1,
741 TEXT_CALLBACK_SIZE);
742 LPWSTR newText = ReAlloc(item->pszText, len);
744 TRACE("returned wstr %s, len=%d\n",
745 debugstr_w(callback.item.pszText), len);
747 if (newText)
749 item->pszText = newText;
750 strcpyW(item->pszText, callback.item.pszText);
751 item->cchTextMax = len;
753 /* If ReAlloc fails we have nothing to do, but keep original text */
756 else if (mask & TVIF_TEXT) {
757 /* User put text into our buffer, that is ok unless A string */
758 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
759 LPWSTR newText;
760 int buflen;
761 int len = MultiByteToWideChar( CP_ACP, 0,
762 (LPSTR)callback.item.pszText, -1,
763 NULL, 0);
764 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
765 newText = Alloc(buflen);
767 TRACE("same buffer str %s, len=%d, buflen=%d\n",
768 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
770 if (newText)
772 LPWSTR oldText = item->pszText;
773 item->pszText = newText;
774 MultiByteToWideChar( CP_ACP, 0,
775 (LPSTR)callback.item.pszText, -1,
776 item->pszText, buflen/sizeof(WCHAR));
777 item->cchTextMax = buflen/sizeof(WCHAR);
778 Free(oldText);
783 if (mask & TVIF_IMAGE)
784 item->iImage = callback.item.iImage;
786 if (mask & TVIF_SELECTEDIMAGE)
787 item->iSelectedImage = callback.item.iSelectedImage;
789 if (mask & TVIF_EXPANDEDIMAGE)
790 item->iExpandedImage = callback.item.iExpandedImage;
792 if (mask & TVIF_CHILDREN)
793 item->cChildren = callback.item.cChildren;
795 if (callback.item.mask & TVIF_STATE)
797 item->state &= ~callback.item.stateMask;
798 item->state |= (callback.item.state & callback.item.stateMask);
801 /* These members are now permanently set. */
802 if (callback.item.mask & TVIF_DI_SETITEM)
803 item->callbackMask &= ~callback.item.mask;
806 /***************************************************************************
807 * This function uses cChildren field to decide whether the item has
808 * children or not.
809 * Note: if this returns TRUE, the child items may not actually exist,
810 * they could be virtual.
812 * Just use item->firstChild to check for physical children.
814 static BOOL
815 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
817 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
819 return item->cChildren > 0;
822 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
824 INT format;
826 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
828 if (nCommand != NF_REQUERY) return 0;
830 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
831 TRACE("format=%d\n", format);
833 /* Invalid format returned by NF_QUERY defaults to ANSI*/
834 if (format != NFR_ANSI && format != NFR_UNICODE)
835 format = NFR_ANSI;
837 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
839 return format;
842 /* Item Position ********************************************************/
844 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
845 static VOID
846 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
848 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
849 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
850 > TVS_LINESATROOT);
852 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
853 - infoPtr->scrollX;
854 item->stateOffset = item->linesOffset + infoPtr->uIndent;
855 item->imageOffset = item->stateOffset
856 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
857 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
860 static VOID
861 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
863 HDC hdc;
864 HFONT hOldFont=0;
865 SIZE sz;
867 /* DRAW's OM docker creates items like this */
868 if (item->pszText == NULL)
870 item->textWidth = 0;
871 return;
874 if (hDC != 0)
876 hdc = hDC;
878 else
880 hdc = GetDC(infoPtr->hwnd);
881 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
884 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
885 item->textWidth = sz.cx;
887 if (hDC == 0)
889 SelectObject(hdc, hOldFont);
890 ReleaseDC(0, hdc);
894 static VOID
895 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
897 item->rect.top = infoPtr->uItemHeight *
898 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
900 item->rect.bottom = item->rect.top
901 + infoPtr->uItemHeight * item->iIntegral - 1;
903 item->rect.left = 0;
904 item->rect.right = infoPtr->clientWidth;
907 /* We know that only items after start need their order updated. */
908 static void
909 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
911 TREEVIEW_ITEM *item;
912 int order;
914 if (!start)
916 start = infoPtr->root->firstChild;
917 order = 0;
919 else
920 order = start->visibleOrder;
922 for (item = start; item != NULL;
923 item = TREEVIEW_GetNextListItem(infoPtr, item))
925 if (!ISVISIBLE(item) && order > 0)
926 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
927 item->visibleOrder = order;
928 order += item->iIntegral;
931 infoPtr->maxVisibleOrder = order;
933 for (item = start; item != NULL;
934 item = TREEVIEW_GetNextListItem(infoPtr, item))
936 TREEVIEW_ComputeItemRect(infoPtr, item);
941 /* Update metrics of all items in selected subtree.
942 * root must be expanded
944 static VOID
945 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
947 TREEVIEW_ITEM *sibling;
948 HDC hdc;
949 HFONT hOldFont;
951 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
952 return;
954 root->state &= ~TVIS_EXPANDED;
955 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
956 root->state |= TVIS_EXPANDED;
958 hdc = GetDC(infoPtr->hwnd);
959 hOldFont = SelectObject(hdc, infoPtr->hFont);
961 for (; root != sibling;
962 root = TREEVIEW_GetNextListItem(infoPtr, root))
964 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
966 if (root->callbackMask & TVIF_TEXT)
967 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
969 if (root->textWidth == 0)
971 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
972 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
976 SelectObject(hdc, hOldFont);
977 ReleaseDC(infoPtr->hwnd, hdc);
980 /* Item Allocation **********************************************************/
982 static TREEVIEW_ITEM *
983 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
985 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
987 if (!newItem)
988 return NULL;
990 /* I_IMAGENONE would make more sense but this is neither what is
991 * documented (MSDN doesn't specify) nor what Windows actually does
992 * (it sets it to zero)... and I can so imagine an application using
993 * inc/dec to toggle the images. */
994 newItem->iImage = 0;
995 newItem->iSelectedImage = 0;
996 newItem->iExpandedImage = (WORD)I_IMAGENONE;
997 newItem->infoPtr = infoPtr;
999 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1001 Free(newItem);
1002 return NULL;
1005 return newItem;
1008 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1009 * free item->pszText. */
1010 static void
1011 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1013 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1014 if (infoPtr->selectedItem == item)
1015 infoPtr->selectedItem = NULL;
1016 if (infoPtr->hotItem == item)
1017 infoPtr->hotItem = NULL;
1018 if (infoPtr->focusedItem == item)
1019 infoPtr->focusedItem = NULL;
1020 if (infoPtr->firstVisible == item)
1021 infoPtr->firstVisible = NULL;
1022 if (infoPtr->dropItem == item)
1023 infoPtr->dropItem = NULL;
1024 if (infoPtr->insertMarkItem == item)
1025 infoPtr->insertMarkItem = NULL;
1026 Free(item);
1030 /* Item Insertion *******************************************************/
1032 /***************************************************************************
1033 * This method inserts newItem before sibling as a child of parent.
1034 * sibling can be NULL, but only if parent has no children.
1036 static void
1037 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1038 TREEVIEW_ITEM *parent)
1040 assert(parent != NULL);
1042 if (sibling != NULL)
1044 assert(sibling->parent == parent);
1046 if (sibling->prevSibling != NULL)
1047 sibling->prevSibling->nextSibling = newItem;
1049 newItem->prevSibling = sibling->prevSibling;
1050 sibling->prevSibling = newItem;
1052 else
1053 newItem->prevSibling = NULL;
1055 newItem->nextSibling = sibling;
1057 if (parent->firstChild == sibling)
1058 parent->firstChild = newItem;
1060 if (parent->lastChild == NULL)
1061 parent->lastChild = newItem;
1064 /***************************************************************************
1065 * This method inserts newItem after sibling as a child of parent.
1066 * sibling can be NULL, but only if parent has no children.
1068 static void
1069 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1070 TREEVIEW_ITEM *parent)
1072 assert(parent != NULL);
1074 if (sibling != NULL)
1076 assert(sibling->parent == parent);
1078 if (sibling->nextSibling != NULL)
1079 sibling->nextSibling->prevSibling = newItem;
1081 newItem->nextSibling = sibling->nextSibling;
1082 sibling->nextSibling = newItem;
1084 else
1085 newItem->nextSibling = NULL;
1087 newItem->prevSibling = sibling;
1089 if (parent->lastChild == sibling)
1090 parent->lastChild = newItem;
1092 if (parent->firstChild == NULL)
1093 parent->firstChild = newItem;
1096 static BOOL
1097 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1098 const TVITEMEXW *tvItem, BOOL isW)
1100 UINT callbackClear = 0;
1101 UINT callbackSet = 0;
1103 TRACE("item %p\n", item);
1104 /* Do this first in case it fails. */
1105 if (tvItem->mask & TVIF_TEXT)
1107 item->textWidth = 0; /* force width recalculation */
1108 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1110 int len;
1111 LPWSTR newText;
1112 if (isW)
1113 len = lstrlenW(tvItem->pszText) + 1;
1114 else
1115 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1117 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1119 if (newText == NULL) return FALSE;
1121 callbackClear |= TVIF_TEXT;
1123 item->pszText = newText;
1124 item->cchTextMax = len;
1125 if (isW)
1126 lstrcpynW(item->pszText, tvItem->pszText, len);
1127 else
1128 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1129 item->pszText, len);
1131 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1133 else
1135 callbackSet |= TVIF_TEXT;
1137 item->pszText = ReAlloc(item->pszText,
1138 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1139 item->cchTextMax = TEXT_CALLBACK_SIZE;
1140 TRACE("setting callback, item %p\n", item);
1144 if (tvItem->mask & TVIF_CHILDREN)
1146 item->cChildren = tvItem->cChildren;
1148 if (item->cChildren == I_CHILDRENCALLBACK)
1149 callbackSet |= TVIF_CHILDREN;
1150 else
1151 callbackClear |= TVIF_CHILDREN;
1154 if (tvItem->mask & TVIF_IMAGE)
1156 item->iImage = tvItem->iImage;
1158 if (item->iImage == I_IMAGECALLBACK)
1159 callbackSet |= TVIF_IMAGE;
1160 else
1161 callbackClear |= TVIF_IMAGE;
1164 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1166 item->iSelectedImage = tvItem->iSelectedImage;
1168 if (item->iSelectedImage == I_IMAGECALLBACK)
1169 callbackSet |= TVIF_SELECTEDIMAGE;
1170 else
1171 callbackClear |= TVIF_SELECTEDIMAGE;
1174 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1176 item->iExpandedImage = tvItem->iExpandedImage;
1178 if (item->iExpandedImage == I_IMAGECALLBACK)
1179 callbackSet |= TVIF_EXPANDEDIMAGE;
1180 else
1181 callbackClear |= TVIF_EXPANDEDIMAGE;
1184 if (tvItem->mask & TVIF_PARAM)
1185 item->lParam = tvItem->lParam;
1187 /* If the application sets TVIF_INTEGRAL without
1188 * supplying a TVITEMEX structure, it's toast. */
1189 if (tvItem->mask & TVIF_INTEGRAL)
1190 item->iIntegral = tvItem->iIntegral;
1192 if (tvItem->mask & TVIF_STATE)
1194 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1195 tvItem->stateMask);
1196 item->state &= ~tvItem->stateMask;
1197 item->state |= (tvItem->state & tvItem->stateMask);
1200 if (tvItem->mask & TVIF_STATEEX)
1202 FIXME("New extended state: %x\n", tvItem->uStateEx);
1205 item->callbackMask |= callbackSet;
1206 item->callbackMask &= ~callbackClear;
1208 return TRUE;
1211 /* Note that the new item is pre-zeroed. */
1212 static LRESULT
1213 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1215 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1216 HTREEITEM insertAfter;
1217 TREEVIEW_ITEM *newItem, *parentItem;
1218 BOOL bTextUpdated = FALSE;
1220 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1222 parentItem = infoPtr->root;
1224 else
1226 parentItem = ptdi->hParent;
1228 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1230 WARN("invalid parent %p\n", parentItem);
1231 return 0;
1235 insertAfter = ptdi->hInsertAfter;
1237 /* Validate this now for convenience. */
1238 switch ((DWORD_PTR)insertAfter)
1240 case (DWORD_PTR)TVI_FIRST:
1241 case (DWORD_PTR)TVI_LAST:
1242 case (DWORD_PTR)TVI_SORT:
1243 break;
1245 default:
1246 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1247 insertAfter->parent != parentItem)
1249 WARN("invalid insert after %p\n", insertAfter);
1250 insertAfter = TVI_LAST;
1254 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1255 (tvItem->mask & TVIF_TEXT)
1256 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1257 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1258 : "<no label>");
1260 newItem = TREEVIEW_AllocateItem(infoPtr);
1261 if (newItem == NULL)
1262 return 0;
1264 newItem->parent = parentItem;
1265 newItem->iIntegral = 1;
1266 newItem->visibleOrder = -1;
1268 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1269 return 0;
1271 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1273 infoPtr->uNumItems++;
1275 switch ((DWORD_PTR)insertAfter)
1277 case (DWORD_PTR)TVI_FIRST:
1279 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1280 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1281 if (infoPtr->firstVisible == originalFirst)
1282 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1284 break;
1286 case (DWORD_PTR)TVI_LAST:
1287 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1288 break;
1290 /* hInsertAfter names a specific item we want to insert after */
1291 default:
1292 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1293 break;
1295 case (DWORD_PTR)TVI_SORT:
1297 TREEVIEW_ITEM *aChild;
1298 TREEVIEW_ITEM *previousChild = NULL;
1299 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1300 BOOL bItemInserted = FALSE;
1302 aChild = parentItem->firstChild;
1304 bTextUpdated = TRUE;
1305 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1307 /* Iterate the parent children to see where we fit in */
1308 while (aChild != NULL)
1310 INT comp;
1312 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1313 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1315 if (comp < 0) /* we are smaller than the current one */
1317 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1318 if (infoPtr->firstVisible == originalFirst &&
1319 aChild == originalFirst)
1320 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1321 bItemInserted = TRUE;
1322 break;
1324 else if (comp > 0) /* we are bigger than the current one */
1326 previousChild = aChild;
1328 /* This will help us to exit if there is no more sibling */
1329 aChild = (aChild->nextSibling == 0)
1330 ? NULL
1331 : aChild->nextSibling;
1333 /* Look at the next item */
1334 continue;
1336 else if (comp == 0)
1339 * An item with this name is already existing, therefore,
1340 * we add after the one we found
1342 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1343 bItemInserted = TRUE;
1344 break;
1349 * we reach the end of the child list and the item has not
1350 * yet been inserted, therefore, insert it after the last child.
1352 if ((!bItemInserted) && (aChild == NULL))
1353 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1355 break;
1360 TRACE("new item %p; parent %p, mask %x\n", newItem,
1361 newItem->parent, tvItem->mask);
1363 newItem->iLevel = newItem->parent->iLevel + 1;
1365 if (newItem->parent->cChildren == 0)
1366 newItem->parent->cChildren = 1;
1368 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1370 if (STATEIMAGEINDEX(newItem->state) == 0)
1371 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1374 if (infoPtr->firstVisible == NULL)
1375 infoPtr->firstVisible = newItem;
1377 TREEVIEW_VerifyTree(infoPtr);
1379 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1381 if (parentItem == infoPtr->root ||
1382 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1384 TREEVIEW_ITEM *item;
1385 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1387 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1388 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1390 if (!bTextUpdated)
1391 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1393 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1394 TREEVIEW_UpdateScrollBars(infoPtr);
1396 * if the item was inserted in a visible part of the tree,
1397 * invalidate it, as well as those after it
1399 for (item = newItem;
1400 item != NULL;
1401 item = TREEVIEW_GetNextListItem(infoPtr, item))
1402 TREEVIEW_Invalidate(infoPtr, item);
1404 else
1406 /* refresh treeview if newItem is the first item inserted under parentItem */
1407 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1409 /* parent got '+' - update it */
1410 TREEVIEW_Invalidate(infoPtr, parentItem);
1414 return (LRESULT)newItem;
1417 /* Item Deletion ************************************************************/
1418 static void
1419 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1421 static void
1422 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1424 TREEVIEW_ITEM *kill = parentItem->firstChild;
1426 while (kill != NULL)
1428 TREEVIEW_ITEM *next = kill->nextSibling;
1430 TREEVIEW_RemoveItem(infoPtr, kill);
1432 kill = next;
1435 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1436 assert(parentItem->firstChild == NULL);
1437 assert(parentItem->lastChild == NULL);
1440 static void
1441 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1443 TREEVIEW_ITEM *parentItem = item->parent;
1445 assert(item != NULL);
1446 assert(item->parent != NULL); /* i.e. it must not be the root */
1448 if (parentItem->firstChild == item)
1449 parentItem->firstChild = item->nextSibling;
1451 if (parentItem->lastChild == item)
1452 parentItem->lastChild = item->prevSibling;
1454 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1455 && parentItem->cChildren > 0)
1456 parentItem->cChildren = 0;
1458 if (item->prevSibling)
1459 item->prevSibling->nextSibling = item->nextSibling;
1461 if (item->nextSibling)
1462 item->nextSibling->prevSibling = item->prevSibling;
1465 static void
1466 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1468 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1470 if (item->firstChild)
1471 TREEVIEW_RemoveAllChildren(infoPtr, item);
1473 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1474 TVIF_HANDLE | TVIF_PARAM, item, 0);
1476 TREEVIEW_UnlinkItem(item);
1478 infoPtr->uNumItems--;
1480 if (item->pszText != LPSTR_TEXTCALLBACKW)
1481 Free(item->pszText);
1483 TREEVIEW_FreeItem(infoPtr, item);
1487 /* Empty out the tree. */
1488 static void
1489 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1491 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1493 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1496 static LRESULT
1497 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1499 TREEVIEW_ITEM *newSelection = NULL;
1500 TREEVIEW_ITEM *newFirstVisible = NULL;
1501 TREEVIEW_ITEM *parent, *prev = NULL;
1502 BOOL visible = FALSE;
1504 if (item == TVI_ROOT || !item)
1506 TRACE("TVI_ROOT\n");
1507 parent = infoPtr->root;
1508 newSelection = NULL;
1509 visible = TRUE;
1510 TREEVIEW_RemoveTree(infoPtr);
1512 else
1514 if (!TREEVIEW_ValidItem(infoPtr, item))
1515 return FALSE;
1517 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1518 parent = item->parent;
1520 if (ISVISIBLE(item))
1522 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1523 visible = TRUE;
1526 if (infoPtr->selectedItem != NULL
1527 && (item == infoPtr->selectedItem
1528 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1530 if (item->nextSibling)
1531 newSelection = item->nextSibling;
1532 else if (item->parent != infoPtr->root)
1533 newSelection = item->parent;
1534 else
1535 newSelection = item->prevSibling;
1536 TRACE("newSelection = %p\n", newSelection);
1539 if (infoPtr->firstVisible == item)
1541 if (item->nextSibling)
1542 newFirstVisible = item->nextSibling;
1543 else if (item->prevSibling)
1544 newFirstVisible = item->prevSibling;
1545 else if (item->parent != infoPtr->root)
1546 newFirstVisible = item->parent;
1547 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1549 else
1550 newFirstVisible = infoPtr->firstVisible;
1552 TREEVIEW_RemoveItem(infoPtr, item);
1555 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1556 if (!infoPtr->selectedItem && newSelection)
1558 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1559 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1562 /* Validate insertMark dropItem.
1563 * hotItem ??? - used for comparison only.
1565 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1566 infoPtr->insertMarkItem = 0;
1568 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1569 infoPtr->dropItem = 0;
1571 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1572 newFirstVisible = infoPtr->root->firstChild;
1574 TREEVIEW_VerifyTree(infoPtr);
1576 if (!infoPtr->bRedraw) return TRUE;
1578 if (visible)
1580 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1581 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1582 TREEVIEW_UpdateScrollBars(infoPtr);
1583 TREEVIEW_Invalidate(infoPtr, NULL);
1585 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1587 /* parent lost '+/-' - update it */
1588 TREEVIEW_Invalidate(infoPtr, parent);
1591 return TRUE;
1595 /* Get/Set Messages *********************************************************/
1596 static LRESULT
1597 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1599 infoPtr->bRedraw = wParam != 0;
1601 if (infoPtr->bRedraw)
1603 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1604 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1605 TREEVIEW_UpdateScrollBars(infoPtr);
1606 TREEVIEW_Invalidate(infoPtr, NULL);
1608 return 0;
1611 static LRESULT
1612 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1614 TRACE("\n");
1615 return infoPtr->uIndent;
1618 static LRESULT
1619 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1621 TRACE("\n");
1623 if (newIndent < MINIMUM_INDENT)
1624 newIndent = MINIMUM_INDENT;
1626 if (infoPtr->uIndent != newIndent)
1628 infoPtr->uIndent = newIndent;
1629 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1630 TREEVIEW_UpdateScrollBars(infoPtr);
1631 TREEVIEW_Invalidate(infoPtr, NULL);
1634 return 0;
1638 static LRESULT
1639 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1641 TRACE("\n");
1642 return (LRESULT)infoPtr->hwndToolTip;
1645 static LRESULT
1646 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1648 HWND prevToolTip;
1650 TRACE("\n");
1651 prevToolTip = infoPtr->hwndToolTip;
1652 infoPtr->hwndToolTip = hwndTT;
1654 return (LRESULT)prevToolTip;
1657 static LRESULT
1658 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1660 BOOL rc = infoPtr->bNtfUnicode;
1661 infoPtr->bNtfUnicode = fUnicode;
1662 return rc;
1665 static LRESULT
1666 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1668 return infoPtr->bNtfUnicode;
1671 static LRESULT
1672 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1674 return infoPtr->uScrollTime;
1677 static LRESULT
1678 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1680 UINT uOldScrollTime = infoPtr->uScrollTime;
1682 infoPtr->uScrollTime = min(uScrollTime, 100);
1684 return uOldScrollTime;
1688 static LRESULT
1689 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1691 TRACE("\n");
1693 switch (wParam)
1695 case TVSIL_NORMAL:
1696 return (LRESULT)infoPtr->himlNormal;
1698 case TVSIL_STATE:
1699 return (LRESULT)infoPtr->himlState;
1701 default:
1702 return 0;
1706 #define TVHEIGHT_MIN 16
1707 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1709 /* Compute the natural height for items. */
1710 static UINT
1711 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1713 TEXTMETRICW tm;
1714 HDC hdc = GetDC(0);
1715 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1716 UINT height;
1718 /* Height is the maximum of:
1719 * 16 (a hack because our fonts are tiny), and
1720 * The text height + border & margin, and
1721 * The size of the normal image list
1723 GetTextMetricsW(hdc, &tm);
1724 SelectObject(hdc, hOldFont);
1725 ReleaseDC(0, hdc);
1727 height = TVHEIGHT_MIN;
1728 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1729 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1730 if (height < infoPtr->normalImageHeight)
1731 height = infoPtr->normalImageHeight;
1733 /* Round down, unless we support odd ("non even") heights. */
1734 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1735 height &= ~1;
1737 return height;
1740 static LRESULT
1741 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1743 HIMAGELIST himlOld = 0;
1744 int oldWidth = infoPtr->normalImageWidth;
1745 int oldHeight = infoPtr->normalImageHeight;
1747 TRACE("%u,%p\n", type, himlNew);
1749 switch (type)
1751 case TVSIL_NORMAL:
1752 himlOld = infoPtr->himlNormal;
1753 infoPtr->himlNormal = himlNew;
1755 if (himlNew)
1756 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1757 &infoPtr->normalImageHeight);
1758 else
1760 infoPtr->normalImageWidth = 0;
1761 infoPtr->normalImageHeight = 0;
1764 break;
1766 case TVSIL_STATE:
1767 himlOld = infoPtr->himlState;
1768 infoPtr->himlState = himlNew;
1770 if (himlNew)
1771 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1772 &infoPtr->stateImageHeight);
1773 else
1775 infoPtr->stateImageWidth = 0;
1776 infoPtr->stateImageHeight = 0;
1779 break;
1781 default:
1782 ERR("unknown imagelist type %u\n", type);
1785 if (oldWidth != infoPtr->normalImageWidth ||
1786 oldHeight != infoPtr->normalImageHeight)
1788 BOOL bRecalcVisible = FALSE;
1790 if (oldHeight != infoPtr->normalImageHeight &&
1791 !infoPtr->bHeightSet)
1793 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1794 bRecalcVisible = TRUE;
1797 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1798 infoPtr->normalImageWidth != infoPtr->uIndent)
1800 infoPtr->uIndent = infoPtr->normalImageWidth;
1801 bRecalcVisible = TRUE;
1804 if (bRecalcVisible)
1805 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1807 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1808 TREEVIEW_UpdateScrollBars(infoPtr);
1811 TREEVIEW_Invalidate(infoPtr, NULL);
1813 return (LRESULT)himlOld;
1816 static LRESULT
1817 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1819 INT prevHeight = infoPtr->uItemHeight;
1821 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1822 if (newHeight == -1)
1824 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1825 infoPtr->bHeightSet = FALSE;
1827 else
1829 if (newHeight == 0) newHeight = 1;
1830 infoPtr->uItemHeight = newHeight;
1831 infoPtr->bHeightSet = TRUE;
1834 /* Round down, unless we support odd ("non even") heights. */
1835 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1837 infoPtr->uItemHeight &= ~1;
1838 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1841 if (infoPtr->uItemHeight != prevHeight)
1843 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1844 TREEVIEW_UpdateScrollBars(infoPtr);
1845 TREEVIEW_Invalidate(infoPtr, NULL);
1848 return prevHeight;
1851 static LRESULT
1852 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1854 TRACE("\n");
1855 return infoPtr->uItemHeight;
1859 static LRESULT
1860 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1862 TRACE("%p\n", infoPtr->hFont);
1863 return (LRESULT)infoPtr->hFont;
1867 static INT CALLBACK
1868 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1870 (void)unused;
1872 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1874 return 1;
1877 static LRESULT
1878 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1880 UINT uHeight = infoPtr->uItemHeight;
1882 TRACE("%p %i\n", hFont, bRedraw);
1884 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1886 DeleteObject(infoPtr->hBoldFont);
1887 DeleteObject(infoPtr->hUnderlineFont);
1888 DeleteObject(infoPtr->hBoldUnderlineFont);
1889 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1890 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1891 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
1893 if (!infoPtr->bHeightSet)
1894 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1896 if (uHeight != infoPtr->uItemHeight)
1897 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1899 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1901 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1902 TREEVIEW_UpdateScrollBars(infoPtr);
1904 if (bRedraw)
1905 TREEVIEW_Invalidate(infoPtr, NULL);
1907 return 0;
1911 static LRESULT
1912 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1914 TRACE("\n");
1915 return (LRESULT)infoPtr->clrLine;
1918 static LRESULT
1919 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1921 COLORREF prevColor = infoPtr->clrLine;
1923 TRACE("\n");
1924 infoPtr->clrLine = color;
1925 return (LRESULT)prevColor;
1929 static LRESULT
1930 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1932 TRACE("\n");
1933 return (LRESULT)infoPtr->clrText;
1936 static LRESULT
1937 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1939 COLORREF prevColor = infoPtr->clrText;
1941 TRACE("\n");
1942 infoPtr->clrText = color;
1944 if (infoPtr->clrText != prevColor)
1945 TREEVIEW_Invalidate(infoPtr, NULL);
1947 return (LRESULT)prevColor;
1951 static LRESULT
1952 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1954 TRACE("\n");
1955 return (LRESULT)infoPtr->clrBk;
1958 static LRESULT
1959 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1961 COLORREF prevColor = infoPtr->clrBk;
1963 TRACE("\n");
1964 infoPtr->clrBk = newColor;
1966 if (newColor != prevColor)
1967 TREEVIEW_Invalidate(infoPtr, NULL);
1969 return (LRESULT)prevColor;
1973 static LRESULT
1974 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1976 TRACE("\n");
1977 return (LRESULT)infoPtr->clrInsertMark;
1980 static LRESULT
1981 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1983 COLORREF prevColor = infoPtr->clrInsertMark;
1985 TRACE("%x\n", color);
1986 infoPtr->clrInsertMark = color;
1988 return (LRESULT)prevColor;
1992 static LRESULT
1993 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1995 TRACE("%d %p\n", wParam, item);
1997 if (!TREEVIEW_ValidItem(infoPtr, item))
1998 return 0;
2000 infoPtr->insertBeforeorAfter = wParam;
2001 infoPtr->insertMarkItem = item;
2003 TREEVIEW_Invalidate(infoPtr, NULL);
2005 return 1;
2009 /************************************************************************
2010 * Some serious braindamage here. lParam is a pointer to both the
2011 * input HTREEITEM and the output RECT.
2013 static LRESULT
2014 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2016 TREEVIEW_ITEM *item;
2017 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2019 TRACE("\n");
2021 if (pItem == NULL)
2022 return FALSE;
2024 item = *pItem;
2025 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2026 return FALSE;
2029 * If wParam is TRUE return the text size otherwise return
2030 * the whole item size
2032 if (fTextRect)
2034 /* Windows does not send TVN_GETDISPINFO here. */
2036 lpRect->top = item->rect.top;
2037 lpRect->bottom = item->rect.bottom;
2039 lpRect->left = item->textOffset;
2040 if (!item->textWidth)
2041 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2043 lpRect->right = item->textOffset + item->textWidth + 4;
2045 else
2047 *lpRect = item->rect;
2050 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2052 return TRUE;
2055 static inline LRESULT
2056 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2058 /* Surprise! This does not take integral height into account. */
2059 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2060 return infoPtr->clientHeight / infoPtr->uItemHeight;
2064 static LRESULT
2065 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2067 TREEVIEW_ITEM *item = tvItem->hItem;
2069 if (!TREEVIEW_ValidItem(infoPtr, item))
2071 if (!item) return FALSE;
2073 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2074 infoPtr = item->infoPtr;
2075 if (!TREEVIEW_ValidItem(infoPtr, item)) return FALSE;
2078 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2080 if (tvItem->mask & TVIF_CHILDREN)
2082 if (item->cChildren==I_CHILDRENCALLBACK)
2083 FIXME("I_CHILDRENCALLBACK not supported\n");
2084 tvItem->cChildren = item->cChildren;
2087 if (tvItem->mask & TVIF_HANDLE)
2088 tvItem->hItem = item;
2090 if (tvItem->mask & TVIF_IMAGE)
2091 tvItem->iImage = item->iImage;
2093 if (tvItem->mask & TVIF_INTEGRAL)
2094 tvItem->iIntegral = item->iIntegral;
2096 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2097 tvItem->lParam = item->lParam;
2099 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2100 tvItem->iSelectedImage = item->iSelectedImage;
2102 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2103 tvItem->iExpandedImage = item->iExpandedImage;
2105 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2106 tvItem->state = item->state;
2108 if (tvItem->mask & TVIF_TEXT)
2110 if (item->pszText == NULL)
2112 if (tvItem->cchTextMax > 0)
2113 tvItem->pszText[0] = '\0';
2115 else if (isW)
2117 if (item->pszText == LPSTR_TEXTCALLBACKW)
2119 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2120 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2122 else
2124 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2127 else
2129 if (item->pszText == LPSTR_TEXTCALLBACKW)
2131 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2132 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2134 else
2136 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2137 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2142 if (tvItem->mask & TVIF_STATEEX)
2144 FIXME("Extended item state not supported, returning 0.\n");
2145 tvItem->uStateEx = 0;
2148 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2149 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2151 return TRUE;
2154 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2155 * which is wrong. */
2156 static LRESULT
2157 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2159 TREEVIEW_ITEM *item;
2160 TREEVIEW_ITEM originalItem;
2162 item = tvItem->hItem;
2164 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2165 tvItem->mask);
2167 if (!TREEVIEW_ValidItem(infoPtr, item))
2168 return FALSE;
2170 /* store the original item values */
2171 originalItem = *item;
2173 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2174 return FALSE;
2176 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2177 if ((tvItem->mask & TVIF_TEXT
2178 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2179 && ISVISIBLE(item))
2181 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2182 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2185 if (tvItem->mask != 0 && ISVISIBLE(item))
2187 /* The refresh updates everything, but we can't wait until then. */
2188 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2190 /* if any of the item's values changed and it's not a callback, redraw the item */
2191 if (item_changed(&originalItem, item, tvItem))
2193 if (tvItem->mask & TVIF_INTEGRAL)
2195 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2196 TREEVIEW_UpdateScrollBars(infoPtr);
2198 TREEVIEW_Invalidate(infoPtr, NULL);
2200 else
2202 TREEVIEW_UpdateScrollBars(infoPtr);
2203 TREEVIEW_Invalidate(infoPtr, item);
2208 return TRUE;
2211 static LRESULT
2212 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2214 TRACE("\n");
2216 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2217 return 0;
2219 return (item->state & mask);
2222 static LRESULT
2223 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2225 TREEVIEW_ITEM *retval;
2227 retval = 0;
2229 /* handle all the global data here */
2230 switch (which)
2232 case TVGN_CHILD: /* Special case: child of 0 is root */
2233 if (item)
2234 break;
2235 /* fall through */
2236 case TVGN_ROOT:
2237 retval = infoPtr->root->firstChild;
2238 break;
2240 case TVGN_CARET:
2241 retval = infoPtr->selectedItem;
2242 break;
2244 case TVGN_FIRSTVISIBLE:
2245 retval = infoPtr->firstVisible;
2246 break;
2248 case TVGN_DROPHILITE:
2249 retval = infoPtr->dropItem;
2250 break;
2252 case TVGN_LASTVISIBLE:
2253 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2254 break;
2257 if (retval)
2259 TRACE("flags:%x, returns %p\n", which, retval);
2260 return (LRESULT)retval;
2263 if (item == TVI_ROOT) item = infoPtr->root;
2265 if (!TREEVIEW_ValidItem(infoPtr, item))
2266 return FALSE;
2268 switch (which)
2270 case TVGN_NEXT:
2271 retval = item->nextSibling;
2272 break;
2273 case TVGN_PREVIOUS:
2274 retval = item->prevSibling;
2275 break;
2276 case TVGN_PARENT:
2277 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2278 break;
2279 case TVGN_CHILD:
2280 retval = item->firstChild;
2281 break;
2282 case TVGN_NEXTVISIBLE:
2283 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2284 break;
2285 case TVGN_PREVIOUSVISIBLE:
2286 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2287 break;
2288 default:
2289 TRACE("Unknown msg %x,item %p\n", which, item);
2290 break;
2293 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2294 return (LRESULT)retval;
2298 static LRESULT
2299 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2301 TRACE(" %d\n", infoPtr->uNumItems);
2302 return (LRESULT)infoPtr->uNumItems;
2305 static VOID
2306 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2308 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2310 static const unsigned int state_table[] = { 0, 2, 1 };
2312 unsigned int state;
2314 state = STATEIMAGEINDEX(item->state);
2315 TRACE("state:%x\n", state);
2316 item->state &= ~TVIS_STATEIMAGEMASK;
2318 if (state < 3)
2319 state = state_table[state];
2321 item->state |= INDEXTOSTATEIMAGEMASK(state);
2323 TRACE("state:%x\n", state);
2324 TREEVIEW_Invalidate(infoPtr, item);
2329 /* Painting *************************************************************/
2331 /* Draw the lines and expand button for an item. Also draws one section
2332 * of the line from item's parent to item's parent's next sibling. */
2333 static void
2334 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2336 LONG centerx, centery;
2337 BOOL lar = ((infoPtr->dwStyle
2338 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2339 > TVS_LINESATROOT);
2340 HBRUSH hbr, hbrOld;
2341 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2343 if (!lar && item->iLevel == 0)
2344 return;
2346 hbr = CreateSolidBrush(clrBk);
2347 hbrOld = SelectObject(hdc, hbr);
2349 centerx = (item->linesOffset + item->stateOffset) / 2;
2350 centery = (item->rect.top + item->rect.bottom) / 2;
2352 if (infoPtr->dwStyle & TVS_HASLINES)
2354 HPEN hOldPen, hNewPen;
2355 HTREEITEM parent;
2356 LOGBRUSH lb;
2358 /* Get a dotted grey pen */
2359 lb.lbStyle = BS_SOLID;
2360 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2361 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2362 hOldPen = SelectObject(hdc, hNewPen);
2364 /* Make sure the center is on a dot (using +2 instead
2365 * of +1 gives us pixel-by-pixel compat with native) */
2366 centery = (centery + 2) & ~1;
2368 MoveToEx(hdc, item->stateOffset, centery, NULL);
2369 LineTo(hdc, centerx - 1, centery);
2371 if (item->prevSibling || item->parent != infoPtr->root)
2373 MoveToEx(hdc, centerx, item->rect.top, NULL);
2374 LineTo(hdc, centerx, centery);
2377 if (item->nextSibling)
2379 MoveToEx(hdc, centerx, centery, NULL);
2380 LineTo(hdc, centerx, item->rect.bottom + 1);
2383 /* Draw the line from our parent to its next sibling. */
2384 parent = item->parent;
2385 while (parent != infoPtr->root)
2387 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2389 if (parent->nextSibling
2390 /* skip top-levels unless TVS_LINESATROOT */
2391 && parent->stateOffset > parent->linesOffset)
2393 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2394 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2397 parent = parent->parent;
2400 SelectObject(hdc, hOldPen);
2401 DeleteObject(hNewPen);
2405 * Display the (+/-) signs
2408 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2410 if (item->cChildren)
2412 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2413 if (theme)
2415 RECT glyphRect = item->rect;
2416 glyphRect.left = item->linesOffset;
2417 glyphRect.right = item->stateOffset;
2418 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2419 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2420 &glyphRect, NULL);
2422 else
2424 LONG height = item->rect.bottom - item->rect.top;
2425 LONG width = item->stateOffset - item->linesOffset;
2426 LONG rectsize = min(height, width) / 4;
2427 /* plussize = ceil(rectsize * 3/4) */
2428 LONG plussize = (rectsize + 1) * 3 / 4;
2430 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2431 HPEN old_pen = SelectObject(hdc, new_pen);
2433 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2434 centerx + rectsize + 2, centery + rectsize + 2);
2436 SelectObject(hdc, old_pen);
2437 DeleteObject(new_pen);
2439 /* draw +/- signs with current text color */
2440 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2441 old_pen = SelectObject(hdc, new_pen);
2443 if (height < 18 || width < 18)
2445 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2446 LineTo(hdc, centerx + plussize, centery);
2448 if (!(item->state & TVIS_EXPANDED) ||
2449 (item->state & TVIS_EXPANDPARTIAL))
2451 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2452 LineTo(hdc, centerx, centery + plussize);
2455 else
2457 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2458 centerx + plussize, centery + 2);
2460 if (!(item->state & TVIS_EXPANDED) ||
2461 (item->state & TVIS_EXPANDPARTIAL))
2463 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2464 centerx + 2, centery + plussize);
2465 SetPixel(hdc, centerx - 1, centery, clrBk);
2466 SetPixel(hdc, centerx + 1, centery, clrBk);
2470 SelectObject(hdc, old_pen);
2471 DeleteObject(new_pen);
2475 SelectObject(hdc, hbrOld);
2476 DeleteObject(hbr);
2479 static void
2480 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2482 INT cditem;
2483 HFONT hOldFont;
2484 COLORREF oldTextColor, oldTextBkColor;
2485 int centery;
2486 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2487 NMTVCUSTOMDRAW nmcdhdr;
2489 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2491 /* - If item is drop target or it is selected and window is in focus -
2492 * use blue background (COLOR_HIGHLIGHT).
2493 * - If item is selected, window is not in focus, but it has style
2494 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2495 * - Otherwise - use background color
2497 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2498 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2499 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2501 if ((item->state & TVIS_DROPHILITED) || inFocus)
2503 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2504 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2506 else
2508 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2509 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2512 else
2514 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2515 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2516 nmcdhdr.clrText = comctl32_color.clrHighlight;
2517 else
2518 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2521 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2523 /* The custom draw handler can query the text rectangle,
2524 * so get ready. */
2525 /* should already be known, set to 0 when changed */
2526 if (!item->textWidth)
2527 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2529 cditem = 0;
2531 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2533 cditem = TREEVIEW_SendCustomDrawItemNotify
2534 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2535 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2537 if (cditem & CDRF_SKIPDEFAULT)
2539 SelectObject(hdc, hOldFont);
2540 return;
2544 if (cditem & CDRF_NEWFONT)
2545 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2547 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2549 /* Set colors. Custom draw handler can change these so we do this after it. */
2550 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2551 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2553 centery = (item->rect.top + item->rect.bottom) / 2;
2556 * Display the images associated with this item
2559 INT imageIndex;
2561 /* State images are displayed to the left of the Normal image
2562 * image number is in state; zero should be `display no image'.
2564 imageIndex = STATEIMAGEINDEX(item->state);
2566 if (infoPtr->himlState && imageIndex)
2568 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2569 item->stateOffset,
2570 centery - infoPtr->stateImageHeight / 2,
2571 ILD_NORMAL);
2574 /* Now, draw the normal image; can be either selected,
2575 * non-selected or expanded image.
2578 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2580 /* The item is currently selected */
2581 imageIndex = item->iSelectedImage;
2583 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2585 /* The item is currently not selected but expanded */
2586 imageIndex = item->iExpandedImage;
2588 else
2590 /* The item is not selected and not expanded */
2591 imageIndex = item->iImage;
2594 if (infoPtr->himlNormal)
2596 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2598 style |= item->state & TVIS_OVERLAYMASK;
2600 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2601 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2602 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2603 style);
2609 * Display the text associated with this item
2612 /* Don't paint item's text if it's being edited */
2613 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2615 if (item->pszText)
2617 RECT rcText;
2618 UINT align;
2619 SIZE sz;
2621 rcText.top = item->rect.top;
2622 rcText.bottom = item->rect.bottom;
2623 rcText.left = item->textOffset;
2624 rcText.right = rcText.left + item->textWidth + 4;
2626 TRACE("drawing text %s at (%s)\n",
2627 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2629 /* Draw it */
2630 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2632 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2633 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2634 ETO_CLIPPED | ETO_OPAQUE,
2635 &rcText,
2636 item->pszText,
2637 lstrlenW(item->pszText),
2638 NULL);
2639 SetTextAlign(hdc, align);
2641 /* Draw focus box around the selected item */
2642 if ((item == infoPtr->selectedItem) && inFocus)
2644 DrawFocusRect(hdc,&rcText);
2649 /* Draw insertion mark if necessary */
2651 if (infoPtr->insertMarkItem)
2652 TRACE("item:%d,mark:%p\n",
2653 TREEVIEW_GetItemIndex(infoPtr, item),
2654 infoPtr->insertMarkItem);
2656 if (item == infoPtr->insertMarkItem)
2658 HPEN hNewPen, hOldPen;
2659 int offset;
2660 int left, right;
2662 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2663 hOldPen = SelectObject(hdc, hNewPen);
2665 if (infoPtr->insertBeforeorAfter)
2666 offset = item->rect.bottom - 1;
2667 else
2668 offset = item->rect.top + 1;
2670 left = item->textOffset - 2;
2671 right = item->textOffset + item->textWidth + 2;
2673 MoveToEx(hdc, left, offset - 3, NULL);
2674 LineTo(hdc, left, offset + 4);
2676 MoveToEx(hdc, left, offset, NULL);
2677 LineTo(hdc, right + 1, offset);
2679 MoveToEx(hdc, right, offset + 3, NULL);
2680 LineTo(hdc, right, offset - 4);
2682 SelectObject(hdc, hOldPen);
2683 DeleteObject(hNewPen);
2686 /* Restore the hdc state */
2687 SetTextColor(hdc, oldTextColor);
2688 SetBkColor(hdc, oldTextBkColor);
2689 SelectObject(hdc, hOldFont);
2691 if (cditem & CDRF_NOTIFYPOSTPAINT)
2693 cditem = TREEVIEW_SendCustomDrawItemNotify
2694 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2695 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2699 /* Computes treeHeight and treeWidth and updates the scroll bars.
2701 static void
2702 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2704 TREEVIEW_ITEM *item;
2705 HWND hwnd = infoPtr->hwnd;
2706 BOOL vert = FALSE;
2707 BOOL horz = FALSE;
2708 SCROLLINFO si;
2709 LONG scrollX = infoPtr->scrollX;
2711 infoPtr->treeWidth = 0;
2712 infoPtr->treeHeight = 0;
2714 /* We iterate through all visible items in order to get the tree height
2715 * and width */
2716 item = infoPtr->root->firstChild;
2718 while (item != NULL)
2720 if (ISVISIBLE(item))
2722 /* actually we draw text at textOffset + 2 */
2723 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2724 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2726 /* This is scroll-adjusted, but we fix this below. */
2727 infoPtr->treeHeight = item->rect.bottom;
2730 item = TREEVIEW_GetNextListItem(infoPtr, item);
2733 /* Fix the scroll adjusted treeHeight and treeWidth. */
2734 if (infoPtr->root->firstChild)
2735 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2737 infoPtr->treeWidth += infoPtr->scrollX;
2739 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2741 /* Adding one scroll bar may take up enough space that it forces us
2742 * to add the other as well. */
2743 if (infoPtr->treeHeight > infoPtr->clientHeight)
2745 vert = TRUE;
2747 if (infoPtr->treeWidth
2748 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2749 horz = TRUE;
2751 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2752 horz = TRUE;
2754 if (!vert && horz && infoPtr->treeHeight
2755 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2756 vert = TRUE;
2758 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2760 si.cbSize = sizeof(SCROLLINFO);
2761 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2762 si.nMin = 0;
2764 if (vert)
2766 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2767 if ( si.nPage && NULL != infoPtr->firstVisible)
2769 si.nPos = infoPtr->firstVisible->visibleOrder;
2770 si.nMax = infoPtr->maxVisibleOrder - 1;
2772 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2774 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2775 ShowScrollBar(hwnd, SB_VERT, TRUE);
2776 infoPtr->uInternalStatus |= TV_VSCROLL;
2778 else
2780 if (infoPtr->uInternalStatus & TV_VSCROLL)
2781 ShowScrollBar(hwnd, SB_VERT, FALSE);
2782 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2785 else
2787 if (infoPtr->uInternalStatus & TV_VSCROLL)
2788 ShowScrollBar(hwnd, SB_VERT, FALSE);
2789 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2792 if (horz)
2794 si.nPage = infoPtr->clientWidth;
2795 si.nPos = infoPtr->scrollX;
2796 si.nMax = infoPtr->treeWidth - 1;
2798 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2800 si.nPos = si.nMax - max( si.nPage-1, 0 );
2801 scrollX = si.nPos;
2804 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2805 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2806 infoPtr->uInternalStatus |= TV_HSCROLL;
2808 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2809 TREEVIEW_HScroll(infoPtr,
2810 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2812 else
2814 if (infoPtr->uInternalStatus & TV_HSCROLL)
2815 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2816 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2818 scrollX = 0;
2819 if (infoPtr->scrollX != 0)
2821 TREEVIEW_HScroll(infoPtr,
2822 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2826 if (!horz)
2827 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2830 static void
2831 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2833 HBRUSH hBrush;
2834 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2836 hBrush = CreateSolidBrush(clrBk);
2837 FillRect(hdc, rc, hBrush);
2838 DeleteObject(hBrush);
2841 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2842 static LRESULT
2843 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2845 RECT rect;
2847 TRACE("%p\n", infoPtr);
2849 GetClientRect(infoPtr->hwnd, &rect);
2850 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2852 return 1;
2855 static void
2856 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2858 HWND hwnd = infoPtr->hwnd;
2859 RECT rect = *rc;
2860 TREEVIEW_ITEM *item;
2862 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2864 TRACE("empty window\n");
2865 return;
2868 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2869 hdc, rect);
2871 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2873 ReleaseDC(hwnd, hdc);
2874 return;
2877 for (item = infoPtr->root->firstChild;
2878 item != NULL;
2879 item = TREEVIEW_GetNextListItem(infoPtr, item))
2881 if (ISVISIBLE(item))
2883 /* Avoid unneeded calculations */
2884 if (item->rect.top > rect.bottom)
2885 break;
2886 if (item->rect.bottom < rect.top)
2887 continue;
2889 TREEVIEW_DrawItem(infoPtr, hdc, item);
2893 TREEVIEW_UpdateScrollBars(infoPtr);
2895 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2896 infoPtr->cdmode =
2897 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2900 static inline void
2901 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2903 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2906 static void
2907 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2909 if (item)
2910 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2911 else
2912 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2915 static void
2916 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2918 RECT rc;
2919 HBITMAP hbm, hbmOld;
2920 HDC hdc, hdcScreen;
2921 int nIndex;
2923 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2925 hdcScreen = GetDC(0);
2927 hdc = CreateCompatibleDC(hdcScreen);
2928 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2929 hbmOld = SelectObject(hdc, hbm);
2931 SetRect(&rc, 0, 0, 48, 16);
2932 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2934 SetRect(&rc, 18, 2, 30, 14);
2935 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2936 DFCS_BUTTONCHECK|DFCS_FLAT);
2938 SetRect(&rc, 34, 2, 46, 14);
2939 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2940 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2942 SelectObject(hdc, hbmOld);
2943 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2944 comctl32_color.clrWindow);
2945 TRACE("checkbox index %d\n", nIndex);
2947 DeleteObject(hbm);
2948 DeleteDC(hdc);
2949 ReleaseDC(0, hdcScreen);
2951 infoPtr->stateImageWidth = 16;
2952 infoPtr->stateImageHeight = 16;
2955 static void
2956 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2958 TREEVIEW_ITEM *child = item->firstChild;
2960 item->state &= ~TVIS_STATEIMAGEMASK;
2961 item->state |= INDEXTOSTATEIMAGEMASK(1);
2963 while (child)
2965 TREEVIEW_ITEM *next = child->nextSibling;
2966 TREEVIEW_ResetImageStateIndex(infoPtr, child);
2967 child = next;
2971 static LRESULT
2972 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2974 HDC hdc;
2975 PAINTSTRUCT ps;
2976 RECT rc;
2978 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2980 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
2982 TREEVIEW_InitCheckboxes(infoPtr);
2983 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
2985 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
2986 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
2987 TREEVIEW_UpdateScrollBars(infoPtr);
2988 TREEVIEW_Invalidate(infoPtr, NULL);
2991 if (hdc_ref)
2993 hdc = hdc_ref;
2994 GetClientRect(infoPtr->hwnd, &rc);
2995 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2997 else
2999 hdc = BeginPaint(infoPtr->hwnd, &ps);
3000 rc = ps.rcPaint;
3001 if(ps.fErase)
3002 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3005 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3006 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3008 if (!hdc_ref)
3009 EndPaint(infoPtr->hwnd, &ps);
3011 return 0;
3014 static LRESULT
3015 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3017 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3019 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3020 return 0;
3022 if (options & PRF_ERASEBKGND)
3023 TREEVIEW_EraseBackground(infoPtr, hdc);
3025 if (options & PRF_CLIENT)
3027 RECT rc;
3028 GetClientRect(infoPtr->hwnd, &rc);
3029 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3032 return 0;
3035 /* Sorting **************************************************************/
3037 /***************************************************************************
3038 * Forward the DPA local callback to the treeview owner callback
3040 static INT WINAPI
3041 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3042 const TVSORTCB *pCallBackSort)
3044 /* Forward the call to the client-defined callback */
3045 return pCallBackSort->lpfnCompare(first->lParam,
3046 second->lParam,
3047 pCallBackSort->lParam);
3050 /***************************************************************************
3051 * Treeview native sort routine: sort on item text.
3053 static INT WINAPI
3054 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3055 const TREEVIEW_INFO *infoPtr)
3057 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3058 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3060 if(first->pszText && second->pszText)
3061 return lstrcmpiW(first->pszText, second->pszText);
3062 else if(first->pszText)
3063 return -1;
3064 else if(second->pszText)
3065 return 1;
3066 else
3067 return 0;
3070 /* Returns the number of physical children belonging to item. */
3071 static INT
3072 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3074 INT cChildren = 0;
3075 HTREEITEM hti;
3077 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3078 cChildren++;
3080 return cChildren;
3083 /* Returns a DPA containing a pointer to each physical child of item in
3084 * sibling order. If item has no children, an empty DPA is returned. */
3085 static HDPA
3086 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3088 HTREEITEM child;
3090 HDPA list = DPA_Create(8);
3091 if (list == 0) return NULL;
3093 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3095 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3097 DPA_Destroy(list);
3098 return NULL;
3102 return list;
3105 /***************************************************************************
3106 * Setup the treeview structure with regards of the sort method
3107 * and sort the children of the TV item specified in lParam
3108 * fRecurse: currently unused. Should be zero.
3109 * parent: if pSort!=NULL, should equal pSort->hParent.
3110 * otherwise, item which child items are to be sorted.
3111 * pSort: sort method info. if NULL, sort on item text.
3112 * if non-NULL, sort on item's lParam content, and let the
3113 * application decide what that means. See also TVM_SORTCHILDRENCB.
3116 static LRESULT
3117 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3118 LPTVSORTCB pSort)
3120 INT cChildren;
3121 PFNDPACOMPARE pfnCompare;
3122 LPARAM lpCompare;
3124 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3125 if (parent == TVI_ROOT || parent == NULL)
3126 parent = infoPtr->root;
3128 /* Check for a valid handle to the parent item */
3129 if (!TREEVIEW_ValidItem(infoPtr, parent))
3131 ERR("invalid item hParent=%p\n", parent);
3132 return FALSE;
3135 if (pSort)
3137 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3138 lpCompare = (LPARAM)pSort;
3140 else
3142 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3143 lpCompare = (LPARAM)infoPtr;
3146 cChildren = TREEVIEW_CountChildren(parent);
3148 /* Make sure there is something to sort */
3149 if (cChildren > 1)
3151 /* TREEVIEW_ITEM rechaining */
3152 INT count = 0;
3153 HTREEITEM item = 0;
3154 HTREEITEM nextItem = 0;
3155 HTREEITEM prevItem = 0;
3157 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3159 if (sortList == NULL)
3160 return FALSE;
3162 /* let DPA sort the list */
3163 DPA_Sort(sortList, pfnCompare, lpCompare);
3165 /* The order of DPA entries has been changed, so fixup the
3166 * nextSibling and prevSibling pointers. */
3168 item = DPA_GetPtr(sortList, count++);
3169 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3171 /* link the two current item together */
3172 item->nextSibling = nextItem;
3173 nextItem->prevSibling = item;
3175 if (prevItem == NULL)
3177 /* this is the first item, update the parent */
3178 parent->firstChild = item;
3179 item->prevSibling = NULL;
3181 else
3183 /* fix the back chaining */
3184 item->prevSibling = prevItem;
3187 /* get ready for the next one */
3188 prevItem = item;
3189 item = nextItem;
3192 /* the last item is pointed to by item and never has a sibling */
3193 item->nextSibling = NULL;
3194 parent->lastChild = item;
3196 DPA_Destroy(sortList);
3198 TREEVIEW_VerifyTree(infoPtr);
3200 if (parent->state & TVIS_EXPANDED)
3202 int visOrder = infoPtr->firstVisible->visibleOrder;
3204 if (parent == infoPtr->root)
3205 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3206 else
3207 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3209 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3211 TREEVIEW_ITEM *item;
3213 for (item = infoPtr->root->firstChild; item != NULL;
3214 item = TREEVIEW_GetNextListItem(infoPtr, item))
3216 if (item->visibleOrder == visOrder)
3217 break;
3220 if (!item) item = parent->firstChild;
3221 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3224 TREEVIEW_Invalidate(infoPtr, NULL);
3227 return TRUE;
3229 return FALSE;
3233 /***************************************************************************
3234 * Setup the treeview structure with regards of the sort method
3235 * and sort the children of the TV item specified in lParam
3237 static LRESULT
3238 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3240 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3244 /***************************************************************************
3245 * Sort the children of the TV item specified in lParam.
3247 static LRESULT
3248 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3250 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3254 /* Expansion/Collapse ***************************************************/
3256 static BOOL
3257 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3258 UINT action)
3260 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3261 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3262 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3263 0, item);
3266 static VOID
3267 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3268 UINT action)
3270 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3271 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3272 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3273 0, item);
3277 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3278 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3279 static BOOL
3280 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3281 BOOL bRemoveChildren, BOOL bUser)
3283 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3284 BOOL bSetSelection, bSetFirstVisible;
3285 RECT scrollRect;
3286 LONG scrollDist = 0;
3287 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3288 BOOL wasExpanded;
3290 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3292 if (!TREEVIEW_HasChildren(infoPtr, item))
3293 return FALSE;
3295 if (bUser)
3296 TREEVIEW_SendExpanding(infoPtr, item, action);
3298 if (item->firstChild == NULL)
3299 return FALSE;
3301 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3302 item->state &= ~TVIS_EXPANDED;
3304 if (wasExpanded && bUser)
3305 TREEVIEW_SendExpanded(infoPtr, item, action);
3307 bSetSelection = (infoPtr->selectedItem != NULL
3308 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3310 bSetFirstVisible = (infoPtr->firstVisible != NULL
3311 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3313 tmpItem = item;
3314 while (tmpItem)
3316 if (tmpItem->nextSibling)
3318 nextItem = tmpItem->nextSibling;
3319 break;
3321 tmpItem = tmpItem->parent;
3324 if (nextItem)
3325 scrollDist = nextItem->rect.top;
3327 if (bRemoveChildren)
3329 INT old_cChildren = item->cChildren;
3330 TRACE("TVE_COLLAPSERESET\n");
3331 item->state &= ~TVIS_EXPANDEDONCE;
3332 TREEVIEW_RemoveAllChildren(infoPtr, item);
3333 item->cChildren = old_cChildren;
3335 if (!wasExpanded)
3336 return FALSE;
3338 if (item->firstChild)
3340 TREEVIEW_ITEM *i, *sibling;
3342 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3344 for (i = item->firstChild; i != sibling;
3345 i = TREEVIEW_GetNextListItem(infoPtr, i))
3347 i->visibleOrder = -1;
3351 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3353 if (nextItem)
3354 scrollDist = -(scrollDist - nextItem->rect.top);
3356 if (bSetSelection)
3358 /* Don't call DoSelectItem, it sends notifications. */
3359 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3360 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3361 item->state |= TVIS_SELECTED;
3362 infoPtr->selectedItem = item;
3365 TREEVIEW_UpdateScrollBars(infoPtr);
3367 scrollRect.left = 0;
3368 scrollRect.right = infoPtr->clientWidth;
3369 scrollRect.bottom = infoPtr->clientHeight;
3371 if (nextItem)
3373 scrollRect.top = nextItem->rect.top;
3375 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3376 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3377 TREEVIEW_Invalidate(infoPtr, item);
3378 } else {
3379 scrollRect.top = item->rect.top;
3380 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3383 TREEVIEW_SetFirstVisible(infoPtr,
3384 bSetFirstVisible ? item : infoPtr->firstVisible,
3385 TRUE);
3387 return wasExpanded;
3390 static BOOL
3391 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3392 BOOL partial, BOOL user)
3394 LONG scrollDist;
3395 LONG orgNextTop = 0;
3396 RECT scrollRect;
3397 TREEVIEW_ITEM *nextItem, *tmpItem;
3398 BOOL sendsNotifications;
3400 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3402 if (!TREEVIEW_HasChildren(infoPtr, item))
3403 return FALSE;
3405 tmpItem = item; nextItem = NULL;
3406 while (tmpItem)
3408 if (tmpItem->nextSibling)
3410 nextItem = tmpItem->nextSibling;
3411 break;
3413 tmpItem = tmpItem->parent;
3416 if (nextItem)
3417 orgNextTop = nextItem->rect.top;
3419 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3421 sendsNotifications = user || ((item->cChildren != 0) &&
3422 !(item->state & TVIS_EXPANDEDONCE));
3423 if (sendsNotifications)
3425 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3427 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3428 return FALSE;
3431 if (!item->firstChild)
3432 return FALSE;
3434 item->state |= TVIS_EXPANDED;
3436 if (partial)
3437 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3439 if (ISVISIBLE(item))
3441 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3442 TREEVIEW_UpdateSubTree(infoPtr, item);
3443 TREEVIEW_UpdateScrollBars(infoPtr);
3445 scrollRect.left = 0;
3446 scrollRect.bottom = infoPtr->treeHeight;
3447 scrollRect.right = infoPtr->clientWidth;
3448 if (nextItem)
3450 scrollDist = nextItem->rect.top - orgNextTop;
3451 scrollRect.top = orgNextTop;
3453 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3454 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3455 TREEVIEW_Invalidate (infoPtr, item);
3456 } else {
3457 scrollRect.top = item->rect.top;
3458 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3461 /* Scroll up so that as many children as possible are visible.
3462 * This fails when expanding causes an HScroll bar to appear, but we
3463 * don't know that yet, so the last item is obscured. */
3464 if (item->firstChild != NULL)
3466 int nChildren = item->lastChild->visibleOrder
3467 - item->firstChild->visibleOrder + 1;
3469 int visible_pos = item->visibleOrder
3470 - infoPtr->firstVisible->visibleOrder;
3472 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3474 if (visible_pos > 0 && nChildren > rows_below)
3476 int scroll = nChildren - rows_below;
3478 if (scroll > visible_pos)
3479 scroll = visible_pos;
3481 if (scroll > 0)
3483 TREEVIEW_ITEM *newFirstVisible
3484 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3485 scroll);
3488 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3494 if (sendsNotifications) {
3495 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3496 item->state |= TVIS_EXPANDEDONCE;
3499 return TRUE;
3502 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3503 to mouse messages and TVM_SELECTITEM.
3505 selection - previously selected item, used to collapse a part of a tree
3506 item - new selected item
3508 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3509 HTREEITEM selection, HTREEITEM item)
3511 TREEVIEW_ITEM *SelItem;
3513 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3515 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3518 * Close the previous selection all the way to the root
3519 * as long as the new selection is not a child
3521 if(selection && (selection != item))
3523 BOOL closeit = TRUE;
3524 SelItem = item;
3526 /* determine if the hitItem is a child of the currently selected item */
3527 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3528 (SelItem->parent != infoPtr->root))
3530 closeit = (SelItem != selection);
3531 SelItem = SelItem->parent;
3534 if(closeit)
3536 if(TREEVIEW_ValidItem(infoPtr, selection))
3537 SelItem = selection;
3539 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3540 SelItem->parent != infoPtr->root)
3542 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3543 SelItem = SelItem->parent;
3549 * Expand the current item
3551 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3554 static BOOL
3555 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3557 TRACE("item=%p, user=%d\n", item, user);
3559 if (item->state & TVIS_EXPANDED)
3560 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3561 else
3562 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3565 static VOID
3566 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3568 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3570 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3572 if (TREEVIEW_HasChildren(infoPtr, item))
3573 TREEVIEW_ExpandAll(infoPtr, item);
3577 /* Note:If the specified item is the child of a collapsed parent item,
3578 the parent's list of child items is (recursively) expanded to reveal the
3579 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3580 know if it also applies here.
3583 static LRESULT
3584 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3586 if (!TREEVIEW_ValidItem(infoPtr, item))
3587 return 0;
3589 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3590 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3591 flag, item->state);
3593 switch (flag & TVE_TOGGLE)
3595 case TVE_COLLAPSE:
3596 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3597 FALSE);
3599 case TVE_EXPAND:
3600 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3601 FALSE);
3603 case TVE_TOGGLE:
3604 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3606 default:
3607 return 0;
3611 /* Hit-Testing **********************************************************/
3613 static TREEVIEW_ITEM *
3614 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3616 TREEVIEW_ITEM *item;
3617 LONG row;
3619 if (!infoPtr->firstVisible)
3620 return NULL;
3622 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3624 for (item = infoPtr->firstVisible; item != NULL;
3625 item = TREEVIEW_GetNextListItem(infoPtr, item))
3627 if (row >= item->visibleOrder
3628 && row < item->visibleOrder + item->iIntegral)
3629 break;
3632 return item;
3635 static LRESULT
3636 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3638 TREEVIEW_ITEM *item;
3639 RECT rect;
3640 UINT status;
3641 LONG x, y;
3643 lpht->hItem = 0;
3644 GetClientRect(infoPtr->hwnd, &rect);
3645 status = 0;
3646 x = lpht->pt.x;
3647 y = lpht->pt.y;
3649 if (x < rect.left)
3651 status |= TVHT_TOLEFT;
3653 else if (x > rect.right)
3655 status |= TVHT_TORIGHT;
3658 if (y < rect.top)
3660 status |= TVHT_ABOVE;
3662 else if (y > rect.bottom)
3664 status |= TVHT_BELOW;
3667 if (status)
3669 lpht->flags = status;
3670 return 0;
3673 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3674 if (!item)
3676 lpht->flags = TVHT_NOWHERE;
3677 return 0;
3680 if (x >= item->textOffset + item->textWidth)
3682 lpht->flags = TVHT_ONITEMRIGHT;
3684 else if (x >= item->textOffset)
3686 lpht->flags = TVHT_ONITEMLABEL;
3688 else if (x >= item->imageOffset)
3690 lpht->flags = TVHT_ONITEMICON;
3692 else if (x >= item->stateOffset)
3694 lpht->flags = TVHT_ONITEMSTATEICON;
3696 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3698 lpht->flags = TVHT_ONITEMBUTTON;
3700 else
3702 lpht->flags = TVHT_ONITEMINDENT;
3705 lpht->hItem = item;
3706 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3708 return (LRESULT)item;
3711 /* Item Label Editing ***************************************************/
3713 static LRESULT
3714 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3716 return (LRESULT)infoPtr->hwndEdit;
3719 static LRESULT CALLBACK
3720 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3722 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3723 BOOL bCancel = FALSE;
3724 LRESULT rc;
3726 switch (uMsg)
3728 case WM_PAINT:
3729 TRACE("WM_PAINT start\n");
3730 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3731 lParam);
3732 TRACE("WM_PAINT done\n");
3733 return rc;
3735 case WM_KILLFOCUS:
3736 if (infoPtr->bIgnoreEditKillFocus)
3737 return TRUE;
3738 break;
3740 case WM_DESTROY:
3742 WNDPROC editProc = infoPtr->wpEditOrig;
3743 infoPtr->wpEditOrig = 0;
3744 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3745 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3748 case WM_GETDLGCODE:
3749 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3751 case WM_KEYDOWN:
3752 if (wParam == VK_ESCAPE)
3754 bCancel = TRUE;
3755 break;
3757 else if (wParam == VK_RETURN)
3759 break;
3762 /* fall through */
3763 default:
3764 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3767 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3768 /* eg. Using a messagebox */
3770 infoPtr->bIgnoreEditKillFocus = TRUE;
3771 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3772 infoPtr->bIgnoreEditKillFocus = FALSE;
3774 return 0;
3778 /* should handle edit control messages here */
3780 static LRESULT
3781 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3783 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3785 switch (HIWORD(wParam))
3787 case EN_UPDATE:
3790 * Adjust the edit window size
3792 WCHAR buffer[1024];
3793 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3794 HDC hdc = GetDC(infoPtr->hwndEdit);
3795 SIZE sz;
3796 HFONT hFont, hOldFont = 0;
3798 TRACE("edit=%p\n", infoPtr->hwndEdit);
3800 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3802 infoPtr->bLabelChanged = TRUE;
3804 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3806 /* Select font to get the right dimension of the string */
3807 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3809 if (hFont != 0)
3811 hOldFont = SelectObject(hdc, hFont);
3814 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3816 TEXTMETRICW textMetric;
3818 /* Add Extra spacing for the next character */
3819 GetTextMetricsW(hdc, &textMetric);
3820 sz.cx += (textMetric.tmMaxCharWidth * 2);
3822 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3823 sz.cx = min(sz.cx,
3824 infoPtr->clientWidth - editItem->textOffset + 2);
3826 SetWindowPos(infoPtr->hwndEdit,
3827 HWND_TOP,
3830 sz.cx,
3831 editItem->rect.bottom - editItem->rect.top + 3,
3832 SWP_NOMOVE | SWP_DRAWFRAME);
3835 if (hFont != 0)
3837 SelectObject(hdc, hOldFont);
3840 ReleaseDC(infoPtr->hwnd, hdc);
3841 break;
3843 case EN_KILLFOCUS:
3844 /* apparently we should respect passed handle value */
3845 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3847 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3848 break;
3850 default:
3851 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3854 return 0;
3857 static HWND
3858 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3860 HWND hwnd = infoPtr->hwnd;
3861 HWND hwndEdit;
3862 SIZE sz;
3863 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3864 HDC hdc;
3865 HFONT hOldFont=0;
3866 TEXTMETRICW textMetric;
3868 TRACE("%p %p\n", hwnd, hItem);
3869 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3870 return NULL;
3872 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3873 return NULL;
3875 if (infoPtr->hwndEdit)
3876 return infoPtr->hwndEdit;
3878 infoPtr->bLabelChanged = FALSE;
3880 /* make edit item visible */
3881 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3883 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3885 hdc = GetDC(hwnd);
3886 /* Select the font to get appropriate metric dimensions */
3887 if (infoPtr->hFont != 0)
3889 hOldFont = SelectObject(hdc, infoPtr->hFont);
3892 /* Get string length in pixels */
3893 if (hItem->pszText)
3894 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3895 &sz);
3896 else
3897 GetTextExtentPoint32A(hdc, "", 0, &sz);
3899 /* Add Extra spacing for the next character */
3900 GetTextMetricsW(hdc, &textMetric);
3901 sz.cx += (textMetric.tmMaxCharWidth * 2);
3903 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3904 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3906 if (infoPtr->hFont != 0)
3908 SelectObject(hdc, hOldFont);
3911 ReleaseDC(hwnd, hdc);
3913 infoPtr->editItem = hItem;
3915 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3916 WC_EDITW,
3918 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3919 WS_CLIPSIBLINGS | ES_WANTRETURN |
3920 ES_LEFT, hItem->textOffset - 2,
3921 hItem->rect.top - 1, sz.cx + 3,
3922 hItem->rect.bottom -
3923 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3924 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3926 infoPtr->hwndEdit = hwndEdit;
3928 /* Get a 2D border. */
3929 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3930 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3931 SetWindowLongW(hwndEdit, GWL_STYLE,
3932 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3934 SendMessageW(hwndEdit, WM_SETFONT,
3935 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3937 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3938 (DWORD_PTR)
3939 TREEVIEW_Edit_SubclassProc);
3940 if (hItem->pszText)
3941 SetWindowTextW(hwndEdit, hItem->pszText);
3943 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3945 DestroyWindow(hwndEdit);
3946 infoPtr->hwndEdit = 0;
3947 infoPtr->editItem = NULL;
3948 return NULL;
3951 SetFocus(hwndEdit);
3952 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3953 ShowWindow(hwndEdit, SW_SHOW);
3955 return hwndEdit;
3959 static LRESULT
3960 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3962 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3963 NMTVDISPINFOW tvdi;
3964 BOOL bCommit;
3965 WCHAR tmpText[1024] = { '\0' };
3966 WCHAR *newText = tmpText;
3967 int iLength = 0;
3969 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3971 tvdi.item.mask = 0;
3972 tvdi.item.hItem = editedItem;
3973 tvdi.item.state = editedItem->state;
3974 tvdi.item.lParam = editedItem->lParam;
3976 if (!bCancel)
3978 if (!infoPtr->bNtfUnicode)
3979 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3980 else
3981 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3983 if (iLength >= 1023)
3985 ERR("Insufficient space to retrieve new item label\n");
3988 tvdi.item.mask = TVIF_TEXT;
3989 tvdi.item.pszText = tmpText;
3990 tvdi.item.cchTextMax = iLength + 1;
3992 else
3994 tvdi.item.pszText = NULL;
3995 tvdi.item.cchTextMax = 0;
3998 bCommit = TREEVIEW_SendRealNotify(infoPtr, TVN_ENDLABELEDITW, &tvdi.hdr);
4000 if (!bCancel && bCommit) /* Apply the changes */
4002 if (!infoPtr->bNtfUnicode)
4004 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
4005 newText = Alloc(len * sizeof(WCHAR));
4006 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
4007 iLength = len - 1;
4010 if (strcmpW(newText, editedItem->pszText) != 0)
4012 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
4013 if (ptr == NULL)
4015 ERR("OutOfMemory, cannot allocate space for label\n");
4016 if(newText != tmpText) Free(newText);
4017 DestroyWindow(infoPtr->hwndEdit);
4018 infoPtr->hwndEdit = 0;
4019 infoPtr->editItem = NULL;
4020 return FALSE;
4022 else
4024 editedItem->pszText = ptr;
4025 editedItem->cchTextMax = iLength + 1;
4026 strcpyW(editedItem->pszText, newText);
4027 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4030 if(newText != tmpText) Free(newText);
4033 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4034 DestroyWindow(infoPtr->hwndEdit);
4035 infoPtr->hwndEdit = 0;
4036 infoPtr->editItem = NULL;
4037 return TRUE;
4040 static LRESULT
4041 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4043 if (wParam != TV_EDIT_TIMER)
4045 ERR("got unknown timer\n");
4046 return 1;
4049 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4050 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4052 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4054 return 0;
4058 /* Mouse Tracking/Drag **************************************************/
4060 /***************************************************************************
4061 * This is quite unusual piece of code, but that's how it's implemented in
4062 * Windows.
4064 static LRESULT
4065 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4067 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4068 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4069 RECT r;
4070 MSG msg;
4072 r.top = pt.y - cyDrag;
4073 r.left = pt.x - cxDrag;
4074 r.bottom = pt.y + cyDrag;
4075 r.right = pt.x + cxDrag;
4077 SetCapture(infoPtr->hwnd);
4079 while (1)
4081 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4083 if (msg.message == WM_MOUSEMOVE)
4085 pt.x = (short)LOWORD(msg.lParam);
4086 pt.y = (short)HIWORD(msg.lParam);
4087 if (PtInRect(&r, pt))
4088 continue;
4089 else
4091 ReleaseCapture();
4092 return 1;
4095 else if (msg.message >= WM_LBUTTONDOWN &&
4096 msg.message <= WM_RBUTTONDBLCLK)
4098 break;
4101 DispatchMessageW(&msg);
4104 if (GetCapture() != infoPtr->hwnd)
4105 return 0;
4108 ReleaseCapture();
4109 return 0;
4113 static LRESULT
4114 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4116 TREEVIEW_ITEM *item;
4117 TVHITTESTINFO hit;
4119 TRACE("\n");
4120 SetFocus(infoPtr->hwnd);
4122 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4124 /* If there is pending 'edit label' event - kill it now */
4125 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4128 hit.pt.x = (short)LOWORD(lParam);
4129 hit.pt.y = (short)HIWORD(lParam);
4131 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4132 if (!item)
4133 return 0;
4134 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4136 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4137 { /* FIXME! */
4138 switch (hit.flags)
4140 case TVHT_ONITEMRIGHT:
4141 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4142 break;
4144 case TVHT_ONITEMINDENT:
4145 if (!(infoPtr->dwStyle & TVS_HASLINES))
4147 break;
4149 else
4151 int level = hit.pt.x / infoPtr->uIndent;
4152 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4154 while (item->iLevel > level)
4156 item = item->parent;
4159 /* fall through */
4162 case TVHT_ONITEMLABEL:
4163 case TVHT_ONITEMICON:
4164 case TVHT_ONITEMBUTTON:
4165 TREEVIEW_Toggle(infoPtr, item, TRUE);
4166 break;
4168 case TVHT_ONITEMSTATEICON:
4169 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4170 TREEVIEW_ToggleItemState(infoPtr, item);
4171 else
4172 TREEVIEW_Toggle(infoPtr, item, TRUE);
4173 break;
4176 return TRUE;
4180 static LRESULT
4181 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4183 HWND hwnd = infoPtr->hwnd;
4184 TVHITTESTINFO ht;
4185 BOOL bTrack, bDoLabelEdit;
4187 /* If Edit control is active - kill it and return.
4188 * The best way to do it is to set focus to itself.
4189 * Edit control subclassed procedure will automatically call
4190 * EndEditLabelNow.
4192 if (infoPtr->hwndEdit)
4194 SetFocus(hwnd);
4195 return 0;
4198 ht.pt.x = (short)LOWORD(lParam);
4199 ht.pt.y = (short)HIWORD(lParam);
4201 TREEVIEW_HitTest(infoPtr, &ht);
4202 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4204 /* update focusedItem and redraw both items */
4205 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4207 infoPtr->focusedItem = ht.hItem;
4208 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4209 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4212 bTrack = (ht.flags & TVHT_ONITEM)
4213 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4216 * If the style allows editing and the node is already selected
4217 * and the click occurred on the item label...
4219 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4220 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4222 /* Send NM_CLICK right away */
4223 if (!bTrack)
4224 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4225 goto setfocus;
4227 if (ht.flags & TVHT_ONITEMBUTTON)
4229 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4230 goto setfocus;
4232 else if (bTrack)
4233 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4234 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4236 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4237 infoPtr->dropItem = ht.hItem;
4239 /* clean up focusedItem as we dragged and won't select this item */
4240 if(infoPtr->focusedItem)
4242 /* refresh the item that was focused */
4243 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4244 infoPtr->focusedItem = NULL;
4246 /* refresh the selected item to return the filled background */
4247 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4250 return 0;
4254 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4255 goto setfocus;
4257 if (bDoLabelEdit)
4259 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4260 KillTimer(hwnd, TV_EDIT_TIMER);
4262 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4263 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4265 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4267 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4269 /* Select the current item */
4270 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4271 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4273 else if (ht.flags & TVHT_ONITEMSTATEICON)
4275 /* TVS_CHECKBOXES requires us to toggle the current state */
4276 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4277 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4280 setfocus:
4281 SetFocus(hwnd);
4282 return 0;
4286 static LRESULT
4287 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4289 TVHITTESTINFO ht;
4291 if (infoPtr->hwndEdit)
4293 SetFocus(infoPtr->hwnd);
4294 return 0;
4297 ht.pt.x = (short)LOWORD(lParam);
4298 ht.pt.y = (short)HIWORD(lParam);
4300 TREEVIEW_HitTest(infoPtr, &ht);
4302 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4304 if (ht.hItem)
4306 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4307 infoPtr->dropItem = ht.hItem;
4310 else
4312 SetFocus(infoPtr->hwnd);
4313 if(!TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK))
4315 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4316 SendMessageW(infoPtr->hwndNotify, WM_CONTEXTMENU,
4317 (WPARAM)infoPtr->hwnd, (LPARAM)GetMessagePos());
4321 return 0;
4324 static LRESULT
4325 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4327 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4328 INT cx, cy;
4329 HDC hdc, htopdc;
4330 HWND hwtop;
4331 HBITMAP hbmp, hOldbmp;
4332 SIZE size;
4333 RECT rc;
4334 HFONT hOldFont;
4336 TRACE("\n");
4338 if (!(infoPtr->himlNormal))
4339 return 0;
4341 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4342 return 0;
4344 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4346 hwtop = GetDesktopWindow();
4347 htopdc = GetDC(hwtop);
4348 hdc = CreateCompatibleDC(htopdc);
4350 hOldFont = SelectObject(hdc, infoPtr->hFont);
4352 if (dragItem->pszText)
4353 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4354 &size);
4355 else
4356 GetTextExtentPoint32A(hdc, "", 0, &size);
4358 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4359 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4360 hOldbmp = SelectObject(hdc, hbmp);
4362 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4363 size.cx += cx;
4364 if (cy > size.cy)
4365 size.cy = cy;
4367 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4368 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4369 ILD_NORMAL);
4372 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4373 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4376 /* draw item text */
4378 SetRect(&rc, cx, 0, size.cx, size.cy);
4380 if (dragItem->pszText)
4381 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4382 DT_LEFT);
4384 SelectObject(hdc, hOldFont);
4385 SelectObject(hdc, hOldbmp);
4387 ImageList_Add(infoPtr->dragList, hbmp, 0);
4389 DeleteDC(hdc);
4390 DeleteObject(hbmp);
4391 ReleaseDC(hwtop, htopdc);
4393 return (LRESULT)infoPtr->dragList;
4396 /* Selection ************************************************************/
4398 static LRESULT
4399 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4400 INT cause)
4402 TREEVIEW_ITEM *prevSelect;
4404 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4406 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4407 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4408 newSelect ? newSelect->state : 0);
4410 /* reset and redraw focusedItem if focusedItem was set so we don't */
4411 /* have to worry about the previously focused item when we set a new one */
4412 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4413 infoPtr->focusedItem = NULL;
4415 switch (action)
4417 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4418 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4419 /* Fall through */
4420 case TVGN_CARET:
4421 prevSelect = infoPtr->selectedItem;
4423 if (prevSelect == newSelect) {
4424 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4425 break;
4428 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4429 TVN_SELCHANGINGW,
4430 cause,
4431 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4432 prevSelect,
4433 newSelect))
4434 return FALSE;
4436 if (prevSelect)
4437 prevSelect->state &= ~TVIS_SELECTED;
4438 if (newSelect)
4439 newSelect->state |= TVIS_SELECTED;
4441 infoPtr->selectedItem = newSelect;
4443 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4445 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4446 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4448 TREEVIEW_SendTreeviewNotify(infoPtr,
4449 TVN_SELCHANGEDW,
4450 cause,
4451 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4452 prevSelect,
4453 newSelect);
4454 break;
4456 case TVGN_DROPHILITE:
4457 prevSelect = infoPtr->dropItem;
4459 if (prevSelect)
4460 prevSelect->state &= ~TVIS_DROPHILITED;
4462 infoPtr->dropItem = newSelect;
4464 if (newSelect)
4465 newSelect->state |= TVIS_DROPHILITED;
4467 TREEVIEW_Invalidate(infoPtr, prevSelect);
4468 TREEVIEW_Invalidate(infoPtr, newSelect);
4469 break;
4471 case TVGN_FIRSTVISIBLE:
4472 if (newSelect != NULL)
4474 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4475 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4476 TREEVIEW_Invalidate(infoPtr, NULL);
4478 break;
4481 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4482 return TRUE;
4485 /* FIXME: handle NM_KILLFOCUS etc */
4486 static LRESULT
4487 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4489 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4491 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4492 return FALSE;
4494 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4496 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4497 return FALSE;
4499 TREEVIEW_SingleExpand(infoPtr, selection, item);
4501 return TRUE;
4504 /*************************************************************************
4505 * TREEVIEW_ProcessLetterKeys
4507 * Processes keyboard messages generated by pressing the letter keys
4508 * on the keyboard.
4509 * What this does is perform a case insensitive search from the
4510 * current position with the following quirks:
4511 * - If two chars or more are pressed in quick succession we search
4512 * for the corresponding string (e.g. 'abc').
4513 * - If there is a delay we wipe away the current search string and
4514 * restart with just that char.
4515 * - If the user keeps pressing the same character, whether slowly or
4516 * fast, so that the search string is entirely composed of this
4517 * character ('aaaaa' for instance), then we search for first item
4518 * that starting with that character.
4519 * - If the user types the above character in quick succession, then
4520 * we must also search for the corresponding string ('aaaaa'), and
4521 * go to that string if there is a match.
4523 * RETURNS
4525 * Zero.
4527 * BUGS
4529 * - The current implementation has a list of characters it will
4530 * accept and it ignores everything else. In particular it will
4531 * ignore accentuated characters which seems to match what
4532 * Windows does. But I'm not sure it makes sense to follow
4533 * Windows there.
4534 * - We don't sound a beep when the search fails.
4535 * - The search should start from the focused item, not from the selected
4536 * item. One reason for this is to allow for multiple selections in trees.
4537 * But currently infoPtr->focusedItem does not seem very usable.
4539 * SEE ALSO
4541 * TREEVIEW_ProcessLetterKeys
4543 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4545 HTREEITEM nItem;
4546 HTREEITEM endidx,idx;
4547 TVITEMEXW item;
4548 WCHAR buffer[MAX_PATH];
4549 DWORD timestamp,elapsed;
4551 /* simple parameter checking */
4552 if (!charCode || !keyData) return 0;
4554 /* only allow the valid WM_CHARs through */
4555 if (!isalnum(charCode) &&
4556 charCode != '.' && charCode != '`' && charCode != '!' &&
4557 charCode != '@' && charCode != '#' && charCode != '$' &&
4558 charCode != '%' && charCode != '^' && charCode != '&' &&
4559 charCode != '*' && charCode != '(' && charCode != ')' &&
4560 charCode != '-' && charCode != '_' && charCode != '+' &&
4561 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4562 charCode != '}' && charCode != '[' && charCode != '{' &&
4563 charCode != '/' && charCode != '?' && charCode != '>' &&
4564 charCode != '<' && charCode != ',' && charCode != '~')
4565 return 0;
4567 /* compute how much time elapsed since last keypress */
4568 timestamp = GetTickCount();
4569 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4570 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4571 } else {
4572 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4575 /* update the search parameters */
4576 infoPtr->lastKeyPressTimestamp=timestamp;
4577 if (elapsed < KEY_DELAY) {
4578 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4579 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4581 if (infoPtr->charCode != charCode) {
4582 infoPtr->charCode=charCode=0;
4584 } else {
4585 infoPtr->charCode=charCode;
4586 infoPtr->szSearchParam[0]=charCode;
4587 infoPtr->nSearchParamLength=1;
4588 /* Redundant with the 1 char string */
4589 charCode=0;
4592 /* and search from the current position */
4593 nItem=NULL;
4594 if (infoPtr->selectedItem != NULL) {
4595 endidx=infoPtr->selectedItem;
4596 /* if looking for single character match,
4597 * then we must always move forward
4599 if (infoPtr->nSearchParamLength == 1)
4600 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4601 else
4602 idx=endidx;
4603 } else {
4604 endidx=NULL;
4605 idx=infoPtr->root->firstChild;
4607 do {
4608 /* At the end point, sort out wrapping */
4609 if (idx == NULL) {
4611 /* If endidx is null, stop at the last item (ie top to bottom) */
4612 if (endidx == NULL)
4613 break;
4615 /* Otherwise, start again at the very beginning */
4616 idx=infoPtr->root->firstChild;
4618 /* But if we are stopping on the first child, end now! */
4619 if (idx == endidx) break;
4622 /* get item */
4623 ZeroMemory(&item, sizeof(item));
4624 item.mask = TVIF_TEXT;
4625 item.hItem = idx;
4626 item.pszText = buffer;
4627 item.cchTextMax = sizeof(buffer);
4628 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4630 /* check for a match */
4631 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4632 nItem=idx;
4633 break;
4634 } else if ( (charCode != 0) && (nItem == NULL) &&
4635 (nItem != infoPtr->selectedItem) &&
4636 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4637 /* This would work but we must keep looking for a longer match */
4638 nItem=idx;
4640 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4641 } while (idx != endidx);
4643 if (nItem != NULL) {
4644 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4645 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4649 return 0;
4652 /* Scrolling ************************************************************/
4654 static LRESULT
4655 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4657 int viscount;
4658 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4659 HTREEITEM newFirstVisible = NULL;
4660 int visible_pos = -1;
4662 if (!TREEVIEW_ValidItem(infoPtr, item))
4663 return FALSE;
4665 if (!ISVISIBLE(item))
4667 /* Expand parents as necessary. */
4668 HTREEITEM parent;
4670 /* see if we are trying to ensure that root is visible */
4671 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4672 parent = item->parent;
4673 else
4674 parent = item; /* this item is the topmost item */
4676 while (parent != infoPtr->root)
4678 if (!(parent->state & TVIS_EXPANDED))
4679 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4681 parent = parent->parent;
4685 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4687 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4688 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4690 if (hasFirstVisible)
4691 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4693 if (visible_pos < 0)
4695 /* item is before the start of the list: put it at the top. */
4696 newFirstVisible = item;
4698 else if (visible_pos >= viscount
4699 /* Sometimes, before we are displayed, GVC is 0, causing us to
4700 * spuriously scroll up. */
4701 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4703 /* item is past the end of the list. */
4704 int scroll = visible_pos - viscount;
4706 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4707 scroll + 1);
4710 if (bHScroll)
4712 /* Scroll window so item's text is visible as much as possible */
4713 /* Calculation of amount of extra space is taken from EditLabel code */
4714 INT pos, x;
4715 TEXTMETRICW textMetric;
4716 HDC hdc = GetWindowDC(infoPtr->hwnd);
4718 x = item->textWidth;
4720 GetTextMetricsW(hdc, &textMetric);
4721 ReleaseDC(infoPtr->hwnd, hdc);
4723 x += (textMetric.tmMaxCharWidth * 2);
4724 x = max(x, textMetric.tmMaxCharWidth * 3);
4726 if (item->textOffset < 0)
4727 pos = item->textOffset;
4728 else if (item->textOffset + x > infoPtr->clientWidth)
4730 if (x > infoPtr->clientWidth)
4731 pos = item->textOffset;
4732 else
4733 pos = item->textOffset + x - infoPtr->clientWidth;
4735 else
4736 pos = 0;
4738 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4741 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4743 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4745 return TRUE;
4748 return FALSE;
4751 static VOID
4752 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4753 TREEVIEW_ITEM *newFirstVisible,
4754 BOOL bUpdateScrollPos)
4756 int gap_size;
4758 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4760 if (newFirstVisible != NULL)
4762 /* Prevent an empty gap from appearing at the bottom... */
4763 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4764 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4766 if (gap_size > 0)
4768 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4769 -gap_size);
4771 /* ... unless we just don't have enough items. */
4772 if (newFirstVisible == NULL)
4773 newFirstVisible = infoPtr->root->firstChild;
4777 if (infoPtr->firstVisible != newFirstVisible)
4779 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4781 infoPtr->firstVisible = newFirstVisible;
4782 TREEVIEW_Invalidate(infoPtr, NULL);
4784 else
4786 TREEVIEW_ITEM *item;
4787 int scroll = infoPtr->uItemHeight *
4788 (infoPtr->firstVisible->visibleOrder
4789 - newFirstVisible->visibleOrder);
4791 infoPtr->firstVisible = newFirstVisible;
4793 for (item = infoPtr->root->firstChild; item != NULL;
4794 item = TREEVIEW_GetNextListItem(infoPtr, item))
4796 item->rect.top += scroll;
4797 item->rect.bottom += scroll;
4800 if (bUpdateScrollPos)
4801 SetScrollPos(infoPtr->hwnd, SB_VERT,
4802 newFirstVisible->visibleOrder, TRUE);
4804 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4809 /************************************************************************
4810 * VScroll is always in units of visible items. i.e. we always have a
4811 * visible item aligned to the top of the control. (Unless we have no
4812 * items at all.)
4814 static LRESULT
4815 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4817 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4818 TREEVIEW_ITEM *newFirstVisible = NULL;
4820 int nScrollCode = LOWORD(wParam);
4822 TRACE("wp %lx\n", wParam);
4824 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4825 return 0;
4827 if (!oldFirstVisible)
4829 assert(infoPtr->root->firstChild == NULL);
4830 return 0;
4833 switch (nScrollCode)
4835 case SB_TOP:
4836 newFirstVisible = infoPtr->root->firstChild;
4837 break;
4839 case SB_BOTTOM:
4840 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4841 break;
4843 case SB_LINEUP:
4844 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4845 break;
4847 case SB_LINEDOWN:
4848 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4849 break;
4851 case SB_PAGEUP:
4852 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4853 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4854 break;
4856 case SB_PAGEDOWN:
4857 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4858 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4859 break;
4861 case SB_THUMBTRACK:
4862 case SB_THUMBPOSITION:
4863 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4864 infoPtr->root->firstChild,
4865 (LONG)(SHORT)HIWORD(wParam));
4866 break;
4868 case SB_ENDSCROLL:
4869 return 0;
4872 if (newFirstVisible != NULL)
4874 if (newFirstVisible != oldFirstVisible)
4875 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4876 nScrollCode != SB_THUMBTRACK);
4877 else if (nScrollCode == SB_THUMBPOSITION)
4878 SetScrollPos(infoPtr->hwnd, SB_VERT,
4879 newFirstVisible->visibleOrder, TRUE);
4882 return 0;
4885 static LRESULT
4886 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4888 int maxWidth;
4889 int scrollX = infoPtr->scrollX;
4890 int nScrollCode = LOWORD(wParam);
4892 TRACE("wp %lx\n", wParam);
4894 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4895 return FALSE;
4897 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4898 /* shall never occur */
4899 if (maxWidth <= 0)
4901 scrollX = 0;
4902 goto scroll;
4905 switch (nScrollCode)
4907 case SB_LINELEFT:
4908 scrollX -= infoPtr->uItemHeight;
4909 break;
4910 case SB_LINERIGHT:
4911 scrollX += infoPtr->uItemHeight;
4912 break;
4913 case SB_PAGELEFT:
4914 scrollX -= infoPtr->clientWidth;
4915 break;
4916 case SB_PAGERIGHT:
4917 scrollX += infoPtr->clientWidth;
4918 break;
4920 case SB_THUMBTRACK:
4921 case SB_THUMBPOSITION:
4922 scrollX = (int)(SHORT)HIWORD(wParam);
4923 break;
4925 case SB_ENDSCROLL:
4926 return 0;
4929 if (scrollX > maxWidth)
4930 scrollX = maxWidth;
4931 else if (scrollX < 0)
4932 scrollX = 0;
4934 scroll:
4935 if (scrollX != infoPtr->scrollX)
4937 TREEVIEW_ITEM *item;
4938 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4940 for (item = infoPtr->root->firstChild; item != NULL;
4941 item = TREEVIEW_GetNextListItem(infoPtr, item))
4943 item->linesOffset += scroll_pixels;
4944 item->stateOffset += scroll_pixels;
4945 item->imageOffset += scroll_pixels;
4946 item->textOffset += scroll_pixels;
4949 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4950 infoPtr->scrollX = scrollX;
4951 UpdateWindow(infoPtr->hwnd);
4954 if (nScrollCode != SB_THUMBTRACK)
4955 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4957 return 0;
4960 static LRESULT
4961 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4963 short wheelDelta;
4964 UINT pulScrollLines = 3;
4966 if (wParam & (MK_SHIFT | MK_CONTROL))
4967 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4969 if (infoPtr->firstVisible == NULL)
4970 return TRUE;
4972 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4974 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4975 /* if scrolling changes direction, ignore left overs */
4976 if ((wheelDelta < 0 && infoPtr->wheelRemainder < 0) ||
4977 (wheelDelta > 0 && infoPtr->wheelRemainder > 0))
4978 infoPtr->wheelRemainder += wheelDelta;
4979 else
4980 infoPtr->wheelRemainder = wheelDelta;
4982 if (infoPtr->wheelRemainder && pulScrollLines)
4984 int newDy;
4985 int maxDy;
4986 int lineScroll;
4988 lineScroll = pulScrollLines * (float)infoPtr->wheelRemainder / WHEEL_DELTA;
4989 infoPtr->wheelRemainder -= WHEEL_DELTA * lineScroll / (int)pulScrollLines;
4991 newDy = infoPtr->firstVisible->visibleOrder - lineScroll;
4992 maxDy = infoPtr->maxVisibleOrder;
4994 if (newDy > maxDy)
4995 newDy = maxDy;
4997 if (newDy < 0)
4998 newDy = 0;
5000 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
5002 return TRUE;
5005 /* Create/Destroy *******************************************************/
5007 static LRESULT
5008 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5010 RECT rcClient;
5011 TREEVIEW_INFO *infoPtr;
5012 LOGFONTW lf;
5014 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5016 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5018 if (infoPtr == NULL)
5020 ERR("could not allocate info memory!\n");
5021 return 0;
5024 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5026 infoPtr->hwnd = hwnd;
5027 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5028 infoPtr->Timer = 0;
5029 infoPtr->uNumItems = 0;
5030 infoPtr->cdmode = 0;
5031 infoPtr->uScrollTime = 300; /* milliseconds */
5032 infoPtr->bRedraw = TRUE;
5034 GetClientRect(hwnd, &rcClient);
5036 /* No scroll bars yet. */
5037 infoPtr->clientWidth = rcClient.right;
5038 infoPtr->clientHeight = rcClient.bottom;
5039 infoPtr->uInternalStatus = 0;
5041 infoPtr->treeWidth = 0;
5042 infoPtr->treeHeight = 0;
5044 infoPtr->uIndent = MINIMUM_INDENT;
5045 infoPtr->selectedItem = NULL;
5046 infoPtr->focusedItem = NULL;
5047 infoPtr->hotItem = NULL;
5048 infoPtr->editItem = NULL;
5049 infoPtr->firstVisible = NULL;
5050 infoPtr->maxVisibleOrder = 0;
5051 infoPtr->dropItem = NULL;
5052 infoPtr->insertMarkItem = NULL;
5053 infoPtr->insertBeforeorAfter = 0;
5054 /* dragList */
5056 infoPtr->scrollX = 0;
5057 infoPtr->wheelRemainder = 0;
5059 infoPtr->clrBk = CLR_NONE; /* use system color */
5060 infoPtr->clrText = CLR_NONE; /* use system color */
5061 infoPtr->clrLine = CLR_DEFAULT;
5062 infoPtr->clrInsertMark = CLR_DEFAULT;
5064 /* hwndToolTip */
5066 infoPtr->hwndEdit = NULL;
5067 infoPtr->wpEditOrig = NULL;
5068 infoPtr->bIgnoreEditKillFocus = FALSE;
5069 infoPtr->bLabelChanged = FALSE;
5071 infoPtr->himlNormal = NULL;
5072 infoPtr->himlState = NULL;
5073 infoPtr->normalImageWidth = 0;
5074 infoPtr->normalImageHeight = 0;
5075 infoPtr->stateImageWidth = 0;
5076 infoPtr->stateImageHeight = 0;
5078 infoPtr->items = DPA_Create(16);
5080 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5081 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5082 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5083 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5084 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
5085 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5087 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5089 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5090 infoPtr->root->state = TVIS_EXPANDED;
5091 infoPtr->root->iLevel = -1;
5092 infoPtr->root->visibleOrder = -1;
5094 infoPtr->hwndNotify = lpcs->hwndParent;
5095 infoPtr->hwndToolTip = 0;
5097 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5098 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5100 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5101 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5102 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5103 hwnd, 0, 0, 0);
5105 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5106 ShowScrollBar(hwnd, SB_VERT, FALSE);
5107 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5109 OpenThemeData (hwnd, themeClass);
5111 return 0;
5115 static LRESULT
5116 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5118 TRACE("\n");
5120 /* free item data */
5121 TREEVIEW_RemoveTree(infoPtr);
5122 /* root isn't freed with other items */
5123 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5124 DPA_Destroy(infoPtr->items);
5126 /* tool tip is automatically destroyed: we are its owner */
5128 /* Restore original wndproc */
5129 if (infoPtr->hwndEdit)
5130 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5131 (DWORD_PTR)infoPtr->wpEditOrig);
5133 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5135 /* Deassociate treeview from the window before doing anything drastic. */
5136 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5138 DeleteObject(infoPtr->hDefaultFont);
5139 DeleteObject(infoPtr->hBoldFont);
5140 DeleteObject(infoPtr->hUnderlineFont);
5141 DeleteObject(infoPtr->hBoldUnderlineFont);
5142 Free(infoPtr);
5144 return 0;
5147 /* Miscellaneous Messages ***********************************************/
5149 static LRESULT
5150 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5152 static const struct
5154 unsigned char code;
5156 scroll[] =
5158 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5159 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5160 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5161 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5162 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5163 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5164 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5165 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5166 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5167 #undef SCROLL_ENTRY
5170 if (key >= VK_PRIOR && key <= VK_DOWN)
5172 unsigned char code = scroll[key - VK_PRIOR].code;
5174 (((code & (1 << 7)) == (SB_HORZ << 7))
5175 ? TREEVIEW_HScroll
5176 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5179 return 0;
5182 /************************************************************************
5183 * TREEVIEW_KeyDown
5185 * VK_UP Move selection to the previous non-hidden item.
5186 * VK_DOWN Move selection to the next non-hidden item.
5187 * VK_HOME Move selection to the first item.
5188 * VK_END Move selection to the last item.
5189 * VK_LEFT If expanded then collapse, otherwise move to parent.
5190 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5191 * VK_ADD Expand.
5192 * VK_SUBTRACT Collapse.
5193 * VK_MULTIPLY Expand all.
5194 * VK_PRIOR Move up GetVisibleCount items.
5195 * VK_NEXT Move down GetVisibleCount items.
5196 * VK_BACK Move to parent.
5197 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5199 static LRESULT
5200 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5202 /* If it is non-NULL and different, it will be selected and visible. */
5203 TREEVIEW_ITEM *newSelection = NULL;
5204 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5205 NMTVKEYDOWN nmkeydown;
5207 TRACE("%lx\n", wParam);
5209 nmkeydown.wVKey = wParam;
5210 nmkeydown.flags = 0;
5211 TREEVIEW_SendRealNotify(infoPtr, TVN_KEYDOWN, &nmkeydown.hdr);
5213 if (prevItem == NULL)
5214 return FALSE;
5216 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5217 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5219 switch (wParam)
5221 case VK_UP:
5222 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5223 if (!newSelection)
5224 newSelection = infoPtr->root->firstChild;
5225 break;
5227 case VK_DOWN:
5228 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5229 break;
5231 case VK_RETURN:
5232 TREEVIEW_SendSimpleNotify(infoPtr, NM_RETURN);
5233 break;
5235 case VK_HOME:
5236 newSelection = infoPtr->root->firstChild;
5237 break;
5239 case VK_END:
5240 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5241 break;
5243 case VK_LEFT:
5244 if (prevItem->state & TVIS_EXPANDED)
5246 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5248 else if (prevItem->parent != infoPtr->root)
5250 newSelection = prevItem->parent;
5252 break;
5254 case VK_RIGHT:
5255 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5257 if (!(prevItem->state & TVIS_EXPANDED))
5258 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5259 else
5261 newSelection = prevItem->firstChild;
5265 break;
5267 case VK_MULTIPLY:
5268 TREEVIEW_ExpandAll(infoPtr, prevItem);
5269 break;
5271 case VK_ADD:
5272 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5273 break;
5275 case VK_SUBTRACT:
5276 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5277 break;
5279 case VK_PRIOR:
5280 newSelection
5281 = TREEVIEW_GetListItem(infoPtr, prevItem,
5282 -TREEVIEW_GetVisibleCount(infoPtr));
5283 break;
5285 case VK_NEXT:
5286 newSelection
5287 = TREEVIEW_GetListItem(infoPtr, prevItem,
5288 TREEVIEW_GetVisibleCount(infoPtr));
5289 break;
5291 case VK_BACK:
5292 newSelection = prevItem->parent;
5293 if (newSelection == infoPtr->root)
5294 newSelection = NULL;
5295 break;
5297 case VK_SPACE:
5298 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5299 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5300 break;
5303 if (newSelection && newSelection != prevItem)
5305 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5306 TVC_BYKEYBOARD))
5308 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5312 return FALSE;
5315 static LRESULT
5316 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5318 /* remove hot effect from item */
5319 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5320 infoPtr->hotItem = NULL;
5322 return 0;
5325 static LRESULT
5326 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5328 POINT pt;
5329 TRACKMOUSEEVENT trackinfo;
5330 TREEVIEW_ITEM * item;
5332 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5334 /* fill in the TRACKMOUSEEVENT struct */
5335 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5336 trackinfo.dwFlags = TME_QUERY;
5337 trackinfo.hwndTrack = infoPtr->hwnd;
5339 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5340 _TrackMouseEvent(&trackinfo);
5342 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5343 if(!(trackinfo.dwFlags & TME_LEAVE))
5345 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5346 trackinfo.hwndTrack = infoPtr->hwnd;
5347 /* do it as fast as possible, minimal systimer latency will be used */
5348 trackinfo.dwHoverTime = 1;
5350 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5351 /* and can properly deactivate the hot item */
5352 _TrackMouseEvent(&trackinfo);
5355 pt.x = (short)LOWORD(lParam);
5356 pt.y = (short)HIWORD(lParam);
5358 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5360 if (item != infoPtr->hotItem)
5362 /* redraw old hot item */
5363 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5364 infoPtr->hotItem = item;
5365 /* redraw new hot item */
5366 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5369 return 0;
5372 /* Draw themed border */
5373 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5375 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5376 HDC dc;
5377 RECT r;
5378 HRGN cliprgn;
5379 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5380 cyEdge = GetSystemMetrics (SM_CYEDGE);
5382 if (!theme)
5383 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5385 GetWindowRect(infoPtr->hwnd, &r);
5387 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5388 r.right - cxEdge, r.bottom - cyEdge);
5389 if (region != (HRGN)1)
5390 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5391 OffsetRect(&r, -r.left, -r.top);
5393 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5394 OffsetRect(&r, -r.left, -r.top);
5396 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5397 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5398 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5399 ReleaseDC(infoPtr->hwnd, dc);
5401 /* Call default proc to get the scrollbars etc. painted */
5402 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5404 return TRUE;
5407 static LRESULT
5408 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5410 LPNMHDR lpnmh = (LPNMHDR)lParam;
5412 if (lpnmh->code == PGN_CALCSIZE) {
5413 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5415 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5416 lppgc->iWidth = infoPtr->treeWidth;
5417 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5418 infoPtr->treeWidth, infoPtr->clientWidth);
5420 else {
5421 lppgc->iHeight = infoPtr->treeHeight;
5422 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5423 infoPtr->treeHeight, infoPtr->clientHeight);
5425 return 0;
5427 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5430 static LRESULT
5431 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5433 if (wParam == SIZE_RESTORED)
5435 infoPtr->clientWidth = (short)LOWORD(lParam);
5436 infoPtr->clientHeight = (short)HIWORD(lParam);
5438 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5439 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5440 TREEVIEW_UpdateScrollBars(infoPtr);
5442 else
5444 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5447 TREEVIEW_Invalidate(infoPtr, NULL);
5448 return 0;
5451 static LRESULT
5452 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5454 TRACE("(%lx %lx)\n", wParam, lParam);
5456 if (wParam == GWL_STYLE)
5458 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5460 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5462 if (dwNewStyle & TVS_CHECKBOXES)
5464 TREEVIEW_InitCheckboxes(infoPtr);
5465 TRACE("checkboxes enabled\n");
5467 /* set all items to state image index 1 */
5468 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5470 else
5472 FIXME("tried to disable checkboxes\n");
5476 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5478 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5480 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5481 TRACE("tooltips enabled\n");
5483 else
5485 DestroyWindow(infoPtr->hwndToolTip);
5486 infoPtr->hwndToolTip = 0;
5487 TRACE("tooltips disabled\n");
5491 infoPtr->dwStyle = dwNewStyle;
5494 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
5495 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5496 TREEVIEW_UpdateScrollBars(infoPtr);
5497 TREEVIEW_Invalidate(infoPtr, NULL);
5499 return 0;
5502 static LRESULT
5503 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5505 POINT pt;
5506 TREEVIEW_ITEM * item;
5507 NMMOUSE nmmouse;
5509 GetCursorPos(&pt);
5510 ScreenToClient(infoPtr->hwnd, &pt);
5512 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5514 memset(&nmmouse, 0, sizeof(nmmouse));
5515 if (item)
5517 nmmouse.dwItemSpec = (DWORD_PTR)item;
5518 nmmouse.dwItemData = item->lParam;
5520 nmmouse.pt.x = 0;
5521 nmmouse.pt.y = 0;
5522 nmmouse.dwHitInfo = lParam;
5523 if (TREEVIEW_SendRealNotify(infoPtr, NM_SETCURSOR, &nmmouse.hdr))
5524 return 0;
5526 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5528 SetCursor(infoPtr->hcurHand);
5529 return 0;
5531 else
5532 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5535 static LRESULT
5536 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5538 TRACE("\n");
5540 if (!infoPtr->selectedItem)
5542 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5543 TVC_UNKNOWN);
5546 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5547 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5548 return 0;
5551 static LRESULT
5552 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5554 TRACE("\n");
5556 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5557 UpdateWindow(infoPtr->hwnd);
5558 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5559 return 0;
5562 /* update theme after a WM_THEMECHANGED message */
5563 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5565 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5566 CloseThemeData (theme);
5567 OpenThemeData (infoPtr->hwnd, themeClass);
5568 return 0;
5572 static LRESULT WINAPI
5573 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5575 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5577 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5579 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5580 else
5582 if (uMsg == WM_CREATE)
5583 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5584 else
5585 goto def;
5588 switch (uMsg)
5590 case TVM_CREATEDRAGIMAGE:
5591 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5593 case TVM_DELETEITEM:
5594 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5596 case TVM_EDITLABELA:
5597 case TVM_EDITLABELW:
5598 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5600 case TVM_ENDEDITLABELNOW:
5601 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5603 case TVM_ENSUREVISIBLE:
5604 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5606 case TVM_EXPAND:
5607 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5609 case TVM_GETBKCOLOR:
5610 return TREEVIEW_GetBkColor(infoPtr);
5612 case TVM_GETCOUNT:
5613 return TREEVIEW_GetCount(infoPtr);
5615 case TVM_GETEDITCONTROL:
5616 return TREEVIEW_GetEditControl(infoPtr);
5618 case TVM_GETIMAGELIST:
5619 return TREEVIEW_GetImageList(infoPtr, wParam);
5621 case TVM_GETINDENT:
5622 return TREEVIEW_GetIndent(infoPtr);
5624 case TVM_GETINSERTMARKCOLOR:
5625 return TREEVIEW_GetInsertMarkColor(infoPtr);
5627 case TVM_GETISEARCHSTRINGA:
5628 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5629 return 0;
5631 case TVM_GETISEARCHSTRINGW:
5632 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5633 return 0;
5635 case TVM_GETITEMA:
5636 case TVM_GETITEMW:
5637 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5638 uMsg == TVM_GETITEMW);
5639 case TVM_GETITEMHEIGHT:
5640 return TREEVIEW_GetItemHeight(infoPtr);
5642 case TVM_GETITEMRECT:
5643 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5645 case TVM_GETITEMSTATE:
5646 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5648 case TVM_GETLINECOLOR:
5649 return TREEVIEW_GetLineColor(infoPtr);
5651 case TVM_GETNEXTITEM:
5652 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5654 case TVM_GETSCROLLTIME:
5655 return TREEVIEW_GetScrollTime(infoPtr);
5657 case TVM_GETTEXTCOLOR:
5658 return TREEVIEW_GetTextColor(infoPtr);
5660 case TVM_GETTOOLTIPS:
5661 return TREEVIEW_GetToolTips(infoPtr);
5663 case TVM_GETUNICODEFORMAT:
5664 return TREEVIEW_GetUnicodeFormat(infoPtr);
5666 case TVM_GETVISIBLECOUNT:
5667 return TREEVIEW_GetVisibleCount(infoPtr);
5669 case TVM_HITTEST:
5670 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5672 case TVM_INSERTITEMA:
5673 case TVM_INSERTITEMW:
5674 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5675 uMsg == TVM_INSERTITEMW);
5676 case TVM_SELECTITEM:
5677 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5679 case TVM_SETBKCOLOR:
5680 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5682 case TVM_SETIMAGELIST:
5683 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5685 case TVM_SETINDENT:
5686 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5688 case TVM_SETINSERTMARK:
5689 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5691 case TVM_SETINSERTMARKCOLOR:
5692 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5694 case TVM_SETITEMA:
5695 case TVM_SETITEMW:
5696 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5697 uMsg == TVM_SETITEMW);
5698 case TVM_SETLINECOLOR:
5699 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5701 case TVM_SETITEMHEIGHT:
5702 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5704 case TVM_SETSCROLLTIME:
5705 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5707 case TVM_SETTEXTCOLOR:
5708 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5710 case TVM_SETTOOLTIPS:
5711 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5713 case TVM_SETUNICODEFORMAT:
5714 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5716 case TVM_SORTCHILDREN:
5717 return TREEVIEW_SortChildren(infoPtr, lParam);
5719 case TVM_SORTCHILDRENCB:
5720 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5722 case WM_CHAR:
5723 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5725 case WM_COMMAND:
5726 return TREEVIEW_Command(infoPtr, wParam, lParam);
5728 case WM_DESTROY:
5729 return TREEVIEW_Destroy(infoPtr);
5731 /* WM_ENABLE */
5733 case WM_ERASEBKGND:
5734 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5736 case WM_GETDLGCODE:
5737 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5739 case WM_GETFONT:
5740 return TREEVIEW_GetFont(infoPtr);
5742 case WM_HSCROLL:
5743 return TREEVIEW_HScroll(infoPtr, wParam);
5745 case WM_KEYDOWN:
5746 return TREEVIEW_KeyDown(infoPtr, wParam);
5748 case WM_KILLFOCUS:
5749 return TREEVIEW_KillFocus(infoPtr);
5751 case WM_LBUTTONDBLCLK:
5752 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5754 case WM_LBUTTONDOWN:
5755 return TREEVIEW_LButtonDown(infoPtr, lParam);
5757 /* WM_MBUTTONDOWN */
5759 case WM_MOUSELEAVE:
5760 return TREEVIEW_MouseLeave(infoPtr);
5762 case WM_MOUSEMOVE:
5763 return TREEVIEW_MouseMove(infoPtr, lParam);
5765 case WM_NCLBUTTONDOWN:
5766 if (infoPtr->hwndEdit)
5767 SetFocus(infoPtr->hwnd);
5768 goto def;
5770 case WM_NCPAINT:
5771 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5773 case WM_NOTIFY:
5774 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5776 case WM_NOTIFYFORMAT:
5777 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5779 case WM_PRINTCLIENT:
5780 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5782 case WM_PAINT:
5783 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5785 case WM_RBUTTONDOWN:
5786 return TREEVIEW_RButtonDown(infoPtr, lParam);
5788 case WM_SETCURSOR:
5789 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5791 case WM_SETFOCUS:
5792 return TREEVIEW_SetFocus(infoPtr);
5794 case WM_SETFONT:
5795 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5797 case WM_SETREDRAW:
5798 return TREEVIEW_SetRedraw(infoPtr, wParam);
5800 case WM_SIZE:
5801 return TREEVIEW_Size(infoPtr, wParam, lParam);
5803 case WM_STYLECHANGED:
5804 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5806 case WM_SYSCOLORCHANGE:
5807 COMCTL32_RefreshSysColors();
5808 return 0;
5810 /* WM_SYSKEYDOWN */
5812 case WM_TIMER:
5813 return TREEVIEW_HandleTimer(infoPtr, wParam);
5815 case WM_THEMECHANGED:
5816 return TREEVIEW_ThemeChanged (infoPtr);
5818 case WM_VSCROLL:
5819 return TREEVIEW_VScroll(infoPtr, wParam);
5821 /* WM_WININICHANGE */
5823 case WM_MOUSEWHEEL:
5824 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5826 case WM_DRAWITEM:
5827 TRACE("drawItem\n");
5828 goto def;
5830 default:
5831 /* This mostly catches MFC and Delphi messages. :( */
5832 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5833 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5834 def:
5835 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5840 /* Class Registration ***************************************************/
5842 VOID
5843 TREEVIEW_Register(void)
5845 WNDCLASSW wndClass;
5847 TRACE("\n");
5849 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5850 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5851 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5852 wndClass.cbClsExtra = 0;
5853 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5855 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5856 wndClass.hbrBackground = 0;
5857 wndClass.lpszClassName = WC_TREEVIEWW;
5859 RegisterClassW(&wndClass);
5863 VOID
5864 TREEVIEW_Unregister(void)
5866 UnregisterClassW(WC_TREEVIEWW, NULL);
5870 /* Tree Verification ****************************************************/
5872 static inline void
5873 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5875 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5876 const TREEVIEW_ITEM *item)
5878 assert(infoPtr != NULL);
5879 assert(item != NULL);
5881 /* both NULL, or both non-null */
5882 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5884 assert(item->firstChild != item);
5885 assert(item->lastChild != item);
5887 if (item->firstChild)
5889 assert(item->firstChild->parent == item);
5890 assert(item->firstChild->prevSibling == NULL);
5893 if (item->lastChild)
5895 assert(item->lastChild->parent == item);
5896 assert(item->lastChild->nextSibling == NULL);
5899 assert(item->nextSibling != item);
5900 if (item->nextSibling)
5902 assert(item->nextSibling->parent == item->parent);
5903 assert(item->nextSibling->prevSibling == item);
5906 assert(item->prevSibling != item);
5907 if (item->prevSibling)
5909 assert(item->prevSibling->parent == item->parent);
5910 assert(item->prevSibling->nextSibling == item);
5914 static inline void
5915 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5917 assert(item != NULL);
5919 assert(item->parent != NULL);
5920 assert(item->parent != item);
5921 assert(item->iLevel == item->parent->iLevel + 1);
5923 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5925 TREEVIEW_VerifyItemCommon(infoPtr, item);
5927 TREEVIEW_VerifyChildren(infoPtr, item);
5930 static inline void
5931 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5933 const TREEVIEW_ITEM *child;
5934 assert(item != NULL);
5936 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5937 TREEVIEW_VerifyItem(infoPtr, child);
5940 static inline void
5941 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5943 TREEVIEW_ITEM *root = infoPtr->root;
5945 assert(root != NULL);
5946 assert(root->iLevel == -1);
5947 assert(root->parent == NULL);
5948 assert(root->prevSibling == NULL);
5950 TREEVIEW_VerifyItemCommon(infoPtr, root);
5952 TREEVIEW_VerifyChildren(infoPtr, root);
5955 static void
5956 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5958 if (!TRACE_ON(treeview)) return;
5960 assert(infoPtr != NULL);
5961 TREEVIEW_VerifyRoot(infoPtr);