windowscodecs/tests: Fix compilation on systems that don't support nameless structs.
[wine/multimedia.git] / dlls / comctl32 / treeview.c
blob63e350725aa8f36f08c0de420026a3157802e08c
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 COLORREF clrBk;
106 COLORREF clrText;
107 COLORREF clrLine;
108 COLORREF clrInsertMark;
109 HFONT hFont;
110 HFONT hDefaultFont;
111 HFONT hBoldFont;
112 HFONT hUnderlineFont;
113 HCURSOR hcurHand;
114 HWND hwndToolTip;
116 HWND hwndEdit;
117 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
118 BOOL bIgnoreEditKillFocus;
119 BOOL bLabelChanged;
121 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
122 HIMAGELIST himlNormal;
123 int normalImageHeight;
124 int normalImageWidth;
125 HIMAGELIST himlState;
126 int stateImageHeight;
127 int stateImageWidth;
128 HDPA items;
130 DWORD lastKeyPressTimestamp;
131 WPARAM charCode;
132 INT nSearchParamLength;
133 WCHAR szSearchParam[ MAX_PATH ];
134 } TREEVIEW_INFO;
136 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
138 HTREEITEM parent; /* handle to parent or 0 if at root */
139 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
140 HTREEITEM firstChild; /* handle to first child or 0 if no child */
142 UINT callbackMask;
143 UINT state;
144 UINT stateMask;
145 LPWSTR pszText;
146 int cchTextMax;
147 int iImage;
148 int iSelectedImage;
149 int iExpandedImage;
150 int cChildren;
151 LPARAM lParam;
152 int iIntegral; /* item height multiplier (1 is normal) */
153 int iLevel; /* indentation level:0=root level */
154 HTREEITEM lastChild;
155 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
156 RECT rect;
157 LONG linesOffset;
158 LONG stateOffset;
159 LONG imageOffset;
160 LONG textOffset;
161 LONG textWidth; /* horizontal text extent for pszText */
162 LONG visibleOrder; /* visible ordering, 0 is first visible item */
163 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
164 } TREEVIEW_ITEM;
166 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
167 #define KEY_DELAY 450
169 /* bitflags for infoPtr->uInternalStatus */
171 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
172 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
173 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
174 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
175 #define TV_RDRAG 0x10 /* ditto Rbutton */
176 #define TV_RDRAGGING 0x20
178 /* bitflags for infoPtr->timer */
180 #define TV_EDIT_TIMER 2
181 #define TV_EDIT_TIMER_SET 2
183 #define TEXT_CALLBACK_SIZE 260
185 #define TREEVIEW_LEFT_MARGIN 8
187 #define MINIMUM_INDENT 19
189 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
191 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
192 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
193 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
195 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
196 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
197 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
198 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
200 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
203 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
206 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
208 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
209 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
210 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
211 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
212 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
213 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
214 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
216 /* Random Utilities *****************************************************/
217 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
219 /* Returns the treeview private data if hwnd is a treeview.
220 * Otherwise returns an undefined value. */
221 static inline TREEVIEW_INFO *
222 TREEVIEW_GetInfoPtr(HWND hwnd)
224 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
227 /* Don't call this. Nothing wants an item index. */
228 static inline int
229 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
231 return DPA_GetPtrIndex(infoPtr->items, handle);
234 /* Checks if item has changed and needs to be redrawn */
235 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
236 const TVITEMEXW *tvChange)
238 /* Number of children has changed */
239 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
240 return TRUE;
242 /* Image has changed and it's not a callback */
243 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
244 tiNew->iImage != I_IMAGECALLBACK)
245 return TRUE;
247 /* Selected image has changed and it's not a callback */
248 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
249 tiNew->iSelectedImage != I_IMAGECALLBACK)
250 return TRUE;
252 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
253 tiNew->iExpandedImage != I_IMAGECALLBACK)
254 return TRUE;
256 /* Text has changed and it's not a callback */
257 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
258 tiNew->pszText != LPSTR_TEXTCALLBACKW)
259 return TRUE;
261 /* Indent has changed */
262 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
263 return TRUE;
265 /* Item state has changed */
266 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
267 return TRUE;
269 return FALSE;
272 /***************************************************************************
273 * This method checks that handle is an item for this tree.
275 static BOOL
276 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
278 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
280 TRACE("invalid item %p\n", handle);
281 return FALSE;
283 else
284 return TRUE;
287 static HFONT
288 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
290 LOGFONTW font;
292 GetObjectW(hOrigFont, sizeof(font), &font);
293 font.lfWeight = FW_BOLD;
294 return CreateFontIndirectW(&font);
297 static HFONT
298 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
300 LOGFONTW font;
302 GetObjectW(hOrigFont, sizeof(font), &font);
303 font.lfUnderline = TRUE;
304 return CreateFontIndirectW(&font);
307 static inline HFONT
308 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
310 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
311 return infoPtr->hUnderlineFont;
312 if (item->state & TVIS_BOLD)
313 return infoPtr->hBoldFont;
314 return infoPtr->hFont;
317 /* for trace/debugging purposes only */
318 static const char *
319 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
321 if (item == NULL) return "<null item>";
322 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
323 if (item->pszText == NULL) return "<null>";
324 return debugstr_w(item->pszText);
327 /* An item is not a child of itself. */
328 static BOOL
329 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
333 child = child->parent;
334 if (child == parent) return TRUE;
335 } while (child != NULL);
337 return FALSE;
341 /* Tree Traversal *******************************************************/
343 /***************************************************************************
344 * This method returns the last expanded sibling or child child item
345 * of a tree node
347 static TREEVIEW_ITEM *
348 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
350 if (!item) return NULL;
352 while (item->lastChild)
354 if (item->state & TVIS_EXPANDED)
355 item = item->lastChild;
356 else
357 break;
360 if (item == infoPtr->root)
361 return NULL;
363 return item;
366 /***************************************************************************
367 * This method returns the previous non-hidden item in the list not
368 * considering the tree hierarchy.
370 static TREEVIEW_ITEM *
371 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
373 if (tvItem->prevSibling)
375 /* This item has a prevSibling, get the last item in the sibling's tree. */
376 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
378 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
379 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
380 else
381 return upItem;
383 else
385 /* this item does not have a prevSibling, get the parent */
386 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
391 /***************************************************************************
392 * This method returns the next physical item in the treeview not
393 * considering the tree hierarchy.
395 static TREEVIEW_ITEM *
396 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
399 * If this item has children and is expanded, return the first child
401 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
403 return tvItem->firstChild;
408 * try to get the sibling
410 if (tvItem->nextSibling)
411 return tvItem->nextSibling;
414 * Otherwise, get the parent's sibling.
416 while (tvItem->parent)
418 tvItem = tvItem->parent;
420 if (tvItem->nextSibling)
421 return tvItem->nextSibling;
424 return NULL;
427 /***************************************************************************
428 * This method returns the nth item starting at the given item. It returns
429 * the last item (or first) we we run out of items.
431 * Will scroll backward if count is <0.
432 * forward if count is >0.
434 static TREEVIEW_ITEM *
435 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
436 LONG count)
438 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
439 TREEVIEW_ITEM *previousItem;
441 assert(item != NULL);
443 if (count > 0)
445 next_item = TREEVIEW_GetNextListItem;
447 else if (count < 0)
449 count = -count;
450 next_item = TREEVIEW_GetPrevListItem;
452 else
453 return item;
457 previousItem = item;
458 item = next_item(infoPtr, item);
460 } while (--count && item != NULL);
463 return item ? item : previousItem;
466 /* Notifications ************************************************************/
468 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
470 if (!infoPtr->bNtfUnicode) {
471 switch (code) {
472 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
473 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
474 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
475 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
476 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
477 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
478 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
479 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
480 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
481 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
482 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
483 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
486 return code;
489 static inline BOOL
490 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPNMHDR pnmh)
492 TRACE("wParam=%ld, lParam=%p\n", wParam, pnmh);
493 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, (LPARAM)pnmh);
496 static BOOL
497 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
499 NMHDR nmhdr;
500 HWND hwnd = infoPtr->hwnd;
502 TRACE("%d\n", code);
503 nmhdr.hwndFrom = hwnd;
504 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
505 nmhdr.code = get_notifycode(infoPtr, code);
507 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, &nmhdr);
510 static VOID
511 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
513 tvItem->mask = mask;
514 tvItem->hItem = item;
515 tvItem->state = item->state;
516 tvItem->stateMask = 0;
517 tvItem->iImage = item->iImage;
518 tvItem->iSelectedImage = item->iSelectedImage;
519 tvItem->cChildren = item->cChildren;
520 tvItem->lParam = item->lParam;
522 if(mask & TVIF_TEXT)
524 if (!infoPtr->bNtfUnicode)
526 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
527 tvItem->pszText = Alloc (tvItem->cchTextMax);
528 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
530 else
532 tvItem->cchTextMax = item->cchTextMax;
533 tvItem->pszText = item->pszText;
536 else
538 tvItem->cchTextMax = 0;
539 tvItem->pszText = NULL;
543 static BOOL
544 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
545 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
547 HWND hwnd = infoPtr->hwnd;
548 NMTREEVIEWW nmhdr;
549 BOOL ret;
551 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
552 code, action, oldItem, newItem);
554 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
556 nmhdr.hdr.hwndFrom = hwnd;
557 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
558 nmhdr.hdr.code = get_notifycode(infoPtr, code);
559 nmhdr.action = action;
561 if (oldItem)
562 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
564 if (newItem)
565 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
567 nmhdr.ptDrag.x = 0;
568 nmhdr.ptDrag.y = 0;
570 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
571 if (!infoPtr->bNtfUnicode)
573 Free(nmhdr.itemOld.pszText);
574 Free(nmhdr.itemNew.pszText);
576 return ret;
579 static BOOL
580 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
581 HTREEITEM dragItem, POINT pt)
583 HWND hwnd = infoPtr->hwnd;
584 NMTREEVIEWW nmhdr;
586 TRACE("code:%d dragitem:%p\n", code, dragItem);
588 nmhdr.hdr.hwndFrom = hwnd;
589 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
590 nmhdr.hdr.code = get_notifycode(infoPtr, code);
591 nmhdr.action = 0;
592 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
593 nmhdr.itemNew.hItem = dragItem;
594 nmhdr.itemNew.state = dragItem->state;
595 nmhdr.itemNew.lParam = dragItem->lParam;
597 nmhdr.ptDrag.x = pt.x;
598 nmhdr.ptDrag.y = pt.y;
600 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, &nmhdr.hdr);
604 static BOOL
605 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
606 HDC hdc, RECT rc)
608 HWND hwnd = infoPtr->hwnd;
609 NMTVCUSTOMDRAW nmcdhdr;
610 LPNMCUSTOMDRAW nmcd;
612 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
614 nmcd = &nmcdhdr.nmcd;
615 nmcd->hdr.hwndFrom = hwnd;
616 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
617 nmcd->hdr.code = NM_CUSTOMDRAW;
618 nmcd->dwDrawStage = dwDrawStage;
619 nmcd->hdc = hdc;
620 nmcd->rc = rc;
621 nmcd->dwItemSpec = 0;
622 nmcd->uItemState = 0;
623 nmcd->lItemlParam = 0;
624 nmcdhdr.clrText = infoPtr->clrText;
625 nmcdhdr.clrTextBk = infoPtr->clrBk;
626 nmcdhdr.iLevel = 0;
628 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr.nmcd.hdr);
633 /* FIXME: need to find out when the flags in uItemState need to be set */
635 static BOOL
636 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
637 TREEVIEW_ITEM *item, UINT uItemDrawState,
638 NMTVCUSTOMDRAW *nmcdhdr)
640 HWND hwnd = infoPtr->hwnd;
641 LPNMCUSTOMDRAW nmcd;
642 DWORD dwDrawStage;
643 DWORD_PTR dwItemSpec;
644 UINT uItemState;
646 dwDrawStage = CDDS_ITEM | uItemDrawState;
647 dwItemSpec = (DWORD_PTR)item;
648 uItemState = 0;
649 if (item->state & TVIS_SELECTED)
650 uItemState |= CDIS_SELECTED;
651 if (item == infoPtr->selectedItem)
652 uItemState |= CDIS_FOCUS;
653 if (item == infoPtr->hotItem)
654 uItemState |= CDIS_HOT;
656 nmcd = &nmcdhdr->nmcd;
657 nmcd->hdr.hwndFrom = hwnd;
658 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
659 nmcd->hdr.code = NM_CUSTOMDRAW;
660 nmcd->dwDrawStage = dwDrawStage;
661 nmcd->hdc = hdc;
662 nmcd->rc = item->rect;
663 nmcd->dwItemSpec = dwItemSpec;
664 nmcd->uItemState = uItemState;
665 nmcd->lItemlParam = item->lParam;
666 nmcdhdr->iLevel = item->iLevel;
668 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
669 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
670 nmcd->uItemState, nmcd->lItemlParam);
672 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, &nmcdhdr->nmcd.hdr);
675 static BOOL
676 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
678 HWND hwnd = infoPtr->hwnd;
679 NMTVDISPINFOW tvdi;
680 BOOL ret;
682 tvdi.hdr.hwndFrom = hwnd;
683 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
684 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
686 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
687 &tvdi.item, editItem);
689 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
691 if (!infoPtr->bNtfUnicode)
692 Free(tvdi.item.pszText);
694 return ret;
697 static void
698 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
699 UINT mask)
701 NMTVDISPINFOEXW callback;
702 HWND hwnd = infoPtr->hwnd;
704 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
705 mask &= item->callbackMask;
707 if (mask == 0) return;
709 callback.hdr.hwndFrom = hwnd;
710 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
711 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
713 /* 'state' always contains valid value, as well as 'lParam'.
714 * All other parameters are uninitialized.
716 callback.item.pszText = item->pszText;
717 callback.item.cchTextMax = item->cchTextMax;
718 callback.item.mask = mask;
719 callback.item.hItem = item;
720 callback.item.state = item->state;
721 callback.item.lParam = item->lParam;
723 /* If text is changed we need to recalculate textWidth */
724 if (mask & TVIF_TEXT)
725 item->textWidth = 0;
727 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, &callback.hdr);
728 TRACE("resulting code 0x%08x\n", callback.hdr.code);
730 /* It may have changed due to a call to SetItem. */
731 mask &= item->callbackMask;
733 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
735 /* Instead of copying text into our buffer user specified his own */
736 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
737 LPWSTR newText;
738 int buflen;
739 int len = MultiByteToWideChar( CP_ACP, 0,
740 (LPSTR)callback.item.pszText, -1,
741 NULL, 0);
742 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
743 newText = ReAlloc(item->pszText, buflen);
745 TRACE("returned str %s, len=%d, buflen=%d\n",
746 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
748 if (newText)
750 item->pszText = newText;
751 MultiByteToWideChar( CP_ACP, 0,
752 (LPSTR)callback.item.pszText, -1,
753 item->pszText, buflen/sizeof(WCHAR));
754 item->cchTextMax = buflen/sizeof(WCHAR);
756 /* If ReAlloc fails we have nothing to do, but keep original text */
758 else {
759 int len = max(lstrlenW(callback.item.pszText) + 1,
760 TEXT_CALLBACK_SIZE);
761 LPWSTR newText = ReAlloc(item->pszText, len);
763 TRACE("returned wstr %s, len=%d\n",
764 debugstr_w(callback.item.pszText), len);
766 if (newText)
768 item->pszText = newText;
769 strcpyW(item->pszText, callback.item.pszText);
770 item->cchTextMax = len;
772 /* If ReAlloc fails we have nothing to do, but keep original text */
775 else if (mask & TVIF_TEXT) {
776 /* User put text into our buffer, that is ok unless A string */
777 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
778 LPWSTR newText;
779 int buflen;
780 int len = MultiByteToWideChar( CP_ACP, 0,
781 (LPSTR)callback.item.pszText, -1,
782 NULL, 0);
783 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
784 newText = Alloc(buflen);
786 TRACE("same buffer str %s, len=%d, buflen=%d\n",
787 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
789 if (newText)
791 LPWSTR oldText = item->pszText;
792 item->pszText = newText;
793 MultiByteToWideChar( CP_ACP, 0,
794 (LPSTR)callback.item.pszText, -1,
795 item->pszText, buflen/sizeof(WCHAR));
796 item->cchTextMax = buflen/sizeof(WCHAR);
797 Free(oldText);
802 if (mask & TVIF_IMAGE)
803 item->iImage = callback.item.iImage;
805 if (mask & TVIF_SELECTEDIMAGE)
806 item->iSelectedImage = callback.item.iSelectedImage;
808 if (mask & TVIF_EXPANDEDIMAGE)
809 item->iExpandedImage = callback.item.iExpandedImage;
811 if (mask & TVIF_CHILDREN)
812 item->cChildren = callback.item.cChildren;
814 if (callback.item.mask & TVIF_STATE)
816 item->state &= ~callback.item.stateMask;
817 item->state |= (callback.item.state & callback.item.stateMask);
820 /* These members are now permanently set. */
821 if (callback.item.mask & TVIF_DI_SETITEM)
822 item->callbackMask &= ~callback.item.mask;
825 /***************************************************************************
826 * This function uses cChildren field to decide whether the item has
827 * children or not.
828 * Note: if this returns TRUE, the child items may not actually exist,
829 * they could be virtual.
831 * Just use item->firstChild to check for physical children.
833 static BOOL
834 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
836 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
838 return item->cChildren > 0;
841 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
843 INT format;
845 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
847 if (nCommand != NF_REQUERY) return 0;
849 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
850 TRACE("format=%d\n", format);
852 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
854 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
856 return format;
859 /* Item Position ********************************************************/
861 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
862 static VOID
863 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
865 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
866 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
867 > TVS_LINESATROOT);
869 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
870 - infoPtr->scrollX;
871 item->stateOffset = item->linesOffset + infoPtr->uIndent;
872 item->imageOffset = item->stateOffset
873 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
874 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
877 static VOID
878 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
880 HDC hdc;
881 HFONT hOldFont=0;
882 SIZE sz;
884 /* DRAW's OM docker creates items like this */
885 if (item->pszText == NULL)
887 item->textWidth = 0;
888 return;
891 if (hDC != 0)
893 hdc = hDC;
895 else
897 hdc = GetDC(infoPtr->hwnd);
898 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
901 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
902 item->textWidth = sz.cx;
904 if (hDC == 0)
906 SelectObject(hdc, hOldFont);
907 ReleaseDC(0, hdc);
911 static VOID
912 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
914 item->rect.top = infoPtr->uItemHeight *
915 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
917 item->rect.bottom = item->rect.top
918 + infoPtr->uItemHeight * item->iIntegral - 1;
920 item->rect.left = 0;
921 item->rect.right = infoPtr->clientWidth;
924 /* We know that only items after start need their order updated. */
925 static void
926 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
928 TREEVIEW_ITEM *item;
929 int order;
931 if (!start)
933 start = infoPtr->root->firstChild;
934 order = 0;
936 else
937 order = start->visibleOrder;
939 for (item = start; item != NULL;
940 item = TREEVIEW_GetNextListItem(infoPtr, item))
942 if (!ISVISIBLE(item) && order > 0)
943 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
944 item->visibleOrder = order;
945 order += item->iIntegral;
948 infoPtr->maxVisibleOrder = order;
950 for (item = start; item != NULL;
951 item = TREEVIEW_GetNextListItem(infoPtr, item))
953 TREEVIEW_ComputeItemRect(infoPtr, item);
958 /* Update metrics of all items in selected subtree.
959 * root must be expanded
961 static VOID
962 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
964 TREEVIEW_ITEM *sibling;
965 HDC hdc;
966 HFONT hOldFont;
968 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
969 return;
971 root->state &= ~TVIS_EXPANDED;
972 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
973 root->state |= TVIS_EXPANDED;
975 hdc = GetDC(infoPtr->hwnd);
976 hOldFont = SelectObject(hdc, infoPtr->hFont);
978 for (; root != sibling;
979 root = TREEVIEW_GetNextListItem(infoPtr, root))
981 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
983 if (root->callbackMask & TVIF_TEXT)
984 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
986 if (root->textWidth == 0)
988 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
989 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
993 SelectObject(hdc, hOldFont);
994 ReleaseDC(infoPtr->hwnd, hdc);
997 /* Item Allocation **********************************************************/
999 static TREEVIEW_ITEM *
1000 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1002 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1004 if (!newItem)
1005 return NULL;
1007 /* I_IMAGENONE would make more sense but this is neither what is
1008 * documented (MSDN doesn't specify) nor what Windows actually does
1009 * (it sets it to zero)... and I can so imagine an application using
1010 * inc/dec to toggle the images. */
1011 newItem->iImage = 0;
1012 newItem->iSelectedImage = 0;
1013 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1014 newItem->infoPtr = infoPtr;
1016 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1018 Free(newItem);
1019 return NULL;
1022 return newItem;
1025 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1026 * free item->pszText. */
1027 static void
1028 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1030 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1031 if (infoPtr->selectedItem == item)
1032 infoPtr->selectedItem = NULL;
1033 if (infoPtr->hotItem == item)
1034 infoPtr->hotItem = NULL;
1035 if (infoPtr->focusedItem == item)
1036 infoPtr->focusedItem = NULL;
1037 if (infoPtr->firstVisible == item)
1038 infoPtr->firstVisible = NULL;
1039 if (infoPtr->dropItem == item)
1040 infoPtr->dropItem = NULL;
1041 if (infoPtr->insertMarkItem == item)
1042 infoPtr->insertMarkItem = NULL;
1043 Free(item);
1047 /* Item Insertion *******************************************************/
1049 /***************************************************************************
1050 * This method inserts newItem before sibling as a child of parent.
1051 * sibling can be NULL, but only if parent has no children.
1053 static void
1054 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1055 TREEVIEW_ITEM *parent)
1057 assert(parent != NULL);
1059 if (sibling != NULL)
1061 assert(sibling->parent == parent);
1063 if (sibling->prevSibling != NULL)
1064 sibling->prevSibling->nextSibling = newItem;
1066 newItem->prevSibling = sibling->prevSibling;
1067 sibling->prevSibling = newItem;
1069 else
1070 newItem->prevSibling = NULL;
1072 newItem->nextSibling = sibling;
1074 if (parent->firstChild == sibling)
1075 parent->firstChild = newItem;
1077 if (parent->lastChild == NULL)
1078 parent->lastChild = newItem;
1081 /***************************************************************************
1082 * This method inserts newItem after sibling as a child of parent.
1083 * sibling can be NULL, but only if parent has no children.
1085 static void
1086 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1087 TREEVIEW_ITEM *parent)
1089 assert(parent != NULL);
1091 if (sibling != NULL)
1093 assert(sibling->parent == parent);
1095 if (sibling->nextSibling != NULL)
1096 sibling->nextSibling->prevSibling = newItem;
1098 newItem->nextSibling = sibling->nextSibling;
1099 sibling->nextSibling = newItem;
1101 else
1102 newItem->nextSibling = NULL;
1104 newItem->prevSibling = sibling;
1106 if (parent->lastChild == sibling)
1107 parent->lastChild = newItem;
1109 if (parent->firstChild == NULL)
1110 parent->firstChild = newItem;
1113 static BOOL
1114 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1115 const TVITEMEXW *tvItem, BOOL isW)
1117 UINT callbackClear = 0;
1118 UINT callbackSet = 0;
1120 TRACE("item %p\n", item);
1121 /* Do this first in case it fails. */
1122 if (tvItem->mask & TVIF_TEXT)
1124 item->textWidth = 0; /* force width recalculation */
1125 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1127 int len;
1128 LPWSTR newText;
1129 if (isW)
1130 len = lstrlenW(tvItem->pszText) + 1;
1131 else
1132 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1134 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1136 if (newText == NULL) return FALSE;
1138 callbackClear |= TVIF_TEXT;
1140 item->pszText = newText;
1141 item->cchTextMax = len;
1142 if (isW)
1143 lstrcpynW(item->pszText, tvItem->pszText, len);
1144 else
1145 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1146 item->pszText, len);
1148 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1150 else
1152 callbackSet |= TVIF_TEXT;
1154 item->pszText = ReAlloc(item->pszText,
1155 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1156 item->cchTextMax = TEXT_CALLBACK_SIZE;
1157 TRACE("setting callback, item %p\n", item);
1161 if (tvItem->mask & TVIF_CHILDREN)
1163 item->cChildren = tvItem->cChildren;
1165 if (item->cChildren == I_CHILDRENCALLBACK)
1166 callbackSet |= TVIF_CHILDREN;
1167 else
1168 callbackClear |= TVIF_CHILDREN;
1171 if (tvItem->mask & TVIF_IMAGE)
1173 item->iImage = tvItem->iImage;
1175 if (item->iImage == I_IMAGECALLBACK)
1176 callbackSet |= TVIF_IMAGE;
1177 else
1178 callbackClear |= TVIF_IMAGE;
1181 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1183 item->iSelectedImage = tvItem->iSelectedImage;
1185 if (item->iSelectedImage == I_IMAGECALLBACK)
1186 callbackSet |= TVIF_SELECTEDIMAGE;
1187 else
1188 callbackClear |= TVIF_SELECTEDIMAGE;
1191 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1193 item->iExpandedImage = tvItem->iExpandedImage;
1195 if (item->iExpandedImage == I_IMAGECALLBACK)
1196 callbackSet |= TVIF_EXPANDEDIMAGE;
1197 else
1198 callbackClear |= TVIF_EXPANDEDIMAGE;
1201 if (tvItem->mask & TVIF_PARAM)
1202 item->lParam = tvItem->lParam;
1204 /* If the application sets TVIF_INTEGRAL without
1205 * supplying a TVITEMEX structure, it's toast. */
1206 if (tvItem->mask & TVIF_INTEGRAL)
1207 item->iIntegral = tvItem->iIntegral;
1209 if (tvItem->mask & TVIF_STATE)
1211 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1212 tvItem->stateMask);
1213 item->state &= ~tvItem->stateMask;
1214 item->state |= (tvItem->state & tvItem->stateMask);
1217 if (tvItem->mask & TVIF_STATEEX)
1219 FIXME("New extended state: %x\n", tvItem->uStateEx);
1222 item->callbackMask |= callbackSet;
1223 item->callbackMask &= ~callbackClear;
1225 return TRUE;
1228 /* Note that the new item is pre-zeroed. */
1229 static LRESULT
1230 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1232 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1233 HTREEITEM insertAfter;
1234 TREEVIEW_ITEM *newItem, *parentItem;
1235 BOOL bTextUpdated = FALSE;
1237 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1239 parentItem = infoPtr->root;
1241 else
1243 parentItem = ptdi->hParent;
1245 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1247 WARN("invalid parent %p\n", parentItem);
1248 return 0;
1252 insertAfter = ptdi->hInsertAfter;
1254 /* Validate this now for convenience. */
1255 switch ((DWORD_PTR)insertAfter)
1257 case (DWORD_PTR)TVI_FIRST:
1258 case (DWORD_PTR)TVI_LAST:
1259 case (DWORD_PTR)TVI_SORT:
1260 break;
1262 default:
1263 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1264 insertAfter->parent != parentItem)
1266 WARN("invalid insert after %p\n", insertAfter);
1267 insertAfter = TVI_LAST;
1271 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1272 (tvItem->mask & TVIF_TEXT)
1273 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1274 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1275 : "<no label>");
1277 newItem = TREEVIEW_AllocateItem(infoPtr);
1278 if (newItem == NULL)
1279 return 0;
1281 newItem->parent = parentItem;
1282 newItem->iIntegral = 1;
1283 newItem->visibleOrder = -1;
1285 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1286 return 0;
1288 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1290 infoPtr->uNumItems++;
1292 switch ((DWORD_PTR)insertAfter)
1294 case (DWORD_PTR)TVI_FIRST:
1296 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1297 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1298 if (infoPtr->firstVisible == originalFirst)
1299 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1301 break;
1303 case (DWORD_PTR)TVI_LAST:
1304 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1305 break;
1307 /* hInsertAfter names a specific item we want to insert after */
1308 default:
1309 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1310 break;
1312 case (DWORD_PTR)TVI_SORT:
1314 TREEVIEW_ITEM *aChild;
1315 TREEVIEW_ITEM *previousChild = NULL;
1316 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1317 BOOL bItemInserted = FALSE;
1319 aChild = parentItem->firstChild;
1321 bTextUpdated = TRUE;
1322 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1324 /* Iterate the parent children to see where we fit in */
1325 while (aChild != NULL)
1327 INT comp;
1329 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1330 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1332 if (comp < 0) /* we are smaller than the current one */
1334 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1335 if (infoPtr->firstVisible == originalFirst &&
1336 aChild == originalFirst)
1337 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1338 bItemInserted = TRUE;
1339 break;
1341 else if (comp > 0) /* we are bigger than the current one */
1343 previousChild = aChild;
1345 /* This will help us to exit if there is no more sibling */
1346 aChild = (aChild->nextSibling == 0)
1347 ? NULL
1348 : aChild->nextSibling;
1350 /* Look at the next item */
1351 continue;
1353 else if (comp == 0)
1356 * An item with this name is already existing, therefore,
1357 * we add after the one we found
1359 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1360 bItemInserted = TRUE;
1361 break;
1366 * we reach the end of the child list and the item has not
1367 * yet been inserted, therefore, insert it after the last child.
1369 if ((!bItemInserted) && (aChild == NULL))
1370 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1372 break;
1377 TRACE("new item %p; parent %p, mask %x\n", newItem,
1378 newItem->parent, tvItem->mask);
1380 newItem->iLevel = newItem->parent->iLevel + 1;
1382 if (newItem->parent->cChildren == 0)
1383 newItem->parent->cChildren = 1;
1385 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1387 if (STATEIMAGEINDEX(newItem->state) == 0)
1388 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1391 if (infoPtr->firstVisible == NULL)
1392 infoPtr->firstVisible = newItem;
1394 TREEVIEW_VerifyTree(infoPtr);
1396 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1398 if (parentItem == infoPtr->root ||
1399 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1401 TREEVIEW_ITEM *item;
1402 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1404 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1405 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1407 if (!bTextUpdated)
1408 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1410 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1411 TREEVIEW_UpdateScrollBars(infoPtr);
1413 * if the item was inserted in a visible part of the tree,
1414 * invalidate it, as well as those after it
1416 for (item = newItem;
1417 item != NULL;
1418 item = TREEVIEW_GetNextListItem(infoPtr, item))
1419 TREEVIEW_Invalidate(infoPtr, item);
1421 else
1423 /* refresh treeview if newItem is the first item inserted under parentItem */
1424 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1426 /* parent got '+' - update it */
1427 TREEVIEW_Invalidate(infoPtr, parentItem);
1431 return (LRESULT)newItem;
1434 /* Item Deletion ************************************************************/
1435 static void
1436 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1438 static void
1439 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1441 TREEVIEW_ITEM *kill = parentItem->firstChild;
1443 while (kill != NULL)
1445 TREEVIEW_ITEM *next = kill->nextSibling;
1447 TREEVIEW_RemoveItem(infoPtr, kill);
1449 kill = next;
1452 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1453 assert(parentItem->firstChild == NULL);
1454 assert(parentItem->lastChild == NULL);
1457 static void
1458 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1460 TREEVIEW_ITEM *parentItem = item->parent;
1462 assert(item != NULL);
1463 assert(item->parent != NULL); /* i.e. it must not be the root */
1465 if (parentItem->firstChild == item)
1466 parentItem->firstChild = item->nextSibling;
1468 if (parentItem->lastChild == item)
1469 parentItem->lastChild = item->prevSibling;
1471 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1472 && parentItem->cChildren > 0)
1473 parentItem->cChildren = 0;
1475 if (item->prevSibling)
1476 item->prevSibling->nextSibling = item->nextSibling;
1478 if (item->nextSibling)
1479 item->nextSibling->prevSibling = item->prevSibling;
1482 static void
1483 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1485 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1487 if (item->firstChild)
1488 TREEVIEW_RemoveAllChildren(infoPtr, item);
1490 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1491 TVIF_HANDLE | TVIF_PARAM, item, 0);
1493 TREEVIEW_UnlinkItem(item);
1495 infoPtr->uNumItems--;
1497 if (item->pszText != LPSTR_TEXTCALLBACKW)
1498 Free(item->pszText);
1500 TREEVIEW_FreeItem(infoPtr, item);
1504 /* Empty out the tree. */
1505 static void
1506 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1508 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1510 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1513 static LRESULT
1514 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1516 TREEVIEW_ITEM *newSelection = NULL;
1517 TREEVIEW_ITEM *newFirstVisible = NULL;
1518 TREEVIEW_ITEM *parent, *prev = NULL;
1519 BOOL visible = FALSE;
1521 if (item == TVI_ROOT || !item)
1523 TRACE("TVI_ROOT\n");
1524 parent = infoPtr->root;
1525 newSelection = NULL;
1526 visible = TRUE;
1527 TREEVIEW_RemoveTree(infoPtr);
1529 else
1531 if (!TREEVIEW_ValidItem(infoPtr, item))
1532 return FALSE;
1534 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1535 parent = item->parent;
1537 if (ISVISIBLE(item))
1539 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1540 visible = TRUE;
1543 if (infoPtr->selectedItem != NULL
1544 && (item == infoPtr->selectedItem
1545 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1547 if (item->nextSibling)
1548 newSelection = item->nextSibling;
1549 else if (item->parent != infoPtr->root)
1550 newSelection = item->parent;
1551 else
1552 newSelection = item->prevSibling;
1553 TRACE("newSelection = %p\n", newSelection);
1556 if (infoPtr->firstVisible == item)
1558 if (item->nextSibling)
1559 newFirstVisible = item->nextSibling;
1560 else if (item->prevSibling)
1561 newFirstVisible = item->prevSibling;
1562 else if (item->parent != infoPtr->root)
1563 newFirstVisible = item->parent;
1564 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1566 else
1567 newFirstVisible = infoPtr->firstVisible;
1569 TREEVIEW_RemoveItem(infoPtr, item);
1572 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1573 if (!infoPtr->selectedItem && newSelection)
1575 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1576 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1579 /* Validate insertMark dropItem.
1580 * hotItem ??? - used for comparison only.
1582 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1583 infoPtr->insertMarkItem = 0;
1585 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1586 infoPtr->dropItem = 0;
1588 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1589 newFirstVisible = infoPtr->root->firstChild;
1591 TREEVIEW_VerifyTree(infoPtr);
1593 if (!infoPtr->bRedraw) return TRUE;
1595 if (visible)
1597 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1598 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1599 TREEVIEW_UpdateScrollBars(infoPtr);
1600 TREEVIEW_Invalidate(infoPtr, NULL);
1602 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1604 /* parent lost '+/-' - update it */
1605 TREEVIEW_Invalidate(infoPtr, parent);
1608 return TRUE;
1612 /* Get/Set Messages *********************************************************/
1613 static LRESULT
1614 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1616 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1618 if (infoPtr->bRedraw)
1620 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1621 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1622 TREEVIEW_UpdateScrollBars(infoPtr);
1623 TREEVIEW_Invalidate(infoPtr, NULL);
1625 return 0;
1628 static LRESULT
1629 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1631 TRACE("\n");
1632 return infoPtr->uIndent;
1635 static LRESULT
1636 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1638 TRACE("\n");
1640 if (newIndent < MINIMUM_INDENT)
1641 newIndent = MINIMUM_INDENT;
1643 if (infoPtr->uIndent != newIndent)
1645 infoPtr->uIndent = newIndent;
1646 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1647 TREEVIEW_UpdateScrollBars(infoPtr);
1648 TREEVIEW_Invalidate(infoPtr, NULL);
1651 return 0;
1655 static LRESULT
1656 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1658 TRACE("\n");
1659 return (LRESULT)infoPtr->hwndToolTip;
1662 static LRESULT
1663 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1665 HWND prevToolTip;
1667 TRACE("\n");
1668 prevToolTip = infoPtr->hwndToolTip;
1669 infoPtr->hwndToolTip = hwndTT;
1671 return (LRESULT)prevToolTip;
1674 static LRESULT
1675 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1677 BOOL rc = infoPtr->bNtfUnicode;
1678 infoPtr->bNtfUnicode = fUnicode;
1679 return rc;
1682 static LRESULT
1683 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1685 return infoPtr->bNtfUnicode;
1688 static LRESULT
1689 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1691 return infoPtr->uScrollTime;
1694 static LRESULT
1695 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1697 UINT uOldScrollTime = infoPtr->uScrollTime;
1699 infoPtr->uScrollTime = min(uScrollTime, 100);
1701 return uOldScrollTime;
1705 static LRESULT
1706 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1708 TRACE("\n");
1710 switch (wParam)
1712 case TVSIL_NORMAL:
1713 return (LRESULT)infoPtr->himlNormal;
1715 case TVSIL_STATE:
1716 return (LRESULT)infoPtr->himlState;
1718 default:
1719 return 0;
1723 #define TVHEIGHT_MIN 16
1724 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1726 /* Compute the natural height for items. */
1727 static UINT
1728 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1730 TEXTMETRICW tm;
1731 HDC hdc = GetDC(0);
1732 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1733 UINT height;
1735 /* Height is the maximum of:
1736 * 16 (a hack because our fonts are tiny), and
1737 * The text height + border & margin, and
1738 * The size of the normal image list
1740 GetTextMetricsW(hdc, &tm);
1741 SelectObject(hdc, hOldFont);
1742 ReleaseDC(0, hdc);
1744 height = TVHEIGHT_MIN;
1745 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1746 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1747 if (height < infoPtr->normalImageHeight)
1748 height = infoPtr->normalImageHeight;
1750 /* Round down, unless we support odd ("non even") heights. */
1751 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1752 height &= ~1;
1754 return height;
1757 static LRESULT
1758 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1760 HIMAGELIST himlOld = 0;
1761 int oldWidth = infoPtr->normalImageWidth;
1762 int oldHeight = infoPtr->normalImageHeight;
1764 TRACE("%u,%p\n", type, himlNew);
1766 switch (type)
1768 case TVSIL_NORMAL:
1769 himlOld = infoPtr->himlNormal;
1770 infoPtr->himlNormal = himlNew;
1772 if (himlNew)
1773 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1774 &infoPtr->normalImageHeight);
1775 else
1777 infoPtr->normalImageWidth = 0;
1778 infoPtr->normalImageHeight = 0;
1781 break;
1783 case TVSIL_STATE:
1784 himlOld = infoPtr->himlState;
1785 infoPtr->himlState = himlNew;
1787 if (himlNew)
1788 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1789 &infoPtr->stateImageHeight);
1790 else
1792 infoPtr->stateImageWidth = 0;
1793 infoPtr->stateImageHeight = 0;
1796 break;
1798 default:
1799 ERR("unknown imagelist type %u\n", type);
1802 if (oldWidth != infoPtr->normalImageWidth ||
1803 oldHeight != infoPtr->normalImageHeight)
1805 BOOL bRecalcVisible = FALSE;
1807 if (oldHeight != infoPtr->normalImageHeight &&
1808 !infoPtr->bHeightSet)
1810 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1811 bRecalcVisible = TRUE;
1814 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1815 infoPtr->normalImageWidth != infoPtr->uIndent)
1817 infoPtr->uIndent = infoPtr->normalImageWidth;
1818 bRecalcVisible = TRUE;
1821 if (bRecalcVisible)
1822 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1824 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1825 TREEVIEW_UpdateScrollBars(infoPtr);
1828 TREEVIEW_Invalidate(infoPtr, NULL);
1830 return (LRESULT)himlOld;
1833 static LRESULT
1834 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1836 INT prevHeight = infoPtr->uItemHeight;
1838 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1839 if (newHeight == -1)
1841 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1842 infoPtr->bHeightSet = FALSE;
1844 else
1846 if (newHeight == 0) newHeight = 1;
1847 infoPtr->uItemHeight = newHeight;
1848 infoPtr->bHeightSet = TRUE;
1851 /* Round down, unless we support odd ("non even") heights. */
1852 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1854 infoPtr->uItemHeight &= ~1;
1855 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1858 if (infoPtr->uItemHeight != prevHeight)
1860 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1861 TREEVIEW_UpdateScrollBars(infoPtr);
1862 TREEVIEW_Invalidate(infoPtr, NULL);
1865 return prevHeight;
1868 static LRESULT
1869 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1871 TRACE("\n");
1872 return infoPtr->uItemHeight;
1876 static LRESULT
1877 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1879 TRACE("%p\n", infoPtr->hFont);
1880 return (LRESULT)infoPtr->hFont;
1884 static INT CALLBACK
1885 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1887 (void)unused;
1889 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1891 return 1;
1894 static LRESULT
1895 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1897 UINT uHeight = infoPtr->uItemHeight;
1899 TRACE("%p %i\n", hFont, bRedraw);
1901 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1903 DeleteObject(infoPtr->hBoldFont);
1904 DeleteObject(infoPtr->hUnderlineFont);
1905 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1906 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1908 if (!infoPtr->bHeightSet)
1909 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1911 if (uHeight != infoPtr->uItemHeight)
1912 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1914 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1916 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1917 TREEVIEW_UpdateScrollBars(infoPtr);
1919 if (bRedraw)
1920 TREEVIEW_Invalidate(infoPtr, NULL);
1922 return 0;
1926 static LRESULT
1927 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1929 TRACE("\n");
1930 return (LRESULT)infoPtr->clrLine;
1933 static LRESULT
1934 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1936 COLORREF prevColor = infoPtr->clrLine;
1938 TRACE("\n");
1939 infoPtr->clrLine = color;
1940 return (LRESULT)prevColor;
1944 static LRESULT
1945 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1947 TRACE("\n");
1948 return (LRESULT)infoPtr->clrText;
1951 static LRESULT
1952 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1954 COLORREF prevColor = infoPtr->clrText;
1956 TRACE("\n");
1957 infoPtr->clrText = color;
1959 if (infoPtr->clrText != prevColor)
1960 TREEVIEW_Invalidate(infoPtr, NULL);
1962 return (LRESULT)prevColor;
1966 static LRESULT
1967 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1969 TRACE("\n");
1970 return (LRESULT)infoPtr->clrBk;
1973 static LRESULT
1974 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1976 COLORREF prevColor = infoPtr->clrBk;
1978 TRACE("\n");
1979 infoPtr->clrBk = newColor;
1981 if (newColor != prevColor)
1982 TREEVIEW_Invalidate(infoPtr, NULL);
1984 return (LRESULT)prevColor;
1988 static LRESULT
1989 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1991 TRACE("\n");
1992 return (LRESULT)infoPtr->clrInsertMark;
1995 static LRESULT
1996 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1998 COLORREF prevColor = infoPtr->clrInsertMark;
2000 TRACE("%x\n", color);
2001 infoPtr->clrInsertMark = color;
2003 return (LRESULT)prevColor;
2007 static LRESULT
2008 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
2010 TRACE("%d %p\n", wParam, item);
2012 if (!TREEVIEW_ValidItem(infoPtr, item))
2013 return 0;
2015 infoPtr->insertBeforeorAfter = wParam;
2016 infoPtr->insertMarkItem = item;
2018 TREEVIEW_Invalidate(infoPtr, NULL);
2020 return 1;
2024 /************************************************************************
2025 * Some serious braindamage here. lParam is a pointer to both the
2026 * input HTREEITEM and the output RECT.
2028 static LRESULT
2029 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2031 TREEVIEW_ITEM *item;
2032 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2034 TRACE("\n");
2036 if (pItem == NULL)
2037 return FALSE;
2039 item = *pItem;
2040 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2041 return FALSE;
2044 * If wParam is TRUE return the text size otherwise return
2045 * the whole item size
2047 if (fTextRect)
2049 /* Windows does not send TVN_GETDISPINFO here. */
2051 lpRect->top = item->rect.top;
2052 lpRect->bottom = item->rect.bottom;
2054 lpRect->left = item->textOffset;
2055 if (!item->textWidth)
2056 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2058 lpRect->right = item->textOffset + item->textWidth + 4;
2060 else
2062 *lpRect = item->rect;
2065 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2067 return TRUE;
2070 static inline LRESULT
2071 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2073 /* Surprise! This does not take integral height into account. */
2074 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2075 return infoPtr->clientHeight / infoPtr->uItemHeight;
2079 static LRESULT
2080 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2082 TREEVIEW_ITEM *item = tvItem->hItem;
2084 if (!TREEVIEW_ValidItem(infoPtr, item))
2086 if (!item) return FALSE;
2088 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2089 infoPtr = item->infoPtr;
2090 if (!TREEVIEW_ValidItem(infoPtr, item)) return FALSE;
2093 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2095 if (tvItem->mask & TVIF_CHILDREN)
2097 if (item->cChildren==I_CHILDRENCALLBACK)
2098 FIXME("I_CHILDRENCALLBACK not supported\n");
2099 tvItem->cChildren = item->cChildren;
2102 if (tvItem->mask & TVIF_HANDLE)
2103 tvItem->hItem = item;
2105 if (tvItem->mask & TVIF_IMAGE)
2106 tvItem->iImage = item->iImage;
2108 if (tvItem->mask & TVIF_INTEGRAL)
2109 tvItem->iIntegral = item->iIntegral;
2111 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2112 tvItem->lParam = item->lParam;
2114 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2115 tvItem->iSelectedImage = item->iSelectedImage;
2117 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2118 tvItem->iExpandedImage = item->iExpandedImage;
2120 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2121 tvItem->state = item->state;
2123 if (tvItem->mask & TVIF_TEXT)
2125 if (item->pszText == NULL)
2127 if (tvItem->cchTextMax > 0)
2128 tvItem->pszText[0] = '\0';
2130 else if (isW)
2132 if (item->pszText == LPSTR_TEXTCALLBACKW)
2134 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2135 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2137 else
2139 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2142 else
2144 if (item->pszText == LPSTR_TEXTCALLBACKW)
2146 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2147 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2149 else
2151 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2152 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2157 if (tvItem->mask & TVIF_STATEEX)
2159 FIXME("Extended item state not supported, returning 0.\n");
2160 tvItem->uStateEx = 0;
2163 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2164 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2166 return TRUE;
2169 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2170 * which is wrong. */
2171 static LRESULT
2172 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2174 TREEVIEW_ITEM *item;
2175 TREEVIEW_ITEM originalItem;
2177 item = tvItem->hItem;
2179 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2180 tvItem->mask);
2182 if (!TREEVIEW_ValidItem(infoPtr, item))
2183 return FALSE;
2185 /* store the original item values */
2186 originalItem = *item;
2188 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2189 return FALSE;
2191 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2192 if ((tvItem->mask & TVIF_TEXT
2193 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2194 && ISVISIBLE(item))
2196 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2197 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2200 if (tvItem->mask != 0 && ISVISIBLE(item))
2202 /* The refresh updates everything, but we can't wait until then. */
2203 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2205 /* if any of the item's values changed and it's not a callback, redraw the item */
2206 if (item_changed(&originalItem, item, tvItem))
2208 if (tvItem->mask & TVIF_INTEGRAL)
2210 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2211 TREEVIEW_UpdateScrollBars(infoPtr);
2213 TREEVIEW_Invalidate(infoPtr, NULL);
2215 else
2217 TREEVIEW_UpdateScrollBars(infoPtr);
2218 TREEVIEW_Invalidate(infoPtr, item);
2223 return TRUE;
2226 static LRESULT
2227 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2229 TRACE("\n");
2231 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2232 return 0;
2234 return (item->state & mask);
2237 static LRESULT
2238 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2240 TREEVIEW_ITEM *retval;
2242 retval = 0;
2244 /* handle all the global data here */
2245 switch (which)
2247 case TVGN_CHILD: /* Special case: child of 0 is root */
2248 if (item)
2249 break;
2250 /* fall through */
2251 case TVGN_ROOT:
2252 retval = infoPtr->root->firstChild;
2253 break;
2255 case TVGN_CARET:
2256 retval = infoPtr->selectedItem;
2257 break;
2259 case TVGN_FIRSTVISIBLE:
2260 retval = infoPtr->firstVisible;
2261 break;
2263 case TVGN_DROPHILITE:
2264 retval = infoPtr->dropItem;
2265 break;
2267 case TVGN_LASTVISIBLE:
2268 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2269 break;
2272 if (retval)
2274 TRACE("flags:%x, returns %p\n", which, retval);
2275 return (LRESULT)retval;
2278 if (item == TVI_ROOT) item = infoPtr->root;
2280 if (!TREEVIEW_ValidItem(infoPtr, item))
2281 return FALSE;
2283 switch (which)
2285 case TVGN_NEXT:
2286 retval = item->nextSibling;
2287 break;
2288 case TVGN_PREVIOUS:
2289 retval = item->prevSibling;
2290 break;
2291 case TVGN_PARENT:
2292 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2293 break;
2294 case TVGN_CHILD:
2295 retval = item->firstChild;
2296 break;
2297 case TVGN_NEXTVISIBLE:
2298 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2299 break;
2300 case TVGN_PREVIOUSVISIBLE:
2301 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2302 break;
2303 default:
2304 TRACE("Unknown msg %x,item %p\n", which, item);
2305 break;
2308 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2309 return (LRESULT)retval;
2313 static LRESULT
2314 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2316 TRACE(" %d\n", infoPtr->uNumItems);
2317 return (LRESULT)infoPtr->uNumItems;
2320 static VOID
2321 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2323 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2325 static const unsigned int state_table[] = { 0, 2, 1 };
2327 unsigned int state;
2329 state = STATEIMAGEINDEX(item->state);
2330 TRACE("state:%x\n", state);
2331 item->state &= ~TVIS_STATEIMAGEMASK;
2333 if (state < 3)
2334 state = state_table[state];
2336 item->state |= INDEXTOSTATEIMAGEMASK(state);
2338 TRACE("state:%x\n", state);
2339 TREEVIEW_Invalidate(infoPtr, item);
2344 /* Painting *************************************************************/
2346 /* Draw the lines and expand button for an item. Also draws one section
2347 * of the line from item's parent to item's parent's next sibling. */
2348 static void
2349 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2351 LONG centerx, centery;
2352 BOOL lar = ((infoPtr->dwStyle
2353 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2354 > TVS_LINESATROOT);
2355 HBRUSH hbr, hbrOld;
2356 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2358 if (!lar && item->iLevel == 0)
2359 return;
2361 hbr = CreateSolidBrush(clrBk);
2362 hbrOld = SelectObject(hdc, hbr);
2364 centerx = (item->linesOffset + item->stateOffset) / 2;
2365 centery = (item->rect.top + item->rect.bottom) / 2;
2367 if (infoPtr->dwStyle & TVS_HASLINES)
2369 HPEN hOldPen, hNewPen;
2370 HTREEITEM parent;
2371 LOGBRUSH lb;
2373 /* Get a dotted grey pen */
2374 lb.lbStyle = BS_SOLID;
2375 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2376 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2377 hOldPen = SelectObject(hdc, hNewPen);
2379 /* Make sure the center is on a dot (using +2 instead
2380 * of +1 gives us pixel-by-pixel compat with native) */
2381 centery = (centery + 2) & ~1;
2383 MoveToEx(hdc, item->stateOffset, centery, NULL);
2384 LineTo(hdc, centerx - 1, centery);
2386 if (item->prevSibling || item->parent != infoPtr->root)
2388 MoveToEx(hdc, centerx, item->rect.top, NULL);
2389 LineTo(hdc, centerx, centery);
2392 if (item->nextSibling)
2394 MoveToEx(hdc, centerx, centery, NULL);
2395 LineTo(hdc, centerx, item->rect.bottom + 1);
2398 /* Draw the line from our parent to its next sibling. */
2399 parent = item->parent;
2400 while (parent != infoPtr->root)
2402 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2404 if (parent->nextSibling
2405 /* skip top-levels unless TVS_LINESATROOT */
2406 && parent->stateOffset > parent->linesOffset)
2408 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2409 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2412 parent = parent->parent;
2415 SelectObject(hdc, hOldPen);
2416 DeleteObject(hNewPen);
2420 * Display the (+/-) signs
2423 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2425 if (item->cChildren)
2427 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2428 if (theme)
2430 RECT glyphRect = item->rect;
2431 glyphRect.left = item->linesOffset;
2432 glyphRect.right = item->stateOffset;
2433 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2434 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2435 &glyphRect, NULL);
2437 else
2439 LONG height = item->rect.bottom - item->rect.top;
2440 LONG width = item->stateOffset - item->linesOffset;
2441 LONG rectsize = min(height, width) / 4;
2442 /* plussize = ceil(rectsize * 3/4) */
2443 LONG plussize = (rectsize + 1) * 3 / 4;
2445 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2446 HPEN old_pen = SelectObject(hdc, new_pen);
2448 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2449 centerx + rectsize + 2, centery + rectsize + 2);
2451 SelectObject(hdc, old_pen);
2452 DeleteObject(new_pen);
2454 /* draw +/- signs with current text color */
2455 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2456 old_pen = SelectObject(hdc, new_pen);
2458 if (height < 18 || width < 18)
2460 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2461 LineTo(hdc, centerx + plussize, centery);
2463 if (!(item->state & TVIS_EXPANDED) ||
2464 (item->state & TVIS_EXPANDPARTIAL))
2466 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2467 LineTo(hdc, centerx, centery + plussize);
2470 else
2472 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2473 centerx + plussize, centery + 2);
2475 if (!(item->state & TVIS_EXPANDED) ||
2476 (item->state & TVIS_EXPANDPARTIAL))
2478 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2479 centerx + 2, centery + plussize);
2480 SetPixel(hdc, centerx - 1, centery, clrBk);
2481 SetPixel(hdc, centerx + 1, centery, clrBk);
2485 SelectObject(hdc, old_pen);
2486 DeleteObject(new_pen);
2490 SelectObject(hdc, hbrOld);
2491 DeleteObject(hbr);
2494 static void
2495 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2497 INT cditem;
2498 HFONT hOldFont;
2499 COLORREF oldTextColor, oldTextBkColor;
2500 int centery;
2501 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2502 NMTVCUSTOMDRAW nmcdhdr;
2504 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2506 /* - If item is drop target or it is selected and window is in focus -
2507 * use blue background (COLOR_HIGHLIGHT).
2508 * - If item is selected, window is not in focus, but it has style
2509 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2510 * - Otherwise - use background color
2512 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2513 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2514 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2516 if ((item->state & TVIS_DROPHILITED) || inFocus)
2518 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2519 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2521 else
2523 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2524 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2527 else
2529 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2530 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2531 nmcdhdr.clrText = comctl32_color.clrHighlight;
2532 else
2533 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2536 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2538 /* The custom draw handler can query the text rectangle,
2539 * so get ready. */
2540 /* should already be known, set to 0 when changed */
2541 if (!item->textWidth)
2542 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2544 cditem = 0;
2546 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2548 cditem = TREEVIEW_SendCustomDrawItemNotify
2549 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2550 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2552 if (cditem & CDRF_SKIPDEFAULT)
2554 SelectObject(hdc, hOldFont);
2555 return;
2559 if (cditem & CDRF_NEWFONT)
2560 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2562 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2564 /* Set colors. Custom draw handler can change these so we do this after it. */
2565 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2566 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2568 centery = (item->rect.top + item->rect.bottom) / 2;
2571 * Display the images associated with this item
2574 INT imageIndex;
2576 /* State images are displayed to the left of the Normal image
2577 * image number is in state; zero should be `display no image'.
2579 imageIndex = STATEIMAGEINDEX(item->state);
2581 if (infoPtr->himlState && imageIndex)
2583 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2584 item->stateOffset,
2585 centery - infoPtr->stateImageHeight / 2,
2586 ILD_NORMAL);
2589 /* Now, draw the normal image; can be either selected,
2590 * non-selected or expanded image.
2593 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2595 /* The item is currently selected */
2596 imageIndex = item->iSelectedImage;
2598 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2600 /* The item is currently not selected but expanded */
2601 imageIndex = item->iExpandedImage;
2603 else
2605 /* The item is not selected and not expanded */
2606 imageIndex = item->iImage;
2609 if (infoPtr->himlNormal)
2611 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2613 style |= item->state & TVIS_OVERLAYMASK;
2615 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2616 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2617 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2618 style);
2624 * Display the text associated with this item
2627 /* Don't paint item's text if it's being edited */
2628 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2630 if (item->pszText)
2632 RECT rcText;
2633 UINT align;
2634 SIZE sz;
2636 rcText.top = item->rect.top;
2637 rcText.bottom = item->rect.bottom;
2638 rcText.left = item->textOffset;
2639 rcText.right = rcText.left + item->textWidth + 4;
2641 TRACE("drawing text %s at (%s)\n",
2642 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2644 /* Draw it */
2645 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2647 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2648 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2649 ETO_CLIPPED | ETO_OPAQUE,
2650 &rcText,
2651 item->pszText,
2652 lstrlenW(item->pszText),
2653 NULL);
2654 SetTextAlign(hdc, align);
2656 /* Draw focus box around the selected item */
2657 if ((item == infoPtr->selectedItem) && inFocus)
2659 DrawFocusRect(hdc,&rcText);
2664 /* Draw insertion mark if necessary */
2666 if (infoPtr->insertMarkItem)
2667 TRACE("item:%d,mark:%p\n",
2668 TREEVIEW_GetItemIndex(infoPtr, item),
2669 infoPtr->insertMarkItem);
2671 if (item == infoPtr->insertMarkItem)
2673 HPEN hNewPen, hOldPen;
2674 int offset;
2675 int left, right;
2677 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2678 hOldPen = SelectObject(hdc, hNewPen);
2680 if (infoPtr->insertBeforeorAfter)
2681 offset = item->rect.bottom - 1;
2682 else
2683 offset = item->rect.top + 1;
2685 left = item->textOffset - 2;
2686 right = item->textOffset + item->textWidth + 2;
2688 MoveToEx(hdc, left, offset - 3, NULL);
2689 LineTo(hdc, left, offset + 4);
2691 MoveToEx(hdc, left, offset, NULL);
2692 LineTo(hdc, right + 1, offset);
2694 MoveToEx(hdc, right, offset + 3, NULL);
2695 LineTo(hdc, right, offset - 4);
2697 SelectObject(hdc, hOldPen);
2698 DeleteObject(hNewPen);
2701 if (cditem & CDRF_NOTIFYPOSTPAINT)
2703 cditem = TREEVIEW_SendCustomDrawItemNotify
2704 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2705 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2708 /* Restore the hdc state */
2709 SetTextColor(hdc, oldTextColor);
2710 SetBkColor(hdc, oldTextBkColor);
2711 SelectObject(hdc, hOldFont);
2714 /* Computes treeHeight and treeWidth and updates the scroll bars.
2716 static void
2717 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2719 TREEVIEW_ITEM *item;
2720 HWND hwnd = infoPtr->hwnd;
2721 BOOL vert = FALSE;
2722 BOOL horz = FALSE;
2723 SCROLLINFO si;
2724 LONG scrollX = infoPtr->scrollX;
2726 infoPtr->treeWidth = 0;
2727 infoPtr->treeHeight = 0;
2729 /* We iterate through all visible items in order to get the tree height
2730 * and width */
2731 item = infoPtr->root->firstChild;
2733 while (item != NULL)
2735 if (ISVISIBLE(item))
2737 /* actually we draw text at textOffset + 2 */
2738 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2739 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2741 /* This is scroll-adjusted, but we fix this below. */
2742 infoPtr->treeHeight = item->rect.bottom;
2745 item = TREEVIEW_GetNextListItem(infoPtr, item);
2748 /* Fix the scroll adjusted treeHeight and treeWidth. */
2749 if (infoPtr->root->firstChild)
2750 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2752 infoPtr->treeWidth += infoPtr->scrollX;
2754 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2756 /* Adding one scroll bar may take up enough space that it forces us
2757 * to add the other as well. */
2758 if (infoPtr->treeHeight > infoPtr->clientHeight)
2760 vert = TRUE;
2762 if (infoPtr->treeWidth
2763 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2764 horz = TRUE;
2766 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2767 horz = TRUE;
2769 if (!vert && horz && infoPtr->treeHeight
2770 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2771 vert = TRUE;
2773 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2775 si.cbSize = sizeof(SCROLLINFO);
2776 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2777 si.nMin = 0;
2779 if (vert)
2781 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2782 if ( si.nPage && NULL != infoPtr->firstVisible)
2784 si.nPos = infoPtr->firstVisible->visibleOrder;
2785 si.nMax = infoPtr->maxVisibleOrder - 1;
2787 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2789 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2790 ShowScrollBar(hwnd, SB_VERT, TRUE);
2791 infoPtr->uInternalStatus |= TV_VSCROLL;
2793 else
2795 if (infoPtr->uInternalStatus & TV_VSCROLL)
2796 ShowScrollBar(hwnd, SB_VERT, FALSE);
2797 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2800 else
2802 if (infoPtr->uInternalStatus & TV_VSCROLL)
2803 ShowScrollBar(hwnd, SB_VERT, FALSE);
2804 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2807 if (horz)
2809 si.nPage = infoPtr->clientWidth;
2810 si.nPos = infoPtr->scrollX;
2811 si.nMax = infoPtr->treeWidth - 1;
2813 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2815 si.nPos = si.nMax - max( si.nPage-1, 0 );
2816 scrollX = si.nPos;
2819 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2820 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2821 infoPtr->uInternalStatus |= TV_HSCROLL;
2823 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2824 TREEVIEW_HScroll(infoPtr,
2825 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2827 else
2829 if (infoPtr->uInternalStatus & TV_HSCROLL)
2830 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2831 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2833 scrollX = 0;
2834 if (infoPtr->scrollX != 0)
2836 TREEVIEW_HScroll(infoPtr,
2837 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2841 if (!horz)
2842 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2845 static void
2846 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2848 HBRUSH hBrush;
2849 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2851 hBrush = CreateSolidBrush(clrBk);
2852 FillRect(hdc, rc, hBrush);
2853 DeleteObject(hBrush);
2856 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2857 static LRESULT
2858 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2860 RECT rect;
2862 TRACE("%p\n", infoPtr);
2864 GetClientRect(infoPtr->hwnd, &rect);
2865 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2867 return 1;
2870 static void
2871 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2873 HWND hwnd = infoPtr->hwnd;
2874 RECT rect = *rc;
2875 TREEVIEW_ITEM *item;
2877 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2879 TRACE("empty window\n");
2880 return;
2883 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2884 hdc, rect);
2886 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2888 ReleaseDC(hwnd, hdc);
2889 return;
2892 for (item = infoPtr->root->firstChild;
2893 item != NULL;
2894 item = TREEVIEW_GetNextListItem(infoPtr, item))
2896 if (ISVISIBLE(item))
2898 /* Avoid unneeded calculations */
2899 if (item->rect.top > rect.bottom)
2900 break;
2901 if (item->rect.bottom < rect.top)
2902 continue;
2904 TREEVIEW_DrawItem(infoPtr, hdc, item);
2908 TREEVIEW_UpdateScrollBars(infoPtr);
2910 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2911 infoPtr->cdmode =
2912 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2915 static inline void
2916 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2918 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2921 static void
2922 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2924 if (item)
2925 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2926 else
2927 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2930 static LRESULT
2931 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2933 HDC hdc;
2934 PAINTSTRUCT ps;
2935 RECT rc;
2937 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2939 if (hdc_ref)
2941 hdc = hdc_ref;
2942 GetClientRect(infoPtr->hwnd, &rc);
2943 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2945 else
2947 hdc = BeginPaint(infoPtr->hwnd, &ps);
2948 rc = ps.rcPaint;
2949 if(ps.fErase)
2950 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2953 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2954 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2956 if (!hdc_ref)
2957 EndPaint(infoPtr->hwnd, &ps);
2959 return 0;
2962 static LRESULT
2963 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2965 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2967 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2968 return 0;
2970 if (options & PRF_ERASEBKGND)
2971 TREEVIEW_EraseBackground(infoPtr, hdc);
2973 if (options & PRF_CLIENT)
2975 RECT rc;
2976 GetClientRect(infoPtr->hwnd, &rc);
2977 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2980 return 0;
2983 /* Sorting **************************************************************/
2985 /***************************************************************************
2986 * Forward the DPA local callback to the treeview owner callback
2988 static INT WINAPI
2989 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2990 const TVSORTCB *pCallBackSort)
2992 /* Forward the call to the client-defined callback */
2993 return pCallBackSort->lpfnCompare(first->lParam,
2994 second->lParam,
2995 pCallBackSort->lParam);
2998 /***************************************************************************
2999 * Treeview native sort routine: sort on item text.
3001 static INT WINAPI
3002 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3003 const TREEVIEW_INFO *infoPtr)
3005 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3006 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3008 if(first->pszText && second->pszText)
3009 return lstrcmpiW(first->pszText, second->pszText);
3010 else if(first->pszText)
3011 return -1;
3012 else if(second->pszText)
3013 return 1;
3014 else
3015 return 0;
3018 /* Returns the number of physical children belonging to item. */
3019 static INT
3020 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3022 INT cChildren = 0;
3023 HTREEITEM hti;
3025 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3026 cChildren++;
3028 return cChildren;
3031 /* Returns a DPA containing a pointer to each physical child of item in
3032 * sibling order. If item has no children, an empty DPA is returned. */
3033 static HDPA
3034 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3036 HTREEITEM child;
3038 HDPA list = DPA_Create(8);
3039 if (list == 0) return NULL;
3041 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3043 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3045 DPA_Destroy(list);
3046 return NULL;
3050 return list;
3053 /***************************************************************************
3054 * Setup the treeview structure with regards of the sort method
3055 * and sort the children of the TV item specified in lParam
3056 * fRecurse: currently unused. Should be zero.
3057 * parent: if pSort!=NULL, should equal pSort->hParent.
3058 * otherwise, item which child items are to be sorted.
3059 * pSort: sort method info. if NULL, sort on item text.
3060 * if non-NULL, sort on item's lParam content, and let the
3061 * application decide what that means. See also TVM_SORTCHILDRENCB.
3064 static LRESULT
3065 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3066 LPTVSORTCB pSort)
3068 INT cChildren;
3069 PFNDPACOMPARE pfnCompare;
3070 LPARAM lpCompare;
3072 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3073 if (parent == TVI_ROOT || parent == NULL)
3074 parent = infoPtr->root;
3076 /* Check for a valid handle to the parent item */
3077 if (!TREEVIEW_ValidItem(infoPtr, parent))
3079 ERR("invalid item hParent=%p\n", parent);
3080 return FALSE;
3083 if (pSort)
3085 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3086 lpCompare = (LPARAM)pSort;
3088 else
3090 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3091 lpCompare = (LPARAM)infoPtr;
3094 cChildren = TREEVIEW_CountChildren(parent);
3096 /* Make sure there is something to sort */
3097 if (cChildren > 1)
3099 /* TREEVIEW_ITEM rechaining */
3100 INT count = 0;
3101 HTREEITEM item = 0;
3102 HTREEITEM nextItem = 0;
3103 HTREEITEM prevItem = 0;
3105 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3107 if (sortList == NULL)
3108 return FALSE;
3110 /* let DPA sort the list */
3111 DPA_Sort(sortList, pfnCompare, lpCompare);
3113 /* The order of DPA entries has been changed, so fixup the
3114 * nextSibling and prevSibling pointers. */
3116 item = DPA_GetPtr(sortList, count++);
3117 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3119 /* link the two current item together */
3120 item->nextSibling = nextItem;
3121 nextItem->prevSibling = item;
3123 if (prevItem == NULL)
3125 /* this is the first item, update the parent */
3126 parent->firstChild = item;
3127 item->prevSibling = NULL;
3129 else
3131 /* fix the back chaining */
3132 item->prevSibling = prevItem;
3135 /* get ready for the next one */
3136 prevItem = item;
3137 item = nextItem;
3140 /* the last item is pointed to by item and never has a sibling */
3141 item->nextSibling = NULL;
3142 parent->lastChild = item;
3144 DPA_Destroy(sortList);
3146 TREEVIEW_VerifyTree(infoPtr);
3148 if (parent->state & TVIS_EXPANDED)
3150 int visOrder = infoPtr->firstVisible->visibleOrder;
3152 if (parent == infoPtr->root)
3153 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3154 else
3155 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3157 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3159 TREEVIEW_ITEM *item;
3161 for (item = infoPtr->root->firstChild; item != NULL;
3162 item = TREEVIEW_GetNextListItem(infoPtr, item))
3164 if (item->visibleOrder == visOrder)
3165 break;
3168 if (!item) item = parent->firstChild;
3169 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3172 TREEVIEW_Invalidate(infoPtr, NULL);
3175 return TRUE;
3177 return FALSE;
3181 /***************************************************************************
3182 * Setup the treeview structure with regards of the sort method
3183 * and sort the children of the TV item specified in lParam
3185 static LRESULT
3186 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3188 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3192 /***************************************************************************
3193 * Sort the children of the TV item specified in lParam.
3195 static LRESULT
3196 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3198 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3202 /* Expansion/Collapse ***************************************************/
3204 static BOOL
3205 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3206 UINT action)
3208 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3209 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3210 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3211 0, item);
3214 static VOID
3215 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3216 UINT action)
3218 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3219 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3220 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3221 0, item);
3225 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3226 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3227 static BOOL
3228 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3229 BOOL bRemoveChildren, BOOL bUser)
3231 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3232 BOOL bSetSelection, bSetFirstVisible;
3233 RECT scrollRect;
3234 LONG scrollDist = 0;
3235 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3237 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3239 if (!(item->state & TVIS_EXPANDED))
3240 return FALSE;
3242 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3243 TREEVIEW_SendExpanding(infoPtr, item, action);
3245 if (item->firstChild == NULL)
3246 return FALSE;
3248 item->state &= ~TVIS_EXPANDED;
3250 if (bUser || !(item->state & TVIS_EXPANDEDONCE))
3251 TREEVIEW_SendExpanded(infoPtr, item, action);
3253 bSetSelection = (infoPtr->selectedItem != NULL
3254 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3256 bSetFirstVisible = (infoPtr->firstVisible != NULL
3257 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3259 tmpItem = item;
3260 while (tmpItem)
3262 if (tmpItem->nextSibling)
3264 nextItem = tmpItem->nextSibling;
3265 break;
3267 tmpItem = tmpItem->parent;
3270 if (nextItem)
3271 scrollDist = nextItem->rect.top;
3273 if (bRemoveChildren)
3275 INT old_cChildren = item->cChildren;
3276 TRACE("TVE_COLLAPSERESET\n");
3277 item->state &= ~TVIS_EXPANDEDONCE;
3278 TREEVIEW_RemoveAllChildren(infoPtr, item);
3279 item->cChildren = old_cChildren;
3282 if (item->firstChild)
3284 TREEVIEW_ITEM *i, *sibling;
3286 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3288 for (i = item->firstChild; i != sibling;
3289 i = TREEVIEW_GetNextListItem(infoPtr, i))
3291 i->visibleOrder = -1;
3295 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3297 if (nextItem)
3298 scrollDist = -(scrollDist - nextItem->rect.top);
3300 if (bSetSelection)
3302 /* Don't call DoSelectItem, it sends notifications. */
3303 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3304 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3305 item->state |= TVIS_SELECTED;
3306 infoPtr->selectedItem = item;
3309 TREEVIEW_UpdateScrollBars(infoPtr);
3311 scrollRect.left = 0;
3312 scrollRect.right = infoPtr->clientWidth;
3313 scrollRect.bottom = infoPtr->clientHeight;
3315 if (nextItem)
3317 scrollRect.top = nextItem->rect.top;
3319 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3320 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3321 TREEVIEW_Invalidate(infoPtr, item);
3322 } else {
3323 scrollRect.top = item->rect.top;
3324 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3327 TREEVIEW_SetFirstVisible(infoPtr,
3328 bSetFirstVisible ? item : infoPtr->firstVisible,
3329 TRUE);
3331 return TRUE;
3334 static BOOL
3335 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
3336 BOOL partial, BOOL user)
3338 LONG scrollDist;
3339 LONG orgNextTop = 0;
3340 RECT scrollRect;
3341 TREEVIEW_ITEM *nextItem, *tmpItem;
3342 BOOL sendsNotifications;
3344 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, item, partial, user);
3346 if (item->state & TVIS_EXPANDED)
3347 return TRUE;
3349 tmpItem = item; nextItem = NULL;
3350 while (tmpItem)
3352 if (tmpItem->nextSibling)
3354 nextItem = tmpItem->nextSibling;
3355 break;
3357 tmpItem = tmpItem->parent;
3360 if (nextItem)
3361 orgNextTop = nextItem->rect.top;
3363 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3365 sendsNotifications = user || ((item->cChildren != 0) &&
3366 !(item->state & TVIS_EXPANDEDONCE));
3367 if (sendsNotifications)
3369 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3371 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3372 return FALSE;
3375 if (!item->firstChild)
3376 return FALSE;
3378 item->state |= TVIS_EXPANDED;
3380 if (partial)
3381 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3383 if (ISVISIBLE(item))
3385 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
3386 TREEVIEW_UpdateSubTree(infoPtr, item);
3387 TREEVIEW_UpdateScrollBars(infoPtr);
3389 scrollRect.left = 0;
3390 scrollRect.bottom = infoPtr->treeHeight;
3391 scrollRect.right = infoPtr->clientWidth;
3392 if (nextItem)
3394 scrollDist = nextItem->rect.top - orgNextTop;
3395 scrollRect.top = orgNextTop;
3397 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3398 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3399 TREEVIEW_Invalidate (infoPtr, item);
3400 } else {
3401 scrollRect.top = item->rect.top;
3402 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3405 /* Scroll up so that as many children as possible are visible.
3406 * This fails when expanding causes an HScroll bar to appear, but we
3407 * don't know that yet, so the last item is obscured. */
3408 if (item->firstChild != NULL)
3410 int nChildren = item->lastChild->visibleOrder
3411 - item->firstChild->visibleOrder + 1;
3413 int visible_pos = item->visibleOrder
3414 - infoPtr->firstVisible->visibleOrder;
3416 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3418 if (visible_pos > 0 && nChildren > rows_below)
3420 int scroll = nChildren - rows_below;
3422 if (scroll > visible_pos)
3423 scroll = visible_pos;
3425 if (scroll > 0)
3427 TREEVIEW_ITEM *newFirstVisible
3428 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3429 scroll);
3432 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3438 if (sendsNotifications) {
3439 TREEVIEW_SendExpanded(infoPtr, item, TVE_EXPAND);
3440 item->state |= TVIS_EXPANDEDONCE;
3443 return TRUE;
3446 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3447 to mouse messages and TVM_SELECTITEM.
3449 selection - previously selected item, used to collapse a part of a tree
3450 item - new selected item
3452 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3453 HTREEITEM selection, HTREEITEM item)
3455 TREEVIEW_ITEM *SelItem;
3457 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3459 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3462 * Close the previous selection all the way to the root
3463 * as long as the new selection is not a child
3465 if(selection && (selection != item))
3467 BOOL closeit = TRUE;
3468 SelItem = item;
3470 /* determine if the hitItem is a child of the currently selected item */
3471 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3472 (SelItem->parent != infoPtr->root))
3474 closeit = (SelItem != selection);
3475 SelItem = SelItem->parent;
3478 if(closeit)
3480 if(TREEVIEW_ValidItem(infoPtr, selection))
3481 SelItem = selection;
3483 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3484 SelItem->parent != infoPtr->root)
3486 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3487 SelItem = SelItem->parent;
3493 * Expand the current item
3495 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3498 static BOOL
3499 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, BOOL user)
3501 TRACE("item=%p, user=%d\n", item, user);
3503 if (item->state & TVIS_EXPANDED)
3504 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3505 else
3506 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3509 static VOID
3510 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3512 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3514 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3516 if (TREEVIEW_HasChildren(infoPtr, item))
3517 TREEVIEW_ExpandAll(infoPtr, item);
3521 /* Note:If the specified item is the child of a collapsed parent item,
3522 the parent's list of child items is (recursively) expanded to reveal the
3523 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3524 know if it also applies here.
3527 static LRESULT
3528 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM item)
3530 if (!TREEVIEW_ValidItem(infoPtr, item))
3531 return 0;
3533 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3534 TREEVIEW_ItemName(item), TREEVIEW_GetItemIndex(infoPtr, item),
3535 flag, item->state);
3537 switch (flag & TVE_TOGGLE)
3539 case TVE_COLLAPSE:
3540 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3541 FALSE);
3543 case TVE_EXPAND:
3544 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3545 FALSE);
3547 case TVE_TOGGLE:
3548 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3550 default:
3551 return 0;
3555 /* Hit-Testing **********************************************************/
3557 static TREEVIEW_ITEM *
3558 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3560 TREEVIEW_ITEM *item;
3561 LONG row;
3563 if (!infoPtr->firstVisible)
3564 return NULL;
3566 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3568 for (item = infoPtr->firstVisible; item != NULL;
3569 item = TREEVIEW_GetNextListItem(infoPtr, item))
3571 if (row >= item->visibleOrder
3572 && row < item->visibleOrder + item->iIntegral)
3573 break;
3576 return item;
3579 static LRESULT
3580 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3582 TREEVIEW_ITEM *item;
3583 RECT rect;
3584 UINT status;
3585 LONG x, y;
3587 lpht->hItem = 0;
3588 GetClientRect(infoPtr->hwnd, &rect);
3589 status = 0;
3590 x = lpht->pt.x;
3591 y = lpht->pt.y;
3593 if (x < rect.left)
3595 status |= TVHT_TOLEFT;
3597 else if (x > rect.right)
3599 status |= TVHT_TORIGHT;
3602 if (y < rect.top)
3604 status |= TVHT_ABOVE;
3606 else if (y > rect.bottom)
3608 status |= TVHT_BELOW;
3611 if (status)
3613 lpht->flags = status;
3614 return 0;
3617 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3618 if (!item)
3620 lpht->flags = TVHT_NOWHERE;
3621 return 0;
3624 if (x >= item->textOffset + item->textWidth)
3626 lpht->flags = TVHT_ONITEMRIGHT;
3628 else if (x >= item->textOffset)
3630 lpht->flags = TVHT_ONITEMLABEL;
3632 else if (x >= item->imageOffset)
3634 lpht->flags = TVHT_ONITEMICON;
3636 else if (x >= item->stateOffset)
3638 lpht->flags = TVHT_ONITEMSTATEICON;
3640 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3642 lpht->flags = TVHT_ONITEMBUTTON;
3644 else
3646 lpht->flags = TVHT_ONITEMINDENT;
3649 lpht->hItem = item;
3650 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3652 return (LRESULT)item;
3655 /* Item Label Editing ***************************************************/
3657 static LRESULT
3658 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3660 return (LRESULT)infoPtr->hwndEdit;
3663 static LRESULT CALLBACK
3664 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3666 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3667 BOOL bCancel = FALSE;
3668 LRESULT rc;
3670 switch (uMsg)
3672 case WM_PAINT:
3673 TRACE("WM_PAINT start\n");
3674 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3675 lParam);
3676 TRACE("WM_PAINT done\n");
3677 return rc;
3679 case WM_KILLFOCUS:
3680 if (infoPtr->bIgnoreEditKillFocus)
3681 return TRUE;
3682 break;
3684 case WM_DESTROY:
3686 WNDPROC editProc = infoPtr->wpEditOrig;
3687 infoPtr->wpEditOrig = 0;
3688 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3689 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3692 case WM_GETDLGCODE:
3693 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3695 case WM_KEYDOWN:
3696 if (wParam == VK_ESCAPE)
3698 bCancel = TRUE;
3699 break;
3701 else if (wParam == VK_RETURN)
3703 break;
3706 /* fall through */
3707 default:
3708 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3711 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3712 /* eg. Using a messagebox */
3714 infoPtr->bIgnoreEditKillFocus = TRUE;
3715 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3716 infoPtr->bIgnoreEditKillFocus = FALSE;
3718 return 0;
3722 /* should handle edit control messages here */
3724 static LRESULT
3725 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3727 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3729 switch (HIWORD(wParam))
3731 case EN_UPDATE:
3734 * Adjust the edit window size
3736 WCHAR buffer[1024];
3737 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3738 HDC hdc = GetDC(infoPtr->hwndEdit);
3739 SIZE sz;
3740 HFONT hFont, hOldFont = 0;
3742 TRACE("edit=%p\n", infoPtr->hwndEdit);
3744 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3746 infoPtr->bLabelChanged = TRUE;
3748 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3750 /* Select font to get the right dimension of the string */
3751 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3753 if (hFont != 0)
3755 hOldFont = SelectObject(hdc, hFont);
3758 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3760 TEXTMETRICW textMetric;
3762 /* Add Extra spacing for the next character */
3763 GetTextMetricsW(hdc, &textMetric);
3764 sz.cx += (textMetric.tmMaxCharWidth * 2);
3766 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3767 sz.cx = min(sz.cx,
3768 infoPtr->clientWidth - editItem->textOffset + 2);
3770 SetWindowPos(infoPtr->hwndEdit,
3771 HWND_TOP,
3774 sz.cx,
3775 editItem->rect.bottom - editItem->rect.top + 3,
3776 SWP_NOMOVE | SWP_DRAWFRAME);
3779 if (hFont != 0)
3781 SelectObject(hdc, hOldFont);
3784 ReleaseDC(infoPtr->hwnd, hdc);
3785 break;
3787 case EN_KILLFOCUS:
3788 /* apparently we should respect passed handle value */
3789 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3791 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3792 break;
3794 default:
3795 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3798 return 0;
3801 static HWND
3802 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3804 HWND hwnd = infoPtr->hwnd;
3805 HWND hwndEdit;
3806 SIZE sz;
3807 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3808 HDC hdc;
3809 HFONT hOldFont=0;
3810 TEXTMETRICW textMetric;
3812 TRACE("%p %p\n", hwnd, hItem);
3813 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3814 return NULL;
3816 if (infoPtr->hwndEdit)
3817 return infoPtr->hwndEdit;
3819 infoPtr->bLabelChanged = FALSE;
3821 /* make edit item visible */
3822 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3824 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3826 hdc = GetDC(hwnd);
3827 /* Select the font to get appropriate metric dimensions */
3828 if (infoPtr->hFont != 0)
3830 hOldFont = SelectObject(hdc, infoPtr->hFont);
3833 /* Get string length in pixels */
3834 if (hItem->pszText)
3835 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3836 &sz);
3837 else
3838 GetTextExtentPoint32A(hdc, "", 0, &sz);
3840 /* Add Extra spacing for the next character */
3841 GetTextMetricsW(hdc, &textMetric);
3842 sz.cx += (textMetric.tmMaxCharWidth * 2);
3844 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3845 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3847 if (infoPtr->hFont != 0)
3849 SelectObject(hdc, hOldFont);
3852 ReleaseDC(hwnd, hdc);
3854 infoPtr->editItem = hItem;
3856 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3857 WC_EDITW,
3859 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3860 WS_CLIPSIBLINGS | ES_WANTRETURN |
3861 ES_LEFT, hItem->textOffset - 2,
3862 hItem->rect.top - 1, sz.cx + 3,
3863 hItem->rect.bottom -
3864 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3865 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3867 infoPtr->hwndEdit = hwndEdit;
3869 /* Get a 2D border. */
3870 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3871 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3872 SetWindowLongW(hwndEdit, GWL_STYLE,
3873 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3875 SendMessageW(hwndEdit, WM_SETFONT,
3876 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3878 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3879 (DWORD_PTR)
3880 TREEVIEW_Edit_SubclassProc);
3881 if (hItem->pszText)
3882 SetWindowTextW(hwndEdit, hItem->pszText);
3884 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3886 DestroyWindow(hwndEdit);
3887 infoPtr->hwndEdit = 0;
3888 infoPtr->editItem = NULL;
3889 return NULL;
3892 SetFocus(hwndEdit);
3893 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3894 ShowWindow(hwndEdit, SW_SHOW);
3896 return hwndEdit;
3900 static LRESULT
3901 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3903 HWND hwnd = infoPtr->hwnd;
3904 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3905 NMTVDISPINFOW tvdi;
3906 BOOL bCommit;
3907 WCHAR tmpText[1024] = { '\0' };
3908 WCHAR *newText = tmpText;
3909 int iLength = 0;
3911 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3913 tvdi.hdr.hwndFrom = hwnd;
3914 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3915 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3916 tvdi.item.mask = 0;
3917 tvdi.item.hItem = editedItem;
3918 tvdi.item.state = editedItem->state;
3919 tvdi.item.lParam = editedItem->lParam;
3921 if (!bCancel)
3923 if (!infoPtr->bNtfUnicode)
3924 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3925 else
3926 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3928 if (iLength >= 1023)
3930 ERR("Insufficient space to retrieve new item label\n");
3933 tvdi.item.mask = TVIF_TEXT;
3934 tvdi.item.pszText = tmpText;
3935 tvdi.item.cchTextMax = iLength + 1;
3937 else
3939 tvdi.item.pszText = NULL;
3940 tvdi.item.cchTextMax = 0;
3943 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, &tvdi.hdr);
3945 if (!bCancel && bCommit) /* Apply the changes */
3947 if (!infoPtr->bNtfUnicode)
3949 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3950 newText = Alloc(len * sizeof(WCHAR));
3951 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3952 iLength = len - 1;
3955 if (strcmpW(newText, editedItem->pszText) != 0)
3957 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3958 if (ptr == NULL)
3960 ERR("OutOfMemory, cannot allocate space for label\n");
3961 if(newText != tmpText) Free(newText);
3962 DestroyWindow(infoPtr->hwndEdit);
3963 infoPtr->hwndEdit = 0;
3964 infoPtr->editItem = NULL;
3965 return FALSE;
3967 else
3969 editedItem->pszText = ptr;
3970 editedItem->cchTextMax = iLength + 1;
3971 strcpyW(editedItem->pszText, newText);
3972 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3975 if(newText != tmpText) Free(newText);
3978 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3979 DestroyWindow(infoPtr->hwndEdit);
3980 infoPtr->hwndEdit = 0;
3981 infoPtr->editItem = NULL;
3982 return TRUE;
3985 static LRESULT
3986 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3988 if (wParam != TV_EDIT_TIMER)
3990 ERR("got unknown timer\n");
3991 return 1;
3994 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3995 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3997 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3999 return 0;
4003 /* Mouse Tracking/Drag **************************************************/
4005 /***************************************************************************
4006 * This is quite unusual piece of code, but that's how it's implemented in
4007 * Windows.
4009 static LRESULT
4010 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
4012 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4013 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4014 RECT r;
4015 MSG msg;
4017 r.top = pt.y - cyDrag;
4018 r.left = pt.x - cxDrag;
4019 r.bottom = pt.y + cyDrag;
4020 r.right = pt.x + cxDrag;
4022 SetCapture(infoPtr->hwnd);
4024 while (1)
4026 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4028 if (msg.message == WM_MOUSEMOVE)
4030 pt.x = (short)LOWORD(msg.lParam);
4031 pt.y = (short)HIWORD(msg.lParam);
4032 if (PtInRect(&r, pt))
4033 continue;
4034 else
4036 ReleaseCapture();
4037 return 1;
4040 else if (msg.message >= WM_LBUTTONDOWN &&
4041 msg.message <= WM_RBUTTONDBLCLK)
4043 if (msg.message == WM_RBUTTONUP)
4044 TREEVIEW_RButtonUp(infoPtr, &pt);
4045 break;
4048 DispatchMessageW(&msg);
4051 if (GetCapture() != infoPtr->hwnd)
4052 return 0;
4055 ReleaseCapture();
4056 return 0;
4060 static LRESULT
4061 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4063 TREEVIEW_ITEM *item;
4064 TVHITTESTINFO hit;
4066 TRACE("\n");
4067 SetFocus(infoPtr->hwnd);
4069 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4071 /* If there is pending 'edit label' event - kill it now */
4072 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4075 hit.pt.x = (short)LOWORD(lParam);
4076 hit.pt.y = (short)HIWORD(lParam);
4078 item = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4079 if (!item)
4080 return 0;
4081 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, item));
4083 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4084 { /* FIXME! */
4085 switch (hit.flags)
4087 case TVHT_ONITEMRIGHT:
4088 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4089 break;
4091 case TVHT_ONITEMINDENT:
4092 if (!(infoPtr->dwStyle & TVS_HASLINES))
4094 break;
4096 else
4098 int level = hit.pt.x / infoPtr->uIndent;
4099 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4101 while (item->iLevel > level)
4103 item = item->parent;
4106 /* fall through */
4109 case TVHT_ONITEMLABEL:
4110 case TVHT_ONITEMICON:
4111 case TVHT_ONITEMBUTTON:
4112 TREEVIEW_Toggle(infoPtr, item, TRUE);
4113 break;
4115 case TVHT_ONITEMSTATEICON:
4116 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4117 TREEVIEW_ToggleItemState(infoPtr, item);
4118 else
4119 TREEVIEW_Toggle(infoPtr, item, TRUE);
4120 break;
4123 return TRUE;
4127 static LRESULT
4128 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4130 HWND hwnd = infoPtr->hwnd;
4131 TVHITTESTINFO ht;
4132 BOOL bTrack, bDoLabelEdit;
4134 /* If Edit control is active - kill it and return.
4135 * The best way to do it is to set focus to itself.
4136 * Edit control subclassed procedure will automatically call
4137 * EndEditLabelNow.
4139 if (infoPtr->hwndEdit)
4141 SetFocus(hwnd);
4142 return 0;
4145 ht.pt.x = (short)LOWORD(lParam);
4146 ht.pt.y = (short)HIWORD(lParam);
4148 TREEVIEW_HitTest(infoPtr, &ht);
4149 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4151 /* update focusedItem and redraw both items */
4152 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4154 infoPtr->focusedItem = ht.hItem;
4155 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4156 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4159 bTrack = (ht.flags & TVHT_ONITEM)
4160 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4163 * If the style allows editing and the node is already selected
4164 * and the click occurred on the item label...
4166 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4167 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4169 /* Send NM_CLICK right away */
4170 if (!bTrack)
4171 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4172 goto setfocus;
4174 if (ht.flags & TVHT_ONITEMBUTTON)
4176 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4177 goto setfocus;
4179 else if (bTrack)
4180 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4181 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4183 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4184 infoPtr->dropItem = ht.hItem;
4186 /* clean up focusedItem as we dragged and won't select this item */
4187 if(infoPtr->focusedItem)
4189 /* refresh the item that was focused */
4190 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4191 infoPtr->focusedItem = NULL;
4193 /* refresh the selected item to return the filled background */
4194 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4197 return 0;
4201 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4202 goto setfocus;
4204 if (bDoLabelEdit)
4206 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4207 KillTimer(hwnd, TV_EDIT_TIMER);
4209 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4210 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4212 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4214 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4216 /* Select the current item */
4217 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4218 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4220 else if (ht.flags & TVHT_ONITEMSTATEICON)
4222 /* TVS_CHECKBOXES requires us to toggle the current state */
4223 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4224 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4227 setfocus:
4228 SetFocus(hwnd);
4229 return 0;
4233 static LRESULT
4234 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4236 TVHITTESTINFO ht;
4238 if (infoPtr->hwndEdit)
4240 SetFocus(infoPtr->hwnd);
4241 return 0;
4244 ht.pt.x = (short)LOWORD(lParam);
4245 ht.pt.y = (short)HIWORD(lParam);
4247 TREEVIEW_HitTest(infoPtr, &ht);
4249 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4251 if (ht.hItem)
4253 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4254 infoPtr->dropItem = ht.hItem;
4257 else
4259 SetFocus(infoPtr->hwnd);
4260 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4263 return 0;
4266 static LRESULT
4267 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4269 TVHITTESTINFO ht;
4271 ht.pt = *pPt;
4273 TREEVIEW_HitTest(infoPtr, &ht);
4275 if (ht.hItem)
4277 /* Change to screen coordinate for WM_CONTEXTMENU */
4278 ClientToScreen(infoPtr->hwnd, &ht.pt);
4280 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4281 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4282 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4284 return 0;
4288 static LRESULT
4289 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4291 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4292 INT cx, cy;
4293 HDC hdc, htopdc;
4294 HWND hwtop;
4295 HBITMAP hbmp, hOldbmp;
4296 SIZE size;
4297 RECT rc;
4298 HFONT hOldFont;
4300 TRACE("\n");
4302 if (!(infoPtr->himlNormal))
4303 return 0;
4305 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4306 return 0;
4308 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4310 hwtop = GetDesktopWindow();
4311 htopdc = GetDC(hwtop);
4312 hdc = CreateCompatibleDC(htopdc);
4314 hOldFont = SelectObject(hdc, infoPtr->hFont);
4316 if (dragItem->pszText)
4317 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4318 &size);
4319 else
4320 GetTextExtentPoint32A(hdc, "", 0, &size);
4322 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4323 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4324 hOldbmp = SelectObject(hdc, hbmp);
4326 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4327 size.cx += cx;
4328 if (cy > size.cy)
4329 size.cy = cy;
4331 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4332 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4333 ILD_NORMAL);
4336 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4337 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4340 /* draw item text */
4342 SetRect(&rc, cx, 0, size.cx, size.cy);
4344 if (dragItem->pszText)
4345 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4346 DT_LEFT);
4348 SelectObject(hdc, hOldFont);
4349 SelectObject(hdc, hOldbmp);
4351 ImageList_Add(infoPtr->dragList, hbmp, 0);
4353 DeleteDC(hdc);
4354 DeleteObject(hbmp);
4355 ReleaseDC(hwtop, htopdc);
4357 return (LRESULT)infoPtr->dragList;
4360 /* Selection ************************************************************/
4362 static LRESULT
4363 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4364 INT cause)
4366 TREEVIEW_ITEM *prevSelect;
4368 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4370 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4371 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4372 newSelect ? newSelect->state : 0);
4374 /* reset and redraw focusedItem if focusedItem was set so we don't */
4375 /* have to worry about the previously focused item when we set a new one */
4376 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4377 infoPtr->focusedItem = NULL;
4379 switch (action)
4381 case TVGN_CARET|TVSI_NOSINGLEEXPAND:
4382 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4383 /* Fall through */
4384 case TVGN_CARET:
4385 prevSelect = infoPtr->selectedItem;
4387 if (prevSelect == newSelect) {
4388 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4389 break;
4392 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4393 TVN_SELCHANGINGW,
4394 cause,
4395 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4396 prevSelect,
4397 newSelect))
4398 return FALSE;
4400 if (prevSelect)
4401 prevSelect->state &= ~TVIS_SELECTED;
4402 if (newSelect)
4403 newSelect->state |= TVIS_SELECTED;
4405 infoPtr->selectedItem = newSelect;
4407 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4409 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4410 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4412 TREEVIEW_SendTreeviewNotify(infoPtr,
4413 TVN_SELCHANGEDW,
4414 cause,
4415 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4416 prevSelect,
4417 newSelect);
4418 break;
4420 case TVGN_DROPHILITE:
4421 prevSelect = infoPtr->dropItem;
4423 if (prevSelect)
4424 prevSelect->state &= ~TVIS_DROPHILITED;
4426 infoPtr->dropItem = newSelect;
4428 if (newSelect)
4429 newSelect->state |= TVIS_DROPHILITED;
4431 TREEVIEW_Invalidate(infoPtr, prevSelect);
4432 TREEVIEW_Invalidate(infoPtr, newSelect);
4433 break;
4435 case TVGN_FIRSTVISIBLE:
4436 if (newSelect != NULL)
4438 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4439 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4440 TREEVIEW_Invalidate(infoPtr, NULL);
4442 break;
4445 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4446 return TRUE;
4449 /* FIXME: handle NM_KILLFOCUS etc */
4450 static LRESULT
4451 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4453 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4455 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4456 return FALSE;
4458 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4460 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4461 return FALSE;
4463 TREEVIEW_SingleExpand(infoPtr, selection, item);
4465 return TRUE;
4468 /*************************************************************************
4469 * TREEVIEW_ProcessLetterKeys
4471 * Processes keyboard messages generated by pressing the letter keys
4472 * on the keyboard.
4473 * What this does is perform a case insensitive search from the
4474 * current position with the following quirks:
4475 * - If two chars or more are pressed in quick succession we search
4476 * for the corresponding string (e.g. 'abc').
4477 * - If there is a delay we wipe away the current search string and
4478 * restart with just that char.
4479 * - If the user keeps pressing the same character, whether slowly or
4480 * fast, so that the search string is entirely composed of this
4481 * character ('aaaaa' for instance), then we search for first item
4482 * that starting with that character.
4483 * - If the user types the above character in quick succession, then
4484 * we must also search for the corresponding string ('aaaaa'), and
4485 * go to that string if there is a match.
4487 * RETURNS
4489 * Zero.
4491 * BUGS
4493 * - The current implementation has a list of characters it will
4494 * accept and it ignores everything else. In particular it will
4495 * ignore accentuated characters which seems to match what
4496 * Windows does. But I'm not sure it makes sense to follow
4497 * Windows there.
4498 * - We don't sound a beep when the search fails.
4499 * - The search should start from the focused item, not from the selected
4500 * item. One reason for this is to allow for multiple selections in trees.
4501 * But currently infoPtr->focusedItem does not seem very usable.
4503 * SEE ALSO
4505 * TREEVIEW_ProcessLetterKeys
4507 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4509 HTREEITEM nItem;
4510 HTREEITEM endidx,idx;
4511 TVITEMEXW item;
4512 WCHAR buffer[MAX_PATH];
4513 DWORD timestamp,elapsed;
4515 /* simple parameter checking */
4516 if (!charCode || !keyData) return 0;
4518 /* only allow the valid WM_CHARs through */
4519 if (!isalnum(charCode) &&
4520 charCode != '.' && charCode != '`' && charCode != '!' &&
4521 charCode != '@' && charCode != '#' && charCode != '$' &&
4522 charCode != '%' && charCode != '^' && charCode != '&' &&
4523 charCode != '*' && charCode != '(' && charCode != ')' &&
4524 charCode != '-' && charCode != '_' && charCode != '+' &&
4525 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4526 charCode != '}' && charCode != '[' && charCode != '{' &&
4527 charCode != '/' && charCode != '?' && charCode != '>' &&
4528 charCode != '<' && charCode != ',' && charCode != '~')
4529 return 0;
4531 /* compute how much time elapsed since last keypress */
4532 timestamp = GetTickCount();
4533 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4534 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4535 } else {
4536 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4539 /* update the search parameters */
4540 infoPtr->lastKeyPressTimestamp=timestamp;
4541 if (elapsed < KEY_DELAY) {
4542 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4543 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4545 if (infoPtr->charCode != charCode) {
4546 infoPtr->charCode=charCode=0;
4548 } else {
4549 infoPtr->charCode=charCode;
4550 infoPtr->szSearchParam[0]=charCode;
4551 infoPtr->nSearchParamLength=1;
4552 /* Redundant with the 1 char string */
4553 charCode=0;
4556 /* and search from the current position */
4557 nItem=NULL;
4558 if (infoPtr->selectedItem != NULL) {
4559 endidx=infoPtr->selectedItem;
4560 /* if looking for single character match,
4561 * then we must always move forward
4563 if (infoPtr->nSearchParamLength == 1)
4564 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4565 else
4566 idx=endidx;
4567 } else {
4568 endidx=NULL;
4569 idx=infoPtr->root->firstChild;
4571 do {
4572 /* At the end point, sort out wrapping */
4573 if (idx == NULL) {
4575 /* If endidx is null, stop at the last item (ie top to bottom) */
4576 if (endidx == NULL)
4577 break;
4579 /* Otherwise, start again at the very beginning */
4580 idx=infoPtr->root->firstChild;
4582 /* But if we are stopping on the first child, end now! */
4583 if (idx == endidx) break;
4586 /* get item */
4587 ZeroMemory(&item, sizeof(item));
4588 item.mask = TVIF_TEXT;
4589 item.hItem = idx;
4590 item.pszText = buffer;
4591 item.cchTextMax = sizeof(buffer);
4592 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4594 /* check for a match */
4595 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4596 nItem=idx;
4597 break;
4598 } else if ( (charCode != 0) && (nItem == NULL) &&
4599 (nItem != infoPtr->selectedItem) &&
4600 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4601 /* This would work but we must keep looking for a longer match */
4602 nItem=idx;
4604 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4605 } while (idx != endidx);
4607 if (nItem != NULL) {
4608 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4609 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4613 return 0;
4616 /* Scrolling ************************************************************/
4618 static LRESULT
4619 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4621 int viscount;
4622 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4623 HTREEITEM newFirstVisible = NULL;
4624 int visible_pos = -1;
4626 if (!TREEVIEW_ValidItem(infoPtr, item))
4627 return FALSE;
4629 if (!ISVISIBLE(item))
4631 /* Expand parents as necessary. */
4632 HTREEITEM parent;
4634 /* see if we are trying to ensure that root is visible */
4635 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4636 parent = item->parent;
4637 else
4638 parent = item; /* this item is the topmost item */
4640 while (parent != infoPtr->root)
4642 if (!(parent->state & TVIS_EXPANDED))
4643 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4645 parent = parent->parent;
4649 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4651 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4652 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4654 if (hasFirstVisible)
4655 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4657 if (visible_pos < 0)
4659 /* item is before the start of the list: put it at the top. */
4660 newFirstVisible = item;
4662 else if (visible_pos >= viscount
4663 /* Sometimes, before we are displayed, GVC is 0, causing us to
4664 * spuriously scroll up. */
4665 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4667 /* item is past the end of the list. */
4668 int scroll = visible_pos - viscount;
4670 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4671 scroll + 1);
4674 if (bHScroll)
4676 /* Scroll window so item's text is visible as much as possible */
4677 /* Calculation of amount of extra space is taken from EditLabel code */
4678 INT pos, x;
4679 TEXTMETRICW textMetric;
4680 HDC hdc = GetWindowDC(infoPtr->hwnd);
4682 x = item->textWidth;
4684 GetTextMetricsW(hdc, &textMetric);
4685 ReleaseDC(infoPtr->hwnd, hdc);
4687 x += (textMetric.tmMaxCharWidth * 2);
4688 x = max(x, textMetric.tmMaxCharWidth * 3);
4690 if (item->textOffset < 0)
4691 pos = item->textOffset;
4692 else if (item->textOffset + x > infoPtr->clientWidth)
4694 if (x > infoPtr->clientWidth)
4695 pos = item->textOffset;
4696 else
4697 pos = item->textOffset + x - infoPtr->clientWidth;
4699 else
4700 pos = 0;
4702 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4705 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4707 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4709 return TRUE;
4712 return FALSE;
4715 static VOID
4716 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4717 TREEVIEW_ITEM *newFirstVisible,
4718 BOOL bUpdateScrollPos)
4720 int gap_size;
4722 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4724 if (newFirstVisible != NULL)
4726 /* Prevent an empty gap from appearing at the bottom... */
4727 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4728 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4730 if (gap_size > 0)
4732 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4733 -gap_size);
4735 /* ... unless we just don't have enough items. */
4736 if (newFirstVisible == NULL)
4737 newFirstVisible = infoPtr->root->firstChild;
4741 if (infoPtr->firstVisible != newFirstVisible)
4743 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4745 infoPtr->firstVisible = newFirstVisible;
4746 TREEVIEW_Invalidate(infoPtr, NULL);
4748 else
4750 TREEVIEW_ITEM *item;
4751 int scroll = infoPtr->uItemHeight *
4752 (infoPtr->firstVisible->visibleOrder
4753 - newFirstVisible->visibleOrder);
4755 infoPtr->firstVisible = newFirstVisible;
4757 for (item = infoPtr->root->firstChild; item != NULL;
4758 item = TREEVIEW_GetNextListItem(infoPtr, item))
4760 item->rect.top += scroll;
4761 item->rect.bottom += scroll;
4764 if (bUpdateScrollPos)
4765 SetScrollPos(infoPtr->hwnd, SB_VERT,
4766 newFirstVisible->visibleOrder, TRUE);
4768 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4773 /************************************************************************
4774 * VScroll is always in units of visible items. i.e. we always have a
4775 * visible item aligned to the top of the control. (Unless we have no
4776 * items at all.)
4778 static LRESULT
4779 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4781 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4782 TREEVIEW_ITEM *newFirstVisible = NULL;
4784 int nScrollCode = LOWORD(wParam);
4786 TRACE("wp %lx\n", wParam);
4788 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4789 return 0;
4791 if (!oldFirstVisible)
4793 assert(infoPtr->root->firstChild == NULL);
4794 return 0;
4797 switch (nScrollCode)
4799 case SB_TOP:
4800 newFirstVisible = infoPtr->root->firstChild;
4801 break;
4803 case SB_BOTTOM:
4804 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4805 break;
4807 case SB_LINEUP:
4808 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4809 break;
4811 case SB_LINEDOWN:
4812 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4813 break;
4815 case SB_PAGEUP:
4816 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4817 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4818 break;
4820 case SB_PAGEDOWN:
4821 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4822 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4823 break;
4825 case SB_THUMBTRACK:
4826 case SB_THUMBPOSITION:
4827 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4828 infoPtr->root->firstChild,
4829 (LONG)(SHORT)HIWORD(wParam));
4830 break;
4832 case SB_ENDSCROLL:
4833 return 0;
4836 if (newFirstVisible != NULL)
4838 if (newFirstVisible != oldFirstVisible)
4839 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4840 nScrollCode != SB_THUMBTRACK);
4841 else if (nScrollCode == SB_THUMBPOSITION)
4842 SetScrollPos(infoPtr->hwnd, SB_VERT,
4843 newFirstVisible->visibleOrder, TRUE);
4846 return 0;
4849 static LRESULT
4850 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4852 int maxWidth;
4853 int scrollX = infoPtr->scrollX;
4854 int nScrollCode = LOWORD(wParam);
4856 TRACE("wp %lx\n", wParam);
4858 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4859 return FALSE;
4861 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4862 /* shall never occur */
4863 if (maxWidth <= 0)
4865 scrollX = 0;
4866 goto scroll;
4869 switch (nScrollCode)
4871 case SB_LINELEFT:
4872 scrollX -= infoPtr->uItemHeight;
4873 break;
4874 case SB_LINERIGHT:
4875 scrollX += infoPtr->uItemHeight;
4876 break;
4877 case SB_PAGELEFT:
4878 scrollX -= infoPtr->clientWidth;
4879 break;
4880 case SB_PAGERIGHT:
4881 scrollX += infoPtr->clientWidth;
4882 break;
4884 case SB_THUMBTRACK:
4885 case SB_THUMBPOSITION:
4886 scrollX = (int)(SHORT)HIWORD(wParam);
4887 break;
4889 case SB_ENDSCROLL:
4890 return 0;
4893 if (scrollX > maxWidth)
4894 scrollX = maxWidth;
4895 else if (scrollX < 0)
4896 scrollX = 0;
4898 scroll:
4899 if (scrollX != infoPtr->scrollX)
4901 TREEVIEW_ITEM *item;
4902 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4904 for (item = infoPtr->root->firstChild; item != NULL;
4905 item = TREEVIEW_GetNextListItem(infoPtr, item))
4907 item->linesOffset += scroll_pixels;
4908 item->stateOffset += scroll_pixels;
4909 item->imageOffset += scroll_pixels;
4910 item->textOffset += scroll_pixels;
4913 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4914 infoPtr->scrollX = scrollX;
4915 UpdateWindow(infoPtr->hwnd);
4918 if (nScrollCode != SB_THUMBTRACK)
4919 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4921 return 0;
4924 static LRESULT
4925 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4927 short gcWheelDelta;
4928 UINT pulScrollLines = 3;
4930 if (wParam & (MK_SHIFT | MK_CONTROL))
4931 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4933 if (infoPtr->firstVisible == NULL)
4934 return TRUE;
4936 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4938 gcWheelDelta = -(short)HIWORD(wParam);
4939 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4941 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4943 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4944 int maxDy = infoPtr->maxVisibleOrder;
4946 if (newDy > maxDy)
4947 newDy = maxDy;
4949 if (newDy < 0)
4950 newDy = 0;
4952 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4954 return TRUE;
4957 /* Create/Destroy *******************************************************/
4959 static void
4960 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
4962 RECT rc;
4963 HBITMAP hbm, hbmOld;
4964 HDC hdc, hdcScreen;
4965 int nIndex;
4967 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4969 hdcScreen = GetDC(0);
4971 hdc = CreateCompatibleDC(hdcScreen);
4972 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4973 hbmOld = SelectObject(hdc, hbm);
4975 SetRect(&rc, 0, 0, 48, 16);
4976 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4978 SetRect(&rc, 18, 2, 30, 14);
4979 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4980 DFCS_BUTTONCHECK|DFCS_FLAT);
4982 SetRect(&rc, 34, 2, 46, 14);
4983 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4984 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4986 SelectObject(hdc, hbmOld);
4987 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4988 comctl32_color.clrWindow);
4989 TRACE("checkbox index %d\n", nIndex);
4991 DeleteObject(hbm);
4992 DeleteDC(hdc);
4993 ReleaseDC(0, hdcScreen);
4995 infoPtr->stateImageWidth = 16;
4996 infoPtr->stateImageHeight = 16;
4999 static LRESULT
5000 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
5002 RECT rcClient;
5003 TREEVIEW_INFO *infoPtr;
5004 LOGFONTW lf;
5006 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
5008 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
5010 if (infoPtr == NULL)
5012 ERR("could not allocate info memory!\n");
5013 return 0;
5016 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
5018 infoPtr->hwnd = hwnd;
5019 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5020 infoPtr->Timer = 0;
5021 infoPtr->uNumItems = 0;
5022 infoPtr->cdmode = 0;
5023 infoPtr->uScrollTime = 300; /* milliseconds */
5024 infoPtr->bRedraw = TRUE;
5026 GetClientRect(hwnd, &rcClient);
5028 /* No scroll bars yet. */
5029 infoPtr->clientWidth = rcClient.right;
5030 infoPtr->clientHeight = rcClient.bottom;
5031 infoPtr->uInternalStatus = 0;
5033 infoPtr->treeWidth = 0;
5034 infoPtr->treeHeight = 0;
5036 infoPtr->uIndent = MINIMUM_INDENT;
5037 infoPtr->selectedItem = NULL;
5038 infoPtr->focusedItem = NULL;
5039 infoPtr->hotItem = NULL;
5040 infoPtr->editItem = NULL;
5041 infoPtr->firstVisible = NULL;
5042 infoPtr->maxVisibleOrder = 0;
5043 infoPtr->dropItem = NULL;
5044 infoPtr->insertMarkItem = NULL;
5045 infoPtr->insertBeforeorAfter = 0;
5046 /* dragList */
5048 infoPtr->scrollX = 0;
5050 infoPtr->clrBk = CLR_NONE; /* use system color */
5051 infoPtr->clrText = CLR_NONE; /* use system color */
5052 infoPtr->clrLine = CLR_DEFAULT;
5053 infoPtr->clrInsertMark = CLR_DEFAULT;
5055 /* hwndToolTip */
5057 infoPtr->hwndEdit = NULL;
5058 infoPtr->wpEditOrig = NULL;
5059 infoPtr->bIgnoreEditKillFocus = FALSE;
5060 infoPtr->bLabelChanged = FALSE;
5062 infoPtr->himlNormal = NULL;
5063 infoPtr->himlState = NULL;
5064 infoPtr->normalImageWidth = 0;
5065 infoPtr->normalImageHeight = 0;
5066 infoPtr->stateImageWidth = 0;
5067 infoPtr->stateImageHeight = 0;
5069 infoPtr->items = DPA_Create(16);
5071 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5072 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5073 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5074 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5075 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5077 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5079 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5080 infoPtr->root->state = TVIS_EXPANDED;
5081 infoPtr->root->iLevel = -1;
5082 infoPtr->root->visibleOrder = -1;
5084 infoPtr->hwndNotify = lpcs->hwndParent;
5085 infoPtr->hwndToolTip = 0;
5087 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5089 /* Determine what type of notify should be issued */
5090 /* sets infoPtr->bNtfUnicode */
5091 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5093 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5094 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5095 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5096 hwnd, 0, 0, 0);
5098 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5099 TREEVIEW_InitCheckboxes(infoPtr);
5101 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5102 ShowScrollBar(hwnd, SB_VERT, FALSE);
5103 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5105 OpenThemeData (hwnd, themeClass);
5107 return 0;
5111 static LRESULT
5112 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5114 TRACE("\n");
5116 /* free item data */
5117 TREEVIEW_RemoveTree(infoPtr);
5118 /* root isn't freed with other items */
5119 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5120 DPA_Destroy(infoPtr->items);
5122 /* tool tip is automatically destroyed: we are its owner */
5124 /* Restore original wndproc */
5125 if (infoPtr->hwndEdit)
5126 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5127 (DWORD_PTR)infoPtr->wpEditOrig);
5129 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5131 /* Deassociate treeview from the window before doing anything drastic. */
5132 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5134 DeleteObject(infoPtr->hDefaultFont);
5135 DeleteObject(infoPtr->hBoldFont);
5136 DeleteObject(infoPtr->hUnderlineFont);
5137 Free(infoPtr);
5139 return 0;
5142 /* Miscellaneous Messages ***********************************************/
5144 static LRESULT
5145 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5147 static const struct
5149 unsigned char code;
5151 scroll[] =
5153 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5154 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5155 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5156 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5157 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5158 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5159 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5160 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5161 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5162 #undef SCROLL_ENTRY
5165 if (key >= VK_PRIOR && key <= VK_DOWN)
5167 unsigned char code = scroll[key - VK_PRIOR].code;
5169 (((code & (1 << 7)) == (SB_HORZ << 7))
5170 ? TREEVIEW_HScroll
5171 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5174 return 0;
5177 /************************************************************************
5178 * TREEVIEW_KeyDown
5180 * VK_UP Move selection to the previous non-hidden item.
5181 * VK_DOWN Move selection to the next non-hidden item.
5182 * VK_HOME Move selection to the first item.
5183 * VK_END Move selection to the last item.
5184 * VK_LEFT If expanded then collapse, otherwise move to parent.
5185 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5186 * VK_ADD Expand.
5187 * VK_SUBTRACT Collapse.
5188 * VK_MULTIPLY Expand all.
5189 * VK_PRIOR Move up GetVisibleCount items.
5190 * VK_NEXT Move down GetVisibleCount items.
5191 * VK_BACK Move to parent.
5192 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5194 static LRESULT
5195 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5197 /* If it is non-NULL and different, it will be selected and visible. */
5198 TREEVIEW_ITEM *newSelection = NULL;
5200 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5202 TRACE("%lx\n", wParam);
5204 if (prevItem == NULL)
5205 return FALSE;
5207 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5208 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5210 switch (wParam)
5212 case VK_UP:
5213 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5214 if (!newSelection)
5215 newSelection = infoPtr->root->firstChild;
5216 break;
5218 case VK_DOWN:
5219 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5220 break;
5222 case VK_HOME:
5223 newSelection = infoPtr->root->firstChild;
5224 break;
5226 case VK_END:
5227 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5228 break;
5230 case VK_LEFT:
5231 if (prevItem->state & TVIS_EXPANDED)
5233 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5235 else if (prevItem->parent != infoPtr->root)
5237 newSelection = prevItem->parent;
5239 break;
5241 case VK_RIGHT:
5242 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5244 if (!(prevItem->state & TVIS_EXPANDED))
5245 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5246 else
5248 newSelection = prevItem->firstChild;
5252 break;
5254 case VK_MULTIPLY:
5255 TREEVIEW_ExpandAll(infoPtr, prevItem);
5256 break;
5258 case VK_ADD:
5259 if (!(prevItem->state & TVIS_EXPANDED))
5260 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5261 break;
5263 case VK_SUBTRACT:
5264 if (prevItem->state & TVIS_EXPANDED)
5265 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5266 break;
5268 case VK_PRIOR:
5269 newSelection
5270 = TREEVIEW_GetListItem(infoPtr, prevItem,
5271 -TREEVIEW_GetVisibleCount(infoPtr));
5272 break;
5274 case VK_NEXT:
5275 newSelection
5276 = TREEVIEW_GetListItem(infoPtr, prevItem,
5277 TREEVIEW_GetVisibleCount(infoPtr));
5278 break;
5280 case VK_BACK:
5281 newSelection = prevItem->parent;
5282 if (newSelection == infoPtr->root)
5283 newSelection = NULL;
5284 break;
5286 case VK_SPACE:
5287 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5288 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5289 break;
5292 if (newSelection && newSelection != prevItem)
5294 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5295 TVC_BYKEYBOARD))
5297 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5301 return FALSE;
5304 static LRESULT
5305 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5307 /* remove hot effect from item */
5308 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5309 infoPtr->hotItem = NULL;
5311 return 0;
5314 static LRESULT
5315 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5317 POINT pt;
5318 TRACKMOUSEEVENT trackinfo;
5319 TREEVIEW_ITEM * item;
5321 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5323 /* fill in the TRACKMOUSEEVENT struct */
5324 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5325 trackinfo.dwFlags = TME_QUERY;
5326 trackinfo.hwndTrack = infoPtr->hwnd;
5328 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5329 _TrackMouseEvent(&trackinfo);
5331 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5332 if(!(trackinfo.dwFlags & TME_LEAVE))
5334 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5335 trackinfo.hwndTrack = infoPtr->hwnd;
5336 /* do it as fast as possible, minimal systimer latency will be used */
5337 trackinfo.dwHoverTime = 1;
5339 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5340 /* and can properly deactivate the hot item */
5341 _TrackMouseEvent(&trackinfo);
5344 pt.x = (short)LOWORD(lParam);
5345 pt.y = (short)HIWORD(lParam);
5347 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5349 if (item != infoPtr->hotItem)
5351 /* redraw old hot item */
5352 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5353 infoPtr->hotItem = item;
5354 /* redraw new hot item */
5355 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5358 return 0;
5361 /* Draw themed border */
5362 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5364 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5365 HDC dc;
5366 RECT r;
5367 HRGN cliprgn;
5368 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5369 cyEdge = GetSystemMetrics (SM_CYEDGE);
5371 if (!theme)
5372 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5374 GetWindowRect(infoPtr->hwnd, &r);
5376 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5377 r.right - cxEdge, r.bottom - cyEdge);
5378 if (region != (HRGN)1)
5379 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5380 OffsetRect(&r, -r.left, -r.top);
5382 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5383 OffsetRect(&r, -r.left, -r.top);
5385 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5386 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5387 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5388 ReleaseDC(infoPtr->hwnd, dc);
5390 /* Call default proc to get the scrollbars etc. painted */
5391 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5393 return TRUE;
5396 static LRESULT
5397 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5399 LPNMHDR lpnmh = (LPNMHDR)lParam;
5401 if (lpnmh->code == PGN_CALCSIZE) {
5402 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5404 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5405 lppgc->iWidth = infoPtr->treeWidth;
5406 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5407 infoPtr->treeWidth, infoPtr->clientWidth);
5409 else {
5410 lppgc->iHeight = infoPtr->treeHeight;
5411 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5412 infoPtr->treeHeight, infoPtr->clientHeight);
5414 return 0;
5416 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5419 static LRESULT
5420 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5422 if (wParam == SIZE_RESTORED)
5424 infoPtr->clientWidth = (short)LOWORD(lParam);
5425 infoPtr->clientHeight = (short)HIWORD(lParam);
5427 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5428 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5429 TREEVIEW_UpdateScrollBars(infoPtr);
5431 else
5433 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5436 TREEVIEW_Invalidate(infoPtr, NULL);
5437 return 0;
5440 static void TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5442 TREEVIEW_ITEM *child = item->firstChild;
5444 item->state &= ~TVIS_STATEIMAGEMASK;
5445 item->state |= INDEXTOSTATEIMAGEMASK(1);
5447 while (child)
5449 TREEVIEW_ITEM *next = child->nextSibling;
5450 TREEVIEW_ResetImageStateIndex(infoPtr, child);
5451 child = next;
5455 static LRESULT
5456 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5458 TRACE("(%lx %lx)\n", wParam, lParam);
5460 if (wParam == GWL_STYLE)
5462 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5464 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5466 if (dwNewStyle & TVS_CHECKBOXES)
5468 TREEVIEW_InitCheckboxes(infoPtr);
5469 TRACE("checkboxes enabled\n");
5471 /* set all items to state image index 1 */
5472 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
5474 else
5476 FIXME("tried to disable checkboxes\n");
5480 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5482 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5484 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5485 TRACE("tooltips enabled\n");
5487 else
5489 DestroyWindow(infoPtr->hwndToolTip);
5490 infoPtr->hwndToolTip = 0;
5491 TRACE("tooltips disabled\n");
5495 infoPtr->dwStyle = dwNewStyle;
5498 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5499 TREEVIEW_UpdateScrollBars(infoPtr);
5500 TREEVIEW_Invalidate(infoPtr, NULL);
5502 return 0;
5505 static LRESULT
5506 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5508 POINT pt;
5509 TREEVIEW_ITEM * item;
5510 NMMOUSE nmmouse;
5512 GetCursorPos(&pt);
5513 ScreenToClient(infoPtr->hwnd, &pt);
5515 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5517 memset(&nmmouse, 0, sizeof(nmmouse));
5518 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5519 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5520 nmmouse.hdr.code = NM_SETCURSOR;
5521 if (item)
5523 nmmouse.dwItemSpec = (DWORD_PTR)item;
5524 nmmouse.dwItemData = item->lParam;
5526 nmmouse.pt.x = 0;
5527 nmmouse.pt.y = 0;
5528 nmmouse.dwHitInfo = lParam;
5529 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, &nmmouse.hdr))
5530 return 0;
5532 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5534 SetCursor(infoPtr->hcurHand);
5535 return 0;
5537 else
5538 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5541 static LRESULT
5542 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5544 TRACE("\n");
5546 if (!infoPtr->selectedItem)
5548 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5549 TVC_UNKNOWN);
5552 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5553 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5554 return 0;
5557 static LRESULT
5558 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5560 TRACE("\n");
5562 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5563 UpdateWindow(infoPtr->hwnd);
5564 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5565 return 0;
5568 /* update theme after a WM_THEMECHANGED message */
5569 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5571 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5572 CloseThemeData (theme);
5573 OpenThemeData (infoPtr->hwnd, themeClass);
5574 return 0;
5578 static LRESULT WINAPI
5579 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5581 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5583 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5585 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5586 else
5588 if (uMsg == WM_CREATE)
5589 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5590 else
5591 goto def;
5594 switch (uMsg)
5596 case TVM_CREATEDRAGIMAGE:
5597 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5599 case TVM_DELETEITEM:
5600 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5602 case TVM_EDITLABELA:
5603 case TVM_EDITLABELW:
5604 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5606 case TVM_ENDEDITLABELNOW:
5607 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5609 case TVM_ENSUREVISIBLE:
5610 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5612 case TVM_EXPAND:
5613 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5615 case TVM_GETBKCOLOR:
5616 return TREEVIEW_GetBkColor(infoPtr);
5618 case TVM_GETCOUNT:
5619 return TREEVIEW_GetCount(infoPtr);
5621 case TVM_GETEDITCONTROL:
5622 return TREEVIEW_GetEditControl(infoPtr);
5624 case TVM_GETIMAGELIST:
5625 return TREEVIEW_GetImageList(infoPtr, wParam);
5627 case TVM_GETINDENT:
5628 return TREEVIEW_GetIndent(infoPtr);
5630 case TVM_GETINSERTMARKCOLOR:
5631 return TREEVIEW_GetInsertMarkColor(infoPtr);
5633 case TVM_GETISEARCHSTRINGA:
5634 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5635 return 0;
5637 case TVM_GETISEARCHSTRINGW:
5638 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5639 return 0;
5641 case TVM_GETITEMA:
5642 case TVM_GETITEMW:
5643 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5644 uMsg == TVM_GETITEMW);
5645 case TVM_GETITEMHEIGHT:
5646 return TREEVIEW_GetItemHeight(infoPtr);
5648 case TVM_GETITEMRECT:
5649 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5651 case TVM_GETITEMSTATE:
5652 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5654 case TVM_GETLINECOLOR:
5655 return TREEVIEW_GetLineColor(infoPtr);
5657 case TVM_GETNEXTITEM:
5658 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5660 case TVM_GETSCROLLTIME:
5661 return TREEVIEW_GetScrollTime(infoPtr);
5663 case TVM_GETTEXTCOLOR:
5664 return TREEVIEW_GetTextColor(infoPtr);
5666 case TVM_GETTOOLTIPS:
5667 return TREEVIEW_GetToolTips(infoPtr);
5669 case TVM_GETUNICODEFORMAT:
5670 return TREEVIEW_GetUnicodeFormat(infoPtr);
5672 case TVM_GETVISIBLECOUNT:
5673 return TREEVIEW_GetVisibleCount(infoPtr);
5675 case TVM_HITTEST:
5676 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5678 case TVM_INSERTITEMA:
5679 case TVM_INSERTITEMW:
5680 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5681 uMsg == TVM_INSERTITEMW);
5682 case TVM_SELECTITEM:
5683 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5685 case TVM_SETBKCOLOR:
5686 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5688 case TVM_SETIMAGELIST:
5689 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5691 case TVM_SETINDENT:
5692 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5694 case TVM_SETINSERTMARK:
5695 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5697 case TVM_SETINSERTMARKCOLOR:
5698 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5700 case TVM_SETITEMA:
5701 case TVM_SETITEMW:
5702 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5703 uMsg == TVM_SETITEMW);
5704 case TVM_SETLINECOLOR:
5705 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5707 case TVM_SETITEMHEIGHT:
5708 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5710 case TVM_SETSCROLLTIME:
5711 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5713 case TVM_SETTEXTCOLOR:
5714 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5716 case TVM_SETTOOLTIPS:
5717 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5719 case TVM_SETUNICODEFORMAT:
5720 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5722 case TVM_SORTCHILDREN:
5723 return TREEVIEW_SortChildren(infoPtr, lParam);
5725 case TVM_SORTCHILDRENCB:
5726 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5728 case WM_CHAR:
5729 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5731 case WM_COMMAND:
5732 return TREEVIEW_Command(infoPtr, wParam, lParam);
5734 case WM_DESTROY:
5735 return TREEVIEW_Destroy(infoPtr);
5737 /* WM_ENABLE */
5739 case WM_ERASEBKGND:
5740 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5742 case WM_GETDLGCODE:
5743 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5745 case WM_GETFONT:
5746 return TREEVIEW_GetFont(infoPtr);
5748 case WM_HSCROLL:
5749 return TREEVIEW_HScroll(infoPtr, wParam);
5751 case WM_KEYDOWN:
5752 return TREEVIEW_KeyDown(infoPtr, wParam);
5754 case WM_KILLFOCUS:
5755 return TREEVIEW_KillFocus(infoPtr);
5757 case WM_LBUTTONDBLCLK:
5758 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5760 case WM_LBUTTONDOWN:
5761 return TREEVIEW_LButtonDown(infoPtr, lParam);
5763 /* WM_MBUTTONDOWN */
5765 case WM_MOUSELEAVE:
5766 return TREEVIEW_MouseLeave(infoPtr);
5768 case WM_MOUSEMOVE:
5769 return TREEVIEW_MouseMove(infoPtr, lParam);
5771 case WM_NCLBUTTONDOWN:
5772 if (infoPtr->hwndEdit)
5773 SetFocus(infoPtr->hwnd);
5774 goto def;
5776 case WM_NCPAINT:
5777 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5779 case WM_NOTIFY:
5780 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5782 case WM_NOTIFYFORMAT:
5783 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5785 case WM_PRINTCLIENT:
5786 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5788 case WM_PAINT:
5789 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5791 case WM_RBUTTONDOWN:
5792 return TREEVIEW_RButtonDown(infoPtr, lParam);
5794 case WM_SETCURSOR:
5795 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5797 case WM_SETFOCUS:
5798 return TREEVIEW_SetFocus(infoPtr);
5800 case WM_SETFONT:
5801 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5803 case WM_SETREDRAW:
5804 return TREEVIEW_SetRedraw(infoPtr, wParam);
5806 case WM_SIZE:
5807 return TREEVIEW_Size(infoPtr, wParam, lParam);
5809 case WM_STYLECHANGED:
5810 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5812 case WM_SYSCOLORCHANGE:
5813 COMCTL32_RefreshSysColors();
5814 return 0;
5816 /* WM_SYSKEYDOWN */
5818 case WM_TIMER:
5819 return TREEVIEW_HandleTimer(infoPtr, wParam);
5821 case WM_THEMECHANGED:
5822 return TREEVIEW_ThemeChanged (infoPtr);
5824 case WM_VSCROLL:
5825 return TREEVIEW_VScroll(infoPtr, wParam);
5827 /* WM_WININICHANGE */
5829 case WM_MOUSEWHEEL:
5830 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5832 case WM_DRAWITEM:
5833 TRACE("drawItem\n");
5834 goto def;
5836 default:
5837 /* This mostly catches MFC and Delphi messages. :( */
5838 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5839 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5840 def:
5841 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5846 /* Class Registration ***************************************************/
5848 VOID
5849 TREEVIEW_Register(void)
5851 WNDCLASSW wndClass;
5853 TRACE("\n");
5855 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5856 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5857 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5858 wndClass.cbClsExtra = 0;
5859 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5861 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5862 wndClass.hbrBackground = 0;
5863 wndClass.lpszClassName = WC_TREEVIEWW;
5865 RegisterClassW(&wndClass);
5869 VOID
5870 TREEVIEW_Unregister(void)
5872 UnregisterClassW(WC_TREEVIEWW, NULL);
5876 /* Tree Verification ****************************************************/
5878 static inline void
5879 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item);
5881 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5882 const TREEVIEW_ITEM *item)
5884 assert(infoPtr != NULL);
5885 assert(item != NULL);
5887 /* both NULL, or both non-null */
5888 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5890 assert(item->firstChild != item);
5891 assert(item->lastChild != item);
5893 if (item->firstChild)
5895 assert(item->firstChild->parent == item);
5896 assert(item->firstChild->prevSibling == NULL);
5899 if (item->lastChild)
5901 assert(item->lastChild->parent == item);
5902 assert(item->lastChild->nextSibling == NULL);
5905 assert(item->nextSibling != item);
5906 if (item->nextSibling)
5908 assert(item->nextSibling->parent == item->parent);
5909 assert(item->nextSibling->prevSibling == item);
5912 assert(item->prevSibling != item);
5913 if (item->prevSibling)
5915 assert(item->prevSibling->parent == item->parent);
5916 assert(item->prevSibling->nextSibling == item);
5920 static inline void
5921 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5923 assert(item != NULL);
5925 assert(item->parent != NULL);
5926 assert(item->parent != item);
5927 assert(item->iLevel == item->parent->iLevel + 1);
5929 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5931 TREEVIEW_VerifyItemCommon(infoPtr, item);
5933 TREEVIEW_VerifyChildren(infoPtr, item);
5936 static inline void
5937 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
5939 const TREEVIEW_ITEM *child;
5940 assert(item != NULL);
5942 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5943 TREEVIEW_VerifyItem(infoPtr, child);
5946 static inline void
5947 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5949 TREEVIEW_ITEM *root = infoPtr->root;
5951 assert(root != NULL);
5952 assert(root->iLevel == -1);
5953 assert(root->parent == NULL);
5954 assert(root->prevSibling == NULL);
5956 TREEVIEW_VerifyItemCommon(infoPtr, root);
5958 TREEVIEW_VerifyChildren(infoPtr, root);
5961 static void
5962 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5964 if (!TRACE_ON(treeview)) return;
5966 assert(infoPtr != NULL);
5967 TREEVIEW_VerifyRoot(infoPtr);