shell32: Fix the errors in two Chinese (Simplified) resources.
[wine/hacks.git] / dlls / comctl32 / treeview.c
blob5adbb3012a59867fb565d790bcbc0ba4cae1e34d
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 *****************************************************/
216 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
218 /* Returns the treeview private data if hwnd is a treeview.
219 * Otherwise returns an undefined value. */
220 static inline TREEVIEW_INFO *
221 TREEVIEW_GetInfoPtr(HWND hwnd)
223 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
226 /* Don't call this. Nothing wants an item index. */
227 static inline int
228 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
230 return DPA_GetPtrIndex(infoPtr->items, handle);
233 /* Checks if item has changed and needs to be redrawn */
234 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
235 const TVITEMEXW *tvChange)
237 /* Number of children has changed */
238 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
239 return TRUE;
241 /* Image has changed and it's not a callback */
242 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
243 tiNew->iImage != I_IMAGECALLBACK)
244 return TRUE;
246 /* Selected image has changed and it's not a callback */
247 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
248 tiNew->iSelectedImage != I_IMAGECALLBACK)
249 return TRUE;
251 /* Text has changed and it's not a callback */
252 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
253 tiNew->pszText != LPSTR_TEXTCALLBACKW)
254 return TRUE;
256 /* Indent has changed */
257 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
258 return TRUE;
260 /* Item state has changed */
261 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
262 return TRUE;
264 return FALSE;
267 /***************************************************************************
268 * This method checks that handle is an item for this tree.
270 static BOOL
271 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
273 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
275 TRACE("invalid item %p\n", handle);
276 return FALSE;
278 else
279 return TRUE;
282 static HFONT
283 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
285 LOGFONTW font;
287 GetObjectW(hOrigFont, sizeof(font), &font);
288 font.lfWeight = FW_BOLD;
289 return CreateFontIndirectW(&font);
292 static HFONT
293 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
295 LOGFONTW font;
297 GetObjectW(hOrigFont, sizeof(font), &font);
298 font.lfUnderline = TRUE;
299 return CreateFontIndirectW(&font);
302 static inline HFONT
303 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
305 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
306 return infoPtr->hUnderlineFont;
307 if (item->state & TVIS_BOLD)
308 return infoPtr->hBoldFont;
309 return infoPtr->hFont;
312 /* for trace/debugging purposes only */
313 static const char *
314 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
316 if (item == NULL) return "<null item>";
317 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
318 if (item->pszText == NULL) return "<null>";
319 return debugstr_w(item->pszText);
322 /* An item is not a child of itself. */
323 static BOOL
324 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
328 child = child->parent;
329 if (child == parent) return TRUE;
330 } while (child != NULL);
332 return FALSE;
336 /* Tree Traversal *******************************************************/
338 /***************************************************************************
339 * This method returns the last expanded sibling or child child item
340 * of a tree node
342 static TREEVIEW_ITEM *
343 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
345 if (!wineItem)
346 return NULL;
348 while (wineItem->lastChild)
350 if (wineItem->state & TVIS_EXPANDED)
351 wineItem = wineItem->lastChild;
352 else
353 break;
356 if (wineItem == infoPtr->root)
357 return NULL;
359 return wineItem;
362 /***************************************************************************
363 * This method returns the previous non-hidden item in the list not
364 * considering the tree hierarchy.
366 static TREEVIEW_ITEM *
367 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
369 if (tvItem->prevSibling)
371 /* This item has a prevSibling, get the last item in the sibling's tree. */
372 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
374 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
375 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
376 else
377 return upItem;
379 else
381 /* this item does not have a prevSibling, get the parent */
382 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
387 /***************************************************************************
388 * This method returns the next physical item in the treeview not
389 * considering the tree hierarchy.
391 static TREEVIEW_ITEM *
392 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
395 * If this item has children and is expanded, return the first child
397 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
399 return tvItem->firstChild;
404 * try to get the sibling
406 if (tvItem->nextSibling)
407 return tvItem->nextSibling;
410 * Otherwise, get the parent's sibling.
412 while (tvItem->parent)
414 tvItem = tvItem->parent;
416 if (tvItem->nextSibling)
417 return tvItem->nextSibling;
420 return NULL;
423 /***************************************************************************
424 * This method returns the nth item starting at the given item. It returns
425 * the last item (or first) we we run out of items.
427 * Will scroll backward if count is <0.
428 * forward if count is >0.
430 static TREEVIEW_ITEM *
431 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
432 LONG count)
434 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
435 TREEVIEW_ITEM *previousItem;
437 assert(wineItem != NULL);
439 if (count > 0)
441 next_item = TREEVIEW_GetNextListItem;
443 else if (count < 0)
445 count = -count;
446 next_item = TREEVIEW_GetPrevListItem;
448 else
449 return wineItem;
453 previousItem = wineItem;
454 wineItem = next_item(infoPtr, wineItem);
456 } while (--count && wineItem != NULL);
459 return wineItem ? wineItem : previousItem;
462 /* Notifications ************************************************************/
464 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
466 if (!infoPtr->bNtfUnicode) {
467 switch (code) {
468 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
469 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
470 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
471 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
472 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
473 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
474 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
475 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
476 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
477 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
478 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
479 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
482 return code;
485 static inline BOOL
486 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
488 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
489 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
492 static BOOL
493 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
495 NMHDR nmhdr;
496 HWND hwnd = infoPtr->hwnd;
498 TRACE("%d\n", code);
499 nmhdr.hwndFrom = hwnd;
500 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
501 nmhdr.code = get_notifycode(infoPtr, code);
503 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
506 static VOID
507 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
509 tvItem->mask = mask;
510 tvItem->hItem = item;
511 tvItem->state = item->state;
512 tvItem->stateMask = 0;
513 tvItem->iImage = item->iImage;
514 tvItem->iSelectedImage = item->iSelectedImage;
515 tvItem->cChildren = item->cChildren;
516 tvItem->lParam = item->lParam;
518 if(mask & TVIF_TEXT)
520 if (!infoPtr->bNtfUnicode)
522 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
523 tvItem->pszText = Alloc (tvItem->cchTextMax);
524 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
526 else
528 tvItem->cchTextMax = item->cchTextMax;
529 tvItem->pszText = item->pszText;
532 else
534 tvItem->cchTextMax = 0;
535 tvItem->pszText = NULL;
539 static BOOL
540 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
541 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
543 HWND hwnd = infoPtr->hwnd;
544 NMTREEVIEWW nmhdr;
545 BOOL ret;
547 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
548 code, action, oldItem, newItem);
550 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
552 nmhdr.hdr.hwndFrom = hwnd;
553 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
554 nmhdr.hdr.code = get_notifycode(infoPtr, code);
555 nmhdr.action = action;
557 if (oldItem)
558 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
560 if (newItem)
561 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
563 nmhdr.ptDrag.x = 0;
564 nmhdr.ptDrag.y = 0;
566 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
567 if (!infoPtr->bNtfUnicode)
569 Free(nmhdr.itemOld.pszText);
570 Free(nmhdr.itemNew.pszText);
572 return ret;
575 static BOOL
576 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
577 HTREEITEM dragItem, POINT pt)
579 HWND hwnd = infoPtr->hwnd;
580 NMTREEVIEWW nmhdr;
582 TRACE("code:%d dragitem:%p\n", code, dragItem);
584 nmhdr.hdr.hwndFrom = hwnd;
585 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
586 nmhdr.hdr.code = get_notifycode(infoPtr, code);
587 nmhdr.action = 0;
588 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
589 nmhdr.itemNew.hItem = dragItem;
590 nmhdr.itemNew.state = dragItem->state;
591 nmhdr.itemNew.lParam = dragItem->lParam;
593 nmhdr.ptDrag.x = pt.x;
594 nmhdr.ptDrag.y = pt.y;
596 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
600 static BOOL
601 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
602 HDC hdc, RECT rc)
604 HWND hwnd = infoPtr->hwnd;
605 NMTVCUSTOMDRAW nmcdhdr;
606 LPNMCUSTOMDRAW nmcd;
608 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
610 nmcd = &nmcdhdr.nmcd;
611 nmcd->hdr.hwndFrom = hwnd;
612 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
613 nmcd->hdr.code = NM_CUSTOMDRAW;
614 nmcd->dwDrawStage = dwDrawStage;
615 nmcd->hdc = hdc;
616 nmcd->rc = rc;
617 nmcd->dwItemSpec = 0;
618 nmcd->uItemState = 0;
619 nmcd->lItemlParam = 0;
620 nmcdhdr.clrText = infoPtr->clrText;
621 nmcdhdr.clrTextBk = infoPtr->clrBk;
622 nmcdhdr.iLevel = 0;
624 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
629 /* FIXME: need to find out when the flags in uItemState need to be set */
631 static BOOL
632 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
633 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
634 NMTVCUSTOMDRAW *nmcdhdr)
636 HWND hwnd = infoPtr->hwnd;
637 LPNMCUSTOMDRAW nmcd;
638 DWORD dwDrawStage;
639 DWORD_PTR dwItemSpec;
640 UINT uItemState;
642 dwDrawStage = CDDS_ITEM | uItemDrawState;
643 dwItemSpec = (DWORD_PTR)wineItem;
644 uItemState = 0;
645 if (wineItem->state & TVIS_SELECTED)
646 uItemState |= CDIS_SELECTED;
647 if (wineItem == infoPtr->selectedItem)
648 uItemState |= CDIS_FOCUS;
649 if (wineItem == infoPtr->hotItem)
650 uItemState |= CDIS_HOT;
652 nmcd = &nmcdhdr->nmcd;
653 nmcd->hdr.hwndFrom = hwnd;
654 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
655 nmcd->hdr.code = NM_CUSTOMDRAW;
656 nmcd->dwDrawStage = dwDrawStage;
657 nmcd->hdc = hdc;
658 nmcd->rc = wineItem->rect;
659 nmcd->dwItemSpec = dwItemSpec;
660 nmcd->uItemState = uItemState;
661 nmcd->lItemlParam = wineItem->lParam;
662 nmcdhdr->iLevel = wineItem->iLevel;
664 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
665 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
666 nmcd->uItemState, nmcd->lItemlParam);
668 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
671 static BOOL
672 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
674 HWND hwnd = infoPtr->hwnd;
675 NMTVDISPINFOW tvdi;
676 BOOL ret;
678 tvdi.hdr.hwndFrom = hwnd;
679 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
680 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
682 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
683 &tvdi.item, editItem);
685 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
687 if (!infoPtr->bNtfUnicode)
688 Free(tvdi.item.pszText);
690 return ret;
693 static void
694 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
695 UINT mask)
697 NMTVDISPINFOEXW callback;
698 HWND hwnd = infoPtr->hwnd;
700 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
701 mask &= wineItem->callbackMask;
703 if (mask == 0) return;
705 callback.hdr.hwndFrom = hwnd;
706 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
707 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
709 /* 'state' always contains valid value, as well as 'lParam'.
710 * All other parameters are uninitialized.
712 callback.item.pszText = wineItem->pszText;
713 callback.item.cchTextMax = wineItem->cchTextMax;
714 callback.item.mask = mask;
715 callback.item.hItem = wineItem;
716 callback.item.state = wineItem->state;
717 callback.item.lParam = wineItem->lParam;
719 /* If text is changed we need to recalculate textWidth */
720 if (mask & TVIF_TEXT)
721 wineItem->textWidth = 0;
723 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
725 /* It may have changed due to a call to SetItem. */
726 mask &= wineItem->callbackMask;
728 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
730 /* Instead of copying text into our buffer user specified its own */
731 if (!infoPtr->bNtfUnicode) {
732 LPWSTR newText;
733 int buflen;
734 int len = MultiByteToWideChar( CP_ACP, 0,
735 (LPSTR)callback.item.pszText, -1,
736 NULL, 0);
737 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
738 newText = ReAlloc(wineItem->pszText, buflen);
740 TRACE("returned str %s, len=%d, buflen=%d\n",
741 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
743 if (newText)
745 wineItem->pszText = newText;
746 MultiByteToWideChar( CP_ACP, 0,
747 (LPSTR)callback.item.pszText, -1,
748 wineItem->pszText, buflen/sizeof(WCHAR));
749 wineItem->cchTextMax = buflen/sizeof(WCHAR);
751 /* If ReAlloc fails we have nothing to do, but keep original text */
753 else {
754 int len = max(lstrlenW(callback.item.pszText) + 1,
755 TEXT_CALLBACK_SIZE);
756 LPWSTR newText = ReAlloc(wineItem->pszText, len);
758 TRACE("returned wstr %s, len=%d\n",
759 debugstr_w(callback.item.pszText), len);
761 if (newText)
763 wineItem->pszText = newText;
764 strcpyW(wineItem->pszText, callback.item.pszText);
765 wineItem->cchTextMax = len;
767 /* If ReAlloc fails we have nothing to do, but keep original text */
770 else if (mask & TVIF_TEXT) {
771 /* User put text into our buffer, that is ok unless A string */
772 if (!infoPtr->bNtfUnicode) {
773 LPWSTR newText;
774 LPWSTR oldText = NULL;
775 int buflen;
776 int len = MultiByteToWideChar( CP_ACP, 0,
777 (LPSTR)callback.item.pszText, -1,
778 NULL, 0);
779 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
780 newText = Alloc(buflen);
782 TRACE("same buffer str %s, len=%d, buflen=%d\n",
783 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
785 if (newText)
787 oldText = wineItem->pszText;
788 wineItem->pszText = newText;
789 MultiByteToWideChar( CP_ACP, 0,
790 (LPSTR)callback.item.pszText, -1,
791 wineItem->pszText, buflen/sizeof(WCHAR));
792 wineItem->cchTextMax = buflen/sizeof(WCHAR);
793 Free(oldText);
798 if (mask & TVIF_IMAGE)
799 wineItem->iImage = callback.item.iImage;
801 if (mask & TVIF_SELECTEDIMAGE)
802 wineItem->iSelectedImage = callback.item.iSelectedImage;
804 if (mask & TVIF_CHILDREN)
805 wineItem->cChildren = callback.item.cChildren;
807 /* These members are now permanently set. */
808 if (callback.item.mask & TVIF_DI_SETITEM)
809 wineItem->callbackMask &= ~callback.item.mask;
812 /***************************************************************************
813 * This function uses cChildren field to decide whether the item has
814 * children or not.
815 * Note: if this returns TRUE, the child items may not actually exist,
816 * they could be virtual.
818 * Just use wineItem->firstChild to check for physical children.
820 static BOOL
821 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
823 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
825 return wineItem->cChildren > 0;
828 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
830 INT format;
832 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
834 if (nCommand != NF_REQUERY) return 0;
836 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
837 TRACE("format=%d\n", format);
839 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
841 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
843 return format;
846 /* Item Position ********************************************************/
848 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
849 static VOID
850 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
852 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
853 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
854 > TVS_LINESATROOT);
856 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
857 - infoPtr->scrollX;
858 item->stateOffset = item->linesOffset + infoPtr->uIndent;
859 item->imageOffset = item->stateOffset
860 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
861 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
864 static VOID
865 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
867 HDC hdc;
868 HFONT hOldFont=0;
869 SIZE sz;
871 /* DRAW's OM docker creates items like this */
872 if (item->pszText == NULL)
874 item->textWidth = 0;
875 return;
878 if (hDC != 0)
880 hdc = hDC;
882 else
884 hdc = GetDC(infoPtr->hwnd);
885 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
888 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
889 item->textWidth = sz.cx;
891 if (hDC == 0)
893 SelectObject(hdc, hOldFont);
894 ReleaseDC(0, hdc);
898 static VOID
899 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
901 item->rect.top = infoPtr->uItemHeight *
902 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
904 item->rect.bottom = item->rect.top
905 + infoPtr->uItemHeight * item->iIntegral - 1;
907 item->rect.left = 0;
908 item->rect.right = infoPtr->clientWidth;
911 /* We know that only items after start need their order updated. */
912 static void
913 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
915 TREEVIEW_ITEM *item;
916 int order;
918 if (!start)
920 start = infoPtr->root->firstChild;
921 order = 0;
923 else
924 order = start->visibleOrder;
926 for (item = start; item != NULL;
927 item = TREEVIEW_GetNextListItem(infoPtr, item))
929 if (!ISVISIBLE(item) && order > 0)
930 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
931 item->visibleOrder = order;
932 order += item->iIntegral;
935 infoPtr->maxVisibleOrder = order;
937 for (item = start; item != NULL;
938 item = TREEVIEW_GetNextListItem(infoPtr, item))
940 TREEVIEW_ComputeItemRect(infoPtr, item);
945 /* Update metrics of all items in selected subtree.
946 * root must be expanded
948 static VOID
949 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
951 TREEVIEW_ITEM *sibling;
952 HDC hdc;
953 HFONT hOldFont;
955 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
956 return;
958 root->state &= ~TVIS_EXPANDED;
959 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
960 root->state |= TVIS_EXPANDED;
962 hdc = GetDC(infoPtr->hwnd);
963 hOldFont = SelectObject(hdc, infoPtr->hFont);
965 for (; root != sibling;
966 root = TREEVIEW_GetNextListItem(infoPtr, root))
968 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
970 if (root->callbackMask & TVIF_TEXT)
971 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
973 if (root->textWidth == 0)
975 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
976 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
980 SelectObject(hdc, hOldFont);
981 ReleaseDC(infoPtr->hwnd, hdc);
984 /* Item Allocation **********************************************************/
986 static TREEVIEW_ITEM *
987 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
989 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
991 if (!newItem)
992 return NULL;
994 /* I_IMAGENONE would make more sense but this is neither what is
995 * documented (MSDN doesn't specify) nor what Windows actually does
996 * (it sets it to zero)... and I can so imagine an application using
997 * inc/dec to toggle the images. */
998 newItem->iImage = 0;
999 newItem->iSelectedImage = 0;
1001 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1003 Free(newItem);
1004 return NULL;
1007 return newItem;
1010 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1011 * free item->pszText. */
1012 static void
1013 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1015 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1016 if (infoPtr->selectedItem == item)
1017 infoPtr->selectedItem = NULL;
1018 if (infoPtr->hotItem == item)
1019 infoPtr->hotItem = NULL;
1020 if (infoPtr->focusedItem == item)
1021 infoPtr->focusedItem = NULL;
1022 if (infoPtr->firstVisible == item)
1023 infoPtr->firstVisible = NULL;
1024 if (infoPtr->dropItem == item)
1025 infoPtr->dropItem = NULL;
1026 if (infoPtr->insertMarkItem == item)
1027 infoPtr->insertMarkItem = NULL;
1028 Free(item);
1032 /* Item Insertion *******************************************************/
1034 /***************************************************************************
1035 * This method inserts newItem before sibling as a child of parent.
1036 * sibling can be NULL, but only if parent has no children.
1038 static void
1039 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1040 TREEVIEW_ITEM *parent)
1042 assert(parent != NULL);
1044 if (sibling != NULL)
1046 assert(sibling->parent == parent);
1048 if (sibling->prevSibling != NULL)
1049 sibling->prevSibling->nextSibling = newItem;
1051 newItem->prevSibling = sibling->prevSibling;
1052 sibling->prevSibling = newItem;
1054 else
1055 newItem->prevSibling = NULL;
1057 newItem->nextSibling = sibling;
1059 if (parent->firstChild == sibling)
1060 parent->firstChild = newItem;
1062 if (parent->lastChild == NULL)
1063 parent->lastChild = newItem;
1066 /***************************************************************************
1067 * This method inserts newItem after sibling as a child of parent.
1068 * sibling can be NULL, but only if parent has no children.
1070 static void
1071 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1072 TREEVIEW_ITEM *parent)
1074 assert(parent != NULL);
1076 if (sibling != NULL)
1078 assert(sibling->parent == parent);
1080 if (sibling->nextSibling != NULL)
1081 sibling->nextSibling->prevSibling = newItem;
1083 newItem->nextSibling = sibling->nextSibling;
1084 sibling->nextSibling = newItem;
1086 else
1087 newItem->nextSibling = NULL;
1089 newItem->prevSibling = sibling;
1091 if (parent->lastChild == sibling)
1092 parent->lastChild = newItem;
1094 if (parent->firstChild == NULL)
1095 parent->firstChild = newItem;
1098 static BOOL
1099 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1100 const TVITEMEXW *tvItem, BOOL isW)
1102 UINT callbackClear = 0;
1103 UINT callbackSet = 0;
1105 TRACE("item %p\n", wineItem);
1106 /* Do this first in case it fails. */
1107 if (tvItem->mask & TVIF_TEXT)
1109 wineItem->textWidth = 0; /* force width recalculation */
1110 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1112 int len;
1113 LPWSTR newText;
1114 if (isW)
1115 len = lstrlenW(tvItem->pszText) + 1;
1116 else
1117 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1119 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1121 if (newText == NULL) return FALSE;
1123 callbackClear |= TVIF_TEXT;
1125 wineItem->pszText = newText;
1126 wineItem->cchTextMax = len;
1127 if (isW)
1128 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1129 else
1130 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1131 wineItem->pszText, len);
1133 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1135 else
1137 callbackSet |= TVIF_TEXT;
1139 wineItem->pszText = ReAlloc(wineItem->pszText,
1140 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1141 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1142 TRACE("setting callback, item %p\n", wineItem);
1146 if (tvItem->mask & TVIF_CHILDREN)
1148 wineItem->cChildren = tvItem->cChildren;
1150 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1151 callbackSet |= TVIF_CHILDREN;
1152 else
1153 callbackClear |= TVIF_CHILDREN;
1156 if (tvItem->mask & TVIF_IMAGE)
1158 wineItem->iImage = tvItem->iImage;
1160 if (wineItem->iImage == I_IMAGECALLBACK)
1161 callbackSet |= TVIF_IMAGE;
1162 else
1163 callbackClear |= TVIF_IMAGE;
1166 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1168 wineItem->iSelectedImage = tvItem->iSelectedImage;
1170 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1171 callbackSet |= TVIF_SELECTEDIMAGE;
1172 else
1173 callbackClear |= TVIF_SELECTEDIMAGE;
1176 if (tvItem->mask & TVIF_PARAM)
1177 wineItem->lParam = tvItem->lParam;
1179 /* If the application sets TVIF_INTEGRAL without
1180 * supplying a TVITEMEX structure, it's toast. */
1181 if (tvItem->mask & TVIF_INTEGRAL)
1182 wineItem->iIntegral = tvItem->iIntegral;
1184 if (tvItem->mask & TVIF_STATE)
1186 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1187 tvItem->stateMask);
1188 wineItem->state &= ~tvItem->stateMask;
1189 wineItem->state |= (tvItem->state & tvItem->stateMask);
1192 wineItem->callbackMask |= callbackSet;
1193 wineItem->callbackMask &= ~callbackClear;
1195 return TRUE;
1198 /* Note that the new item is pre-zeroed. */
1199 static LRESULT
1200 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1202 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1203 HTREEITEM insertAfter;
1204 TREEVIEW_ITEM *newItem, *parentItem;
1205 BOOL bTextUpdated = FALSE;
1207 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1209 parentItem = infoPtr->root;
1211 else
1213 parentItem = ptdi->hParent;
1215 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1217 WARN("invalid parent %p\n", parentItem);
1218 return 0;
1222 insertAfter = ptdi->hInsertAfter;
1224 /* Validate this now for convenience. */
1225 switch ((DWORD_PTR)insertAfter)
1227 case (DWORD_PTR)TVI_FIRST:
1228 case (DWORD_PTR)TVI_LAST:
1229 case (DWORD_PTR)TVI_SORT:
1230 break;
1232 default:
1233 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1234 insertAfter->parent != parentItem)
1236 WARN("invalid insert after %p\n", insertAfter);
1237 insertAfter = TVI_LAST;
1241 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1242 (tvItem->mask & TVIF_TEXT)
1243 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1244 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1245 : "<no label>");
1247 newItem = TREEVIEW_AllocateItem(infoPtr);
1248 if (newItem == NULL)
1249 return 0;
1251 newItem->parent = parentItem;
1252 newItem->iIntegral = 1;
1253 newItem->visibleOrder = -1;
1255 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1256 return 0;
1258 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1260 infoPtr->uNumItems++;
1262 switch ((DWORD_PTR)insertAfter)
1264 case (DWORD_PTR)TVI_FIRST:
1266 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1267 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1268 if (infoPtr->firstVisible == originalFirst)
1269 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1271 break;
1273 case (DWORD_PTR)TVI_LAST:
1274 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1275 break;
1277 /* hInsertAfter names a specific item we want to insert after */
1278 default:
1279 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1280 break;
1282 case (DWORD_PTR)TVI_SORT:
1284 TREEVIEW_ITEM *aChild;
1285 TREEVIEW_ITEM *previousChild = NULL;
1286 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1287 BOOL bItemInserted = FALSE;
1289 aChild = parentItem->firstChild;
1291 bTextUpdated = TRUE;
1292 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1294 /* Iterate the parent children to see where we fit in */
1295 while (aChild != NULL)
1297 INT comp;
1299 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1300 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1302 if (comp < 0) /* we are smaller than the current one */
1304 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1305 if (infoPtr->firstVisible == originalFirst &&
1306 aChild == originalFirst)
1307 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1308 bItemInserted = TRUE;
1309 break;
1311 else if (comp > 0) /* we are bigger than the current one */
1313 previousChild = aChild;
1315 /* This will help us to exit if there is no more sibling */
1316 aChild = (aChild->nextSibling == 0)
1317 ? NULL
1318 : aChild->nextSibling;
1320 /* Look at the next item */
1321 continue;
1323 else if (comp == 0)
1326 * An item with this name is already existing, therefore,
1327 * we add after the one we found
1329 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1330 bItemInserted = TRUE;
1331 break;
1336 * we reach the end of the child list and the item has not
1337 * yet been inserted, therefore, insert it after the last child.
1339 if ((!bItemInserted) && (aChild == NULL))
1340 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1342 break;
1347 TRACE("new item %p; parent %p, mask %x\n", newItem,
1348 newItem->parent, tvItem->mask);
1350 newItem->iLevel = newItem->parent->iLevel + 1;
1352 if (newItem->parent->cChildren == 0)
1353 newItem->parent->cChildren = 1;
1355 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1357 if (STATEIMAGEINDEX(newItem->state) == 0)
1358 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1361 if (infoPtr->firstVisible == NULL)
1362 infoPtr->firstVisible = newItem;
1364 TREEVIEW_VerifyTree(infoPtr);
1366 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1368 if (parentItem == infoPtr->root ||
1369 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1371 TREEVIEW_ITEM *item;
1372 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1374 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1375 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1377 if (!bTextUpdated)
1378 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1380 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1381 TREEVIEW_UpdateScrollBars(infoPtr);
1383 * if the item was inserted in a visible part of the tree,
1384 * invalidate it, as well as those after it
1386 for (item = newItem;
1387 item != NULL;
1388 item = TREEVIEW_GetNextListItem(infoPtr, item))
1389 TREEVIEW_Invalidate(infoPtr, item);
1391 else
1393 /* refresh treeview if newItem is the first item inserted under parentItem */
1394 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1396 /* parent got '+' - update it */
1397 TREEVIEW_Invalidate(infoPtr, parentItem);
1401 return (LRESULT)newItem;
1404 /* Item Deletion ************************************************************/
1405 static void
1406 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1408 static void
1409 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1411 TREEVIEW_ITEM *kill = parentItem->firstChild;
1413 while (kill != NULL)
1415 TREEVIEW_ITEM *next = kill->nextSibling;
1417 TREEVIEW_RemoveItem(infoPtr, kill);
1419 kill = next;
1422 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1423 assert(parentItem->firstChild == NULL);
1424 assert(parentItem->lastChild == NULL);
1427 static void
1428 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1430 TREEVIEW_ITEM *parentItem = item->parent;
1432 assert(item != NULL);
1433 assert(item->parent != NULL); /* i.e. it must not be the root */
1435 if (parentItem->firstChild == item)
1436 parentItem->firstChild = item->nextSibling;
1438 if (parentItem->lastChild == item)
1439 parentItem->lastChild = item->prevSibling;
1441 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1442 && parentItem->cChildren > 0)
1443 parentItem->cChildren = 0;
1445 if (item->prevSibling)
1446 item->prevSibling->nextSibling = item->nextSibling;
1448 if (item->nextSibling)
1449 item->nextSibling->prevSibling = item->prevSibling;
1452 static void
1453 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1455 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1457 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1458 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1460 if (wineItem->firstChild)
1461 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1463 TREEVIEW_UnlinkItem(wineItem);
1465 infoPtr->uNumItems--;
1467 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1468 Free(wineItem->pszText);
1470 TREEVIEW_FreeItem(infoPtr, wineItem);
1474 /* Empty out the tree. */
1475 static void
1476 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1478 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1480 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1483 static LRESULT
1484 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1486 TREEVIEW_ITEM *newSelection = NULL;
1487 TREEVIEW_ITEM *newFirstVisible = NULL;
1488 TREEVIEW_ITEM *parent, *prev = NULL;
1489 BOOL visible = FALSE;
1491 if (wineItem == TVI_ROOT)
1493 TRACE("TVI_ROOT\n");
1494 parent = infoPtr->root;
1495 newSelection = NULL;
1496 visible = TRUE;
1497 TREEVIEW_RemoveTree(infoPtr);
1499 else
1501 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1502 return FALSE;
1504 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1505 parent = wineItem->parent;
1507 if (ISVISIBLE(wineItem))
1509 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1510 visible = TRUE;
1513 if (infoPtr->selectedItem != NULL
1514 && (wineItem == infoPtr->selectedItem
1515 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1517 if (wineItem->nextSibling)
1518 newSelection = wineItem->nextSibling;
1519 else if (wineItem->parent != infoPtr->root)
1520 newSelection = wineItem->parent;
1521 else
1522 newSelection = wineItem->prevSibling;
1523 TRACE("newSelection = %p\n", newSelection);
1526 if (infoPtr->firstVisible == wineItem)
1528 if (wineItem->nextSibling)
1529 newFirstVisible = wineItem->nextSibling;
1530 else if (wineItem->prevSibling)
1531 newFirstVisible = wineItem->prevSibling;
1532 else if (wineItem->parent != infoPtr->root)
1533 newFirstVisible = wineItem->parent;
1534 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1536 else
1537 newFirstVisible = infoPtr->firstVisible;
1539 TREEVIEW_RemoveItem(infoPtr, wineItem);
1542 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1543 if (!infoPtr->selectedItem && newSelection)
1545 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1546 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1549 /* Validate insertMark dropItem.
1550 * hotItem ??? - used for comparison only.
1552 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1553 infoPtr->insertMarkItem = 0;
1555 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1556 infoPtr->dropItem = 0;
1558 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1559 newFirstVisible = infoPtr->root->firstChild;
1561 TREEVIEW_VerifyTree(infoPtr);
1563 if (!infoPtr->bRedraw) return TRUE;
1565 if (visible)
1567 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1568 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1569 TREEVIEW_UpdateScrollBars(infoPtr);
1570 TREEVIEW_Invalidate(infoPtr, NULL);
1572 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1574 /* parent lost '+/-' - update it */
1575 TREEVIEW_Invalidate(infoPtr, parent);
1578 return TRUE;
1582 /* Get/Set Messages *********************************************************/
1583 static LRESULT
1584 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1586 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1588 if (infoPtr->bRedraw)
1590 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1591 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1592 TREEVIEW_UpdateScrollBars(infoPtr);
1593 TREEVIEW_Invalidate(infoPtr, NULL);
1595 return 0;
1598 static LRESULT
1599 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1601 TRACE("\n");
1602 return infoPtr->uIndent;
1605 static LRESULT
1606 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1608 TRACE("\n");
1610 if (newIndent < MINIMUM_INDENT)
1611 newIndent = MINIMUM_INDENT;
1613 if (infoPtr->uIndent != newIndent)
1615 infoPtr->uIndent = newIndent;
1616 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1617 TREEVIEW_UpdateScrollBars(infoPtr);
1618 TREEVIEW_Invalidate(infoPtr, NULL);
1621 return 0;
1625 static LRESULT
1626 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1628 TRACE("\n");
1629 return (LRESULT)infoPtr->hwndToolTip;
1632 static LRESULT
1633 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1635 HWND prevToolTip;
1637 TRACE("\n");
1638 prevToolTip = infoPtr->hwndToolTip;
1639 infoPtr->hwndToolTip = hwndTT;
1641 return (LRESULT)prevToolTip;
1644 static LRESULT
1645 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1647 BOOL rc = infoPtr->bNtfUnicode;
1648 infoPtr->bNtfUnicode = fUnicode;
1649 return rc;
1652 static LRESULT
1653 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1655 return infoPtr->bNtfUnicode;
1658 static LRESULT
1659 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1661 return infoPtr->uScrollTime;
1664 static LRESULT
1665 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1667 UINT uOldScrollTime = infoPtr->uScrollTime;
1669 infoPtr->uScrollTime = min(uScrollTime, 100);
1671 return uOldScrollTime;
1675 static LRESULT
1676 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1678 TRACE("\n");
1680 switch (wParam)
1682 case TVSIL_NORMAL:
1683 return (LRESULT)infoPtr->himlNormal;
1685 case TVSIL_STATE:
1686 return (LRESULT)infoPtr->himlState;
1688 default:
1689 return 0;
1693 #define TVHEIGHT_MIN 16
1694 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1696 /* Compute the natural height for items. */
1697 static UINT
1698 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1700 TEXTMETRICW tm;
1701 HDC hdc = GetDC(0);
1702 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1703 UINT height;
1705 /* Height is the maximum of:
1706 * 16 (a hack because our fonts are tiny), and
1707 * The text height + border & margin, and
1708 * The size of the normal image list
1710 GetTextMetricsW(hdc, &tm);
1711 SelectObject(hdc, hOldFont);
1712 ReleaseDC(0, hdc);
1714 height = TVHEIGHT_MIN;
1715 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1716 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1717 if (height < infoPtr->normalImageHeight)
1718 height = infoPtr->normalImageHeight;
1720 /* Round down, unless we support odd ("non even") heights. */
1721 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1722 height &= ~1;
1724 return height;
1727 static LRESULT
1728 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1730 HIMAGELIST himlOld = 0;
1731 int oldWidth = infoPtr->normalImageWidth;
1732 int oldHeight = infoPtr->normalImageHeight;
1735 TRACE("%lx,%p\n", wParam, himlNew);
1737 switch (wParam)
1739 case TVSIL_NORMAL:
1740 himlOld = infoPtr->himlNormal;
1741 infoPtr->himlNormal = himlNew;
1743 if (himlNew != NULL)
1744 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1745 &infoPtr->normalImageHeight);
1746 else
1748 infoPtr->normalImageWidth = 0;
1749 infoPtr->normalImageHeight = 0;
1752 break;
1754 case TVSIL_STATE:
1755 himlOld = infoPtr->himlState;
1756 infoPtr->himlState = himlNew;
1758 if (himlNew != NULL)
1759 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1760 &infoPtr->stateImageHeight);
1761 else
1763 infoPtr->stateImageWidth = 0;
1764 infoPtr->stateImageHeight = 0;
1767 break;
1770 if (oldWidth != infoPtr->normalImageWidth ||
1771 oldHeight != infoPtr->normalImageHeight)
1773 BOOL bRecalcVisible = FALSE;
1775 if (oldHeight != infoPtr->normalImageHeight &&
1776 !infoPtr->bHeightSet)
1778 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1779 bRecalcVisible = TRUE;
1782 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1783 infoPtr->normalImageWidth != infoPtr->uIndent)
1785 infoPtr->uIndent = infoPtr->normalImageWidth;
1786 bRecalcVisible = TRUE;
1789 if (bRecalcVisible)
1790 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1792 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1793 TREEVIEW_UpdateScrollBars(infoPtr);
1796 TREEVIEW_Invalidate(infoPtr, NULL);
1798 return (LRESULT)himlOld;
1801 static LRESULT
1802 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1804 INT prevHeight = infoPtr->uItemHeight;
1806 TRACE("%d\n", newHeight);
1807 if (newHeight == -1)
1809 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1810 infoPtr->bHeightSet = FALSE;
1812 else
1814 infoPtr->uItemHeight = newHeight;
1815 infoPtr->bHeightSet = TRUE;
1818 /* Round down, unless we support odd ("non even") heights. */
1819 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1820 infoPtr->uItemHeight &= ~1;
1822 if (infoPtr->uItemHeight != prevHeight)
1824 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1825 TREEVIEW_UpdateScrollBars(infoPtr);
1826 TREEVIEW_Invalidate(infoPtr, NULL);
1829 return prevHeight;
1832 static LRESULT
1833 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1835 TRACE("\n");
1836 return infoPtr->uItemHeight;
1840 static LRESULT
1841 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1843 TRACE("%p\n", infoPtr->hFont);
1844 return (LRESULT)infoPtr->hFont;
1848 static INT CALLBACK
1849 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1851 (void)unused;
1853 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1855 return 1;
1858 static LRESULT
1859 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1861 UINT uHeight = infoPtr->uItemHeight;
1863 TRACE("%p %i\n", hFont, bRedraw);
1865 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1867 DeleteObject(infoPtr->hBoldFont);
1868 DeleteObject(infoPtr->hUnderlineFont);
1869 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1870 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1872 if (!infoPtr->bHeightSet)
1873 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1875 if (uHeight != infoPtr->uItemHeight)
1876 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1878 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1880 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1881 TREEVIEW_UpdateScrollBars(infoPtr);
1883 if (bRedraw)
1884 TREEVIEW_Invalidate(infoPtr, NULL);
1886 return 0;
1890 static LRESULT
1891 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1893 TRACE("\n");
1894 return (LRESULT)infoPtr->clrLine;
1897 static LRESULT
1898 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1900 COLORREF prevColor = infoPtr->clrLine;
1902 TRACE("\n");
1903 infoPtr->clrLine = color;
1904 return (LRESULT)prevColor;
1908 static LRESULT
1909 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1911 TRACE("\n");
1912 return (LRESULT)infoPtr->clrText;
1915 static LRESULT
1916 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1918 COLORREF prevColor = infoPtr->clrText;
1920 TRACE("\n");
1921 infoPtr->clrText = color;
1923 if (infoPtr->clrText != prevColor)
1924 TREEVIEW_Invalidate(infoPtr, NULL);
1926 return (LRESULT)prevColor;
1930 static LRESULT
1931 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1933 TRACE("\n");
1934 return (LRESULT)infoPtr->clrBk;
1937 static LRESULT
1938 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1940 COLORREF prevColor = infoPtr->clrBk;
1942 TRACE("\n");
1943 infoPtr->clrBk = newColor;
1945 if (newColor != prevColor)
1946 TREEVIEW_Invalidate(infoPtr, NULL);
1948 return (LRESULT)prevColor;
1952 static LRESULT
1953 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1955 TRACE("\n");
1956 return (LRESULT)infoPtr->clrInsertMark;
1959 static LRESULT
1960 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1962 COLORREF prevColor = infoPtr->clrInsertMark;
1964 TRACE("%x\n", color);
1965 infoPtr->clrInsertMark = color;
1967 return (LRESULT)prevColor;
1971 static LRESULT
1972 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1974 TRACE("%d %p\n", wParam, item);
1976 if (!TREEVIEW_ValidItem(infoPtr, item))
1977 return 0;
1979 infoPtr->insertBeforeorAfter = wParam;
1980 infoPtr->insertMarkItem = item;
1982 TREEVIEW_Invalidate(infoPtr, NULL);
1984 return 1;
1988 /************************************************************************
1989 * Some serious braindamage here. lParam is a pointer to both the
1990 * input HTREEITEM and the output RECT.
1992 static LRESULT
1993 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1995 TREEVIEW_ITEM *wineItem;
1996 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1998 TRACE("\n");
2000 * validate parameters
2002 if (pItem == NULL)
2003 return FALSE;
2005 wineItem = *pItem;
2006 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2007 return FALSE;
2010 * If wParam is TRUE return the text size otherwise return
2011 * the whole item size
2013 if (fTextRect)
2015 /* Windows does not send TVN_GETDISPINFO here. */
2017 lpRect->top = wineItem->rect.top;
2018 lpRect->bottom = wineItem->rect.bottom;
2020 lpRect->left = wineItem->textOffset;
2021 if (!wineItem->textWidth)
2022 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2024 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2026 else
2028 *lpRect = wineItem->rect;
2031 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2033 return TRUE;
2036 static inline LRESULT
2037 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2039 /* Surprise! This does not take integral height into account. */
2040 return infoPtr->clientHeight / infoPtr->uItemHeight;
2044 static LRESULT
2045 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2047 TREEVIEW_ITEM *wineItem;
2049 wineItem = tvItem->hItem;
2050 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2051 return FALSE;
2053 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2055 if (tvItem->mask & TVIF_CHILDREN)
2057 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2058 FIXME("I_CHILDRENCALLBACK not supported\n");
2059 tvItem->cChildren = wineItem->cChildren;
2062 if (tvItem->mask & TVIF_HANDLE)
2063 tvItem->hItem = wineItem;
2065 if (tvItem->mask & TVIF_IMAGE)
2066 tvItem->iImage = wineItem->iImage;
2068 if (tvItem->mask & TVIF_INTEGRAL)
2069 tvItem->iIntegral = wineItem->iIntegral;
2071 /* undocumented: windows ignores TVIF_PARAM and
2072 * * always sets lParam
2074 tvItem->lParam = wineItem->lParam;
2076 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2077 tvItem->iSelectedImage = wineItem->iSelectedImage;
2079 if (tvItem->mask & TVIF_STATE)
2080 /* Careful here - Windows ignores the stateMask when you get the state
2081 That contradicts the documentation, but makes more common sense, masking
2082 retrieval in this way seems overkill */
2083 tvItem->state = wineItem->state;
2085 if (tvItem->mask & TVIF_TEXT)
2087 if (wineItem->pszText == NULL)
2089 if (tvItem->cchTextMax > 0)
2090 tvItem->pszText[0] = '\0';
2092 else if (isW)
2094 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2096 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2097 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2099 else
2101 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2104 else
2106 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2108 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2109 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2111 else
2113 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2114 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2118 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2119 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2121 return TRUE;
2124 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2125 * which is wrong. */
2126 static LRESULT
2127 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2129 TREEVIEW_ITEM *wineItem;
2130 TREEVIEW_ITEM originalItem;
2132 wineItem = tvItem->hItem;
2134 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2135 tvItem->mask);
2137 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2138 return FALSE;
2140 /* store the original item values */
2141 originalItem = *wineItem;
2143 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2144 return FALSE;
2146 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2147 if ((tvItem->mask & TVIF_TEXT
2148 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2149 && ISVISIBLE(wineItem))
2151 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2152 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2155 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2157 /* The refresh updates everything, but we can't wait until then. */
2158 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2160 /* if any of the item's values changed and it's not a callback, redraw the item */
2161 if (item_changed(&originalItem, wineItem, tvItem))
2163 if (tvItem->mask & TVIF_INTEGRAL)
2165 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2166 TREEVIEW_UpdateScrollBars(infoPtr);
2168 TREEVIEW_Invalidate(infoPtr, NULL);
2170 else
2172 TREEVIEW_UpdateScrollBars(infoPtr);
2173 TREEVIEW_Invalidate(infoPtr, wineItem);
2178 return TRUE;
2181 static LRESULT
2182 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2184 TRACE("\n");
2186 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2187 return 0;
2189 return (wineItem->state & mask);
2192 static LRESULT
2193 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2195 TREEVIEW_ITEM *retval;
2197 retval = 0;
2199 /* handle all the global data here */
2200 switch (which)
2202 case TVGN_CHILD: /* Special case: child of 0 is root */
2203 if (wineItem)
2204 break;
2205 /* fall through */
2206 case TVGN_ROOT:
2207 retval = infoPtr->root->firstChild;
2208 break;
2210 case TVGN_CARET:
2211 retval = infoPtr->selectedItem;
2212 break;
2214 case TVGN_FIRSTVISIBLE:
2215 retval = infoPtr->firstVisible;
2216 break;
2218 case TVGN_DROPHILITE:
2219 retval = infoPtr->dropItem;
2220 break;
2222 case TVGN_LASTVISIBLE:
2223 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2224 break;
2227 if (retval)
2229 TRACE("flags:%x, returns %p\n", which, retval);
2230 return (LRESULT)retval;
2233 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2235 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2236 return FALSE;
2238 switch (which)
2240 case TVGN_NEXT:
2241 retval = wineItem->nextSibling;
2242 break;
2243 case TVGN_PREVIOUS:
2244 retval = wineItem->prevSibling;
2245 break;
2246 case TVGN_PARENT:
2247 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2248 break;
2249 case TVGN_CHILD:
2250 retval = wineItem->firstChild;
2251 break;
2252 case TVGN_NEXTVISIBLE:
2253 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2254 break;
2255 case TVGN_PREVIOUSVISIBLE:
2256 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2257 break;
2258 default:
2259 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2260 break;
2263 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2264 return (LRESULT)retval;
2268 static LRESULT
2269 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2271 TRACE(" %d\n", infoPtr->uNumItems);
2272 return (LRESULT)infoPtr->uNumItems;
2275 static VOID
2276 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2278 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2280 static const unsigned int state_table[] = { 0, 2, 1 };
2282 unsigned int state;
2284 state = STATEIMAGEINDEX(item->state);
2285 TRACE("state:%x\n", state);
2286 item->state &= ~TVIS_STATEIMAGEMASK;
2288 if (state < 3)
2289 state = state_table[state];
2291 item->state |= INDEXTOSTATEIMAGEMASK(state);
2293 TRACE("state:%x\n", state);
2294 TREEVIEW_Invalidate(infoPtr, item);
2299 /* Painting *************************************************************/
2301 /* Draw the lines and expand button for an item. Also draws one section
2302 * of the line from item's parent to item's parent's next sibling. */
2303 static void
2304 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2306 LONG centerx, centery;
2307 BOOL lar = ((infoPtr->dwStyle
2308 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2309 > TVS_LINESATROOT);
2310 HBRUSH hbr, hbrOld;
2311 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2313 if (!lar && item->iLevel == 0)
2314 return;
2316 hbr = CreateSolidBrush(clrBk);
2317 hbrOld = SelectObject(hdc, hbr);
2319 centerx = (item->linesOffset + item->stateOffset) / 2;
2320 centery = (item->rect.top + item->rect.bottom) / 2;
2322 if (infoPtr->dwStyle & TVS_HASLINES)
2324 HPEN hOldPen, hNewPen;
2325 HTREEITEM parent;
2326 LOGBRUSH lb;
2328 /* Get a dotted grey pen */
2329 lb.lbStyle = BS_SOLID;
2330 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2331 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2332 hOldPen = SelectObject(hdc, hNewPen);
2334 /* Make sure the center is on a dot (using +2 instead
2335 * of +1 gives us pixel-by-pixel compat with native) */
2336 centery = (centery + 2) & ~1;
2338 MoveToEx(hdc, item->stateOffset, centery, NULL);
2339 LineTo(hdc, centerx - 1, centery);
2341 if (item->prevSibling || item->parent != infoPtr->root)
2343 MoveToEx(hdc, centerx, item->rect.top, NULL);
2344 LineTo(hdc, centerx, centery);
2347 if (item->nextSibling)
2349 MoveToEx(hdc, centerx, centery, NULL);
2350 LineTo(hdc, centerx, item->rect.bottom + 1);
2353 /* Draw the line from our parent to its next sibling. */
2354 parent = item->parent;
2355 while (parent != infoPtr->root)
2357 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2359 if (parent->nextSibling
2360 /* skip top-levels unless TVS_LINESATROOT */
2361 && parent->stateOffset > parent->linesOffset)
2363 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2364 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2367 parent = parent->parent;
2370 SelectObject(hdc, hOldPen);
2371 DeleteObject(hNewPen);
2375 * Display the (+/-) signs
2378 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2380 if (item->cChildren)
2382 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2383 if (theme)
2385 RECT glyphRect = item->rect;
2386 glyphRect.left = item->linesOffset;
2387 glyphRect.right = item->stateOffset;
2388 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2389 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2390 &glyphRect, NULL);
2392 else
2394 LONG height = item->rect.bottom - item->rect.top;
2395 LONG width = item->stateOffset - item->linesOffset;
2396 LONG rectsize = min(height, width) / 4;
2397 /* plussize = ceil(rectsize * 3/4) */
2398 LONG plussize = (rectsize + 1) * 3 / 4;
2400 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2401 HPEN old_pen = SelectObject(hdc, new_pen);
2403 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2404 centerx + rectsize + 2, centery + rectsize + 2);
2406 SelectObject(hdc, old_pen);
2407 DeleteObject(new_pen);
2409 /* draw +/- signs with current text color */
2410 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2411 old_pen = SelectObject(hdc, new_pen);
2413 if (height < 18 || width < 18)
2415 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2416 LineTo(hdc, centerx + plussize, centery);
2418 if (!(item->state & TVIS_EXPANDED))
2420 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2421 LineTo(hdc, centerx, centery + plussize);
2424 else
2426 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2427 centerx + plussize, centery + 2);
2429 if (!(item->state & TVIS_EXPANDED))
2431 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2432 centerx + 2, centery + plussize);
2433 SetPixel(hdc, centerx - 1, centery, clrBk);
2434 SetPixel(hdc, centerx + 1, centery, clrBk);
2438 SelectObject(hdc, old_pen);
2439 DeleteObject(new_pen);
2443 SelectObject(hdc, hbrOld);
2444 DeleteObject(hbr);
2447 static void
2448 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2450 INT cditem;
2451 HFONT hOldFont;
2452 COLORREF oldTextColor, oldTextBkColor;
2453 int centery;
2454 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2455 NMTVCUSTOMDRAW nmcdhdr;
2457 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2459 /* - If item is drop target or it is selected and window is in focus -
2460 * use blue background (COLOR_HIGHLIGHT).
2461 * - If item is selected, window is not in focus, but it has style
2462 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2463 * - Otherwise - use background color
2465 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2466 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2467 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2469 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2471 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2472 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2474 else
2476 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2477 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2480 else
2482 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2483 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2484 nmcdhdr.clrText = comctl32_color.clrHighlight;
2485 else
2486 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2489 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2491 /* The custom draw handler can query the text rectangle,
2492 * so get ready. */
2493 /* should already be known, set to 0 when changed */
2494 if (!wineItem->textWidth)
2495 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2497 cditem = 0;
2499 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2501 cditem = TREEVIEW_SendCustomDrawItemNotify
2502 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2503 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2505 if (cditem & CDRF_SKIPDEFAULT)
2507 SelectObject(hdc, hOldFont);
2508 return;
2512 if (cditem & CDRF_NEWFONT)
2513 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2515 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2517 /* Set colors. Custom draw handler can change these so we do this after it. */
2518 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2519 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2521 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2524 * Display the images associated with this item
2527 INT imageIndex;
2529 /* State images are displayed to the left of the Normal image
2530 * image number is in state; zero should be `display no image'.
2532 imageIndex = STATEIMAGEINDEX(wineItem->state);
2534 if (infoPtr->himlState && imageIndex)
2536 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2537 wineItem->stateOffset,
2538 centery - infoPtr->stateImageHeight / 2,
2539 ILD_NORMAL);
2542 /* Now, draw the normal image; can be either selected or
2543 * non-selected image.
2546 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2548 /* The item is currently selected */
2549 imageIndex = wineItem->iSelectedImage;
2551 else
2553 /* The item is not selected */
2554 imageIndex = wineItem->iImage;
2557 if (infoPtr->himlNormal)
2559 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2561 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2562 wineItem->imageOffset,
2563 centery - infoPtr->normalImageHeight / 2,
2564 ILD_NORMAL | ovlIdx);
2570 * Display the text associated with this item
2573 /* Don't paint item's text if it's being edited */
2574 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2576 if (wineItem->pszText)
2578 RECT rcText;
2580 rcText.top = wineItem->rect.top;
2581 rcText.bottom = wineItem->rect.bottom;
2582 rcText.left = wineItem->textOffset;
2583 rcText.right = rcText.left + wineItem->textWidth + 4;
2585 TRACE("drawing text %s at (%s)\n",
2586 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2588 /* Draw it */
2589 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2590 ETO_CLIPPED | ETO_OPAQUE,
2591 &rcText,
2592 wineItem->pszText,
2593 lstrlenW(wineItem->pszText),
2594 NULL);
2596 /* Draw the box around the selected item */
2597 if ((wineItem == infoPtr->selectedItem) && inFocus)
2599 DrawFocusRect(hdc,&rcText);
2605 /* Draw insertion mark if necessary */
2607 if (infoPtr->insertMarkItem)
2608 TRACE("item:%d,mark:%p\n",
2609 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2610 infoPtr->insertMarkItem);
2612 if (wineItem == infoPtr->insertMarkItem)
2614 HPEN hNewPen, hOldPen;
2615 int offset;
2616 int left, right;
2618 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2619 hOldPen = SelectObject(hdc, hNewPen);
2621 if (infoPtr->insertBeforeorAfter)
2622 offset = wineItem->rect.bottom - 1;
2623 else
2624 offset = wineItem->rect.top + 1;
2626 left = wineItem->textOffset - 2;
2627 right = wineItem->textOffset + wineItem->textWidth + 2;
2629 MoveToEx(hdc, left, offset - 3, NULL);
2630 LineTo(hdc, left, offset + 4);
2632 MoveToEx(hdc, left, offset, NULL);
2633 LineTo(hdc, right + 1, offset);
2635 MoveToEx(hdc, right, offset + 3, NULL);
2636 LineTo(hdc, right, offset - 4);
2638 SelectObject(hdc, hOldPen);
2639 DeleteObject(hNewPen);
2642 if (cditem & CDRF_NOTIFYPOSTPAINT)
2644 cditem = TREEVIEW_SendCustomDrawItemNotify
2645 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2646 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2649 /* Restore the hdc state */
2650 SetTextColor(hdc, oldTextColor);
2651 SetBkColor(hdc, oldTextBkColor);
2652 SelectObject(hdc, hOldFont);
2655 /* Computes treeHeight and treeWidth and updates the scroll bars.
2657 static void
2658 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2660 TREEVIEW_ITEM *wineItem;
2661 HWND hwnd = infoPtr->hwnd;
2662 BOOL vert = FALSE;
2663 BOOL horz = FALSE;
2664 SCROLLINFO si;
2665 LONG scrollX = infoPtr->scrollX;
2667 infoPtr->treeWidth = 0;
2668 infoPtr->treeHeight = 0;
2670 /* We iterate through all visible items in order to get the tree height
2671 * and width */
2672 wineItem = infoPtr->root->firstChild;
2674 while (wineItem != NULL)
2676 if (ISVISIBLE(wineItem))
2678 /* actually we draw text at textOffset + 2 */
2679 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2680 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2682 /* This is scroll-adjusted, but we fix this below. */
2683 infoPtr->treeHeight = wineItem->rect.bottom;
2686 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2689 /* Fix the scroll adjusted treeHeight and treeWidth. */
2690 if (infoPtr->root->firstChild)
2691 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2693 infoPtr->treeWidth += infoPtr->scrollX;
2695 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2697 /* Adding one scroll bar may take up enough space that it forces us
2698 * to add the other as well. */
2699 if (infoPtr->treeHeight > infoPtr->clientHeight)
2701 vert = TRUE;
2703 if (infoPtr->treeWidth
2704 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2705 horz = TRUE;
2707 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2708 horz = TRUE;
2710 if (!vert && horz && infoPtr->treeHeight
2711 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2712 vert = TRUE;
2714 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2716 si.cbSize = sizeof(SCROLLINFO);
2717 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2718 si.nMin = 0;
2720 if (vert)
2722 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2723 if ( si.nPage && NULL != infoPtr->firstVisible)
2725 si.nPos = infoPtr->firstVisible->visibleOrder;
2726 si.nMax = infoPtr->maxVisibleOrder - 1;
2728 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2730 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2731 ShowScrollBar(hwnd, SB_VERT, TRUE);
2732 infoPtr->uInternalStatus |= TV_VSCROLL;
2734 else
2736 if (infoPtr->uInternalStatus & TV_VSCROLL)
2737 ShowScrollBar(hwnd, SB_VERT, FALSE);
2738 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2741 else
2743 if (infoPtr->uInternalStatus & TV_VSCROLL)
2744 ShowScrollBar(hwnd, SB_VERT, FALSE);
2745 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2748 if (horz)
2750 si.nPage = infoPtr->clientWidth;
2751 si.nPos = infoPtr->scrollX;
2752 si.nMax = infoPtr->treeWidth - 1;
2754 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2756 si.nPos = si.nMax - max( si.nPage-1, 0 );
2757 scrollX = si.nPos;
2760 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2761 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2762 infoPtr->uInternalStatus |= TV_HSCROLL;
2764 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2765 TREEVIEW_HScroll(infoPtr,
2766 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2768 else
2770 if (infoPtr->uInternalStatus & TV_HSCROLL)
2771 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2772 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2774 scrollX = 0;
2775 if (infoPtr->scrollX != 0)
2777 TREEVIEW_HScroll(infoPtr,
2778 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2782 if (!horz)
2783 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2786 static void
2787 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2789 HBRUSH hBrush;
2790 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2792 hBrush = CreateSolidBrush(clrBk);
2793 FillRect(hdc, rc, hBrush);
2794 DeleteObject(hBrush);
2797 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2798 static LRESULT
2799 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2801 RECT rect;
2803 TRACE("%p\n", infoPtr);
2805 GetClientRect(infoPtr->hwnd, &rect);
2806 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2808 return 1;
2811 static void
2812 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2814 HWND hwnd = infoPtr->hwnd;
2815 RECT rect = *rc;
2816 TREEVIEW_ITEM *wineItem;
2818 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2820 TRACE("empty window\n");
2821 return;
2824 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2825 hdc, rect);
2827 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2829 ReleaseDC(hwnd, hdc);
2830 return;
2833 for (wineItem = infoPtr->root->firstChild;
2834 wineItem != NULL;
2835 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2837 if (ISVISIBLE(wineItem))
2839 /* Avoid unneeded calculations */
2840 if (wineItem->rect.top > rect.bottom)
2841 break;
2842 if (wineItem->rect.bottom < rect.top)
2843 continue;
2845 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2849 TREEVIEW_UpdateScrollBars(infoPtr);
2851 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2852 infoPtr->cdmode =
2853 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2856 static inline void
2857 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2859 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2862 static void
2863 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2865 if (item)
2866 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2867 else
2868 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2871 static LRESULT
2872 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2874 HDC hdc;
2875 PAINTSTRUCT ps;
2876 RECT rc;
2878 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2880 if (hdc_ref)
2882 hdc = hdc_ref;
2883 GetClientRect(infoPtr->hwnd, &rc);
2885 else
2887 hdc = BeginPaint(infoPtr->hwnd, &ps);
2888 rc = ps.rcPaint;
2889 if(ps.fErase)
2890 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2893 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2894 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2896 if (!hdc_ref)
2897 EndPaint(infoPtr->hwnd, &ps);
2899 return 0;
2902 static LRESULT
2903 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2905 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2907 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2908 return 0;
2910 if (options & PRF_ERASEBKGND)
2911 TREEVIEW_EraseBackground(infoPtr, hdc);
2913 if (options & PRF_CLIENT)
2915 RECT rc;
2916 GetClientRect(infoPtr->hwnd, &rc);
2917 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2920 return 0;
2923 /* Sorting **************************************************************/
2925 /***************************************************************************
2926 * Forward the DPA local callback to the treeview owner callback
2928 static INT WINAPI
2929 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2930 const TVSORTCB *pCallBackSort)
2932 /* Forward the call to the client-defined callback */
2933 return pCallBackSort->lpfnCompare(first->lParam,
2934 second->lParam,
2935 pCallBackSort->lParam);
2938 /***************************************************************************
2939 * Treeview native sort routine: sort on item text.
2941 static INT WINAPI
2942 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2943 const TREEVIEW_INFO *infoPtr)
2945 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2946 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2948 if(first->pszText && second->pszText)
2949 return lstrcmpiW(first->pszText, second->pszText);
2950 else if(first->pszText)
2951 return -1;
2952 else if(second->pszText)
2953 return 1;
2954 else
2955 return 0;
2958 /* Returns the number of physical children belonging to item. */
2959 static INT
2960 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2962 INT cChildren = 0;
2963 HTREEITEM hti;
2965 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2966 cChildren++;
2968 return cChildren;
2971 /* Returns a DPA containing a pointer to each physical child of item in
2972 * sibling order. If item has no children, an empty DPA is returned. */
2973 static HDPA
2974 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
2976 HTREEITEM child = item->firstChild;
2978 HDPA list = DPA_Create(8);
2979 if (list == 0) return NULL;
2981 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2983 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2985 DPA_Destroy(list);
2986 return NULL;
2990 return list;
2993 /***************************************************************************
2994 * Setup the treeview structure with regards of the sort method
2995 * and sort the children of the TV item specified in lParam
2996 * fRecurse: currently unused. Should be zero.
2997 * parent: if pSort!=NULL, should equal pSort->hParent.
2998 * otherwise, item which child items are to be sorted.
2999 * pSort: sort method info. if NULL, sort on item text.
3000 * if non-NULL, sort on item's lParam content, and let the
3001 * application decide what that means. See also TVM_SORTCHILDRENCB.
3004 static LRESULT
3005 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3006 LPTVSORTCB pSort)
3008 INT cChildren;
3009 PFNDPACOMPARE pfnCompare;
3010 LPARAM lpCompare;
3012 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3013 if (parent == TVI_ROOT || parent == NULL)
3014 parent = infoPtr->root;
3016 /* Check for a valid handle to the parent item */
3017 if (!TREEVIEW_ValidItem(infoPtr, parent))
3019 ERR("invalid item hParent=%p\n", parent);
3020 return FALSE;
3023 if (pSort)
3025 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3026 lpCompare = (LPARAM)pSort;
3028 else
3030 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3031 lpCompare = (LPARAM)infoPtr;
3034 cChildren = TREEVIEW_CountChildren(parent);
3036 /* Make sure there is something to sort */
3037 if (cChildren > 1)
3039 /* TREEVIEW_ITEM rechaining */
3040 INT count = 0;
3041 HTREEITEM item = 0;
3042 HTREEITEM nextItem = 0;
3043 HTREEITEM prevItem = 0;
3045 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3047 if (sortList == NULL)
3048 return FALSE;
3050 /* let DPA sort the list */
3051 DPA_Sort(sortList, pfnCompare, lpCompare);
3053 /* The order of DPA entries has been changed, so fixup the
3054 * nextSibling and prevSibling pointers. */
3056 item = DPA_GetPtr(sortList, count++);
3057 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3059 /* link the two current item together */
3060 item->nextSibling = nextItem;
3061 nextItem->prevSibling = item;
3063 if (prevItem == NULL)
3065 /* this is the first item, update the parent */
3066 parent->firstChild = item;
3067 item->prevSibling = NULL;
3069 else
3071 /* fix the back chaining */
3072 item->prevSibling = prevItem;
3075 /* get ready for the next one */
3076 prevItem = item;
3077 item = nextItem;
3080 /* the last item is pointed to by item and never has a sibling */
3081 item->nextSibling = NULL;
3082 parent->lastChild = item;
3084 DPA_Destroy(sortList);
3086 TREEVIEW_VerifyTree(infoPtr);
3088 if (parent->state & TVIS_EXPANDED)
3090 int visOrder = infoPtr->firstVisible->visibleOrder;
3092 if (parent == infoPtr->root)
3093 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3094 else
3095 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3097 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3099 TREEVIEW_ITEM *item;
3101 for (item = infoPtr->root->firstChild; item != NULL;
3102 item = TREEVIEW_GetNextListItem(infoPtr, item))
3104 if (item->visibleOrder == visOrder)
3105 break;
3108 if (!item) item = parent->firstChild;
3109 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3112 TREEVIEW_Invalidate(infoPtr, NULL);
3115 return TRUE;
3117 return FALSE;
3121 /***************************************************************************
3122 * Setup the treeview structure with regards of the sort method
3123 * and sort the children of the TV item specified in lParam
3125 static LRESULT
3126 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3128 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3132 /***************************************************************************
3133 * Sort the children of the TV item specified in lParam.
3135 static LRESULT
3136 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3138 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3142 /* Expansion/Collapse ***************************************************/
3144 static BOOL
3145 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3146 UINT action)
3148 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3149 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3150 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3151 0, wineItem);
3154 static VOID
3155 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3156 UINT action)
3158 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3159 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3160 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3161 0, wineItem);
3165 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3166 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3167 static BOOL
3168 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3169 BOOL bRemoveChildren, BOOL bUser)
3171 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3172 BOOL bSetSelection, bSetFirstVisible;
3173 RECT scrollRect;
3174 LONG scrollDist = 0;
3175 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3177 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3179 if (!(wineItem->state & TVIS_EXPANDED))
3180 return FALSE;
3182 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3183 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3185 if (wineItem->firstChild == NULL)
3186 return FALSE;
3188 wineItem->state &= ~TVIS_EXPANDED;
3190 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3191 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3193 bSetSelection = (infoPtr->selectedItem != NULL
3194 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3196 bSetFirstVisible = (infoPtr->firstVisible != NULL
3197 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3199 tmpItem = wineItem;
3200 while (tmpItem)
3202 if (tmpItem->nextSibling)
3204 nextItem = tmpItem->nextSibling;
3205 break;
3207 tmpItem = tmpItem->parent;
3210 if (nextItem)
3211 scrollDist = nextItem->rect.top;
3213 if (bRemoveChildren)
3215 INT old_cChildren = wineItem->cChildren;
3216 TRACE("TVE_COLLAPSERESET\n");
3217 wineItem->state &= ~TVIS_EXPANDEDONCE;
3218 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3219 wineItem->cChildren = old_cChildren;
3222 if (wineItem->firstChild)
3224 TREEVIEW_ITEM *item, *sibling;
3226 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3228 for (item = wineItem->firstChild; item != sibling;
3229 item = TREEVIEW_GetNextListItem(infoPtr, item))
3231 item->visibleOrder = -1;
3235 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3237 if (nextItem)
3238 scrollDist = -(scrollDist - nextItem->rect.top);
3240 if (bSetSelection)
3242 /* Don't call DoSelectItem, it sends notifications. */
3243 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3244 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3245 wineItem->state |= TVIS_SELECTED;
3246 infoPtr->selectedItem = wineItem;
3249 TREEVIEW_UpdateScrollBars(infoPtr);
3251 scrollRect.left = 0;
3252 scrollRect.right = infoPtr->clientWidth;
3253 scrollRect.bottom = infoPtr->clientHeight;
3255 if (nextItem)
3257 scrollRect.top = nextItem->rect.top;
3259 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3260 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3261 TREEVIEW_Invalidate(infoPtr, wineItem);
3262 } else {
3263 scrollRect.top = wineItem->rect.top;
3264 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3267 TREEVIEW_SetFirstVisible(infoPtr,
3268 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3269 TRUE);
3271 return TRUE;
3274 static BOOL
3275 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3276 BOOL bExpandPartial, BOOL bUser)
3278 LONG scrollDist;
3279 LONG orgNextTop = 0;
3280 RECT scrollRect;
3281 TREEVIEW_ITEM *nextItem, *tmpItem;
3283 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, wineItem, bExpandPartial, bUser);
3285 if (wineItem->state & TVIS_EXPANDED)
3286 return TRUE;
3288 tmpItem = wineItem; nextItem = NULL;
3289 while (tmpItem)
3291 if (tmpItem->nextSibling)
3293 nextItem = tmpItem->nextSibling;
3294 break;
3296 tmpItem = tmpItem->parent;
3299 if (nextItem)
3300 orgNextTop = nextItem->rect.top;
3302 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3304 if (bUser || ((wineItem->cChildren != 0) &&
3305 !(wineItem->state & TVIS_EXPANDEDONCE)))
3307 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3309 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3310 return FALSE;
3313 if (!wineItem->firstChild)
3314 return FALSE;
3316 wineItem->state |= TVIS_EXPANDED;
3317 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3318 wineItem->state |= TVIS_EXPANDEDONCE;
3320 else
3322 if (!wineItem->firstChild)
3323 return FALSE;
3325 /* this item has already been expanded */
3326 wineItem->state |= TVIS_EXPANDED;
3329 if (bExpandPartial)
3330 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3332 if (ISVISIBLE(wineItem))
3334 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3335 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3336 TREEVIEW_UpdateScrollBars(infoPtr);
3338 scrollRect.left = 0;
3339 scrollRect.bottom = infoPtr->treeHeight;
3340 scrollRect.right = infoPtr->clientWidth;
3341 if (nextItem)
3343 scrollDist = nextItem->rect.top - orgNextTop;
3344 scrollRect.top = orgNextTop;
3346 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3347 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3348 TREEVIEW_Invalidate (infoPtr, wineItem);
3349 } else {
3350 scrollRect.top = wineItem->rect.top;
3351 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3354 /* Scroll up so that as many children as possible are visible.
3355 * This fails when expanding causes an HScroll bar to appear, but we
3356 * don't know that yet, so the last item is obscured. */
3357 if (wineItem->firstChild != NULL)
3359 int nChildren = wineItem->lastChild->visibleOrder
3360 - wineItem->firstChild->visibleOrder + 1;
3362 int visible_pos = wineItem->visibleOrder
3363 - infoPtr->firstVisible->visibleOrder;
3365 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3367 if (visible_pos > 0 && nChildren > rows_below)
3369 int scroll = nChildren - rows_below;
3371 if (scroll > visible_pos)
3372 scroll = visible_pos;
3374 if (scroll > 0)
3376 TREEVIEW_ITEM *newFirstVisible
3377 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3378 scroll);
3381 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3387 return TRUE;
3390 static BOOL
3391 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3393 TRACE("\n");
3395 if (wineItem->state & TVIS_EXPANDED)
3396 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3397 else
3398 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3401 static VOID
3402 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3404 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3406 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3408 if (TREEVIEW_HasChildren(infoPtr, item))
3409 TREEVIEW_ExpandAll(infoPtr, item);
3413 /* Note:If the specified item is the child of a collapsed parent item,
3414 the parent's list of child items is (recursively) expanded to reveal the
3415 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3416 know if it also applies here.
3419 static LRESULT
3420 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3422 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3423 return 0;
3425 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3426 TREEVIEW_ItemName(wineItem), TREEVIEW_GetItemIndex(infoPtr, wineItem),
3427 flag, wineItem->state);
3429 switch (flag & TVE_TOGGLE)
3431 case TVE_COLLAPSE:
3432 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3433 FALSE);
3435 case TVE_EXPAND:
3436 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3437 FALSE);
3439 case TVE_TOGGLE:
3440 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3442 default:
3443 return 0;
3447 /* Hit-Testing **********************************************************/
3449 static TREEVIEW_ITEM *
3450 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3452 TREEVIEW_ITEM *wineItem;
3453 LONG row;
3455 if (!infoPtr->firstVisible)
3456 return NULL;
3458 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3460 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3461 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3463 if (row >= wineItem->visibleOrder
3464 && row < wineItem->visibleOrder + wineItem->iIntegral)
3465 break;
3468 return wineItem;
3471 static LRESULT
3472 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3474 TREEVIEW_ITEM *wineItem;
3475 RECT rect;
3476 UINT status;
3477 LONG x, y;
3479 lpht->hItem = 0;
3480 GetClientRect(infoPtr->hwnd, &rect);
3481 status = 0;
3482 x = lpht->pt.x;
3483 y = lpht->pt.y;
3485 if (x < rect.left)
3487 status |= TVHT_TOLEFT;
3489 else if (x > rect.right)
3491 status |= TVHT_TORIGHT;
3494 if (y < rect.top)
3496 status |= TVHT_ABOVE;
3498 else if (y > rect.bottom)
3500 status |= TVHT_BELOW;
3503 if (status)
3505 lpht->flags = status;
3506 return 0;
3509 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3510 if (!wineItem)
3512 lpht->flags = TVHT_NOWHERE;
3513 return 0;
3516 if (x >= wineItem->textOffset + wineItem->textWidth)
3518 lpht->flags = TVHT_ONITEMRIGHT;
3520 else if (x >= wineItem->textOffset)
3522 lpht->flags = TVHT_ONITEMLABEL;
3524 else if (x >= wineItem->imageOffset)
3526 lpht->flags = TVHT_ONITEMICON;
3528 else if (x >= wineItem->stateOffset)
3530 lpht->flags = TVHT_ONITEMSTATEICON;
3532 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3534 lpht->flags = TVHT_ONITEMBUTTON;
3536 else
3538 lpht->flags = TVHT_ONITEMINDENT;
3541 lpht->hItem = wineItem;
3542 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3544 return (LRESULT)wineItem;
3547 /* Item Label Editing ***************************************************/
3549 static LRESULT
3550 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3552 return (LRESULT)infoPtr->hwndEdit;
3555 static LRESULT CALLBACK
3556 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3558 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3559 BOOL bCancel = FALSE;
3560 LRESULT rc;
3562 switch (uMsg)
3564 case WM_PAINT:
3565 TRACE("WM_PAINT start\n");
3566 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3567 lParam);
3568 TRACE("WM_PAINT done\n");
3569 return rc;
3571 case WM_KILLFOCUS:
3572 if (infoPtr->bIgnoreEditKillFocus)
3573 return TRUE;
3574 break;
3576 case WM_DESTROY:
3578 WNDPROC editProc = infoPtr->wpEditOrig;
3579 infoPtr->wpEditOrig = 0;
3580 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3581 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3584 case WM_GETDLGCODE:
3585 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3587 case WM_KEYDOWN:
3588 if (wParam == VK_ESCAPE)
3590 bCancel = TRUE;
3591 break;
3593 else if (wParam == VK_RETURN)
3595 break;
3598 /* fall through */
3599 default:
3600 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3603 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3604 /* eg. Using a messagebox */
3606 infoPtr->bIgnoreEditKillFocus = TRUE;
3607 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3608 infoPtr->bIgnoreEditKillFocus = FALSE;
3610 return 0;
3614 /* should handle edit control messages here */
3616 static LRESULT
3617 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3619 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3621 switch (HIWORD(wParam))
3623 case EN_UPDATE:
3626 * Adjust the edit window size
3628 WCHAR buffer[1024];
3629 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3630 HDC hdc = GetDC(infoPtr->hwndEdit);
3631 SIZE sz;
3632 HFONT hFont, hOldFont = 0;
3634 TRACE("edit=%p\n", infoPtr->hwndEdit);
3636 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3638 infoPtr->bLabelChanged = TRUE;
3640 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3642 /* Select font to get the right dimension of the string */
3643 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3645 if (hFont != 0)
3647 hOldFont = SelectObject(hdc, hFont);
3650 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3652 TEXTMETRICW textMetric;
3654 /* Add Extra spacing for the next character */
3655 GetTextMetricsW(hdc, &textMetric);
3656 sz.cx += (textMetric.tmMaxCharWidth * 2);
3658 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3659 sz.cx = min(sz.cx,
3660 infoPtr->clientWidth - editItem->textOffset + 2);
3662 SetWindowPos(infoPtr->hwndEdit,
3663 HWND_TOP,
3666 sz.cx,
3667 editItem->rect.bottom - editItem->rect.top + 3,
3668 SWP_NOMOVE | SWP_DRAWFRAME);
3671 if (hFont != 0)
3673 SelectObject(hdc, hOldFont);
3676 ReleaseDC(infoPtr->hwnd, hdc);
3677 break;
3679 case EN_KILLFOCUS:
3680 /* apparently we should respect passed handle value */
3681 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3683 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3684 break;
3686 default:
3687 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3690 return 0;
3693 static HWND
3694 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3696 HWND hwnd = infoPtr->hwnd;
3697 HWND hwndEdit;
3698 SIZE sz;
3699 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3700 HDC hdc;
3701 HFONT hOldFont=0;
3702 TEXTMETRICW textMetric;
3704 TRACE("%p %p\n", hwnd, hItem);
3705 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3706 return NULL;
3708 if (infoPtr->hwndEdit)
3709 return infoPtr->hwndEdit;
3711 infoPtr->bLabelChanged = FALSE;
3713 /* make edit item visible */
3714 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3716 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3718 hdc = GetDC(hwnd);
3719 /* Select the font to get appropriate metric dimensions */
3720 if (infoPtr->hFont != 0)
3722 hOldFont = SelectObject(hdc, infoPtr->hFont);
3725 /* Get string length in pixels */
3726 if (hItem->pszText)
3727 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3728 &sz);
3729 else
3730 GetTextExtentPoint32A(hdc, "", 0, &sz);
3732 /* Add Extra spacing for the next character */
3733 GetTextMetricsW(hdc, &textMetric);
3734 sz.cx += (textMetric.tmMaxCharWidth * 2);
3736 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3737 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3739 if (infoPtr->hFont != 0)
3741 SelectObject(hdc, hOldFont);
3744 ReleaseDC(hwnd, hdc);
3746 infoPtr->editItem = hItem;
3748 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3749 WC_EDITW,
3751 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3752 WS_CLIPSIBLINGS | ES_WANTRETURN |
3753 ES_LEFT, hItem->textOffset - 2,
3754 hItem->rect.top - 1, sz.cx + 3,
3755 hItem->rect.bottom -
3756 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3757 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3759 infoPtr->hwndEdit = hwndEdit;
3761 /* Get a 2D border. */
3762 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3763 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3764 SetWindowLongW(hwndEdit, GWL_STYLE,
3765 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3767 SendMessageW(hwndEdit, WM_SETFONT,
3768 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3770 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3771 (DWORD_PTR)
3772 TREEVIEW_Edit_SubclassProc);
3774 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3776 DestroyWindow(hwndEdit);
3777 infoPtr->hwndEdit = 0;
3778 infoPtr->editItem = NULL;
3779 return NULL;
3782 if (hItem->pszText)
3783 SetWindowTextW(hwndEdit, hItem->pszText);
3785 SetFocus(hwndEdit);
3786 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3787 ShowWindow(hwndEdit, SW_SHOW);
3789 return hwndEdit;
3793 static LRESULT
3794 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3796 HWND hwnd = infoPtr->hwnd;
3797 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3798 NMTVDISPINFOW tvdi;
3799 BOOL bCommit;
3800 WCHAR tmpText[1024] = { '\0' };
3801 WCHAR *newText = tmpText;
3802 int iLength = 0;
3804 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3806 tvdi.hdr.hwndFrom = hwnd;
3807 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3808 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3809 tvdi.item.mask = 0;
3810 tvdi.item.hItem = editedItem;
3811 tvdi.item.state = editedItem->state;
3812 tvdi.item.lParam = editedItem->lParam;
3814 if (!bCancel)
3816 if (!infoPtr->bNtfUnicode)
3817 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3818 else
3819 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3821 if (iLength >= 1023)
3823 ERR("Insufficient space to retrieve new item label\n");
3826 tvdi.item.mask = TVIF_TEXT;
3827 tvdi.item.pszText = tmpText;
3828 tvdi.item.cchTextMax = iLength + 1;
3830 else
3832 tvdi.item.pszText = NULL;
3833 tvdi.item.cchTextMax = 0;
3836 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3838 if (!bCancel && bCommit) /* Apply the changes */
3840 if (!infoPtr->bNtfUnicode)
3842 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3843 newText = Alloc(len * sizeof(WCHAR));
3844 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3845 iLength = len - 1;
3848 if (strcmpW(newText, editedItem->pszText) != 0)
3850 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3851 if (ptr == NULL)
3853 ERR("OutOfMemory, cannot allocate space for label\n");
3854 if(newText != tmpText) Free(newText);
3855 DestroyWindow(infoPtr->hwndEdit);
3856 infoPtr->hwndEdit = 0;
3857 infoPtr->editItem = NULL;
3858 return FALSE;
3860 else
3862 editedItem->pszText = ptr;
3863 editedItem->cchTextMax = iLength + 1;
3864 strcpyW(editedItem->pszText, newText);
3865 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3868 if(newText != tmpText) Free(newText);
3871 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3872 DestroyWindow(infoPtr->hwndEdit);
3873 infoPtr->hwndEdit = 0;
3874 infoPtr->editItem = NULL;
3875 return TRUE;
3878 static LRESULT
3879 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3881 if (wParam != TV_EDIT_TIMER)
3883 ERR("got unknown timer\n");
3884 return 1;
3887 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3888 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3890 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3892 return 0;
3896 /* Mouse Tracking/Drag **************************************************/
3898 /***************************************************************************
3899 * This is quite unusual piece of code, but that's how it's implemented in
3900 * Windows.
3902 static LRESULT
3903 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3905 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3906 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3907 RECT r;
3908 MSG msg;
3910 r.top = pt.y - cyDrag;
3911 r.left = pt.x - cxDrag;
3912 r.bottom = pt.y + cyDrag;
3913 r.right = pt.x + cxDrag;
3915 SetCapture(infoPtr->hwnd);
3917 while (1)
3919 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3921 if (msg.message == WM_MOUSEMOVE)
3923 pt.x = (short)LOWORD(msg.lParam);
3924 pt.y = (short)HIWORD(msg.lParam);
3925 if (PtInRect(&r, pt))
3926 continue;
3927 else
3929 ReleaseCapture();
3930 return 1;
3933 else if (msg.message >= WM_LBUTTONDOWN &&
3934 msg.message <= WM_RBUTTONDBLCLK)
3936 if (msg.message == WM_RBUTTONUP)
3937 TREEVIEW_RButtonUp(infoPtr, &pt);
3938 break;
3941 DispatchMessageW(&msg);
3944 if (GetCapture() != infoPtr->hwnd)
3945 return 0;
3948 ReleaseCapture();
3949 return 0;
3953 static LRESULT
3954 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3956 TREEVIEW_ITEM *wineItem;
3957 TVHITTESTINFO hit;
3959 TRACE("\n");
3960 SetFocus(infoPtr->hwnd);
3962 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3964 /* If there is pending 'edit label' event - kill it now */
3965 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3968 hit.pt.x = (short)LOWORD(lParam);
3969 hit.pt.y = (short)HIWORD(lParam);
3971 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3972 if (!wineItem)
3973 return 0;
3974 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3976 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3977 { /* FIXME! */
3978 switch (hit.flags)
3980 case TVHT_ONITEMRIGHT:
3981 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3982 break;
3984 case TVHT_ONITEMINDENT:
3985 if (!(infoPtr->dwStyle & TVS_HASLINES))
3987 break;
3989 else
3991 int level = hit.pt.x / infoPtr->uIndent;
3992 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3994 while (wineItem->iLevel > level)
3996 wineItem = wineItem->parent;
3999 /* fall through */
4002 case TVHT_ONITEMLABEL:
4003 case TVHT_ONITEMICON:
4004 case TVHT_ONITEMBUTTON:
4005 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4006 break;
4008 case TVHT_ONITEMSTATEICON:
4009 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4010 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4011 else
4012 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4013 break;
4016 return TRUE;
4020 static LRESULT
4021 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4023 HWND hwnd = infoPtr->hwnd;
4024 TVHITTESTINFO ht;
4025 BOOL bTrack, bDoLabelEdit;
4027 /* If Edit control is active - kill it and return.
4028 * The best way to do it is to set focus to itself.
4029 * Edit control subclassed procedure will automatically call
4030 * EndEditLabelNow.
4032 if (infoPtr->hwndEdit)
4034 SetFocus(hwnd);
4035 return 0;
4038 ht.pt.x = (short)LOWORD(lParam);
4039 ht.pt.y = (short)HIWORD(lParam);
4041 TREEVIEW_HitTest(infoPtr, &ht);
4042 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4044 /* update focusedItem and redraw both items */
4045 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4047 infoPtr->focusedItem = ht.hItem;
4048 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4049 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4052 bTrack = (ht.flags & TVHT_ONITEM)
4053 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4056 * If the style allows editing and the node is already selected
4057 * and the click occurred on the item label...
4059 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4060 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4062 /* Send NM_CLICK right away */
4063 if (!bTrack)
4064 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4065 goto setfocus;
4067 if (ht.flags & TVHT_ONITEMBUTTON)
4069 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4070 goto setfocus;
4072 else if (bTrack)
4073 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4074 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4076 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4077 infoPtr->dropItem = ht.hItem;
4079 /* clean up focusedItem as we dragged and won't select this item */
4080 if(infoPtr->focusedItem)
4082 /* refresh the item that was focused */
4083 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4084 infoPtr->focusedItem = NULL;
4086 /* refresh the selected item to return the filled background */
4087 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4090 return 0;
4094 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4095 goto setfocus;
4097 if (bDoLabelEdit)
4099 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4100 KillTimer(hwnd, TV_EDIT_TIMER);
4102 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4103 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4105 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4108 * if we are TVS_SINGLEEXPAND then we want this single click to
4109 * do a bunch of things.
4111 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4112 (infoPtr->hwndEdit == 0))
4114 TREEVIEW_ITEM *SelItem;
4117 * Send the notification
4119 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4122 * Close the previous selection all the way to the root
4123 * as long as the new selection is not a child
4125 if((infoPtr->selectedItem)
4126 && (infoPtr->selectedItem != ht.hItem))
4128 BOOL closeit = TRUE;
4129 SelItem = ht.hItem;
4131 /* determine if the hitItem is a child of the currently selected item */
4132 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4134 closeit = (SelItem != infoPtr->selectedItem);
4135 SelItem = SelItem->parent;
4138 if(closeit)
4140 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4141 SelItem = infoPtr->selectedItem;
4143 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4145 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4146 SelItem = SelItem->parent;
4152 * Expand the current item
4154 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4157 /* Select the current item */
4158 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4160 else if (ht.flags & TVHT_ONITEMSTATEICON)
4162 /* TVS_CHECKBOXES requires us to toggle the current state */
4163 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4164 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4167 setfocus:
4168 SetFocus(hwnd);
4169 return 0;
4173 static LRESULT
4174 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4176 TVHITTESTINFO ht;
4178 if (infoPtr->hwndEdit)
4180 SetFocus(infoPtr->hwnd);
4181 return 0;
4184 ht.pt.x = (short)LOWORD(lParam);
4185 ht.pt.y = (short)HIWORD(lParam);
4187 TREEVIEW_HitTest(infoPtr, &ht);
4189 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4191 if (ht.hItem)
4193 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4194 infoPtr->dropItem = ht.hItem;
4197 else
4199 SetFocus(infoPtr->hwnd);
4200 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4203 return 0;
4206 static LRESULT
4207 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4209 TVHITTESTINFO ht;
4211 ht.pt = *pPt;
4213 TREEVIEW_HitTest(infoPtr, &ht);
4215 if (ht.hItem)
4217 /* Change to screen coordinate for WM_CONTEXTMENU */
4218 ClientToScreen(infoPtr->hwnd, &ht.pt);
4220 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4221 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4222 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4224 return 0;
4228 static LRESULT
4229 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4231 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4232 INT cx, cy;
4233 HDC hdc, htopdc;
4234 HWND hwtop;
4235 HBITMAP hbmp, hOldbmp;
4236 SIZE size;
4237 RECT rc;
4238 HFONT hOldFont;
4240 TRACE("\n");
4242 if (!(infoPtr->himlNormal))
4243 return 0;
4245 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4246 return 0;
4248 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4250 hwtop = GetDesktopWindow();
4251 htopdc = GetDC(hwtop);
4252 hdc = CreateCompatibleDC(htopdc);
4254 hOldFont = SelectObject(hdc, infoPtr->hFont);
4256 if (dragItem->pszText)
4257 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4258 &size);
4259 else
4260 GetTextExtentPoint32A(hdc, "", 0, &size);
4262 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4263 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4264 hOldbmp = SelectObject(hdc, hbmp);
4266 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4267 size.cx += cx;
4268 if (cy > size.cy)
4269 size.cy = cy;
4271 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4272 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4273 ILD_NORMAL);
4276 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4277 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4280 /* draw item text */
4282 SetRect(&rc, cx, 0, size.cx, size.cy);
4284 if (dragItem->pszText)
4285 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4286 DT_LEFT);
4288 SelectObject(hdc, hOldFont);
4289 SelectObject(hdc, hOldbmp);
4291 ImageList_Add(infoPtr->dragList, hbmp, 0);
4293 DeleteDC(hdc);
4294 DeleteObject(hbmp);
4295 ReleaseDC(hwtop, htopdc);
4297 return (LRESULT)infoPtr->dragList;
4300 /* Selection ************************************************************/
4302 static LRESULT
4303 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4304 INT cause)
4306 TREEVIEW_ITEM *prevSelect;
4308 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4310 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4311 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4312 newSelect ? newSelect->state : 0);
4314 /* reset and redraw focusedItem if focusedItem was set so we don't */
4315 /* have to worry about the previously focused item when we set a new one */
4316 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4317 infoPtr->focusedItem = NULL;
4319 switch (action)
4321 case TVGN_CARET:
4322 prevSelect = infoPtr->selectedItem;
4324 if (prevSelect == newSelect) {
4325 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4326 break;
4329 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4330 TVN_SELCHANGINGW,
4331 cause,
4332 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4333 prevSelect,
4334 newSelect))
4335 return FALSE;
4337 if (prevSelect)
4338 prevSelect->state &= ~TVIS_SELECTED;
4339 if (newSelect)
4340 newSelect->state |= TVIS_SELECTED;
4342 infoPtr->selectedItem = newSelect;
4344 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4346 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4347 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4349 TREEVIEW_SendTreeviewNotify(infoPtr,
4350 TVN_SELCHANGEDW,
4351 cause,
4352 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4353 prevSelect,
4354 newSelect);
4355 break;
4357 case TVGN_DROPHILITE:
4358 prevSelect = infoPtr->dropItem;
4360 if (prevSelect)
4361 prevSelect->state &= ~TVIS_DROPHILITED;
4363 infoPtr->dropItem = newSelect;
4365 if (newSelect)
4366 newSelect->state |= TVIS_DROPHILITED;
4368 TREEVIEW_Invalidate(infoPtr, prevSelect);
4369 TREEVIEW_Invalidate(infoPtr, newSelect);
4370 break;
4372 case TVGN_FIRSTVISIBLE:
4373 if (newSelect != NULL)
4375 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4376 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4377 TREEVIEW_Invalidate(infoPtr, NULL);
4379 break;
4382 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4383 return TRUE;
4386 /* FIXME: handle NM_KILLFOCUS etc */
4387 static LRESULT
4388 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4390 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4391 return FALSE;
4393 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4395 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4396 return FALSE;
4398 return TRUE;
4401 /*************************************************************************
4402 * TREEVIEW_ProcessLetterKeys
4404 * Processes keyboard messages generated by pressing the letter keys
4405 * on the keyboard.
4406 * What this does is perform a case insensitive search from the
4407 * current position with the following quirks:
4408 * - If two chars or more are pressed in quick succession we search
4409 * for the corresponding string (e.g. 'abc').
4410 * - If there is a delay we wipe away the current search string and
4411 * restart with just that char.
4412 * - If the user keeps pressing the same character, whether slowly or
4413 * fast, so that the search string is entirely composed of this
4414 * character ('aaaaa' for instance), then we search for first item
4415 * that starting with that character.
4416 * - If the user types the above character in quick succession, then
4417 * we must also search for the corresponding string ('aaaaa'), and
4418 * go to that string if there is a match.
4420 * RETURNS
4422 * Zero.
4424 * BUGS
4426 * - The current implementation has a list of characters it will
4427 * accept and it ignores everything else. In particular it will
4428 * ignore accentuated characters which seems to match what
4429 * Windows does. But I'm not sure it makes sense to follow
4430 * Windows there.
4431 * - We don't sound a beep when the search fails.
4432 * - The search should start from the focused item, not from the selected
4433 * item. One reason for this is to allow for multiple selections in trees.
4434 * But currently infoPtr->focusedItem does not seem very usable.
4436 * SEE ALSO
4438 * TREEVIEW_ProcessLetterKeys
4440 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4442 HTREEITEM nItem;
4443 HTREEITEM endidx,idx;
4444 TVITEMEXW item;
4445 WCHAR buffer[MAX_PATH];
4446 DWORD timestamp,elapsed;
4448 /* simple parameter checking */
4449 if (!charCode || !keyData) return 0;
4451 /* only allow the valid WM_CHARs through */
4452 if (!isalnum(charCode) &&
4453 charCode != '.' && charCode != '`' && charCode != '!' &&
4454 charCode != '@' && charCode != '#' && charCode != '$' &&
4455 charCode != '%' && charCode != '^' && charCode != '&' &&
4456 charCode != '*' && charCode != '(' && charCode != ')' &&
4457 charCode != '-' && charCode != '_' && charCode != '+' &&
4458 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4459 charCode != '}' && charCode != '[' && charCode != '{' &&
4460 charCode != '/' && charCode != '?' && charCode != '>' &&
4461 charCode != '<' && charCode != ',' && charCode != '~')
4462 return 0;
4464 /* compute how much time elapsed since last keypress */
4465 timestamp = GetTickCount();
4466 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4467 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4468 } else {
4469 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4472 /* update the search parameters */
4473 infoPtr->lastKeyPressTimestamp=timestamp;
4474 if (elapsed < KEY_DELAY) {
4475 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4476 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4478 if (infoPtr->charCode != charCode) {
4479 infoPtr->charCode=charCode=0;
4481 } else {
4482 infoPtr->charCode=charCode;
4483 infoPtr->szSearchParam[0]=charCode;
4484 infoPtr->nSearchParamLength=1;
4485 /* Redundant with the 1 char string */
4486 charCode=0;
4489 /* and search from the current position */
4490 nItem=NULL;
4491 if (infoPtr->selectedItem != NULL) {
4492 endidx=infoPtr->selectedItem;
4493 /* if looking for single character match,
4494 * then we must always move forward
4496 if (infoPtr->nSearchParamLength == 1)
4497 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4498 else
4499 idx=endidx;
4500 } else {
4501 endidx=NULL;
4502 idx=infoPtr->root->firstChild;
4504 do {
4505 /* At the end point, sort out wrapping */
4506 if (idx == NULL) {
4508 /* If endidx is null, stop at the last item (ie top to bottom) */
4509 if (endidx == NULL)
4510 break;
4512 /* Otherwise, start again at the very beginning */
4513 idx=infoPtr->root->firstChild;
4515 /* But if we are stopping on the first child, end now! */
4516 if (idx == endidx) break;
4519 /* get item */
4520 ZeroMemory(&item, sizeof(item));
4521 item.mask = TVIF_TEXT;
4522 item.hItem = idx;
4523 item.pszText = buffer;
4524 item.cchTextMax = sizeof(buffer);
4525 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4527 /* check for a match */
4528 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4529 nItem=idx;
4530 break;
4531 } else if ( (charCode != 0) && (nItem == NULL) &&
4532 (nItem != infoPtr->selectedItem) &&
4533 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4534 /* This would work but we must keep looking for a longer match */
4535 nItem=idx;
4537 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4538 } while (idx != endidx);
4540 if (nItem != NULL) {
4541 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4542 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4546 return 0;
4549 /* Scrolling ************************************************************/
4551 static LRESULT
4552 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4554 int viscount;
4555 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4556 HTREEITEM newFirstVisible = NULL;
4557 int visible_pos = -1;
4559 if (!TREEVIEW_ValidItem(infoPtr, item))
4560 return FALSE;
4562 if (!ISVISIBLE(item))
4564 /* Expand parents as necessary. */
4565 HTREEITEM parent;
4567 /* see if we are trying to ensure that root is visible */
4568 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4569 parent = item->parent;
4570 else
4571 parent = item; /* this item is the topmost item */
4573 while (parent != infoPtr->root)
4575 if (!(parent->state & TVIS_EXPANDED))
4576 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4578 parent = parent->parent;
4582 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4584 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4585 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4587 if (hasFirstVisible)
4588 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4590 if (visible_pos < 0)
4592 /* item is before the start of the list: put it at the top. */
4593 newFirstVisible = item;
4595 else if (visible_pos >= viscount
4596 /* Sometimes, before we are displayed, GVC is 0, causing us to
4597 * spuriously scroll up. */
4598 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4600 /* item is past the end of the list. */
4601 int scroll = visible_pos - viscount;
4603 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4604 scroll + 1);
4607 if (bHScroll)
4609 /* Scroll window so item's text is visible as much as possible */
4610 /* Calculation of amount of extra space is taken from EditLabel code */
4611 INT pos, x;
4612 TEXTMETRICW textMetric;
4613 HDC hdc = GetWindowDC(infoPtr->hwnd);
4615 x = item->textWidth;
4617 GetTextMetricsW(hdc, &textMetric);
4618 ReleaseDC(infoPtr->hwnd, hdc);
4620 x += (textMetric.tmMaxCharWidth * 2);
4621 x = max(x, textMetric.tmMaxCharWidth * 3);
4623 if (item->textOffset < 0)
4624 pos = item->textOffset;
4625 else if (item->textOffset + x > infoPtr->clientWidth)
4627 if (x > infoPtr->clientWidth)
4628 pos = item->textOffset;
4629 else
4630 pos = item->textOffset + x - infoPtr->clientWidth;
4632 else
4633 pos = 0;
4635 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4638 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4640 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4642 return TRUE;
4645 return FALSE;
4648 static VOID
4649 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4650 TREEVIEW_ITEM *newFirstVisible,
4651 BOOL bUpdateScrollPos)
4653 int gap_size;
4655 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4657 if (newFirstVisible != NULL)
4659 /* Prevent an empty gap from appearing at the bottom... */
4660 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4661 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4663 if (gap_size > 0)
4665 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4666 -gap_size);
4668 /* ... unless we just don't have enough items. */
4669 if (newFirstVisible == NULL)
4670 newFirstVisible = infoPtr->root->firstChild;
4674 if (infoPtr->firstVisible != newFirstVisible)
4676 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4678 infoPtr->firstVisible = newFirstVisible;
4679 TREEVIEW_Invalidate(infoPtr, NULL);
4681 else
4683 TREEVIEW_ITEM *item;
4684 int scroll = infoPtr->uItemHeight *
4685 (infoPtr->firstVisible->visibleOrder
4686 - newFirstVisible->visibleOrder);
4688 infoPtr->firstVisible = newFirstVisible;
4690 for (item = infoPtr->root->firstChild; item != NULL;
4691 item = TREEVIEW_GetNextListItem(infoPtr, item))
4693 item->rect.top += scroll;
4694 item->rect.bottom += scroll;
4697 if (bUpdateScrollPos)
4698 SetScrollPos(infoPtr->hwnd, SB_VERT,
4699 newFirstVisible->visibleOrder, TRUE);
4701 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4706 /************************************************************************
4707 * VScroll is always in units of visible items. i.e. we always have a
4708 * visible item aligned to the top of the control. (Unless we have no
4709 * items at all.)
4711 static LRESULT
4712 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4714 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4715 TREEVIEW_ITEM *newFirstVisible = NULL;
4717 int nScrollCode = LOWORD(wParam);
4719 TRACE("wp %lx\n", wParam);
4721 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4722 return 0;
4724 if (!oldFirstVisible)
4726 assert(infoPtr->root->firstChild == NULL);
4727 return 0;
4730 switch (nScrollCode)
4732 case SB_TOP:
4733 newFirstVisible = infoPtr->root->firstChild;
4734 break;
4736 case SB_BOTTOM:
4737 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4738 break;
4740 case SB_LINEUP:
4741 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4742 break;
4744 case SB_LINEDOWN:
4745 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4746 break;
4748 case SB_PAGEUP:
4749 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4750 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4751 break;
4753 case SB_PAGEDOWN:
4754 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4755 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4756 break;
4758 case SB_THUMBTRACK:
4759 case SB_THUMBPOSITION:
4760 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4761 infoPtr->root->firstChild,
4762 (LONG)(SHORT)HIWORD(wParam));
4763 break;
4765 case SB_ENDSCROLL:
4766 return 0;
4769 if (newFirstVisible != NULL)
4771 if (newFirstVisible != oldFirstVisible)
4772 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4773 nScrollCode != SB_THUMBTRACK);
4774 else if (nScrollCode == SB_THUMBPOSITION)
4775 SetScrollPos(infoPtr->hwnd, SB_VERT,
4776 newFirstVisible->visibleOrder, TRUE);
4779 return 0;
4782 static LRESULT
4783 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4785 int maxWidth;
4786 int scrollX = infoPtr->scrollX;
4787 int nScrollCode = LOWORD(wParam);
4789 TRACE("wp %lx\n", wParam);
4791 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4792 return FALSE;
4794 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4795 /* shall never occur */
4796 if (maxWidth <= 0)
4798 scrollX = 0;
4799 goto scroll;
4802 switch (nScrollCode)
4804 case SB_LINELEFT:
4805 scrollX -= infoPtr->uItemHeight;
4806 break;
4807 case SB_LINERIGHT:
4808 scrollX += infoPtr->uItemHeight;
4809 break;
4810 case SB_PAGELEFT:
4811 scrollX -= infoPtr->clientWidth;
4812 break;
4813 case SB_PAGERIGHT:
4814 scrollX += infoPtr->clientWidth;
4815 break;
4817 case SB_THUMBTRACK:
4818 case SB_THUMBPOSITION:
4819 scrollX = (int)(SHORT)HIWORD(wParam);
4820 break;
4822 case SB_ENDSCROLL:
4823 return 0;
4826 if (scrollX > maxWidth)
4827 scrollX = maxWidth;
4828 else if (scrollX < 0)
4829 scrollX = 0;
4831 scroll:
4832 if (scrollX != infoPtr->scrollX)
4834 TREEVIEW_ITEM *item;
4835 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4837 for (item = infoPtr->root->firstChild; item != NULL;
4838 item = TREEVIEW_GetNextListItem(infoPtr, item))
4840 item->linesOffset += scroll_pixels;
4841 item->stateOffset += scroll_pixels;
4842 item->imageOffset += scroll_pixels;
4843 item->textOffset += scroll_pixels;
4846 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4847 infoPtr->scrollX = scrollX;
4848 UpdateWindow(infoPtr->hwnd);
4851 if (nScrollCode != SB_THUMBTRACK)
4852 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4854 return 0;
4857 static LRESULT
4858 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4860 short gcWheelDelta;
4861 UINT pulScrollLines = 3;
4863 if (wParam & (MK_SHIFT | MK_CONTROL))
4864 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4866 if (infoPtr->firstVisible == NULL)
4867 return TRUE;
4869 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4871 gcWheelDelta = -(short)HIWORD(wParam);
4872 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4874 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4876 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4877 int maxDy = infoPtr->maxVisibleOrder;
4879 if (newDy > maxDy)
4880 newDy = maxDy;
4882 if (newDy < 0)
4883 newDy = 0;
4885 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4887 return TRUE;
4890 /* Create/Destroy *******************************************************/
4892 static void
4893 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4895 RECT rc;
4896 HBITMAP hbm, hbmOld;
4897 HDC hdc, hdcScreen;
4898 int nIndex;
4900 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4902 hdcScreen = GetDC(0);
4904 hdc = CreateCompatibleDC(hdcScreen);
4905 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4906 hbmOld = SelectObject(hdc, hbm);
4908 SetRect(&rc, 0, 0, 48, 16);
4909 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4911 SetRect(&rc, 18, 2, 30, 14);
4912 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4913 DFCS_BUTTONCHECK|DFCS_FLAT);
4915 SetRect(&rc, 34, 2, 46, 14);
4916 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4917 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4919 SelectObject(hdc, hbmOld);
4920 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4921 comctl32_color.clrWindow);
4922 TRACE("checkbox index %d\n", nIndex);
4924 DeleteObject(hbm);
4925 DeleteDC(hdc);
4926 ReleaseDC(0, hdcScreen);
4928 infoPtr->stateImageWidth = 16;
4929 infoPtr->stateImageHeight = 16;
4932 static LRESULT
4933 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4935 RECT rcClient;
4936 TREEVIEW_INFO *infoPtr;
4937 LOGFONTW lf;
4939 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4941 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4943 if (infoPtr == NULL)
4945 ERR("could not allocate info memory!\n");
4946 return 0;
4949 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4951 infoPtr->hwnd = hwnd;
4952 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4953 infoPtr->Timer = 0;
4954 infoPtr->uNumItems = 0;
4955 infoPtr->cdmode = 0;
4956 infoPtr->uScrollTime = 300; /* milliseconds */
4957 infoPtr->bRedraw = TRUE;
4959 GetClientRect(hwnd, &rcClient);
4961 /* No scroll bars yet. */
4962 infoPtr->clientWidth = rcClient.right;
4963 infoPtr->clientHeight = rcClient.bottom;
4964 infoPtr->uInternalStatus = 0;
4966 infoPtr->treeWidth = 0;
4967 infoPtr->treeHeight = 0;
4969 infoPtr->uIndent = MINIMUM_INDENT;
4970 infoPtr->selectedItem = NULL;
4971 infoPtr->focusedItem = NULL;
4972 infoPtr->hotItem = NULL;
4973 infoPtr->editItem = NULL;
4974 infoPtr->firstVisible = NULL;
4975 infoPtr->maxVisibleOrder = 0;
4976 infoPtr->dropItem = NULL;
4977 infoPtr->insertMarkItem = NULL;
4978 infoPtr->insertBeforeorAfter = 0;
4979 /* dragList */
4981 infoPtr->scrollX = 0;
4983 infoPtr->clrBk = CLR_NONE; /* use system color */
4984 infoPtr->clrText = CLR_NONE; /* use system color */
4985 infoPtr->clrLine = CLR_DEFAULT;
4986 infoPtr->clrInsertMark = CLR_DEFAULT;
4988 /* hwndToolTip */
4990 infoPtr->hwndEdit = NULL;
4991 infoPtr->wpEditOrig = NULL;
4992 infoPtr->bIgnoreEditKillFocus = FALSE;
4993 infoPtr->bLabelChanged = FALSE;
4995 infoPtr->himlNormal = NULL;
4996 infoPtr->himlState = NULL;
4997 infoPtr->normalImageWidth = 0;
4998 infoPtr->normalImageHeight = 0;
4999 infoPtr->stateImageWidth = 0;
5000 infoPtr->stateImageHeight = 0;
5002 infoPtr->items = DPA_Create(16);
5004 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5005 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5006 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5007 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5008 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5010 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5012 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5013 infoPtr->root->state = TVIS_EXPANDED;
5014 infoPtr->root->iLevel = -1;
5015 infoPtr->root->visibleOrder = -1;
5017 infoPtr->hwndNotify = lpcs->hwndParent;
5018 infoPtr->hwndToolTip = 0;
5020 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5022 /* Determine what type of notify should be issued */
5023 /* sets infoPtr->bNtfUnicode */
5024 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5026 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5027 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5028 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5029 hwnd, 0, 0, 0);
5031 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5032 initialize_checkboxes(infoPtr);
5034 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5035 ShowScrollBar(hwnd, SB_VERT, FALSE);
5036 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5038 OpenThemeData (hwnd, themeClass);
5040 return 0;
5044 static LRESULT
5045 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5047 TRACE("\n");
5049 /* free item data */
5050 TREEVIEW_RemoveTree(infoPtr);
5051 /* root isn't freed with other items */
5052 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5053 DPA_Destroy(infoPtr->items);
5055 /* tool tip is automatically destroyed: we are its owner */
5057 /* Restore original wndproc */
5058 if (infoPtr->hwndEdit)
5059 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5060 (DWORD_PTR)infoPtr->wpEditOrig);
5062 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5064 /* Deassociate treeview from the window before doing anything drastic. */
5065 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5068 DeleteObject(infoPtr->hDefaultFont);
5069 DeleteObject(infoPtr->hBoldFont);
5070 DeleteObject(infoPtr->hUnderlineFont);
5071 Free(infoPtr);
5073 return 0;
5076 /* Miscellaneous Messages ***********************************************/
5078 static LRESULT
5079 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5081 static const struct
5083 unsigned char code;
5085 scroll[] =
5087 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5088 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5089 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5090 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5091 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5092 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5093 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5094 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5095 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5096 #undef SCROLL_ENTRY
5099 if (key >= VK_PRIOR && key <= VK_DOWN)
5101 unsigned char code = scroll[key - VK_PRIOR].code;
5103 (((code & (1 << 7)) == (SB_HORZ << 7))
5104 ? TREEVIEW_HScroll
5105 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5108 return 0;
5111 /************************************************************************
5112 * TREEVIEW_KeyDown
5114 * VK_UP Move selection to the previous non-hidden item.
5115 * VK_DOWN Move selection to the next non-hidden item.
5116 * VK_HOME Move selection to the first item.
5117 * VK_END Move selection to the last item.
5118 * VK_LEFT If expanded then collapse, otherwise move to parent.
5119 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5120 * VK_ADD Expand.
5121 * VK_SUBTRACT Collapse.
5122 * VK_MULTIPLY Expand all.
5123 * VK_PRIOR Move up GetVisibleCount items.
5124 * VK_NEXT Move down GetVisibleCount items.
5125 * VK_BACK Move to parent.
5126 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5128 static LRESULT
5129 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5131 /* If it is non-NULL and different, it will be selected and visible. */
5132 TREEVIEW_ITEM *newSelection = NULL;
5134 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5136 TRACE("%lx\n", wParam);
5138 if (prevItem == NULL)
5139 return FALSE;
5141 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5142 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5144 switch (wParam)
5146 case VK_UP:
5147 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5148 if (!newSelection)
5149 newSelection = infoPtr->root->firstChild;
5150 break;
5152 case VK_DOWN:
5153 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5154 break;
5156 case VK_HOME:
5157 newSelection = infoPtr->root->firstChild;
5158 break;
5160 case VK_END:
5161 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5162 break;
5164 case VK_LEFT:
5165 if (prevItem->state & TVIS_EXPANDED)
5167 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5169 else if (prevItem->parent != infoPtr->root)
5171 newSelection = prevItem->parent;
5173 break;
5175 case VK_RIGHT:
5176 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5178 if (!(prevItem->state & TVIS_EXPANDED))
5179 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5180 else
5182 newSelection = prevItem->firstChild;
5186 break;
5188 case VK_MULTIPLY:
5189 TREEVIEW_ExpandAll(infoPtr, prevItem);
5190 break;
5192 case VK_ADD:
5193 if (!(prevItem->state & TVIS_EXPANDED))
5194 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5195 break;
5197 case VK_SUBTRACT:
5198 if (prevItem->state & TVIS_EXPANDED)
5199 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5200 break;
5202 case VK_PRIOR:
5203 newSelection
5204 = TREEVIEW_GetListItem(infoPtr, prevItem,
5205 -TREEVIEW_GetVisibleCount(infoPtr));
5206 break;
5208 case VK_NEXT:
5209 newSelection
5210 = TREEVIEW_GetListItem(infoPtr, prevItem,
5211 TREEVIEW_GetVisibleCount(infoPtr));
5212 break;
5214 case VK_BACK:
5215 newSelection = prevItem->parent;
5216 if (newSelection == infoPtr->root)
5217 newSelection = NULL;
5218 break;
5220 case VK_SPACE:
5221 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5222 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5223 break;
5226 if (newSelection && newSelection != prevItem)
5228 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5229 TVC_BYKEYBOARD))
5231 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5235 return FALSE;
5238 static LRESULT
5239 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5241 /* remove hot effect from item */
5242 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5243 infoPtr->hotItem = NULL;
5245 return 0;
5248 static LRESULT
5249 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5251 POINT pt;
5252 TRACKMOUSEEVENT trackinfo;
5253 TREEVIEW_ITEM * item;
5255 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5257 /* fill in the TRACKMOUSEEVENT struct */
5258 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5259 trackinfo.dwFlags = TME_QUERY;
5260 trackinfo.hwndTrack = infoPtr->hwnd;
5262 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5263 _TrackMouseEvent(&trackinfo);
5265 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5266 if(!(trackinfo.dwFlags & TME_LEAVE))
5268 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5269 trackinfo.hwndTrack = infoPtr->hwnd;
5270 /* do it as fast as possible, minimal systimer latency will be used */
5271 trackinfo.dwHoverTime = 1;
5273 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5274 /* and can properly deactivate the hot item */
5275 _TrackMouseEvent(&trackinfo);
5278 pt.x = (short)LOWORD(lParam);
5279 pt.y = (short)HIWORD(lParam);
5281 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5283 if (item != infoPtr->hotItem)
5285 /* redraw old hot item */
5286 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5287 infoPtr->hotItem = item;
5288 /* redraw new hot item */
5289 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5292 return 0;
5295 /* Draw themed border */
5296 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5298 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5299 HDC dc;
5300 RECT r;
5301 HRGN cliprgn;
5302 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5303 cyEdge = GetSystemMetrics (SM_CYEDGE);
5305 if (!theme)
5306 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5308 GetWindowRect(infoPtr->hwnd, &r);
5310 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5311 r.right - cxEdge, r.bottom - cyEdge);
5312 if (region != (HRGN)1)
5313 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5314 OffsetRect(&r, -r.left, -r.top);
5316 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5317 OffsetRect(&r, -r.left, -r.top);
5319 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5320 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5321 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5322 ReleaseDC(infoPtr->hwnd, dc);
5324 /* Call default proc to get the scrollbars etc. painted */
5325 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5327 return TRUE;
5330 static LRESULT
5331 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5333 LPNMHDR lpnmh = (LPNMHDR)lParam;
5335 if (lpnmh->code == PGN_CALCSIZE) {
5336 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5338 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5339 lppgc->iWidth = infoPtr->treeWidth;
5340 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5341 infoPtr->treeWidth, infoPtr->clientWidth);
5343 else {
5344 lppgc->iHeight = infoPtr->treeHeight;
5345 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5346 infoPtr->treeHeight, infoPtr->clientHeight);
5348 return 0;
5350 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5353 static LRESULT
5354 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5356 if (wParam == SIZE_RESTORED)
5358 infoPtr->clientWidth = (short)LOWORD(lParam);
5359 infoPtr->clientHeight = (short)HIWORD(lParam);
5361 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5362 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5363 TREEVIEW_UpdateScrollBars(infoPtr);
5365 else
5367 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5370 TREEVIEW_Invalidate(infoPtr, NULL);
5371 return 0;
5374 static LRESULT
5375 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5377 TRACE("(%lx %lx)\n", wParam, lParam);
5379 if (wParam == GWL_STYLE)
5381 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5383 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5385 if (dwNewStyle & TVS_CHECKBOXES)
5387 initialize_checkboxes(infoPtr);
5388 TRACE("checkboxes enabled\n");
5390 else
5392 FIXME("tried to disable checkboxes\n");
5396 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5398 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5400 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5401 TRACE("tooltips enabled\n");
5403 else
5405 DestroyWindow(infoPtr->hwndToolTip);
5406 infoPtr->hwndToolTip = 0;
5407 TRACE("tooltips disabled\n");
5411 infoPtr->dwStyle = dwNewStyle;
5414 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5415 TREEVIEW_UpdateScrollBars(infoPtr);
5416 TREEVIEW_Invalidate(infoPtr, NULL);
5418 return 0;
5421 static LRESULT
5422 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5424 POINT pt;
5425 TREEVIEW_ITEM * item;
5426 NMMOUSE nmmouse;
5428 GetCursorPos(&pt);
5429 ScreenToClient(infoPtr->hwnd, &pt);
5431 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5433 memset(&nmmouse, 0, sizeof(nmmouse));
5434 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5435 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5436 nmmouse.hdr.code = NM_SETCURSOR;
5437 if (item)
5439 nmmouse.dwItemSpec = (DWORD_PTR)item;
5440 nmmouse.dwItemData = item->lParam;
5442 nmmouse.pt.x = 0;
5443 nmmouse.pt.y = 0;
5444 nmmouse.dwHitInfo = lParam;
5445 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, (LPARAM)&nmmouse))
5446 return 0;
5448 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5450 SetCursor(infoPtr->hcurHand);
5451 return 0;
5453 else
5454 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5457 static LRESULT
5458 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5460 TRACE("\n");
5462 if (!infoPtr->selectedItem)
5464 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5465 TVC_UNKNOWN);
5468 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5469 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5470 return 0;
5473 static LRESULT
5474 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5476 TRACE("\n");
5478 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5479 UpdateWindow(infoPtr->hwnd);
5480 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5481 return 0;
5484 /* update theme after a WM_THEMECHANGED message */
5485 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5487 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5488 CloseThemeData (theme);
5489 OpenThemeData (infoPtr->hwnd, themeClass);
5490 return 0;
5494 static LRESULT WINAPI
5495 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5497 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5499 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5501 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5502 else
5504 if (uMsg == WM_CREATE)
5505 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5506 else
5507 goto def;
5510 switch (uMsg)
5512 case TVM_CREATEDRAGIMAGE:
5513 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5515 case TVM_DELETEITEM:
5516 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5518 case TVM_EDITLABELA:
5519 case TVM_EDITLABELW:
5520 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5522 case TVM_ENDEDITLABELNOW:
5523 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5525 case TVM_ENSUREVISIBLE:
5526 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5528 case TVM_EXPAND:
5529 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5531 case TVM_GETBKCOLOR:
5532 return TREEVIEW_GetBkColor(infoPtr);
5534 case TVM_GETCOUNT:
5535 return TREEVIEW_GetCount(infoPtr);
5537 case TVM_GETEDITCONTROL:
5538 return TREEVIEW_GetEditControl(infoPtr);
5540 case TVM_GETIMAGELIST:
5541 return TREEVIEW_GetImageList(infoPtr, wParam);
5543 case TVM_GETINDENT:
5544 return TREEVIEW_GetIndent(infoPtr);
5546 case TVM_GETINSERTMARKCOLOR:
5547 return TREEVIEW_GetInsertMarkColor(infoPtr);
5549 case TVM_GETISEARCHSTRINGA:
5550 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5551 return 0;
5553 case TVM_GETISEARCHSTRINGW:
5554 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5555 return 0;
5557 case TVM_GETITEMA:
5558 case TVM_GETITEMW:
5559 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5560 uMsg == TVM_GETITEMW);
5561 case TVM_GETITEMHEIGHT:
5562 return TREEVIEW_GetItemHeight(infoPtr);
5564 case TVM_GETITEMRECT:
5565 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5567 case TVM_GETITEMSTATE:
5568 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5570 case TVM_GETLINECOLOR:
5571 return TREEVIEW_GetLineColor(infoPtr);
5573 case TVM_GETNEXTITEM:
5574 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5576 case TVM_GETSCROLLTIME:
5577 return TREEVIEW_GetScrollTime(infoPtr);
5579 case TVM_GETTEXTCOLOR:
5580 return TREEVIEW_GetTextColor(infoPtr);
5582 case TVM_GETTOOLTIPS:
5583 return TREEVIEW_GetToolTips(infoPtr);
5585 case TVM_GETUNICODEFORMAT:
5586 return TREEVIEW_GetUnicodeFormat(infoPtr);
5588 case TVM_GETVISIBLECOUNT:
5589 return TREEVIEW_GetVisibleCount(infoPtr);
5591 case TVM_HITTEST:
5592 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5594 case TVM_INSERTITEMA:
5595 case TVM_INSERTITEMW:
5596 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5597 uMsg == TVM_INSERTITEMW);
5598 case TVM_SELECTITEM:
5599 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5601 case TVM_SETBKCOLOR:
5602 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5604 case TVM_SETIMAGELIST:
5605 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5607 case TVM_SETINDENT:
5608 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5610 case TVM_SETINSERTMARK:
5611 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5613 case TVM_SETINSERTMARKCOLOR:
5614 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5616 case TVM_SETITEMA:
5617 case TVM_SETITEMW:
5618 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5619 uMsg == TVM_SETITEMW);
5620 case TVM_SETLINECOLOR:
5621 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5623 case TVM_SETITEMHEIGHT:
5624 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5626 case TVM_SETSCROLLTIME:
5627 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5629 case TVM_SETTEXTCOLOR:
5630 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5632 case TVM_SETTOOLTIPS:
5633 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5635 case TVM_SETUNICODEFORMAT:
5636 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5638 case TVM_SORTCHILDREN:
5639 return TREEVIEW_SortChildren(infoPtr, lParam);
5641 case TVM_SORTCHILDRENCB:
5642 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5644 case WM_CHAR:
5645 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5647 case WM_COMMAND:
5648 return TREEVIEW_Command(infoPtr, wParam, lParam);
5650 case WM_DESTROY:
5651 return TREEVIEW_Destroy(infoPtr);
5653 /* WM_ENABLE */
5655 case WM_ERASEBKGND:
5656 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5658 case WM_GETDLGCODE:
5659 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5661 case WM_GETFONT:
5662 return TREEVIEW_GetFont(infoPtr);
5664 case WM_HSCROLL:
5665 return TREEVIEW_HScroll(infoPtr, wParam);
5667 case WM_KEYDOWN:
5668 return TREEVIEW_KeyDown(infoPtr, wParam);
5670 case WM_KILLFOCUS:
5671 return TREEVIEW_KillFocus(infoPtr);
5673 case WM_LBUTTONDBLCLK:
5674 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5676 case WM_LBUTTONDOWN:
5677 return TREEVIEW_LButtonDown(infoPtr, lParam);
5679 /* WM_MBUTTONDOWN */
5681 case WM_MOUSELEAVE:
5682 return TREEVIEW_MouseLeave(infoPtr);
5684 case WM_MOUSEMOVE:
5685 return TREEVIEW_MouseMove(infoPtr, lParam);
5687 case WM_NCLBUTTONDOWN:
5688 if (infoPtr->hwndEdit)
5689 SetFocus(infoPtr->hwnd);
5690 goto def;
5692 case WM_NCPAINT:
5693 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5695 case WM_NOTIFY:
5696 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5698 case WM_NOTIFYFORMAT:
5699 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5701 case WM_PRINTCLIENT:
5702 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5704 case WM_PAINT:
5705 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5707 case WM_RBUTTONDOWN:
5708 return TREEVIEW_RButtonDown(infoPtr, lParam);
5710 case WM_SETCURSOR:
5711 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5713 case WM_SETFOCUS:
5714 return TREEVIEW_SetFocus(infoPtr);
5716 case WM_SETFONT:
5717 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5719 case WM_SETREDRAW:
5720 return TREEVIEW_SetRedraw(infoPtr, wParam);
5722 case WM_SIZE:
5723 return TREEVIEW_Size(infoPtr, wParam, lParam);
5725 case WM_STYLECHANGED:
5726 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5728 case WM_SYSCOLORCHANGE:
5729 COMCTL32_RefreshSysColors();
5730 return 0;
5732 /* WM_SYSKEYDOWN */
5734 case WM_TIMER:
5735 return TREEVIEW_HandleTimer(infoPtr, wParam);
5737 case WM_THEMECHANGED:
5738 return TREEVIEW_ThemeChanged (infoPtr);
5740 case WM_VSCROLL:
5741 return TREEVIEW_VScroll(infoPtr, wParam);
5743 /* WM_WININICHANGE */
5745 case WM_MOUSEWHEEL:
5746 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5748 case WM_DRAWITEM:
5749 TRACE("drawItem\n");
5750 goto def;
5752 default:
5753 /* This mostly catches MFC and Delphi messages. :( */
5754 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5755 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5756 def:
5757 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5762 /* Class Registration ***************************************************/
5764 VOID
5765 TREEVIEW_Register(void)
5767 WNDCLASSW wndClass;
5769 TRACE("\n");
5771 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5772 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5773 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5774 wndClass.cbClsExtra = 0;
5775 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5777 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5778 wndClass.hbrBackground = 0;
5779 wndClass.lpszClassName = WC_TREEVIEWW;
5781 RegisterClassW(&wndClass);
5785 VOID
5786 TREEVIEW_Unregister(void)
5788 UnregisterClassW(WC_TREEVIEWW, NULL);
5792 /* Tree Verification ****************************************************/
5794 static inline void
5795 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5797 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5798 TREEVIEW_ITEM *item)
5800 assert(infoPtr != NULL);
5801 assert(item != NULL);
5803 /* both NULL, or both non-null */
5804 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5806 assert(item->firstChild != item);
5807 assert(item->lastChild != item);
5809 if (item->firstChild)
5811 assert(item->firstChild->parent == item);
5812 assert(item->firstChild->prevSibling == NULL);
5815 if (item->lastChild)
5817 assert(item->lastChild->parent == item);
5818 assert(item->lastChild->nextSibling == NULL);
5821 assert(item->nextSibling != item);
5822 if (item->nextSibling)
5824 assert(item->nextSibling->parent == item->parent);
5825 assert(item->nextSibling->prevSibling == item);
5828 assert(item->prevSibling != item);
5829 if (item->prevSibling)
5831 assert(item->prevSibling->parent == item->parent);
5832 assert(item->prevSibling->nextSibling == item);
5836 static inline void
5837 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5839 assert(item != NULL);
5841 assert(item->parent != NULL);
5842 assert(item->parent != item);
5843 assert(item->iLevel == item->parent->iLevel + 1);
5845 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5847 TREEVIEW_VerifyItemCommon(infoPtr, item);
5849 TREEVIEW_VerifyChildren(infoPtr, item);
5852 static inline void
5853 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5855 TREEVIEW_ITEM *child;
5856 assert(item != NULL);
5858 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5859 TREEVIEW_VerifyItem(infoPtr, child);
5862 static inline void
5863 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5865 TREEVIEW_ITEM *root = infoPtr->root;
5867 assert(root != NULL);
5868 assert(root->iLevel == -1);
5869 assert(root->parent == NULL);
5870 assert(root->prevSibling == NULL);
5872 TREEVIEW_VerifyItemCommon(infoPtr, root);
5874 TREEVIEW_VerifyChildren(infoPtr, root);
5877 static void
5878 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5880 if (!TRACE_ON(treeview)) return;
5882 assert(infoPtr != NULL);
5883 TREEVIEW_VerifyRoot(infoPtr);