winmm: dwBytesRecorded controls how much is played, not dwBufferLength.
[wine/hramrach.git] / dlls / comctl32 / treeview.c
blob8eb0c6db9184d6038b7099b2e07ae68b1a935859
1 /* Treeview control
3 * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4 * Copyright 1998,1999 Alex Priem <alexp@sci.kun.nl>
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTES
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
30 * TODO:
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
38 * Make the insertion mark look right.
39 * Scroll (instead of repaint) as much as possible.
42 #include "config.h"
43 #include "wine/port.h"
45 #include <assert.h>
46 #include <ctype.h>
47 #include <stdarg.h>
48 #include <string.h>
49 #include <limits.h>
50 #include <stdlib.h>
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
54 #include "windef.h"
55 #include "winbase.h"
56 #include "wingdi.h"
57 #include "winuser.h"
58 #include "winnls.h"
59 #include "commctrl.h"
60 #include "comctl32.h"
61 #include "uxtheme.h"
62 #include "tmschema.h"
63 #include "wine/unicode.h"
64 #include "wine/debug.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
68 /* internal structures */
70 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
72 UINT callbackMask;
73 UINT state;
74 UINT stateMask;
75 LPWSTR pszText;
76 int cchTextMax;
77 int iImage;
78 int iSelectedImage;
79 int cChildren;
80 LPARAM lParam;
81 int iIntegral; /* item height multiplier (1 is normal) */
82 int iLevel; /* indentation level:0=root level */
83 HTREEITEM parent; /* handle to parent or 0 if at root */
84 HTREEITEM firstChild; /* handle to first child or 0 if no child */
85 HTREEITEM lastChild;
86 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
87 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
88 RECT rect;
89 LONG linesOffset;
90 LONG stateOffset;
91 LONG imageOffset;
92 LONG textOffset;
93 LONG textWidth; /* horizontal text extent for pszText */
94 LONG visibleOrder; /* visible ordering, 0 is first visible item */
95 } TREEVIEW_ITEM;
98 typedef struct tagTREEVIEW_INFO
100 HWND hwnd;
101 HWND hwndNotify; /* Owner window to send notifications to */
102 DWORD dwStyle;
103 HTREEITEM root;
104 UINT uInternalStatus;
105 INT Timer;
106 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
107 INT cdmode; /* last custom draw setting */
108 UINT uScrollTime; /* max. time for scrolling in milliseconds */
109 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
111 UINT uItemHeight; /* item height */
112 BOOL bHeightSet;
114 LONG clientWidth; /* width of control window */
115 LONG clientHeight; /* height of control window */
117 LONG treeWidth; /* width of visible tree items */
118 LONG treeHeight; /* height of visible tree items */
120 UINT uIndent; /* indentation in pixels */
121 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
122 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
123 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
124 HTREEITEM editItem; /* item being edited with builtin edit box */
126 HTREEITEM firstVisible; /* handle to first visible item */
127 LONG maxVisibleOrder;
128 HTREEITEM dropItem; /* handle to item selected by drag cursor */
129 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
130 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
131 HIMAGELIST dragList; /* Bitmap of dragged item */
132 LONG scrollX;
133 COLORREF clrBk;
134 COLORREF clrText;
135 COLORREF clrLine;
136 COLORREF clrInsertMark;
137 HFONT hFont;
138 HFONT hDefaultFont;
139 HFONT hBoldFont;
140 HFONT hUnderlineFont;
141 HCURSOR hcurHand;
142 HWND hwndToolTip;
144 HWND hwndEdit;
145 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
146 BOOL bIgnoreEditKillFocus;
147 BOOL bLabelChanged;
149 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
150 HIMAGELIST himlNormal;
151 int normalImageHeight;
152 int normalImageWidth;
153 HIMAGELIST himlState;
154 int stateImageHeight;
155 int stateImageWidth;
156 HDPA items;
158 DWORD lastKeyPressTimestamp;
159 WPARAM charCode;
160 INT nSearchParamLength;
161 WCHAR szSearchParam[ MAX_PATH ];
162 } TREEVIEW_INFO;
165 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
166 #define KEY_DELAY 450
168 /* bitflags for infoPtr->uInternalStatus */
170 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
171 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
172 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
173 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
174 #define TV_RDRAG 0x10 /* ditto Rbutton */
175 #define TV_RDRAGGING 0x20
177 /* bitflags for infoPtr->timer */
179 #define TV_EDIT_TIMER 2
180 #define TV_EDIT_TIMER_SET 2
182 #define TEXT_CALLBACK_SIZE 260
184 #define TREEVIEW_LEFT_MARGIN 8
186 #define MINIMUM_INDENT 19
188 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
190 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
191 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
192 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
194 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
195 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
196 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
197 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
199 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
202 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
205 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
207 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
208 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
209 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
210 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
211 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
212 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
213 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
215 /* Random Utilities *****************************************************/
217 #ifndef NDEBUG
218 static inline void
219 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
221 (void)infoPtr;
223 #else
224 /* The definition is at the end of the file. */
225 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
226 #endif
228 /* Returns the treeview private data if hwnd is a treeview.
229 * Otherwise returns an undefined value. */
230 static TREEVIEW_INFO *
231 TREEVIEW_GetInfoPtr(HWND hwnd)
233 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
236 /* Don't call this. Nothing wants an item index. */
237 static inline int
238 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
240 return DPA_GetPtrIndex(infoPtr->items, handle);
243 /* Checks if item has changed and needs to be redrawn */
244 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
245 const TVITEMEXW *tvChange)
247 /* Number of children has changed */
248 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
249 return TRUE;
251 /* Image has changed and it's not a callback */
252 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
253 tiNew->iImage != I_IMAGECALLBACK)
254 return TRUE;
256 /* Selected image has changed and it's not a callback */
257 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
258 tiNew->iSelectedImage != I_IMAGECALLBACK)
259 return TRUE;
261 /* Text has changed and it's not a callback */
262 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
263 tiNew->pszText != LPSTR_TEXTCALLBACKW)
264 return TRUE;
266 /* Indent has changed */
267 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
268 return TRUE;
270 /* Item state has changed */
271 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
272 return TRUE;
274 return FALSE;
277 /***************************************************************************
278 * This method checks that handle is an item for this tree.
280 static BOOL
281 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
283 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
285 TRACE("invalid item %p\n", handle);
286 return FALSE;
288 else
289 return TRUE;
292 static HFONT
293 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
295 LOGFONTW font;
297 GetObjectW(hOrigFont, sizeof(font), &font);
298 font.lfWeight = FW_BOLD;
299 return CreateFontIndirectW(&font);
302 static HFONT
303 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
305 LOGFONTW font;
307 GetObjectW(hOrigFont, sizeof(font), &font);
308 font.lfUnderline = TRUE;
309 return CreateFontIndirectW(&font);
312 static inline HFONT
313 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
315 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
316 return infoPtr->hUnderlineFont;
317 if (item->state & TVIS_BOLD)
318 return infoPtr->hBoldFont;
319 return infoPtr->hFont;
322 /* for trace/debugging purposes only */
323 static const char *
324 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
326 if (item == NULL) return "<null item>";
327 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
328 if (item->pszText == NULL) return "<null>";
329 return debugstr_w(item->pszText);
332 /* An item is not a child of itself. */
333 static BOOL
334 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
338 child = child->parent;
339 if (child == parent) return TRUE;
340 } while (child != NULL);
342 return FALSE;
346 /* Tree Traversal *******************************************************/
348 /***************************************************************************
349 * This method returns the last expanded sibling or child child item
350 * of a tree node
352 static TREEVIEW_ITEM *
353 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
355 if (!wineItem)
356 return NULL;
358 while (wineItem->lastChild)
360 if (wineItem->state & TVIS_EXPANDED)
361 wineItem = wineItem->lastChild;
362 else
363 break;
366 if (wineItem == infoPtr->root)
367 return NULL;
369 return wineItem;
372 /***************************************************************************
373 * This method returns the previous non-hidden item in the list not
374 * considering the tree hierarchy.
376 static TREEVIEW_ITEM *
377 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
379 if (tvItem->prevSibling)
381 /* This item has a prevSibling, get the last item in the sibling's tree. */
382 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
384 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
385 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
386 else
387 return upItem;
389 else
391 /* this item does not have a prevSibling, get the parent */
392 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
397 /***************************************************************************
398 * This method returns the next physical item in the treeview not
399 * considering the tree hierarchy.
401 static TREEVIEW_ITEM *
402 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
405 * If this item has children and is expanded, return the first child
407 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
409 return tvItem->firstChild;
414 * try to get the sibling
416 if (tvItem->nextSibling)
417 return tvItem->nextSibling;
420 * Otherwise, get the parent's sibling.
422 while (tvItem->parent)
424 tvItem = tvItem->parent;
426 if (tvItem->nextSibling)
427 return tvItem->nextSibling;
430 return NULL;
433 /***************************************************************************
434 * This method returns the nth item starting at the given item. It returns
435 * the last item (or first) we we run out of items.
437 * Will scroll backward if count is <0.
438 * forward if count is >0.
440 static TREEVIEW_ITEM *
441 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
442 LONG count)
444 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
445 TREEVIEW_ITEM *previousItem;
447 assert(wineItem != NULL);
449 if (count > 0)
451 next_item = TREEVIEW_GetNextListItem;
453 else if (count < 0)
455 count = -count;
456 next_item = TREEVIEW_GetPrevListItem;
458 else
459 return wineItem;
463 previousItem = wineItem;
464 wineItem = next_item(infoPtr, wineItem);
466 } while (--count && wineItem != NULL);
469 return wineItem ? wineItem : previousItem;
472 /* Notifications ************************************************************/
474 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
476 if (!infoPtr->bNtfUnicode) {
477 switch (code) {
478 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
479 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
480 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
481 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
482 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
483 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
484 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
485 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
486 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
487 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
488 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
489 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
492 return code;
495 static LRESULT
496 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
498 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
499 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
502 static BOOL
503 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
505 NMHDR nmhdr;
506 HWND hwnd = infoPtr->hwnd;
508 TRACE("%d\n", code);
509 nmhdr.hwndFrom = hwnd;
510 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
511 nmhdr.code = get_notifycode(infoPtr, code);
513 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
516 static VOID
517 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
519 tvItem->mask = mask;
520 tvItem->hItem = item;
521 tvItem->state = item->state;
522 tvItem->stateMask = 0;
523 tvItem->iImage = item->iImage;
524 tvItem->iSelectedImage = item->iSelectedImage;
525 tvItem->cChildren = item->cChildren;
526 tvItem->lParam = item->lParam;
528 if(mask & TVIF_TEXT)
530 if (!infoPtr->bNtfUnicode)
532 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
533 tvItem->pszText = Alloc (tvItem->cchTextMax);
534 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
536 else
538 tvItem->cchTextMax = item->cchTextMax;
539 tvItem->pszText = item->pszText;
542 else
544 tvItem->cchTextMax = 0;
545 tvItem->pszText = NULL;
549 static BOOL
550 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
551 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
553 HWND hwnd = infoPtr->hwnd;
554 NMTREEVIEWW nmhdr;
555 BOOL ret;
557 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
558 code, action, oldItem, newItem);
560 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
562 nmhdr.hdr.hwndFrom = hwnd;
563 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
564 nmhdr.hdr.code = get_notifycode(infoPtr, code);
565 nmhdr.action = action;
567 if (oldItem)
568 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
570 if (newItem)
571 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
573 nmhdr.ptDrag.x = 0;
574 nmhdr.ptDrag.y = 0;
576 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
577 if (!infoPtr->bNtfUnicode)
579 Free(nmhdr.itemOld.pszText);
580 Free(nmhdr.itemNew.pszText);
582 return ret;
585 static BOOL
586 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
587 HTREEITEM dragItem, POINT pt)
589 HWND hwnd = infoPtr->hwnd;
590 NMTREEVIEWW nmhdr;
592 TRACE("code:%d dragitem:%p\n", code, dragItem);
594 nmhdr.hdr.hwndFrom = hwnd;
595 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
596 nmhdr.hdr.code = get_notifycode(infoPtr, code);
597 nmhdr.action = 0;
598 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
599 nmhdr.itemNew.hItem = dragItem;
600 nmhdr.itemNew.state = dragItem->state;
601 nmhdr.itemNew.lParam = dragItem->lParam;
603 nmhdr.ptDrag.x = pt.x;
604 nmhdr.ptDrag.y = pt.y;
606 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
610 static BOOL
611 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
612 HDC hdc, RECT rc)
614 HWND hwnd = infoPtr->hwnd;
615 NMTVCUSTOMDRAW nmcdhdr;
616 LPNMCUSTOMDRAW nmcd;
618 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
620 nmcd = &nmcdhdr.nmcd;
621 nmcd->hdr.hwndFrom = hwnd;
622 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
623 nmcd->hdr.code = NM_CUSTOMDRAW;
624 nmcd->dwDrawStage = dwDrawStage;
625 nmcd->hdc = hdc;
626 nmcd->rc = rc;
627 nmcd->dwItemSpec = 0;
628 nmcd->uItemState = 0;
629 nmcd->lItemlParam = 0;
630 nmcdhdr.clrText = infoPtr->clrText;
631 nmcdhdr.clrTextBk = infoPtr->clrBk;
632 nmcdhdr.iLevel = 0;
634 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
639 /* FIXME: need to find out when the flags in uItemState need to be set */
641 static BOOL
642 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
643 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
644 NMTVCUSTOMDRAW *nmcdhdr)
646 HWND hwnd = infoPtr->hwnd;
647 LPNMCUSTOMDRAW nmcd;
648 DWORD dwDrawStage;
649 DWORD_PTR dwItemSpec;
650 UINT uItemState;
651 INT retval;
653 dwDrawStage = CDDS_ITEM | uItemDrawState;
654 dwItemSpec = (DWORD_PTR)wineItem;
655 uItemState = 0;
656 if (wineItem->state & TVIS_SELECTED)
657 uItemState |= CDIS_SELECTED;
658 if (wineItem == infoPtr->selectedItem)
659 uItemState |= CDIS_FOCUS;
660 if (wineItem == infoPtr->hotItem)
661 uItemState |= CDIS_HOT;
663 nmcd = &nmcdhdr->nmcd;
664 nmcd->hdr.hwndFrom = hwnd;
665 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
666 nmcd->hdr.code = NM_CUSTOMDRAW;
667 nmcd->dwDrawStage = dwDrawStage;
668 nmcd->hdc = hdc;
669 nmcd->rc = wineItem->rect;
670 nmcd->dwItemSpec = dwItemSpec;
671 nmcd->uItemState = uItemState;
672 nmcd->lItemlParam = wineItem->lParam;
673 nmcdhdr->iLevel = wineItem->iLevel;
675 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
676 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
677 nmcd->uItemState, nmcd->lItemlParam);
679 retval = TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
681 return retval;
684 static BOOL
685 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
687 HWND hwnd = infoPtr->hwnd;
688 NMTVDISPINFOW tvdi;
689 BOOL ret;
691 tvdi.hdr.hwndFrom = hwnd;
692 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
693 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
695 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
696 &tvdi.item, editItem);
698 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
700 if (!infoPtr->bNtfUnicode)
701 Free(tvdi.item.pszText);
703 return ret;
706 static void
707 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
708 UINT mask)
710 NMTVDISPINFOEXW callback;
711 HWND hwnd = infoPtr->hwnd;
713 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
714 mask &= wineItem->callbackMask;
716 if (mask == 0) return;
718 callback.hdr.hwndFrom = hwnd;
719 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
720 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
722 /* 'state' always contains valid value, as well as 'lParam'.
723 * All other parameters are uninitialized.
725 callback.item.pszText = wineItem->pszText;
726 callback.item.cchTextMax = wineItem->cchTextMax;
727 callback.item.mask = mask;
728 callback.item.hItem = wineItem;
729 callback.item.state = wineItem->state;
730 callback.item.lParam = wineItem->lParam;
732 /* If text is changed we need to recalculate textWidth */
733 if (mask & TVIF_TEXT)
734 wineItem->textWidth = 0;
736 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
738 /* It may have changed due to a call to SetItem. */
739 mask &= wineItem->callbackMask;
741 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
743 /* Instead of copying text into our buffer user specified its own */
744 if (!infoPtr->bNtfUnicode) {
745 LPWSTR newText;
746 int buflen;
747 int len = MultiByteToWideChar( CP_ACP, 0,
748 (LPSTR)callback.item.pszText, -1,
749 NULL, 0);
750 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
751 newText = ReAlloc(wineItem->pszText, buflen);
753 TRACE("returned str %s, len=%d, buflen=%d\n",
754 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
756 if (newText)
758 wineItem->pszText = newText;
759 MultiByteToWideChar( CP_ACP, 0,
760 (LPSTR)callback.item.pszText, -1,
761 wineItem->pszText, buflen/sizeof(WCHAR));
762 wineItem->cchTextMax = buflen/sizeof(WCHAR);
764 /* If ReAlloc fails we have nothing to do, but keep original text */
766 else {
767 int len = max(lstrlenW(callback.item.pszText) + 1,
768 TEXT_CALLBACK_SIZE);
769 LPWSTR newText = ReAlloc(wineItem->pszText, len);
771 TRACE("returned wstr %s, len=%d\n",
772 debugstr_w(callback.item.pszText), len);
774 if (newText)
776 wineItem->pszText = newText;
777 strcpyW(wineItem->pszText, callback.item.pszText);
778 wineItem->cchTextMax = len;
780 /* If ReAlloc fails we have nothing to do, but keep original text */
783 else if (mask & TVIF_TEXT) {
784 /* User put text into our buffer, that is ok unless A string */
785 if (!infoPtr->bNtfUnicode) {
786 LPWSTR newText;
787 LPWSTR oldText = NULL;
788 int buflen;
789 int len = MultiByteToWideChar( CP_ACP, 0,
790 (LPSTR)callback.item.pszText, -1,
791 NULL, 0);
792 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
793 newText = Alloc(buflen);
795 TRACE("same buffer str %s, len=%d, buflen=%d\n",
796 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
798 if (newText)
800 oldText = wineItem->pszText;
801 wineItem->pszText = newText;
802 MultiByteToWideChar( CP_ACP, 0,
803 (LPSTR)callback.item.pszText, -1,
804 wineItem->pszText, buflen/sizeof(WCHAR));
805 wineItem->cchTextMax = buflen/sizeof(WCHAR);
806 Free(oldText);
811 if (mask & TVIF_IMAGE)
812 wineItem->iImage = callback.item.iImage;
814 if (mask & TVIF_SELECTEDIMAGE)
815 wineItem->iSelectedImage = callback.item.iSelectedImage;
817 if (mask & TVIF_CHILDREN)
818 wineItem->cChildren = callback.item.cChildren;
820 /* These members are now permanently set. */
821 if (callback.item.mask & TVIF_DI_SETITEM)
822 wineItem->callbackMask &= ~callback.item.mask;
825 /***************************************************************************
826 * This function uses cChildren field to decide whether the item has
827 * children or not.
828 * Note: if this returns TRUE, the child items may not actually exist,
829 * they could be virtual.
831 * Just use wineItem->firstChild to check for physical children.
833 static BOOL
834 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
836 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
838 return wineItem->cChildren > 0;
841 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
843 INT format;
845 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
847 if (nCommand != NF_REQUERY) return 0;
849 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
850 TRACE("format=%d\n", format);
852 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
854 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
856 return format;
859 /* Item Position ********************************************************/
861 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
862 static VOID
863 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
865 /* Same effect, different optimisation. */
866 #if 0
867 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
868 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
869 #else
870 BOOL lar = ((infoPtr->dwStyle
871 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
872 > TVS_LINESATROOT);
873 #endif
875 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
876 - infoPtr->scrollX;
877 item->stateOffset = item->linesOffset + infoPtr->uIndent;
878 item->imageOffset = item->stateOffset
879 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
880 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
883 static VOID
884 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
886 HDC hdc;
887 HFONT hOldFont=0;
888 SIZE sz;
890 /* DRAW's OM docker creates items like this */
891 if (item->pszText == NULL)
893 item->textWidth = 0;
894 return;
897 if (hDC != 0)
899 hdc = hDC;
901 else
903 hdc = GetDC(infoPtr->hwnd);
904 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
907 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
908 item->textWidth = sz.cx;
910 if (hDC == 0)
912 SelectObject(hdc, hOldFont);
913 ReleaseDC(0, hdc);
917 static VOID
918 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
920 item->rect.top = infoPtr->uItemHeight *
921 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
923 item->rect.bottom = item->rect.top
924 + infoPtr->uItemHeight * item->iIntegral - 1;
926 item->rect.left = 0;
927 item->rect.right = infoPtr->clientWidth;
930 /* We know that only items after start need their order updated. */
931 static void
932 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
934 TREEVIEW_ITEM *item;
935 int order;
937 if (!start)
939 start = infoPtr->root->firstChild;
940 order = 0;
942 else
943 order = start->visibleOrder;
945 for (item = start; item != NULL;
946 item = TREEVIEW_GetNextListItem(infoPtr, item))
948 if (!ISVISIBLE(item) && order > 0)
949 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
950 item->visibleOrder = order;
951 order += item->iIntegral;
954 infoPtr->maxVisibleOrder = order;
956 for (item = start; item != NULL;
957 item = TREEVIEW_GetNextListItem(infoPtr, item))
959 TREEVIEW_ComputeItemRect(infoPtr, item);
964 /* Update metrics of all items in selected subtree.
965 * root must be expanded
967 static VOID
968 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
970 TREEVIEW_ITEM *sibling;
971 HDC hdc;
972 HFONT hOldFont;
974 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
975 return;
977 root->state &= ~TVIS_EXPANDED;
978 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
979 root->state |= TVIS_EXPANDED;
981 hdc = GetDC(infoPtr->hwnd);
982 hOldFont = SelectObject(hdc, infoPtr->hFont);
984 for (; root != sibling;
985 root = TREEVIEW_GetNextListItem(infoPtr, root))
987 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
989 if (root->callbackMask & TVIF_TEXT)
990 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
992 if (root->textWidth == 0)
994 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
995 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
999 SelectObject(hdc, hOldFont);
1000 ReleaseDC(infoPtr->hwnd, hdc);
1003 /* Item Allocation **********************************************************/
1005 static TREEVIEW_ITEM *
1006 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
1008 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1010 if (!newItem)
1011 return NULL;
1013 /* I_IMAGENONE would make more sense but this is neither what is
1014 * documented (MSDN doesn't specify) nor what Windows actually does
1015 * (it sets it to zero)... and I can so imagine an application using
1016 * inc/dec to toggle the images. */
1017 newItem->iImage = 0;
1018 newItem->iSelectedImage = 0;
1020 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1022 Free(newItem);
1023 return NULL;
1026 return newItem;
1029 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1030 * free item->pszText. */
1031 static void
1032 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1034 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, 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;
1047 Free(item);
1051 /* Item Insertion *******************************************************/
1053 /***************************************************************************
1054 * This method inserts newItem before sibling as a child of parent.
1055 * sibling can be NULL, but only if parent has no children.
1057 static void
1058 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1059 TREEVIEW_ITEM *parent)
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(parent != NULL);
1095 if (sibling != NULL)
1097 assert(sibling->parent == parent);
1099 if (sibling->nextSibling != NULL)
1100 sibling->nextSibling->prevSibling = newItem;
1102 newItem->nextSibling = sibling->nextSibling;
1103 sibling->nextSibling = newItem;
1105 else
1106 newItem->nextSibling = NULL;
1108 newItem->prevSibling = sibling;
1110 if (parent->lastChild == sibling)
1111 parent->lastChild = newItem;
1113 if (parent->firstChild == NULL)
1114 parent->firstChild = newItem;
1117 static BOOL
1118 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1119 const TVITEMEXW *tvItem, BOOL isW)
1121 UINT callbackClear = 0;
1122 UINT callbackSet = 0;
1124 TRACE("item %p\n", wineItem);
1125 /* Do this first in case it fails. */
1126 if (tvItem->mask & TVIF_TEXT)
1128 wineItem->textWidth = 0; /* force width recalculation */
1129 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1131 int len;
1132 LPWSTR newText;
1133 if (isW)
1134 len = lstrlenW(tvItem->pszText) + 1;
1135 else
1136 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1138 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1140 if (newText == NULL) return FALSE;
1142 callbackClear |= TVIF_TEXT;
1144 wineItem->pszText = newText;
1145 wineItem->cchTextMax = len;
1146 if (isW)
1147 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1148 else
1149 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1150 wineItem->pszText, len);
1152 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1154 else
1156 callbackSet |= TVIF_TEXT;
1158 wineItem->pszText = ReAlloc(wineItem->pszText,
1159 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1160 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1161 TRACE("setting callback, item %p\n", wineItem);
1165 if (tvItem->mask & TVIF_CHILDREN)
1167 wineItem->cChildren = tvItem->cChildren;
1169 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1170 callbackSet |= TVIF_CHILDREN;
1171 else
1172 callbackClear |= TVIF_CHILDREN;
1175 if (tvItem->mask & TVIF_IMAGE)
1177 wineItem->iImage = tvItem->iImage;
1179 if (wineItem->iImage == I_IMAGECALLBACK)
1180 callbackSet |= TVIF_IMAGE;
1181 else
1182 callbackClear |= TVIF_IMAGE;
1185 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1187 wineItem->iSelectedImage = tvItem->iSelectedImage;
1189 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1190 callbackSet |= TVIF_SELECTEDIMAGE;
1191 else
1192 callbackClear |= TVIF_SELECTEDIMAGE;
1195 if (tvItem->mask & TVIF_PARAM)
1196 wineItem->lParam = tvItem->lParam;
1198 /* If the application sets TVIF_INTEGRAL without
1199 * supplying a TVITEMEX structure, it's toast. */
1200 if (tvItem->mask & TVIF_INTEGRAL)
1201 wineItem->iIntegral = tvItem->iIntegral;
1203 if (tvItem->mask & TVIF_STATE)
1205 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1206 tvItem->stateMask);
1207 wineItem->state &= ~tvItem->stateMask;
1208 wineItem->state |= (tvItem->state & tvItem->stateMask);
1211 wineItem->callbackMask |= callbackSet;
1212 wineItem->callbackMask &= ~callbackClear;
1214 return TRUE;
1217 /* Note that the new item is pre-zeroed. */
1218 static LRESULT
1219 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1221 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1222 HTREEITEM insertAfter;
1223 TREEVIEW_ITEM *newItem, *parentItem;
1224 BOOL bTextUpdated = FALSE;
1226 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1228 parentItem = infoPtr->root;
1230 else
1232 parentItem = ptdi->hParent;
1234 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1236 WARN("invalid parent %p\n", parentItem);
1237 return 0;
1241 insertAfter = ptdi->hInsertAfter;
1243 /* Validate this now for convenience. */
1244 switch ((DWORD_PTR)insertAfter)
1246 case (DWORD_PTR)TVI_FIRST:
1247 case (DWORD_PTR)TVI_LAST:
1248 case (DWORD_PTR)TVI_SORT:
1249 break;
1251 default:
1252 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1253 insertAfter->parent != parentItem)
1255 WARN("invalid insert after %p\n", insertAfter);
1256 insertAfter = TVI_LAST;
1260 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1261 (tvItem->mask & TVIF_TEXT)
1262 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1263 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1264 : "<no label>");
1266 newItem = TREEVIEW_AllocateItem(infoPtr);
1267 if (newItem == NULL)
1268 return 0;
1270 newItem->parent = parentItem;
1271 newItem->iIntegral = 1;
1272 newItem->visibleOrder = -1;
1274 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1275 return 0;
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 (!infoPtr->bRedraw) return (LRESULT)newItem;
1387 if (parentItem == infoPtr->root ||
1388 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1390 TREEVIEW_ITEM *item;
1391 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1393 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1394 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1396 if (!bTextUpdated)
1397 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1399 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1400 TREEVIEW_UpdateScrollBars(infoPtr);
1402 * if the item was inserted in a visible part of the tree,
1403 * invalidate it, as well as those after it
1405 for (item = newItem;
1406 item != NULL;
1407 item = TREEVIEW_GetNextListItem(infoPtr, item))
1408 TREEVIEW_Invalidate(infoPtr, item);
1410 else
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);
1582 if (!infoPtr->bRedraw) return TRUE;
1584 if (visible)
1586 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1587 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1588 TREEVIEW_UpdateScrollBars(infoPtr);
1589 TREEVIEW_Invalidate(infoPtr, NULL);
1591 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1593 /* parent lost '+/-' - update it */
1594 TREEVIEW_Invalidate(infoPtr, parent);
1597 return TRUE;
1601 /* Get/Set Messages *********************************************************/
1602 static LRESULT
1603 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1605 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1607 if (infoPtr->bRedraw)
1609 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1610 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1611 TREEVIEW_UpdateScrollBars(infoPtr);
1612 TREEVIEW_Invalidate(infoPtr, NULL);
1614 return 0;
1617 static LRESULT
1618 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1620 TRACE("\n");
1621 return infoPtr->uIndent;
1624 static LRESULT
1625 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1627 TRACE("\n");
1629 if (newIndent < MINIMUM_INDENT)
1630 newIndent = MINIMUM_INDENT;
1632 if (infoPtr->uIndent != newIndent)
1634 infoPtr->uIndent = newIndent;
1635 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1636 TREEVIEW_UpdateScrollBars(infoPtr);
1637 TREEVIEW_Invalidate(infoPtr, NULL);
1640 return 0;
1644 static LRESULT
1645 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1647 TRACE("\n");
1648 return (LRESULT)infoPtr->hwndToolTip;
1651 static LRESULT
1652 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1654 HWND prevToolTip;
1656 TRACE("\n");
1657 prevToolTip = infoPtr->hwndToolTip;
1658 infoPtr->hwndToolTip = hwndTT;
1660 return (LRESULT)prevToolTip;
1663 static LRESULT
1664 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1666 BOOL rc = infoPtr->bNtfUnicode;
1667 infoPtr->bNtfUnicode = fUnicode;
1668 return rc;
1671 static LRESULT
1672 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1674 return infoPtr->bNtfUnicode;
1677 static LRESULT
1678 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1680 return infoPtr->uScrollTime;
1683 static LRESULT
1684 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1686 UINT uOldScrollTime = infoPtr->uScrollTime;
1688 infoPtr->uScrollTime = min(uScrollTime, 100);
1690 return uOldScrollTime;
1694 static LRESULT
1695 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1697 TRACE("\n");
1699 switch (wParam)
1701 case TVSIL_NORMAL:
1702 return (LRESULT)infoPtr->himlNormal;
1704 case TVSIL_STATE:
1705 return (LRESULT)infoPtr->himlState;
1707 default:
1708 return 0;
1712 #define TVHEIGHT_MIN 16
1713 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1715 /* Compute the natural height for items. */
1716 static UINT
1717 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1719 TEXTMETRICW tm;
1720 HDC hdc = GetDC(0);
1721 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1722 UINT height;
1724 /* Height is the maximum of:
1725 * 16 (a hack because our fonts are tiny), and
1726 * The text height + border & margin, and
1727 * The size of the normal image list
1729 GetTextMetricsW(hdc, &tm);
1730 SelectObject(hdc, hOldFont);
1731 ReleaseDC(0, hdc);
1733 height = TVHEIGHT_MIN;
1734 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1735 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1736 if (height < infoPtr->normalImageHeight)
1737 height = infoPtr->normalImageHeight;
1739 /* Round down, unless we support odd ("non even") heights. */
1740 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1741 height &= ~1;
1743 return height;
1746 static LRESULT
1747 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1749 HIMAGELIST himlOld = 0;
1750 int oldWidth = infoPtr->normalImageWidth;
1751 int oldHeight = infoPtr->normalImageHeight;
1754 TRACE("%lx,%p\n", wParam, himlNew);
1756 switch (wParam)
1758 case TVSIL_NORMAL:
1759 himlOld = infoPtr->himlNormal;
1760 infoPtr->himlNormal = himlNew;
1762 if (himlNew != NULL)
1763 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1764 &infoPtr->normalImageHeight);
1765 else
1767 infoPtr->normalImageWidth = 0;
1768 infoPtr->normalImageHeight = 0;
1771 break;
1773 case TVSIL_STATE:
1774 himlOld = infoPtr->himlState;
1775 infoPtr->himlState = himlNew;
1777 if (himlNew != NULL)
1778 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1779 &infoPtr->stateImageHeight);
1780 else
1782 infoPtr->stateImageWidth = 0;
1783 infoPtr->stateImageHeight = 0;
1786 break;
1789 if (oldWidth != infoPtr->normalImageWidth ||
1790 oldHeight != infoPtr->normalImageHeight)
1792 BOOL bRecalcVisible = FALSE;
1794 if (oldHeight != infoPtr->normalImageHeight &&
1795 !infoPtr->bHeightSet)
1797 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1798 bRecalcVisible = TRUE;
1801 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1802 infoPtr->normalImageWidth != infoPtr->uIndent)
1804 infoPtr->uIndent = infoPtr->normalImageWidth;
1805 bRecalcVisible = TRUE;
1808 if (bRecalcVisible)
1809 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1811 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1812 TREEVIEW_UpdateScrollBars(infoPtr);
1815 TREEVIEW_Invalidate(infoPtr, NULL);
1817 return (LRESULT)himlOld;
1820 static LRESULT
1821 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1823 INT prevHeight = infoPtr->uItemHeight;
1825 TRACE("%d\n", newHeight);
1826 if (newHeight == -1)
1828 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1829 infoPtr->bHeightSet = FALSE;
1831 else
1833 infoPtr->uItemHeight = newHeight;
1834 infoPtr->bHeightSet = TRUE;
1837 /* Round down, unless we support odd ("non even") heights. */
1838 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1839 infoPtr->uItemHeight &= ~1;
1841 if (infoPtr->uItemHeight != prevHeight)
1843 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1844 TREEVIEW_UpdateScrollBars(infoPtr);
1845 TREEVIEW_Invalidate(infoPtr, NULL);
1848 return prevHeight;
1851 static LRESULT
1852 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1854 TRACE("\n");
1855 return infoPtr->uItemHeight;
1859 static LRESULT
1860 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1862 TRACE("%p\n", infoPtr->hFont);
1863 return (LRESULT)infoPtr->hFont;
1867 static INT CALLBACK
1868 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1870 (void)unused;
1872 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1874 return 1;
1877 static LRESULT
1878 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1880 UINT uHeight = infoPtr->uItemHeight;
1882 TRACE("%p %i\n", hFont, bRedraw);
1884 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1886 DeleteObject(infoPtr->hBoldFont);
1887 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1888 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1890 if (!infoPtr->bHeightSet)
1891 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1893 if (uHeight != infoPtr->uItemHeight)
1894 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1896 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1898 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1899 TREEVIEW_UpdateScrollBars(infoPtr);
1901 if (bRedraw)
1902 TREEVIEW_Invalidate(infoPtr, NULL);
1904 return 0;
1908 static LRESULT
1909 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1911 TRACE("\n");
1912 return (LRESULT)infoPtr->clrLine;
1915 static LRESULT
1916 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1918 COLORREF prevColor = infoPtr->clrLine;
1920 TRACE("\n");
1921 infoPtr->clrLine = color;
1922 return (LRESULT)prevColor;
1926 static LRESULT
1927 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1929 TRACE("\n");
1930 return (LRESULT)infoPtr->clrText;
1933 static LRESULT
1934 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1936 COLORREF prevColor = infoPtr->clrText;
1938 TRACE("\n");
1939 infoPtr->clrText = color;
1941 if (infoPtr->clrText != prevColor)
1942 TREEVIEW_Invalidate(infoPtr, NULL);
1944 return (LRESULT)prevColor;
1948 static LRESULT
1949 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1951 TRACE("\n");
1952 return (LRESULT)infoPtr->clrBk;
1955 static LRESULT
1956 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1958 COLORREF prevColor = infoPtr->clrBk;
1960 TRACE("\n");
1961 infoPtr->clrBk = newColor;
1963 if (newColor != prevColor)
1964 TREEVIEW_Invalidate(infoPtr, NULL);
1966 return (LRESULT)prevColor;
1970 static LRESULT
1971 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1973 TRACE("\n");
1974 return (LRESULT)infoPtr->clrInsertMark;
1977 static LRESULT
1978 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1980 COLORREF prevColor = infoPtr->clrInsertMark;
1982 TRACE("%x\n", color);
1983 infoPtr->clrInsertMark = color;
1985 return (LRESULT)prevColor;
1989 static LRESULT
1990 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1992 TRACE("%d %p\n", wParam, item);
1994 if (!TREEVIEW_ValidItem(infoPtr, item))
1995 return 0;
1997 infoPtr->insertBeforeorAfter = wParam;
1998 infoPtr->insertMarkItem = item;
2000 TREEVIEW_Invalidate(infoPtr, NULL);
2002 return 1;
2006 /************************************************************************
2007 * Some serious braindamage here. lParam is a pointer to both the
2008 * input HTREEITEM and the output RECT.
2010 static LRESULT
2011 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2013 TREEVIEW_ITEM *wineItem;
2014 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2016 TRACE("\n");
2018 * validate parameters
2020 if (pItem == NULL)
2021 return FALSE;
2023 wineItem = *pItem;
2024 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2025 return FALSE;
2028 * If wParam is TRUE return the text size otherwise return
2029 * the whole item size
2031 if (fTextRect)
2033 /* Windows does not send TVN_GETDISPINFO here. */
2035 lpRect->top = wineItem->rect.top;
2036 lpRect->bottom = wineItem->rect.bottom;
2038 lpRect->left = wineItem->textOffset;
2039 if (!wineItem->textWidth)
2040 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2042 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2044 else
2046 *lpRect = wineItem->rect;
2049 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2051 return TRUE;
2054 static inline LRESULT
2055 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2057 /* Surprise! This does not take integral height into account. */
2058 return infoPtr->clientHeight / infoPtr->uItemHeight;
2062 static LRESULT
2063 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2065 TREEVIEW_ITEM *wineItem;
2067 wineItem = tvItem->hItem;
2068 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2069 return FALSE;
2071 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2073 if (tvItem->mask & TVIF_CHILDREN)
2075 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2076 FIXME("I_CHILDRENCALLBACK not supported\n");
2077 tvItem->cChildren = wineItem->cChildren;
2080 if (tvItem->mask & TVIF_HANDLE)
2081 tvItem->hItem = wineItem;
2083 if (tvItem->mask & TVIF_IMAGE)
2084 tvItem->iImage = wineItem->iImage;
2086 if (tvItem->mask & TVIF_INTEGRAL)
2087 tvItem->iIntegral = wineItem->iIntegral;
2089 /* undocumented: windows ignores TVIF_PARAM and
2090 * * always sets lParam
2092 tvItem->lParam = wineItem->lParam;
2094 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2095 tvItem->iSelectedImage = wineItem->iSelectedImage;
2097 if (tvItem->mask & TVIF_STATE)
2098 /* Careful here - Windows ignores the stateMask when you get the state
2099 That contradicts the documentation, but makes more common sense, masking
2100 retrieval in this way seems overkill */
2101 tvItem->state = wineItem->state;
2103 if (tvItem->mask & TVIF_TEXT)
2105 if (wineItem->pszText == NULL)
2107 if (tvItem->cchTextMax > 0)
2108 tvItem->pszText[0] = '\0';
2110 else if (isW)
2112 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2114 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2115 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2117 else
2119 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2122 else
2124 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2126 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2127 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2129 else
2131 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2132 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2136 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2137 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2139 return TRUE;
2142 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2143 * which is wrong. */
2144 static LRESULT
2145 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2147 TREEVIEW_ITEM *wineItem;
2148 TREEVIEW_ITEM originalItem;
2150 wineItem = tvItem->hItem;
2152 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2153 tvItem->mask);
2155 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2156 return FALSE;
2158 /* store the original item values */
2159 originalItem = *wineItem;
2161 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2162 return FALSE;
2164 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2165 if ((tvItem->mask & TVIF_TEXT
2166 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2167 && ISVISIBLE(wineItem))
2169 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2170 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2173 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2175 /* The refresh updates everything, but we can't wait until then. */
2176 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2178 /* if any of the item's values changed and it's not a callback, redraw the item */
2179 if (item_changed(&originalItem, wineItem, tvItem))
2181 if (tvItem->mask & TVIF_INTEGRAL)
2183 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2184 TREEVIEW_UpdateScrollBars(infoPtr);
2186 TREEVIEW_Invalidate(infoPtr, NULL);
2188 else
2190 TREEVIEW_UpdateScrollBars(infoPtr);
2191 TREEVIEW_Invalidate(infoPtr, wineItem);
2196 return TRUE;
2199 static LRESULT
2200 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2202 TRACE("\n");
2204 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2205 return 0;
2207 return (wineItem->state & mask);
2210 static LRESULT
2211 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2213 TREEVIEW_ITEM *retval;
2215 retval = 0;
2217 /* handle all the global data here */
2218 switch (which)
2220 case TVGN_CHILD: /* Special case: child of 0 is root */
2221 if (wineItem)
2222 break;
2223 /* fall through */
2224 case TVGN_ROOT:
2225 retval = infoPtr->root->firstChild;
2226 break;
2228 case TVGN_CARET:
2229 retval = infoPtr->selectedItem;
2230 break;
2232 case TVGN_FIRSTVISIBLE:
2233 retval = infoPtr->firstVisible;
2234 break;
2236 case TVGN_DROPHILITE:
2237 retval = infoPtr->dropItem;
2238 break;
2240 case TVGN_LASTVISIBLE:
2241 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2242 break;
2245 if (retval)
2247 TRACE("flags:%x, returns %p\n", which, retval);
2248 return (LRESULT)retval;
2251 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2253 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2254 return FALSE;
2256 switch (which)
2258 case TVGN_NEXT:
2259 retval = wineItem->nextSibling;
2260 break;
2261 case TVGN_PREVIOUS:
2262 retval = wineItem->prevSibling;
2263 break;
2264 case TVGN_PARENT:
2265 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2266 break;
2267 case TVGN_CHILD:
2268 retval = wineItem->firstChild;
2269 break;
2270 case TVGN_NEXTVISIBLE:
2271 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2272 break;
2273 case TVGN_PREVIOUSVISIBLE:
2274 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2275 break;
2276 default:
2277 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2278 break;
2281 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2282 return (LRESULT)retval;
2286 static LRESULT
2287 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2289 TRACE(" %d\n", infoPtr->uNumItems);
2290 return (LRESULT)infoPtr->uNumItems;
2293 static VOID
2294 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2296 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2298 static const unsigned int state_table[] = { 0, 2, 1 };
2300 unsigned int state;
2302 state = STATEIMAGEINDEX(item->state);
2303 TRACE("state:%x\n", state);
2304 item->state &= ~TVIS_STATEIMAGEMASK;
2306 if (state < 3)
2307 state = state_table[state];
2309 item->state |= INDEXTOSTATEIMAGEMASK(state);
2311 TRACE("state:%x\n", state);
2312 TREEVIEW_Invalidate(infoPtr, item);
2317 /* Painting *************************************************************/
2319 /* Draw the lines and expand button for an item. Also draws one section
2320 * of the line from item's parent to item's parent's next sibling. */
2321 static void
2322 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2324 LONG centerx, centery;
2325 BOOL lar = ((infoPtr->dwStyle
2326 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2327 > TVS_LINESATROOT);
2328 HBRUSH hbr, hbrOld;
2329 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2331 if (!lar && item->iLevel == 0)
2332 return;
2334 hbr = CreateSolidBrush(clrBk);
2335 hbrOld = SelectObject(hdc, hbr);
2337 centerx = (item->linesOffset + item->stateOffset) / 2;
2338 centery = (item->rect.top + item->rect.bottom) / 2;
2340 if (infoPtr->dwStyle & TVS_HASLINES)
2342 HPEN hOldPen, hNewPen;
2343 HTREEITEM parent;
2344 LOGBRUSH lb;
2346 /* Get a dotted grey pen */
2347 lb.lbStyle = BS_SOLID;
2348 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2349 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2350 hOldPen = SelectObject(hdc, hNewPen);
2352 /* Make sure the center is on a dot (using +2 instead
2353 * of +1 gives us pixel-by-pixel compat with native) */
2354 centery = (centery + 2) & ~1;
2356 MoveToEx(hdc, item->stateOffset, centery, NULL);
2357 LineTo(hdc, centerx - 1, centery);
2359 if (item->prevSibling || item->parent != infoPtr->root)
2361 MoveToEx(hdc, centerx, item->rect.top, NULL);
2362 LineTo(hdc, centerx, centery);
2365 if (item->nextSibling)
2367 MoveToEx(hdc, centerx, centery, NULL);
2368 LineTo(hdc, centerx, item->rect.bottom + 1);
2371 /* Draw the line from our parent to its next sibling. */
2372 parent = item->parent;
2373 while (parent != infoPtr->root)
2375 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2377 if (parent->nextSibling
2378 /* skip top-levels unless TVS_LINESATROOT */
2379 && parent->stateOffset > parent->linesOffset)
2381 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2382 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2385 parent = parent->parent;
2388 SelectObject(hdc, hOldPen);
2389 DeleteObject(hNewPen);
2393 * Display the (+/-) signs
2396 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2398 if (item->cChildren)
2400 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2401 if (theme)
2403 RECT glyphRect = item->rect;
2404 glyphRect.left = item->linesOffset;
2405 glyphRect.right = item->stateOffset;
2406 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2407 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2408 &glyphRect, NULL);
2410 else
2412 LONG height = item->rect.bottom - item->rect.top;
2413 LONG width = item->stateOffset - item->linesOffset;
2414 LONG rectsize = min(height, width) / 4;
2415 /* plussize = ceil(rectsize * 3/4) */
2416 LONG plussize = (rectsize + 1) * 3 / 4;
2418 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2419 HPEN old_pen = SelectObject(hdc, new_pen);
2421 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2422 centerx + rectsize + 2, centery + rectsize + 2);
2424 SelectObject(hdc, old_pen);
2425 DeleteObject(new_pen);
2427 /* draw +/- signs with current text color */
2428 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2429 old_pen = SelectObject(hdc, new_pen);
2431 if (height < 18 || width < 18)
2433 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2434 LineTo(hdc, centerx + plussize, centery);
2436 if (!(item->state & TVIS_EXPANDED))
2438 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2439 LineTo(hdc, centerx, centery + plussize);
2442 else
2444 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2445 centerx + plussize, centery + 2);
2447 if (!(item->state & TVIS_EXPANDED))
2449 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2450 centerx + 2, centery + plussize);
2451 SetPixel(hdc, centerx - 1, centery, clrBk);
2452 SetPixel(hdc, centerx + 1, centery, clrBk);
2456 SelectObject(hdc, old_pen);
2457 DeleteObject(new_pen);
2461 SelectObject(hdc, hbrOld);
2462 DeleteObject(hbr);
2465 static void
2466 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2468 INT cditem;
2469 HFONT hOldFont;
2470 COLORREF oldTextColor, oldTextBkColor;
2471 int centery;
2472 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2473 NMTVCUSTOMDRAW nmcdhdr;
2475 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2477 /* - If item is drop target or it is selected and window is in focus -
2478 * use blue background (COLOR_HIGHLIGHT).
2479 * - If item is selected, window is not in focus, but it has style
2480 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2481 * - Otherwise - use background color
2483 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2484 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2485 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2487 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2489 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2490 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2492 else
2494 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2495 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2498 else
2500 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2501 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2502 nmcdhdr.clrText = comctl32_color.clrHighlight;
2503 else
2504 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2507 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2509 /* The custom draw handler can query the text rectangle,
2510 * so get ready. */
2511 /* should already be known, set to 0 when changed */
2512 if (!wineItem->textWidth)
2513 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2515 cditem = 0;
2517 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2519 cditem = TREEVIEW_SendCustomDrawItemNotify
2520 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2521 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2523 if (cditem & CDRF_SKIPDEFAULT)
2525 SelectObject(hdc, hOldFont);
2526 return;
2530 if (cditem & CDRF_NEWFONT)
2531 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2533 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2535 /* Set colors. Custom draw handler can change these so we do this after it. */
2536 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2537 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2539 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2542 * Display the images associated with this item
2545 INT imageIndex;
2547 /* State images are displayed to the left of the Normal image
2548 * image number is in state; zero should be `display no image'.
2550 imageIndex = STATEIMAGEINDEX(wineItem->state);
2552 if (infoPtr->himlState && imageIndex)
2554 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2555 wineItem->stateOffset,
2556 centery - infoPtr->stateImageHeight / 2,
2557 ILD_NORMAL);
2560 /* Now, draw the normal image; can be either selected or
2561 * non-selected image.
2564 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2566 /* The item is currently selected */
2567 imageIndex = wineItem->iSelectedImage;
2569 else
2571 /* The item is not selected */
2572 imageIndex = wineItem->iImage;
2575 if (infoPtr->himlNormal)
2577 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2579 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2580 wineItem->imageOffset,
2581 centery - infoPtr->normalImageHeight / 2,
2582 ILD_NORMAL | ovlIdx);
2588 * Display the text associated with this item
2591 /* Don't paint item's text if it's being edited */
2592 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2594 if (wineItem->pszText)
2596 RECT rcText;
2598 rcText.top = wineItem->rect.top;
2599 rcText.bottom = wineItem->rect.bottom;
2600 rcText.left = wineItem->textOffset;
2601 rcText.right = rcText.left + wineItem->textWidth + 4;
2603 TRACE("drawing text %s at (%s)\n",
2604 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2606 /* Draw it */
2607 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2608 ETO_CLIPPED | ETO_OPAQUE,
2609 &rcText,
2610 wineItem->pszText,
2611 lstrlenW(wineItem->pszText),
2612 NULL);
2614 /* Draw the box around the selected item */
2615 if ((wineItem == infoPtr->selectedItem) && inFocus)
2617 DrawFocusRect(hdc,&rcText);
2623 /* Draw insertion mark if necessary */
2625 if (infoPtr->insertMarkItem)
2626 TRACE("item:%d,mark:%p\n",
2627 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2628 infoPtr->insertMarkItem);
2630 if (wineItem == infoPtr->insertMarkItem)
2632 HPEN hNewPen, hOldPen;
2633 int offset;
2634 int left, right;
2636 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2637 hOldPen = SelectObject(hdc, hNewPen);
2639 if (infoPtr->insertBeforeorAfter)
2640 offset = wineItem->rect.bottom - 1;
2641 else
2642 offset = wineItem->rect.top + 1;
2644 left = wineItem->textOffset - 2;
2645 right = wineItem->textOffset + wineItem->textWidth + 2;
2647 MoveToEx(hdc, left, offset - 3, NULL);
2648 LineTo(hdc, left, offset + 4);
2650 MoveToEx(hdc, left, offset, NULL);
2651 LineTo(hdc, right + 1, offset);
2653 MoveToEx(hdc, right, offset + 3, NULL);
2654 LineTo(hdc, right, offset - 4);
2656 SelectObject(hdc, hOldPen);
2657 DeleteObject(hNewPen);
2660 if (cditem & CDRF_NOTIFYPOSTPAINT)
2662 cditem = TREEVIEW_SendCustomDrawItemNotify
2663 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2664 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2667 /* Restore the hdc state */
2668 SetTextColor(hdc, oldTextColor);
2669 SetBkColor(hdc, oldTextBkColor);
2670 SelectObject(hdc, hOldFont);
2673 /* Computes treeHeight and treeWidth and updates the scroll bars.
2675 static void
2676 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2678 TREEVIEW_ITEM *wineItem;
2679 HWND hwnd = infoPtr->hwnd;
2680 BOOL vert = FALSE;
2681 BOOL horz = FALSE;
2682 SCROLLINFO si;
2683 LONG scrollX = infoPtr->scrollX;
2685 infoPtr->treeWidth = 0;
2686 infoPtr->treeHeight = 0;
2688 /* We iterate through all visible items in order to get the tree height
2689 * and width */
2690 wineItem = infoPtr->root->firstChild;
2692 while (wineItem != NULL)
2694 if (ISVISIBLE(wineItem))
2696 /* actually we draw text at textOffset + 2 */
2697 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2698 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2700 /* This is scroll-adjusted, but we fix this below. */
2701 infoPtr->treeHeight = wineItem->rect.bottom;
2704 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2707 /* Fix the scroll adjusted treeHeight and treeWidth. */
2708 if (infoPtr->root->firstChild)
2709 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2711 infoPtr->treeWidth += infoPtr->scrollX;
2713 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2715 /* Adding one scroll bar may take up enough space that it forces us
2716 * to add the other as well. */
2717 if (infoPtr->treeHeight > infoPtr->clientHeight)
2719 vert = TRUE;
2721 if (infoPtr->treeWidth
2722 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2723 horz = TRUE;
2725 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2726 horz = TRUE;
2728 if (!vert && horz && infoPtr->treeHeight
2729 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2730 vert = TRUE;
2732 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2734 si.cbSize = sizeof(SCROLLINFO);
2735 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2736 si.nMin = 0;
2738 if (vert)
2740 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2741 if ( si.nPage && NULL != infoPtr->firstVisible)
2743 si.nPos = infoPtr->firstVisible->visibleOrder;
2744 si.nMax = infoPtr->maxVisibleOrder - 1;
2746 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2748 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2749 ShowScrollBar(hwnd, SB_VERT, TRUE);
2750 infoPtr->uInternalStatus |= TV_VSCROLL;
2752 else
2754 if (infoPtr->uInternalStatus & TV_VSCROLL)
2755 ShowScrollBar(hwnd, SB_VERT, FALSE);
2756 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2759 else
2761 if (infoPtr->uInternalStatus & TV_VSCROLL)
2762 ShowScrollBar(hwnd, SB_VERT, FALSE);
2763 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2766 if (horz)
2768 si.nPage = infoPtr->clientWidth;
2769 si.nPos = infoPtr->scrollX;
2770 si.nMax = infoPtr->treeWidth - 1;
2772 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2774 si.nPos = si.nMax - max( si.nPage-1, 0 );
2775 scrollX = si.nPos;
2778 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2779 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2780 infoPtr->uInternalStatus |= TV_HSCROLL;
2782 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2783 TREEVIEW_HScroll(infoPtr,
2784 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2786 else
2788 if (infoPtr->uInternalStatus & TV_HSCROLL)
2789 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2790 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2792 scrollX = 0;
2793 if (infoPtr->scrollX != 0)
2795 TREEVIEW_HScroll(infoPtr,
2796 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2800 if (!horz)
2801 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2804 static void
2805 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2807 HBRUSH hBrush;
2808 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2810 hBrush = CreateSolidBrush(clrBk);
2811 FillRect(hdc, rc, hBrush);
2812 DeleteObject(hBrush);
2815 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2816 static LRESULT
2817 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2819 RECT rect;
2821 TRACE("%p\n", infoPtr);
2823 GetClientRect(infoPtr->hwnd, &rect);
2824 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2826 return 1;
2829 static void
2830 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2832 HWND hwnd = infoPtr->hwnd;
2833 RECT rect = *rc;
2834 TREEVIEW_ITEM *wineItem;
2836 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2838 TRACE("empty window\n");
2839 return;
2842 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2843 hdc, rect);
2845 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2847 ReleaseDC(hwnd, hdc);
2848 return;
2851 for (wineItem = infoPtr->root->firstChild;
2852 wineItem != NULL;
2853 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2855 if (ISVISIBLE(wineItem))
2857 /* Avoid unneeded calculations */
2858 if (wineItem->rect.top > rect.bottom)
2859 break;
2860 if (wineItem->rect.bottom < rect.top)
2861 continue;
2863 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2867 TREEVIEW_UpdateScrollBars(infoPtr);
2869 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2870 infoPtr->cdmode =
2871 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2874 static inline void
2875 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2877 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2880 static void
2881 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2883 if (item)
2884 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2885 else
2886 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2889 static LRESULT
2890 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2892 HDC hdc;
2893 PAINTSTRUCT ps;
2894 RECT rc;
2896 TRACE("\n");
2898 if (hdc_ref)
2900 hdc = hdc_ref;
2901 GetClientRect(infoPtr->hwnd, &rc);
2903 else
2905 hdc = BeginPaint(infoPtr->hwnd, &ps);
2906 rc = ps.rcPaint;
2907 if(ps.fErase)
2908 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2911 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2912 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2914 if (!hdc_ref)
2915 EndPaint(infoPtr->hwnd, &ps);
2917 return 0;
2920 static LRESULT
2921 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2923 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2925 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2926 return 0;
2928 if (options & PRF_ERASEBKGND)
2929 TREEVIEW_EraseBackground(infoPtr, hdc);
2931 if (options & PRF_CLIENT)
2933 RECT rc;
2934 GetClientRect(infoPtr->hwnd, &rc);
2935 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2938 return 0;
2941 /* Sorting **************************************************************/
2943 /***************************************************************************
2944 * Forward the DPA local callback to the treeview owner callback
2946 static INT WINAPI
2947 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2948 const TVSORTCB *pCallBackSort)
2950 /* Forward the call to the client-defined callback */
2951 return pCallBackSort->lpfnCompare(first->lParam,
2952 second->lParam,
2953 pCallBackSort->lParam);
2956 /***************************************************************************
2957 * Treeview native sort routine: sort on item text.
2959 static INT WINAPI
2960 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2961 const TREEVIEW_INFO *infoPtr)
2963 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2964 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2966 if(first->pszText && second->pszText)
2967 return lstrcmpiW(first->pszText, second->pszText);
2968 else if(first->pszText)
2969 return -1;
2970 else if(second->pszText)
2971 return 1;
2972 else
2973 return 0;
2976 /* Returns the number of physical children belonging to item. */
2977 static INT
2978 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2980 INT cChildren = 0;
2981 HTREEITEM hti;
2983 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2984 cChildren++;
2986 return cChildren;
2989 /* Returns a DPA containing a pointer to each physical child of item in
2990 * sibling order. If item has no children, an empty DPA is returned. */
2991 static HDPA
2992 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
2994 HTREEITEM child = item->firstChild;
2996 HDPA list = DPA_Create(8);
2997 if (list == 0) return NULL;
2999 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3001 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3003 DPA_Destroy(list);
3004 return NULL;
3008 return list;
3011 /***************************************************************************
3012 * Setup the treeview structure with regards of the sort method
3013 * and sort the children of the TV item specified in lParam
3014 * fRecurse: currently unused. Should be zero.
3015 * parent: if pSort!=NULL, should equal pSort->hParent.
3016 * otherwise, item which child items are to be sorted.
3017 * pSort: sort method info. if NULL, sort on item text.
3018 * if non-NULL, sort on item's lParam content, and let the
3019 * application decide what that means. See also TVM_SORTCHILDRENCB.
3022 static LRESULT
3023 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3024 LPTVSORTCB pSort)
3026 INT cChildren;
3027 PFNDPACOMPARE pfnCompare;
3028 LPARAM lpCompare;
3030 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3031 if (parent == TVI_ROOT || parent == NULL)
3032 parent = infoPtr->root;
3034 /* Check for a valid handle to the parent item */
3035 if (!TREEVIEW_ValidItem(infoPtr, parent))
3037 ERR("invalid item hParent=%p\n", parent);
3038 return FALSE;
3041 if (pSort)
3043 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3044 lpCompare = (LPARAM)pSort;
3046 else
3048 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3049 lpCompare = (LPARAM)infoPtr;
3052 cChildren = TREEVIEW_CountChildren(parent);
3054 /* Make sure there is something to sort */
3055 if (cChildren > 1)
3057 /* TREEVIEW_ITEM rechaining */
3058 INT count = 0;
3059 HTREEITEM item = 0;
3060 HTREEITEM nextItem = 0;
3061 HTREEITEM prevItem = 0;
3063 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3065 if (sortList == NULL)
3066 return FALSE;
3068 /* let DPA sort the list */
3069 DPA_Sort(sortList, pfnCompare, lpCompare);
3071 /* The order of DPA entries has been changed, so fixup the
3072 * nextSibling and prevSibling pointers. */
3074 item = DPA_GetPtr(sortList, count++);
3075 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3077 /* link the two current item together */
3078 item->nextSibling = nextItem;
3079 nextItem->prevSibling = item;
3081 if (prevItem == NULL)
3083 /* this is the first item, update the parent */
3084 parent->firstChild = item;
3085 item->prevSibling = NULL;
3087 else
3089 /* fix the back chaining */
3090 item->prevSibling = prevItem;
3093 /* get ready for the next one */
3094 prevItem = item;
3095 item = nextItem;
3098 /* the last item is pointed to by item and never has a sibling */
3099 item->nextSibling = NULL;
3100 parent->lastChild = item;
3102 DPA_Destroy(sortList);
3104 TREEVIEW_VerifyTree(infoPtr);
3106 if (parent->state & TVIS_EXPANDED)
3108 int visOrder = infoPtr->firstVisible->visibleOrder;
3110 if (parent == infoPtr->root)
3111 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3112 else
3113 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3115 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3117 TREEVIEW_ITEM *item;
3119 for (item = infoPtr->root->firstChild; item != NULL;
3120 item = TREEVIEW_GetNextListItem(infoPtr, item))
3122 if (item->visibleOrder == visOrder)
3123 break;
3126 if (!item) item = parent->firstChild;
3127 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3130 TREEVIEW_Invalidate(infoPtr, NULL);
3133 return TRUE;
3135 return FALSE;
3139 /***************************************************************************
3140 * Setup the treeview structure with regards of the sort method
3141 * and sort the children of the TV item specified in lParam
3143 static LRESULT
3144 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3146 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3150 /***************************************************************************
3151 * Sort the children of the TV item specified in lParam.
3153 static LRESULT
3154 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3156 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3160 /* Expansion/Collapse ***************************************************/
3162 static BOOL
3163 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3164 UINT action)
3166 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3167 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3168 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3169 0, wineItem);
3172 static VOID
3173 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3174 UINT action)
3176 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3177 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3178 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3179 0, wineItem);
3183 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3184 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3185 static BOOL
3186 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3187 BOOL bRemoveChildren, BOOL bUser)
3189 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3190 BOOL bSetSelection, bSetFirstVisible;
3191 RECT scrollRect;
3192 LONG scrollDist = 0;
3193 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3195 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3197 if (!(wineItem->state & TVIS_EXPANDED))
3198 return FALSE;
3200 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3201 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3203 if (wineItem->firstChild == NULL)
3204 return FALSE;
3206 wineItem->state &= ~TVIS_EXPANDED;
3208 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3209 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3211 bSetSelection = (infoPtr->selectedItem != NULL
3212 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3214 bSetFirstVisible = (infoPtr->firstVisible != NULL
3215 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3217 tmpItem = wineItem;
3218 while (tmpItem)
3220 if (tmpItem->nextSibling)
3222 nextItem = tmpItem->nextSibling;
3223 break;
3225 tmpItem = tmpItem->parent;
3228 if (nextItem)
3229 scrollDist = nextItem->rect.top;
3231 if (bRemoveChildren)
3233 INT old_cChildren = wineItem->cChildren;
3234 TRACE("TVE_COLLAPSERESET\n");
3235 wineItem->state &= ~TVIS_EXPANDEDONCE;
3236 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3237 wineItem->cChildren = old_cChildren;
3240 if (wineItem->firstChild)
3242 TREEVIEW_ITEM *item, *sibling;
3244 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3246 for (item = wineItem->firstChild; item != sibling;
3247 item = TREEVIEW_GetNextListItem(infoPtr, item))
3249 item->visibleOrder = -1;
3253 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3255 if (nextItem)
3256 scrollDist = -(scrollDist - nextItem->rect.top);
3258 if (bSetSelection)
3260 /* Don't call DoSelectItem, it sends notifications. */
3261 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3262 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3263 wineItem->state |= TVIS_SELECTED;
3264 infoPtr->selectedItem = wineItem;
3267 TREEVIEW_UpdateScrollBars(infoPtr);
3269 scrollRect.left = 0;
3270 scrollRect.right = infoPtr->clientWidth;
3271 scrollRect.bottom = infoPtr->clientHeight;
3273 if (nextItem)
3275 scrollRect.top = nextItem->rect.top;
3277 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3278 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3279 TREEVIEW_Invalidate(infoPtr, wineItem);
3280 } else {
3281 scrollRect.top = wineItem->rect.top;
3282 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3285 TREEVIEW_SetFirstVisible(infoPtr,
3286 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3287 TRUE);
3289 return TRUE;
3292 static BOOL
3293 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3294 BOOL bExpandPartial, BOOL bUser)
3296 LONG scrollDist;
3297 LONG orgNextTop = 0;
3298 RECT scrollRect;
3299 TREEVIEW_ITEM *nextItem, *tmpItem;
3301 TRACE("\n");
3303 if (wineItem->state & TVIS_EXPANDED)
3304 return TRUE;
3306 tmpItem = wineItem; nextItem = NULL;
3307 while (tmpItem)
3309 if (tmpItem->nextSibling)
3311 nextItem = tmpItem->nextSibling;
3312 break;
3314 tmpItem = tmpItem->parent;
3317 if (nextItem)
3318 orgNextTop = nextItem->rect.top;
3320 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3322 if (bUser || ((wineItem->cChildren != 0) &&
3323 !(wineItem->state & TVIS_EXPANDEDONCE)))
3325 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3327 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3328 return FALSE;
3331 if (!wineItem->firstChild)
3332 return FALSE;
3334 wineItem->state |= TVIS_EXPANDED;
3335 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3336 wineItem->state |= TVIS_EXPANDEDONCE;
3338 else
3340 if (!wineItem->firstChild)
3341 return FALSE;
3343 /* this item has already been expanded */
3344 wineItem->state |= TVIS_EXPANDED;
3347 if (bExpandPartial)
3348 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3350 if (ISVISIBLE(wineItem))
3352 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3353 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3354 TREEVIEW_UpdateScrollBars(infoPtr);
3356 scrollRect.left = 0;
3357 scrollRect.bottom = infoPtr->treeHeight;
3358 scrollRect.right = infoPtr->clientWidth;
3359 if (nextItem)
3361 scrollDist = nextItem->rect.top - orgNextTop;
3362 scrollRect.top = orgNextTop;
3364 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3365 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3366 TREEVIEW_Invalidate (infoPtr, wineItem);
3367 } else {
3368 scrollRect.top = wineItem->rect.top;
3369 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3372 /* Scroll up so that as many children as possible are visible.
3373 * This fails when expanding causes an HScroll bar to appear, but we
3374 * don't know that yet, so the last item is obscured. */
3375 if (wineItem->firstChild != NULL)
3377 int nChildren = wineItem->lastChild->visibleOrder
3378 - wineItem->firstChild->visibleOrder + 1;
3380 int visible_pos = wineItem->visibleOrder
3381 - infoPtr->firstVisible->visibleOrder;
3383 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3385 if (visible_pos > 0 && nChildren > rows_below)
3387 int scroll = nChildren - rows_below;
3389 if (scroll > visible_pos)
3390 scroll = visible_pos;
3392 if (scroll > 0)
3394 TREEVIEW_ITEM *newFirstVisible
3395 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3396 scroll);
3399 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3405 return TRUE;
3408 static BOOL
3409 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3411 TRACE("\n");
3413 if (wineItem->state & TVIS_EXPANDED)
3414 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3415 else
3416 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3419 static VOID
3420 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3422 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3424 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3426 if (TREEVIEW_HasChildren(infoPtr, item))
3427 TREEVIEW_ExpandAll(infoPtr, item);
3431 /* Note:If the specified item is the child of a collapsed parent item,
3432 the parent's list of child items is (recursively) expanded to reveal the
3433 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3434 know if it also applies here.
3437 static LRESULT
3438 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3440 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3441 return 0;
3443 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3444 TREEVIEW_ItemName(wineItem), flag,
3445 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3447 switch (flag & TVE_TOGGLE)
3449 case TVE_COLLAPSE:
3450 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3451 FALSE);
3453 case TVE_EXPAND:
3454 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3455 FALSE);
3457 case TVE_TOGGLE:
3458 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3460 default:
3461 return 0;
3464 #if 0
3465 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3466 #endif
3469 /* Hit-Testing **********************************************************/
3471 static TREEVIEW_ITEM *
3472 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3474 TREEVIEW_ITEM *wineItem;
3475 LONG row;
3477 if (!infoPtr->firstVisible)
3478 return NULL;
3480 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3482 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3483 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3485 if (row >= wineItem->visibleOrder
3486 && row < wineItem->visibleOrder + wineItem->iIntegral)
3487 break;
3490 return wineItem;
3493 static LRESULT
3494 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3496 TREEVIEW_ITEM *wineItem;
3497 RECT rect;
3498 UINT status;
3499 LONG x, y;
3501 lpht->hItem = 0;
3502 GetClientRect(infoPtr->hwnd, &rect);
3503 status = 0;
3504 x = lpht->pt.x;
3505 y = lpht->pt.y;
3507 if (x < rect.left)
3509 status |= TVHT_TOLEFT;
3511 else if (x > rect.right)
3513 status |= TVHT_TORIGHT;
3516 if (y < rect.top)
3518 status |= TVHT_ABOVE;
3520 else if (y > rect.bottom)
3522 status |= TVHT_BELOW;
3525 if (status)
3527 lpht->flags = status;
3528 return 0;
3531 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3532 if (!wineItem)
3534 lpht->flags = TVHT_NOWHERE;
3535 return 0;
3538 if (x >= wineItem->textOffset + wineItem->textWidth)
3540 lpht->flags = TVHT_ONITEMRIGHT;
3542 else if (x >= wineItem->textOffset)
3544 lpht->flags = TVHT_ONITEMLABEL;
3546 else if (x >= wineItem->imageOffset)
3548 lpht->flags = TVHT_ONITEMICON;
3550 else if (x >= wineItem->stateOffset)
3552 lpht->flags = TVHT_ONITEMSTATEICON;
3554 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3556 lpht->flags = TVHT_ONITEMBUTTON;
3558 else
3560 lpht->flags = TVHT_ONITEMINDENT;
3563 lpht->hItem = wineItem;
3564 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3566 return (LRESULT)wineItem;
3569 /* Item Label Editing ***************************************************/
3571 static LRESULT
3572 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3574 return (LRESULT)infoPtr->hwndEdit;
3577 static LRESULT CALLBACK
3578 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3580 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3581 BOOL bCancel = FALSE;
3582 LRESULT rc;
3584 switch (uMsg)
3586 case WM_PAINT:
3587 TRACE("WM_PAINT start\n");
3588 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3589 lParam);
3590 TRACE("WM_PAINT done\n");
3591 return rc;
3593 case WM_KILLFOCUS:
3594 if (infoPtr->bIgnoreEditKillFocus)
3595 return TRUE;
3596 break;
3598 case WM_DESTROY:
3600 WNDPROC editProc = infoPtr->wpEditOrig;
3601 infoPtr->wpEditOrig = 0;
3602 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3603 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3606 case WM_GETDLGCODE:
3607 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3609 case WM_KEYDOWN:
3610 if (wParam == VK_ESCAPE)
3612 bCancel = TRUE;
3613 break;
3615 else if (wParam == VK_RETURN)
3617 break;
3620 /* fall through */
3621 default:
3622 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3625 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3626 /* eg. Using a messagebox */
3628 infoPtr->bIgnoreEditKillFocus = TRUE;
3629 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3630 infoPtr->bIgnoreEditKillFocus = FALSE;
3632 return 0;
3636 /* should handle edit control messages here */
3638 static LRESULT
3639 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3641 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3643 switch (HIWORD(wParam))
3645 case EN_UPDATE:
3648 * Adjust the edit window size
3650 WCHAR buffer[1024];
3651 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3652 HDC hdc = GetDC(infoPtr->hwndEdit);
3653 SIZE sz;
3654 HFONT hFont, hOldFont = 0;
3656 TRACE("edit=%p\n", infoPtr->hwndEdit);
3658 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3660 infoPtr->bLabelChanged = TRUE;
3662 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3664 /* Select font to get the right dimension of the string */
3665 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3667 if (hFont != 0)
3669 hOldFont = SelectObject(hdc, hFont);
3672 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3674 TEXTMETRICW textMetric;
3676 /* Add Extra spacing for the next character */
3677 GetTextMetricsW(hdc, &textMetric);
3678 sz.cx += (textMetric.tmMaxCharWidth * 2);
3680 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3681 sz.cx = min(sz.cx,
3682 infoPtr->clientWidth - editItem->textOffset + 2);
3684 SetWindowPos(infoPtr->hwndEdit,
3685 HWND_TOP,
3688 sz.cx,
3689 editItem->rect.bottom - editItem->rect.top + 3,
3690 SWP_NOMOVE | SWP_DRAWFRAME);
3693 if (hFont != 0)
3695 SelectObject(hdc, hOldFont);
3698 ReleaseDC(infoPtr->hwnd, hdc);
3699 break;
3701 case EN_KILLFOCUS:
3702 /* apparently we should respect passed handle value */
3703 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3705 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3706 break;
3708 default:
3709 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3712 return 0;
3715 static HWND
3716 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3718 HWND hwnd = infoPtr->hwnd;
3719 HWND hwndEdit;
3720 SIZE sz;
3721 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3722 HDC hdc;
3723 HFONT hOldFont=0;
3724 TEXTMETRICW textMetric;
3726 TRACE("%p %p\n", hwnd, hItem);
3727 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3728 return NULL;
3730 if (infoPtr->hwndEdit)
3731 return infoPtr->hwndEdit;
3733 infoPtr->bLabelChanged = FALSE;
3735 /* make edit item visible */
3736 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3738 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3740 hdc = GetDC(hwnd);
3741 /* Select the font to get appropriate metric dimensions */
3742 if (infoPtr->hFont != 0)
3744 hOldFont = SelectObject(hdc, infoPtr->hFont);
3747 /* Get string length in pixels */
3748 if (hItem->pszText)
3749 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3750 &sz);
3751 else
3752 GetTextExtentPoint32A(hdc, "", 0, &sz);
3754 /* Add Extra spacing for the next character */
3755 GetTextMetricsW(hdc, &textMetric);
3756 sz.cx += (textMetric.tmMaxCharWidth * 2);
3758 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3759 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3761 if (infoPtr->hFont != 0)
3763 SelectObject(hdc, hOldFont);
3766 ReleaseDC(hwnd, hdc);
3768 infoPtr->editItem = hItem;
3770 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3771 WC_EDITW,
3773 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3774 WS_CLIPSIBLINGS | ES_WANTRETURN |
3775 ES_LEFT, hItem->textOffset - 2,
3776 hItem->rect.top - 1, sz.cx + 3,
3777 hItem->rect.bottom -
3778 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3779 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3781 infoPtr->hwndEdit = hwndEdit;
3783 /* Get a 2D border. */
3784 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3785 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3786 SetWindowLongW(hwndEdit, GWL_STYLE,
3787 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3789 SendMessageW(hwndEdit, WM_SETFONT,
3790 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3792 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3793 (DWORD_PTR)
3794 TREEVIEW_Edit_SubclassProc);
3796 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3798 DestroyWindow(hwndEdit);
3799 infoPtr->hwndEdit = 0;
3800 infoPtr->editItem = NULL;
3801 return NULL;
3804 if (hItem->pszText)
3805 SetWindowTextW(hwndEdit, hItem->pszText);
3807 SetFocus(hwndEdit);
3808 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3809 ShowWindow(hwndEdit, SW_SHOW);
3811 return hwndEdit;
3815 static LRESULT
3816 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3818 HWND hwnd = infoPtr->hwnd;
3819 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3820 NMTVDISPINFOW tvdi;
3821 BOOL bCommit;
3822 WCHAR tmpText[1024] = { '\0' };
3823 WCHAR *newText = tmpText;
3824 int iLength = 0;
3826 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3828 tvdi.hdr.hwndFrom = hwnd;
3829 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3830 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3831 tvdi.item.mask = 0;
3832 tvdi.item.hItem = editedItem;
3833 tvdi.item.state = editedItem->state;
3834 tvdi.item.lParam = editedItem->lParam;
3836 if (!bCancel)
3838 if (!infoPtr->bNtfUnicode)
3839 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3840 else
3841 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3843 if (iLength >= 1023)
3845 ERR("Insufficient space to retrieve new item label\n");
3848 tvdi.item.mask = TVIF_TEXT;
3849 tvdi.item.pszText = tmpText;
3850 tvdi.item.cchTextMax = iLength + 1;
3852 else
3854 tvdi.item.pszText = NULL;
3855 tvdi.item.cchTextMax = 0;
3858 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3860 if (!bCancel && bCommit) /* Apply the changes */
3862 if (!infoPtr->bNtfUnicode)
3864 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3865 newText = Alloc(len * sizeof(WCHAR));
3866 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3867 iLength = len - 1;
3870 if (strcmpW(newText, editedItem->pszText) != 0)
3872 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3873 if (ptr == NULL)
3875 ERR("OutOfMemory, cannot allocate space for label\n");
3876 if(newText != tmpText) Free(newText);
3877 DestroyWindow(infoPtr->hwndEdit);
3878 infoPtr->hwndEdit = 0;
3879 infoPtr->editItem = NULL;
3880 return FALSE;
3882 else
3884 editedItem->pszText = ptr;
3885 editedItem->cchTextMax = iLength + 1;
3886 strcpyW(editedItem->pszText, newText);
3887 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3890 if(newText != tmpText) Free(newText);
3893 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3894 DestroyWindow(infoPtr->hwndEdit);
3895 infoPtr->hwndEdit = 0;
3896 infoPtr->editItem = NULL;
3897 return TRUE;
3900 static LRESULT
3901 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3903 if (wParam != TV_EDIT_TIMER)
3905 ERR("got unknown timer\n");
3906 return 1;
3909 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3910 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3912 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3914 return 0;
3918 /* Mouse Tracking/Drag **************************************************/
3920 /***************************************************************************
3921 * This is quite unusual piece of code, but that's how it's implemented in
3922 * Windows.
3924 static LRESULT
3925 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3927 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3928 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3929 RECT r;
3930 MSG msg;
3932 r.top = pt.y - cyDrag;
3933 r.left = pt.x - cxDrag;
3934 r.bottom = pt.y + cyDrag;
3935 r.right = pt.x + cxDrag;
3937 SetCapture(infoPtr->hwnd);
3939 while (1)
3941 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3943 if (msg.message == WM_MOUSEMOVE)
3945 pt.x = (short)LOWORD(msg.lParam);
3946 pt.y = (short)HIWORD(msg.lParam);
3947 if (PtInRect(&r, pt))
3948 continue;
3949 else
3951 ReleaseCapture();
3952 return 1;
3955 else if (msg.message >= WM_LBUTTONDOWN &&
3956 msg.message <= WM_RBUTTONDBLCLK)
3958 if (msg.message == WM_RBUTTONUP)
3959 TREEVIEW_RButtonUp(infoPtr, &pt);
3960 break;
3963 DispatchMessageW(&msg);
3966 if (GetCapture() != infoPtr->hwnd)
3967 return 0;
3970 ReleaseCapture();
3971 return 0;
3975 static LRESULT
3976 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3978 TREEVIEW_ITEM *wineItem;
3979 TVHITTESTINFO hit;
3981 TRACE("\n");
3982 SetFocus(infoPtr->hwnd);
3984 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3986 /* If there is pending 'edit label' event - kill it now */
3987 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3990 hit.pt.x = (short)LOWORD(lParam);
3991 hit.pt.y = (short)HIWORD(lParam);
3993 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3994 if (!wineItem)
3995 return 0;
3996 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3998 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3999 { /* FIXME! */
4000 switch (hit.flags)
4002 case TVHT_ONITEMRIGHT:
4003 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4004 break;
4006 case TVHT_ONITEMINDENT:
4007 if (!(infoPtr->dwStyle & TVS_HASLINES))
4009 break;
4011 else
4013 int level = hit.pt.x / infoPtr->uIndent;
4014 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4016 while (wineItem->iLevel > level)
4018 wineItem = wineItem->parent;
4021 /* fall through */
4024 case TVHT_ONITEMLABEL:
4025 case TVHT_ONITEMICON:
4026 case TVHT_ONITEMBUTTON:
4027 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4028 break;
4030 case TVHT_ONITEMSTATEICON:
4031 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4032 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4033 else
4034 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4035 break;
4038 return TRUE;
4042 static LRESULT
4043 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4045 HWND hwnd = infoPtr->hwnd;
4046 TVHITTESTINFO ht;
4047 BOOL bTrack, bDoLabelEdit;
4049 /* If Edit control is active - kill it and return.
4050 * The best way to do it is to set focus to itself.
4051 * Edit control subclassed procedure will automatically call
4052 * EndEditLabelNow.
4054 if (infoPtr->hwndEdit)
4056 SetFocus(hwnd);
4057 return 0;
4060 ht.pt.x = (short)LOWORD(lParam);
4061 ht.pt.y = (short)HIWORD(lParam);
4063 TREEVIEW_HitTest(infoPtr, &ht);
4064 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4066 /* update focusedItem and redraw both items */
4067 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4069 infoPtr->focusedItem = ht.hItem;
4070 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4071 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4074 bTrack = (ht.flags & TVHT_ONITEM)
4075 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4078 * If the style allows editing and the node is already selected
4079 * and the click occurred on the item label...
4081 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4082 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4084 /* Send NM_CLICK right away */
4085 if (!bTrack)
4086 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4087 goto setfocus;
4089 if (ht.flags & TVHT_ONITEMBUTTON)
4091 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4092 goto setfocus;
4094 else if (bTrack)
4095 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4096 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4098 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4099 infoPtr->dropItem = ht.hItem;
4101 /* clean up focusedItem as we dragged and won't select this item */
4102 if(infoPtr->focusedItem)
4104 /* refresh the item that was focused */
4105 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4106 infoPtr->focusedItem = NULL;
4108 /* refresh the selected item to return the filled background */
4109 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4112 return 0;
4116 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4117 goto setfocus;
4119 if (bDoLabelEdit)
4121 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4122 KillTimer(hwnd, TV_EDIT_TIMER);
4124 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4125 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4127 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4130 * if we are TVS_SINGLEEXPAND then we want this single click to
4131 * do a bunch of things.
4133 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4134 (infoPtr->hwndEdit == 0))
4136 TREEVIEW_ITEM *SelItem;
4139 * Send the notification
4141 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4144 * Close the previous selection all the way to the root
4145 * as long as the new selection is not a child
4147 if((infoPtr->selectedItem)
4148 && (infoPtr->selectedItem != ht.hItem))
4150 BOOL closeit = TRUE;
4151 SelItem = ht.hItem;
4153 /* determine if the hitItem is a child of the currently selected item */
4154 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4156 closeit = (SelItem != infoPtr->selectedItem);
4157 SelItem = SelItem->parent;
4160 if(closeit)
4162 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4163 SelItem = infoPtr->selectedItem;
4165 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4167 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4168 SelItem = SelItem->parent;
4174 * Expand the current item
4176 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4179 /* Select the current item */
4180 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4182 else if (ht.flags & TVHT_ONITEMSTATEICON)
4184 /* TVS_CHECKBOXES requires us to toggle the current state */
4185 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4186 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4189 setfocus:
4190 SetFocus(hwnd);
4191 return 0;
4195 static LRESULT
4196 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4198 TVHITTESTINFO ht;
4200 if (infoPtr->hwndEdit)
4202 SetFocus(infoPtr->hwnd);
4203 return 0;
4206 ht.pt.x = (short)LOWORD(lParam);
4207 ht.pt.y = (short)HIWORD(lParam);
4209 TREEVIEW_HitTest(infoPtr, &ht);
4211 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4213 if (ht.hItem)
4215 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4216 infoPtr->dropItem = ht.hItem;
4219 else
4221 SetFocus(infoPtr->hwnd);
4222 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4225 return 0;
4228 static LRESULT
4229 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4231 TVHITTESTINFO ht;
4233 ht.pt = *pPt;
4235 TREEVIEW_HitTest(infoPtr, &ht);
4237 if (ht.hItem)
4239 /* Change to screen coordinate for WM_CONTEXTMENU */
4240 ClientToScreen(infoPtr->hwnd, &ht.pt);
4242 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4243 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4244 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4246 return 0;
4250 static LRESULT
4251 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4253 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4254 INT cx, cy;
4255 HDC hdc, htopdc;
4256 HWND hwtop;
4257 HBITMAP hbmp, hOldbmp;
4258 SIZE size;
4259 RECT rc;
4260 HFONT hOldFont;
4262 TRACE("\n");
4264 if (!(infoPtr->himlNormal))
4265 return 0;
4267 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4268 return 0;
4270 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4272 hwtop = GetDesktopWindow();
4273 htopdc = GetDC(hwtop);
4274 hdc = CreateCompatibleDC(htopdc);
4276 hOldFont = SelectObject(hdc, infoPtr->hFont);
4278 if (dragItem->pszText)
4279 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4280 &size);
4281 else
4282 GetTextExtentPoint32A(hdc, "", 0, &size);
4284 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4285 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4286 hOldbmp = SelectObject(hdc, hbmp);
4288 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4289 size.cx += cx;
4290 if (cy > size.cy)
4291 size.cy = cy;
4293 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4294 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4295 ILD_NORMAL);
4298 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4299 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4302 /* draw item text */
4304 SetRect(&rc, cx, 0, size.cx, size.cy);
4306 if (dragItem->pszText)
4307 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4308 DT_LEFT);
4310 SelectObject(hdc, hOldFont);
4311 SelectObject(hdc, hOldbmp);
4313 ImageList_Add(infoPtr->dragList, hbmp, 0);
4315 DeleteDC(hdc);
4316 DeleteObject(hbmp);
4317 ReleaseDC(hwtop, htopdc);
4319 return (LRESULT)infoPtr->dragList;
4322 /* Selection ************************************************************/
4324 static LRESULT
4325 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4326 INT cause)
4328 TREEVIEW_ITEM *prevSelect;
4330 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4332 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4333 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4334 newSelect ? newSelect->state : 0);
4336 /* reset and redraw focusedItem if focusedItem was set so we don't */
4337 /* have to worry about the previously focused item when we set a new one */
4338 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4339 infoPtr->focusedItem = NULL;
4341 switch (action)
4343 case TVGN_CARET:
4344 prevSelect = infoPtr->selectedItem;
4346 if (prevSelect == newSelect) {
4347 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4348 break;
4351 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4352 TVN_SELCHANGINGW,
4353 cause,
4354 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4355 prevSelect,
4356 newSelect))
4357 return FALSE;
4359 if (prevSelect)
4360 prevSelect->state &= ~TVIS_SELECTED;
4361 if (newSelect)
4362 newSelect->state |= TVIS_SELECTED;
4364 infoPtr->selectedItem = newSelect;
4366 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4368 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4369 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4371 TREEVIEW_SendTreeviewNotify(infoPtr,
4372 TVN_SELCHANGEDW,
4373 cause,
4374 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4375 prevSelect,
4376 newSelect);
4377 break;
4379 case TVGN_DROPHILITE:
4380 prevSelect = infoPtr->dropItem;
4382 if (prevSelect)
4383 prevSelect->state &= ~TVIS_DROPHILITED;
4385 infoPtr->dropItem = newSelect;
4387 if (newSelect)
4388 newSelect->state |= TVIS_DROPHILITED;
4390 TREEVIEW_Invalidate(infoPtr, prevSelect);
4391 TREEVIEW_Invalidate(infoPtr, newSelect);
4392 break;
4394 case TVGN_FIRSTVISIBLE:
4395 if (newSelect != NULL)
4397 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4398 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4399 TREEVIEW_Invalidate(infoPtr, NULL);
4401 break;
4404 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4405 return TRUE;
4408 /* FIXME: handle NM_KILLFOCUS etc */
4409 static LRESULT
4410 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4412 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4413 return FALSE;
4415 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4417 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4418 return FALSE;
4420 return TRUE;
4423 /*************************************************************************
4424 * TREEVIEW_ProcessLetterKeys
4426 * Processes keyboard messages generated by pressing the letter keys
4427 * on the keyboard.
4428 * What this does is perform a case insensitive search from the
4429 * current position with the following quirks:
4430 * - If two chars or more are pressed in quick succession we search
4431 * for the corresponding string (e.g. 'abc').
4432 * - If there is a delay we wipe away the current search string and
4433 * restart with just that char.
4434 * - If the user keeps pressing the same character, whether slowly or
4435 * fast, so that the search string is entirely composed of this
4436 * character ('aaaaa' for instance), then we search for first item
4437 * that starting with that character.
4438 * - If the user types the above character in quick succession, then
4439 * we must also search for the corresponding string ('aaaaa'), and
4440 * go to that string if there is a match.
4442 * RETURNS
4444 * Zero.
4446 * BUGS
4448 * - The current implementation has a list of characters it will
4449 * accept and it ignores everything else. In particular it will
4450 * ignore accentuated characters which seems to match what
4451 * Windows does. But I'm not sure it makes sense to follow
4452 * Windows there.
4453 * - We don't sound a beep when the search fails.
4454 * - The search should start from the focused item, not from the selected
4455 * item. One reason for this is to allow for multiple selections in trees.
4456 * But currently infoPtr->focusedItem does not seem very usable.
4458 * SEE ALSO
4460 * TREEVIEW_ProcessLetterKeys
4462 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4464 HTREEITEM nItem;
4465 HTREEITEM endidx,idx;
4466 TVITEMEXW item;
4467 WCHAR buffer[MAX_PATH];
4468 DWORD timestamp,elapsed;
4470 /* simple parameter checking */
4471 if (!charCode || !keyData) return 0;
4473 /* only allow the valid WM_CHARs through */
4474 if (!isalnum(charCode) &&
4475 charCode != '.' && charCode != '`' && charCode != '!' &&
4476 charCode != '@' && charCode != '#' && charCode != '$' &&
4477 charCode != '%' && charCode != '^' && charCode != '&' &&
4478 charCode != '*' && charCode != '(' && charCode != ')' &&
4479 charCode != '-' && charCode != '_' && charCode != '+' &&
4480 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4481 charCode != '}' && charCode != '[' && charCode != '{' &&
4482 charCode != '/' && charCode != '?' && charCode != '>' &&
4483 charCode != '<' && charCode != ',' && charCode != '~')
4484 return 0;
4486 /* compute how much time elapsed since last keypress */
4487 timestamp = GetTickCount();
4488 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4489 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4490 } else {
4491 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4494 /* update the search parameters */
4495 infoPtr->lastKeyPressTimestamp=timestamp;
4496 if (elapsed < KEY_DELAY) {
4497 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4498 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4500 if (infoPtr->charCode != charCode) {
4501 infoPtr->charCode=charCode=0;
4503 } else {
4504 infoPtr->charCode=charCode;
4505 infoPtr->szSearchParam[0]=charCode;
4506 infoPtr->nSearchParamLength=1;
4507 /* Redundant with the 1 char string */
4508 charCode=0;
4511 /* and search from the current position */
4512 nItem=NULL;
4513 if (infoPtr->selectedItem != NULL) {
4514 endidx=infoPtr->selectedItem;
4515 /* if looking for single character match,
4516 * then we must always move forward
4518 if (infoPtr->nSearchParamLength == 1)
4519 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4520 else
4521 idx=endidx;
4522 } else {
4523 endidx=NULL;
4524 idx=infoPtr->root->firstChild;
4526 do {
4527 /* At the end point, sort out wrapping */
4528 if (idx == NULL) {
4530 /* If endidx is null, stop at the last item (ie top to bottom) */
4531 if (endidx == NULL)
4532 break;
4534 /* Otherwise, start again at the very beginning */
4535 idx=infoPtr->root->firstChild;
4537 /* But if we are stopping on the first child, end now! */
4538 if (idx == endidx) break;
4541 /* get item */
4542 ZeroMemory(&item, sizeof(item));
4543 item.mask = TVIF_TEXT;
4544 item.hItem = idx;
4545 item.pszText = buffer;
4546 item.cchTextMax = sizeof(buffer);
4547 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4549 /* check for a match */
4550 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4551 nItem=idx;
4552 break;
4553 } else if ( (charCode != 0) && (nItem == NULL) &&
4554 (nItem != infoPtr->selectedItem) &&
4555 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4556 /* This would work but we must keep looking for a longer match */
4557 nItem=idx;
4559 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4560 } while (idx != endidx);
4562 if (nItem != NULL) {
4563 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4564 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4568 return 0;
4571 /* Scrolling ************************************************************/
4573 static LRESULT
4574 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4576 int viscount;
4577 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4578 HTREEITEM newFirstVisible = NULL;
4579 int visible_pos = -1;
4581 if (!TREEVIEW_ValidItem(infoPtr, item))
4582 return FALSE;
4584 if (!ISVISIBLE(item))
4586 /* Expand parents as necessary. */
4587 HTREEITEM parent;
4589 /* see if we are trying to ensure that root is visible */
4590 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4591 parent = item->parent;
4592 else
4593 parent = item; /* this item is the topmost item */
4595 while (parent != infoPtr->root)
4597 if (!(parent->state & TVIS_EXPANDED))
4598 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4600 parent = parent->parent;
4604 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4606 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4607 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4609 if (hasFirstVisible)
4610 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4612 if (visible_pos < 0)
4614 /* item is before the start of the list: put it at the top. */
4615 newFirstVisible = item;
4617 else if (visible_pos >= viscount
4618 /* Sometimes, before we are displayed, GVC is 0, causing us to
4619 * spuriously scroll up. */
4620 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4622 /* item is past the end of the list. */
4623 int scroll = visible_pos - viscount;
4625 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4626 scroll + 1);
4629 if (bHScroll)
4631 /* Scroll window so item's text is visible as much as possible */
4632 /* Calculation of amount of extra space is taken from EditLabel code */
4633 INT pos, x;
4634 TEXTMETRICW textMetric;
4635 HDC hdc = GetWindowDC(infoPtr->hwnd);
4637 x = item->textWidth;
4639 GetTextMetricsW(hdc, &textMetric);
4640 ReleaseDC(infoPtr->hwnd, hdc);
4642 x += (textMetric.tmMaxCharWidth * 2);
4643 x = max(x, textMetric.tmMaxCharWidth * 3);
4645 if (item->textOffset < 0)
4646 pos = item->textOffset;
4647 else if (item->textOffset + x > infoPtr->clientWidth)
4649 if (x > infoPtr->clientWidth)
4650 pos = item->textOffset;
4651 else
4652 pos = item->textOffset + x - infoPtr->clientWidth;
4654 else
4655 pos = 0;
4657 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4660 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4662 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4664 return TRUE;
4667 return FALSE;
4670 static VOID
4671 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4672 TREEVIEW_ITEM *newFirstVisible,
4673 BOOL bUpdateScrollPos)
4675 int gap_size;
4677 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4679 if (newFirstVisible != NULL)
4681 /* Prevent an empty gap from appearing at the bottom... */
4682 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4683 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4685 if (gap_size > 0)
4687 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4688 -gap_size);
4690 /* ... unless we just don't have enough items. */
4691 if (newFirstVisible == NULL)
4692 newFirstVisible = infoPtr->root->firstChild;
4696 if (infoPtr->firstVisible != newFirstVisible)
4698 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4700 infoPtr->firstVisible = newFirstVisible;
4701 TREEVIEW_Invalidate(infoPtr, NULL);
4703 else
4705 TREEVIEW_ITEM *item;
4706 int scroll = infoPtr->uItemHeight *
4707 (infoPtr->firstVisible->visibleOrder
4708 - newFirstVisible->visibleOrder);
4710 infoPtr->firstVisible = newFirstVisible;
4712 for (item = infoPtr->root->firstChild; item != NULL;
4713 item = TREEVIEW_GetNextListItem(infoPtr, item))
4715 item->rect.top += scroll;
4716 item->rect.bottom += scroll;
4719 if (bUpdateScrollPos)
4720 SetScrollPos(infoPtr->hwnd, SB_VERT,
4721 newFirstVisible->visibleOrder, TRUE);
4723 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4728 /************************************************************************
4729 * VScroll is always in units of visible items. i.e. we always have a
4730 * visible item aligned to the top of the control. (Unless we have no
4731 * items at all.)
4733 static LRESULT
4734 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4736 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4737 TREEVIEW_ITEM *newFirstVisible = NULL;
4739 int nScrollCode = LOWORD(wParam);
4741 TRACE("wp %lx\n", wParam);
4743 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4744 return 0;
4746 if (!oldFirstVisible)
4748 assert(infoPtr->root->firstChild == NULL);
4749 return 0;
4752 switch (nScrollCode)
4754 case SB_TOP:
4755 newFirstVisible = infoPtr->root->firstChild;
4756 break;
4758 case SB_BOTTOM:
4759 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4760 break;
4762 case SB_LINEUP:
4763 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4764 break;
4766 case SB_LINEDOWN:
4767 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4768 break;
4770 case SB_PAGEUP:
4771 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4772 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4773 break;
4775 case SB_PAGEDOWN:
4776 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4777 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4778 break;
4780 case SB_THUMBTRACK:
4781 case SB_THUMBPOSITION:
4782 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4783 infoPtr->root->firstChild,
4784 (LONG)(SHORT)HIWORD(wParam));
4785 break;
4787 case SB_ENDSCROLL:
4788 return 0;
4791 if (newFirstVisible != NULL)
4793 if (newFirstVisible != oldFirstVisible)
4794 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4795 nScrollCode != SB_THUMBTRACK);
4796 else if (nScrollCode == SB_THUMBPOSITION)
4797 SetScrollPos(infoPtr->hwnd, SB_VERT,
4798 newFirstVisible->visibleOrder, TRUE);
4801 return 0;
4804 static LRESULT
4805 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4807 int maxWidth;
4808 int scrollX = infoPtr->scrollX;
4809 int nScrollCode = LOWORD(wParam);
4811 TRACE("wp %lx\n", wParam);
4813 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4814 return FALSE;
4816 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4817 /* shall never occur */
4818 if (maxWidth <= 0)
4820 scrollX = 0;
4821 goto scroll;
4824 switch (nScrollCode)
4826 case SB_LINELEFT:
4827 scrollX -= infoPtr->uItemHeight;
4828 break;
4829 case SB_LINERIGHT:
4830 scrollX += infoPtr->uItemHeight;
4831 break;
4832 case SB_PAGELEFT:
4833 scrollX -= infoPtr->clientWidth;
4834 break;
4835 case SB_PAGERIGHT:
4836 scrollX += infoPtr->clientWidth;
4837 break;
4839 case SB_THUMBTRACK:
4840 case SB_THUMBPOSITION:
4841 scrollX = (int)(SHORT)HIWORD(wParam);
4842 break;
4844 case SB_ENDSCROLL:
4845 return 0;
4848 if (scrollX > maxWidth)
4849 scrollX = maxWidth;
4850 else if (scrollX < 0)
4851 scrollX = 0;
4853 scroll:
4854 if (scrollX != infoPtr->scrollX)
4856 TREEVIEW_ITEM *item;
4857 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4859 for (item = infoPtr->root->firstChild; item != NULL;
4860 item = TREEVIEW_GetNextListItem(infoPtr, item))
4862 item->linesOffset += scroll_pixels;
4863 item->stateOffset += scroll_pixels;
4864 item->imageOffset += scroll_pixels;
4865 item->textOffset += scroll_pixels;
4868 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4869 infoPtr->scrollX = scrollX;
4870 UpdateWindow(infoPtr->hwnd);
4873 if (nScrollCode != SB_THUMBTRACK)
4874 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4876 return 0;
4879 static LRESULT
4880 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4882 short gcWheelDelta;
4883 UINT pulScrollLines = 3;
4885 if (wParam & (MK_SHIFT | MK_CONTROL))
4886 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4888 if (infoPtr->firstVisible == NULL)
4889 return TRUE;
4891 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4893 gcWheelDelta = -(short)HIWORD(wParam);
4894 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4896 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4898 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4899 int maxDy = infoPtr->maxVisibleOrder;
4901 if (newDy > maxDy)
4902 newDy = maxDy;
4904 if (newDy < 0)
4905 newDy = 0;
4907 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4909 return TRUE;
4912 /* Create/Destroy *******************************************************/
4914 static void
4915 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4917 RECT rc;
4918 HBITMAP hbm, hbmOld;
4919 HDC hdc, hdcScreen;
4920 int nIndex;
4922 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4924 hdcScreen = GetDC(0);
4926 hdc = CreateCompatibleDC(hdcScreen);
4927 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4928 hbmOld = SelectObject(hdc, hbm);
4930 SetRect(&rc, 0, 0, 48, 16);
4931 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4933 SetRect(&rc, 18, 2, 30, 14);
4934 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4935 DFCS_BUTTONCHECK|DFCS_FLAT);
4937 SetRect(&rc, 34, 2, 46, 14);
4938 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4939 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4941 SelectObject(hdc, hbmOld);
4942 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4943 comctl32_color.clrWindow);
4944 TRACE("checkbox index %d\n", nIndex);
4946 DeleteObject(hbm);
4947 DeleteDC(hdc);
4948 ReleaseDC(0, hdcScreen);
4950 infoPtr->stateImageWidth = 16;
4951 infoPtr->stateImageHeight = 16;
4954 static LRESULT
4955 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4957 RECT rcClient;
4958 TREEVIEW_INFO *infoPtr;
4959 LOGFONTW lf;
4961 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4963 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4965 if (infoPtr == NULL)
4967 ERR("could not allocate info memory!\n");
4968 return 0;
4971 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4973 infoPtr->hwnd = hwnd;
4974 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4975 infoPtr->Timer = 0;
4976 infoPtr->uNumItems = 0;
4977 infoPtr->cdmode = 0;
4978 infoPtr->uScrollTime = 300; /* milliseconds */
4979 infoPtr->bRedraw = TRUE;
4981 GetClientRect(hwnd, &rcClient);
4983 /* No scroll bars yet. */
4984 infoPtr->clientWidth = rcClient.right;
4985 infoPtr->clientHeight = rcClient.bottom;
4986 infoPtr->uInternalStatus = 0;
4988 infoPtr->treeWidth = 0;
4989 infoPtr->treeHeight = 0;
4991 infoPtr->uIndent = MINIMUM_INDENT;
4992 infoPtr->selectedItem = NULL;
4993 infoPtr->focusedItem = NULL;
4994 infoPtr->hotItem = NULL;
4995 infoPtr->editItem = NULL;
4996 infoPtr->firstVisible = NULL;
4997 infoPtr->maxVisibleOrder = 0;
4998 infoPtr->dropItem = NULL;
4999 infoPtr->insertMarkItem = NULL;
5000 infoPtr->insertBeforeorAfter = 0;
5001 /* dragList */
5003 infoPtr->scrollX = 0;
5005 infoPtr->clrBk = CLR_NONE; /* use system color */
5006 infoPtr->clrText = CLR_NONE; /* use system color */
5007 infoPtr->clrLine = CLR_DEFAULT;
5008 infoPtr->clrInsertMark = CLR_DEFAULT;
5010 /* hwndToolTip */
5012 infoPtr->hwndEdit = NULL;
5013 infoPtr->wpEditOrig = NULL;
5014 infoPtr->bIgnoreEditKillFocus = FALSE;
5015 infoPtr->bLabelChanged = FALSE;
5017 infoPtr->himlNormal = NULL;
5018 infoPtr->himlState = NULL;
5019 infoPtr->normalImageWidth = 0;
5020 infoPtr->normalImageHeight = 0;
5021 infoPtr->stateImageWidth = 0;
5022 infoPtr->stateImageHeight = 0;
5024 infoPtr->items = DPA_Create(16);
5026 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5027 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5028 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5029 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5030 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5032 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5034 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5035 infoPtr->root->state = TVIS_EXPANDED;
5036 infoPtr->root->iLevel = -1;
5037 infoPtr->root->visibleOrder = -1;
5039 infoPtr->hwndNotify = lpcs->hwndParent;
5040 #if 0
5041 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
5042 #endif
5044 infoPtr->hwndToolTip = 0;
5046 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5048 /* Determine what type of notify should be issued */
5049 /* sets infoPtr->bNtfUnicode */
5050 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5052 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5053 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5054 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5055 hwnd, 0, 0, 0);
5057 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5058 initialize_checkboxes(infoPtr);
5060 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5061 ShowScrollBar(hwnd, SB_VERT, FALSE);
5062 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5064 OpenThemeData (hwnd, themeClass);
5066 return 0;
5070 static LRESULT
5071 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5073 TRACE("\n");
5075 /* free item data */
5076 TREEVIEW_RemoveTree(infoPtr);
5077 /* root isn't freed with other items */
5078 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5079 DPA_Destroy(infoPtr->items);
5081 /* tool tip is automatically destroyed: we are its owner */
5083 /* Restore original wndproc */
5084 if (infoPtr->hwndEdit)
5085 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5086 (DWORD_PTR)infoPtr->wpEditOrig);
5088 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5090 /* Deassociate treeview from the window before doing anything drastic. */
5091 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5094 DeleteObject(infoPtr->hDefaultFont);
5095 DeleteObject(infoPtr->hBoldFont);
5096 DeleteObject(infoPtr->hUnderlineFont);
5097 Free(infoPtr);
5099 return 0;
5102 /* Miscellaneous Messages ***********************************************/
5104 static LRESULT
5105 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5107 static const struct
5109 unsigned char code;
5111 scroll[] =
5113 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5114 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5115 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5116 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5117 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5118 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5119 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5120 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5121 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5122 #undef SCROLL_ENTRY
5125 if (key >= VK_PRIOR && key <= VK_DOWN)
5127 unsigned char code = scroll[key - VK_PRIOR].code;
5129 (((code & (1 << 7)) == (SB_HORZ << 7))
5130 ? TREEVIEW_HScroll
5131 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5134 return 0;
5137 /************************************************************************
5138 * TREEVIEW_KeyDown
5140 * VK_UP Move selection to the previous non-hidden item.
5141 * VK_DOWN Move selection to the next non-hidden item.
5142 * VK_HOME Move selection to the first item.
5143 * VK_END Move selection to the last item.
5144 * VK_LEFT If expanded then collapse, otherwise move to parent.
5145 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5146 * VK_ADD Expand.
5147 * VK_SUBTRACT Collapse.
5148 * VK_MULTIPLY Expand all.
5149 * VK_PRIOR Move up GetVisibleCount items.
5150 * VK_NEXT Move down GetVisibleCount items.
5151 * VK_BACK Move to parent.
5152 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5154 static LRESULT
5155 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5157 /* If it is non-NULL and different, it will be selected and visible. */
5158 TREEVIEW_ITEM *newSelection = NULL;
5160 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5162 TRACE("%lx\n", wParam);
5164 if (prevItem == NULL)
5165 return FALSE;
5167 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5168 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5170 switch (wParam)
5172 case VK_UP:
5173 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5174 if (!newSelection)
5175 newSelection = infoPtr->root->firstChild;
5176 break;
5178 case VK_DOWN:
5179 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5180 break;
5182 case VK_HOME:
5183 newSelection = infoPtr->root->firstChild;
5184 break;
5186 case VK_END:
5187 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5188 break;
5190 case VK_LEFT:
5191 if (prevItem->state & TVIS_EXPANDED)
5193 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5195 else if (prevItem->parent != infoPtr->root)
5197 newSelection = prevItem->parent;
5199 break;
5201 case VK_RIGHT:
5202 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5204 if (!(prevItem->state & TVIS_EXPANDED))
5205 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5206 else
5208 newSelection = prevItem->firstChild;
5212 break;
5214 case VK_MULTIPLY:
5215 TREEVIEW_ExpandAll(infoPtr, prevItem);
5216 break;
5218 case VK_ADD:
5219 if (!(prevItem->state & TVIS_EXPANDED))
5220 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5221 break;
5223 case VK_SUBTRACT:
5224 if (prevItem->state & TVIS_EXPANDED)
5225 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5226 break;
5228 case VK_PRIOR:
5229 newSelection
5230 = TREEVIEW_GetListItem(infoPtr, prevItem,
5231 -TREEVIEW_GetVisibleCount(infoPtr));
5232 break;
5234 case VK_NEXT:
5235 newSelection
5236 = TREEVIEW_GetListItem(infoPtr, prevItem,
5237 TREEVIEW_GetVisibleCount(infoPtr));
5238 break;
5240 case VK_BACK:
5241 newSelection = prevItem->parent;
5242 if (newSelection == infoPtr->root)
5243 newSelection = NULL;
5244 break;
5246 case VK_SPACE:
5247 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5248 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5249 break;
5252 if (newSelection && newSelection != prevItem)
5254 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5255 TVC_BYKEYBOARD))
5257 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5261 return FALSE;
5264 static LRESULT
5265 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5267 /* remove hot effect from item */
5268 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5269 infoPtr->hotItem = NULL;
5271 return 0;
5274 static LRESULT
5275 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5277 POINT pt;
5278 TRACKMOUSEEVENT trackinfo;
5279 TREEVIEW_ITEM * item;
5281 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5283 /* fill in the TRACKMOUSEEVENT struct */
5284 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5285 trackinfo.dwFlags = TME_QUERY;
5286 trackinfo.hwndTrack = infoPtr->hwnd;
5288 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5289 _TrackMouseEvent(&trackinfo);
5291 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5292 if(!(trackinfo.dwFlags & TME_LEAVE))
5294 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5295 trackinfo.hwndTrack = infoPtr->hwnd;
5296 /* do it as fast as possible, minimal systimer latency will be used */
5297 trackinfo.dwHoverTime = 1;
5299 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5300 /* and can properly deactivate the hot item */
5301 _TrackMouseEvent(&trackinfo);
5304 pt.x = (short)LOWORD(lParam);
5305 pt.y = (short)HIWORD(lParam);
5307 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5309 if (item != infoPtr->hotItem)
5311 /* redraw old hot item */
5312 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5313 infoPtr->hotItem = item;
5314 /* redraw new hot item */
5315 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5318 return 0;
5321 /* Draw themed border */
5322 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5324 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5325 HDC dc;
5326 RECT r;
5327 HRGN cliprgn;
5328 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5329 cyEdge = GetSystemMetrics (SM_CYEDGE);
5331 if (!theme)
5332 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5334 GetWindowRect(infoPtr->hwnd, &r);
5336 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5337 r.right - cxEdge, r.bottom - cyEdge);
5338 if (region != (HRGN)1)
5339 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5340 OffsetRect(&r, -r.left, -r.top);
5342 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5343 OffsetRect(&r, -r.left, -r.top);
5345 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5346 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5347 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5348 ReleaseDC(infoPtr->hwnd, dc);
5350 /* Call default proc to get the scrollbars etc. painted */
5351 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5353 return TRUE;
5356 static LRESULT
5357 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5359 LPNMHDR lpnmh = (LPNMHDR)lParam;
5361 if (lpnmh->code == PGN_CALCSIZE) {
5362 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5364 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5365 lppgc->iWidth = infoPtr->treeWidth;
5366 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5367 infoPtr->treeWidth, infoPtr->clientWidth);
5369 else {
5370 lppgc->iHeight = infoPtr->treeHeight;
5371 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5372 infoPtr->treeHeight, infoPtr->clientHeight);
5374 return 0;
5376 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5379 static LRESULT
5380 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5382 if (wParam == SIZE_RESTORED)
5384 infoPtr->clientWidth = (short)LOWORD(lParam);
5385 infoPtr->clientHeight = (short)HIWORD(lParam);
5387 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5388 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5389 TREEVIEW_UpdateScrollBars(infoPtr);
5391 else
5393 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5396 TREEVIEW_Invalidate(infoPtr, NULL);
5397 return 0;
5400 static LRESULT
5401 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5403 TRACE("(%lx %lx)\n", wParam, lParam);
5405 if (wParam == GWL_STYLE)
5407 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5409 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5411 if (dwNewStyle & TVS_CHECKBOXES)
5413 initialize_checkboxes(infoPtr);
5414 TRACE("checkboxes enabled\n");
5416 else
5418 FIXME("tried to disable checkboxes\n");
5422 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5424 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5426 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5427 TRACE("tooltips enabled\n");
5429 else
5431 DestroyWindow(infoPtr->hwndToolTip);
5432 infoPtr->hwndToolTip = 0;
5433 TRACE("tooltips disabled\n");
5437 infoPtr->dwStyle = dwNewStyle;
5440 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5441 TREEVIEW_UpdateScrollBars(infoPtr);
5442 TREEVIEW_Invalidate(infoPtr, NULL);
5444 return 0;
5447 static LRESULT
5448 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5450 POINT pt;
5451 TREEVIEW_ITEM * item;
5452 NMMOUSE nmmouse;
5454 GetCursorPos(&pt);
5455 ScreenToClient(infoPtr->hwnd, &pt);
5457 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5459 memset(&nmmouse, 0, sizeof(nmmouse));
5460 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5461 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5462 nmmouse.hdr.code = NM_SETCURSOR;
5463 if (item)
5465 nmmouse.dwItemSpec = (DWORD_PTR)item;
5466 nmmouse.dwItemData = item->lParam;
5468 nmmouse.pt.x = 0;
5469 nmmouse.pt.y = 0;
5470 nmmouse.dwHitInfo = lParam;
5471 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, (LPARAM)&nmmouse))
5472 return 0;
5474 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5476 SetCursor(infoPtr->hcurHand);
5477 return 0;
5479 else
5480 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5483 static LRESULT
5484 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5486 TRACE("\n");
5488 if (!infoPtr->selectedItem)
5490 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5491 TVC_UNKNOWN);
5494 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5495 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5496 return 0;
5499 static LRESULT
5500 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5502 TRACE("\n");
5504 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5505 UpdateWindow(infoPtr->hwnd);
5506 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5507 return 0;
5510 /* update theme after a WM_THEMECHANGED message */
5511 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5513 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5514 CloseThemeData (theme);
5515 OpenThemeData (infoPtr->hwnd, themeClass);
5516 return 0;
5520 static LRESULT WINAPI
5521 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5523 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5525 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5527 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5528 else
5530 if (uMsg == WM_CREATE)
5531 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5532 else
5533 goto def;
5536 switch (uMsg)
5538 case TVM_CREATEDRAGIMAGE:
5539 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5541 case TVM_DELETEITEM:
5542 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5544 case TVM_EDITLABELA:
5545 case TVM_EDITLABELW:
5546 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5548 case TVM_ENDEDITLABELNOW:
5549 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5551 case TVM_ENSUREVISIBLE:
5552 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5554 case TVM_EXPAND:
5555 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5557 case TVM_GETBKCOLOR:
5558 return TREEVIEW_GetBkColor(infoPtr);
5560 case TVM_GETCOUNT:
5561 return TREEVIEW_GetCount(infoPtr);
5563 case TVM_GETEDITCONTROL:
5564 return TREEVIEW_GetEditControl(infoPtr);
5566 case TVM_GETIMAGELIST:
5567 return TREEVIEW_GetImageList(infoPtr, wParam);
5569 case TVM_GETINDENT:
5570 return TREEVIEW_GetIndent(infoPtr);
5572 case TVM_GETINSERTMARKCOLOR:
5573 return TREEVIEW_GetInsertMarkColor(infoPtr);
5575 case TVM_GETISEARCHSTRINGA:
5576 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5577 return 0;
5579 case TVM_GETISEARCHSTRINGW:
5580 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5581 return 0;
5583 case TVM_GETITEMA:
5584 case TVM_GETITEMW:
5585 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5586 uMsg == TVM_GETITEMW);
5587 case TVM_GETITEMHEIGHT:
5588 return TREEVIEW_GetItemHeight(infoPtr);
5590 case TVM_GETITEMRECT:
5591 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5593 case TVM_GETITEMSTATE:
5594 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5596 case TVM_GETLINECOLOR:
5597 return TREEVIEW_GetLineColor(infoPtr);
5599 case TVM_GETNEXTITEM:
5600 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5602 case TVM_GETSCROLLTIME:
5603 return TREEVIEW_GetScrollTime(infoPtr);
5605 case TVM_GETTEXTCOLOR:
5606 return TREEVIEW_GetTextColor(infoPtr);
5608 case TVM_GETTOOLTIPS:
5609 return TREEVIEW_GetToolTips(infoPtr);
5611 case TVM_GETUNICODEFORMAT:
5612 return TREEVIEW_GetUnicodeFormat(infoPtr);
5614 case TVM_GETVISIBLECOUNT:
5615 return TREEVIEW_GetVisibleCount(infoPtr);
5617 case TVM_HITTEST:
5618 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5620 case TVM_INSERTITEMA:
5621 case TVM_INSERTITEMW:
5622 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5623 uMsg == TVM_INSERTITEMW);
5624 case TVM_SELECTITEM:
5625 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5627 case TVM_SETBKCOLOR:
5628 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5630 case TVM_SETIMAGELIST:
5631 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5633 case TVM_SETINDENT:
5634 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5636 case TVM_SETINSERTMARK:
5637 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5639 case TVM_SETINSERTMARKCOLOR:
5640 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5642 case TVM_SETITEMA:
5643 case TVM_SETITEMW:
5644 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5645 uMsg == TVM_SETITEMW);
5646 case TVM_SETLINECOLOR:
5647 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5649 case TVM_SETITEMHEIGHT:
5650 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5652 case TVM_SETSCROLLTIME:
5653 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5655 case TVM_SETTEXTCOLOR:
5656 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5658 case TVM_SETTOOLTIPS:
5659 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5661 case TVM_SETUNICODEFORMAT:
5662 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5664 case TVM_SORTCHILDREN:
5665 return TREEVIEW_SortChildren(infoPtr, lParam);
5667 case TVM_SORTCHILDRENCB:
5668 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5670 case WM_CHAR:
5671 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5673 case WM_COMMAND:
5674 return TREEVIEW_Command(infoPtr, wParam, lParam);
5676 case WM_DESTROY:
5677 return TREEVIEW_Destroy(infoPtr);
5679 /* WM_ENABLE */
5681 case WM_ERASEBKGND:
5682 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5684 case WM_GETDLGCODE:
5685 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5687 case WM_GETFONT:
5688 return TREEVIEW_GetFont(infoPtr);
5690 case WM_HSCROLL:
5691 return TREEVIEW_HScroll(infoPtr, wParam);
5693 case WM_KEYDOWN:
5694 return TREEVIEW_KeyDown(infoPtr, wParam);
5696 case WM_KILLFOCUS:
5697 return TREEVIEW_KillFocus(infoPtr);
5699 case WM_LBUTTONDBLCLK:
5700 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5702 case WM_LBUTTONDOWN:
5703 return TREEVIEW_LButtonDown(infoPtr, lParam);
5705 /* WM_MBUTTONDOWN */
5707 case WM_MOUSELEAVE:
5708 return TREEVIEW_MouseLeave(infoPtr);
5710 case WM_MOUSEMOVE:
5711 return TREEVIEW_MouseMove(infoPtr, lParam);
5713 case WM_NCLBUTTONDOWN:
5714 if (infoPtr->hwndEdit)
5715 SetFocus(infoPtr->hwnd);
5716 goto def;
5718 case WM_NCPAINT:
5719 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5721 case WM_NOTIFY:
5722 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5724 case WM_NOTIFYFORMAT:
5725 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5727 case WM_PRINTCLIENT:
5728 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5730 case WM_PAINT:
5731 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5733 case WM_RBUTTONDOWN:
5734 return TREEVIEW_RButtonDown(infoPtr, lParam);
5736 case WM_SETCURSOR:
5737 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5739 case WM_SETFOCUS:
5740 return TREEVIEW_SetFocus(infoPtr);
5742 case WM_SETFONT:
5743 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5745 case WM_SETREDRAW:
5746 return TREEVIEW_SetRedraw(infoPtr, wParam);
5748 case WM_SIZE:
5749 return TREEVIEW_Size(infoPtr, wParam, lParam);
5751 case WM_STYLECHANGED:
5752 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5754 case WM_SYSCOLORCHANGE:
5755 COMCTL32_RefreshSysColors();
5756 return 0;
5758 /* WM_SYSKEYDOWN */
5760 case WM_TIMER:
5761 return TREEVIEW_HandleTimer(infoPtr, wParam);
5763 case WM_THEMECHANGED:
5764 return TREEVIEW_ThemeChanged (infoPtr);
5766 case WM_VSCROLL:
5767 return TREEVIEW_VScroll(infoPtr, wParam);
5769 /* WM_WININICHANGE */
5771 case WM_MOUSEWHEEL:
5772 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5774 case WM_DRAWITEM:
5775 TRACE("drawItem\n");
5776 goto def;
5778 default:
5779 /* This mostly catches MFC and Delphi messages. :( */
5780 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5781 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5782 def:
5783 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5788 /* Class Registration ***************************************************/
5790 VOID
5791 TREEVIEW_Register(void)
5793 WNDCLASSW wndClass;
5795 TRACE("\n");
5797 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5798 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5799 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5800 wndClass.cbClsExtra = 0;
5801 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5803 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5804 wndClass.hbrBackground = 0;
5805 wndClass.lpszClassName = WC_TREEVIEWW;
5807 RegisterClassW(&wndClass);
5811 VOID
5812 TREEVIEW_Unregister(void)
5814 UnregisterClassW(WC_TREEVIEWW, NULL);
5818 /* Tree Verification ****************************************************/
5820 #ifdef NDEBUG
5821 static inline void
5822 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5824 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5825 TREEVIEW_ITEM *item)
5827 assert(infoPtr != NULL);
5828 assert(item != NULL);
5830 /* both NULL, or both non-null */
5831 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5833 assert(item->firstChild != item);
5834 assert(item->lastChild != item);
5836 if (item->firstChild)
5838 assert(item->firstChild->parent == item);
5839 assert(item->firstChild->prevSibling == NULL);
5842 if (item->lastChild)
5844 assert(item->lastChild->parent == item);
5845 assert(item->lastChild->nextSibling == NULL);
5848 assert(item->nextSibling != item);
5849 if (item->nextSibling)
5851 assert(item->nextSibling->parent == item->parent);
5852 assert(item->nextSibling->prevSibling == item);
5855 assert(item->prevSibling != item);
5856 if (item->prevSibling)
5858 assert(item->prevSibling->parent == item->parent);
5859 assert(item->prevSibling->nextSibling == item);
5863 static inline void
5864 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5866 assert(item != NULL);
5868 assert(item->parent != NULL);
5869 assert(item->parent != item);
5870 assert(item->iLevel == item->parent->iLevel + 1);
5872 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5874 TREEVIEW_VerifyItemCommon(infoPtr, item);
5876 TREEVIEW_VerifyChildren(infoPtr, item);
5879 static inline void
5880 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5882 TREEVIEW_ITEM *child;
5883 assert(item != NULL);
5885 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5886 TREEVIEW_VerifyItem(infoPtr, child);
5889 static inline void
5890 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5892 TREEVIEW_ITEM *root = infoPtr->root;
5894 assert(root != NULL);
5895 assert(root->iLevel == -1);
5896 assert(root->parent == NULL);
5897 assert(root->prevSibling == NULL);
5899 TREEVIEW_VerifyItemCommon(infoPtr, root);
5901 TREEVIEW_VerifyChildren(infoPtr, root);
5904 static void
5905 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5907 assert(infoPtr != NULL);
5909 TREEVIEW_VerifyRoot(infoPtr);
5911 #endif