push 735ae92a6c6e93f1d42ab1ae3fd17e22de57873e
[wine/hacks.git] / dlls / comctl32 / treeview.c
blob40c053d85a68396207112e1ab74b64b9854fed56
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: All items always! have valid (allocated) pszText field.
27 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
28 * of size TEXT_CALLBACK_SIZE in DoSetItem.
29 * We use callbackMask to keep track of fields to be updated.
31 * TODO:
32 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
33 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
35 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
37 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
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 "tmschema.h"
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 /* internal structures */
69 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
71 UINT callbackMask;
72 UINT state;
73 UINT stateMask;
74 LPWSTR pszText;
75 int cchTextMax;
76 int iImage;
77 int iSelectedImage;
78 int cChildren;
79 LPARAM lParam;
80 int iIntegral; /* item height multiplier (1 is normal) */
81 int iLevel; /* indentation level:0=root level */
82 HTREEITEM parent; /* handle to parent or 0 if at root */
83 HTREEITEM firstChild; /* handle to first child or 0 if no child */
84 HTREEITEM lastChild;
85 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
86 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
87 RECT rect;
88 LONG linesOffset;
89 LONG stateOffset;
90 LONG imageOffset;
91 LONG textOffset;
92 LONG textWidth; /* horizontal text extent for pszText */
93 LONG visibleOrder; /* visible ordering, 0 is first visible item */
94 } TREEVIEW_ITEM;
97 typedef struct tagTREEVIEW_INFO
99 HWND hwnd;
100 HWND hwndNotify; /* Owner window to send notifications to */
101 DWORD dwStyle;
102 HTREEITEM root;
103 UINT uInternalStatus;
104 INT Timer;
105 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
106 INT cdmode; /* last custom draw setting */
107 UINT uScrollTime; /* max. time for scrolling in milliseconds */
108 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
110 UINT uItemHeight; /* item height */
111 BOOL bHeightSet;
113 LONG clientWidth; /* width of control window */
114 LONG clientHeight; /* height of control window */
116 LONG treeWidth; /* width of visible tree items */
117 LONG treeHeight; /* height of visible tree items */
119 UINT uIndent; /* indentation in pixels */
120 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
121 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
122 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
124 HTREEITEM firstVisible; /* handle to first visible item */
125 LONG maxVisibleOrder;
126 HTREEITEM dropItem; /* handle to item selected by drag cursor */
127 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
128 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
129 HIMAGELIST dragList; /* Bitmap of dragged item */
130 LONG scrollX;
131 COLORREF clrBk;
132 COLORREF clrText;
133 COLORREF clrLine;
134 COLORREF clrInsertMark;
135 HFONT hFont;
136 HFONT hDefaultFont;
137 HFONT hBoldFont;
138 HFONT hUnderlineFont;
139 HCURSOR hcurHand;
140 HWND hwndToolTip;
142 HWND hwndEdit;
143 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
144 BOOL bIgnoreEditKillFocus;
145 BOOL bLabelChanged;
147 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
148 HIMAGELIST himlNormal;
149 int normalImageHeight;
150 int normalImageWidth;
151 HIMAGELIST himlState;
152 int stateImageHeight;
153 int stateImageWidth;
154 HDPA items;
156 DWORD lastKeyPressTimestamp; /* Added */
157 WPARAM charCode; /* Added */
158 INT nSearchParamLength; /* Added */
159 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
160 } TREEVIEW_INFO;
163 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
164 #define KEY_DELAY 450
166 /* bitflags for infoPtr->uInternalStatus */
168 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
169 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
170 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
171 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
172 #define TV_RDRAG 0x10 /* dito Rbutton */
173 #define TV_RDRAGGING 0x20
175 /* bitflags for infoPtr->timer */
177 #define TV_EDIT_TIMER 2
178 #define TV_EDIT_TIMER_SET 2
181 VOID TREEVIEW_Register (VOID);
182 VOID TREEVIEW_Unregister (VOID);
185 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
188 #define TEXT_CALLBACK_SIZE 260
190 #define TREEVIEW_LEFT_MARGIN 8
192 #define MINIMUM_INDENT 19
194 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
196 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
197 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
198 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
201 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
204 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
207 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
209 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
210 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
211 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
212 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
213 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
214 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
215 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
216 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
219 /* Random Utilities *****************************************************/
221 #ifndef NDEBUG
222 static inline void
223 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
225 (void)infoPtr;
227 #else
228 /* The definition is at the end of the file. */
229 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
230 #endif
232 /* Returns the treeview private data if hwnd is a treeview.
233 * Otherwise returns an undefined value. */
234 static TREEVIEW_INFO *
235 TREEVIEW_GetInfoPtr(HWND hwnd)
237 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
240 /* Don't call this. Nothing wants an item index. */
241 static inline int
242 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
244 assert(infoPtr != NULL);
246 return DPA_GetPtrIndex(infoPtr->items, handle);
249 /* Checks if item has changed and needs to be redrawn */
250 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
251 const TVITEMEXW *tvChange)
253 /* Number of children has changed */
254 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
255 return TRUE;
257 /* Image has changed and it's not a callback */
258 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
259 tiNew->iImage != I_IMAGECALLBACK)
260 return TRUE;
262 /* Selected image has changed and it's not a callback */
263 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
264 tiNew->iSelectedImage != I_IMAGECALLBACK)
265 return TRUE;
267 /* Text has changed and it's not a callback */
268 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
269 tiNew->pszText != LPSTR_TEXTCALLBACKW)
270 return TRUE;
272 /* Indent has changed */
273 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
274 return TRUE;
276 /* Item state has changed */
277 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
278 return TRUE;
280 return FALSE;
283 /***************************************************************************
284 * This method checks that handle is an item for this tree.
286 static BOOL
287 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
289 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
291 TRACE("invalid item %p\n", handle);
292 return FALSE;
294 else
295 return TRUE;
298 static HFONT
299 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
301 LOGFONTW font;
303 GetObjectW(hOrigFont, sizeof(font), &font);
304 font.lfWeight = FW_BOLD;
305 return CreateFontIndirectW(&font);
308 static HFONT
309 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
311 LOGFONTW font;
313 GetObjectW(hOrigFont, sizeof(font), &font);
314 font.lfUnderline = TRUE;
315 return CreateFontIndirectW(&font);
318 static inline HFONT
319 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
321 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
322 return infoPtr->hUnderlineFont;
323 if (item->state & TVIS_BOLD)
324 return infoPtr->hBoldFont;
325 return infoPtr->hFont;
328 /* for trace/debugging purposes only */
329 static const char *
330 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
332 if (item == NULL) return "<null item>";
333 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
334 if (item->pszText == NULL) return "<null>";
335 return debugstr_w(item->pszText);
338 /* An item is not a child of itself. */
339 static BOOL
340 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
344 child = child->parent;
345 if (child == parent) return TRUE;
346 } while (child != NULL);
348 return FALSE;
352 /* Tree Traversal *******************************************************/
354 /***************************************************************************
355 * This method returns the last expanded sibling or child child item
356 * of a tree node
358 static TREEVIEW_ITEM *
359 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
361 if (!wineItem)
362 return NULL;
364 while (wineItem->lastChild)
366 if (wineItem->state & TVIS_EXPANDED)
367 wineItem = wineItem->lastChild;
368 else
369 break;
372 if (wineItem == infoPtr->root)
373 return NULL;
375 return wineItem;
378 /***************************************************************************
379 * This method returns the previous non-hidden item in the list not
380 * considering the tree hierarchy.
382 static TREEVIEW_ITEM *
383 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
385 if (tvItem->prevSibling)
387 /* This item has a prevSibling, get the last item in the sibling's tree. */
388 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
390 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
391 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
392 else
393 return upItem;
395 else
397 /* this item does not have a prevSibling, get the parent */
398 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
403 /***************************************************************************
404 * This method returns the next physical item in the treeview not
405 * considering the tree hierarchy.
407 static TREEVIEW_ITEM *
408 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
410 assert(tvItem != NULL);
413 * If this item has children and is expanded, return the first child
415 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
417 return tvItem->firstChild;
422 * try to get the sibling
424 if (tvItem->nextSibling)
425 return tvItem->nextSibling;
428 * Otherwise, get the parent's sibling.
430 while (tvItem->parent)
432 tvItem = tvItem->parent;
434 if (tvItem->nextSibling)
435 return tvItem->nextSibling;
438 return NULL;
441 /***************************************************************************
442 * This method returns the nth item starting at the given item. It returns
443 * the last item (or first) we we run out of items.
445 * Will scroll backward if count is <0.
446 * forward if count is >0.
448 static TREEVIEW_ITEM *
449 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
450 LONG count)
452 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
453 TREEVIEW_ITEM *previousItem;
455 assert(wineItem != NULL);
457 if (count > 0)
459 next_item = TREEVIEW_GetNextListItem;
461 else if (count < 0)
463 count = -count;
464 next_item = TREEVIEW_GetPrevListItem;
466 else
467 return wineItem;
471 previousItem = wineItem;
472 wineItem = next_item(infoPtr, wineItem);
474 } while (--count && wineItem != NULL);
477 return wineItem ? wineItem : previousItem;
480 /* Notifications ************************************************************/
482 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
484 if (!infoPtr->bNtfUnicode) {
485 switch (code) {
486 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
487 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
488 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
489 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
490 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
491 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
492 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
493 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
494 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
495 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
496 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
497 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
500 return code;
503 static LRESULT
504 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
506 TRACE("wParam=%d, lParam=%ld\n", wParam, lParam);
507 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
510 static BOOL
511 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
513 NMHDR nmhdr;
514 HWND hwnd = infoPtr->hwnd;
516 TRACE("%d\n", code);
517 nmhdr.hwndFrom = hwnd;
518 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
519 nmhdr.code = get_notifycode(infoPtr, code);
521 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
522 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
525 static VOID
526 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
528 tvItem->mask = mask;
529 tvItem->hItem = item;
530 tvItem->state = item->state;
531 tvItem->stateMask = 0;
532 tvItem->iImage = item->iImage;
533 tvItem->iSelectedImage = item->iSelectedImage;
534 tvItem->cChildren = item->cChildren;
535 tvItem->lParam = item->lParam;
537 if(mask & TVIF_TEXT)
539 if (!infoPtr->bNtfUnicode)
541 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
542 tvItem->pszText = Alloc (tvItem->cchTextMax);
543 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
545 else
547 tvItem->cchTextMax = item->cchTextMax;
548 tvItem->pszText = item->pszText;
551 else
553 tvItem->cchTextMax = 0;
554 tvItem->pszText = NULL;
558 static BOOL
559 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
560 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
562 HWND hwnd = infoPtr->hwnd;
563 NMTREEVIEWW nmhdr;
564 BOOL ret;
566 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
567 code, action, oldItem, newItem);
569 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
571 nmhdr.hdr.hwndFrom = hwnd;
572 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
573 nmhdr.hdr.code = get_notifycode(infoPtr, code);
574 nmhdr.action = action;
576 if (oldItem)
577 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
579 if (newItem)
580 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
582 nmhdr.ptDrag.x = 0;
583 nmhdr.ptDrag.y = 0;
585 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
586 (WPARAM)nmhdr.hdr.idFrom,
587 (LPARAM)&nmhdr);
588 if (!infoPtr->bNtfUnicode)
590 Free(nmhdr.itemOld.pszText);
591 Free(nmhdr.itemNew.pszText);
593 return ret;
596 static BOOL
597 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
598 HTREEITEM dragItem, POINT pt)
600 HWND hwnd = infoPtr->hwnd;
601 NMTREEVIEWW nmhdr;
603 TRACE("code:%d dragitem:%p\n", code, dragItem);
605 nmhdr.hdr.hwndFrom = hwnd;
606 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
607 nmhdr.hdr.code = get_notifycode(infoPtr, code);
608 nmhdr.action = 0;
609 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
610 nmhdr.itemNew.hItem = dragItem;
611 nmhdr.itemNew.state = dragItem->state;
612 nmhdr.itemNew.lParam = dragItem->lParam;
614 nmhdr.ptDrag.x = pt.x;
615 nmhdr.ptDrag.y = pt.y;
617 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
618 (WPARAM)nmhdr.hdr.idFrom,
619 (LPARAM)&nmhdr);
623 static BOOL
624 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
625 HDC hdc, RECT rc)
627 HWND hwnd = infoPtr->hwnd;
628 NMTVCUSTOMDRAW nmcdhdr;
629 LPNMCUSTOMDRAW nmcd;
631 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
633 nmcd = &nmcdhdr.nmcd;
634 nmcd->hdr.hwndFrom = hwnd;
635 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
636 nmcd->hdr.code = NM_CUSTOMDRAW;
637 nmcd->dwDrawStage = dwDrawStage;
638 nmcd->hdc = hdc;
639 nmcd->rc = rc;
640 nmcd->dwItemSpec = 0;
641 nmcd->uItemState = 0;
642 nmcd->lItemlParam = 0;
643 nmcdhdr.clrText = infoPtr->clrText;
644 nmcdhdr.clrTextBk = infoPtr->clrBk;
645 nmcdhdr.iLevel = 0;
647 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
648 (WPARAM)nmcd->hdr.idFrom,
649 (LPARAM)&nmcdhdr);
654 /* FIXME: need to find out when the flags in uItemState need to be set */
656 static BOOL
657 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
658 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
659 NMTVCUSTOMDRAW *nmcdhdr)
661 HWND hwnd = infoPtr->hwnd;
662 LPNMCUSTOMDRAW nmcd;
663 DWORD dwDrawStage;
664 DWORD_PTR dwItemSpec;
665 UINT uItemState;
666 INT retval;
668 dwDrawStage = CDDS_ITEM | uItemDrawState;
669 dwItemSpec = (DWORD_PTR)wineItem;
670 uItemState = 0;
671 if (wineItem->state & TVIS_SELECTED)
672 uItemState |= CDIS_SELECTED;
673 if (wineItem == infoPtr->selectedItem)
674 uItemState |= CDIS_FOCUS;
675 if (wineItem == infoPtr->hotItem)
676 uItemState |= CDIS_HOT;
678 nmcd = &nmcdhdr->nmcd;
679 nmcd->hdr.hwndFrom = hwnd;
680 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
681 nmcd->hdr.code = NM_CUSTOMDRAW;
682 nmcd->dwDrawStage = dwDrawStage;
683 nmcd->hdc = hdc;
684 nmcd->rc = wineItem->rect;
685 nmcd->dwItemSpec = dwItemSpec;
686 nmcd->uItemState = uItemState;
687 nmcd->lItemlParam = wineItem->lParam;
688 nmcdhdr->iLevel = wineItem->iLevel;
690 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
691 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
692 nmcd->uItemState, nmcd->lItemlParam);
694 retval = TREEVIEW_SendRealNotify(infoPtr,
695 (WPARAM)nmcd->hdr.idFrom,
696 (LPARAM)nmcdhdr);
698 return (BOOL)retval;
701 static BOOL
702 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
704 HWND hwnd = infoPtr->hwnd;
705 NMTVDISPINFOW tvdi;
706 BOOL ret;
708 tvdi.hdr.hwndFrom = hwnd;
709 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
710 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
712 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
713 &tvdi.item, editItem);
715 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
717 if (!infoPtr->bNtfUnicode)
718 Free(tvdi.item.pszText);
720 return ret;
723 static void
724 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
725 UINT mask)
727 NMTVDISPINFOW callback;
728 HWND hwnd = infoPtr->hwnd;
730 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
731 mask &= wineItem->callbackMask;
733 if (mask == 0) return;
735 callback.hdr.hwndFrom = hwnd;
736 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
737 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
739 /* 'state' always contains valid value, as well as 'lParam'.
740 * All other parameters are uninitialized.
742 callback.item.pszText = wineItem->pszText;
743 callback.item.cchTextMax = wineItem->cchTextMax;
744 callback.item.mask = mask;
745 callback.item.hItem = wineItem;
746 callback.item.state = wineItem->state;
747 callback.item.lParam = wineItem->lParam;
749 /* If text is changed we need to recalculate textWidth */
750 if (mask & TVIF_TEXT)
751 wineItem->textWidth = 0;
753 TREEVIEW_SendRealNotify(infoPtr,
754 (WPARAM)callback.hdr.idFrom, (LPARAM)&callback);
756 /* It may have changed due to a call to SetItem. */
757 mask &= wineItem->callbackMask;
759 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
761 /* Instead of copying text into our buffer user specified its own */
762 if (!infoPtr->bNtfUnicode) {
763 LPWSTR newText;
764 int buflen;
765 int len = MultiByteToWideChar( CP_ACP, 0,
766 (LPSTR)callback.item.pszText, -1,
767 NULL, 0);
768 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
769 newText = (LPWSTR)ReAlloc(wineItem->pszText, buflen);
771 TRACE("returned str %s, len=%d, buflen=%d\n",
772 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
774 if (newText)
776 wineItem->pszText = newText;
777 MultiByteToWideChar( CP_ACP, 0,
778 (LPSTR)callback.item.pszText, -1,
779 wineItem->pszText, buflen/sizeof(WCHAR));
780 wineItem->cchTextMax = buflen/sizeof(WCHAR);
782 /* If ReAlloc fails we have nothing to do, but keep original text */
784 else {
785 int len = max(lstrlenW(callback.item.pszText) + 1,
786 TEXT_CALLBACK_SIZE);
787 LPWSTR newText = ReAlloc(wineItem->pszText, len);
789 TRACE("returned wstr %s, len=%d\n",
790 debugstr_w(callback.item.pszText), len);
792 if (newText)
794 wineItem->pszText = newText;
795 strcpyW(wineItem->pszText, callback.item.pszText);
796 wineItem->cchTextMax = len;
798 /* If ReAlloc fails we have nothing to do, but keep original text */
801 else if (mask & TVIF_TEXT) {
802 /* User put text into our buffer, that is ok unless A string */
803 if (!infoPtr->bNtfUnicode) {
804 LPWSTR newText;
805 LPWSTR oldText = NULL;
806 int buflen;
807 int len = MultiByteToWideChar( CP_ACP, 0,
808 (LPSTR)callback.item.pszText, -1,
809 NULL, 0);
810 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
811 newText = (LPWSTR)Alloc(buflen);
813 TRACE("same buffer str %s, len=%d, buflen=%d\n",
814 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
816 if (newText)
818 oldText = wineItem->pszText;
819 wineItem->pszText = newText;
820 MultiByteToWideChar( CP_ACP, 0,
821 (LPSTR)callback.item.pszText, -1,
822 wineItem->pszText, buflen/sizeof(WCHAR));
823 wineItem->cchTextMax = buflen/sizeof(WCHAR);
824 Free(oldText);
829 if (mask & TVIF_IMAGE)
830 wineItem->iImage = callback.item.iImage;
832 if (mask & TVIF_SELECTEDIMAGE)
833 wineItem->iSelectedImage = callback.item.iSelectedImage;
835 if (mask & TVIF_CHILDREN)
836 wineItem->cChildren = callback.item.cChildren;
838 /* These members are now permanently set. */
839 if (callback.item.mask & TVIF_DI_SETITEM)
840 wineItem->callbackMask &= ~callback.item.mask;
843 /***************************************************************************
844 * This function uses cChildren field to decide whether the item has
845 * children or not.
846 * Note: if this returns TRUE, the child items may not actually exist,
847 * they could be virtual.
849 * Just use wineItem->firstChild to check for physical children.
851 static BOOL
852 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
854 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
856 return wineItem->cChildren > 0;
860 /* Item Position ********************************************************/
862 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
863 static VOID
864 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
866 /* Same effect, different optimisation. */
867 #if 0
868 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
869 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
870 #else
871 BOOL lar = ((infoPtr->dwStyle
872 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
873 > TVS_LINESATROOT);
874 #endif
876 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
877 - infoPtr->scrollX;
878 item->stateOffset = item->linesOffset + infoPtr->uIndent;
879 item->imageOffset = item->stateOffset
880 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
881 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
884 static VOID
885 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
887 HDC hdc;
888 HFONT hOldFont=0;
889 SIZE sz;
891 /* DRAW's OM docker creates items like this */
892 if (item->pszText == NULL)
894 item->textWidth = 0;
895 return;
898 if (hDC != 0)
900 hdc = hDC;
902 else
904 hdc = GetDC(infoPtr->hwnd);
905 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
908 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
909 item->textWidth = sz.cx;
911 if (hDC == 0)
913 SelectObject(hdc, hOldFont);
914 ReleaseDC(0, hdc);
918 static VOID
919 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
921 item->rect.top = infoPtr->uItemHeight *
922 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
924 item->rect.bottom = item->rect.top
925 + infoPtr->uItemHeight * item->iIntegral - 1;
927 item->rect.left = 0;
928 item->rect.right = infoPtr->clientWidth;
931 /* We know that only items after start need their order updated. */
932 static void
933 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
935 TREEVIEW_ITEM *item;
936 int order;
938 if (!start)
940 start = infoPtr->root->firstChild;
941 order = 0;
943 else
944 order = start->visibleOrder;
946 for (item = start; item != NULL;
947 item = TREEVIEW_GetNextListItem(infoPtr, item))
949 item->visibleOrder = order;
950 order += item->iIntegral;
953 infoPtr->maxVisibleOrder = order;
955 for (item = start; item != NULL;
956 item = TREEVIEW_GetNextListItem(infoPtr, item))
958 TREEVIEW_ComputeItemRect(infoPtr, item);
963 /* Update metrics of all items in selected subtree.
964 * root must be expanded
966 static VOID
967 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
969 TREEVIEW_ITEM *sibling;
970 HDC hdc;
971 HFONT hOldFont;
973 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
974 return;
976 root->state &= ~TVIS_EXPANDED;
977 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
978 root->state |= TVIS_EXPANDED;
980 hdc = GetDC(infoPtr->hwnd);
981 hOldFont = SelectObject(hdc, infoPtr->hFont);
983 for (; root != sibling;
984 root = TREEVIEW_GetNextListItem(infoPtr, root))
986 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
988 if (root->callbackMask & TVIF_TEXT)
989 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
991 if (root->textWidth == 0)
993 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
994 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
998 SelectObject(hdc, hOldFont);
999 ReleaseDC(infoPtr->hwnd, hdc);
1002 /* Item Allocation **********************************************************/
1004 static TREEVIEW_ITEM *
1005 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1007 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1009 if (!newItem)
1010 return NULL;
1012 /* I_IMAGENONE would make more sense but this is neither what is
1013 * documented (MSDN doesn't specify) nor what Windows actually does
1014 * (it sets it to zero)... and I can so imagine an application using
1015 * inc/dec to toggle the images. */
1016 newItem->iImage = 0;
1017 newItem->iSelectedImage = 0;
1019 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1021 Free(newItem);
1022 return NULL;
1025 return newItem;
1028 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1029 * free item->pszText. */
1030 static void
1031 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1033 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1034 Free(item);
1035 if (infoPtr->selectedItem == item)
1036 infoPtr->selectedItem = NULL;
1037 if (infoPtr->hotItem == item)
1038 infoPtr->hotItem = NULL;
1039 if (infoPtr->focusedItem == item)
1040 infoPtr->focusedItem = NULL;
1041 if (infoPtr->firstVisible == item)
1042 infoPtr->firstVisible = NULL;
1043 if (infoPtr->dropItem == item)
1044 infoPtr->dropItem = NULL;
1045 if (infoPtr->insertMarkItem == item)
1046 infoPtr->insertMarkItem = NULL;
1050 /* Item Insertion *******************************************************/
1052 /***************************************************************************
1053 * This method inserts newItem before sibling as a child of parent.
1054 * sibling can be NULL, but only if parent has no children.
1056 static void
1057 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1058 TREEVIEW_ITEM *parent)
1060 assert(newItem != NULL);
1061 assert(parent != NULL);
1063 if (sibling != NULL)
1065 assert(sibling->parent == parent);
1067 if (sibling->prevSibling != NULL)
1068 sibling->prevSibling->nextSibling = newItem;
1070 newItem->prevSibling = sibling->prevSibling;
1071 sibling->prevSibling = newItem;
1073 else
1074 newItem->prevSibling = NULL;
1076 newItem->nextSibling = sibling;
1078 if (parent->firstChild == sibling)
1079 parent->firstChild = newItem;
1081 if (parent->lastChild == NULL)
1082 parent->lastChild = newItem;
1085 /***************************************************************************
1086 * This method inserts newItem after sibling as a child of parent.
1087 * sibling can be NULL, but only if parent has no children.
1089 static void
1090 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1091 TREEVIEW_ITEM *parent)
1093 assert(newItem != NULL);
1094 assert(parent != NULL);
1096 if (sibling != NULL)
1098 assert(sibling->parent == parent);
1100 if (sibling->nextSibling != NULL)
1101 sibling->nextSibling->prevSibling = newItem;
1103 newItem->nextSibling = sibling->nextSibling;
1104 sibling->nextSibling = newItem;
1106 else
1107 newItem->nextSibling = NULL;
1109 newItem->prevSibling = sibling;
1111 if (parent->lastChild == sibling)
1112 parent->lastChild = newItem;
1114 if (parent->firstChild == NULL)
1115 parent->firstChild = newItem;
1118 static BOOL
1119 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1120 const TVITEMEXW *tvItem, BOOL isW)
1122 UINT callbackClear = 0;
1123 UINT callbackSet = 0;
1125 TRACE("item %p\n", wineItem);
1126 /* Do this first in case it fails. */
1127 if (tvItem->mask & TVIF_TEXT)
1129 wineItem->textWidth = 0; /* force width recalculation */
1130 if (tvItem->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
1132 int len;
1133 LPWSTR newText;
1134 if (isW)
1135 len = lstrlenW(tvItem->pszText) + 1;
1136 else
1137 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1139 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1141 if (newText == NULL) return FALSE;
1143 callbackClear |= TVIF_TEXT;
1145 wineItem->pszText = newText;
1146 wineItem->cchTextMax = len;
1147 if (isW)
1148 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1149 else
1150 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1151 wineItem->pszText, len);
1153 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1155 else
1157 callbackSet |= TVIF_TEXT;
1159 wineItem->pszText = ReAlloc(wineItem->pszText,
1160 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1161 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1162 TRACE("setting callback, item %p\n", wineItem);
1166 if (tvItem->mask & TVIF_CHILDREN)
1168 wineItem->cChildren = tvItem->cChildren;
1170 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1171 callbackSet |= TVIF_CHILDREN;
1172 else
1173 callbackClear |= TVIF_CHILDREN;
1176 if (tvItem->mask & TVIF_IMAGE)
1178 wineItem->iImage = tvItem->iImage;
1180 if (wineItem->iImage == I_IMAGECALLBACK)
1181 callbackSet |= TVIF_IMAGE;
1182 else
1183 callbackClear |= TVIF_IMAGE;
1186 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1188 wineItem->iSelectedImage = tvItem->iSelectedImage;
1190 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1191 callbackSet |= TVIF_SELECTEDIMAGE;
1192 else
1193 callbackClear |= TVIF_SELECTEDIMAGE;
1196 if (tvItem->mask & TVIF_PARAM)
1197 wineItem->lParam = tvItem->lParam;
1199 /* If the application sets TVIF_INTEGRAL without
1200 * supplying a TVITEMEX structure, it's toast. */
1201 if (tvItem->mask & TVIF_INTEGRAL)
1202 wineItem->iIntegral = tvItem->iIntegral;
1204 if (tvItem->mask & TVIF_STATE)
1206 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1207 tvItem->stateMask);
1208 wineItem->state &= ~tvItem->stateMask;
1209 wineItem->state |= (tvItem->state & tvItem->stateMask);
1212 wineItem->callbackMask |= callbackSet;
1213 wineItem->callbackMask &= ~callbackClear;
1215 return TRUE;
1218 /* Note that the new item is pre-zeroed. */
1219 static LRESULT
1220 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1222 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1223 HTREEITEM insertAfter;
1224 TREEVIEW_ITEM *newItem, *parentItem;
1225 BOOL bTextUpdated = FALSE;
1227 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1229 parentItem = infoPtr->root;
1231 else
1233 parentItem = ptdi->hParent;
1235 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1237 WARN("invalid parent %p\n", parentItem);
1238 return (LRESULT)(HTREEITEM)NULL;
1242 insertAfter = ptdi->hInsertAfter;
1244 /* Validate this now for convenience. */
1245 switch ((DWORD_PTR)insertAfter)
1247 case (DWORD_PTR)TVI_FIRST:
1248 case (DWORD_PTR)TVI_LAST:
1249 case (DWORD_PTR)TVI_SORT:
1250 break;
1252 default:
1253 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1254 insertAfter->parent != parentItem)
1256 WARN("invalid insert after %p\n", insertAfter);
1257 insertAfter = TVI_LAST;
1261 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1262 (tvItem->mask & TVIF_TEXT)
1263 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1264 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1265 : "<no label>");
1267 newItem = TREEVIEW_AllocateItem(infoPtr);
1268 if (newItem == NULL)
1269 return (LRESULT)(HTREEITEM)NULL;
1271 newItem->parent = parentItem;
1272 newItem->iIntegral = 1;
1274 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1275 return (LRESULT)(HTREEITEM)NULL;
1277 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1279 infoPtr->uNumItems++;
1281 switch ((DWORD_PTR)insertAfter)
1283 case (DWORD_PTR)TVI_FIRST:
1285 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1286 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1287 if (infoPtr->firstVisible == originalFirst)
1288 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1290 break;
1292 case (DWORD_PTR)TVI_LAST:
1293 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1294 break;
1296 /* hInsertAfter names a specific item we want to insert after */
1297 default:
1298 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1299 break;
1301 case (DWORD_PTR)TVI_SORT:
1303 TREEVIEW_ITEM *aChild;
1304 TREEVIEW_ITEM *previousChild = NULL;
1305 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1306 BOOL bItemInserted = FALSE;
1308 aChild = parentItem->firstChild;
1310 bTextUpdated = TRUE;
1311 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1313 /* Iterate the parent children to see where we fit in */
1314 while (aChild != NULL)
1316 INT comp;
1318 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1319 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1321 if (comp < 0) /* we are smaller than the current one */
1323 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1324 if (infoPtr->firstVisible == originalFirst &&
1325 aChild == originalFirst)
1326 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1327 bItemInserted = TRUE;
1328 break;
1330 else if (comp > 0) /* we are bigger than the current one */
1332 previousChild = aChild;
1334 /* This will help us to exit if there is no more sibling */
1335 aChild = (aChild->nextSibling == 0)
1336 ? NULL
1337 : aChild->nextSibling;
1339 /* Look at the next item */
1340 continue;
1342 else if (comp == 0)
1345 * An item with this name is already existing, therefore,
1346 * we add after the one we found
1348 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1349 bItemInserted = TRUE;
1350 break;
1355 * we reach the end of the child list and the item has not
1356 * yet been inserted, therefore, insert it after the last child.
1358 if ((!bItemInserted) && (aChild == NULL))
1359 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1361 break;
1366 TRACE("new item %p; parent %p, mask %x\n", newItem,
1367 newItem->parent, tvItem->mask);
1369 newItem->iLevel = newItem->parent->iLevel + 1;
1371 if (newItem->parent->cChildren == 0)
1372 newItem->parent->cChildren = 1;
1374 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1376 if (STATEIMAGEINDEX(newItem->state) == 0)
1377 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1380 if (infoPtr->firstVisible == NULL)
1381 infoPtr->firstVisible = newItem;
1383 TREEVIEW_VerifyTree(infoPtr);
1385 if (parentItem == infoPtr->root ||
1386 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1388 TREEVIEW_ITEM *item;
1389 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1391 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1392 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1394 if (!bTextUpdated)
1395 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1397 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1398 TREEVIEW_UpdateScrollBars(infoPtr);
1400 * if the item was inserted in a visible part of the tree,
1401 * invalidate it, as well as those after it
1403 for (item = newItem;
1404 item != NULL;
1405 item = TREEVIEW_GetNextListItem(infoPtr, item))
1406 TREEVIEW_Invalidate(infoPtr, item);
1408 else
1410 newItem->visibleOrder = -1;
1412 /* refresh treeview if newItem is the first item inserted under parentItem */
1413 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1415 /* parent got '+' - update it */
1416 TREEVIEW_Invalidate(infoPtr, parentItem);
1420 return (LRESULT)newItem;
1423 /* Item Deletion ************************************************************/
1424 static void
1425 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1427 static void
1428 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1430 TREEVIEW_ITEM *kill = parentItem->firstChild;
1432 while (kill != NULL)
1434 TREEVIEW_ITEM *next = kill->nextSibling;
1436 TREEVIEW_RemoveItem(infoPtr, kill);
1438 kill = next;
1441 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1442 assert(parentItem->firstChild == NULL);
1443 assert(parentItem->lastChild == NULL);
1446 static void
1447 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1449 TREEVIEW_ITEM *parentItem = item->parent;
1451 assert(item != NULL);
1452 assert(item->parent != NULL); /* i.e. it must not be the root */
1454 if (parentItem->firstChild == item)
1455 parentItem->firstChild = item->nextSibling;
1457 if (parentItem->lastChild == item)
1458 parentItem->lastChild = item->prevSibling;
1460 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1461 && parentItem->cChildren > 0)
1462 parentItem->cChildren = 0;
1464 if (item->prevSibling)
1465 item->prevSibling->nextSibling = item->nextSibling;
1467 if (item->nextSibling)
1468 item->nextSibling->prevSibling = item->prevSibling;
1471 static void
1472 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1474 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1476 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1477 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1479 if (wineItem->firstChild)
1480 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1482 TREEVIEW_UnlinkItem(wineItem);
1484 infoPtr->uNumItems--;
1486 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1487 Free(wineItem->pszText);
1489 TREEVIEW_FreeItem(infoPtr, wineItem);
1493 /* Empty out the tree. */
1494 static void
1495 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1497 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1499 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1502 static LRESULT
1503 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1505 TREEVIEW_ITEM *newSelection = NULL;
1506 TREEVIEW_ITEM *newFirstVisible = NULL;
1507 TREEVIEW_ITEM *parent, *prev = NULL;
1508 BOOL visible = FALSE;
1510 if (wineItem == TVI_ROOT)
1512 TRACE("TVI_ROOT\n");
1513 parent = infoPtr->root;
1514 newSelection = NULL;
1515 visible = TRUE;
1516 TREEVIEW_RemoveTree(infoPtr);
1518 else
1520 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1521 return FALSE;
1523 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1524 parent = wineItem->parent;
1526 if (ISVISIBLE(wineItem))
1528 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1529 visible = TRUE;
1532 if (infoPtr->selectedItem != NULL
1533 && (wineItem == infoPtr->selectedItem
1534 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1536 if (wineItem->nextSibling)
1537 newSelection = wineItem->nextSibling;
1538 else if (wineItem->parent != infoPtr->root)
1539 newSelection = wineItem->parent;
1540 else
1541 newSelection = wineItem->prevSibling;
1542 TRACE("newSelection = %p\n", newSelection);
1545 if (infoPtr->firstVisible == wineItem)
1547 if (wineItem->nextSibling)
1548 newFirstVisible = wineItem->nextSibling;
1549 else if (wineItem->prevSibling)
1550 newFirstVisible = wineItem->prevSibling;
1551 else if (wineItem->parent != infoPtr->root)
1552 newFirstVisible = wineItem->parent;
1553 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1555 else
1556 newFirstVisible = infoPtr->firstVisible;
1558 TREEVIEW_RemoveItem(infoPtr, wineItem);
1561 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1562 if (!infoPtr->selectedItem && newSelection)
1564 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1565 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1568 /* Validate insertMark dropItem.
1569 * hotItem ??? - used for comparison only.
1571 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1572 infoPtr->insertMarkItem = 0;
1574 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1575 infoPtr->dropItem = 0;
1577 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1578 newFirstVisible = infoPtr->root->firstChild;
1580 TREEVIEW_VerifyTree(infoPtr);
1583 if (visible)
1585 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1586 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1587 TREEVIEW_UpdateScrollBars(infoPtr);
1588 TREEVIEW_Invalidate(infoPtr, NULL);
1590 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1592 /* parent lost '+/-' - update it */
1593 TREEVIEW_Invalidate(infoPtr, parent);
1596 return TRUE;
1600 /* Get/Set Messages *********************************************************/
1601 static LRESULT
1602 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1604 if(wParam)
1605 infoPtr->bRedraw = TRUE;
1606 else
1607 infoPtr->bRedraw = FALSE;
1609 return 0;
1612 static LRESULT
1613 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1615 TRACE("\n");
1616 return infoPtr->uIndent;
1619 static LRESULT
1620 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1622 TRACE("\n");
1624 if (newIndent < MINIMUM_INDENT)
1625 newIndent = MINIMUM_INDENT;
1627 if (infoPtr->uIndent != newIndent)
1629 infoPtr->uIndent = newIndent;
1630 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1631 TREEVIEW_UpdateScrollBars(infoPtr);
1632 TREEVIEW_Invalidate(infoPtr, NULL);
1635 return 0;
1639 static LRESULT
1640 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1642 TRACE("\n");
1643 return (LRESULT)infoPtr->hwndToolTip;
1646 static LRESULT
1647 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1649 HWND prevToolTip;
1651 TRACE("\n");
1652 prevToolTip = infoPtr->hwndToolTip;
1653 infoPtr->hwndToolTip = hwndTT;
1655 return (LRESULT)prevToolTip;
1658 static LRESULT
1659 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1661 BOOL rc = infoPtr->bNtfUnicode;
1662 infoPtr->bNtfUnicode = fUnicode;
1663 return rc;
1666 static LRESULT
1667 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1669 return infoPtr->bNtfUnicode;
1672 static LRESULT
1673 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1675 return infoPtr->uScrollTime;
1678 static LRESULT
1679 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1681 UINT uOldScrollTime = infoPtr->uScrollTime;
1683 infoPtr->uScrollTime = min(uScrollTime, 100);
1685 return uOldScrollTime;
1689 static LRESULT
1690 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1692 TRACE("\n");
1694 switch (wParam)
1696 case (WPARAM)TVSIL_NORMAL:
1697 return (LRESULT)infoPtr->himlNormal;
1699 case (WPARAM)TVSIL_STATE:
1700 return (LRESULT)infoPtr->himlState;
1702 default:
1703 return 0;
1707 #define TVHEIGHT_MIN 16
1708 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1710 /* Compute the natural height for items. */
1711 static UINT
1712 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1714 TEXTMETRICW tm;
1715 HDC hdc = GetDC(0);
1716 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1717 UINT height;
1719 /* Height is the maximum of:
1720 * 16 (a hack because our fonts are tiny), and
1721 * The text height + border & margin, and
1722 * The size of the normal image list
1724 GetTextMetricsW(hdc, &tm);
1725 SelectObject(hdc, hOldFont);
1726 ReleaseDC(0, hdc);
1728 height = TVHEIGHT_MIN;
1729 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1730 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1731 if (height < infoPtr->normalImageHeight)
1732 height = infoPtr->normalImageHeight;
1734 /* Round down, unless we support odd ("non even") heights. */
1735 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1736 height &= ~1;
1738 return height;
1741 static LRESULT
1742 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1744 HIMAGELIST himlOld = 0;
1745 int oldWidth = infoPtr->normalImageWidth;
1746 int oldHeight = infoPtr->normalImageHeight;
1749 TRACE("%x,%p\n", wParam, himlNew);
1751 switch (wParam)
1753 case (WPARAM)TVSIL_NORMAL:
1754 himlOld = infoPtr->himlNormal;
1755 infoPtr->himlNormal = himlNew;
1757 if (himlNew != NULL)
1758 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1759 &infoPtr->normalImageHeight);
1760 else
1762 infoPtr->normalImageWidth = 0;
1763 infoPtr->normalImageHeight = 0;
1766 break;
1768 case (WPARAM)TVSIL_STATE:
1769 himlOld = infoPtr->himlState;
1770 infoPtr->himlState = himlNew;
1772 if (himlNew != NULL)
1773 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1774 &infoPtr->stateImageHeight);
1775 else
1777 infoPtr->stateImageWidth = 0;
1778 infoPtr->stateImageHeight = 0;
1781 break;
1784 if (oldWidth != infoPtr->normalImageWidth ||
1785 oldHeight != infoPtr->normalImageHeight)
1787 BOOL bRecalcVisible = FALSE;
1789 if (oldHeight != infoPtr->normalImageHeight &&
1790 !infoPtr->bHeightSet)
1792 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1793 bRecalcVisible = TRUE;
1796 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1797 infoPtr->normalImageWidth != infoPtr->uIndent)
1799 infoPtr->uIndent = infoPtr->normalImageWidth;
1800 bRecalcVisible = TRUE;
1803 if (bRecalcVisible)
1804 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1806 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1807 TREEVIEW_UpdateScrollBars(infoPtr);
1810 TREEVIEW_Invalidate(infoPtr, NULL);
1812 return (LRESULT)himlOld;
1815 static LRESULT
1816 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1818 INT prevHeight = infoPtr->uItemHeight;
1820 TRACE("%d\n", newHeight);
1821 if (newHeight == -1)
1823 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1824 infoPtr->bHeightSet = FALSE;
1826 else
1828 infoPtr->uItemHeight = newHeight;
1829 infoPtr->bHeightSet = TRUE;
1832 /* Round down, unless we support odd ("non even") heights. */
1833 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1834 infoPtr->uItemHeight &= ~1;
1836 if (infoPtr->uItemHeight != prevHeight)
1838 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1839 TREEVIEW_UpdateScrollBars(infoPtr);
1840 TREEVIEW_Invalidate(infoPtr, NULL);
1843 return prevHeight;
1846 static LRESULT
1847 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1849 TRACE("\n");
1850 return infoPtr->uItemHeight;
1854 static LRESULT
1855 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1857 TRACE("%p\n", infoPtr->hFont);
1858 return (LRESULT)infoPtr->hFont;
1862 static INT CALLBACK
1863 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1865 (void)unused;
1867 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1869 return 1;
1872 static LRESULT
1873 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1875 UINT uHeight = infoPtr->uItemHeight;
1877 TRACE("%p %i\n", hFont, bRedraw);
1879 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1881 DeleteObject(infoPtr->hBoldFont);
1882 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1883 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1885 if (!infoPtr->bHeightSet)
1886 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1888 if (uHeight != infoPtr->uItemHeight)
1889 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1891 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1893 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1894 TREEVIEW_UpdateScrollBars(infoPtr);
1896 if (bRedraw)
1897 TREEVIEW_Invalidate(infoPtr, NULL);
1899 return 0;
1903 static LRESULT
1904 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1906 TRACE("\n");
1907 return (LRESULT)infoPtr->clrLine;
1910 static LRESULT
1911 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1913 COLORREF prevColor = infoPtr->clrLine;
1915 TRACE("\n");
1916 infoPtr->clrLine = color;
1917 return (LRESULT)prevColor;
1921 static LRESULT
1922 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1924 TRACE("\n");
1925 return (LRESULT)infoPtr->clrText;
1928 static LRESULT
1929 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1931 COLORREF prevColor = infoPtr->clrText;
1933 TRACE("\n");
1934 infoPtr->clrText = color;
1936 if (infoPtr->clrText != prevColor)
1937 TREEVIEW_Invalidate(infoPtr, NULL);
1939 return (LRESULT)prevColor;
1943 static LRESULT
1944 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1946 TRACE("\n");
1947 return (LRESULT)infoPtr->clrBk;
1950 static LRESULT
1951 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1953 COLORREF prevColor = infoPtr->clrBk;
1955 TRACE("\n");
1956 infoPtr->clrBk = newColor;
1958 if (newColor != prevColor)
1959 TREEVIEW_Invalidate(infoPtr, NULL);
1961 return (LRESULT)prevColor;
1965 static LRESULT
1966 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1968 TRACE("\n");
1969 return (LRESULT)infoPtr->clrInsertMark;
1972 static LRESULT
1973 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1975 COLORREF prevColor = infoPtr->clrInsertMark;
1977 TRACE("%x\n", color);
1978 infoPtr->clrInsertMark = color;
1980 return (LRESULT)prevColor;
1984 static LRESULT
1985 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1987 TRACE("%d %p\n", wParam, item);
1989 if (!TREEVIEW_ValidItem(infoPtr, item))
1990 return 0;
1992 infoPtr->insertBeforeorAfter = wParam;
1993 infoPtr->insertMarkItem = item;
1995 TREEVIEW_Invalidate(infoPtr, NULL);
1997 return 1;
2001 /************************************************************************
2002 * Some serious braindamage here. lParam is a pointer to both the
2003 * input HTREEITEM and the output RECT.
2005 static LRESULT
2006 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2008 TREEVIEW_ITEM *wineItem;
2009 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2011 TRACE("\n");
2013 * validate parameters
2015 if (pItem == NULL)
2016 return FALSE;
2018 wineItem = *pItem;
2019 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2020 return FALSE;
2023 * If wParam is TRUE return the text size otherwise return
2024 * the whole item size
2026 if (fTextRect)
2028 /* Windows does not send TVN_GETDISPINFO here. */
2030 lpRect->top = wineItem->rect.top;
2031 lpRect->bottom = wineItem->rect.bottom;
2033 lpRect->left = wineItem->textOffset;
2034 lpRect->right = wineItem->textOffset + wineItem->textWidth;
2036 else
2038 *lpRect = wineItem->rect;
2041 TRACE("%s [L:%d R:%d T:%d B:%d]\n", fTextRect ? "text" : "item",
2042 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
2044 return TRUE;
2047 static inline LRESULT
2048 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2050 /* Suprise! This does not take integral height into account. */
2051 return infoPtr->clientHeight / infoPtr->uItemHeight;
2055 static LRESULT
2056 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2058 TREEVIEW_ITEM *wineItem;
2060 wineItem = tvItem->hItem;
2061 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2062 return FALSE;
2064 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2066 if (tvItem->mask & TVIF_CHILDREN)
2068 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2069 FIXME("I_CHILDRENCALLBACK not supported\n");
2070 tvItem->cChildren = wineItem->cChildren;
2073 if (tvItem->mask & TVIF_HANDLE)
2074 tvItem->hItem = wineItem;
2076 if (tvItem->mask & TVIF_IMAGE)
2077 tvItem->iImage = wineItem->iImage;
2079 if (tvItem->mask & TVIF_INTEGRAL)
2080 tvItem->iIntegral = wineItem->iIntegral;
2082 /* undocumented: windows ignores TVIF_PARAM and
2083 * * always sets lParam
2085 tvItem->lParam = wineItem->lParam;
2087 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2088 tvItem->iSelectedImage = wineItem->iSelectedImage;
2090 if (tvItem->mask & TVIF_STATE)
2091 /* Careful here - Windows ignores the stateMask when you get the state
2092 That contradicts the documentation, but makes more common sense, masking
2093 retrieval in this way seems overkill */
2094 tvItem->state = wineItem->state;
2096 if (tvItem->mask & TVIF_TEXT)
2098 if (isW)
2100 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2102 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2103 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2105 else
2107 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2110 else
2112 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2114 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2115 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2117 else
2119 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2120 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2124 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2125 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2127 return TRUE;
2130 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2131 * which is wrong. */
2132 static LRESULT
2133 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2135 TREEVIEW_ITEM *wineItem;
2136 TREEVIEW_ITEM originalItem;
2138 wineItem = tvItem->hItem;
2140 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2141 tvItem->mask);
2143 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2144 return FALSE;
2146 /* store the orignal item values */
2147 originalItem = *wineItem;
2149 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2150 return FALSE;
2152 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2153 if ((tvItem->mask & TVIF_TEXT
2154 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2155 && ISVISIBLE(wineItem))
2157 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2158 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2161 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2163 /* The refresh updates everything, but we can't wait until then. */
2164 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2166 /* if any of the item's values changed and it's not a callback, redraw the item */
2167 if (item_changed(&originalItem, wineItem, tvItem))
2169 if (tvItem->mask & TVIF_INTEGRAL)
2171 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2172 TREEVIEW_UpdateScrollBars(infoPtr);
2174 TREEVIEW_Invalidate(infoPtr, NULL);
2176 else
2178 TREEVIEW_UpdateScrollBars(infoPtr);
2179 TREEVIEW_Invalidate(infoPtr, wineItem);
2184 return TRUE;
2187 static LRESULT
2188 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2190 TRACE("\n");
2192 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2193 return 0;
2195 return (wineItem->state & mask);
2198 static LRESULT
2199 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2201 TREEVIEW_ITEM *retval;
2203 retval = 0;
2205 /* handle all the global data here */
2206 switch (which)
2208 case TVGN_CHILD: /* Special case: child of 0 is root */
2209 if (wineItem)
2210 break;
2211 /* fall through */
2212 case TVGN_ROOT:
2213 retval = infoPtr->root->firstChild;
2214 break;
2216 case TVGN_CARET:
2217 retval = infoPtr->selectedItem;
2218 break;
2220 case TVGN_FIRSTVISIBLE:
2221 retval = infoPtr->firstVisible;
2222 break;
2224 case TVGN_DROPHILITE:
2225 retval = infoPtr->dropItem;
2226 break;
2228 case TVGN_LASTVISIBLE:
2229 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2230 break;
2233 if (retval)
2235 TRACE("flags:%x, returns %p\n", which, retval);
2236 return (LRESULT)retval;
2239 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2241 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2242 return FALSE;
2244 switch (which)
2246 case TVGN_NEXT:
2247 retval = wineItem->nextSibling;
2248 break;
2249 case TVGN_PREVIOUS:
2250 retval = wineItem->prevSibling;
2251 break;
2252 case TVGN_PARENT:
2253 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2254 break;
2255 case TVGN_CHILD:
2256 retval = wineItem->firstChild;
2257 break;
2258 case TVGN_NEXTVISIBLE:
2259 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2260 break;
2261 case TVGN_PREVIOUSVISIBLE:
2262 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2263 break;
2264 default:
2265 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2266 break;
2269 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2270 return (LRESULT)retval;
2274 static LRESULT
2275 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2277 TRACE(" %d\n", infoPtr->uNumItems);
2278 return (LRESULT)infoPtr->uNumItems;
2281 static VOID
2282 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2284 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2286 static const unsigned int state_table[] = { 0, 2, 1 };
2288 unsigned int state;
2290 state = STATEIMAGEINDEX(item->state);
2291 TRACE("state:%x\n", state);
2292 item->state &= ~TVIS_STATEIMAGEMASK;
2294 if (state < 3)
2295 state = state_table[state];
2297 item->state |= INDEXTOSTATEIMAGEMASK(state);
2299 TRACE("state:%x\n", state);
2300 TREEVIEW_Invalidate(infoPtr, item);
2305 /* Painting *************************************************************/
2307 /* Draw the lines and expand button for an item. Also draws one section
2308 * of the line from item's parent to item's parent's next sibling. */
2309 static void
2310 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2312 LONG centerx, centery;
2313 BOOL lar = ((infoPtr->dwStyle
2314 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2315 > TVS_LINESATROOT);
2316 HBRUSH hbr, hbrOld;
2318 if (!lar && item->iLevel == 0)
2319 return;
2321 hbr = CreateSolidBrush(infoPtr->clrBk);
2322 hbrOld = SelectObject(hdc, hbr);
2324 centerx = (item->linesOffset + item->stateOffset) / 2;
2325 centery = (item->rect.top + item->rect.bottom) / 2;
2327 if (infoPtr->dwStyle & TVS_HASLINES)
2329 HPEN hOldPen, hNewPen;
2330 HTREEITEM parent;
2331 LOGBRUSH lb;
2333 /* Get a dotted grey pen */
2334 lb.lbStyle = BS_SOLID;
2335 lb.lbColor = infoPtr->clrLine;
2336 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2337 hOldPen = SelectObject(hdc, hNewPen);
2339 /* Make sure the center is on a dot (using +2 instead
2340 * of +1 gives us pixel-by-pixel compat with native) */
2341 centery = (centery + 2) & ~1;
2343 MoveToEx(hdc, item->stateOffset, centery, NULL);
2344 LineTo(hdc, centerx - 1, centery);
2346 if (item->prevSibling || item->parent != infoPtr->root)
2348 MoveToEx(hdc, centerx, item->rect.top, NULL);
2349 LineTo(hdc, centerx, centery);
2352 if (item->nextSibling)
2354 MoveToEx(hdc, centerx, centery, NULL);
2355 LineTo(hdc, centerx, item->rect.bottom + 1);
2358 /* Draw the line from our parent to its next sibling. */
2359 parent = item->parent;
2360 while (parent != infoPtr->root)
2362 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2364 if (parent->nextSibling
2365 /* skip top-levels unless TVS_LINESATROOT */
2366 && parent->stateOffset > parent->linesOffset)
2368 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2369 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2372 parent = parent->parent;
2375 SelectObject(hdc, hOldPen);
2376 DeleteObject(hNewPen);
2380 * Display the (+/-) signs
2383 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2385 if (item->cChildren)
2387 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2388 if (theme)
2390 RECT glyphRect = item->rect;
2391 glyphRect.left = item->linesOffset;
2392 glyphRect.right = item->stateOffset;
2393 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2394 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2395 &glyphRect, NULL);
2397 else
2399 LONG height = item->rect.bottom - item->rect.top;
2400 LONG width = item->stateOffset - item->linesOffset;
2401 LONG rectsize = min(height, width) / 4;
2402 /* plussize = ceil(rectsize * 3/4) */
2403 LONG plussize = (rectsize + 1) * 3 / 4;
2405 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2406 HPEN hOldPen = SelectObject(hdc, hNewPen);
2408 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2409 centerx + rectsize + 2, centery + rectsize + 2);
2411 SelectObject(hdc, hOldPen);
2412 DeleteObject(hNewPen);
2414 if (height < 18 || width < 18)
2416 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2417 LineTo(hdc, centerx + plussize, centery);
2419 if (!(item->state & TVIS_EXPANDED))
2421 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2422 LineTo(hdc, centerx, centery + plussize);
2425 else
2427 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2428 centerx + plussize, centery + 2);
2430 if (!(item->state & TVIS_EXPANDED))
2432 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2433 centerx + 2, centery + plussize);
2434 SetPixel(hdc, centerx - 1, centery, infoPtr->clrBk);
2435 SetPixel(hdc, centerx + 1, centery, infoPtr->clrBk);
2441 SelectObject(hdc, hbrOld);
2442 DeleteObject(hbr);
2445 static void
2446 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2448 INT cditem;
2449 HFONT hOldFont;
2450 COLORREF oldTextColor, oldTextBkColor;
2451 int centery;
2452 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2453 NMTVCUSTOMDRAW nmcdhdr;
2455 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2457 /* - If item is drop target or it is selected and window is in focus -
2458 * use blue background (COLOR_HIGHLIGHT).
2459 * - If item is selected, window is not in focus, but it has style
2460 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2461 * - Otherwise - use background color
2463 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2464 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2465 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2467 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2469 nmcdhdr.clrTextBk = GetSysColor(COLOR_HIGHLIGHT);
2470 nmcdhdr.clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
2472 else
2474 nmcdhdr.clrTextBk = GetSysColor(COLOR_BTNFACE);
2475 if (infoPtr->clrText == -1)
2476 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2477 else
2478 nmcdhdr.clrText = infoPtr->clrText;
2481 else
2483 nmcdhdr.clrTextBk = infoPtr->clrBk;
2484 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2485 nmcdhdr.clrText = comctl32_color.clrHighlight;
2486 else if (infoPtr->clrText == -1)
2487 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2488 else
2489 nmcdhdr.clrText = infoPtr->clrText;
2492 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2494 /* The custom draw handler can query the text rectangle,
2495 * so get ready. */
2496 /* should already be known, set to 0 when changed */
2497 if (!wineItem->textWidth)
2498 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2500 cditem = 0;
2502 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2504 cditem = TREEVIEW_SendCustomDrawItemNotify
2505 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2506 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2508 if (cditem & CDRF_SKIPDEFAULT)
2510 SelectObject(hdc, hOldFont);
2511 return;
2515 if (cditem & CDRF_NEWFONT)
2516 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2518 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2520 /* Set colors. Custom draw handler can change these so we do this after it. */
2521 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2522 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2524 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2527 * Display the images associated with this item
2530 INT imageIndex;
2532 /* State images are displayed to the left of the Normal image
2533 * image number is in state; zero should be `display no image'.
2535 imageIndex = STATEIMAGEINDEX(wineItem->state);
2537 if (infoPtr->himlState && imageIndex)
2539 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2540 wineItem->stateOffset,
2541 centery - infoPtr->stateImageHeight / 2,
2542 ILD_NORMAL);
2545 /* Now, draw the normal image; can be either selected or
2546 * non-selected image.
2549 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2551 /* The item is currently selected */
2552 imageIndex = wineItem->iSelectedImage;
2554 else
2556 /* The item is not selected */
2557 imageIndex = wineItem->iImage;
2560 if (infoPtr->himlNormal)
2562 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2564 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2565 wineItem->imageOffset,
2566 centery - infoPtr->normalImageHeight / 2,
2567 ILD_NORMAL | ovlIdx);
2573 * Display the text associated with this item
2576 /* Don't paint item's text if it's being edited */
2577 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2579 if (wineItem->pszText)
2581 RECT rcText;
2583 rcText.top = wineItem->rect.top;
2584 rcText.bottom = wineItem->rect.bottom;
2585 rcText.left = wineItem->textOffset;
2586 rcText.right = rcText.left + wineItem->textWidth + 4;
2588 TRACE("drawing text %s at (%d,%d)-(%d,%d)\n",
2589 debugstr_w(wineItem->pszText),
2590 rcText.left, rcText.top, rcText.right, rcText.bottom);
2592 /* Draw it */
2593 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2594 ETO_CLIPPED | ETO_OPAQUE,
2595 &rcText,
2596 wineItem->pszText,
2597 lstrlenW(wineItem->pszText),
2598 NULL);
2600 /* Draw the box around the selected item */
2601 if ((wineItem == infoPtr->selectedItem) && inFocus)
2603 DrawFocusRect(hdc,&rcText);
2609 /* Draw insertion mark if necessary */
2611 if (infoPtr->insertMarkItem)
2612 TRACE("item:%d,mark:%p\n",
2613 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2614 infoPtr->insertMarkItem);
2616 if (wineItem == infoPtr->insertMarkItem)
2618 HPEN hNewPen, hOldPen;
2619 int offset;
2620 int left, right;
2622 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2623 hOldPen = SelectObject(hdc, hNewPen);
2625 if (infoPtr->insertBeforeorAfter)
2626 offset = wineItem->rect.bottom - 1;
2627 else
2628 offset = wineItem->rect.top + 1;
2630 left = wineItem->textOffset - 2;
2631 right = wineItem->textOffset + wineItem->textWidth + 2;
2633 MoveToEx(hdc, left, offset - 3, NULL);
2634 LineTo(hdc, left, offset + 4);
2636 MoveToEx(hdc, left, offset, NULL);
2637 LineTo(hdc, right + 1, offset);
2639 MoveToEx(hdc, right, offset + 3, NULL);
2640 LineTo(hdc, right, offset - 4);
2642 SelectObject(hdc, hOldPen);
2643 DeleteObject(hNewPen);
2646 if (cditem & CDRF_NOTIFYPOSTPAINT)
2648 cditem = TREEVIEW_SendCustomDrawItemNotify
2649 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2650 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2653 /* Restore the hdc state */
2654 SetTextColor(hdc, oldTextColor);
2655 SetBkColor(hdc, oldTextBkColor);
2656 SelectObject(hdc, hOldFont);
2659 /* Computes treeHeight and treeWidth and updates the scroll bars.
2661 static void
2662 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2664 TREEVIEW_ITEM *wineItem;
2665 HWND hwnd = infoPtr->hwnd;
2666 BOOL vert = FALSE;
2667 BOOL horz = FALSE;
2668 SCROLLINFO si;
2669 LONG scrollX = infoPtr->scrollX;
2671 infoPtr->treeWidth = 0;
2672 infoPtr->treeHeight = 0;
2674 /* We iterate through all visible items in order to get the tree height
2675 * and width */
2676 wineItem = infoPtr->root->firstChild;
2678 while (wineItem != NULL)
2680 if (ISVISIBLE(wineItem))
2682 /* actually we draw text at textOffset + 2 */
2683 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2684 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2686 /* This is scroll-adjusted, but we fix this below. */
2687 infoPtr->treeHeight = wineItem->rect.bottom;
2690 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2693 /* Fix the scroll adjusted treeHeight and treeWidth. */
2694 if (infoPtr->root->firstChild)
2695 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2697 infoPtr->treeWidth += infoPtr->scrollX;
2699 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2701 /* Adding one scroll bar may take up enough space that it forces us
2702 * to add the other as well. */
2703 if (infoPtr->treeHeight > infoPtr->clientHeight)
2705 vert = TRUE;
2707 if (infoPtr->treeWidth
2708 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2709 horz = TRUE;
2711 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2712 horz = TRUE;
2714 if (!vert && horz && infoPtr->treeHeight
2715 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2716 vert = TRUE;
2718 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2720 si.cbSize = sizeof(SCROLLINFO);
2721 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2722 si.nMin = 0;
2724 if (vert)
2726 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2727 if ( si.nPage && NULL != infoPtr->firstVisible)
2729 si.nPos = infoPtr->firstVisible->visibleOrder;
2730 si.nMax = infoPtr->maxVisibleOrder - 1;
2732 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2734 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2735 ShowScrollBar(hwnd, SB_VERT, TRUE);
2736 infoPtr->uInternalStatus |= TV_VSCROLL;
2738 else
2740 if (infoPtr->uInternalStatus & TV_VSCROLL)
2741 ShowScrollBar(hwnd, SB_VERT, FALSE);
2742 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2745 else
2747 if (infoPtr->uInternalStatus & TV_VSCROLL)
2748 ShowScrollBar(hwnd, SB_VERT, FALSE);
2749 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2752 if (horz)
2754 si.nPage = infoPtr->clientWidth;
2755 si.nPos = infoPtr->scrollX;
2756 si.nMax = infoPtr->treeWidth - 1;
2758 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2760 si.nPos = si.nMax - max( si.nPage-1, 0 );
2761 scrollX = si.nPos;
2764 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2765 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2766 infoPtr->uInternalStatus |= TV_HSCROLL;
2768 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2769 TREEVIEW_HScroll(infoPtr,
2770 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2772 else
2774 if (infoPtr->uInternalStatus & TV_HSCROLL)
2775 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2776 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2778 scrollX = 0;
2779 if (infoPtr->scrollX != 0)
2781 TREEVIEW_HScroll(infoPtr,
2782 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2786 if (!horz)
2787 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2790 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2791 static LRESULT
2792 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hDC)
2794 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2795 RECT rect;
2797 GetClientRect(infoPtr->hwnd, &rect);
2798 FillRect(hDC, &rect, hBrush);
2799 DeleteObject(hBrush);
2801 return 1;
2804 static void
2805 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2807 HWND hwnd = infoPtr->hwnd;
2808 RECT rect = *rc;
2809 TREEVIEW_ITEM *wineItem;
2811 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2813 TRACE("empty window\n");
2814 return;
2817 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2818 hdc, rect);
2820 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2822 ReleaseDC(hwnd, hdc);
2823 return;
2826 for (wineItem = infoPtr->root->firstChild;
2827 wineItem != NULL;
2828 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2830 if (ISVISIBLE(wineItem))
2832 /* Avoid unneeded calculations */
2833 if (wineItem->rect.top > rect.bottom)
2834 break;
2835 if (wineItem->rect.bottom < rect.top)
2836 continue;
2838 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2842 TREEVIEW_UpdateScrollBars(infoPtr);
2844 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2845 infoPtr->cdmode =
2846 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2849 static void
2850 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2852 if (item != NULL)
2853 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2854 else
2855 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2858 static LRESULT
2859 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2861 HDC hdc;
2862 PAINTSTRUCT ps;
2863 RECT rc;
2865 TRACE("\n");
2867 if (wParam)
2869 hdc = (HDC)wParam;
2870 GetClientRect(infoPtr->hwnd, &rc);
2871 TREEVIEW_EraseBackground(infoPtr, hdc);
2873 else
2875 hdc = BeginPaint(infoPtr->hwnd, &ps);
2876 rc = ps.rcPaint;
2879 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2880 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2882 if (!wParam)
2883 EndPaint(infoPtr->hwnd, &ps);
2885 return 0;
2889 /* Sorting **************************************************************/
2891 /***************************************************************************
2892 * Forward the DPA local callback to the treeview owner callback
2894 static INT WINAPI
2895 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2896 const TVSORTCB *pCallBackSort)
2898 /* Forward the call to the client-defined callback */
2899 return pCallBackSort->lpfnCompare(first->lParam,
2900 second->lParam,
2901 pCallBackSort->lParam);
2904 /***************************************************************************
2905 * Treeview native sort routine: sort on item text.
2907 static INT WINAPI
2908 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2909 const TREEVIEW_INFO *infoPtr)
2911 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2912 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2914 if(first->pszText && second->pszText)
2915 return lstrcmpiW(first->pszText, second->pszText);
2916 else if(first->pszText)
2917 return -1;
2918 else if(second->pszText)
2919 return 1;
2920 else
2921 return 0;
2924 /* Returns the number of physical children belonging to item. */
2925 static INT
2926 TREEVIEW_CountChildren(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2928 INT cChildren = 0;
2929 HTREEITEM hti;
2931 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2932 cChildren++;
2934 return cChildren;
2937 /* Returns a DPA containing a pointer to each physical child of item in
2938 * sibling order. If item has no children, an empty DPA is returned. */
2939 static HDPA
2940 TREEVIEW_BuildChildDPA(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2942 HTREEITEM child = item->firstChild;
2944 HDPA list = DPA_Create(8);
2945 if (list == 0) return NULL;
2947 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2949 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2951 DPA_Destroy(list);
2952 return NULL;
2956 return list;
2959 /***************************************************************************
2960 * Setup the treeview structure with regards of the sort method
2961 * and sort the children of the TV item specified in lParam
2962 * fRecurse: currently unused. Should be zero.
2963 * parent: if pSort!=NULL, should equal pSort->hParent.
2964 * otherwise, item which child items are to be sorted.
2965 * pSort: sort method info. if NULL, sort on item text.
2966 * if non-NULL, sort on item's lParam content, and let the
2967 * application decide what that means. See also TVM_SORTCHILDRENCB.
2970 static LRESULT
2971 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2972 LPTVSORTCB pSort)
2974 INT cChildren;
2975 PFNDPACOMPARE pfnCompare;
2976 LPARAM lpCompare;
2978 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2979 if (parent == TVI_ROOT || parent == NULL)
2980 parent = infoPtr->root;
2982 /* Check for a valid handle to the parent item */
2983 if (!TREEVIEW_ValidItem(infoPtr, parent))
2985 ERR("invalid item hParent=%p\n", parent);
2986 return FALSE;
2989 if (pSort)
2991 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2992 lpCompare = (LPARAM)pSort;
2994 else
2996 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2997 lpCompare = (LPARAM)infoPtr;
3000 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
3002 /* Make sure there is something to sort */
3003 if (cChildren > 1)
3005 /* TREEVIEW_ITEM rechaining */
3006 INT count = 0;
3007 HTREEITEM item = 0;
3008 HTREEITEM nextItem = 0;
3009 HTREEITEM prevItem = 0;
3011 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
3013 if (sortList == NULL)
3014 return FALSE;
3016 /* let DPA sort the list */
3017 DPA_Sort(sortList, pfnCompare, lpCompare);
3019 /* The order of DPA entries has been changed, so fixup the
3020 * nextSibling and prevSibling pointers. */
3022 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
3023 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
3025 /* link the two current item toghether */
3026 item->nextSibling = nextItem;
3027 nextItem->prevSibling = item;
3029 if (prevItem == NULL)
3031 /* this is the first item, update the parent */
3032 parent->firstChild = item;
3033 item->prevSibling = NULL;
3035 else
3037 /* fix the back chaining */
3038 item->prevSibling = prevItem;
3041 /* get ready for the next one */
3042 prevItem = item;
3043 item = nextItem;
3046 /* the last item is pointed to by item and never has a sibling */
3047 item->nextSibling = NULL;
3048 parent->lastChild = item;
3050 DPA_Destroy(sortList);
3052 TREEVIEW_VerifyTree(infoPtr);
3054 if (parent->state & TVIS_EXPANDED)
3056 int visOrder = infoPtr->firstVisible->visibleOrder;
3058 if (parent == infoPtr->root)
3059 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3060 else
3061 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3063 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3065 TREEVIEW_ITEM *item;
3067 for (item = infoPtr->root->firstChild; item != NULL;
3068 item = TREEVIEW_GetNextListItem(infoPtr, item))
3070 if (item->visibleOrder == visOrder)
3071 break;
3074 if (!item) item = parent->firstChild;
3075 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3078 TREEVIEW_Invalidate(infoPtr, NULL);
3081 return TRUE;
3083 return FALSE;
3087 /***************************************************************************
3088 * Setup the treeview structure with regards of the sort method
3089 * and sort the children of the TV item specified in lParam
3091 static LRESULT
3092 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3094 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3098 /***************************************************************************
3099 * Sort the children of the TV item specified in lParam.
3101 static LRESULT
3102 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3104 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3108 /* Expansion/Collapse ***************************************************/
3110 static BOOL
3111 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3112 UINT action)
3114 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3115 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3116 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3117 0, wineItem);
3120 static VOID
3121 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3122 UINT action)
3124 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3125 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3126 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3127 0, wineItem);
3131 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3132 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3133 static BOOL
3134 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3135 BOOL bRemoveChildren, BOOL bUser)
3137 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3138 BOOL bSetSelection, bSetFirstVisible;
3139 RECT scrollRect;
3140 LONG scrollDist = 0;
3141 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3143 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3145 if (!(wineItem->state & TVIS_EXPANDED))
3146 return FALSE;
3148 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3149 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3151 if (wineItem->firstChild == NULL)
3152 return FALSE;
3154 wineItem->state &= ~TVIS_EXPANDED;
3156 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3157 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3159 bSetSelection = (infoPtr->selectedItem != NULL
3160 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3162 bSetFirstVisible = (infoPtr->firstVisible != NULL
3163 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3165 tmpItem = wineItem;
3166 while (tmpItem)
3168 if (tmpItem->nextSibling)
3170 nextItem = tmpItem->nextSibling;
3171 break;
3173 tmpItem = tmpItem->parent;
3176 if (nextItem)
3177 scrollDist = nextItem->rect.top;
3179 if (bRemoveChildren)
3181 INT old_cChildren = wineItem->cChildren;
3182 TRACE("TVE_COLLAPSERESET\n");
3183 wineItem->state &= ~TVIS_EXPANDEDONCE;
3184 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3185 wineItem->cChildren = old_cChildren;
3188 if (wineItem->firstChild)
3190 TREEVIEW_ITEM *item, *sibling;
3192 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3194 for (item = wineItem->firstChild; item != sibling;
3195 item = TREEVIEW_GetNextListItem(infoPtr, item))
3197 item->visibleOrder = -1;
3201 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3203 if (nextItem)
3204 scrollDist = -(scrollDist - nextItem->rect.top);
3206 if (bSetSelection)
3208 /* Don't call DoSelectItem, it sends notifications. */
3209 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3210 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3211 wineItem->state |= TVIS_SELECTED;
3212 infoPtr->selectedItem = wineItem;
3215 TREEVIEW_UpdateScrollBars(infoPtr);
3217 scrollRect.left = 0;
3218 scrollRect.right = infoPtr->clientWidth;
3219 scrollRect.bottom = infoPtr->clientHeight;
3221 if (nextItem)
3223 scrollRect.top = nextItem->rect.top;
3225 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3226 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3227 TREEVIEW_Invalidate(infoPtr, wineItem);
3228 } else {
3229 scrollRect.top = wineItem->rect.top;
3230 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3233 TREEVIEW_SetFirstVisible(infoPtr,
3234 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3235 TRUE);
3237 return TRUE;
3240 static BOOL
3241 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3242 BOOL bExpandPartial, BOOL bUser)
3244 LONG scrollDist;
3245 LONG orgNextTop = 0;
3246 RECT scrollRect;
3247 TREEVIEW_ITEM *nextItem, *tmpItem;
3249 TRACE("\n");
3251 if (wineItem->state & TVIS_EXPANDED)
3252 return TRUE;
3254 tmpItem = wineItem; nextItem = NULL;
3255 while (tmpItem)
3257 if (tmpItem->nextSibling)
3259 nextItem = tmpItem->nextSibling;
3260 break;
3262 tmpItem = tmpItem->parent;
3265 if (nextItem)
3266 orgNextTop = nextItem->rect.top;
3268 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3270 if (bUser || ((wineItem->cChildren != 0) &&
3271 !(wineItem->state & TVIS_EXPANDEDONCE)))
3273 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3275 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3276 return FALSE;
3279 if (!wineItem->firstChild)
3280 return FALSE;
3282 wineItem->state |= TVIS_EXPANDED;
3283 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3284 wineItem->state |= TVIS_EXPANDEDONCE;
3286 else
3288 if (!wineItem->firstChild)
3289 return FALSE;
3291 /* this item has already been expanded */
3292 wineItem->state |= TVIS_EXPANDED;
3295 if (bExpandPartial)
3296 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3298 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3299 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3300 TREEVIEW_UpdateScrollBars(infoPtr);
3302 scrollRect.left = 0;
3303 scrollRect.bottom = infoPtr->treeHeight;
3304 scrollRect.right = infoPtr->clientWidth;
3305 if (nextItem)
3307 scrollDist = nextItem->rect.top - orgNextTop;
3308 scrollRect.top = orgNextTop;
3310 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3311 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3312 TREEVIEW_Invalidate (infoPtr, wineItem);
3313 } else {
3314 scrollRect.top = wineItem->rect.top;
3315 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3318 /* Scroll up so that as many children as possible are visible.
3319 * This fails when expanding causes an HScroll bar to appear, but we
3320 * don't know that yet, so the last item is obscured. */
3321 if (wineItem->firstChild != NULL)
3323 int nChildren = wineItem->lastChild->visibleOrder
3324 - wineItem->firstChild->visibleOrder + 1;
3326 int visible_pos = wineItem->visibleOrder
3327 - infoPtr->firstVisible->visibleOrder;
3329 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3331 if (visible_pos > 0 && nChildren > rows_below)
3333 int scroll = nChildren - rows_below;
3335 if (scroll > visible_pos)
3336 scroll = visible_pos;
3338 if (scroll > 0)
3340 TREEVIEW_ITEM *newFirstVisible
3341 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3342 scroll);
3345 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3350 return TRUE;
3353 static BOOL
3354 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3356 TRACE("\n");
3358 if (wineItem->state & TVIS_EXPANDED)
3359 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3360 else
3361 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3364 static VOID
3365 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3367 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3369 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3371 if (TREEVIEW_HasChildren(infoPtr, item))
3372 TREEVIEW_ExpandAll(infoPtr, item);
3376 /* Note:If the specified item is the child of a collapsed parent item,
3377 the parent's list of child items is (recursively) expanded to reveal the
3378 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3379 know if it also applies here.
3382 static LRESULT
3383 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3385 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3386 return 0;
3388 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3389 TREEVIEW_ItemName(wineItem), flag,
3390 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3392 switch (flag & TVE_TOGGLE)
3394 case TVE_COLLAPSE:
3395 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3396 FALSE);
3398 case TVE_EXPAND:
3399 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3400 FALSE);
3402 case TVE_TOGGLE:
3403 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3405 default:
3406 return 0;
3409 #if 0
3410 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3411 #endif
3414 /* Hit-Testing **********************************************************/
3416 static TREEVIEW_ITEM *
3417 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3419 TREEVIEW_ITEM *wineItem;
3420 LONG row;
3422 if (!infoPtr->firstVisible)
3423 return NULL;
3425 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3427 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3428 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3430 if (row >= wineItem->visibleOrder
3431 && row < wineItem->visibleOrder + wineItem->iIntegral)
3432 break;
3435 return wineItem;
3438 static LRESULT
3439 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3441 TREEVIEW_ITEM *wineItem;
3442 RECT rect;
3443 UINT status;
3444 LONG x, y;
3446 lpht->hItem = 0;
3447 GetClientRect(infoPtr->hwnd, &rect);
3448 status = 0;
3449 x = lpht->pt.x;
3450 y = lpht->pt.y;
3452 if (x < rect.left)
3454 status |= TVHT_TOLEFT;
3456 else if (x > rect.right)
3458 status |= TVHT_TORIGHT;
3461 if (y < rect.top)
3463 status |= TVHT_ABOVE;
3465 else if (y > rect.bottom)
3467 status |= TVHT_BELOW;
3470 if (status)
3472 lpht->flags = status;
3473 return (LRESULT)(HTREEITEM)NULL;
3476 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3477 if (!wineItem)
3479 lpht->flags = TVHT_NOWHERE;
3480 return (LRESULT)(HTREEITEM)NULL;
3483 if (x >= wineItem->textOffset + wineItem->textWidth)
3485 lpht->flags = TVHT_ONITEMRIGHT;
3487 else if (x >= wineItem->textOffset)
3489 lpht->flags = TVHT_ONITEMLABEL;
3491 else if (x >= wineItem->imageOffset)
3493 lpht->flags = TVHT_ONITEMICON;
3495 else if (x >= wineItem->stateOffset)
3497 lpht->flags = TVHT_ONITEMSTATEICON;
3499 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3501 lpht->flags = TVHT_ONITEMBUTTON;
3503 else
3505 lpht->flags = TVHT_ONITEMINDENT;
3508 lpht->hItem = wineItem;
3509 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3511 return (LRESULT)wineItem;
3514 /* Item Label Editing ***************************************************/
3516 static LRESULT
3517 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3519 return (LRESULT)infoPtr->hwndEdit;
3522 static LRESULT CALLBACK
3523 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3525 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3526 BOOL bCancel = FALSE;
3527 LRESULT rc;
3529 switch (uMsg)
3531 case WM_PAINT:
3532 TRACE("WM_PAINT start\n");
3533 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3534 lParam);
3535 TRACE("WM_PAINT done\n");
3536 return rc;
3538 case WM_KILLFOCUS:
3539 if (infoPtr->bIgnoreEditKillFocus)
3540 return TRUE;
3541 break;
3543 case WM_GETDLGCODE:
3544 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3546 case WM_KEYDOWN:
3547 if (wParam == (WPARAM)VK_ESCAPE)
3549 bCancel = TRUE;
3550 break;
3552 else if (wParam == (WPARAM)VK_RETURN)
3554 break;
3557 /* fall through */
3558 default:
3559 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3562 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3563 /* eg. Using a messagebox */
3565 infoPtr->bIgnoreEditKillFocus = TRUE;
3566 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3567 infoPtr->bIgnoreEditKillFocus = FALSE;
3569 return 0;
3573 /* should handle edit control messages here */
3575 static LRESULT
3576 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3578 TRACE("%x %ld\n", wParam, lParam);
3580 switch (HIWORD(wParam))
3582 case EN_UPDATE:
3585 * Adjust the edit window size
3587 WCHAR buffer[1024];
3588 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3589 HDC hdc = GetDC(infoPtr->hwndEdit);
3590 SIZE sz;
3591 int len;
3592 HFONT hFont, hOldFont = 0;
3594 infoPtr->bLabelChanged = TRUE;
3596 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer));
3598 /* Select font to get the right dimension of the string */
3599 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3601 if (hFont != 0)
3603 hOldFont = SelectObject(hdc, hFont);
3606 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3608 TEXTMETRICW textMetric;
3610 /* Add Extra spacing for the next character */
3611 GetTextMetricsW(hdc, &textMetric);
3612 sz.cx += (textMetric.tmMaxCharWidth * 2);
3614 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3615 sz.cx = min(sz.cx,
3616 infoPtr->clientWidth - editItem->textOffset + 2);
3618 SetWindowPos(infoPtr->hwndEdit,
3619 HWND_TOP,
3622 sz.cx,
3623 editItem->rect.bottom - editItem->rect.top + 3,
3624 SWP_NOMOVE | SWP_DRAWFRAME);
3627 if (hFont != 0)
3629 SelectObject(hdc, hOldFont);
3632 ReleaseDC(infoPtr->hwnd, hdc);
3633 break;
3636 default:
3637 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3640 return 0;
3643 static HWND
3644 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3646 HWND hwnd = infoPtr->hwnd;
3647 HWND hwndEdit;
3648 SIZE sz;
3649 TREEVIEW_ITEM *editItem = hItem;
3650 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3651 HDC hdc;
3652 HFONT hOldFont=0;
3653 TEXTMETRICW textMetric;
3654 static const WCHAR EditW[] = {'E','d','i','t',0};
3656 TRACE("%p %p\n", hwnd, hItem);
3657 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3658 return NULL;
3660 if (infoPtr->hwndEdit)
3661 return infoPtr->hwndEdit;
3663 infoPtr->bLabelChanged = FALSE;
3665 /* Make sure that edit item is selected */
3666 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3667 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3669 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3671 hdc = GetDC(hwnd);
3672 /* Select the font to get appropriate metric dimensions */
3673 if (infoPtr->hFont != 0)
3675 hOldFont = SelectObject(hdc, infoPtr->hFont);
3678 /* Get string length in pixels */
3679 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3680 &sz);
3682 /* Add Extra spacing for the next character */
3683 GetTextMetricsW(hdc, &textMetric);
3684 sz.cx += (textMetric.tmMaxCharWidth * 2);
3686 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3687 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3689 if (infoPtr->hFont != 0)
3691 SelectObject(hdc, hOldFont);
3694 ReleaseDC(hwnd, hdc);
3695 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3696 EditW,
3698 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3699 WS_CLIPSIBLINGS | ES_WANTRETURN |
3700 ES_LEFT, editItem->textOffset - 2,
3701 editItem->rect.top - 1, sz.cx + 3,
3702 editItem->rect.bottom -
3703 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3704 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3706 infoPtr->hwndEdit = hwndEdit;
3708 /* Get a 2D border. */
3709 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3710 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3711 SetWindowLongW(hwndEdit, GWL_STYLE,
3712 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3714 SendMessageW(hwndEdit, WM_SETFONT,
3715 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3717 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3718 (DWORD_PTR)
3719 TREEVIEW_Edit_SubclassProc);
3721 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3723 DestroyWindow(hwndEdit);
3724 infoPtr->hwndEdit = 0;
3725 return NULL;
3728 infoPtr->selectedItem = hItem;
3729 SetWindowTextW(hwndEdit, editItem->pszText);
3730 SetFocus(hwndEdit);
3731 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3732 ShowWindow(hwndEdit, SW_SHOW);
3734 return hwndEdit;
3738 static LRESULT
3739 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3741 HWND hwnd = infoPtr->hwnd;
3742 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3743 NMTVDISPINFOW tvdi;
3744 BOOL bCommit;
3745 WCHAR tmpText[1024] = { '\0' };
3746 WCHAR *newText = tmpText;
3747 int iLength = 0;
3749 if (!infoPtr->hwndEdit)
3750 return FALSE;
3752 tvdi.hdr.hwndFrom = hwnd;
3753 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3754 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3755 tvdi.item.mask = 0;
3756 tvdi.item.hItem = editedItem;
3757 tvdi.item.state = editedItem->state;
3758 tvdi.item.lParam = editedItem->lParam;
3760 if (!bCancel)
3762 if (!infoPtr->bNtfUnicode)
3763 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3764 else
3765 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3767 if (iLength >= 1023)
3769 ERR("Insufficient space to retrieve new item label\n");
3772 tvdi.item.mask = TVIF_TEXT;
3773 tvdi.item.pszText = tmpText;
3774 tvdi.item.cchTextMax = iLength + 1;
3776 else
3778 tvdi.item.pszText = NULL;
3779 tvdi.item.cchTextMax = 0;
3782 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
3783 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3785 if (!bCancel && bCommit) /* Apply the changes */
3787 if (!infoPtr->bNtfUnicode)
3789 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3790 newText = Alloc(len * sizeof(WCHAR));
3791 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3792 iLength = len - 1;
3795 if (strcmpW(newText, editedItem->pszText) != 0)
3797 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3798 if (ptr == NULL)
3800 ERR("OutOfMemory, cannot allocate space for label\n");
3801 DestroyWindow(infoPtr->hwndEdit);
3802 infoPtr->hwndEdit = 0;
3803 return FALSE;
3805 else
3807 editedItem->pszText = ptr;
3808 editedItem->cchTextMax = iLength + 1;
3809 strcpyW(editedItem->pszText, newText);
3810 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3813 if(newText != tmpText) Free(newText);
3816 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3817 DestroyWindow(infoPtr->hwndEdit);
3818 infoPtr->hwndEdit = 0;
3819 return TRUE;
3822 static LRESULT
3823 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3825 if (wParam != TV_EDIT_TIMER)
3827 ERR("got unknown timer\n");
3828 return 1;
3831 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3832 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3834 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3836 return 0;
3840 /* Mouse Tracking/Drag **************************************************/
3842 /***************************************************************************
3843 * This is quite unusual piece of code, but that's how it's implemented in
3844 * Windows.
3846 static LRESULT
3847 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3849 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3850 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3851 RECT r;
3852 MSG msg;
3854 r.top = pt.y - cyDrag;
3855 r.left = pt.x - cxDrag;
3856 r.bottom = pt.y + cyDrag;
3857 r.right = pt.x + cxDrag;
3859 SetCapture(infoPtr->hwnd);
3861 while (1)
3863 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3865 if (msg.message == WM_MOUSEMOVE)
3867 pt.x = (short)LOWORD(msg.lParam);
3868 pt.y = (short)HIWORD(msg.lParam);
3869 if (PtInRect(&r, pt))
3870 continue;
3871 else
3873 ReleaseCapture();
3874 return 1;
3877 else if (msg.message >= WM_LBUTTONDOWN &&
3878 msg.message <= WM_RBUTTONDBLCLK)
3880 if (msg.message == WM_RBUTTONUP)
3881 TREEVIEW_RButtonUp(infoPtr, &pt);
3882 break;
3885 DispatchMessageW(&msg);
3888 if (GetCapture() != infoPtr->hwnd)
3889 return 0;
3892 ReleaseCapture();
3893 return 0;
3897 static LRESULT
3898 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3900 TREEVIEW_ITEM *wineItem;
3901 TVHITTESTINFO hit;
3903 TRACE("\n");
3904 SetFocus(infoPtr->hwnd);
3906 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3908 /* If there is pending 'edit label' event - kill it now */
3909 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3912 hit.pt.x = (short)LOWORD(lParam);
3913 hit.pt.y = (short)HIWORD(lParam);
3915 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3916 if (!wineItem)
3917 return 0;
3918 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3920 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3921 { /* FIXME! */
3922 switch (hit.flags)
3924 case TVHT_ONITEMRIGHT:
3925 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3926 break;
3928 case TVHT_ONITEMINDENT:
3929 if (!(infoPtr->dwStyle & TVS_HASLINES))
3931 break;
3933 else
3935 int level = hit.pt.x / infoPtr->uIndent;
3936 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3938 while (wineItem->iLevel > level)
3940 wineItem = wineItem->parent;
3943 /* fall through */
3946 case TVHT_ONITEMLABEL:
3947 case TVHT_ONITEMICON:
3948 case TVHT_ONITEMBUTTON:
3949 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3950 break;
3952 case TVHT_ONITEMSTATEICON:
3953 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3954 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3955 else
3956 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3957 break;
3960 return TRUE;
3964 static LRESULT
3965 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3967 HWND hwnd = infoPtr->hwnd;
3968 TVHITTESTINFO ht;
3969 BOOL bTrack, bDoLabelEdit;
3970 HTREEITEM tempItem;
3972 /* If Edit control is active - kill it and return.
3973 * The best way to do it is to set focus to itself.
3974 * Edit control subclassed procedure will automatically call
3975 * EndEditLabelNow.
3977 if (infoPtr->hwndEdit)
3979 SetFocus(hwnd);
3980 return 0;
3983 ht.pt.x = (short)LOWORD(lParam);
3984 ht.pt.y = (short)HIWORD(lParam);
3986 TREEVIEW_HitTest(infoPtr, &ht);
3987 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3989 /* update focusedItem and redraw both items */
3990 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3992 infoPtr->focusedItem = ht.hItem;
3993 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3995 if(infoPtr->selectedItem)
3996 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3999 bTrack = (ht.flags & TVHT_ONITEM)
4000 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4003 * If the style allows editing and the node is already selected
4004 * and the click occurred on the item label...
4006 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4007 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4009 /* Send NM_CLICK right away */
4010 if (!bTrack)
4011 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4012 goto setfocus;
4014 if (ht.flags & TVHT_ONITEMBUTTON)
4016 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4017 goto setfocus;
4019 else if (bTrack)
4020 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4021 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4023 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4024 infoPtr->dropItem = ht.hItem;
4026 /* clean up focusedItem as we dragged and won't select this item */
4027 if(infoPtr->focusedItem)
4029 /* refresh the item that was focused */
4030 tempItem = infoPtr->focusedItem;
4031 infoPtr->focusedItem = 0;
4032 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
4034 /* refresh the selected item to return the filled background */
4035 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
4038 return 0;
4042 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4043 goto setfocus;
4045 if (bDoLabelEdit)
4047 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4048 KillTimer(hwnd, TV_EDIT_TIMER);
4050 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4051 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4053 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4056 * if we are TVS_SINGLEEXPAND then we want this single click to
4057 * do a bunch of things.
4059 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4060 (infoPtr->hwndEdit == 0))
4062 TREEVIEW_ITEM *SelItem;
4065 * Send the notification
4067 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4070 * Close the previous selection all the way to the root
4071 * as long as the new selection is not a child
4073 if((infoPtr->selectedItem)
4074 && (infoPtr->selectedItem != ht.hItem))
4076 BOOL closeit = TRUE;
4077 SelItem = ht.hItem;
4079 /* determine if the hitItem is a child of the currently selected item */
4080 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4082 closeit = (SelItem != infoPtr->selectedItem);
4083 SelItem = SelItem->parent;
4086 if(closeit)
4088 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4089 SelItem = infoPtr->selectedItem;
4091 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4093 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4094 SelItem = SelItem->parent;
4100 * Expand the current item
4102 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4105 /* Select the current item */
4106 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4108 else if (ht.flags & TVHT_ONITEMSTATEICON)
4110 /* TVS_CHECKBOXES requires us to toggle the current state */
4111 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4112 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4115 setfocus:
4116 SetFocus(hwnd);
4117 return 0;
4121 static LRESULT
4122 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4124 TVHITTESTINFO ht;
4126 if (infoPtr->hwndEdit)
4128 SetFocus(infoPtr->hwnd);
4129 return 0;
4132 ht.pt.x = (short)LOWORD(lParam);
4133 ht.pt.y = (short)HIWORD(lParam);
4135 TREEVIEW_HitTest(infoPtr, &ht);
4137 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4139 if (ht.hItem)
4141 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4142 infoPtr->dropItem = ht.hItem;
4145 else
4147 SetFocus(infoPtr->hwnd);
4148 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4151 return 0;
4154 static LRESULT
4155 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4157 return 0;
4161 static LRESULT
4162 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4164 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4165 INT cx, cy;
4166 HDC hdc, htopdc;
4167 HWND hwtop;
4168 HBITMAP hbmp, hOldbmp;
4169 SIZE size;
4170 RECT rc;
4171 HFONT hOldFont;
4173 TRACE("\n");
4175 if (!(infoPtr->himlNormal))
4176 return 0;
4178 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4179 return 0;
4181 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4183 hwtop = GetDesktopWindow();
4184 htopdc = GetDC(hwtop);
4185 hdc = CreateCompatibleDC(htopdc);
4187 hOldFont = SelectObject(hdc, infoPtr->hFont);
4188 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4189 &size);
4190 TRACE("%d %d %s %d\n", size.cx, size.cy, debugstr_w(dragItem->pszText),
4191 strlenW(dragItem->pszText));
4192 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4193 hOldbmp = SelectObject(hdc, hbmp);
4195 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4196 size.cx += cx;
4197 if (cy > size.cy)
4198 size.cy = cy;
4200 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4201 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4202 ILD_NORMAL);
4205 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4206 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4209 /* draw item text */
4211 SetRect(&rc, cx, 0, size.cx, size.cy);
4212 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4213 DT_LEFT);
4214 SelectObject(hdc, hOldFont);
4215 SelectObject(hdc, hOldbmp);
4217 ImageList_Add(infoPtr->dragList, hbmp, 0);
4219 DeleteDC(hdc);
4220 DeleteObject(hbmp);
4221 ReleaseDC(hwtop, htopdc);
4223 return (LRESULT)infoPtr->dragList;
4226 /* Selection ************************************************************/
4228 static LRESULT
4229 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4230 INT cause)
4232 TREEVIEW_ITEM *prevSelect;
4233 RECT rcFocused;
4235 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4237 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4238 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4239 newSelect ? newSelect->state : 0);
4241 /* reset and redraw focusedItem if focusedItem was set so we don't */
4242 /* have to worry about the previously focused item when we set a new one */
4243 if(infoPtr->focusedItem)
4245 rcFocused = (infoPtr->focusedItem)->rect;
4246 infoPtr->focusedItem = 0;
4247 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4250 switch (action)
4252 case TVGN_CARET:
4253 prevSelect = infoPtr->selectedItem;
4255 if (prevSelect == newSelect) {
4256 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4257 break;
4260 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4261 TVN_SELCHANGINGW,
4262 cause,
4263 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4264 prevSelect,
4265 newSelect))
4266 return FALSE;
4268 if (prevSelect)
4269 prevSelect->state &= ~TVIS_SELECTED;
4270 if (newSelect)
4271 newSelect->state |= TVIS_SELECTED;
4273 infoPtr->selectedItem = newSelect;
4275 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4277 if (prevSelect)
4278 TREEVIEW_Invalidate(infoPtr, prevSelect);
4279 if (newSelect)
4280 TREEVIEW_Invalidate(infoPtr, newSelect);
4282 TREEVIEW_SendTreeviewNotify(infoPtr,
4283 TVN_SELCHANGEDW,
4284 cause,
4285 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4286 prevSelect,
4287 newSelect);
4288 break;
4290 case TVGN_DROPHILITE:
4291 prevSelect = infoPtr->dropItem;
4293 if (prevSelect)
4294 prevSelect->state &= ~TVIS_DROPHILITED;
4296 infoPtr->dropItem = newSelect;
4298 if (newSelect)
4299 newSelect->state |= TVIS_DROPHILITED;
4301 TREEVIEW_Invalidate(infoPtr, prevSelect);
4302 TREEVIEW_Invalidate(infoPtr, newSelect);
4303 break;
4305 case TVGN_FIRSTVISIBLE:
4306 if (newSelect != NULL)
4308 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4309 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4310 TREEVIEW_Invalidate(infoPtr, NULL);
4312 break;
4315 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4316 return TRUE;
4319 /* FIXME: handle NM_KILLFOCUS etc */
4320 static LRESULT
4321 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4323 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4324 return FALSE;
4326 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4328 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4329 return FALSE;
4331 return TRUE;
4334 /*************************************************************************
4335 * TREEVIEW_ProcessLetterKeys
4337 * Processes keyboard messages generated by pressing the letter keys
4338 * on the keyboard.
4339 * What this does is perform a case insensitive search from the
4340 * current position with the following quirks:
4341 * - If two chars or more are pressed in quick succession we search
4342 * for the corresponding string (e.g. 'abc').
4343 * - If there is a delay we wipe away the current search string and
4344 * restart with just that char.
4345 * - If the user keeps pressing the same character, whether slowly or
4346 * fast, so that the search string is entirely composed of this
4347 * character ('aaaaa' for instance), then we search for first item
4348 * that starting with that character.
4349 * - If the user types the above character in quick succession, then
4350 * we must also search for the corresponding string ('aaaaa'), and
4351 * go to that string if there is a match.
4353 * RETURNS
4355 * Zero.
4357 * BUGS
4359 * - The current implementation has a list of characters it will
4360 * accept and it ignores averything else. In particular it will
4361 * ignore accentuated characters which seems to match what
4362 * Windows does. But I'm not sure it makes sense to follow
4363 * Windows there.
4364 * - We don't sound a beep when the search fails.
4365 * - The search should start from the focused item, not from the selected
4366 * item. One reason for this is to allow for multiple selections in trees.
4367 * But currently infoPtr->focusedItem does not seem very usable.
4369 * SEE ALSO
4371 * TREEVIEW_ProcessLetterKeys
4373 static INT TREEVIEW_ProcessLetterKeys(
4374 HWND hwnd, /* handle to the window */
4375 WPARAM charCode, /* the character code, the actual character */
4376 LPARAM keyData /* key data */
4379 TREEVIEW_INFO *infoPtr;
4380 HTREEITEM nItem;
4381 HTREEITEM endidx,idx;
4382 TVITEMEXW item;
4383 WCHAR buffer[MAX_PATH];
4384 DWORD timestamp,elapsed;
4386 /* simple parameter checking */
4387 if (!hwnd || !charCode || !keyData)
4388 return 0;
4390 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4391 if (!infoPtr)
4392 return 0;
4394 /* only allow the valid WM_CHARs through */
4395 if (!isalnum(charCode) &&
4396 charCode != '.' && charCode != '`' && charCode != '!' &&
4397 charCode != '@' && charCode != '#' && charCode != '$' &&
4398 charCode != '%' && charCode != '^' && charCode != '&' &&
4399 charCode != '*' && charCode != '(' && charCode != ')' &&
4400 charCode != '-' && charCode != '_' && charCode != '+' &&
4401 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4402 charCode != '}' && charCode != '[' && charCode != '{' &&
4403 charCode != '/' && charCode != '?' && charCode != '>' &&
4404 charCode != '<' && charCode != ',' && charCode != '~')
4405 return 0;
4407 /* compute how much time elapsed since last keypress */
4408 timestamp = GetTickCount();
4409 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4410 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4411 } else {
4412 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4415 /* update the search parameters */
4416 infoPtr->lastKeyPressTimestamp=timestamp;
4417 if (elapsed < KEY_DELAY) {
4418 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4419 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4421 if (infoPtr->charCode != charCode) {
4422 infoPtr->charCode=charCode=0;
4424 } else {
4425 infoPtr->charCode=charCode;
4426 infoPtr->szSearchParam[0]=charCode;
4427 infoPtr->nSearchParamLength=1;
4428 /* Redundant with the 1 char string */
4429 charCode=0;
4432 /* and search from the current position */
4433 nItem=NULL;
4434 if (infoPtr->selectedItem != NULL) {
4435 endidx=infoPtr->selectedItem;
4436 /* if looking for single character match,
4437 * then we must always move forward
4439 if (infoPtr->nSearchParamLength == 1)
4440 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4441 else
4442 idx=endidx;
4443 } else {
4444 endidx=NULL;
4445 idx=infoPtr->root->firstChild;
4447 do {
4448 /* At the end point, sort out wrapping */
4449 if (idx == NULL) {
4451 /* If endidx is null, stop at the last item (ie top to bottom) */
4452 if (endidx == NULL)
4453 break;
4455 /* Otherwise, start again at the very beginning */
4456 idx=infoPtr->root->firstChild;
4458 /* But if we are stopping on the first child, end now! */
4459 if (idx == endidx) break;
4462 /* get item */
4463 ZeroMemory(&item, sizeof(item));
4464 item.mask = TVIF_TEXT;
4465 item.hItem = idx;
4466 item.pszText = buffer;
4467 item.cchTextMax = sizeof(buffer);
4468 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4470 /* check for a match */
4471 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4472 nItem=idx;
4473 break;
4474 } else if ( (charCode != 0) && (nItem == NULL) &&
4475 (nItem != infoPtr->selectedItem) &&
4476 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4477 /* This would work but we must keep looking for a longer match */
4478 nItem=idx;
4480 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4481 } while (idx != endidx);
4483 if (nItem != NULL) {
4484 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4485 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4489 return 0;
4492 /* Scrolling ************************************************************/
4494 static LRESULT
4495 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4497 int viscount;
4498 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4499 HTREEITEM newFirstVisible = NULL;
4500 int visible_pos = -1;
4502 if (!TREEVIEW_ValidItem(infoPtr, item))
4503 return FALSE;
4505 if (!ISVISIBLE(item))
4507 /* Expand parents as necessary. */
4508 HTREEITEM parent;
4510 /* see if we are trying to ensure that root is vislble */
4511 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4512 parent = item->parent;
4513 else
4514 parent = item; /* this item is the topmost item */
4516 while (parent != infoPtr->root)
4518 if (!(parent->state & TVIS_EXPANDED))
4519 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4521 parent = parent->parent;
4525 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4527 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4528 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4530 if (hasFirstVisible)
4531 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4533 if (visible_pos < 0)
4535 /* item is before the start of the list: put it at the top. */
4536 newFirstVisible = item;
4538 else if (visible_pos >= viscount
4539 /* Sometimes, before we are displayed, GVC is 0, causing us to
4540 * spuriously scroll up. */
4541 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4543 /* item is past the end of the list. */
4544 int scroll = visible_pos - viscount;
4546 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4547 scroll + 1);
4550 if (bHScroll)
4552 /* Scroll window so item's text is visible as much as possible */
4553 /* Calculation of amount of extra space is taken from EditLabel code */
4554 INT pos, x;
4555 TEXTMETRICW textMetric;
4556 HDC hdc = GetWindowDC(infoPtr->hwnd);
4558 x = item->textWidth;
4560 GetTextMetricsW(hdc, &textMetric);
4561 ReleaseDC(infoPtr->hwnd, hdc);
4563 x += (textMetric.tmMaxCharWidth * 2);
4564 x = max(x, textMetric.tmMaxCharWidth * 3);
4566 if (item->textOffset < 0)
4567 pos = item->textOffset;
4568 else if (item->textOffset + x > infoPtr->clientWidth)
4570 if (x > infoPtr->clientWidth)
4571 pos = item->textOffset;
4572 else
4573 pos = item->textOffset + x - infoPtr->clientWidth;
4575 else
4576 pos = 0;
4578 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4581 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4583 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4585 return TRUE;
4588 return FALSE;
4591 static VOID
4592 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4593 TREEVIEW_ITEM *newFirstVisible,
4594 BOOL bUpdateScrollPos)
4596 int gap_size;
4598 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4600 if (newFirstVisible != NULL)
4602 /* Prevent an empty gap from appearing at the bottom... */
4603 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4604 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4606 if (gap_size > 0)
4608 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4609 -gap_size);
4611 /* ... unless we just don't have enough items. */
4612 if (newFirstVisible == NULL)
4613 newFirstVisible = infoPtr->root->firstChild;
4617 if (infoPtr->firstVisible != newFirstVisible)
4619 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4621 infoPtr->firstVisible = newFirstVisible;
4622 TREEVIEW_Invalidate(infoPtr, NULL);
4624 else
4626 TREEVIEW_ITEM *item;
4627 int scroll = infoPtr->uItemHeight *
4628 (infoPtr->firstVisible->visibleOrder
4629 - newFirstVisible->visibleOrder);
4631 infoPtr->firstVisible = newFirstVisible;
4633 for (item = infoPtr->root->firstChild; item != NULL;
4634 item = TREEVIEW_GetNextListItem(infoPtr, item))
4636 item->rect.top += scroll;
4637 item->rect.bottom += scroll;
4640 if (bUpdateScrollPos)
4641 SetScrollPos(infoPtr->hwnd, SB_VERT,
4642 newFirstVisible->visibleOrder, TRUE);
4644 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4649 /************************************************************************
4650 * VScroll is always in units of visible items. i.e. we always have a
4651 * visible item aligned to the top of the control. (Unless we have no
4652 * items at all.)
4654 static LRESULT
4655 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4657 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4658 TREEVIEW_ITEM *newFirstVisible = NULL;
4660 int nScrollCode = LOWORD(wParam);
4662 TRACE("wp %x\n", wParam);
4664 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4665 return 0;
4667 if (!oldFirstVisible)
4669 assert(infoPtr->root->firstChild == NULL);
4670 return 0;
4673 switch (nScrollCode)
4675 case SB_TOP:
4676 newFirstVisible = infoPtr->root->firstChild;
4677 break;
4679 case SB_BOTTOM:
4680 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4681 break;
4683 case SB_LINEUP:
4684 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4685 break;
4687 case SB_LINEDOWN:
4688 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4689 break;
4691 case SB_PAGEUP:
4692 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4693 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4694 break;
4696 case SB_PAGEDOWN:
4697 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4698 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4699 break;
4701 case SB_THUMBTRACK:
4702 case SB_THUMBPOSITION:
4703 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4704 infoPtr->root->firstChild,
4705 (LONG)(SHORT)HIWORD(wParam));
4706 break;
4708 case SB_ENDSCROLL:
4709 return 0;
4712 if (newFirstVisible != NULL)
4714 if (newFirstVisible != oldFirstVisible)
4715 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4716 nScrollCode != SB_THUMBTRACK);
4717 else if (nScrollCode == SB_THUMBPOSITION)
4718 SetScrollPos(infoPtr->hwnd, SB_VERT,
4719 newFirstVisible->visibleOrder, TRUE);
4722 return 0;
4725 static LRESULT
4726 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4728 int maxWidth;
4729 int scrollX = infoPtr->scrollX;
4730 int nScrollCode = LOWORD(wParam);
4732 TRACE("wp %x\n", wParam);
4734 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4735 return FALSE;
4737 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4738 /* shall never occur */
4739 if (maxWidth <= 0)
4741 scrollX = 0;
4742 goto scroll;
4745 switch (nScrollCode)
4747 case SB_LINELEFT:
4748 scrollX -= infoPtr->uItemHeight;
4749 break;
4750 case SB_LINERIGHT:
4751 scrollX += infoPtr->uItemHeight;
4752 break;
4753 case SB_PAGELEFT:
4754 scrollX -= infoPtr->clientWidth;
4755 break;
4756 case SB_PAGERIGHT:
4757 scrollX += infoPtr->clientWidth;
4758 break;
4760 case SB_THUMBTRACK:
4761 case SB_THUMBPOSITION:
4762 scrollX = (int)(SHORT)HIWORD(wParam);
4763 break;
4765 case SB_ENDSCROLL:
4766 return 0;
4769 if (scrollX > maxWidth)
4770 scrollX = maxWidth;
4771 else if (scrollX < 0)
4772 scrollX = 0;
4774 scroll:
4775 if (scrollX != infoPtr->scrollX)
4777 TREEVIEW_ITEM *item;
4778 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4780 for (item = infoPtr->root->firstChild; item != NULL;
4781 item = TREEVIEW_GetNextListItem(infoPtr, item))
4783 item->linesOffset += scroll_pixels;
4784 item->stateOffset += scroll_pixels;
4785 item->imageOffset += scroll_pixels;
4786 item->textOffset += scroll_pixels;
4789 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4790 infoPtr->scrollX = scrollX;
4791 UpdateWindow(infoPtr->hwnd);
4794 if (nScrollCode != SB_THUMBTRACK)
4795 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4797 return 0;
4800 static LRESULT
4801 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4803 short gcWheelDelta;
4804 UINT pulScrollLines = 3;
4806 if (infoPtr->firstVisible == NULL)
4807 return TRUE;
4809 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4811 gcWheelDelta = -(short)HIWORD(wParam);
4812 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4814 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4816 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4817 int maxDy = infoPtr->maxVisibleOrder;
4819 if (newDy > maxDy)
4820 newDy = maxDy;
4822 if (newDy < 0)
4823 newDy = 0;
4825 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4827 return TRUE;
4830 /* Create/Destroy *******************************************************/
4832 static void
4833 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4835 RECT rc;
4836 HBITMAP hbm, hbmOld;
4837 HDC hdc, hdcScreen;
4838 int nIndex;
4840 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4842 hdcScreen = GetDC(0);
4844 hdc = CreateCompatibleDC(hdcScreen);
4845 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4846 hbmOld = SelectObject(hdc, hbm);
4848 SetRect(&rc, 0, 0, 48, 16);
4849 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4851 SetRect(&rc, 18, 2, 30, 14);
4852 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4853 DFCS_BUTTONCHECK|DFCS_FLAT);
4855 SetRect(&rc, 34, 2, 46, 14);
4856 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4857 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4859 SelectObject(hdc, hbmOld);
4860 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4861 GetSysColor(COLOR_WINDOW));
4862 TRACE("checkbox index %d\n", nIndex);
4864 DeleteObject(hbm);
4865 DeleteDC(hdc);
4866 ReleaseDC(0, hdcScreen);
4868 infoPtr->stateImageWidth = 16;
4869 infoPtr->stateImageHeight = 16;
4872 static LRESULT
4873 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4875 RECT rcClient;
4876 TREEVIEW_INFO *infoPtr;
4877 LOGFONTW lf;
4879 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4881 infoPtr = (TREEVIEW_INFO *)Alloc(sizeof(TREEVIEW_INFO));
4883 if (infoPtr == NULL)
4885 ERR("could not allocate info memory!\n");
4886 return 0;
4889 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4891 infoPtr->hwnd = hwnd;
4892 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4893 infoPtr->Timer = 0;
4894 infoPtr->uNumItems = 0;
4895 infoPtr->cdmode = 0;
4896 infoPtr->uScrollTime = 300; /* milliseconds */
4897 infoPtr->bRedraw = TRUE;
4899 GetClientRect(hwnd, &rcClient);
4901 /* No scroll bars yet. */
4902 infoPtr->clientWidth = rcClient.right;
4903 infoPtr->clientHeight = rcClient.bottom;
4904 infoPtr->uInternalStatus = 0;
4906 infoPtr->treeWidth = 0;
4907 infoPtr->treeHeight = 0;
4909 infoPtr->uIndent = MINIMUM_INDENT;
4910 infoPtr->selectedItem = 0;
4911 infoPtr->focusedItem = 0;
4912 infoPtr->hotItem = 0;
4913 infoPtr->firstVisible = 0;
4914 infoPtr->maxVisibleOrder = 0;
4915 infoPtr->dropItem = 0;
4916 infoPtr->insertMarkItem = 0;
4917 infoPtr->insertBeforeorAfter = 0;
4918 /* dragList */
4920 infoPtr->scrollX = 0;
4922 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4923 infoPtr->clrText = -1; /* use system color */
4924 infoPtr->clrLine = RGB(128, 128, 128);
4925 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4927 /* hwndToolTip */
4929 infoPtr->hwndEdit = 0;
4930 infoPtr->wpEditOrig = NULL;
4931 infoPtr->bIgnoreEditKillFocus = FALSE;
4932 infoPtr->bLabelChanged = FALSE;
4934 infoPtr->himlNormal = NULL;
4935 infoPtr->himlState = NULL;
4936 infoPtr->normalImageWidth = 0;
4937 infoPtr->normalImageHeight = 0;
4938 infoPtr->stateImageWidth = 0;
4939 infoPtr->stateImageHeight = 0;
4941 infoPtr->items = DPA_Create(16);
4943 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
4944 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
4945 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4946 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
4947 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
4949 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4951 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4952 infoPtr->root->state = TVIS_EXPANDED;
4953 infoPtr->root->iLevel = -1;
4954 infoPtr->root->visibleOrder = -1;
4956 infoPtr->hwndNotify = lpcs->hwndParent;
4957 #if 0
4958 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4959 #endif
4961 infoPtr->hwndToolTip = 0;
4963 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4965 /* Determine what type of notify should be issued */
4966 /* sets infoPtr->bNtfUnicode */
4967 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4969 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4970 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
4971 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4972 hwnd, 0, 0, 0);
4974 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4975 initialize_checkboxes(infoPtr);
4977 /* Make sure actual scrollbar state is consistent with uInternalStatus */
4978 ShowScrollBar(hwnd, SB_VERT, FALSE);
4979 ShowScrollBar(hwnd, SB_HORZ, FALSE);
4981 OpenThemeData (hwnd, themeClass);
4983 return 0;
4987 static LRESULT
4988 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4990 TRACE("\n");
4992 TREEVIEW_RemoveTree(infoPtr);
4994 /* tool tip is automatically destroyed: we are its owner */
4996 /* Restore original wndproc */
4997 if (infoPtr->hwndEdit)
4998 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
4999 (DWORD_PTR)infoPtr->wpEditOrig);
5001 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5003 /* Deassociate treeview from the window before doing anything drastic. */
5004 SetWindowLongPtrW(infoPtr->hwnd, 0, (DWORD_PTR)NULL);
5007 DeleteObject(infoPtr->hDefaultFont);
5008 DeleteObject(infoPtr->hBoldFont);
5009 DeleteObject(infoPtr->hUnderlineFont);
5010 Free(infoPtr);
5012 return 0;
5015 /* Miscellaneous Messages ***********************************************/
5017 static LRESULT
5018 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5020 static const struct
5022 unsigned char code;
5024 scroll[] =
5026 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5027 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5028 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5029 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5030 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5031 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5032 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5033 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5034 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5035 #undef SCROLL_ENTRY
5038 if (key >= VK_PRIOR && key <= VK_DOWN)
5040 unsigned char code = scroll[key - VK_PRIOR].code;
5042 (((code & (1 << 7)) == (SB_HORZ << 7))
5043 ? TREEVIEW_HScroll
5044 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5047 return 0;
5050 /************************************************************************
5051 * TREEVIEW_KeyDown
5053 * VK_UP Move selection to the previous non-hidden item.
5054 * VK_DOWN Move selection to the next non-hidden item.
5055 * VK_HOME Move selection to the first item.
5056 * VK_END Move selection to the last item.
5057 * VK_LEFT If expanded then collapse, otherwise move to parent.
5058 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5059 * VK_ADD Expand.
5060 * VK_SUBTRACT Collapse.
5061 * VK_MULTIPLY Expand all.
5062 * VK_PRIOR Move up GetVisibleCount items.
5063 * VK_NEXT Move down GetVisibleCount items.
5064 * VK_BACK Move to parent.
5065 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5067 static LRESULT
5068 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5070 /* If it is non-NULL and different, it will be selected and visible. */
5071 TREEVIEW_ITEM *newSelection = NULL;
5073 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5075 TRACE("%x\n", wParam);
5077 if (prevItem == NULL)
5078 return FALSE;
5080 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5081 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5083 switch (wParam)
5085 case VK_UP:
5086 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5087 if (!newSelection)
5088 newSelection = infoPtr->root->firstChild;
5089 break;
5091 case VK_DOWN:
5092 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5093 break;
5095 case VK_HOME:
5096 newSelection = infoPtr->root->firstChild;
5097 break;
5099 case VK_END:
5100 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5101 break;
5103 case VK_LEFT:
5104 if (prevItem->state & TVIS_EXPANDED)
5106 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5108 else if (prevItem->parent != infoPtr->root)
5110 newSelection = prevItem->parent;
5112 break;
5114 case VK_RIGHT:
5115 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5117 if (!(prevItem->state & TVIS_EXPANDED))
5118 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5119 else
5121 newSelection = prevItem->firstChild;
5125 break;
5127 case VK_MULTIPLY:
5128 TREEVIEW_ExpandAll(infoPtr, prevItem);
5129 break;
5131 case VK_ADD:
5132 if (!(prevItem->state & TVIS_EXPANDED))
5133 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5134 break;
5136 case VK_SUBTRACT:
5137 if (prevItem->state & TVIS_EXPANDED)
5138 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5139 break;
5141 case VK_PRIOR:
5142 newSelection
5143 = TREEVIEW_GetListItem(infoPtr, prevItem,
5144 -TREEVIEW_GetVisibleCount(infoPtr));
5145 break;
5147 case VK_NEXT:
5148 newSelection
5149 = TREEVIEW_GetListItem(infoPtr, prevItem,
5150 TREEVIEW_GetVisibleCount(infoPtr));
5151 break;
5153 case VK_BACK:
5154 newSelection = prevItem->parent;
5155 if (newSelection == infoPtr->root)
5156 newSelection = NULL;
5157 break;
5159 case VK_SPACE:
5160 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5161 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5162 break;
5165 if (newSelection && newSelection != prevItem)
5167 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5168 TVC_BYKEYBOARD))
5170 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5174 return FALSE;
5177 static LRESULT
5178 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5180 if (infoPtr->hotItem)
5182 /* remove hot effect from item */
5183 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5184 infoPtr->hotItem = NULL;
5186 return 0;
5189 static LRESULT
5190 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, WPARAM wParam, LPARAM lParam)
5192 POINT pt;
5193 TRACKMOUSEEVENT trackinfo;
5194 TREEVIEW_ITEM * item;
5196 /* fill in the TRACKMOUSEEVENT struct */
5197 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5198 trackinfo.dwFlags = TME_QUERY;
5199 trackinfo.hwndTrack = infoPtr->hwnd;
5200 trackinfo.dwHoverTime = HOVER_DEFAULT;
5202 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5203 _TrackMouseEvent(&trackinfo);
5205 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5206 if(!(trackinfo.dwFlags & TME_LEAVE))
5208 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5210 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5211 /* and can properly deactivate the hot item */
5212 _TrackMouseEvent(&trackinfo);
5215 pt.x = (short)LOWORD(lParam);
5216 pt.y = (short)HIWORD(lParam);
5218 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5220 if (item != infoPtr->hotItem)
5222 /* redraw old hot item */
5223 if (infoPtr->hotItem)
5224 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5225 infoPtr->hotItem = item;
5226 /* redraw new hot item */
5227 if (infoPtr->hotItem)
5228 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5231 return 0;
5234 /* Draw themed border */
5235 static BOOL nc_paint (const TREEVIEW_INFO *infoPtr, HRGN region)
5237 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5238 HDC dc;
5239 RECT r;
5240 HRGN cliprgn;
5241 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5242 cyEdge = GetSystemMetrics (SM_CYEDGE);
5244 if (!theme) return FALSE;
5246 GetWindowRect(infoPtr->hwnd, &r);
5248 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5249 r.right - cxEdge, r.bottom - cyEdge);
5250 if (region != (HRGN)1)
5251 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5252 OffsetRect(&r, -r.left, -r.top);
5254 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5255 OffsetRect(&r, -r.left, -r.top);
5257 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5258 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5259 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5260 ReleaseDC(infoPtr->hwnd, dc);
5262 /* Call default proc to get the scrollbars etc. painted */
5263 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5265 return TRUE;
5268 static LRESULT
5269 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5271 LPNMHDR lpnmh = (LPNMHDR)lParam;
5273 if (lpnmh->code == PGN_CALCSIZE) {
5274 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5276 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5277 lppgc->iWidth = infoPtr->treeWidth;
5278 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5279 infoPtr->treeWidth, infoPtr->clientWidth);
5281 else {
5282 lppgc->iHeight = infoPtr->treeHeight;
5283 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5284 infoPtr->treeHeight, infoPtr->clientHeight);
5286 return 0;
5288 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5291 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5293 INT format;
5295 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5297 if (nCommand != NF_REQUERY) return 0;
5299 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5300 TRACE("format=%d\n", format);
5302 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5304 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5306 return format;
5309 static LRESULT
5310 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5312 if (wParam == SIZE_RESTORED)
5314 infoPtr->clientWidth = (short)LOWORD(lParam);
5315 infoPtr->clientHeight = (short)HIWORD(lParam);
5317 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5318 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5319 TREEVIEW_UpdateScrollBars(infoPtr);
5321 else
5323 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
5326 TREEVIEW_Invalidate(infoPtr, NULL);
5327 return 0;
5330 static LRESULT
5331 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5333 TRACE("(%x %lx)\n", wParam, lParam);
5335 if (wParam == GWL_STYLE)
5337 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5339 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5341 if (dwNewStyle & TVS_CHECKBOXES)
5343 initialize_checkboxes(infoPtr);
5344 TRACE("checkboxes enabled\n");
5346 else
5348 FIXME("tried to disable checkboxes\n");
5352 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5354 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5356 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5357 TRACE("tooltips enabled\n");
5359 else
5361 DestroyWindow(infoPtr->hwndToolTip);
5362 infoPtr->hwndToolTip = 0;
5363 TRACE("tooltips disabled\n");
5367 infoPtr->dwStyle = dwNewStyle;
5370 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5371 TREEVIEW_UpdateScrollBars(infoPtr);
5372 TREEVIEW_Invalidate(infoPtr, NULL);
5374 return 0;
5377 static LRESULT
5378 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5380 POINT pt;
5381 TREEVIEW_ITEM * item;
5383 GetCursorPos(&pt);
5384 ScreenToClient(infoPtr->hwnd, &pt);
5386 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5388 /* FIXME: send NM_SETCURSOR */
5390 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5392 SetCursor(infoPtr->hcurHand);
5393 return 0;
5395 else
5396 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5399 static LRESULT
5400 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5402 TRACE("\n");
5404 if (!infoPtr->selectedItem)
5406 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5407 TVC_UNKNOWN);
5410 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5411 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5412 return 0;
5415 static LRESULT
5416 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5418 TRACE("\n");
5420 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5421 UpdateWindow(infoPtr->hwnd);
5422 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5423 return 0;
5426 /* update theme after a WM_THEMECHANGED message */
5427 static LRESULT theme_changed(const TREEVIEW_INFO *infoPtr)
5429 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5430 CloseThemeData (theme);
5431 OpenThemeData (infoPtr->hwnd, themeClass);
5432 return 0;
5436 static LRESULT WINAPI
5437 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5439 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5441 TRACE("hwnd %p msg %04x wp=%08x lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5443 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5444 else
5446 if (uMsg == WM_CREATE)
5447 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5448 else
5449 goto def;
5452 switch (uMsg)
5454 case TVM_CREATEDRAGIMAGE:
5455 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5457 case TVM_DELETEITEM:
5458 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5460 case TVM_EDITLABELA:
5461 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5463 case TVM_EDITLABELW:
5464 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5466 case TVM_ENDEDITLABELNOW:
5467 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5469 case TVM_ENSUREVISIBLE:
5470 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5472 case TVM_EXPAND:
5473 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5475 case TVM_GETBKCOLOR:
5476 return TREEVIEW_GetBkColor(infoPtr);
5478 case TVM_GETCOUNT:
5479 return TREEVIEW_GetCount(infoPtr);
5481 case TVM_GETEDITCONTROL:
5482 return TREEVIEW_GetEditControl(infoPtr);
5484 case TVM_GETIMAGELIST:
5485 return TREEVIEW_GetImageList(infoPtr, wParam);
5487 case TVM_GETINDENT:
5488 return TREEVIEW_GetIndent(infoPtr);
5490 case TVM_GETINSERTMARKCOLOR:
5491 return TREEVIEW_GetInsertMarkColor(infoPtr);
5493 case TVM_GETISEARCHSTRINGA:
5494 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5495 return 0;
5497 case TVM_GETISEARCHSTRINGW:
5498 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5499 return 0;
5501 case TVM_GETITEMA:
5502 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5504 case TVM_GETITEMW:
5505 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5507 case TVM_GETITEMHEIGHT:
5508 return TREEVIEW_GetItemHeight(infoPtr);
5510 case TVM_GETITEMRECT:
5511 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5513 case TVM_GETITEMSTATE:
5514 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5516 case TVM_GETLINECOLOR:
5517 return TREEVIEW_GetLineColor(infoPtr);
5519 case TVM_GETNEXTITEM:
5520 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5522 case TVM_GETSCROLLTIME:
5523 return TREEVIEW_GetScrollTime(infoPtr);
5525 case TVM_GETTEXTCOLOR:
5526 return TREEVIEW_GetTextColor(infoPtr);
5528 case TVM_GETTOOLTIPS:
5529 return TREEVIEW_GetToolTips(infoPtr);
5531 case TVM_GETUNICODEFORMAT:
5532 return TREEVIEW_GetUnicodeFormat(infoPtr);
5534 case TVM_GETVISIBLECOUNT:
5535 return TREEVIEW_GetVisibleCount(infoPtr);
5537 case TVM_HITTEST:
5538 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5540 case TVM_INSERTITEMA:
5541 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5543 case TVM_INSERTITEMW:
5544 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5546 case TVM_SELECTITEM:
5547 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5549 case TVM_SETBKCOLOR:
5550 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5552 case TVM_SETIMAGELIST:
5553 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5555 case TVM_SETINDENT:
5556 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5558 case TVM_SETINSERTMARK:
5559 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5561 case TVM_SETINSERTMARKCOLOR:
5562 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5564 case TVM_SETITEMA:
5565 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5567 case TVM_SETITEMW:
5568 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5570 case TVM_SETLINECOLOR:
5571 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5573 case TVM_SETITEMHEIGHT:
5574 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5576 case TVM_SETSCROLLTIME:
5577 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5579 case TVM_SETTEXTCOLOR:
5580 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5582 case TVM_SETTOOLTIPS:
5583 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5585 case TVM_SETUNICODEFORMAT:
5586 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5588 case TVM_SORTCHILDREN:
5589 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5591 case TVM_SORTCHILDRENCB:
5592 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5594 case WM_CHAR:
5595 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5597 case WM_COMMAND:
5598 return TREEVIEW_Command(infoPtr, wParam, lParam);
5600 case WM_DESTROY:
5601 return TREEVIEW_Destroy(infoPtr);
5603 /* WM_ENABLE */
5605 case WM_ERASEBKGND:
5606 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5608 case WM_GETDLGCODE:
5609 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5611 case WM_GETFONT:
5612 return TREEVIEW_GetFont(infoPtr);
5614 case WM_HSCROLL:
5615 return TREEVIEW_HScroll(infoPtr, wParam);
5617 case WM_KEYDOWN:
5618 return TREEVIEW_KeyDown(infoPtr, wParam);
5620 case WM_KILLFOCUS:
5621 return TREEVIEW_KillFocus(infoPtr);
5623 case WM_LBUTTONDBLCLK:
5624 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5626 case WM_LBUTTONDOWN:
5627 return TREEVIEW_LButtonDown(infoPtr, lParam);
5629 /* WM_MBUTTONDOWN */
5631 case WM_MOUSELEAVE:
5632 return TREEVIEW_MouseLeave(infoPtr);
5634 case WM_MOUSEMOVE:
5635 if (infoPtr->dwStyle & TVS_TRACKSELECT)
5636 return TREEVIEW_MouseMove(infoPtr, wParam, lParam);
5637 else
5638 return 0;
5640 case WM_NCLBUTTONDOWN:
5641 if (infoPtr->hwndEdit)
5642 SetFocus(infoPtr->hwnd);
5643 goto def;
5645 case WM_NCPAINT:
5646 if (nc_paint (infoPtr, (HRGN)wParam))
5647 return 0;
5648 goto def;
5650 case WM_NOTIFY:
5651 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5653 case WM_NOTIFYFORMAT:
5654 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5656 case WM_PRINTCLIENT:
5657 case WM_PAINT:
5658 return TREEVIEW_Paint(infoPtr, wParam);
5660 case WM_RBUTTONDOWN:
5661 return TREEVIEW_RButtonDown(infoPtr, lParam);
5663 case WM_SETCURSOR:
5664 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5666 case WM_SETFOCUS:
5667 return TREEVIEW_SetFocus(infoPtr);
5669 case WM_SETFONT:
5670 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5672 case WM_SETREDRAW:
5673 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5675 case WM_SIZE:
5676 return TREEVIEW_Size(infoPtr, wParam, lParam);
5678 case WM_STYLECHANGED:
5679 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5681 /* WM_SYSCOLORCHANGE */
5683 /* WM_SYSKEYDOWN */
5685 case WM_TIMER:
5686 return TREEVIEW_HandleTimer(infoPtr, wParam);
5688 case WM_THEMECHANGED:
5689 return theme_changed (infoPtr);
5691 case WM_VSCROLL:
5692 return TREEVIEW_VScroll(infoPtr, wParam);
5694 /* WM_WININICHANGE */
5696 case WM_MOUSEWHEEL:
5697 if (wParam & (MK_SHIFT | MK_CONTROL))
5698 goto def;
5699 return TREEVIEW_MouseWheel(infoPtr, wParam);
5701 case WM_DRAWITEM:
5702 TRACE("drawItem\n");
5703 goto def;
5705 default:
5706 /* This mostly catches MFC and Delphi messages. :( */
5707 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5708 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
5709 def:
5710 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5715 /* Class Registration ***************************************************/
5717 VOID
5718 TREEVIEW_Register(void)
5720 WNDCLASSW wndClass;
5722 TRACE("\n");
5724 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5725 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5726 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5727 wndClass.cbClsExtra = 0;
5728 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5730 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5731 wndClass.hbrBackground = 0;
5732 wndClass.lpszClassName = WC_TREEVIEWW;
5734 RegisterClassW(&wndClass);
5738 VOID
5739 TREEVIEW_Unregister(void)
5741 UnregisterClassW(WC_TREEVIEWW, NULL);
5745 /* Tree Verification ****************************************************/
5747 #ifdef NDEBUG
5748 static inline void
5749 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5751 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5752 TREEVIEW_ITEM *item)
5754 assert(infoPtr != NULL);
5755 assert(item != NULL);
5757 /* both NULL, or both non-null */
5758 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5760 assert(item->firstChild != item);
5761 assert(item->lastChild != item);
5763 if (item->firstChild)
5765 assert(item->firstChild->parent == item);
5766 assert(item->firstChild->prevSibling == NULL);
5769 if (item->lastChild)
5771 assert(item->lastChild->parent == item);
5772 assert(item->lastChild->nextSibling == NULL);
5775 assert(item->nextSibling != item);
5776 if (item->nextSibling)
5778 assert(item->nextSibling->parent == item->parent);
5779 assert(item->nextSibling->prevSibling == item);
5782 assert(item->prevSibling != item);
5783 if (item->prevSibling)
5785 assert(item->prevSibling->parent == item->parent);
5786 assert(item->prevSibling->nextSibling == item);
5790 static inline void
5791 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5793 assert(item != NULL);
5795 assert(item->parent != NULL);
5796 assert(item->parent != item);
5797 assert(item->iLevel == item->parent->iLevel + 1);
5799 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5801 TREEVIEW_VerifyItemCommon(infoPtr, item);
5803 TREEVIEW_VerifyChildren(infoPtr, item);
5806 static inline void
5807 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5809 TREEVIEW_ITEM *child;
5810 assert(item != NULL);
5812 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5813 TREEVIEW_VerifyItem(infoPtr, child);
5816 static inline void
5817 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5819 TREEVIEW_ITEM *root = infoPtr->root;
5821 assert(root != NULL);
5822 assert(root->iLevel == -1);
5823 assert(root->parent == NULL);
5824 assert(root->prevSibling == NULL);
5826 TREEVIEW_VerifyItemCommon(infoPtr, root);
5828 TREEVIEW_VerifyChildren(infoPtr, root);
5831 static void
5832 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5834 assert(infoPtr != NULL);
5836 TREEVIEW_VerifyRoot(infoPtr);
5838 #endif