Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / comctl32 / treeview.c
blob713b102a2eadfa0adf97d0b84bc43bd258b0c5bd
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * NOTES
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: All items always! have valid (allocated) pszText field.
27 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
28 * of size TEXT_CALLBACK_SIZE in DoSetItem.
29 * We use callbackMask to keep track of fields to be updated.
31 * TODO:
32 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
33 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
35 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
37 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
43 #include "config.h"
44 #include "wine/port.h"
46 #include <assert.h>
47 #include <ctype.h>
48 #include <stdarg.h>
49 #include <string.h>
50 #include <limits.h>
51 #include <stdlib.h>
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
55 #include "windef.h"
56 #include "winbase.h"
57 #include "wingdi.h"
58 #include "winuser.h"
59 #include "winnls.h"
60 #include "commctrl.h"
61 #include "comctl32.h"
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
65 /* internal structures */
67 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
69 UINT callbackMask;
70 UINT state;
71 UINT stateMask;
72 LPWSTR pszText;
73 int cchTextMax;
74 int iImage;
75 int iSelectedImage;
76 int cChildren;
77 LPARAM lParam;
78 int iIntegral; /* item height multiplier (1 is normal) */
79 int iLevel; /* indentation level:0=root level */
80 HTREEITEM parent; /* handle to parent or 0 if at root */
81 HTREEITEM firstChild; /* handle to first child or 0 if no child */
82 HTREEITEM lastChild;
83 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
84 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
85 RECT rect;
86 LONG linesOffset;
87 LONG stateOffset;
88 LONG imageOffset;
89 LONG textOffset;
90 LONG textWidth; /* horizontal text extent for pszText */
91 LONG visibleOrder; /* visible ordering, 0 is first visible item */
92 } TREEVIEW_ITEM;
95 typedef struct tagTREEVIEW_INFO
97 HWND hwnd;
98 HWND hwndNotify; /* Owner window to send notifications to */
99 DWORD dwStyle;
100 HTREEITEM root;
101 UINT uInternalStatus;
102 INT Timer;
103 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
104 INT cdmode; /* last custom draw setting */
105 UINT uScrollTime; /* max. time for scrolling in milliseconds */
106 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
108 UINT uItemHeight; /* item height */
109 BOOL bHeightSet;
111 LONG clientWidth; /* width of control window */
112 LONG clientHeight; /* height of control window */
114 LONG treeWidth; /* width of visible tree items */
115 LONG treeHeight; /* height of visible tree items */
117 UINT uIndent; /* indentation in pixels */
118 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
119 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
120 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
122 HTREEITEM firstVisible; /* handle to first visible item */
123 LONG maxVisibleOrder;
124 HTREEITEM dropItem; /* handle to item selected by drag cursor */
125 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
126 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
127 HIMAGELIST dragList; /* Bitmap of dragged item */
128 LONG scrollX;
129 COLORREF clrBk;
130 COLORREF clrText;
131 COLORREF clrLine;
132 COLORREF clrInsertMark;
133 HFONT hFont;
134 HFONT hDefaultFont;
135 HFONT hBoldFont;
136 HFONT hUnderlineFont;
137 HCURSOR hcurHand;
138 HWND hwndToolTip;
140 HWND hwndEdit;
141 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
142 BOOL bIgnoreEditKillFocus;
143 BOOL bLabelChanged;
145 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
146 HIMAGELIST himlNormal;
147 int normalImageHeight;
148 int normalImageWidth;
149 HIMAGELIST himlState;
150 int stateImageHeight;
151 int stateImageWidth;
152 HDPA items;
154 DWORD lastKeyPressTimestamp; /* Added */
155 WPARAM charCode; /* Added */
156 INT nSearchParamLength; /* Added */
157 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
158 } TREEVIEW_INFO;
161 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
162 #define KEY_DELAY 450
164 /* bitflags for infoPtr->uInternalStatus */
166 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
167 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
168 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
169 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
170 #define TV_RDRAG 0x10 /* dito Rbutton */
171 #define TV_RDRAGGING 0x20
173 /* bitflags for infoPtr->timer */
175 #define TV_EDIT_TIMER 2
176 #define TV_EDIT_TIMER_SET 2
179 VOID TREEVIEW_Register (VOID);
180 VOID TREEVIEW_Unregister (VOID);
183 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
186 #define TEXT_CALLBACK_SIZE 260
188 #define TREEVIEW_LEFT_MARGIN 8
190 #define MINIMUM_INDENT 19
192 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
194 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
195 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
196 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
199 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
202 static VOID TREEVIEW_Invalidate(TREEVIEW_INFO *, TREEVIEW_ITEM *);
204 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
205 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
206 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
207 static LRESULT TREEVIEW_RButtonUp(TREEVIEW_INFO *, LPPOINT);
208 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
209 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
210 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
211 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
214 /* Random Utilities *****************************************************/
216 #ifndef NDEBUG
217 static inline void
218 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
220 (void)infoPtr;
222 #else
223 /* The definition is at the end of the file. */
224 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
225 #endif
227 /* Returns the treeview private data if hwnd is a treeview.
228 * Otherwise returns an undefined value. */
229 static TREEVIEW_INFO *
230 TREEVIEW_GetInfoPtr(HWND hwnd)
232 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
235 /* Don't call this. Nothing wants an item index. */
236 static inline int
237 TREEVIEW_GetItemIndex(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
239 assert(infoPtr != NULL);
241 return DPA_GetPtrIndex(infoPtr->items, handle);
244 /* Checks if item has changed and needs to be redrawn */
245 static inline BOOL item_changed (TREEVIEW_ITEM *tiOld, TREEVIEW_ITEM *tiNew, LPTVITEMEXW tvChange)
247 /* Number of children has changed */
248 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
249 return TRUE;
251 /* Image has changed and it's not a callback */
252 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
253 tiNew->iImage != I_IMAGECALLBACK)
254 return TRUE;
256 /* Selected image has changed and it's not a callback */
257 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
258 tiNew->iSelectedImage != I_IMAGECALLBACK)
259 return TRUE;
261 /* Text has changed and it's not a callback */
262 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
263 tiNew->pszText != LPSTR_TEXTCALLBACKW)
264 return TRUE;
266 /* Indent has changed */
267 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
268 return TRUE;
270 /* Item state has changed */
271 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
272 return TRUE;
274 return FALSE;
277 /***************************************************************************
278 * This method checks that handle is an item for this tree.
280 static BOOL
281 TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
283 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
285 TRACE("invalid item %p\n", handle);
286 return FALSE;
288 else
289 return TRUE;
292 static HFONT
293 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
295 LOGFONTW font;
297 GetObjectW(hOrigFont, sizeof(font), &font);
298 font.lfWeight = FW_BOLD;
299 return CreateFontIndirectW(&font);
302 static HFONT
303 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
305 LOGFONTW font;
307 GetObjectW(hOrigFont, sizeof(font), &font);
308 font.lfUnderline = TRUE;
309 return CreateFontIndirectW(&font);
312 static inline HFONT
313 TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
315 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
316 return infoPtr->hUnderlineFont;
317 if (item->state & TVIS_BOLD)
318 return infoPtr->hBoldFont;
319 return infoPtr->hFont;
322 /* for trace/debugging purposes only */
323 static const char *
324 TREEVIEW_ItemName(TREEVIEW_ITEM *item)
326 if (item == NULL) return "<null item>";
327 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
328 if (item->pszText == NULL) return "<null>";
329 return debugstr_w(item->pszText);
332 /* An item is not a child of itself. */
333 static BOOL
334 TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
338 child = child->parent;
339 if (child == parent) return TRUE;
340 } while (child != NULL);
342 return FALSE;
346 /* Tree Traversal *******************************************************/
348 /***************************************************************************
349 * This method returns the last expanded sibling or child child item
350 * of a tree node
352 static TREEVIEW_ITEM *
353 TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
355 if (!wineItem)
356 return NULL;
358 while (wineItem->lastChild)
360 if (wineItem->state & TVIS_EXPANDED)
361 wineItem = wineItem->lastChild;
362 else
363 break;
366 if (wineItem == infoPtr->root)
367 return NULL;
369 return wineItem;
372 /***************************************************************************
373 * This method returns the previous non-hidden item in the list not
374 * considering the tree hierarchy.
376 static TREEVIEW_ITEM *
377 TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
379 if (tvItem->prevSibling)
381 /* This item has a prevSibling, get the last item in the sibling's tree. */
382 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
384 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
385 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
386 else
387 return upItem;
389 else
391 /* this item does not have a prevSibling, get the parent */
392 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
397 /***************************************************************************
398 * This method returns the next physical item in the treeview not
399 * considering the tree hierarchy.
401 static TREEVIEW_ITEM *
402 TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
404 assert(tvItem != NULL);
407 * If this item has children and is expanded, return the first child
409 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
411 return tvItem->firstChild;
416 * try to get the sibling
418 if (tvItem->nextSibling)
419 return tvItem->nextSibling;
422 * Otherwise, get the parent's sibling.
424 while (tvItem->parent)
426 tvItem = tvItem->parent;
428 if (tvItem->nextSibling)
429 return tvItem->nextSibling;
432 return NULL;
435 /***************************************************************************
436 * This method returns the nth item starting at the given item. It returns
437 * the last item (or first) we we run out of items.
439 * Will scroll backward if count is <0.
440 * forward if count is >0.
442 static TREEVIEW_ITEM *
443 TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
444 LONG count)
446 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
447 TREEVIEW_ITEM *previousItem;
449 assert(wineItem != NULL);
451 if (count > 0)
453 next_item = TREEVIEW_GetNextListItem;
455 else if (count < 0)
457 count = -count;
458 next_item = TREEVIEW_GetPrevListItem;
460 else
461 return wineItem;
465 previousItem = wineItem;
466 wineItem = next_item(infoPtr, wineItem);
468 } while (--count && wineItem != NULL);
471 return wineItem ? wineItem : previousItem;
474 /* Notifications ************************************************************/
476 static INT get_notifycode(TREEVIEW_INFO *infoPtr, INT code)
478 if (!infoPtr->bNtfUnicode) {
479 switch (code) {
480 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
481 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
482 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
483 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
484 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
485 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
486 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
487 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
488 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
489 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
490 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
491 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
494 return code;
497 static LRESULT
498 TREEVIEW_SendRealNotify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
500 TRACE("wParam=%d, lParam=%ld\n", wParam, lParam);
501 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
504 static BOOL
505 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
507 NMHDR nmhdr;
508 HWND hwnd = infoPtr->hwnd;
510 TRACE("%d\n", code);
511 nmhdr.hwndFrom = hwnd;
512 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
513 nmhdr.code = get_notifycode(infoPtr, code);
515 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
516 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
519 static VOID
520 TREEVIEW_TVItemFromItem(TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
522 tvItem->mask = mask;
523 tvItem->hItem = item;
524 tvItem->state = item->state;
525 tvItem->stateMask = 0;
526 tvItem->iImage = item->iImage;
527 tvItem->iSelectedImage = item->iSelectedImage;
528 tvItem->cChildren = item->cChildren;
529 tvItem->lParam = item->lParam;
531 if(mask & TVIF_TEXT)
533 if (!infoPtr->bNtfUnicode)
535 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
536 tvItem->pszText = Alloc (tvItem->cchTextMax);
537 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
539 else
541 tvItem->cchTextMax = item->cchTextMax;
542 tvItem->pszText = item->pszText;
545 else
547 tvItem->cchTextMax = 0;
548 tvItem->pszText = NULL;
552 static BOOL
553 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
554 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
556 HWND hwnd = infoPtr->hwnd;
557 NMTREEVIEWW nmhdr;
558 BOOL ret;
560 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
561 code, action, oldItem, newItem);
563 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWA));
565 nmhdr.hdr.hwndFrom = hwnd;
566 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
567 nmhdr.hdr.code = get_notifycode(infoPtr, code);
568 nmhdr.action = action;
570 if (oldItem)
571 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
573 if (newItem)
574 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
576 nmhdr.ptDrag.x = 0;
577 nmhdr.ptDrag.y = 0;
579 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
580 (WPARAM)nmhdr.hdr.idFrom,
581 (LPARAM)&nmhdr);
582 if (!infoPtr->bNtfUnicode)
584 Free(nmhdr.itemOld.pszText);
585 Free(nmhdr.itemNew.pszText);
587 return ret;
590 static BOOL
591 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
592 HTREEITEM dragItem, POINT pt)
594 HWND hwnd = infoPtr->hwnd;
595 NMTREEVIEWW nmhdr;
597 TRACE("code:%d dragitem:%p\n", code, dragItem);
599 nmhdr.hdr.hwndFrom = hwnd;
600 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
601 nmhdr.hdr.code = get_notifycode(infoPtr, code);
602 nmhdr.action = 0;
603 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
604 nmhdr.itemNew.hItem = dragItem;
605 nmhdr.itemNew.state = dragItem->state;
606 nmhdr.itemNew.lParam = dragItem->lParam;
608 nmhdr.ptDrag.x = pt.x;
609 nmhdr.ptDrag.y = pt.y;
611 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
612 (WPARAM)nmhdr.hdr.idFrom,
613 (LPARAM)&nmhdr);
617 static BOOL
618 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
619 HDC hdc, RECT rc)
621 HWND hwnd = infoPtr->hwnd;
622 NMTVCUSTOMDRAW nmcdhdr;
623 LPNMCUSTOMDRAW nmcd;
625 TRACE("drawstage:%lx hdc:%p\n", dwDrawStage, hdc);
627 nmcd = &nmcdhdr.nmcd;
628 nmcd->hdr.hwndFrom = hwnd;
629 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
630 nmcd->hdr.code = NM_CUSTOMDRAW;
631 nmcd->dwDrawStage = dwDrawStage;
632 nmcd->hdc = hdc;
633 nmcd->rc = rc;
634 nmcd->dwItemSpec = 0;
635 nmcd->uItemState = 0;
636 nmcd->lItemlParam = 0;
637 nmcdhdr.clrText = infoPtr->clrText;
638 nmcdhdr.clrTextBk = infoPtr->clrBk;
639 nmcdhdr.iLevel = 0;
641 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
642 (WPARAM)nmcd->hdr.idFrom,
643 (LPARAM)&nmcdhdr);
648 /* FIXME: need to find out when the flags in uItemState need to be set */
650 static BOOL
651 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
652 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
653 NMTVCUSTOMDRAW *nmcdhdr)
655 HWND hwnd = infoPtr->hwnd;
656 LPNMCUSTOMDRAW nmcd;
657 DWORD dwDrawStage, dwItemSpec;
658 UINT uItemState;
659 INT retval;
661 dwDrawStage = CDDS_ITEM | uItemDrawState;
662 dwItemSpec = (DWORD)wineItem;
663 uItemState = 0;
664 if (wineItem->state & TVIS_SELECTED)
665 uItemState |= CDIS_SELECTED;
666 if (wineItem == infoPtr->selectedItem)
667 uItemState |= CDIS_FOCUS;
668 if (wineItem == infoPtr->hotItem)
669 uItemState |= CDIS_HOT;
671 nmcd = &nmcdhdr->nmcd;
672 nmcd->hdr.hwndFrom = hwnd;
673 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
674 nmcd->hdr.code = NM_CUSTOMDRAW;
675 nmcd->dwDrawStage = dwDrawStage;
676 nmcd->hdc = hdc;
677 nmcd->rc = wineItem->rect;
678 nmcd->dwItemSpec = dwItemSpec;
679 nmcd->uItemState = uItemState;
680 nmcd->lItemlParam = wineItem->lParam;
681 nmcdhdr->iLevel = wineItem->iLevel;
683 TRACE("drawstage:%lx hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
684 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
685 nmcd->uItemState, nmcd->lItemlParam);
687 retval = TREEVIEW_SendRealNotify(infoPtr,
688 (WPARAM)nmcd->hdr.idFrom,
689 (LPARAM)nmcdhdr);
691 return (BOOL)retval;
694 static BOOL
695 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
697 HWND hwnd = infoPtr->hwnd;
698 NMTVDISPINFOW tvdi;
699 BOOL ret;
701 tvdi.hdr.hwndFrom = hwnd;
702 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
703 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
705 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
706 &tvdi.item, editItem);
708 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
710 if (!infoPtr->bNtfUnicode)
711 Free(tvdi.item.pszText);
713 return ret;
716 static void
717 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
718 UINT mask)
720 NMTVDISPINFOW callback;
721 HWND hwnd = infoPtr->hwnd;
723 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
724 mask &= wineItem->callbackMask;
726 if (mask == 0) return;
728 callback.hdr.hwndFrom = hwnd;
729 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
730 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
732 /* 'state' always contains valid value, as well as 'lParam'.
733 * All other parameters are uninitialized.
735 callback.item.pszText = wineItem->pszText;
736 callback.item.cchTextMax = wineItem->cchTextMax;
737 callback.item.mask = mask;
738 callback.item.hItem = wineItem;
739 callback.item.state = wineItem->state;
740 callback.item.lParam = wineItem->lParam;
742 /* If text is changed we need to recalculate textWidth */
743 if (mask & TVIF_TEXT)
744 wineItem->textWidth = 0;
746 TREEVIEW_SendRealNotify(infoPtr,
747 (WPARAM)callback.hdr.idFrom, (LPARAM)&callback);
749 /* It may have changed due to a call to SetItem. */
750 mask &= wineItem->callbackMask;
752 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
754 /* Instead of copying text into our buffer user specified its own */
755 if (!infoPtr->bNtfUnicode) {
756 LPWSTR newText;
757 int buflen;
758 int len = MultiByteToWideChar( CP_ACP, 0,
759 (LPSTR)callback.item.pszText, -1,
760 NULL, 0);
761 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
762 newText = (LPWSTR)ReAlloc(wineItem->pszText, buflen);
764 TRACE("returned str %s, len=%d, buflen=%d\n",
765 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
767 if (newText)
769 wineItem->pszText = newText;
770 MultiByteToWideChar( CP_ACP, 0,
771 (LPSTR)callback.item.pszText, -1,
772 wineItem->pszText, buflen);
773 wineItem->cchTextMax = buflen;
775 /* If ReAlloc fails we have nothing to do, but keep original text */
777 else {
778 int len = max(lstrlenW(callback.item.pszText) + 1,
779 TEXT_CALLBACK_SIZE);
780 LPWSTR newText = ReAlloc(wineItem->pszText, len);
782 TRACE("returned wstr %s, len=%d\n",
783 debugstr_w(callback.item.pszText), len);
785 if (newText)
787 wineItem->pszText = newText;
788 strcpyW(wineItem->pszText, callback.item.pszText);
789 wineItem->cchTextMax = len;
791 /* If ReAlloc fails we have nothing to do, but keep original text */
794 else if (mask & TVIF_TEXT) {
795 /* User put text into our buffer, that is ok unless A string */
796 if (!infoPtr->bNtfUnicode) {
797 LPWSTR newText;
798 LPWSTR oldText = NULL;
799 int buflen;
800 int len = MultiByteToWideChar( CP_ACP, 0,
801 (LPSTR)callback.item.pszText, -1,
802 NULL, 0);
803 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
804 newText = (LPWSTR)Alloc(buflen);
806 TRACE("same buffer str %s, len=%d, buflen=%d\n",
807 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
809 if (newText)
811 oldText = wineItem->pszText;
812 wineItem->pszText = newText;
813 MultiByteToWideChar( CP_ACP, 0,
814 (LPSTR)callback.item.pszText, -1,
815 wineItem->pszText, buflen);
816 wineItem->cchTextMax = buflen;
817 if (oldText)
818 Free(oldText);
823 if (mask & TVIF_IMAGE)
824 wineItem->iImage = callback.item.iImage;
826 if (mask & TVIF_SELECTEDIMAGE)
827 wineItem->iSelectedImage = callback.item.iSelectedImage;
829 if (mask & TVIF_CHILDREN)
830 wineItem->cChildren = callback.item.cChildren;
832 /* These members are now permanently set. */
833 if (callback.item.mask & TVIF_DI_SETITEM)
834 wineItem->callbackMask &= ~callback.item.mask;
837 /***************************************************************************
838 * This function uses cChildren field to decide whether the item has
839 * children or not.
840 * Note: if this returns TRUE, the child items may not actually exist,
841 * they could be virtual.
843 * Just use wineItem->firstChild to check for physical children.
845 static BOOL
846 TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
848 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
850 return wineItem->cChildren > 0;
854 /* Item Position ********************************************************/
856 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
857 static VOID
858 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
859 TREEVIEW_ITEM *item)
861 /* Same effect, different optimisation. */
862 #if 0
863 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
864 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
865 #else
866 BOOL lar = ((infoPtr->dwStyle
867 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
868 > TVS_LINESATROOT);
869 #endif
871 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
872 - infoPtr->scrollX;
873 item->stateOffset = item->linesOffset + infoPtr->uIndent;
874 item->imageOffset = item->stateOffset
875 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
876 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
879 static VOID
880 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
882 HDC hdc;
883 HFONT hOldFont=0;
884 SIZE sz;
886 /* DRAW's OM docker creates items like this */
887 if (item->pszText == NULL)
889 item->textWidth = 0;
890 return;
893 if (hDC != 0)
895 hdc = hDC;
897 else
899 hdc = GetDC(infoPtr->hwnd);
900 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
903 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
904 item->textWidth = sz.cx;
906 if (hDC == 0)
908 SelectObject(hdc, hOldFont);
909 ReleaseDC(0, hdc);
913 static VOID
914 TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
916 item->rect.top = infoPtr->uItemHeight *
917 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
919 item->rect.bottom = item->rect.top
920 + infoPtr->uItemHeight * item->iIntegral - 1;
922 item->rect.left = 0;
923 item->rect.right = infoPtr->clientWidth;
926 /* We know that only items after start need their order updated. */
927 static void
928 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
930 TREEVIEW_ITEM *item;
931 int order;
933 if (!start)
935 start = infoPtr->root->firstChild;
936 order = 0;
938 else
939 order = start->visibleOrder;
941 for (item = start; item != NULL;
942 item = TREEVIEW_GetNextListItem(infoPtr, item))
944 item->visibleOrder = order;
945 order += item->iIntegral;
948 infoPtr->maxVisibleOrder = order;
950 for (item = start; item != NULL;
951 item = TREEVIEW_GetNextListItem(infoPtr, item))
953 TREEVIEW_ComputeItemRect(infoPtr, item);
958 /* Update metrics of all items in selected subtree.
959 * root must be expanded
961 static VOID
962 TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
964 TREEVIEW_ITEM *sibling;
965 HDC hdc;
966 HFONT hOldFont;
968 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
969 return;
971 root->state &= ~TVIS_EXPANDED;
972 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
973 root->state |= TVIS_EXPANDED;
975 hdc = GetDC(infoPtr->hwnd);
976 hOldFont = SelectObject(hdc, infoPtr->hFont);
978 for (; root != sibling;
979 root = TREEVIEW_GetNextListItem(infoPtr, root))
981 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
983 if (root->callbackMask & TVIF_TEXT)
984 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
986 if (root->textWidth == 0)
988 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
989 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
993 SelectObject(hdc, hOldFont);
994 ReleaseDC(infoPtr->hwnd, hdc);
997 /* Item Allocation **********************************************************/
999 static TREEVIEW_ITEM *
1000 TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
1002 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1004 if (!newItem)
1005 return NULL;
1007 newItem->iImage = -1;
1008 newItem->iSelectedImage = -1;
1010 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1012 Free(newItem);
1013 return NULL;
1016 return newItem;
1019 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1020 * free item->pszText. */
1021 static void
1022 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1024 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1025 Free(item);
1026 if (infoPtr->selectedItem == item)
1027 infoPtr->selectedItem = NULL;
1028 if (infoPtr->hotItem == item)
1029 infoPtr->hotItem = NULL;
1030 if (infoPtr->focusedItem == item)
1031 infoPtr->focusedItem = NULL;
1032 if (infoPtr->firstVisible == item)
1033 infoPtr->firstVisible = NULL;
1034 if (infoPtr->dropItem == item)
1035 infoPtr->dropItem = NULL;
1036 if (infoPtr->insertMarkItem == item)
1037 infoPtr->insertMarkItem = NULL;
1041 /* Item Insertion *******************************************************/
1043 /***************************************************************************
1044 * This method inserts newItem before sibling as a child of parent.
1045 * sibling can be NULL, but only if parent has no children.
1047 static void
1048 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1049 TREEVIEW_ITEM *parent)
1051 assert(newItem != NULL);
1052 assert(parent != NULL);
1054 if (sibling != NULL)
1056 assert(sibling->parent == parent);
1058 if (sibling->prevSibling != NULL)
1059 sibling->prevSibling->nextSibling = newItem;
1061 newItem->prevSibling = sibling->prevSibling;
1062 sibling->prevSibling = newItem;
1064 else
1065 newItem->prevSibling = NULL;
1067 newItem->nextSibling = sibling;
1069 if (parent->firstChild == sibling)
1070 parent->firstChild = newItem;
1072 if (parent->lastChild == NULL)
1073 parent->lastChild = newItem;
1076 /***************************************************************************
1077 * This method inserts newItem after sibling as a child of parent.
1078 * sibling can be NULL, but only if parent has no children.
1080 static void
1081 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1082 TREEVIEW_ITEM *parent)
1084 assert(newItem != NULL);
1085 assert(parent != NULL);
1087 if (sibling != NULL)
1089 assert(sibling->parent == parent);
1091 if (sibling->nextSibling != NULL)
1092 sibling->nextSibling->prevSibling = newItem;
1094 newItem->nextSibling = sibling->nextSibling;
1095 sibling->nextSibling = newItem;
1097 else
1098 newItem->nextSibling = NULL;
1100 newItem->prevSibling = sibling;
1102 if (parent->lastChild == sibling)
1103 parent->lastChild = newItem;
1105 if (parent->firstChild == NULL)
1106 parent->firstChild = newItem;
1109 static BOOL
1110 TREEVIEW_DoSetItemT(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1111 const TVITEMEXW *tvItem, BOOL isW)
1113 UINT callbackClear = 0;
1114 UINT callbackSet = 0;
1116 TRACE("item %p\n", wineItem);
1117 /* Do this first in case it fails. */
1118 if (tvItem->mask & TVIF_TEXT)
1120 wineItem->textWidth = 0; /* force width recalculation */
1121 if (tvItem->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
1123 int len;
1124 LPWSTR newText;
1125 if (isW)
1126 len = lstrlenW(tvItem->pszText) + 1;
1127 else
1128 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1130 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1132 if (newText == NULL) return FALSE;
1134 callbackClear |= TVIF_TEXT;
1136 wineItem->pszText = newText;
1137 wineItem->cchTextMax = len;
1138 if (isW)
1139 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1140 else
1141 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1142 wineItem->pszText, len);
1144 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1146 else
1148 callbackSet |= TVIF_TEXT;
1150 wineItem->pszText = ReAlloc(wineItem->pszText,
1151 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1152 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1153 TRACE("setting callback, item %p\n", wineItem);
1157 if (tvItem->mask & TVIF_CHILDREN)
1159 wineItem->cChildren = tvItem->cChildren;
1161 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1162 callbackSet |= TVIF_CHILDREN;
1163 else
1164 callbackClear |= TVIF_CHILDREN;
1167 if (tvItem->mask & TVIF_IMAGE)
1169 wineItem->iImage = tvItem->iImage;
1171 if (wineItem->iImage == I_IMAGECALLBACK)
1172 callbackSet |= TVIF_IMAGE;
1173 else
1174 callbackClear |= TVIF_IMAGE;
1177 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1179 wineItem->iSelectedImage = tvItem->iSelectedImage;
1181 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1182 callbackSet |= TVIF_SELECTEDIMAGE;
1183 else
1184 callbackClear |= TVIF_SELECTEDIMAGE;
1187 if (tvItem->mask & TVIF_PARAM)
1188 wineItem->lParam = tvItem->lParam;
1190 /* If the application sets TVIF_INTEGRAL without
1191 * supplying a TVITEMEX structure, it's toast. */
1192 if (tvItem->mask & TVIF_INTEGRAL)
1193 wineItem->iIntegral = tvItem->iIntegral;
1195 if (tvItem->mask & TVIF_STATE)
1197 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1198 tvItem->stateMask);
1199 wineItem->state &= ~tvItem->stateMask;
1200 wineItem->state |= (tvItem->state & tvItem->stateMask);
1203 wineItem->callbackMask |= callbackSet;
1204 wineItem->callbackMask &= ~callbackClear;
1206 return TRUE;
1209 /* Note that the new item is pre-zeroed. */
1210 static LRESULT
1211 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1213 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1214 HTREEITEM insertAfter;
1215 TREEVIEW_ITEM *newItem, *parentItem;
1216 BOOL bTextUpdated = FALSE;
1218 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1220 parentItem = infoPtr->root;
1222 else
1224 parentItem = ptdi->hParent;
1226 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1228 WARN("invalid parent %p\n", parentItem);
1229 return (LRESULT)(HTREEITEM)NULL;
1233 insertAfter = ptdi->hInsertAfter;
1235 /* Validate this now for convenience. */
1236 switch ((DWORD)insertAfter)
1238 case (DWORD)TVI_FIRST:
1239 case (DWORD)TVI_LAST:
1240 case (DWORD)TVI_SORT:
1241 break;
1243 default:
1244 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1245 insertAfter->parent != parentItem)
1247 WARN("invalid insert after %p\n", insertAfter);
1248 insertAfter = TVI_LAST;
1252 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1253 (tvItem->mask & TVIF_TEXT)
1254 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1255 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1256 : "<no label>");
1258 newItem = TREEVIEW_AllocateItem(infoPtr);
1259 if (newItem == NULL)
1260 return (LRESULT)(HTREEITEM)NULL;
1262 newItem->parent = parentItem;
1263 newItem->iIntegral = 1;
1265 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1266 return (LRESULT)(HTREEITEM)NULL;
1268 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1270 infoPtr->uNumItems++;
1272 switch ((DWORD)insertAfter)
1274 case (DWORD)TVI_FIRST:
1276 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1277 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1278 if (infoPtr->firstVisible == originalFirst)
1279 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1281 break;
1283 case (DWORD)TVI_LAST:
1284 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1285 break;
1287 /* hInsertAfter names a specific item we want to insert after */
1288 default:
1289 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1290 break;
1292 case (DWORD)TVI_SORT:
1294 TREEVIEW_ITEM *aChild;
1295 TREEVIEW_ITEM *previousChild = NULL;
1296 BOOL bItemInserted = FALSE;
1298 aChild = parentItem->firstChild;
1300 bTextUpdated = TRUE;
1301 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1303 /* Iterate the parent children to see where we fit in */
1304 while (aChild != NULL)
1306 INT comp;
1308 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1309 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1311 if (comp < 0) /* we are smaller than the current one */
1313 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1314 bItemInserted = TRUE;
1315 break;
1317 else if (comp > 0) /* we are bigger than the current one */
1319 previousChild = aChild;
1321 /* This will help us to exit if there is no more sibling */
1322 aChild = (aChild->nextSibling == 0)
1323 ? NULL
1324 : aChild->nextSibling;
1326 /* Look at the next item */
1327 continue;
1329 else if (comp == 0)
1332 * An item with this name is already existing, therefore,
1333 * we add after the one we found
1335 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1336 bItemInserted = TRUE;
1337 break;
1342 * we reach the end of the child list and the item has not
1343 * yet been inserted, therefore, insert it after the last child.
1345 if ((!bItemInserted) && (aChild == NULL))
1346 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1348 break;
1353 TRACE("new item %p; parent %p, mask %x\n", newItem,
1354 newItem->parent, tvItem->mask);
1356 newItem->iLevel = newItem->parent->iLevel + 1;
1358 if (newItem->parent->cChildren == 0)
1359 newItem->parent->cChildren = 1;
1361 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1363 if (STATEIMAGEINDEX(newItem->state) == 0)
1364 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1367 if (infoPtr->firstVisible == NULL)
1368 infoPtr->firstVisible = newItem;
1370 TREEVIEW_VerifyTree(infoPtr);
1372 if (parentItem == infoPtr->root ||
1373 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1375 TREEVIEW_ITEM *item;
1376 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1378 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1379 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1381 if (!bTextUpdated)
1382 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1384 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1385 TREEVIEW_UpdateScrollBars(infoPtr);
1387 * if the item was inserted in a visible part of the tree,
1388 * invalidate it, as well as those after it
1390 for (item = newItem;
1391 item != NULL;
1392 item = TREEVIEW_GetNextListItem(infoPtr, item))
1393 TREEVIEW_Invalidate(infoPtr, item);
1395 else
1397 newItem->visibleOrder = -1;
1399 /* refresh treeview if newItem is the first item inserted under parentItem */
1400 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1402 /* parent got '+' - update it */
1403 TREEVIEW_Invalidate(infoPtr, parentItem);
1407 return (LRESULT)newItem;
1410 /* Item Deletion ************************************************************/
1411 static void
1412 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1414 static void
1415 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1417 TREEVIEW_ITEM *kill = parentItem->firstChild;
1419 while (kill != NULL)
1421 TREEVIEW_ITEM *next = kill->nextSibling;
1423 TREEVIEW_RemoveItem(infoPtr, kill);
1425 kill = next;
1428 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1429 assert(parentItem->firstChild == NULL);
1430 assert(parentItem->lastChild == NULL);
1433 static void
1434 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1436 TREEVIEW_ITEM *parentItem = item->parent;
1438 assert(item != NULL);
1439 assert(item->parent != NULL); /* i.e. it must not be the root */
1441 if (parentItem->firstChild == item)
1442 parentItem->firstChild = item->nextSibling;
1444 if (parentItem->lastChild == item)
1445 parentItem->lastChild = item->prevSibling;
1447 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1448 && parentItem->cChildren > 0)
1449 parentItem->cChildren = 0;
1451 if (item->prevSibling)
1452 item->prevSibling->nextSibling = item->nextSibling;
1454 if (item->nextSibling)
1455 item->nextSibling->prevSibling = item->prevSibling;
1458 static void
1459 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1461 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1463 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1464 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1466 if (wineItem->firstChild)
1467 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1469 TREEVIEW_UnlinkItem(wineItem);
1471 infoPtr->uNumItems--;
1473 if (wineItem->pszText && wineItem->pszText != LPSTR_TEXTCALLBACKW)
1474 Free(wineItem->pszText);
1476 TREEVIEW_FreeItem(infoPtr, wineItem);
1480 /* Empty out the tree. */
1481 static void
1482 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1484 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1486 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1489 static LRESULT
1490 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1492 TREEVIEW_ITEM *newSelection = NULL;
1493 TREEVIEW_ITEM *newFirstVisible = NULL;
1494 TREEVIEW_ITEM *parent, *prev = NULL;
1495 BOOL visible = FALSE;
1497 if (wineItem == TVI_ROOT)
1499 TRACE("TVI_ROOT\n");
1500 parent = infoPtr->root;
1501 newSelection = NULL;
1502 visible = TRUE;
1503 TREEVIEW_RemoveTree(infoPtr);
1505 else
1507 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1508 return FALSE;
1510 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1511 parent = wineItem->parent;
1513 if (ISVISIBLE(wineItem))
1515 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1516 visible = TRUE;
1519 if (infoPtr->selectedItem != NULL
1520 && (wineItem == infoPtr->selectedItem
1521 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1523 if (wineItem->nextSibling)
1524 newSelection = wineItem->nextSibling;
1525 else if (wineItem->parent != infoPtr->root)
1526 newSelection = wineItem->parent;
1527 else
1528 newSelection = wineItem->prevSibling;
1529 TRACE("newSelection = %p\n", newSelection);
1532 if (infoPtr->firstVisible == wineItem)
1534 if (wineItem->nextSibling)
1535 newFirstVisible = wineItem->nextSibling;
1536 else if (wineItem->prevSibling)
1537 newFirstVisible = wineItem->prevSibling;
1538 else if (wineItem->parent != infoPtr->root)
1539 newFirstVisible = wineItem->parent;
1540 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1542 else
1543 newFirstVisible = infoPtr->firstVisible;
1545 TREEVIEW_RemoveItem(infoPtr, wineItem);
1548 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1549 if (!infoPtr->selectedItem && newSelection)
1551 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1552 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1555 /* Validate insertMark dropItem.
1556 * hotItem ??? - used for comparison only.
1558 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1559 infoPtr->insertMarkItem = 0;
1561 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1562 infoPtr->dropItem = 0;
1564 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1565 newFirstVisible = infoPtr->root->firstChild;
1567 TREEVIEW_VerifyTree(infoPtr);
1570 if (visible)
1572 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1573 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1574 TREEVIEW_UpdateScrollBars(infoPtr);
1575 TREEVIEW_Invalidate(infoPtr, NULL);
1577 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1579 /* parent lost '+/-' - update it */
1580 TREEVIEW_Invalidate(infoPtr, parent);
1583 return TRUE;
1587 /* Get/Set Messages *********************************************************/
1588 static LRESULT
1589 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1591 if(wParam)
1592 infoPtr->bRedraw = TRUE;
1593 else
1594 infoPtr->bRedraw = FALSE;
1596 return 0;
1599 static LRESULT
1600 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1602 TRACE("\n");
1603 return infoPtr->uIndent;
1606 static LRESULT
1607 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1609 TRACE("\n");
1611 if (newIndent < MINIMUM_INDENT)
1612 newIndent = MINIMUM_INDENT;
1614 if (infoPtr->uIndent != newIndent)
1616 infoPtr->uIndent = newIndent;
1617 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1618 TREEVIEW_UpdateScrollBars(infoPtr);
1619 TREEVIEW_Invalidate(infoPtr, NULL);
1622 return 0;
1626 static LRESULT
1627 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1629 TRACE("\n");
1630 return (LRESULT)infoPtr->hwndToolTip;
1633 static LRESULT
1634 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1636 HWND prevToolTip;
1638 TRACE("\n");
1639 prevToolTip = infoPtr->hwndToolTip;
1640 infoPtr->hwndToolTip = hwndTT;
1642 return (LRESULT)prevToolTip;
1645 static LRESULT
1646 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1648 BOOL rc = infoPtr->bNtfUnicode;
1649 infoPtr->bNtfUnicode = fUnicode;
1650 return rc;
1653 static LRESULT
1654 TREEVIEW_GetUnicodeFormat(TREEVIEW_INFO *infoPtr)
1656 return infoPtr->bNtfUnicode;
1659 static LRESULT
1660 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1662 return infoPtr->uScrollTime;
1665 static LRESULT
1666 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1668 UINT uOldScrollTime = infoPtr->uScrollTime;
1670 infoPtr->uScrollTime = min(uScrollTime, 100);
1672 return uOldScrollTime;
1676 static LRESULT
1677 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1679 TRACE("\n");
1681 switch (wParam)
1683 case (WPARAM)TVSIL_NORMAL:
1684 return (LRESULT)infoPtr->himlNormal;
1686 case (WPARAM)TVSIL_STATE:
1687 return (LRESULT)infoPtr->himlState;
1689 default:
1690 return 0;
1694 #define TVHEIGHT_MIN 16
1695 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1697 /* Compute the natural height for items. */
1698 static UINT
1699 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1701 TEXTMETRICW tm;
1702 HDC hdc = GetDC(0);
1703 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1704 UINT height;
1706 /* Height is the maximum of:
1707 * 16 (a hack because our fonts are tiny), and
1708 * The text height + border & margin, and
1709 * The size of the normal image list
1711 GetTextMetricsW(hdc, &tm);
1712 SelectObject(hdc, hOldFont);
1713 ReleaseDC(0, hdc);
1715 height = TVHEIGHT_MIN;
1716 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1717 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1718 if (height < infoPtr->normalImageHeight)
1719 height = infoPtr->normalImageHeight;
1720 return height;
1723 static LRESULT
1724 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1726 HIMAGELIST himlOld = 0;
1727 int oldWidth = infoPtr->normalImageWidth;
1728 int oldHeight = infoPtr->normalImageHeight;
1731 TRACE("%x,%p\n", wParam, himlNew);
1733 switch (wParam)
1735 case (WPARAM)TVSIL_NORMAL:
1736 himlOld = infoPtr->himlNormal;
1737 infoPtr->himlNormal = himlNew;
1739 if (himlNew != NULL)
1740 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1741 &infoPtr->normalImageHeight);
1742 else
1744 infoPtr->normalImageWidth = 0;
1745 infoPtr->normalImageHeight = 0;
1748 break;
1750 case (WPARAM)TVSIL_STATE:
1751 himlOld = infoPtr->himlState;
1752 infoPtr->himlState = himlNew;
1754 if (himlNew != NULL)
1755 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1756 &infoPtr->stateImageHeight);
1757 else
1759 infoPtr->stateImageWidth = 0;
1760 infoPtr->stateImageHeight = 0;
1763 break;
1766 if (oldWidth != infoPtr->normalImageWidth ||
1767 oldHeight != infoPtr->normalImageHeight)
1769 BOOL bRecalcVisible = FALSE;
1771 if (oldHeight != infoPtr->normalImageHeight &&
1772 !infoPtr->bHeightSet)
1774 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1775 bRecalcVisible = TRUE;
1778 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1779 infoPtr->normalImageWidth != infoPtr->uIndent)
1781 infoPtr->uIndent = infoPtr->normalImageWidth;
1782 bRecalcVisible = TRUE;
1785 if (bRecalcVisible)
1786 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1788 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1789 TREEVIEW_UpdateScrollBars(infoPtr);
1792 TREEVIEW_Invalidate(infoPtr, NULL);
1794 return (LRESULT)himlOld;
1797 static LRESULT
1798 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1800 INT prevHeight = infoPtr->uItemHeight;
1802 TRACE("%d \n", newHeight);
1803 if (newHeight == -1)
1805 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1806 infoPtr->bHeightSet = FALSE;
1808 else
1810 infoPtr->uItemHeight = newHeight;
1811 infoPtr->bHeightSet = TRUE;
1814 /* Round down, unless we support odd ("non even") heights. */
1815 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1816 infoPtr->uItemHeight &= ~1;
1818 if (infoPtr->uItemHeight != prevHeight)
1820 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1821 TREEVIEW_UpdateScrollBars(infoPtr);
1822 TREEVIEW_Invalidate(infoPtr, NULL);
1825 return prevHeight;
1828 static LRESULT
1829 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1831 TRACE("\n");
1832 return infoPtr->uItemHeight;
1836 static LRESULT
1837 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1839 TRACE("%p\n", infoPtr->hFont);
1840 return (LRESULT)infoPtr->hFont;
1844 static INT CALLBACK
1845 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1847 (void)unused;
1849 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1851 return 1;
1854 static LRESULT
1855 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1857 UINT uHeight = infoPtr->uItemHeight;
1859 TRACE("%p %i\n", hFont, bRedraw);
1861 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1863 DeleteObject(infoPtr->hBoldFont);
1864 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1865 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1867 if (!infoPtr->bHeightSet)
1868 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1870 if (uHeight != infoPtr->uItemHeight)
1871 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1873 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1875 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1876 TREEVIEW_UpdateScrollBars(infoPtr);
1878 if (bRedraw)
1879 TREEVIEW_Invalidate(infoPtr, NULL);
1881 return 0;
1885 static LRESULT
1886 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1888 TRACE("\n");
1889 return (LRESULT)infoPtr->clrLine;
1892 static LRESULT
1893 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1895 COLORREF prevColor = infoPtr->clrLine;
1897 TRACE("\n");
1898 infoPtr->clrLine = color;
1899 return (LRESULT)prevColor;
1903 static LRESULT
1904 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1906 TRACE("\n");
1907 return (LRESULT)infoPtr->clrText;
1910 static LRESULT
1911 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1913 COLORREF prevColor = infoPtr->clrText;
1915 TRACE("\n");
1916 infoPtr->clrText = color;
1918 if (infoPtr->clrText != prevColor)
1919 TREEVIEW_Invalidate(infoPtr, NULL);
1921 return (LRESULT)prevColor;
1925 static LRESULT
1926 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1928 TRACE("\n");
1929 return (LRESULT)infoPtr->clrBk;
1932 static LRESULT
1933 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1935 COLORREF prevColor = infoPtr->clrBk;
1937 TRACE("\n");
1938 infoPtr->clrBk = newColor;
1940 if (newColor != prevColor)
1941 TREEVIEW_Invalidate(infoPtr, NULL);
1943 return (LRESULT)prevColor;
1947 static LRESULT
1948 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1950 TRACE("\n");
1951 return (LRESULT)infoPtr->clrInsertMark;
1954 static LRESULT
1955 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1957 COLORREF prevColor = infoPtr->clrInsertMark;
1959 TRACE("%lx\n", color);
1960 infoPtr->clrInsertMark = color;
1962 return (LRESULT)prevColor;
1966 static LRESULT
1967 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1969 TRACE("%d %p\n", wParam, item);
1971 if (!TREEVIEW_ValidItem(infoPtr, item))
1972 return 0;
1974 infoPtr->insertBeforeorAfter = wParam;
1975 infoPtr->insertMarkItem = item;
1977 TREEVIEW_Invalidate(infoPtr, NULL);
1979 return 1;
1983 /************************************************************************
1984 * Some serious braindamage here. lParam is a pointer to both the
1985 * input HTREEITEM and the output RECT.
1987 static LRESULT
1988 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1990 TREEVIEW_ITEM *wineItem;
1991 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1993 TRACE("\n");
1995 * validate parameters
1997 if (pItem == NULL)
1998 return FALSE;
2000 wineItem = *pItem;
2001 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2002 return FALSE;
2005 * If wParam is TRUE return the text size otherwise return
2006 * the whole item size
2008 if (fTextRect)
2010 /* Windows does not send TVN_GETDISPINFO here. */
2012 lpRect->top = wineItem->rect.top;
2013 lpRect->bottom = wineItem->rect.bottom;
2015 lpRect->left = wineItem->textOffset;
2016 lpRect->right = wineItem->textOffset + wineItem->textWidth;
2018 else
2020 *lpRect = wineItem->rect;
2023 TRACE("%s [L:%ld R:%ld T:%ld B:%ld]\n", fTextRect ? "text" : "item",
2024 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
2026 return TRUE;
2029 static inline LRESULT
2030 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
2032 /* Suprise! This does not take integral height into account. */
2033 return infoPtr->clientHeight / infoPtr->uItemHeight;
2037 static LRESULT
2038 TREEVIEW_GetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2040 TREEVIEW_ITEM *wineItem;
2042 wineItem = tvItem->hItem;
2043 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2044 return FALSE;
2046 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2048 if (tvItem->mask & TVIF_CHILDREN)
2050 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2051 FIXME("I_CHILDRENCALLBACK not supported\n");
2052 tvItem->cChildren = wineItem->cChildren;
2055 if (tvItem->mask & TVIF_HANDLE)
2056 tvItem->hItem = wineItem;
2058 if (tvItem->mask & TVIF_IMAGE)
2059 tvItem->iImage = wineItem->iImage;
2061 if (tvItem->mask & TVIF_INTEGRAL)
2062 tvItem->iIntegral = wineItem->iIntegral;
2064 /* undocumented: windows ignores TVIF_PARAM and
2065 * * always sets lParam
2067 tvItem->lParam = wineItem->lParam;
2069 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2070 tvItem->iSelectedImage = wineItem->iSelectedImage;
2072 if (tvItem->mask & TVIF_STATE)
2073 /* Careful here - Windows ignores the stateMask when you get the state
2074 That contradicts the documentation, but makes more common sense, masking
2075 retrieval in this way seems overkill */
2076 tvItem->state = wineItem->state;
2078 if (tvItem->mask & TVIF_TEXT)
2080 if (isW)
2082 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2084 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2085 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2087 else
2089 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2092 else
2094 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2096 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2097 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2099 else
2101 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2102 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2106 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2107 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2109 return TRUE;
2112 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2113 * which is wrong. */
2114 static LRESULT
2115 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2117 TREEVIEW_ITEM *wineItem;
2118 TREEVIEW_ITEM originalItem;
2120 wineItem = tvItem->hItem;
2122 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2123 tvItem->mask);
2125 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2126 return FALSE;
2128 /* store the orignal item values */
2129 originalItem = *wineItem;
2131 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2132 return FALSE;
2134 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2135 if ((tvItem->mask & TVIF_TEXT
2136 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2137 && ISVISIBLE(wineItem))
2139 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2140 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2143 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2145 /* The refresh updates everything, but we can't wait until then. */
2146 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2148 /* if any of the item's values changed and it's not a callback, redraw the item */
2149 if (item_changed(&originalItem, wineItem, tvItem))
2151 if (tvItem->mask & TVIF_INTEGRAL)
2153 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2154 TREEVIEW_UpdateScrollBars(infoPtr);
2156 TREEVIEW_Invalidate(infoPtr, NULL);
2158 else
2160 TREEVIEW_UpdateScrollBars(infoPtr);
2161 TREEVIEW_Invalidate(infoPtr, wineItem);
2166 return TRUE;
2169 static LRESULT
2170 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2172 TRACE("\n");
2174 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2175 return 0;
2177 return (wineItem->state & mask);
2180 static LRESULT
2181 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2183 TREEVIEW_ITEM *retval;
2185 retval = 0;
2187 /* handle all the global data here */
2188 switch (which)
2190 case TVGN_CHILD: /* Special case: child of 0 is root */
2191 if (wineItem)
2192 break;
2193 /* fall through */
2194 case TVGN_ROOT:
2195 retval = infoPtr->root->firstChild;
2196 break;
2198 case TVGN_CARET:
2199 retval = infoPtr->selectedItem;
2200 break;
2202 case TVGN_FIRSTVISIBLE:
2203 retval = infoPtr->firstVisible;
2204 break;
2206 case TVGN_DROPHILITE:
2207 retval = infoPtr->dropItem;
2208 break;
2210 case TVGN_LASTVISIBLE:
2211 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2212 break;
2215 if (retval)
2217 TRACE("flags:%x, returns %p\n", which, retval);
2218 return (LRESULT)retval;
2221 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2223 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2224 return FALSE;
2226 switch (which)
2228 case TVGN_NEXT:
2229 retval = wineItem->nextSibling;
2230 break;
2231 case TVGN_PREVIOUS:
2232 retval = wineItem->prevSibling;
2233 break;
2234 case TVGN_PARENT:
2235 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2236 break;
2237 case TVGN_CHILD:
2238 retval = wineItem->firstChild;
2239 break;
2240 case TVGN_NEXTVISIBLE:
2241 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2242 break;
2243 case TVGN_PREVIOUSVISIBLE:
2244 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2245 break;
2246 default:
2247 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2248 break;
2251 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2252 return (LRESULT)retval;
2256 static LRESULT
2257 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2259 TRACE(" %d\n", infoPtr->uNumItems);
2260 return (LRESULT)infoPtr->uNumItems;
2263 static VOID
2264 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2266 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2268 static const unsigned int state_table[] = { 0, 2, 1 };
2270 unsigned int state;
2272 state = STATEIMAGEINDEX(item->state);
2273 TRACE("state:%x\n", state);
2274 item->state &= ~TVIS_STATEIMAGEMASK;
2276 if (state < 3)
2277 state = state_table[state];
2279 item->state |= INDEXTOSTATEIMAGEMASK(state);
2281 TRACE("state:%x\n", state);
2282 TREEVIEW_Invalidate(infoPtr, item);
2287 /* Painting *************************************************************/
2289 /* Draw the lines and expand button for an item. Also draws one section
2290 * of the line from item's parent to item's parent's next sibling. */
2291 static void
2292 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2294 LONG centerx, centery;
2295 BOOL lar = ((infoPtr->dwStyle
2296 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2297 > TVS_LINESATROOT);
2298 HBRUSH hbr, hbrOld;
2300 if (!lar && item->iLevel == 0)
2301 return;
2303 hbr = CreateSolidBrush(infoPtr->clrBk);
2304 hbrOld = SelectObject(hdc, hbr);
2306 centerx = (item->linesOffset + item->stateOffset) / 2;
2307 centery = (item->rect.top + item->rect.bottom) / 2;
2309 if (infoPtr->dwStyle & TVS_HASLINES)
2311 HPEN hOldPen, hNewPen;
2312 HTREEITEM parent;
2315 * Get a dotted grey pen
2317 hNewPen = CreatePen(PS_ALTERNATE, 0, infoPtr->clrLine);
2318 hOldPen = SelectObject(hdc, hNewPen);
2320 MoveToEx(hdc, item->stateOffset, centery, NULL);
2321 LineTo(hdc, centerx - 1, centery);
2323 if (item->prevSibling || item->parent != infoPtr->root)
2325 MoveToEx(hdc, centerx, item->rect.top, NULL);
2326 LineTo(hdc, centerx, centery);
2329 if (item->nextSibling)
2331 MoveToEx(hdc, centerx, centery, NULL);
2332 LineTo(hdc, centerx, item->rect.bottom + 1);
2335 /* Draw the line from our parent to its next sibling. */
2336 parent = item->parent;
2337 while (parent != infoPtr->root)
2339 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2341 if (parent->nextSibling
2342 /* skip top-levels unless TVS_LINESATROOT */
2343 && parent->stateOffset > parent->linesOffset)
2345 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2346 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2349 parent = parent->parent;
2352 SelectObject(hdc, hOldPen);
2353 DeleteObject(hNewPen);
2357 * Display the (+/-) signs
2360 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2362 if (item->cChildren)
2364 LONG height = item->rect.bottom - item->rect.top;
2365 LONG width = item->stateOffset - item->linesOffset;
2366 LONG rectsize = min(height, width) / 4;
2367 /* plussize = ceil(rectsize * 3/4) */
2368 LONG plussize = (rectsize + 1) * 3 / 4;
2370 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2371 HPEN hOldPen = SelectObject(hdc, hNewPen);
2373 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2374 centerx + rectsize + 2, centery + rectsize + 2);
2376 SelectObject(hdc, hOldPen);
2377 DeleteObject(hNewPen);
2379 if (height < 18 || width < 18)
2381 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2382 LineTo(hdc, centerx + plussize, centery);
2384 if (!(item->state & TVIS_EXPANDED))
2386 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2387 LineTo(hdc, centerx, centery + plussize);
2390 else
2392 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2393 centerx + plussize, centery + 2);
2395 if (!(item->state & TVIS_EXPANDED))
2397 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2398 centerx + 2, centery + plussize);
2399 SetPixel(hdc, centerx - 1, centery, infoPtr->clrBk);
2400 SetPixel(hdc, centerx + 1, centery, infoPtr->clrBk);
2405 SelectObject(hdc, hbrOld);
2406 DeleteObject(hbr);
2409 static void
2410 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2412 INT cditem;
2413 HFONT hOldFont;
2414 COLORREF oldTextColor, oldTextBkColor;
2415 int centery;
2416 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2417 NMTVCUSTOMDRAW nmcdhdr;
2419 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2421 /* - If item is drop target or it is selected and window is in focus -
2422 * use blue background (COLOR_HIGHLIGHT).
2423 * - If item is selected, window is not in focus, but it has style
2424 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2425 * - Otherwise - use background color
2427 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2428 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2429 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2431 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2433 nmcdhdr.clrTextBk = GetSysColor(COLOR_HIGHLIGHT);
2434 nmcdhdr.clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
2436 else
2438 nmcdhdr.clrTextBk = GetSysColor(COLOR_BTNFACE);
2439 if (infoPtr->clrText == -1)
2440 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2441 else
2442 nmcdhdr.clrText = infoPtr->clrText;
2445 else
2447 nmcdhdr.clrTextBk = infoPtr->clrBk;
2448 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2449 nmcdhdr.clrText = comctl32_color.clrHighlight;
2450 else if (infoPtr->clrText == -1)
2451 nmcdhdr.clrText = GetSysColor(COLOR_WINDOWTEXT);
2452 else
2453 nmcdhdr.clrText = infoPtr->clrText;
2456 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2458 /* The custom draw handler can query the text rectangle,
2459 * so get ready. */
2460 /* should already be known, set to 0 when changed */
2461 if (!wineItem->textWidth)
2462 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2464 cditem = 0;
2466 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2468 cditem = TREEVIEW_SendCustomDrawItemNotify
2469 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2470 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2472 if (cditem & CDRF_SKIPDEFAULT)
2474 SelectObject(hdc, hOldFont);
2475 return;
2479 if (cditem & CDRF_NEWFONT)
2480 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2482 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2484 /* Set colors. Custom draw handler can change these so we do this after it. */
2485 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2486 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2488 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2491 * Display the images associated with this item
2494 INT imageIndex;
2496 /* State images are displayed to the left of the Normal image
2497 * image number is in state; zero should be `display no image'.
2499 imageIndex = STATEIMAGEINDEX(wineItem->state);
2501 if (infoPtr->himlState && imageIndex)
2503 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2504 wineItem->stateOffset,
2505 centery - infoPtr->stateImageHeight / 2,
2506 ILD_NORMAL);
2509 /* Now, draw the normal image; can be either selected or
2510 * non-selected image.
2513 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2515 /* The item is currently selected */
2516 imageIndex = wineItem->iSelectedImage;
2518 else
2520 /* The item is not selected */
2521 imageIndex = wineItem->iImage;
2524 if (infoPtr->himlNormal)
2526 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2528 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2529 wineItem->imageOffset,
2530 centery - infoPtr->normalImageHeight / 2,
2531 ILD_NORMAL | ovlIdx);
2537 * Display the text associated with this item
2540 /* Don't paint item's text if it's being edited */
2541 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2543 if (wineItem->pszText)
2545 RECT rcText;
2547 rcText.top = wineItem->rect.top;
2548 rcText.bottom = wineItem->rect.bottom;
2549 rcText.left = wineItem->textOffset;
2550 rcText.right = rcText.left + wineItem->textWidth + 4;
2552 TRACE("drawing text %s at (%ld,%ld)-(%ld,%ld)\n",
2553 debugstr_w(wineItem->pszText),
2554 rcText.left, rcText.top, rcText.right, rcText.bottom);
2556 /* Draw it */
2557 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2558 ETO_CLIPPED | ETO_OPAQUE,
2559 &rcText,
2560 wineItem->pszText,
2561 lstrlenW(wineItem->pszText),
2562 NULL);
2564 /* Draw the box around the selected item */
2565 if ((wineItem == infoPtr->selectedItem) && inFocus)
2567 DrawFocusRect(hdc,&rcText);
2573 /* Draw insertion mark if necessary */
2575 if (infoPtr->insertMarkItem)
2576 TRACE("item:%d,mark:%d\n",
2577 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2578 (int)infoPtr->insertMarkItem);
2580 if (wineItem == infoPtr->insertMarkItem)
2582 HPEN hNewPen, hOldPen;
2583 int offset;
2584 int left, right;
2586 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2587 hOldPen = SelectObject(hdc, hNewPen);
2589 if (infoPtr->insertBeforeorAfter)
2590 offset = wineItem->rect.bottom - 1;
2591 else
2592 offset = wineItem->rect.top + 1;
2594 left = wineItem->textOffset - 2;
2595 right = wineItem->textOffset + wineItem->textWidth + 2;
2597 MoveToEx(hdc, left, offset - 3, NULL);
2598 LineTo(hdc, left, offset + 4);
2600 MoveToEx(hdc, left, offset, NULL);
2601 LineTo(hdc, right + 1, offset);
2603 MoveToEx(hdc, right, offset + 3, NULL);
2604 LineTo(hdc, right, offset - 4);
2606 SelectObject(hdc, hOldPen);
2607 DeleteObject(hNewPen);
2610 if (cditem & CDRF_NOTIFYPOSTPAINT)
2612 cditem = TREEVIEW_SendCustomDrawItemNotify
2613 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2614 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2617 /* Restore the hdc state */
2618 SetTextColor(hdc, oldTextColor);
2619 SetBkColor(hdc, oldTextBkColor);
2620 SelectObject(hdc, hOldFont);
2623 /* Computes treeHeight and treeWidth and updates the scroll bars.
2625 static void
2626 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2628 TREEVIEW_ITEM *wineItem;
2629 HWND hwnd = infoPtr->hwnd;
2630 BOOL vert = FALSE;
2631 BOOL horz = FALSE;
2632 SCROLLINFO si;
2633 LONG scrollX = infoPtr->scrollX;
2635 infoPtr->treeWidth = 0;
2636 infoPtr->treeHeight = 0;
2638 /* We iterate through all visible items in order to get the tree height
2639 * and width */
2640 wineItem = infoPtr->root->firstChild;
2642 while (wineItem != NULL)
2644 if (ISVISIBLE(wineItem))
2646 /* actually we draw text at textOffset + 2 */
2647 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2648 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2650 /* This is scroll-adjusted, but we fix this below. */
2651 infoPtr->treeHeight = wineItem->rect.bottom;
2654 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2657 /* Fix the scroll adjusted treeHeight and treeWidth. */
2658 if (infoPtr->root->firstChild)
2659 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2661 infoPtr->treeWidth += infoPtr->scrollX;
2663 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2665 /* Adding one scroll bar may take up enough space that it forces us
2666 * to add the other as well. */
2667 if (infoPtr->treeHeight > infoPtr->clientHeight)
2669 vert = TRUE;
2671 if (infoPtr->treeWidth
2672 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2673 horz = TRUE;
2675 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2676 horz = TRUE;
2678 if (!vert && horz && infoPtr->treeHeight
2679 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2680 vert = TRUE;
2682 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2684 si.cbSize = sizeof(SCROLLINFO);
2685 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2686 si.nMin = 0;
2688 if (vert)
2690 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2691 if ( si.nPage && NULL != infoPtr->firstVisible)
2693 si.nPos = infoPtr->firstVisible->visibleOrder;
2694 si.nMax = infoPtr->maxVisibleOrder - 1;
2696 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2698 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2699 ShowScrollBar(hwnd, SB_VERT, TRUE);
2700 infoPtr->uInternalStatus |= TV_VSCROLL;
2702 else
2704 if (infoPtr->uInternalStatus & TV_VSCROLL)
2705 ShowScrollBar(hwnd, SB_VERT, FALSE);
2706 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2709 else
2711 if (infoPtr->uInternalStatus & TV_VSCROLL)
2712 ShowScrollBar(hwnd, SB_VERT, FALSE);
2713 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2716 if (horz)
2718 si.nPage = infoPtr->clientWidth;
2719 si.nPos = infoPtr->scrollX;
2720 si.nMax = infoPtr->treeWidth - 1;
2722 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2724 si.nPos = si.nMax - max( si.nPage-1, 0 );
2725 scrollX = si.nPos;
2728 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2729 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2730 infoPtr->uInternalStatus |= TV_HSCROLL;
2732 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2734 else
2736 if (infoPtr->uInternalStatus & TV_HSCROLL)
2737 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2738 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2740 scrollX = 0;
2743 if (infoPtr->scrollX != scrollX)
2745 TREEVIEW_HScroll(infoPtr,
2746 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2749 if (!horz)
2750 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2753 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2754 static LRESULT
2755 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2757 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2758 RECT rect;
2760 GetClientRect(infoPtr->hwnd, &rect);
2761 FillRect(hDC, &rect, hBrush);
2762 DeleteObject(hBrush);
2764 return 1;
2767 static void
2768 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2770 HWND hwnd = infoPtr->hwnd;
2771 RECT rect = *rc;
2772 TREEVIEW_ITEM *wineItem;
2774 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2776 TRACE("empty window\n");
2777 return;
2780 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2781 hdc, rect);
2783 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2785 ReleaseDC(hwnd, hdc);
2786 return;
2789 for (wineItem = infoPtr->root->firstChild;
2790 wineItem != NULL;
2791 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2793 if (ISVISIBLE(wineItem))
2795 /* Avoid unneeded calculations */
2796 if (wineItem->rect.top > rect.bottom)
2797 break;
2798 if (wineItem->rect.bottom < rect.top)
2799 continue;
2801 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2805 TREEVIEW_UpdateScrollBars(infoPtr);
2807 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2808 infoPtr->cdmode =
2809 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2812 static void
2813 TREEVIEW_Invalidate(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2815 if (item != NULL)
2816 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2817 else
2818 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2821 static LRESULT
2822 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2824 HDC hdc;
2825 PAINTSTRUCT ps;
2826 RECT rc;
2828 TRACE("\n");
2830 if (wParam)
2832 hdc = (HDC)wParam;
2833 if (!GetUpdateRect(infoPtr->hwnd, &rc, TRUE))
2835 HBITMAP hbitmap;
2836 BITMAP bitmap;
2837 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2838 if (!hbitmap) return 0;
2839 GetObjectW(hbitmap, sizeof(BITMAP), &bitmap);
2840 rc.left = 0; rc.top = 0;
2841 rc.right = bitmap.bmWidth;
2842 rc.bottom = bitmap.bmHeight;
2843 TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
2846 else
2848 hdc = BeginPaint(infoPtr->hwnd, &ps);
2849 rc = ps.rcPaint;
2852 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2853 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2855 if (!wParam)
2856 EndPaint(infoPtr->hwnd, &ps);
2858 return 0;
2862 /* Sorting **************************************************************/
2864 /***************************************************************************
2865 * Forward the DPA local callback to the treeview owner callback
2867 static INT WINAPI
2868 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2870 /* Forward the call to the client-defined callback */
2871 return pCallBackSort->lpfnCompare(first->lParam,
2872 second->lParam,
2873 pCallBackSort->lParam);
2876 /***************************************************************************
2877 * Treeview native sort routine: sort on item text.
2879 static INT WINAPI
2880 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2881 TREEVIEW_INFO *infoPtr)
2883 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2884 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2886 if(first->pszText && second->pszText)
2887 return lstrcmpiW(first->pszText, second->pszText);
2888 else if(first->pszText)
2889 return -1;
2890 else if(second->pszText)
2891 return 1;
2892 else
2893 return 0;
2896 /* Returns the number of physical children belonging to item. */
2897 static INT
2898 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2900 INT cChildren = 0;
2901 HTREEITEM hti;
2903 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2904 cChildren++;
2906 return cChildren;
2909 /* Returns a DPA containing a pointer to each physical child of item in
2910 * sibling order. If item has no children, an empty DPA is returned. */
2911 static HDPA
2912 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2914 HTREEITEM child = item->firstChild;
2916 HDPA list = DPA_Create(8);
2917 if (list == 0) return NULL;
2919 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2921 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2923 DPA_Destroy(list);
2924 return NULL;
2928 return list;
2931 /***************************************************************************
2932 * Setup the treeview structure with regards of the sort method
2933 * and sort the children of the TV item specified in lParam
2934 * fRecurse: currently unused. Should be zero.
2935 * parent: if pSort!=NULL, should equal pSort->hParent.
2936 * otherwise, item which child items are to be sorted.
2937 * pSort: sort method info. if NULL, sort on item text.
2938 * if non-NULL, sort on item's lParam content, and let the
2939 * application decide what that means. See also TVM_SORTCHILDRENCB.
2942 static LRESULT
2943 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2944 LPTVSORTCB pSort)
2946 INT cChildren;
2947 PFNDPACOMPARE pfnCompare;
2948 LPARAM lpCompare;
2950 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2951 if (parent == TVI_ROOT || parent == NULL)
2952 parent = infoPtr->root;
2954 /* Check for a valid handle to the parent item */
2955 if (!TREEVIEW_ValidItem(infoPtr, parent))
2957 ERR("invalid item hParent=%x\n", (INT)parent);
2958 return FALSE;
2961 if (pSort)
2963 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2964 lpCompare = (LPARAM)pSort;
2966 else
2968 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2969 lpCompare = (LPARAM)infoPtr;
2972 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2974 /* Make sure there is something to sort */
2975 if (cChildren > 1)
2977 /* TREEVIEW_ITEM rechaining */
2978 INT count = 0;
2979 HTREEITEM item = 0;
2980 HTREEITEM nextItem = 0;
2981 HTREEITEM prevItem = 0;
2983 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2985 if (sortList == NULL)
2986 return FALSE;
2988 /* let DPA sort the list */
2989 DPA_Sort(sortList, pfnCompare, lpCompare);
2991 /* The order of DPA entries has been changed, so fixup the
2992 * nextSibling and prevSibling pointers. */
2994 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2995 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2997 /* link the two current item toghether */
2998 item->nextSibling = nextItem;
2999 nextItem->prevSibling = item;
3001 if (prevItem == NULL)
3003 /* this is the first item, update the parent */
3004 parent->firstChild = item;
3005 item->prevSibling = NULL;
3007 else
3009 /* fix the back chaining */
3010 item->prevSibling = prevItem;
3013 /* get ready for the next one */
3014 prevItem = item;
3015 item = nextItem;
3018 /* the last item is pointed to by item and never has a sibling */
3019 item->nextSibling = NULL;
3020 parent->lastChild = item;
3022 DPA_Destroy(sortList);
3024 TREEVIEW_VerifyTree(infoPtr);
3026 if (parent->state & TVIS_EXPANDED)
3028 int visOrder = infoPtr->firstVisible->visibleOrder;
3030 if (parent == infoPtr->root)
3031 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3032 else
3033 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3035 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3037 TREEVIEW_ITEM *item;
3039 for (item = infoPtr->root->firstChild; item != NULL;
3040 item = TREEVIEW_GetNextListItem(infoPtr, item))
3042 if (item->visibleOrder == visOrder)
3043 break;
3046 if (!item) item = parent->firstChild;
3047 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3050 TREEVIEW_Invalidate(infoPtr, NULL);
3053 return TRUE;
3055 return FALSE;
3059 /***************************************************************************
3060 * Setup the treeview structure with regards of the sort method
3061 * and sort the children of the TV item specified in lParam
3063 static LRESULT
3064 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3066 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3070 /***************************************************************************
3071 * Sort the children of the TV item specified in lParam.
3073 static LRESULT
3074 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3076 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3080 /* Expansion/Collapse ***************************************************/
3082 static BOOL
3083 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3084 UINT action)
3086 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3087 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3088 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3089 0, wineItem);
3092 static VOID
3093 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3094 UINT action)
3096 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3097 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3098 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3099 0, wineItem);
3103 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3104 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3105 static BOOL
3106 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3107 BOOL bRemoveChildren, BOOL bUser)
3109 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3110 BOOL bSetSelection, bSetFirstVisible;
3111 RECT scrollRect;
3112 LONG scrollDist = 0;
3113 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3115 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3117 if (!(wineItem->state & TVIS_EXPANDED))
3118 return FALSE;
3120 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3121 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3123 if (wineItem->firstChild == NULL)
3124 return FALSE;
3126 wineItem->state &= ~TVIS_EXPANDED;
3128 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3129 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3131 bSetSelection = (infoPtr->selectedItem != NULL
3132 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3134 bSetFirstVisible = (infoPtr->firstVisible != NULL
3135 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3137 tmpItem = wineItem;
3138 while (tmpItem)
3140 if (tmpItem->nextSibling)
3142 nextItem = tmpItem->nextSibling;
3143 break;
3145 tmpItem = tmpItem->parent;
3148 if (nextItem)
3149 scrollDist = nextItem->rect.top;
3151 if (bRemoveChildren)
3153 INT old_cChildren = wineItem->cChildren;
3154 TRACE("TVE_COLLAPSERESET\n");
3155 wineItem->state &= ~TVIS_EXPANDEDONCE;
3156 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3157 wineItem->cChildren = old_cChildren;
3160 if (wineItem->firstChild)
3162 TREEVIEW_ITEM *item, *sibling;
3164 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3166 for (item = wineItem->firstChild; item != sibling;
3167 item = TREEVIEW_GetNextListItem(infoPtr, item))
3169 item->visibleOrder = -1;
3173 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3175 if (nextItem)
3176 scrollDist = -(scrollDist - nextItem->rect.top);
3178 if (bSetSelection)
3180 /* Don't call DoSelectItem, it sends notifications. */
3181 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3182 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3183 wineItem->state |= TVIS_SELECTED;
3184 infoPtr->selectedItem = wineItem;
3187 TREEVIEW_UpdateScrollBars(infoPtr);
3189 scrollRect.left = 0;
3190 scrollRect.right = infoPtr->clientWidth;
3191 scrollRect.bottom = infoPtr->clientHeight;
3193 if (nextItem)
3195 scrollRect.top = nextItem->rect.top;
3197 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3198 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3199 TREEVIEW_Invalidate(infoPtr, wineItem);
3200 } else {
3201 scrollRect.top = wineItem->rect.top;
3202 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3205 TREEVIEW_SetFirstVisible(infoPtr,
3206 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3207 TRUE);
3209 return TRUE;
3212 static BOOL
3213 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3214 BOOL bExpandPartial, BOOL bUser)
3216 LONG scrollDist;
3217 LONG orgNextTop = 0;
3218 RECT scrollRect;
3219 TREEVIEW_ITEM *nextItem, *tmpItem;
3221 TRACE("\n");
3223 if (wineItem->state & TVIS_EXPANDED)
3224 return TRUE;
3226 tmpItem = wineItem; nextItem = NULL;
3227 while (tmpItem)
3229 if (tmpItem->nextSibling)
3231 nextItem = tmpItem->nextSibling;
3232 break;
3234 tmpItem = tmpItem->parent;
3237 if (nextItem)
3238 orgNextTop = nextItem->rect.top;
3240 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3242 if (bUser || ((wineItem->cChildren != 0) &&
3243 !(wineItem->state & TVIS_EXPANDEDONCE)))
3245 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3247 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3248 return FALSE;
3251 if (!wineItem->firstChild)
3252 return FALSE;
3254 wineItem->state |= TVIS_EXPANDED;
3255 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3256 wineItem->state |= TVIS_EXPANDEDONCE;
3258 else
3260 if (!wineItem->firstChild)
3261 return FALSE;
3263 /* this item has already been expanded */
3264 wineItem->state |= TVIS_EXPANDED;
3267 if (bExpandPartial)
3268 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3270 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3271 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3272 TREEVIEW_UpdateScrollBars(infoPtr);
3274 scrollRect.left = 0;
3275 scrollRect.bottom = infoPtr->treeHeight;
3276 scrollRect.right = infoPtr->clientWidth;
3277 if (nextItem)
3279 scrollDist = nextItem->rect.top - orgNextTop;
3280 scrollRect.top = orgNextTop;
3282 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3283 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3284 TREEVIEW_Invalidate (infoPtr, wineItem);
3285 } else {
3286 scrollRect.top = wineItem->rect.top;
3287 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3290 /* Scroll up so that as many children as possible are visible.
3291 * This fails when expanding causes an HScroll bar to appear, but we
3292 * don't know that yet, so the last item is obscured. */
3293 if (wineItem->firstChild != NULL)
3295 int nChildren = wineItem->lastChild->visibleOrder
3296 - wineItem->firstChild->visibleOrder + 1;
3298 int visible_pos = wineItem->visibleOrder
3299 - infoPtr->firstVisible->visibleOrder;
3301 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3303 if (visible_pos > 0 && nChildren > rows_below)
3305 int scroll = nChildren - rows_below;
3307 if (scroll > visible_pos)
3308 scroll = visible_pos;
3310 if (scroll > 0)
3312 TREEVIEW_ITEM *newFirstVisible
3313 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3314 scroll);
3317 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3322 return TRUE;
3325 static BOOL
3326 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3328 TRACE("\n");
3330 if (wineItem->state & TVIS_EXPANDED)
3331 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3332 else
3333 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3336 static VOID
3337 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3339 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3341 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3343 if (TREEVIEW_HasChildren(infoPtr, item))
3344 TREEVIEW_ExpandAll(infoPtr, item);
3348 /* Note:If the specified item is the child of a collapsed parent item,
3349 the parent's list of child items is (recursively) expanded to reveal the
3350 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3351 know if it also applies here.
3354 static LRESULT
3355 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3357 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3358 return 0;
3360 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3361 TREEVIEW_ItemName(wineItem), flag,
3362 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3364 switch (flag & TVE_TOGGLE)
3366 case TVE_COLLAPSE:
3367 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3368 FALSE);
3370 case TVE_EXPAND:
3371 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3372 FALSE);
3374 case TVE_TOGGLE:
3375 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3377 default:
3378 return 0;
3381 #if 0
3382 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3383 #endif
3386 /* Hit-Testing **********************************************************/
3388 static TREEVIEW_ITEM *
3389 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3391 TREEVIEW_ITEM *wineItem;
3392 LONG row;
3394 if (!infoPtr->firstVisible)
3395 return NULL;
3397 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3399 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3400 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3402 if (row >= wineItem->visibleOrder
3403 && row < wineItem->visibleOrder + wineItem->iIntegral)
3404 break;
3407 return wineItem;
3410 static LRESULT
3411 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3413 TREEVIEW_ITEM *wineItem;
3414 RECT rect;
3415 UINT status;
3416 LONG x, y;
3418 lpht->hItem = 0;
3419 GetClientRect(infoPtr->hwnd, &rect);
3420 status = 0;
3421 x = lpht->pt.x;
3422 y = lpht->pt.y;
3424 if (x < rect.left)
3426 status |= TVHT_TOLEFT;
3428 else if (x > rect.right)
3430 status |= TVHT_TORIGHT;
3433 if (y < rect.top)
3435 status |= TVHT_ABOVE;
3437 else if (y > rect.bottom)
3439 status |= TVHT_BELOW;
3442 if (status)
3444 lpht->flags = status;
3445 return (LRESULT)(HTREEITEM)NULL;
3448 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3449 if (!wineItem)
3451 lpht->flags = TVHT_NOWHERE;
3452 return (LRESULT)(HTREEITEM)NULL;
3455 if (x >= wineItem->textOffset + wineItem->textWidth)
3457 lpht->flags = TVHT_ONITEMRIGHT;
3459 else if (x >= wineItem->textOffset)
3461 lpht->flags = TVHT_ONITEMLABEL;
3463 else if (x >= wineItem->imageOffset)
3465 lpht->flags = TVHT_ONITEMICON;
3467 else if (x >= wineItem->stateOffset)
3469 lpht->flags = TVHT_ONITEMSTATEICON;
3471 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3473 lpht->flags = TVHT_ONITEMBUTTON;
3475 else
3477 lpht->flags = TVHT_ONITEMINDENT;
3480 lpht->hItem = wineItem;
3481 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3483 return (LRESULT)wineItem;
3486 /* Item Label Editing ***************************************************/
3488 static LRESULT
3489 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3491 return (LRESULT)infoPtr->hwndEdit;
3494 static LRESULT CALLBACK
3495 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3497 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3498 BOOL bCancel = FALSE;
3499 LRESULT rc;
3501 switch (uMsg)
3503 case WM_PAINT:
3504 TRACE("WM_PAINT start\n");
3505 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3506 lParam);
3507 TRACE("WM_PAINT done\n");
3508 return rc;
3510 case WM_KILLFOCUS:
3511 if (infoPtr->bIgnoreEditKillFocus)
3512 return TRUE;
3513 break;
3515 case WM_GETDLGCODE:
3516 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3518 case WM_KEYDOWN:
3519 if (wParam == (WPARAM)VK_ESCAPE)
3521 bCancel = TRUE;
3522 break;
3524 else if (wParam == (WPARAM)VK_RETURN)
3526 break;
3529 /* fall through */
3530 default:
3531 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3534 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3535 /* eg. Using a messagebox */
3537 infoPtr->bIgnoreEditKillFocus = TRUE;
3538 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3539 infoPtr->bIgnoreEditKillFocus = FALSE;
3541 return 0;
3545 /* should handle edit control messages here */
3547 static LRESULT
3548 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3550 TRACE("%x %ld\n", wParam, lParam);
3552 switch (HIWORD(wParam))
3554 case EN_UPDATE:
3557 * Adjust the edit window size
3559 WCHAR buffer[1024];
3560 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3561 HDC hdc = GetDC(infoPtr->hwndEdit);
3562 SIZE sz;
3563 int len;
3564 HFONT hFont, hOldFont = 0;
3566 infoPtr->bLabelChanged = TRUE;
3568 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer));
3570 /* Select font to get the right dimension of the string */
3571 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3573 if (hFont != 0)
3575 hOldFont = SelectObject(hdc, hFont);
3578 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3580 TEXTMETRICW textMetric;
3582 /* Add Extra spacing for the next character */
3583 GetTextMetricsW(hdc, &textMetric);
3584 sz.cx += (textMetric.tmMaxCharWidth * 2);
3586 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3587 sz.cx = min(sz.cx,
3588 infoPtr->clientWidth - editItem->textOffset + 2);
3590 SetWindowPos(infoPtr->hwndEdit,
3591 HWND_TOP,
3594 sz.cx,
3595 editItem->rect.bottom - editItem->rect.top + 3,
3596 SWP_NOMOVE | SWP_DRAWFRAME);
3599 if (hFont != 0)
3601 SelectObject(hdc, hOldFont);
3604 ReleaseDC(infoPtr->hwnd, hdc);
3605 break;
3608 default:
3609 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3612 return 0;
3615 static HWND
3616 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3618 HWND hwnd = infoPtr->hwnd;
3619 HWND hwndEdit;
3620 SIZE sz;
3621 TREEVIEW_ITEM *editItem = hItem;
3622 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3623 HDC hdc;
3624 HFONT hOldFont=0;
3625 TEXTMETRICW textMetric;
3626 static const WCHAR EditW[] = {'E','d','i','t',0};
3628 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3629 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3630 return NULL;
3632 if (infoPtr->hwndEdit)
3633 return infoPtr->hwndEdit;
3635 infoPtr->bLabelChanged = FALSE;
3637 /* Make sure that edit item is selected */
3638 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3639 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3641 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3643 hdc = GetDC(hwnd);
3644 /* Select the font to get appropriate metric dimensions */
3645 if (infoPtr->hFont != 0)
3647 hOldFont = SelectObject(hdc, infoPtr->hFont);
3650 /* Get string length in pixels */
3651 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3652 &sz);
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, infoPtr->clientWidth - editItem->textOffset + 2);
3661 if (infoPtr->hFont != 0)
3663 SelectObject(hdc, hOldFont);
3666 ReleaseDC(hwnd, hdc);
3667 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3668 EditW,
3670 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3671 WS_CLIPSIBLINGS | ES_WANTRETURN |
3672 ES_LEFT, editItem->textOffset - 2,
3673 editItem->rect.top - 1, sz.cx + 3,
3674 editItem->rect.bottom -
3675 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3676 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3678 infoPtr->hwndEdit = hwndEdit;
3680 /* Get a 2D border. */
3681 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3682 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3683 SetWindowLongW(hwndEdit, GWL_STYLE,
3684 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3686 SendMessageW(hwndEdit, WM_SETFONT,
3687 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3689 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3690 (DWORD_PTR)
3691 TREEVIEW_Edit_SubclassProc);
3693 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3695 DestroyWindow(hwndEdit);
3696 infoPtr->hwndEdit = 0;
3697 return NULL;
3700 infoPtr->selectedItem = hItem;
3701 SetWindowTextW(hwndEdit, editItem->pszText);
3702 SetFocus(hwndEdit);
3703 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3704 ShowWindow(hwndEdit, SW_SHOW);
3706 return hwndEdit;
3710 static LRESULT
3711 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3713 HWND hwnd = infoPtr->hwnd;
3714 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3715 NMTVDISPINFOW tvdi;
3716 BOOL bCommit;
3717 WCHAR tmpText[1024] = { '\0' };
3718 WCHAR *newText = tmpText;
3719 int iLength = 0;
3721 if (!infoPtr->hwndEdit)
3722 return FALSE;
3724 tvdi.hdr.hwndFrom = hwnd;
3725 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3726 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3727 tvdi.item.mask = 0;
3728 tvdi.item.hItem = editedItem;
3729 tvdi.item.state = editedItem->state;
3730 tvdi.item.lParam = editedItem->lParam;
3732 if (!bCancel)
3734 if (!infoPtr->bNtfUnicode)
3735 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3736 else
3737 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3739 if (iLength >= 1023)
3741 ERR("Insufficient space to retrieve new item label\n");
3744 tvdi.item.mask = TVIF_TEXT;
3745 tvdi.item.pszText = tmpText;
3746 tvdi.item.cchTextMax = iLength + 1;
3748 else
3750 tvdi.item.pszText = NULL;
3751 tvdi.item.cchTextMax = 0;
3754 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
3755 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3757 if (!bCancel && bCommit) /* Apply the changes */
3759 if (!infoPtr->bNtfUnicode)
3761 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3762 newText = Alloc(len * sizeof(WCHAR));
3763 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3764 iLength = len - 1;
3767 if (strcmpW(newText, editedItem->pszText) != 0)
3769 if (NULL == ReAlloc(editedItem->pszText, iLength + 1))
3771 ERR("OutOfMemory, cannot allocate space for label\n");
3772 DestroyWindow(infoPtr->hwndEdit);
3773 infoPtr->hwndEdit = 0;
3774 return FALSE;
3776 else
3778 editedItem->cchTextMax = iLength + 1;
3779 strcpyW(editedItem->pszText, newText);
3782 if(newText != tmpText) Free(newText);
3785 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3786 DestroyWindow(infoPtr->hwndEdit);
3787 infoPtr->hwndEdit = 0;
3788 return TRUE;
3791 static LRESULT
3792 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3794 if (wParam != TV_EDIT_TIMER)
3796 ERR("got unknown timer\n");
3797 return 1;
3800 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3801 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3803 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3805 return 0;
3809 /* Mouse Tracking/Drag **************************************************/
3811 /***************************************************************************
3812 * This is quite unusual piece of code, but that's how it's implemented in
3813 * Windows.
3815 static LRESULT
3816 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3818 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3819 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3820 RECT r;
3821 MSG msg;
3823 r.top = pt.y - cyDrag;
3824 r.left = pt.x - cxDrag;
3825 r.bottom = pt.y + cyDrag;
3826 r.right = pt.x + cxDrag;
3828 SetCapture(infoPtr->hwnd);
3830 while (1)
3832 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3834 if (msg.message == WM_MOUSEMOVE)
3836 pt.x = (short)LOWORD(msg.lParam);
3837 pt.y = (short)HIWORD(msg.lParam);
3838 if (PtInRect(&r, pt))
3839 continue;
3840 else
3842 ReleaseCapture();
3843 return 1;
3846 else if (msg.message >= WM_LBUTTONDOWN &&
3847 msg.message <= WM_RBUTTONDBLCLK)
3849 if (msg.message == WM_RBUTTONUP)
3850 TREEVIEW_RButtonUp(infoPtr, &pt);
3851 break;
3854 DispatchMessageW(&msg);
3857 if (GetCapture() != infoPtr->hwnd)
3858 return 0;
3861 ReleaseCapture();
3862 return 0;
3866 static LRESULT
3867 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3869 TREEVIEW_ITEM *wineItem;
3870 TVHITTESTINFO hit;
3872 TRACE("\n");
3873 SetFocus(infoPtr->hwnd);
3875 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3877 /* If there is pending 'edit label' event - kill it now */
3878 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3881 hit.pt.x = (short)LOWORD(lParam);
3882 hit.pt.y = (short)HIWORD(lParam);
3884 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3885 if (!wineItem)
3886 return 0;
3887 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3889 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3890 { /* FIXME! */
3891 switch (hit.flags)
3893 case TVHT_ONITEMRIGHT:
3894 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3895 break;
3897 case TVHT_ONITEMINDENT:
3898 if (!(infoPtr->dwStyle & TVS_HASLINES))
3900 break;
3902 else
3904 int level = hit.pt.x / infoPtr->uIndent;
3905 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3907 while (wineItem->iLevel > level)
3909 wineItem = wineItem->parent;
3912 /* fall through */
3915 case TVHT_ONITEMLABEL:
3916 case TVHT_ONITEMICON:
3917 case TVHT_ONITEMBUTTON:
3918 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3919 break;
3921 case TVHT_ONITEMSTATEICON:
3922 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3923 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3924 else
3925 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3926 break;
3929 return TRUE;
3933 static LRESULT
3934 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3936 HWND hwnd = infoPtr->hwnd;
3937 TVHITTESTINFO ht;
3938 BOOL bTrack, bDoLabelEdit;
3939 HTREEITEM tempItem;
3941 /* If Edit control is active - kill it and return.
3942 * The best way to do it is to set focus to itself.
3943 * Edit control subclassed procedure will automatically call
3944 * EndEditLabelNow.
3946 if (infoPtr->hwndEdit)
3948 SetFocus(hwnd);
3949 return 0;
3952 ht.pt.x = (short)LOWORD(lParam);
3953 ht.pt.y = (short)HIWORD(lParam);
3955 TREEVIEW_HitTest(infoPtr, &ht);
3956 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3958 /* update focusedItem and redraw both items */
3959 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3961 infoPtr->focusedItem = ht.hItem;
3962 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3964 if(infoPtr->selectedItem)
3965 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3968 bTrack = (ht.flags & TVHT_ONITEM)
3969 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3972 * If the style allows editing and the node is already selected
3973 * and the click occurred on the item label...
3975 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
3976 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
3978 /* Send NM_CLICK right away */
3979 if (!bTrack)
3980 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3981 goto setfocus;
3983 if (ht.flags & TVHT_ONITEMBUTTON)
3985 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3986 goto setfocus;
3988 else if (bTrack)
3989 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
3990 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3992 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
3993 infoPtr->dropItem = ht.hItem;
3995 /* clean up focusedItem as we dragged and won't select this item */
3996 if(infoPtr->focusedItem)
3998 /* refresh the item that was focused */
3999 tempItem = infoPtr->focusedItem;
4000 infoPtr->focusedItem = 0;
4001 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
4003 /* refresh the selected item to return the filled background */
4004 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
4007 return 0;
4011 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4012 goto setfocus;
4014 if (bDoLabelEdit)
4016 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4017 KillTimer(hwnd, TV_EDIT_TIMER);
4019 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4020 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4022 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4025 * if we are TVS_SINGLEEXPAND then we want this single click to
4026 * do a bunch of things.
4028 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4029 (infoPtr->hwndEdit == 0))
4031 TREEVIEW_ITEM *SelItem;
4034 * Send the notification
4036 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4039 * Close the previous selection all the way to the root
4040 * as long as the new selection is not a child
4042 if((infoPtr->selectedItem)
4043 && (infoPtr->selectedItem != ht.hItem))
4045 BOOL closeit = TRUE;
4046 SelItem = ht.hItem;
4048 /* determine if the hitItem is a child of the currently selected item */
4049 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4051 closeit = (SelItem != infoPtr->selectedItem);
4052 SelItem = SelItem->parent;
4055 if(closeit)
4057 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4058 SelItem = infoPtr->selectedItem;
4060 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4062 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4063 SelItem = SelItem->parent;
4069 * Expand the current item
4071 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4074 /* Select the current item */
4075 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4077 else if (ht.flags & TVHT_ONITEMSTATEICON)
4079 /* TVS_CHECKBOXES requires us to toggle the current state */
4080 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4081 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4084 setfocus:
4085 SetFocus(hwnd);
4086 return 0;
4090 static LRESULT
4091 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4093 TVHITTESTINFO ht;
4095 if (infoPtr->hwndEdit)
4097 SetFocus(infoPtr->hwnd);
4098 return 0;
4101 ht.pt.x = (short)LOWORD(lParam);
4102 ht.pt.y = (short)HIWORD(lParam);
4104 TREEVIEW_HitTest(infoPtr, &ht);
4106 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4108 if (ht.hItem)
4110 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4111 infoPtr->dropItem = ht.hItem;
4114 else
4116 SetFocus(infoPtr->hwnd);
4117 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4120 return 0;
4123 static LRESULT
4124 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
4126 return 0;
4130 static LRESULT
4131 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4133 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4134 INT cx, cy;
4135 HDC hdc, htopdc;
4136 HWND hwtop;
4137 HBITMAP hbmp, hOldbmp;
4138 SIZE size;
4139 RECT rc;
4140 HFONT hOldFont;
4142 TRACE("\n");
4144 if (!(infoPtr->himlNormal))
4145 return 0;
4147 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4148 return 0;
4150 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4152 hwtop = GetDesktopWindow();
4153 htopdc = GetDC(hwtop);
4154 hdc = CreateCompatibleDC(htopdc);
4156 hOldFont = SelectObject(hdc, infoPtr->hFont);
4157 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4158 &size);
4159 TRACE("%ld %ld %s %d\n", size.cx, size.cy, debugstr_w(dragItem->pszText),
4160 strlenW(dragItem->pszText));
4161 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4162 hOldbmp = SelectObject(hdc, hbmp);
4164 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4165 size.cx += cx;
4166 if (cy > size.cy)
4167 size.cy = cy;
4169 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4170 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4171 ILD_NORMAL);
4174 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4175 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4178 /* draw item text */
4180 SetRect(&rc, cx, 0, size.cx, size.cy);
4181 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4182 DT_LEFT);
4183 SelectObject(hdc, hOldFont);
4184 SelectObject(hdc, hOldbmp);
4186 ImageList_Add(infoPtr->dragList, hbmp, 0);
4188 DeleteDC(hdc);
4189 DeleteObject(hbmp);
4190 ReleaseDC(hwtop, htopdc);
4192 return (LRESULT)infoPtr->dragList;
4195 /* Selection ************************************************************/
4197 static LRESULT
4198 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4199 INT cause)
4201 TREEVIEW_ITEM *prevSelect;
4202 RECT rcFocused;
4204 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4206 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4207 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4208 newSelect ? newSelect->state : 0);
4210 /* reset and redraw focusedItem if focusedItem was set so we don't */
4211 /* have to worry about the previously focused item when we set a new one */
4212 if(infoPtr->focusedItem)
4214 rcFocused = (infoPtr->focusedItem)->rect;
4215 infoPtr->focusedItem = 0;
4216 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4219 switch (action)
4221 case TVGN_CARET:
4222 prevSelect = infoPtr->selectedItem;
4224 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4225 TVN_SELCHANGINGW,
4226 cause,
4227 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4228 prevSelect,
4229 newSelect))
4230 return FALSE;
4232 if (prevSelect)
4233 prevSelect->state &= ~TVIS_SELECTED;
4234 if (newSelect)
4235 newSelect->state |= TVIS_SELECTED;
4237 infoPtr->selectedItem = newSelect;
4239 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4241 if (prevSelect)
4242 TREEVIEW_Invalidate(infoPtr, prevSelect);
4243 if (newSelect)
4244 TREEVIEW_Invalidate(infoPtr, newSelect);
4246 TREEVIEW_SendTreeviewNotify(infoPtr,
4247 TVN_SELCHANGEDW,
4248 cause,
4249 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4250 prevSelect,
4251 newSelect);
4252 break;
4254 case TVGN_DROPHILITE:
4255 prevSelect = infoPtr->dropItem;
4257 if (prevSelect)
4258 prevSelect->state &= ~TVIS_DROPHILITED;
4260 infoPtr->dropItem = newSelect;
4262 if (newSelect)
4263 newSelect->state |= TVIS_DROPHILITED;
4265 TREEVIEW_Invalidate(infoPtr, prevSelect);
4266 TREEVIEW_Invalidate(infoPtr, newSelect);
4267 break;
4269 case TVGN_FIRSTVISIBLE:
4270 if (newSelect != NULL)
4272 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4273 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4274 TREEVIEW_Invalidate(infoPtr, NULL);
4276 break;
4279 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4280 return TRUE;
4283 /* FIXME: handle NM_KILLFOCUS etc */
4284 static LRESULT
4285 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4287 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4288 return FALSE;
4290 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4292 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4293 return FALSE;
4295 return TRUE;
4298 /*************************************************************************
4299 * TREEVIEW_ProcessLetterKeys
4301 * Processes keyboard messages generated by pressing the letter keys
4302 * on the keyboard.
4303 * What this does is perform a case insensitive search from the
4304 * current position with the following quirks:
4305 * - If two chars or more are pressed in quick succession we search
4306 * for the corresponding string (e.g. 'abc').
4307 * - If there is a delay we wipe away the current search string and
4308 * restart with just that char.
4309 * - If the user keeps pressing the same character, whether slowly or
4310 * fast, so that the search string is entirely composed of this
4311 * character ('aaaaa' for instance), then we search for first item
4312 * that starting with that character.
4313 * - If the user types the above character in quick succession, then
4314 * we must also search for the corresponding string ('aaaaa'), and
4315 * go to that string if there is a match.
4317 * RETURNS
4319 * Zero.
4321 * BUGS
4323 * - The current implementation has a list of characters it will
4324 * accept and it ignores averything else. In particular it will
4325 * ignore accentuated characters which seems to match what
4326 * Windows does. But I'm not sure it makes sense to follow
4327 * Windows there.
4328 * - We don't sound a beep when the search fails.
4329 * - The search should start from the focused item, not from the selected
4330 * item. One reason for this is to allow for multiple selections in trees.
4331 * But currently infoPtr->focusedItem does not seem very usable.
4333 * SEE ALSO
4335 * TREEVIEW_ProcessLetterKeys
4337 static INT TREEVIEW_ProcessLetterKeys(
4338 HWND hwnd, /* handle to the window */
4339 WPARAM charCode, /* the character code, the actual character */
4340 LPARAM keyData /* key data */
4343 TREEVIEW_INFO *infoPtr;
4344 HTREEITEM nItem;
4345 HTREEITEM endidx,idx;
4346 TVITEMEXW item;
4347 WCHAR buffer[MAX_PATH];
4348 DWORD timestamp,elapsed;
4350 /* simple parameter checking */
4351 if (!hwnd || !charCode || !keyData)
4352 return 0;
4354 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4355 if (!infoPtr)
4356 return 0;
4358 /* only allow the valid WM_CHARs through */
4359 if (!isalnum(charCode) &&
4360 charCode != '.' && charCode != '`' && charCode != '!' &&
4361 charCode != '@' && charCode != '#' && charCode != '$' &&
4362 charCode != '%' && charCode != '^' && charCode != '&' &&
4363 charCode != '*' && charCode != '(' && charCode != ')' &&
4364 charCode != '-' && charCode != '_' && charCode != '+' &&
4365 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4366 charCode != '}' && charCode != '[' && charCode != '{' &&
4367 charCode != '/' && charCode != '?' && charCode != '>' &&
4368 charCode != '<' && charCode != ',' && charCode != '~')
4369 return 0;
4371 /* compute how much time elapsed since last keypress */
4372 timestamp = GetTickCount();
4373 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4374 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4375 } else {
4376 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4379 /* update the search parameters */
4380 infoPtr->lastKeyPressTimestamp=timestamp;
4381 if (elapsed < KEY_DELAY) {
4382 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4383 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4385 if (infoPtr->charCode != charCode) {
4386 infoPtr->charCode=charCode=0;
4388 } else {
4389 infoPtr->charCode=charCode;
4390 infoPtr->szSearchParam[0]=charCode;
4391 infoPtr->nSearchParamLength=1;
4392 /* Redundant with the 1 char string */
4393 charCode=0;
4396 /* and search from the current position */
4397 nItem=NULL;
4398 if (infoPtr->selectedItem != NULL) {
4399 endidx=infoPtr->selectedItem;
4400 /* if looking for single character match,
4401 * then we must always move forward
4403 if (infoPtr->nSearchParamLength == 1)
4404 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4405 else
4406 idx=endidx;
4407 } else {
4408 endidx=NULL;
4409 idx=infoPtr->root->firstChild;
4411 do {
4412 /* At the end point, sort out wrapping */
4413 if (idx == NULL) {
4415 /* If endidx is null, stop at the last item (ie top to bottom) */
4416 if (endidx == NULL)
4417 break;
4419 /* Otherwise, start again at the very beginning */
4420 idx=infoPtr->root->firstChild;
4422 /* But if we are stopping on the first child, end now! */
4423 if (idx == endidx) break;
4426 /* get item */
4427 ZeroMemory(&item, sizeof(item));
4428 item.mask = TVIF_TEXT;
4429 item.hItem = idx;
4430 item.pszText = buffer;
4431 item.cchTextMax = sizeof(buffer);
4432 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4434 /* check for a match */
4435 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4436 nItem=idx;
4437 break;
4438 } else if ( (charCode != 0) && (nItem == NULL) &&
4439 (nItem != infoPtr->selectedItem) &&
4440 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4441 /* This would work but we must keep looking for a longer match */
4442 nItem=idx;
4444 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4445 } while (idx != endidx);
4447 if (nItem != NULL) {
4448 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4449 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4453 return 0;
4456 /* Scrolling ************************************************************/
4458 static LRESULT
4459 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4461 int viscount;
4462 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4463 HTREEITEM newFirstVisible = NULL;
4464 int visible_pos = -1;
4466 if (!TREEVIEW_ValidItem(infoPtr, item))
4467 return FALSE;
4469 if (!ISVISIBLE(item))
4471 /* Expand parents as necessary. */
4472 HTREEITEM parent;
4474 /* see if we are trying to ensure that root is vislble */
4475 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4476 parent = item->parent;
4477 else
4478 parent = item; /* this item is the topmost item */
4480 while (parent != infoPtr->root)
4482 if (!(parent->state & TVIS_EXPANDED))
4483 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4485 parent = parent->parent;
4489 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4491 TRACE("%p (%s) %ld - %ld viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4492 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4494 if (hasFirstVisible)
4495 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4497 if (visible_pos < 0)
4499 /* item is before the start of the list: put it at the top. */
4500 newFirstVisible = item;
4502 else if (visible_pos >= viscount
4503 /* Sometimes, before we are displayed, GVC is 0, causing us to
4504 * spuriously scroll up. */
4505 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4507 /* item is past the end of the list. */
4508 int scroll = visible_pos - viscount;
4510 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4511 scroll + 1);
4514 if (bHScroll)
4516 /* Scroll window so item's text is visible as much as possible */
4517 /* Calculation of amount of extra space is taken from EditLabel code */
4518 INT pos, x;
4519 TEXTMETRICW textMetric;
4520 HDC hdc = GetWindowDC(infoPtr->hwnd);
4522 x = item->textWidth;
4524 GetTextMetricsW(hdc, &textMetric);
4525 ReleaseDC(infoPtr->hwnd, hdc);
4527 x += (textMetric.tmMaxCharWidth * 2);
4528 x = max(x, textMetric.tmMaxCharWidth * 3);
4530 if (item->textOffset < 0)
4531 pos = item->textOffset;
4532 else if (item->textOffset + x > infoPtr->clientWidth)
4534 if (x > infoPtr->clientWidth)
4535 pos = item->textOffset;
4536 else
4537 pos = item->textOffset + x - infoPtr->clientWidth;
4539 else
4540 pos = 0;
4542 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4545 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4547 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4549 return TRUE;
4552 return FALSE;
4555 static VOID
4556 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4557 TREEVIEW_ITEM *newFirstVisible,
4558 BOOL bUpdateScrollPos)
4560 int gap_size;
4562 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4564 if (newFirstVisible != NULL)
4566 /* Prevent an empty gap from appearing at the bottom... */
4567 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4568 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4570 if (gap_size > 0)
4572 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4573 -gap_size);
4575 /* ... unless we just don't have enough items. */
4576 if (newFirstVisible == NULL)
4577 newFirstVisible = infoPtr->root->firstChild;
4581 if (infoPtr->firstVisible != newFirstVisible)
4583 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4585 infoPtr->firstVisible = newFirstVisible;
4586 TREEVIEW_Invalidate(infoPtr, NULL);
4588 else
4590 TREEVIEW_ITEM *item;
4591 int scroll = infoPtr->uItemHeight *
4592 (infoPtr->firstVisible->visibleOrder
4593 - newFirstVisible->visibleOrder);
4595 infoPtr->firstVisible = newFirstVisible;
4597 for (item = infoPtr->root->firstChild; item != NULL;
4598 item = TREEVIEW_GetNextListItem(infoPtr, item))
4600 item->rect.top += scroll;
4601 item->rect.bottom += scroll;
4604 if (bUpdateScrollPos)
4605 SetScrollPos(infoPtr->hwnd, SB_VERT,
4606 newFirstVisible->visibleOrder, TRUE);
4608 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4613 /************************************************************************
4614 * VScroll is always in units of visible items. i.e. we always have a
4615 * visible item aligned to the top of the control. (Unless we have no
4616 * items at all.)
4618 static LRESULT
4619 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4621 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4622 TREEVIEW_ITEM *newFirstVisible = NULL;
4624 int nScrollCode = LOWORD(wParam);
4626 TRACE("wp %x\n", wParam);
4628 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4629 return 0;
4631 if (infoPtr->hwndEdit)
4632 SetFocus(infoPtr->hwnd);
4634 if (!oldFirstVisible)
4636 assert(infoPtr->root->firstChild == NULL);
4637 return 0;
4640 switch (nScrollCode)
4642 case SB_TOP:
4643 newFirstVisible = infoPtr->root->firstChild;
4644 break;
4646 case SB_BOTTOM:
4647 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4648 break;
4650 case SB_LINEUP:
4651 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4652 break;
4654 case SB_LINEDOWN:
4655 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4656 break;
4658 case SB_PAGEUP:
4659 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4660 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4661 break;
4663 case SB_PAGEDOWN:
4664 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4665 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4666 break;
4668 case SB_THUMBTRACK:
4669 case SB_THUMBPOSITION:
4670 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4671 infoPtr->root->firstChild,
4672 (LONG)(SHORT)HIWORD(wParam));
4673 break;
4675 case SB_ENDSCROLL:
4676 return 0;
4679 if (newFirstVisible != NULL)
4681 if (newFirstVisible != oldFirstVisible)
4682 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4683 nScrollCode != SB_THUMBTRACK);
4684 else if (nScrollCode == SB_THUMBPOSITION)
4685 SetScrollPos(infoPtr->hwnd, SB_VERT,
4686 newFirstVisible->visibleOrder, TRUE);
4689 return 0;
4692 static LRESULT
4693 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4695 int maxWidth;
4696 int scrollX = infoPtr->scrollX;
4697 int nScrollCode = LOWORD(wParam);
4699 TRACE("wp %x\n", wParam);
4701 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4702 return FALSE;
4704 if (infoPtr->hwndEdit)
4705 SetFocus(infoPtr->hwnd);
4707 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4708 /* shall never occur */
4709 if (maxWidth <= 0)
4711 scrollX = 0;
4712 goto scroll;
4715 switch (nScrollCode)
4717 case SB_LINELEFT:
4718 scrollX -= infoPtr->uItemHeight;
4719 break;
4720 case SB_LINERIGHT:
4721 scrollX += infoPtr->uItemHeight;
4722 break;
4723 case SB_PAGELEFT:
4724 scrollX -= infoPtr->clientWidth;
4725 break;
4726 case SB_PAGERIGHT:
4727 scrollX += infoPtr->clientWidth;
4728 break;
4730 case SB_THUMBTRACK:
4731 case SB_THUMBPOSITION:
4732 scrollX = (int)(SHORT)HIWORD(wParam);
4733 break;
4735 case SB_ENDSCROLL:
4736 return 0;
4739 if (scrollX > maxWidth)
4740 scrollX = maxWidth;
4741 else if (scrollX < 0)
4742 scrollX = 0;
4744 scroll:
4745 if (scrollX != infoPtr->scrollX)
4747 TREEVIEW_ITEM *item;
4748 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4750 for (item = infoPtr->root->firstChild; item != NULL;
4751 item = TREEVIEW_GetNextListItem(infoPtr, item))
4753 item->linesOffset += scroll_pixels;
4754 item->stateOffset += scroll_pixels;
4755 item->imageOffset += scroll_pixels;
4756 item->textOffset += scroll_pixels;
4759 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4760 infoPtr->scrollX = scrollX;
4761 UpdateWindow(infoPtr->hwnd);
4764 if (nScrollCode != SB_THUMBTRACK)
4765 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4767 return 0;
4770 static LRESULT
4771 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4773 short gcWheelDelta;
4774 UINT pulScrollLines = 3;
4776 if (infoPtr->firstVisible == NULL)
4777 return TRUE;
4779 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4781 gcWheelDelta = -(short)HIWORD(wParam);
4782 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4784 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4786 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4787 int maxDy = infoPtr->maxVisibleOrder;
4789 if (newDy > maxDy)
4790 newDy = maxDy;
4792 if (newDy < 0)
4793 newDy = 0;
4795 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4797 return TRUE;
4800 /* Create/Destroy *******************************************************/
4802 static LRESULT
4803 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4805 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
4806 RECT rcClient;
4807 TREEVIEW_INFO *infoPtr;
4808 LOGFONTW lf;
4810 TRACE("wnd %p, style %lx\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4812 infoPtr = (TREEVIEW_INFO *)Alloc(sizeof(TREEVIEW_INFO));
4814 if (infoPtr == NULL)
4816 ERR("could not allocate info memory!\n");
4817 return 0;
4820 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4822 infoPtr->hwnd = hwnd;
4823 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4824 infoPtr->Timer = 0;
4825 infoPtr->uNumItems = 0;
4826 infoPtr->cdmode = 0;
4827 infoPtr->uScrollTime = 300; /* milliseconds */
4828 infoPtr->bRedraw = TRUE;
4830 GetClientRect(hwnd, &rcClient);
4832 /* No scroll bars yet. */
4833 infoPtr->clientWidth = rcClient.right;
4834 infoPtr->clientHeight = rcClient.bottom;
4835 infoPtr->uInternalStatus = 0;
4837 infoPtr->treeWidth = 0;
4838 infoPtr->treeHeight = 0;
4840 infoPtr->uIndent = MINIMUM_INDENT;
4841 infoPtr->selectedItem = 0;
4842 infoPtr->focusedItem = 0;
4843 infoPtr->hotItem = 0;
4844 infoPtr->firstVisible = 0;
4845 infoPtr->maxVisibleOrder = 0;
4846 infoPtr->dropItem = 0;
4847 infoPtr->insertMarkItem = 0;
4848 infoPtr->insertBeforeorAfter = 0;
4849 /* dragList */
4851 infoPtr->scrollX = 0;
4853 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4854 infoPtr->clrText = -1; /* use system color */
4855 infoPtr->clrLine = RGB(128, 128, 128);
4856 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4858 /* hwndToolTip */
4860 infoPtr->hwndEdit = 0;
4861 infoPtr->wpEditOrig = NULL;
4862 infoPtr->bIgnoreEditKillFocus = FALSE;
4863 infoPtr->bLabelChanged = FALSE;
4865 infoPtr->himlNormal = NULL;
4866 infoPtr->himlState = NULL;
4867 infoPtr->normalImageWidth = 0;
4868 infoPtr->normalImageHeight = 0;
4869 infoPtr->stateImageWidth = 0;
4870 infoPtr->stateImageHeight = 0;
4872 infoPtr->items = DPA_Create(16);
4874 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
4875 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
4876 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4877 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
4878 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
4880 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4882 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4883 infoPtr->root->state = TVIS_EXPANDED;
4884 infoPtr->root->iLevel = -1;
4885 infoPtr->root->visibleOrder = -1;
4887 infoPtr->hwndNotify = lpcs->hwndParent;
4888 #if 0
4889 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4890 #endif
4892 infoPtr->hwndToolTip = 0;
4894 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4896 /* Determine what type of notify should be issued */
4897 /* sets infoPtr->bNtfUnicode */
4898 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4900 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4901 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4903 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4905 RECT rc;
4906 HBITMAP hbm, hbmOld;
4907 HDC hdc,hdcScreen;
4908 int nIndex;
4910 infoPtr->himlState =
4911 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4913 hdcScreen = CreateDCW(szDisplayW, NULL, NULL, NULL);
4915 /* Create a coloured bitmap compatible with the screen depth
4916 because checkboxes are not black&white */
4917 hdc = CreateCompatibleDC(hdcScreen);
4918 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4919 hbmOld = SelectObject(hdc, hbm);
4921 rc.left = 0; rc.top = 0;
4922 rc.right = 48; rc.bottom = 16;
4923 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4925 rc.left = 18; rc.top = 2;
4926 rc.right = 30; rc.bottom = 14;
4927 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4928 DFCS_BUTTONCHECK|DFCS_FLAT);
4930 rc.left = 34; rc.right = 46;
4931 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4932 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4934 SelectObject(hdc, hbmOld);
4935 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4936 GetSysColor(COLOR_WINDOW));
4937 TRACE("checkbox index %d\n", nIndex);
4939 DeleteObject(hbm);
4940 DeleteDC(hdc);
4941 DeleteDC(hdcScreen);
4943 infoPtr->stateImageWidth = 16;
4944 infoPtr->stateImageHeight = 16;
4947 /* Make sure actual scrollbar state is consistent with uInternalStatus */
4948 ShowScrollBar(hwnd, SB_VERT, FALSE);
4949 ShowScrollBar(hwnd, SB_HORZ, FALSE);
4951 return 0;
4955 static LRESULT
4956 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4958 TRACE("\n");
4960 TREEVIEW_RemoveTree(infoPtr);
4962 /* tool tip is automatically destroyed: we are its owner */
4964 /* Restore original wndproc */
4965 if (infoPtr->hwndEdit)
4966 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
4967 (DWORD_PTR)infoPtr->wpEditOrig);
4969 /* Deassociate treeview from the window before doing anything drastic. */
4970 SetWindowLongPtrW(infoPtr->hwnd, 0, (DWORD_PTR)NULL);
4972 DeleteObject(infoPtr->hDefaultFont);
4973 DeleteObject(infoPtr->hBoldFont);
4974 DeleteObject(infoPtr->hUnderlineFont);
4975 Free(infoPtr);
4977 return 0;
4980 /* Miscellaneous Messages ***********************************************/
4982 static LRESULT
4983 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4985 static const struct
4987 unsigned char code;
4989 scroll[] =
4991 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4992 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4993 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4994 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4995 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4996 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4997 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4998 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4999 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5000 #undef SCROLL_ENTRY
5003 if (key >= VK_PRIOR && key <= VK_DOWN)
5005 unsigned char code = scroll[key - VK_PRIOR].code;
5007 (((code & (1 << 7)) == (SB_HORZ << 7))
5008 ? TREEVIEW_HScroll
5009 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5012 return 0;
5015 /************************************************************************
5016 * TREEVIEW_KeyDown
5018 * VK_UP Move selection to the previous non-hidden item.
5019 * VK_DOWN Move selection to the next non-hidden item.
5020 * VK_HOME Move selection to the first item.
5021 * VK_END Move selection to the last item.
5022 * VK_LEFT If expanded then collapse, otherwise move to parent.
5023 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5024 * VK_ADD Expand.
5025 * VK_SUBTRACT Collapse.
5026 * VK_MULTIPLY Expand all.
5027 * VK_PRIOR Move up GetVisibleCount items.
5028 * VK_NEXT Move down GetVisibleCount items.
5029 * VK_BACK Move to parent.
5030 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5032 static LRESULT
5033 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5035 /* If it is non-NULL and different, it will be selected and visible. */
5036 TREEVIEW_ITEM *newSelection = NULL;
5038 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5040 TRACE("%x\n", wParam);
5042 if (prevItem == NULL)
5043 return FALSE;
5045 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5046 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5048 switch (wParam)
5050 case VK_UP:
5051 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5052 if (!newSelection)
5053 newSelection = infoPtr->root->firstChild;
5054 break;
5056 case VK_DOWN:
5057 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5058 break;
5060 case VK_HOME:
5061 newSelection = infoPtr->root->firstChild;
5062 break;
5064 case VK_END:
5065 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5066 break;
5068 case VK_LEFT:
5069 if (prevItem->state & TVIS_EXPANDED)
5071 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5073 else if (prevItem->parent != infoPtr->root)
5075 newSelection = prevItem->parent;
5077 break;
5079 case VK_RIGHT:
5080 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5082 if (!(prevItem->state & TVIS_EXPANDED))
5083 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5084 else
5086 newSelection = prevItem->firstChild;
5090 break;
5092 case VK_MULTIPLY:
5093 TREEVIEW_ExpandAll(infoPtr, prevItem);
5094 break;
5096 case VK_ADD:
5097 if (!(prevItem->state & TVIS_EXPANDED))
5098 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5099 break;
5101 case VK_SUBTRACT:
5102 if (prevItem->state & TVIS_EXPANDED)
5103 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5104 break;
5106 case VK_PRIOR:
5107 newSelection
5108 = TREEVIEW_GetListItem(infoPtr, prevItem,
5109 -TREEVIEW_GetVisibleCount(infoPtr));
5110 break;
5112 case VK_NEXT:
5113 newSelection
5114 = TREEVIEW_GetListItem(infoPtr, prevItem,
5115 TREEVIEW_GetVisibleCount(infoPtr));
5116 break;
5118 case VK_BACK:
5119 newSelection = prevItem->parent;
5120 if (newSelection == infoPtr->root)
5121 newSelection = NULL;
5122 break;
5124 case VK_SPACE:
5125 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5126 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5127 break;
5130 if (newSelection && newSelection != prevItem)
5132 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5133 TVC_BYKEYBOARD))
5135 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5139 return FALSE;
5142 static LRESULT
5143 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5145 if (infoPtr->hotItem)
5147 /* remove hot effect from item */
5148 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5149 infoPtr->hotItem = NULL;
5151 return 0;
5154 static LRESULT
5155 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, WPARAM wParam, LPARAM lParam)
5157 POINT pt;
5158 TRACKMOUSEEVENT trackinfo;
5159 TREEVIEW_ITEM * item;
5161 /* fill in the TRACKMOUSEEVENT struct */
5162 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5163 trackinfo.dwFlags = TME_QUERY;
5164 trackinfo.hwndTrack = infoPtr->hwnd;
5165 trackinfo.dwHoverTime = HOVER_DEFAULT;
5167 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5168 _TrackMouseEvent(&trackinfo);
5170 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5171 if(!(trackinfo.dwFlags & TME_LEAVE))
5173 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5175 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5176 /* and can properly deactivate the hot item */
5177 _TrackMouseEvent(&trackinfo);
5180 pt.x = (INT)LOWORD(lParam);
5181 pt.y = (INT)HIWORD(lParam);
5183 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5185 if (item != infoPtr->hotItem)
5187 /* redraw old hot item */
5188 if (infoPtr->hotItem)
5189 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5190 infoPtr->hotItem = item;
5191 /* redraw new hot item */
5192 if (infoPtr->hotItem)
5193 InvalidateRect(infoPtr->hwnd, &infoPtr->hotItem->rect, TRUE);
5196 return 0;
5199 static LRESULT
5200 TREEVIEW_Notify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5202 LPNMHDR lpnmh = (LPNMHDR)lParam;
5204 if (lpnmh->code == PGN_CALCSIZE) {
5205 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5207 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5208 lppgc->iWidth = infoPtr->treeWidth;
5209 TRACE("got PGN_CALCSIZE, returning horz size = %ld, client=%ld\n",
5210 infoPtr->treeWidth, infoPtr->clientWidth);
5212 else {
5213 lppgc->iHeight = infoPtr->treeHeight;
5214 TRACE("got PGN_CALCSIZE, returning vert size = %ld, client=%ld\n",
5215 infoPtr->treeHeight, infoPtr->clientHeight);
5217 return 0;
5219 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5222 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5224 INT format;
5226 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5228 if (nCommand != NF_REQUERY) return 0;
5230 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5231 TRACE("format=%d\n", format);
5233 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5235 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5237 return format;
5240 static LRESULT
5241 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5243 if (wParam == SIZE_RESTORED)
5245 infoPtr->clientWidth = (short)LOWORD(lParam);
5246 infoPtr->clientHeight = (short)HIWORD(lParam);
5248 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5249 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5250 TREEVIEW_UpdateScrollBars(infoPtr);
5252 else
5254 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
5257 TREEVIEW_Invalidate(infoPtr, NULL);
5258 return 0;
5261 static LRESULT
5262 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5264 TRACE("(%x %lx)\n", wParam, lParam);
5266 if (wParam == GWL_STYLE)
5268 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5270 /* we have to take special care about tooltips */
5271 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5273 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5275 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5276 TRACE("\n");
5278 else
5280 DestroyWindow(infoPtr->hwndToolTip);
5281 infoPtr->hwndToolTip = 0;
5285 infoPtr->dwStyle = dwNewStyle;
5288 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5289 TREEVIEW_UpdateScrollBars(infoPtr);
5290 TREEVIEW_Invalidate(infoPtr, NULL);
5292 return 0;
5295 static LRESULT
5296 TREEVIEW_SetCursor(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5298 POINT pt;
5299 TREEVIEW_ITEM * item;
5301 GetCursorPos(&pt);
5302 ScreenToClient(infoPtr->hwnd, &pt);
5304 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5306 /* FIXME: send NM_SETCURSOR */
5308 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5310 SetCursor(infoPtr->hcurHand);
5311 return 0;
5313 else
5314 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5317 static LRESULT
5318 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5320 TRACE("\n");
5322 if (!infoPtr->selectedItem)
5324 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5325 TVC_UNKNOWN);
5328 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5329 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5330 return 0;
5333 static LRESULT
5334 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
5336 TRACE("\n");
5338 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5339 UpdateWindow(infoPtr->hwnd);
5340 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5341 return 0;
5345 static LRESULT WINAPI
5346 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5348 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5350 TRACE("hwnd %p msg %04x wp=%08x lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5352 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5353 else
5355 if (uMsg == WM_CREATE)
5356 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5357 else
5358 goto def;
5361 switch (uMsg)
5363 case TVM_CREATEDRAGIMAGE:
5364 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5366 case TVM_DELETEITEM:
5367 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5369 case TVM_EDITLABELA:
5370 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5372 case TVM_EDITLABELW:
5373 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5375 case TVM_ENDEDITLABELNOW:
5376 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5378 case TVM_ENSUREVISIBLE:
5379 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5381 case TVM_EXPAND:
5382 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5384 case TVM_GETBKCOLOR:
5385 return TREEVIEW_GetBkColor(infoPtr);
5387 case TVM_GETCOUNT:
5388 return TREEVIEW_GetCount(infoPtr);
5390 case TVM_GETEDITCONTROL:
5391 return TREEVIEW_GetEditControl(infoPtr);
5393 case TVM_GETIMAGELIST:
5394 return TREEVIEW_GetImageList(infoPtr, wParam);
5396 case TVM_GETINDENT:
5397 return TREEVIEW_GetIndent(infoPtr);
5399 case TVM_GETINSERTMARKCOLOR:
5400 return TREEVIEW_GetInsertMarkColor(infoPtr);
5402 case TVM_GETISEARCHSTRINGA:
5403 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5404 return 0;
5406 case TVM_GETISEARCHSTRINGW:
5407 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5408 return 0;
5410 case TVM_GETITEMA:
5411 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5413 case TVM_GETITEMW:
5414 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5416 case TVM_GETITEMHEIGHT:
5417 return TREEVIEW_GetItemHeight(infoPtr);
5419 case TVM_GETITEMRECT:
5420 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5422 case TVM_GETITEMSTATE:
5423 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5425 case TVM_GETLINECOLOR:
5426 return TREEVIEW_GetLineColor(infoPtr);
5428 case TVM_GETNEXTITEM:
5429 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5431 case TVM_GETSCROLLTIME:
5432 return TREEVIEW_GetScrollTime(infoPtr);
5434 case TVM_GETTEXTCOLOR:
5435 return TREEVIEW_GetTextColor(infoPtr);
5437 case TVM_GETTOOLTIPS:
5438 return TREEVIEW_GetToolTips(infoPtr);
5440 case TVM_GETUNICODEFORMAT:
5441 return TREEVIEW_GetUnicodeFormat(infoPtr);
5443 case TVM_GETVISIBLECOUNT:
5444 return TREEVIEW_GetVisibleCount(infoPtr);
5446 case TVM_HITTEST:
5447 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5449 case TVM_INSERTITEMA:
5450 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5452 case TVM_INSERTITEMW:
5453 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5455 case TVM_SELECTITEM:
5456 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5458 case TVM_SETBKCOLOR:
5459 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5461 case TVM_SETIMAGELIST:
5462 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5464 case TVM_SETINDENT:
5465 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5467 case TVM_SETINSERTMARK:
5468 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5470 case TVM_SETINSERTMARKCOLOR:
5471 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5473 case TVM_SETITEMA:
5474 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5476 case TVM_SETITEMW:
5477 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5479 case TVM_SETLINECOLOR:
5480 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5482 case TVM_SETITEMHEIGHT:
5483 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5485 case TVM_SETSCROLLTIME:
5486 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5488 case TVM_SETTEXTCOLOR:
5489 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5491 case TVM_SETTOOLTIPS:
5492 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5494 case TVM_SETUNICODEFORMAT:
5495 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5497 case TVM_SORTCHILDREN:
5498 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5500 case TVM_SORTCHILDRENCB:
5501 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5503 case WM_CHAR:
5504 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5506 case WM_COMMAND:
5507 return TREEVIEW_Command(infoPtr, wParam, lParam);
5509 case WM_DESTROY:
5510 return TREEVIEW_Destroy(infoPtr);
5512 /* WM_ENABLE */
5514 case WM_ERASEBKGND:
5515 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5517 case WM_GETDLGCODE:
5518 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5520 case WM_GETFONT:
5521 return TREEVIEW_GetFont(infoPtr);
5523 case WM_HSCROLL:
5524 return TREEVIEW_HScroll(infoPtr, wParam);
5526 case WM_KEYDOWN:
5527 return TREEVIEW_KeyDown(infoPtr, wParam);
5529 case WM_KILLFOCUS:
5530 return TREEVIEW_KillFocus(infoPtr);
5532 case WM_LBUTTONDBLCLK:
5533 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5535 case WM_LBUTTONDOWN:
5536 return TREEVIEW_LButtonDown(infoPtr, lParam);
5538 /* WM_MBUTTONDOWN */
5540 case WM_MOUSELEAVE:
5541 return TREEVIEW_MouseLeave(infoPtr);
5543 case WM_MOUSEMOVE:
5544 if (infoPtr->dwStyle & TVS_TRACKSELECT)
5545 return TREEVIEW_MouseMove(infoPtr, wParam, lParam);
5546 else
5547 return 0;
5549 case WM_NOTIFY:
5550 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5552 case WM_NOTIFYFORMAT:
5553 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5555 case WM_PAINT:
5556 return TREEVIEW_Paint(infoPtr, wParam);
5558 /* WM_PRINTCLIENT */
5560 case WM_RBUTTONDOWN:
5561 return TREEVIEW_RButtonDown(infoPtr, lParam);
5563 case WM_SETCURSOR:
5564 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5566 case WM_SETFOCUS:
5567 return TREEVIEW_SetFocus(infoPtr);
5569 case WM_SETFONT:
5570 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5572 case WM_SETREDRAW:
5573 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5575 case WM_SIZE:
5576 return TREEVIEW_Size(infoPtr, wParam, lParam);
5578 case WM_STYLECHANGED:
5579 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5581 /* WM_SYSCOLORCHANGE */
5583 /* WM_SYSKEYDOWN */
5585 case WM_TIMER:
5586 return TREEVIEW_HandleTimer(infoPtr, wParam);
5588 case WM_VSCROLL:
5589 return TREEVIEW_VScroll(infoPtr, wParam);
5591 /* WM_WININICHANGE */
5593 case WM_MOUSEWHEEL:
5594 if (wParam & (MK_SHIFT | MK_CONTROL))
5595 goto def;
5596 return TREEVIEW_MouseWheel(infoPtr, wParam);
5598 case WM_DRAWITEM:
5599 TRACE("drawItem\n");
5600 goto def;
5602 default:
5603 /* This mostly catches MFC and Delphi messages. :( */
5604 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5605 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
5606 def:
5607 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5612 /* Class Registration ***************************************************/
5614 VOID
5615 TREEVIEW_Register(void)
5617 WNDCLASSW wndClass;
5619 TRACE("\n");
5621 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5622 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5623 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5624 wndClass.cbClsExtra = 0;
5625 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5627 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5628 wndClass.hbrBackground = 0;
5629 wndClass.lpszClassName = WC_TREEVIEWW;
5631 RegisterClassW(&wndClass);
5635 VOID
5636 TREEVIEW_Unregister(void)
5638 UnregisterClassW(WC_TREEVIEWW, NULL);
5642 /* Tree Verification ****************************************************/
5644 #ifdef NDEBUG
5645 static inline void
5646 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5648 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5649 TREEVIEW_ITEM *item)
5651 assert(infoPtr != NULL);
5652 assert(item != NULL);
5654 /* both NULL, or both non-null */
5655 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5657 assert(item->firstChild != item);
5658 assert(item->lastChild != item);
5660 if (item->firstChild)
5662 assert(item->firstChild->parent == item);
5663 assert(item->firstChild->prevSibling == NULL);
5666 if (item->lastChild)
5668 assert(item->lastChild->parent == item);
5669 assert(item->lastChild->nextSibling == NULL);
5672 assert(item->nextSibling != item);
5673 if (item->nextSibling)
5675 assert(item->nextSibling->parent == item->parent);
5676 assert(item->nextSibling->prevSibling == item);
5679 assert(item->prevSibling != item);
5680 if (item->prevSibling)
5682 assert(item->prevSibling->parent == item->parent);
5683 assert(item->prevSibling->nextSibling == item);
5687 static inline void
5688 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5690 assert(item != NULL);
5692 assert(item->parent != NULL);
5693 assert(item->parent != item);
5694 assert(item->iLevel == item->parent->iLevel + 1);
5696 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5698 TREEVIEW_VerifyItemCommon(infoPtr, item);
5700 TREEVIEW_VerifyChildren(infoPtr, item);
5703 static inline void
5704 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5706 TREEVIEW_ITEM *child;
5707 assert(item != NULL);
5709 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5710 TREEVIEW_VerifyItem(infoPtr, child);
5713 static inline void
5714 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5716 TREEVIEW_ITEM *root = infoPtr->root;
5718 assert(root != NULL);
5719 assert(root->iLevel == -1);
5720 assert(root->parent == NULL);
5721 assert(root->prevSibling == NULL);
5723 TREEVIEW_VerifyItemCommon(infoPtr, root);
5725 TREEVIEW_VerifyChildren(infoPtr, root);
5728 static void
5729 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5731 assert(infoPtr != NULL);
5733 TREEVIEW_VerifyRoot(infoPtr);
5735 #endif