3 * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4 * Copyright 1998,1999 Alex Priem <alexp@sci.kun.nl>
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_EXPANDPARTIAL, TVIS_EX_FLAT,
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
45 #include <wine/exception.h>
47 WINE_DEFAULT_DEBUG_CHANNEL(treeview
);
49 /* internal structures */
50 typedef struct tagTREEVIEW_INFO
53 HWND hwndNotify
; /* Owner window to send notifications to */
58 UINT uNumItems
; /* number of valid TREEVIEW_ITEMs */
59 INT cdmode
; /* last custom draw setting */
60 UINT uScrollTime
; /* max. time for scrolling in milliseconds */
61 BOOL bRedraw
; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
63 UINT uItemHeight
; /* item height */
66 LONG clientWidth
; /* width of control window */
67 LONG clientHeight
; /* height of control window */
69 LONG treeWidth
; /* width of visible tree items */
70 LONG treeHeight
; /* height of visible tree items */
72 UINT uIndent
; /* indentation in pixels */
73 HTREEITEM selectedItem
; /* handle to selected item or 0 if none */
74 HTREEITEM hotItem
; /* handle currently under cursor, 0 if none */
75 HTREEITEM focusedItem
; /* item that was under the cursor when WM_LBUTTONDOWN was received */
76 HTREEITEM editItem
; /* item being edited with builtin edit box */
78 HTREEITEM firstVisible
; /* handle to item whose top edge is at y = 0 */
80 HTREEITEM dropItem
; /* handle to item selected by drag cursor */
81 HTREEITEM insertMarkItem
; /* item after which insertion mark is placed */
82 BOOL insertBeforeorAfter
; /* flag used by TVM_SETINSERTMARK */
83 HIMAGELIST dragList
; /* Bitmap of dragged item */
89 COLORREF clrInsertMark
;
94 HFONT hBoldUnderlineFont
;
99 WNDPROC wpEditOrig
; /* orig window proc for subclassing edit */
100 BOOL bIgnoreEditKillFocus
;
103 BOOL bNtfUnicode
; /* TRUE if should send NOTIFY with W */
104 HIMAGELIST himlNormal
;
105 int normalImageHeight
;
106 int normalImageWidth
;
107 HIMAGELIST himlState
;
108 int stateImageHeight
;
112 DWORD lastKeyPressTimestamp
;
114 INT nSearchParamLength
;
115 WCHAR szSearchParam
[ MAX_PATH
];
118 typedef struct _TREEITEM
/* HTREEITEM is a _TREEINFO *. */
120 HTREEITEM parent
; /* handle to parent or 0 if at root */
121 HTREEITEM nextSibling
; /* handle to next item in list, 0 if last */
122 HTREEITEM firstChild
; /* handle to first child or 0 if no child */
134 int iIntegral
; /* item height multiplier (1 is normal) */
135 int iLevel
; /* indentation level:0=root level */
137 HTREEITEM prevSibling
; /* handle to prev item in list, 0 if first */
143 LONG textWidth
; /* horizontal text extent for pszText */
144 LONG visibleOrder
; /* Depth-first numbering of the items whose ancestors are all expanded,
145 corresponding to a top-to-bottom ordering in the tree view.
146 Each item takes up "item.iIntegral" spots in the visible order.
147 0 is the root's first child. */
148 const TREEVIEW_INFO
*infoPtr
; /* tree data this item belongs to */
151 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
152 #define KEY_DELAY 450
154 /* bitflags for infoPtr->uInternalStatus */
156 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
157 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
158 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
159 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
160 #define TV_RDRAG 0x10 /* ditto Rbutton */
161 #define TV_RDRAGGING 0x20
163 /* bitflags for infoPtr->timer */
165 #define TV_EDIT_TIMER 2
166 #define TV_EDIT_TIMER_SET 2
168 #define TEXT_CALLBACK_SIZE 260
170 #define TREEVIEW_LEFT_MARGIN 8
172 #define MINIMUM_INDENT 19
174 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
176 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
177 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
178 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
180 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
181 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
182 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
183 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
185 static const WCHAR themeClass
[] = { 'T','r','e','e','v','i','e','w',0 };
188 typedef VOID (*TREEVIEW_ItemEnumFunc
)(TREEVIEW_INFO
*, TREEVIEW_ITEM
*,LPVOID
);
191 static VOID
TREEVIEW_Invalidate(const TREEVIEW_INFO
*, const TREEVIEW_ITEM
*);
193 static LRESULT
TREEVIEW_DoSelectItem(TREEVIEW_INFO
*, INT
, HTREEITEM
, INT
);
194 static VOID
TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*, TREEVIEW_ITEM
*, BOOL
);
195 static LRESULT
TREEVIEW_EnsureVisible(TREEVIEW_INFO
*, HTREEITEM
, BOOL
);
196 static LRESULT
TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
);
197 static VOID
TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
);
198 static LRESULT
TREEVIEW_HScroll(TREEVIEW_INFO
*, WPARAM
);
200 /* Random Utilities *****************************************************/
201 static void TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
);
203 /* Returns the treeview private data if hwnd is a treeview.
204 * Otherwise returns an undefined value. */
205 static inline TREEVIEW_INFO
*
206 TREEVIEW_GetInfoPtr(HWND hwnd
)
208 return (TREEVIEW_INFO
*)GetWindowLongPtrW(hwnd
, 0);
211 /* Don't call this. Nothing wants an item index. */
213 TREEVIEW_GetItemIndex(const TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
215 return DPA_GetPtrIndex(infoPtr
->items
, handle
);
218 /* Checks if item has changed and needs to be redrawn */
219 static inline BOOL
item_changed (const TREEVIEW_ITEM
*tiOld
, const TREEVIEW_ITEM
*tiNew
,
220 const TVITEMEXW
*tvChange
)
222 /* Number of children has changed */
223 if ((tvChange
->mask
& TVIF_CHILDREN
) && (tiOld
->cChildren
!= tiNew
->cChildren
))
226 /* Image has changed and it's not a callback */
227 if ((tvChange
->mask
& TVIF_IMAGE
) && (tiOld
->iImage
!= tiNew
->iImage
) &&
228 tiNew
->iImage
!= I_IMAGECALLBACK
)
231 /* Selected image has changed and it's not a callback */
232 if ((tvChange
->mask
& TVIF_SELECTEDIMAGE
) && (tiOld
->iSelectedImage
!= tiNew
->iSelectedImage
) &&
233 tiNew
->iSelectedImage
!= I_IMAGECALLBACK
)
236 if ((tvChange
->mask
& TVIF_EXPANDEDIMAGE
) && (tiOld
->iExpandedImage
!= tiNew
->iExpandedImage
) &&
237 tiNew
->iExpandedImage
!= I_IMAGECALLBACK
)
240 /* Text has changed and it's not a callback */
241 if ((tvChange
->mask
& TVIF_TEXT
) && (tiOld
->pszText
!= tiNew
->pszText
) &&
242 tiNew
->pszText
!= LPSTR_TEXTCALLBACKW
)
245 /* Indent has changed */
246 if ((tvChange
->mask
& TVIF_INTEGRAL
) && (tiOld
->iIntegral
!= tiNew
->iIntegral
))
249 /* Item state has changed */
250 if ((tvChange
->mask
& TVIF_STATE
) && ((tiOld
->state
^ tiNew
->state
) & tvChange
->stateMask
))
256 /***************************************************************************
257 * This method checks that handle is an item for this tree.
260 TREEVIEW_ValidItem(const TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
262 if (TREEVIEW_GetItemIndex(infoPtr
, handle
) == -1)
264 TRACE("invalid item %p\n", handle
);
272 TREEVIEW_CreateBoldFont(HFONT hOrigFont
)
276 GetObjectW(hOrigFont
, sizeof(font
), &font
);
277 font
.lfWeight
= FW_BOLD
;
278 return CreateFontIndirectW(&font
);
282 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont
)
286 GetObjectW(hOrigFont
, sizeof(font
), &font
);
287 font
.lfUnderline
= TRUE
;
288 return CreateFontIndirectW(&font
);
292 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont
)
296 GetObjectW(hfont
, sizeof(font
), &font
);
297 font
.lfWeight
= FW_BOLD
;
298 font
.lfUnderline
= TRUE
;
299 return CreateFontIndirectW(&font
);
303 TREEVIEW_FontForItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
305 if ((infoPtr
->dwStyle
& TVS_TRACKSELECT
) && (item
== infoPtr
->hotItem
))
306 return item
->state
& TVIS_BOLD
? infoPtr
->hBoldUnderlineFont
: infoPtr
->hUnderlineFont
;
307 if (item
->state
& TVIS_BOLD
)
308 return infoPtr
->hBoldFont
;
309 return infoPtr
->hFont
;
312 /* for trace/debugging purposes only */
314 TREEVIEW_ItemName(const TREEVIEW_ITEM
*item
)
316 if (item
== NULL
) return "<null item>";
317 if (item
->pszText
== LPSTR_TEXTCALLBACKW
) return "<callback>";
318 if (item
->pszText
== NULL
) return "<null>";
319 return debugstr_w(item
->pszText
);
322 /* An item is not a child of itself. */
324 TREEVIEW_IsChildOf(const TREEVIEW_ITEM
*parent
, const TREEVIEW_ITEM
*child
)
328 child
= child
->parent
;
329 if (child
== parent
) return TRUE
;
330 } while (child
!= NULL
);
336 /* Tree Traversal *******************************************************/
338 /***************************************************************************
339 * This method returns the last expanded sibling or child child item
342 static TREEVIEW_ITEM
*
343 TREEVIEW_GetLastListItem(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
345 if (!item
) return NULL
;
347 while (item
->lastChild
)
349 if (item
->state
& TVIS_EXPANDED
)
350 item
= item
->lastChild
;
355 if (item
== infoPtr
->root
)
361 /***************************************************************************
362 * This method returns the previous non-hidden item in the list not
363 * considering the tree hierarchy.
365 static TREEVIEW_ITEM
*
366 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*tvItem
)
368 if (tvItem
->prevSibling
)
370 /* This item has a prevSibling, get the last item in the sibling's tree. */
371 TREEVIEW_ITEM
*upItem
= tvItem
->prevSibling
;
373 if ((upItem
->state
& TVIS_EXPANDED
) && upItem
->lastChild
!= NULL
)
374 return TREEVIEW_GetLastListItem(infoPtr
, upItem
->lastChild
);
380 /* this item does not have a prevSibling, get the parent */
381 return (tvItem
->parent
!= infoPtr
->root
) ? tvItem
->parent
: NULL
;
386 /***************************************************************************
387 * This method returns the next physical item in the treeview not
388 * considering the tree hierarchy.
390 static TREEVIEW_ITEM
*
391 TREEVIEW_GetNextListItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*tvItem
)
394 * If this item has children and is expanded, return the first child
396 if ((tvItem
->state
& TVIS_EXPANDED
) && tvItem
->firstChild
!= NULL
)
398 return tvItem
->firstChild
;
403 * try to get the sibling
405 if (tvItem
->nextSibling
)
406 return tvItem
->nextSibling
;
409 * Otherwise, get the parent's sibling.
411 while (tvItem
->parent
)
413 tvItem
= tvItem
->parent
;
415 if (tvItem
->nextSibling
)
416 return tvItem
->nextSibling
;
422 /***************************************************************************
423 * This method returns the nth item starting at the given item. It returns
424 * the last item (or first) we we run out of items.
426 * Will scroll backward if count is <0.
427 * forward if count is >0.
429 static TREEVIEW_ITEM
*
430 TREEVIEW_GetListItem(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
433 TREEVIEW_ITEM
*(*next_item
)(const TREEVIEW_INFO
*, const TREEVIEW_ITEM
*);
434 TREEVIEW_ITEM
*previousItem
;
436 assert(item
!= NULL
);
440 next_item
= TREEVIEW_GetNextListItem
;
445 next_item
= TREEVIEW_GetPrevListItem
;
453 item
= next_item(infoPtr
, item
);
455 } while (--count
&& item
!= NULL
);
458 return item
? item
: previousItem
;
461 /* Notifications ************************************************************/
463 static INT
get_notifycode(const TREEVIEW_INFO
*infoPtr
, INT code
)
465 if (!infoPtr
->bNtfUnicode
) {
467 case TVN_SELCHANGINGW
: return TVN_SELCHANGINGA
;
468 case TVN_SELCHANGEDW
: return TVN_SELCHANGEDA
;
469 case TVN_GETDISPINFOW
: return TVN_GETDISPINFOA
;
470 case TVN_SETDISPINFOW
: return TVN_SETDISPINFOA
;
471 case TVN_ITEMEXPANDINGW
: return TVN_ITEMEXPANDINGA
;
472 case TVN_ITEMEXPANDEDW
: return TVN_ITEMEXPANDEDA
;
473 case TVN_BEGINDRAGW
: return TVN_BEGINDRAGA
;
474 case TVN_BEGINRDRAGW
: return TVN_BEGINRDRAGA
;
475 case TVN_DELETEITEMW
: return TVN_DELETEITEMA
;
476 case TVN_BEGINLABELEDITW
: return TVN_BEGINLABELEDITA
;
477 case TVN_ENDLABELEDITW
: return TVN_ENDLABELEDITA
;
478 case TVN_GETINFOTIPW
: return TVN_GETINFOTIPA
;
485 TREEVIEW_SendRealNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
, NMHDR
*hdr
)
487 TRACE("code=%d, hdr=%p\n", code
, hdr
);
489 hdr
->hwndFrom
= infoPtr
->hwnd
;
490 hdr
->idFrom
= GetWindowLongPtrW(infoPtr
->hwnd
, GWLP_ID
);
491 hdr
->code
= get_notifycode(infoPtr
, code
);
493 return SendMessageW(infoPtr
->hwndNotify
, WM_NOTIFY
, hdr
->idFrom
, (LPARAM
)hdr
);
497 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
)
500 return TREEVIEW_SendRealNotify(infoPtr
, code
, &hdr
);
504 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO
*infoPtr
, UINT mask
, TVITEMW
*tvItem
, TREEVIEW_ITEM
*item
)
507 tvItem
->hItem
= item
;
508 tvItem
->state
= item
->state
;
509 tvItem
->stateMask
= 0;
510 tvItem
->iImage
= item
->iImage
;
511 tvItem
->iSelectedImage
= item
->iSelectedImage
;
512 tvItem
->cChildren
= item
->cChildren
;
513 tvItem
->lParam
= item
->lParam
;
517 if (!infoPtr
->bNtfUnicode
)
519 tvItem
->cchTextMax
= WideCharToMultiByte( CP_ACP
, 0, item
->pszText
, -1, NULL
, 0, NULL
, NULL
);
520 tvItem
->pszText
= Alloc (tvItem
->cchTextMax
);
521 WideCharToMultiByte( CP_ACP
, 0, item
->pszText
, -1, (LPSTR
)tvItem
->pszText
, tvItem
->cchTextMax
, 0, 0 );
525 tvItem
->cchTextMax
= item
->cchTextMax
;
526 tvItem
->pszText
= item
->pszText
;
531 tvItem
->cchTextMax
= 0;
532 tvItem
->pszText
= NULL
;
537 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
, UINT action
,
538 UINT mask
, HTREEITEM oldItem
, HTREEITEM newItem
)
543 TRACE("code:%d action:0x%x olditem:%p newitem:%p\n",
544 code
, action
, oldItem
, newItem
);
546 memset(&nmhdr
, 0, sizeof(NMTREEVIEWW
));
547 nmhdr
.action
= action
;
550 TREEVIEW_TVItemFromItem(infoPtr
, mask
, &nmhdr
.itemOld
, oldItem
);
553 TREEVIEW_TVItemFromItem(infoPtr
, mask
, &nmhdr
.itemNew
, newItem
);
558 ret
= TREEVIEW_SendRealNotify(infoPtr
, code
, &nmhdr
.hdr
);
559 if (!infoPtr
->bNtfUnicode
)
561 Free(nmhdr
.itemOld
.pszText
);
562 Free(nmhdr
.itemNew
.pszText
);
568 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
,
569 HTREEITEM dragItem
, POINT pt
)
573 TRACE("code:%d dragitem:%p\n", code
, dragItem
);
576 nmhdr
.itemNew
.mask
= TVIF_STATE
| TVIF_PARAM
| TVIF_HANDLE
;
577 nmhdr
.itemNew
.hItem
= dragItem
;
578 nmhdr
.itemNew
.state
= dragItem
->state
;
579 nmhdr
.itemNew
.lParam
= dragItem
->lParam
;
581 nmhdr
.ptDrag
.x
= pt
.x
;
582 nmhdr
.ptDrag
.y
= pt
.y
;
584 return TREEVIEW_SendRealNotify(infoPtr
, code
, &nmhdr
.hdr
);
589 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO
*infoPtr
, DWORD dwDrawStage
,
592 NMTVCUSTOMDRAW nmcdhdr
;
595 TRACE("drawstage:0x%x hdc:%p\n", dwDrawStage
, hdc
);
597 nmcd
= &nmcdhdr
.nmcd
;
598 nmcd
->dwDrawStage
= dwDrawStage
;
601 nmcd
->dwItemSpec
= 0;
602 nmcd
->uItemState
= 0;
603 nmcd
->lItemlParam
= 0;
604 nmcdhdr
.clrText
= infoPtr
->clrText
;
605 nmcdhdr
.clrTextBk
= infoPtr
->clrBk
;
608 return TREEVIEW_SendRealNotify(infoPtr
, NM_CUSTOMDRAW
, &nmcdhdr
.nmcd
.hdr
);
611 /* FIXME: need to find out when the flags in uItemState need to be set */
614 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO
*infoPtr
, HDC hdc
,
615 TREEVIEW_ITEM
*item
, UINT uItemDrawState
,
616 NMTVCUSTOMDRAW
*nmcdhdr
)
620 DWORD_PTR dwItemSpec
;
623 dwDrawStage
= CDDS_ITEM
| uItemDrawState
;
624 dwItemSpec
= (DWORD_PTR
)item
;
626 if (item
->state
& TVIS_SELECTED
)
627 uItemState
|= CDIS_SELECTED
;
628 if (item
== infoPtr
->selectedItem
)
629 uItemState
|= CDIS_FOCUS
;
630 if (item
== infoPtr
->hotItem
)
631 uItemState
|= CDIS_HOT
;
633 nmcd
= &nmcdhdr
->nmcd
;
634 nmcd
->dwDrawStage
= dwDrawStage
;
636 nmcd
->rc
= item
->rect
;
637 nmcd
->dwItemSpec
= dwItemSpec
;
638 nmcd
->uItemState
= uItemState
;
639 nmcd
->lItemlParam
= item
->lParam
;
640 nmcdhdr
->iLevel
= item
->iLevel
;
642 TRACE("drawstage:0x%x hdc:%p item:%lx, itemstate:0x%x, lItemlParam:0x%lx\n",
643 nmcd
->dwDrawStage
, nmcd
->hdc
, nmcd
->dwItemSpec
,
644 nmcd
->uItemState
, nmcd
->lItemlParam
);
646 return TREEVIEW_SendRealNotify(infoPtr
, NM_CUSTOMDRAW
, &nmcdhdr
->nmcd
.hdr
);
650 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*editItem
)
655 TREEVIEW_TVItemFromItem(infoPtr
, TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
| TVIF_TEXT
,
656 &tvdi
.item
, editItem
);
658 ret
= TREEVIEW_SendRealNotify(infoPtr
, TVN_BEGINLABELEDITW
, &tvdi
.hdr
);
660 if (!infoPtr
->bNtfUnicode
)
661 Free(tvdi
.item
.pszText
);
667 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
670 NMTVDISPINFOEXW callback
;
672 TRACE("mask=0x%x, callbackmask=0x%x\n", mask
, item
->callbackMask
);
673 mask
&= item
->callbackMask
;
675 if (mask
== 0) return;
677 /* 'state' always contains valid value, as well as 'lParam'.
678 * All other parameters are uninitialized.
680 callback
.item
.pszText
= item
->pszText
;
681 callback
.item
.cchTextMax
= item
->cchTextMax
;
682 callback
.item
.mask
= mask
;
683 callback
.item
.hItem
= item
;
684 callback
.item
.state
= item
->state
;
685 callback
.item
.lParam
= item
->lParam
;
687 /* If text is changed we need to recalculate textWidth */
688 if (mask
& TVIF_TEXT
)
691 TREEVIEW_SendRealNotify(infoPtr
, TVN_GETDISPINFOW
, &callback
.hdr
);
692 TRACE("resulting code 0x%08x\n", callback
.hdr
.code
);
694 /* It may have changed due to a call to SetItem. */
695 mask
&= item
->callbackMask
;
697 if ((mask
& TVIF_TEXT
) && callback
.item
.pszText
!= item
->pszText
)
699 /* Instead of copying text into our buffer user specified his own */
700 if (!infoPtr
->bNtfUnicode
&& (callback
.hdr
.code
== TVN_GETDISPINFOA
)) {
703 int len
= MultiByteToWideChar( CP_ACP
, 0,
704 (LPSTR
)callback
.item
.pszText
, -1,
706 buflen
= max((len
)*sizeof(WCHAR
), TEXT_CALLBACK_SIZE
);
707 newText
= ReAlloc(item
->pszText
, buflen
);
709 TRACE("returned str %s, len=%d, buflen=%d\n",
710 debugstr_a((LPSTR
)callback
.item
.pszText
), len
, buflen
);
714 item
->pszText
= newText
;
715 MultiByteToWideChar( CP_ACP
, 0,
716 (LPSTR
)callback
.item
.pszText
, -1,
717 item
->pszText
, buflen
/sizeof(WCHAR
));
718 item
->cchTextMax
= buflen
/sizeof(WCHAR
);
720 /* If ReAlloc fails we have nothing to do, but keep original text */
723 int len
= max(lstrlenW(callback
.item
.pszText
) + 1,
725 LPWSTR newText
= ReAlloc(item
->pszText
, len
);
727 TRACE("returned wstr %s, len=%d\n",
728 debugstr_w(callback
.item
.pszText
), len
);
732 item
->pszText
= newText
;
733 strcpyW(item
->pszText
, callback
.item
.pszText
);
734 item
->cchTextMax
= len
;
736 /* If ReAlloc fails we have nothing to do, but keep original text */
739 else if (mask
& TVIF_TEXT
) {
740 /* User put text into our buffer, that is ok unless A string */
741 if (!infoPtr
->bNtfUnicode
&& (callback
.hdr
.code
== TVN_GETDISPINFOA
)) {
744 int len
= MultiByteToWideChar( CP_ACP
, 0,
745 (LPSTR
)callback
.item
.pszText
, -1,
747 buflen
= max((len
)*sizeof(WCHAR
), TEXT_CALLBACK_SIZE
);
748 newText
= Alloc(buflen
);
750 TRACE("same buffer str %s, len=%d, buflen=%d\n",
751 debugstr_a((LPSTR
)callback
.item
.pszText
), len
, buflen
);
755 LPWSTR oldText
= item
->pszText
;
756 item
->pszText
= newText
;
757 MultiByteToWideChar( CP_ACP
, 0,
758 (LPSTR
)callback
.item
.pszText
, -1,
759 item
->pszText
, buflen
/sizeof(WCHAR
));
760 item
->cchTextMax
= buflen
/sizeof(WCHAR
);
766 if (mask
& TVIF_IMAGE
)
767 item
->iImage
= callback
.item
.iImage
;
769 if (mask
& TVIF_SELECTEDIMAGE
)
770 item
->iSelectedImage
= callback
.item
.iSelectedImage
;
772 if (mask
& TVIF_EXPANDEDIMAGE
)
773 item
->iExpandedImage
= callback
.item
.iExpandedImage
;
775 if (mask
& TVIF_CHILDREN
)
776 item
->cChildren
= callback
.item
.cChildren
;
778 if (callback
.item
.mask
& TVIF_STATE
)
780 item
->state
&= ~callback
.item
.stateMask
;
781 item
->state
|= (callback
.item
.state
& callback
.item
.stateMask
);
784 /* These members are now permanently set. */
785 if (callback
.item
.mask
& TVIF_DI_SETITEM
)
786 item
->callbackMask
&= ~callback
.item
.mask
;
789 /***************************************************************************
790 * This function uses cChildren field to decide whether the item has
792 * Note: if this returns TRUE, the child items may not actually exist,
793 * they could be virtual.
795 * Just use item->firstChild to check for physical children.
798 TREEVIEW_HasChildren(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
800 TREEVIEW_UpdateDispInfo(infoPtr
, item
, TVIF_CHILDREN
);
801 /* Protect for a case when callback field is not changed by a host,
802 otherwise negative values trigger normal notifications. */
803 return item
->cChildren
!= 0 && item
->cChildren
!= I_CHILDRENCALLBACK
;
806 static INT
TREEVIEW_NotifyFormat (TREEVIEW_INFO
*infoPtr
, HWND hwndFrom
, UINT nCommand
)
810 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom
, nCommand
);
812 if (nCommand
!= NF_REQUERY
) return 0;
814 format
= SendMessageW(hwndFrom
, WM_NOTIFYFORMAT
, (WPARAM
)infoPtr
->hwnd
, NF_QUERY
);
815 TRACE("format=%d\n", format
);
817 /* Invalid format returned by NF_QUERY defaults to ANSI*/
818 if (format
!= NFR_ANSI
&& format
!= NFR_UNICODE
)
821 infoPtr
->bNtfUnicode
= (format
== NFR_UNICODE
);
826 /* Item Position ********************************************************/
828 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
830 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
832 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
833 BOOL lar
= ((infoPtr
->dwStyle
& (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
836 item
->linesOffset
= infoPtr
->uIndent
* (lar
? item
->iLevel
: item
->iLevel
- 1)
838 item
->stateOffset
= item
->linesOffset
+ infoPtr
->uIndent
;
839 item
->imageOffset
= item
->stateOffset
840 + (STATEIMAGEINDEX(item
->state
) ? infoPtr
->stateImageWidth
: 0);
841 item
->textOffset
= item
->imageOffset
+ infoPtr
->normalImageWidth
;
845 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
, HDC hDC
)
851 /* DRAW's OM docker creates items like this */
852 if (item
->pszText
== NULL
)
864 hdc
= GetDC(infoPtr
->hwnd
);
865 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, item
));
868 GetTextExtentPoint32W(hdc
, item
->pszText
, strlenW(item
->pszText
), &sz
);
869 item
->textWidth
= sz
.cx
;
873 SelectObject(hdc
, hOldFont
);
879 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
881 item
->rect
.top
= infoPtr
->uItemHeight
*
882 (item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
);
884 item
->rect
.bottom
= item
->rect
.top
885 + infoPtr
->uItemHeight
* item
->iIntegral
- 1;
888 item
->rect
.right
= infoPtr
->clientWidth
;
891 /* We know that only items after start need their order updated. */
893 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*start
)
900 start
= infoPtr
->root
->firstChild
;
904 order
= start
->visibleOrder
;
906 for (item
= start
; item
!= NULL
;
907 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
909 if (!ISVISIBLE(item
) && order
> 0)
910 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, item
);
911 item
->visibleOrder
= order
;
912 order
+= item
->iIntegral
;
915 infoPtr
->maxVisibleOrder
= order
;
917 for (item
= start
; item
!= NULL
;
918 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
920 TREEVIEW_ComputeItemRect(infoPtr
, item
);
925 /* Update metrics of all items in selected subtree.
926 * root must be expanded
929 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*root
)
931 TREEVIEW_ITEM
*sibling
;
935 if (!root
->firstChild
|| !(root
->state
& TVIS_EXPANDED
))
938 root
->state
&= ~TVIS_EXPANDED
;
939 sibling
= TREEVIEW_GetNextListItem(infoPtr
, root
);
940 root
->state
|= TVIS_EXPANDED
;
942 hdc
= GetDC(infoPtr
->hwnd
);
943 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
945 for (; root
!= sibling
;
946 root
= TREEVIEW_GetNextListItem(infoPtr
, root
))
948 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, root
);
950 if (root
->callbackMask
& TVIF_TEXT
)
951 TREEVIEW_UpdateDispInfo(infoPtr
, root
, TVIF_TEXT
);
953 if (root
->textWidth
== 0)
955 SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, root
));
956 TREEVIEW_ComputeTextWidth(infoPtr
, root
, hdc
);
960 SelectObject(hdc
, hOldFont
);
961 ReleaseDC(infoPtr
->hwnd
, hdc
);
964 /* Item Allocation **********************************************************/
966 static TREEVIEW_ITEM
*
967 TREEVIEW_AllocateItem(const TREEVIEW_INFO
*infoPtr
)
969 TREEVIEW_ITEM
*newItem
= Alloc(sizeof(TREEVIEW_ITEM
));
974 /* I_IMAGENONE would make more sense but this is neither what is
975 * documented (MSDN doesn't specify) nor what Windows actually does
976 * (it sets it to zero)... and I can so imagine an application using
977 * inc/dec to toggle the images. */
979 newItem
->iSelectedImage
= 0;
980 newItem
->iExpandedImage
= (WORD
)I_IMAGENONE
;
981 newItem
->infoPtr
= infoPtr
;
983 if (DPA_InsertPtr(infoPtr
->items
, INT_MAX
, newItem
) == -1)
992 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
993 * free item->pszText. */
995 TREEVIEW_FreeItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
997 DPA_DeletePtr(infoPtr
->items
, DPA_GetPtrIndex(infoPtr
->items
, item
));
998 if (infoPtr
->selectedItem
== item
)
999 infoPtr
->selectedItem
= NULL
;
1000 if (infoPtr
->hotItem
== item
)
1001 infoPtr
->hotItem
= NULL
;
1002 if (infoPtr
->focusedItem
== item
)
1003 infoPtr
->focusedItem
= NULL
;
1004 if (infoPtr
->firstVisible
== item
)
1005 infoPtr
->firstVisible
= NULL
;
1006 if (infoPtr
->dropItem
== item
)
1007 infoPtr
->dropItem
= NULL
;
1008 if (infoPtr
->insertMarkItem
== item
)
1009 infoPtr
->insertMarkItem
= NULL
;
1014 /* Item Insertion *******************************************************/
1016 /***************************************************************************
1017 * This method inserts newItem before sibling as a child of parent.
1018 * sibling can be NULL, but only if parent has no children.
1021 TREEVIEW_InsertBefore(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
1022 TREEVIEW_ITEM
*parent
)
1024 assert(parent
!= NULL
);
1026 if (sibling
!= NULL
)
1028 assert(sibling
->parent
== parent
);
1030 if (sibling
->prevSibling
!= NULL
)
1031 sibling
->prevSibling
->nextSibling
= newItem
;
1033 newItem
->prevSibling
= sibling
->prevSibling
;
1034 sibling
->prevSibling
= newItem
;
1037 newItem
->prevSibling
= NULL
;
1039 newItem
->nextSibling
= sibling
;
1041 if (parent
->firstChild
== sibling
)
1042 parent
->firstChild
= newItem
;
1044 if (parent
->lastChild
== NULL
)
1045 parent
->lastChild
= newItem
;
1048 /***************************************************************************
1049 * This method inserts newItem after sibling as a child of parent.
1050 * sibling can be NULL, but only if parent has no children.
1053 TREEVIEW_InsertAfter(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
1054 TREEVIEW_ITEM
*parent
)
1056 assert(parent
!= NULL
);
1058 if (sibling
!= NULL
)
1060 assert(sibling
->parent
== parent
);
1062 if (sibling
->nextSibling
!= NULL
)
1063 sibling
->nextSibling
->prevSibling
= newItem
;
1065 newItem
->nextSibling
= sibling
->nextSibling
;
1066 sibling
->nextSibling
= newItem
;
1069 newItem
->nextSibling
= NULL
;
1071 newItem
->prevSibling
= sibling
;
1073 if (parent
->lastChild
== sibling
)
1074 parent
->lastChild
= newItem
;
1076 if (parent
->firstChild
== NULL
)
1077 parent
->firstChild
= newItem
;
1081 TREEVIEW_DoSetItemT(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
1082 const TVITEMEXW
*tvItem
, BOOL isW
)
1084 UINT callbackClear
= 0;
1085 UINT callbackSet
= 0;
1087 TRACE("item %p\n", item
);
1088 /* Do this first in case it fails. */
1089 if (tvItem
->mask
& TVIF_TEXT
)
1091 item
->textWidth
= 0; /* force width recalculation */
1092 if (tvItem
->pszText
!= LPSTR_TEXTCALLBACKW
&& tvItem
->pszText
!= NULL
) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1097 len
= lstrlenW(tvItem
->pszText
) + 1;
1099 len
= MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)tvItem
->pszText
, -1, NULL
, 0);
1101 newText
= ReAlloc(item
->pszText
, len
* sizeof(WCHAR
));
1103 if (newText
== NULL
) return FALSE
;
1105 callbackClear
|= TVIF_TEXT
;
1107 item
->pszText
= newText
;
1108 item
->cchTextMax
= len
;
1110 lstrcpynW(item
->pszText
, tvItem
->pszText
, len
);
1112 MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)tvItem
->pszText
, -1,
1113 item
->pszText
, len
);
1115 TRACE("setting text %s, item %p\n", debugstr_w(item
->pszText
), item
);
1119 callbackSet
|= TVIF_TEXT
;
1121 item
->pszText
= ReAlloc(item
->pszText
,
1122 TEXT_CALLBACK_SIZE
* sizeof(WCHAR
));
1123 item
->cchTextMax
= TEXT_CALLBACK_SIZE
;
1124 TRACE("setting callback, item %p\n", item
);
1128 if (tvItem
->mask
& TVIF_CHILDREN
)
1130 item
->cChildren
= tvItem
->cChildren
;
1132 if (item
->cChildren
== I_CHILDRENCALLBACK
)
1133 callbackSet
|= TVIF_CHILDREN
;
1135 callbackClear
|= TVIF_CHILDREN
;
1138 if (tvItem
->mask
& TVIF_IMAGE
)
1140 item
->iImage
= tvItem
->iImage
;
1142 if (item
->iImage
== I_IMAGECALLBACK
)
1143 callbackSet
|= TVIF_IMAGE
;
1145 callbackClear
|= TVIF_IMAGE
;
1148 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
1150 item
->iSelectedImage
= tvItem
->iSelectedImage
;
1152 if (item
->iSelectedImage
== I_IMAGECALLBACK
)
1153 callbackSet
|= TVIF_SELECTEDIMAGE
;
1155 callbackClear
|= TVIF_SELECTEDIMAGE
;
1158 if (tvItem
->mask
& TVIF_EXPANDEDIMAGE
)
1160 item
->iExpandedImage
= tvItem
->iExpandedImage
;
1162 if (item
->iExpandedImage
== I_IMAGECALLBACK
)
1163 callbackSet
|= TVIF_EXPANDEDIMAGE
;
1165 callbackClear
|= TVIF_EXPANDEDIMAGE
;
1168 if (tvItem
->mask
& TVIF_PARAM
)
1169 item
->lParam
= tvItem
->lParam
;
1171 /* If the application sets TVIF_INTEGRAL without
1172 * supplying a TVITEMEX structure, it's toast. */
1173 if (tvItem
->mask
& TVIF_INTEGRAL
)
1174 item
->iIntegral
= tvItem
->iIntegral
;
1176 if (tvItem
->mask
& TVIF_STATE
)
1178 TRACE("prevstate 0x%x, state 0x%x, mask 0x%x\n", item
->state
, tvItem
->state
,
1180 item
->state
&= ~tvItem
->stateMask
;
1181 item
->state
|= (tvItem
->state
& tvItem
->stateMask
);
1184 if (tvItem
->mask
& TVIF_STATEEX
)
1186 FIXME("New extended state: 0x%x\n", tvItem
->uStateEx
);
1189 item
->callbackMask
|= callbackSet
;
1190 item
->callbackMask
&= ~callbackClear
;
1195 /* Note that the new item is pre-zeroed. */
1197 TREEVIEW_InsertItemT(TREEVIEW_INFO
*infoPtr
, const TVINSERTSTRUCTW
*ptdi
, BOOL isW
)
1199 const TVITEMEXW
*tvItem
= &ptdi
->u
.itemex
;
1200 HTREEITEM insertAfter
;
1201 TREEVIEW_ITEM
*newItem
, *parentItem
;
1202 BOOL bTextUpdated
= FALSE
;
1204 if (ptdi
->hParent
== TVI_ROOT
|| ptdi
->hParent
== 0)
1206 parentItem
= infoPtr
->root
;
1210 parentItem
= ptdi
->hParent
;
1212 if (!TREEVIEW_ValidItem(infoPtr
, parentItem
))
1214 WARN("invalid parent %p\n", parentItem
);
1219 insertAfter
= ptdi
->hInsertAfter
;
1221 /* Validate this now for convenience. */
1222 switch ((DWORD_PTR
)insertAfter
)
1224 case (DWORD_PTR
)TVI_FIRST
:
1225 case (DWORD_PTR
)TVI_LAST
:
1226 case (DWORD_PTR
)TVI_SORT
:
1230 if (!TREEVIEW_ValidItem(infoPtr
, insertAfter
) ||
1231 insertAfter
->parent
!= parentItem
)
1233 WARN("invalid insert after %p\n", insertAfter
);
1234 insertAfter
= TVI_LAST
;
1238 TRACE("parent %p position %p: %s\n", parentItem
, insertAfter
,
1239 (tvItem
->mask
& TVIF_TEXT
)
1240 ? ((tvItem
->pszText
== LPSTR_TEXTCALLBACKW
) ? "<callback>"
1241 : (isW
? debugstr_w(tvItem
->pszText
) : debugstr_a((LPSTR
)tvItem
->pszText
)))
1244 newItem
= TREEVIEW_AllocateItem(infoPtr
);
1245 if (newItem
== NULL
)
1248 newItem
->parent
= parentItem
;
1249 newItem
->iIntegral
= 1;
1250 newItem
->visibleOrder
= -1;
1252 if (!TREEVIEW_DoSetItemT(infoPtr
, newItem
, tvItem
, isW
))
1255 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1257 infoPtr
->uNumItems
++;
1259 switch ((DWORD_PTR
)insertAfter
)
1261 case (DWORD_PTR
)TVI_FIRST
:
1263 TREEVIEW_ITEM
*originalFirst
= parentItem
->firstChild
;
1264 TREEVIEW_InsertBefore(newItem
, parentItem
->firstChild
, parentItem
);
1265 if (infoPtr
->firstVisible
== originalFirst
)
1266 TREEVIEW_SetFirstVisible(infoPtr
, newItem
, TRUE
);
1270 case (DWORD_PTR
)TVI_LAST
:
1271 TREEVIEW_InsertAfter(newItem
, parentItem
->lastChild
, parentItem
);
1274 /* hInsertAfter names a specific item we want to insert after */
1276 TREEVIEW_InsertAfter(newItem
, insertAfter
, insertAfter
->parent
);
1279 case (DWORD_PTR
)TVI_SORT
:
1281 TREEVIEW_ITEM
*aChild
;
1282 TREEVIEW_ITEM
*previousChild
= NULL
;
1283 TREEVIEW_ITEM
*originalFirst
= parentItem
->firstChild
;
1284 BOOL bItemInserted
= FALSE
;
1286 aChild
= parentItem
->firstChild
;
1288 bTextUpdated
= TRUE
;
1289 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1291 /* Iterate the parent children to see where we fit in */
1292 while (aChild
!= NULL
)
1296 TREEVIEW_UpdateDispInfo(infoPtr
, aChild
, TVIF_TEXT
);
1297 comp
= lstrcmpW(newItem
->pszText
, aChild
->pszText
);
1299 if (comp
< 0) /* we are smaller than the current one */
1301 TREEVIEW_InsertBefore(newItem
, aChild
, parentItem
);
1302 if (infoPtr
->firstVisible
== originalFirst
&&
1303 aChild
== originalFirst
)
1304 TREEVIEW_SetFirstVisible(infoPtr
, newItem
, TRUE
);
1305 bItemInserted
= TRUE
;
1308 else if (comp
> 0) /* we are bigger than the current one */
1310 previousChild
= aChild
;
1312 /* This will help us to exit if there is no more sibling */
1313 aChild
= (aChild
->nextSibling
== 0)
1315 : aChild
->nextSibling
;
1317 /* Look at the next item */
1323 * An item with this name is already existing, therefore,
1324 * we add after the one we found
1326 TREEVIEW_InsertAfter(newItem
, aChild
, parentItem
);
1327 bItemInserted
= TRUE
;
1333 * we reach the end of the child list and the item has not
1334 * yet been inserted, therefore, insert it after the last child.
1336 if ((!bItemInserted
) && (aChild
== NULL
))
1337 TREEVIEW_InsertAfter(newItem
, previousChild
, parentItem
);
1344 TRACE("new item %p; parent %p, mask 0x%x\n", newItem
,
1345 newItem
->parent
, tvItem
->mask
);
1347 newItem
->iLevel
= newItem
->parent
->iLevel
+ 1;
1349 if (newItem
->parent
->cChildren
== 0)
1350 newItem
->parent
->cChildren
= 1;
1352 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
1354 if (STATEIMAGEINDEX(newItem
->state
) == 0)
1355 newItem
->state
|= INDEXTOSTATEIMAGEMASK(1);
1358 if (infoPtr
->firstVisible
== NULL
)
1359 infoPtr
->firstVisible
= newItem
;
1361 TREEVIEW_VerifyTree(infoPtr
);
1363 if (!infoPtr
->bRedraw
) return (LRESULT
)newItem
;
1365 if (parentItem
== infoPtr
->root
||
1366 (ISVISIBLE(parentItem
) && parentItem
->state
& TVIS_EXPANDED
))
1368 TREEVIEW_ITEM
*item
;
1369 TREEVIEW_ITEM
*prev
= TREEVIEW_GetPrevListItem(infoPtr
, newItem
);
1371 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1372 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, newItem
);
1375 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1377 TREEVIEW_ComputeTextWidth(infoPtr
, newItem
, 0);
1378 TREEVIEW_UpdateScrollBars(infoPtr
);
1380 * if the item was inserted in a visible part of the tree,
1381 * invalidate it, as well as those after it
1383 for (item
= newItem
;
1385 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
1386 TREEVIEW_Invalidate(infoPtr
, item
);
1390 /* refresh treeview if newItem is the first item inserted under parentItem */
1391 if (ISVISIBLE(parentItem
) && newItem
->prevSibling
== newItem
->nextSibling
)
1393 /* parent got '+' - update it */
1394 TREEVIEW_Invalidate(infoPtr
, parentItem
);
1398 return (LRESULT
)newItem
;
1401 /* Item Deletion ************************************************************/
1403 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
);
1406 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*parentItem
)
1408 TREEVIEW_ITEM
*kill
= parentItem
->firstChild
;
1410 while (kill
!= NULL
)
1412 TREEVIEW_ITEM
*next
= kill
->nextSibling
;
1414 TREEVIEW_RemoveItem(infoPtr
, kill
);
1419 assert(parentItem
->cChildren
<= 0); /* I_CHILDRENCALLBACK or 0 */
1420 assert(parentItem
->firstChild
== NULL
);
1421 assert(parentItem
->lastChild
== NULL
);
1425 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM
*item
)
1427 TREEVIEW_ITEM
*parentItem
;
1429 assert(item
!= NULL
);
1430 assert(item
->parent
!= NULL
); /* i.e. it must not be the root */
1432 parentItem
= item
->parent
;
1434 if (parentItem
->firstChild
== item
)
1435 parentItem
->firstChild
= item
->nextSibling
;
1437 if (parentItem
->lastChild
== item
)
1438 parentItem
->lastChild
= item
->prevSibling
;
1440 if (parentItem
->firstChild
== NULL
&& parentItem
->lastChild
== NULL
1441 && parentItem
->cChildren
> 0)
1442 parentItem
->cChildren
= 0;
1444 if (item
->prevSibling
)
1445 item
->prevSibling
->nextSibling
= item
->nextSibling
;
1447 if (item
->nextSibling
)
1448 item
->nextSibling
->prevSibling
= item
->prevSibling
;
1452 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
1454 TRACE("%p, (%s)\n", item
, TREEVIEW_ItemName(item
));
1456 if (item
->firstChild
)
1457 TREEVIEW_RemoveAllChildren(infoPtr
, item
);
1459 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_DELETEITEMW
, TVC_UNKNOWN
,
1460 TVIF_HANDLE
| TVIF_PARAM
, item
, 0);
1462 TREEVIEW_UnlinkItem(item
);
1464 infoPtr
->uNumItems
--;
1466 if (item
->pszText
!= LPSTR_TEXTCALLBACKW
)
1467 Free(item
->pszText
);
1469 TREEVIEW_FreeItem(infoPtr
, item
);
1473 /* Empty out the tree. */
1475 TREEVIEW_RemoveTree(TREEVIEW_INFO
*infoPtr
)
1477 TREEVIEW_RemoveAllChildren(infoPtr
, infoPtr
->root
);
1479 assert(infoPtr
->uNumItems
== 0); /* root isn't counted in uNumItems */
1483 TREEVIEW_DeleteItem(TREEVIEW_INFO
*infoPtr
, HTREEITEM item
)
1485 TREEVIEW_ITEM
*newSelection
= NULL
;
1486 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
1487 TREEVIEW_ITEM
*parent
, *prev
= NULL
;
1488 BOOL visible
= FALSE
;
1490 if (item
== TVI_ROOT
|| !item
)
1492 TRACE("TVI_ROOT\n");
1493 parent
= infoPtr
->root
;
1494 newSelection
= NULL
;
1496 TREEVIEW_RemoveTree(infoPtr
);
1500 if (!TREEVIEW_ValidItem(infoPtr
, item
))
1503 TRACE("%p (%s)\n", item
, TREEVIEW_ItemName(item
));
1504 parent
= item
->parent
;
1506 if (ISVISIBLE(item
))
1508 prev
= TREEVIEW_GetPrevListItem(infoPtr
, item
);
1512 if (infoPtr
->selectedItem
!= NULL
1513 && (item
== infoPtr
->selectedItem
1514 || TREEVIEW_IsChildOf(item
, infoPtr
->selectedItem
)))
1516 if (item
->nextSibling
)
1517 newSelection
= item
->nextSibling
;
1518 else if (item
->parent
!= infoPtr
->root
)
1519 newSelection
= item
->parent
;
1521 newSelection
= item
->prevSibling
;
1522 TRACE("newSelection = %p\n", newSelection
);
1525 if (infoPtr
->firstVisible
== item
)
1528 if (item
->nextSibling
)
1529 newFirstVisible
= item
->nextSibling
;
1530 else if (item
->prevSibling
)
1531 newFirstVisible
= item
->prevSibling
;
1532 else if (item
->parent
!= infoPtr
->root
)
1533 newFirstVisible
= item
->parent
;
1534 TREEVIEW_SetFirstVisible(infoPtr
, NULL
, TRUE
);
1537 newFirstVisible
= infoPtr
->firstVisible
;
1539 TREEVIEW_RemoveItem(infoPtr
, item
);
1542 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1543 if (!infoPtr
->selectedItem
&& newSelection
)
1545 if (TREEVIEW_ValidItem(infoPtr
, newSelection
))
1546 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
, TVC_UNKNOWN
);
1549 /* Validate insertMark dropItem.
1550 * hotItem ??? - used for comparison only.
1552 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->insertMarkItem
))
1553 infoPtr
->insertMarkItem
= 0;
1555 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->dropItem
))
1556 infoPtr
->dropItem
= 0;
1558 if (!TREEVIEW_ValidItem(infoPtr
, newFirstVisible
))
1559 newFirstVisible
= infoPtr
->root
->firstChild
;
1561 TREEVIEW_VerifyTree(infoPtr
);
1564 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
1566 if (!infoPtr
->bRedraw
) return TRUE
;
1570 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1571 TREEVIEW_UpdateScrollBars(infoPtr
);
1572 TREEVIEW_Invalidate(infoPtr
, NULL
);
1574 else if (ISVISIBLE(parent
) && !TREEVIEW_HasChildren(infoPtr
, parent
))
1576 /* parent lost '+/-' - update it */
1577 TREEVIEW_Invalidate(infoPtr
, parent
);
1584 /* Get/Set Messages *********************************************************/
1586 TREEVIEW_SetRedraw(TREEVIEW_INFO
* infoPtr
, WPARAM wParam
)
1588 infoPtr
->bRedraw
= wParam
!= 0;
1590 if (infoPtr
->bRedraw
)
1592 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1593 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1594 TREEVIEW_UpdateScrollBars(infoPtr
);
1595 TREEVIEW_Invalidate(infoPtr
, NULL
);
1601 TREEVIEW_GetIndent(const TREEVIEW_INFO
*infoPtr
)
1604 return infoPtr
->uIndent
;
1608 TREEVIEW_SetIndent(TREEVIEW_INFO
*infoPtr
, UINT newIndent
)
1612 if (newIndent
< MINIMUM_INDENT
)
1613 newIndent
= MINIMUM_INDENT
;
1615 if (infoPtr
->uIndent
!= newIndent
)
1617 infoPtr
->uIndent
= newIndent
;
1618 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1619 TREEVIEW_UpdateScrollBars(infoPtr
);
1620 TREEVIEW_Invalidate(infoPtr
, NULL
);
1628 TREEVIEW_GetToolTips(const TREEVIEW_INFO
*infoPtr
)
1631 return (LRESULT
)infoPtr
->hwndToolTip
;
1635 TREEVIEW_SetToolTips(TREEVIEW_INFO
*infoPtr
, HWND hwndTT
)
1640 prevToolTip
= infoPtr
->hwndToolTip
;
1641 infoPtr
->hwndToolTip
= hwndTT
;
1643 return (LRESULT
)prevToolTip
;
1647 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO
*infoPtr
, BOOL fUnicode
)
1649 BOOL rc
= infoPtr
->bNtfUnicode
;
1650 infoPtr
->bNtfUnicode
= fUnicode
;
1655 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO
*infoPtr
)
1657 return infoPtr
->bNtfUnicode
;
1661 TREEVIEW_GetScrollTime(const TREEVIEW_INFO
*infoPtr
)
1663 return infoPtr
->uScrollTime
;
1667 TREEVIEW_SetScrollTime(TREEVIEW_INFO
*infoPtr
, UINT uScrollTime
)
1669 UINT uOldScrollTime
= infoPtr
->uScrollTime
;
1671 infoPtr
->uScrollTime
= min(uScrollTime
, 100);
1673 return uOldScrollTime
;
1678 TREEVIEW_GetImageList(const TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
1685 return (LRESULT
)infoPtr
->himlNormal
;
1688 return (LRESULT
)infoPtr
->himlState
;
1695 #define TVHEIGHT_MIN 16
1696 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1698 /* Compute the natural height for items. */
1700 TREEVIEW_NaturalHeight(const TREEVIEW_INFO
*infoPtr
)
1704 HFONT hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
1707 /* Height is the maximum of:
1708 * 16 (a hack because our fonts are tiny), and
1709 * The text height + border & margin, and
1710 * The size of the normal image list
1712 GetTextMetricsW(hdc
, &tm
);
1713 SelectObject(hdc
, hOldFont
);
1716 height
= TVHEIGHT_MIN
;
1717 if (height
< tm
.tmHeight
+ tm
.tmExternalLeading
+ TVHEIGHT_FONT_ADJUST
)
1718 height
= tm
.tmHeight
+ tm
.tmExternalLeading
+ TVHEIGHT_FONT_ADJUST
;
1719 if (height
< infoPtr
->normalImageHeight
)
1720 height
= infoPtr
->normalImageHeight
;
1722 /* Round down, unless we support odd ("non even") heights. */
1723 if (!(infoPtr
->dwStyle
& TVS_NONEVENHEIGHT
))
1730 TREEVIEW_SetImageList(TREEVIEW_INFO
*infoPtr
, UINT type
, HIMAGELIST himlNew
)
1732 HIMAGELIST himlOld
= 0;
1733 int oldWidth
= infoPtr
->normalImageWidth
;
1734 int oldHeight
= infoPtr
->normalImageHeight
;
1736 TRACE("%u,%p\n", type
, himlNew
);
1741 himlOld
= infoPtr
->himlNormal
;
1742 infoPtr
->himlNormal
= himlNew
;
1745 ImageList_GetIconSize(himlNew
, &infoPtr
->normalImageWidth
,
1746 &infoPtr
->normalImageHeight
);
1749 infoPtr
->normalImageWidth
= 0;
1750 infoPtr
->normalImageHeight
= 0;
1756 himlOld
= infoPtr
->himlState
;
1757 infoPtr
->himlState
= himlNew
;
1760 ImageList_GetIconSize(himlNew
, &infoPtr
->stateImageWidth
,
1761 &infoPtr
->stateImageHeight
);
1764 infoPtr
->stateImageWidth
= 0;
1765 infoPtr
->stateImageHeight
= 0;
1771 ERR("unknown imagelist type %u\n", type
);
1774 if (oldWidth
!= infoPtr
->normalImageWidth
||
1775 oldHeight
!= infoPtr
->normalImageHeight
)
1777 BOOL bRecalcVisible
= FALSE
;
1779 if (oldHeight
!= infoPtr
->normalImageHeight
&&
1780 !infoPtr
->bHeightSet
)
1782 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1783 bRecalcVisible
= TRUE
;
1786 if (infoPtr
->normalImageWidth
> MINIMUM_INDENT
&&
1787 infoPtr
->normalImageWidth
!= infoPtr
->uIndent
)
1789 infoPtr
->uIndent
= infoPtr
->normalImageWidth
;
1790 bRecalcVisible
= TRUE
;
1794 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1796 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1797 TREEVIEW_UpdateScrollBars(infoPtr
);
1800 TREEVIEW_Invalidate(infoPtr
, NULL
);
1802 return (LRESULT
)himlOld
;
1806 TREEVIEW_SetItemHeight(TREEVIEW_INFO
*infoPtr
, INT newHeight
)
1808 INT prevHeight
= infoPtr
->uItemHeight
;
1810 TRACE("new=%d, old=%d\n", newHeight
, prevHeight
);
1811 if (newHeight
== -1)
1813 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1814 infoPtr
->bHeightSet
= FALSE
;
1818 if (newHeight
== 0) newHeight
= 1;
1819 infoPtr
->uItemHeight
= newHeight
;
1820 infoPtr
->bHeightSet
= TRUE
;
1823 /* Round down, unless we support odd ("non even") heights. */
1824 if (!(infoPtr
->dwStyle
& TVS_NONEVENHEIGHT
) && infoPtr
->uItemHeight
!= 1)
1826 infoPtr
->uItemHeight
&= ~1;
1827 TRACE("after rounding=%d\n", infoPtr
->uItemHeight
);
1830 if (infoPtr
->uItemHeight
!= prevHeight
)
1832 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1833 TREEVIEW_UpdateScrollBars(infoPtr
);
1834 TREEVIEW_Invalidate(infoPtr
, NULL
);
1841 TREEVIEW_GetItemHeight(const TREEVIEW_INFO
*infoPtr
)
1844 return infoPtr
->uItemHeight
;
1849 TREEVIEW_GetFont(const TREEVIEW_INFO
*infoPtr
)
1851 TRACE("%p\n", infoPtr
->hFont
);
1852 return (LRESULT
)infoPtr
->hFont
;
1857 TREEVIEW_ResetTextWidth(LPVOID pItem
, LPVOID unused
)
1861 ((TREEVIEW_ITEM
*)pItem
)->textWidth
= 0;
1867 TREEVIEW_SetFont(TREEVIEW_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
1869 UINT uHeight
= infoPtr
->uItemHeight
;
1871 TRACE("%p %i\n", hFont
, bRedraw
);
1873 infoPtr
->hFont
= hFont
? hFont
: infoPtr
->hDefaultFont
;
1875 DeleteObject(infoPtr
->hBoldFont
);
1876 DeleteObject(infoPtr
->hUnderlineFont
);
1877 DeleteObject(infoPtr
->hBoldUnderlineFont
);
1878 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
1879 infoPtr
->hUnderlineFont
= TREEVIEW_CreateUnderlineFont(infoPtr
->hFont
);
1880 infoPtr
->hBoldUnderlineFont
= TREEVIEW_CreateBoldUnderlineFont(infoPtr
->hFont
);
1882 if (!infoPtr
->bHeightSet
)
1883 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1885 if (uHeight
!= infoPtr
->uItemHeight
)
1886 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1888 DPA_EnumCallback(infoPtr
->items
, TREEVIEW_ResetTextWidth
, 0);
1890 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1891 TREEVIEW_UpdateScrollBars(infoPtr
);
1894 TREEVIEW_Invalidate(infoPtr
, NULL
);
1901 TREEVIEW_GetLineColor(const TREEVIEW_INFO
*infoPtr
)
1904 return (LRESULT
)infoPtr
->clrLine
;
1908 TREEVIEW_SetLineColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1910 COLORREF prevColor
= infoPtr
->clrLine
;
1913 infoPtr
->clrLine
= color
;
1914 return (LRESULT
)prevColor
;
1919 TREEVIEW_GetTextColor(const TREEVIEW_INFO
*infoPtr
)
1922 return (LRESULT
)infoPtr
->clrText
;
1926 TREEVIEW_SetTextColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1928 COLORREF prevColor
= infoPtr
->clrText
;
1931 infoPtr
->clrText
= color
;
1933 if (infoPtr
->clrText
!= prevColor
)
1934 TREEVIEW_Invalidate(infoPtr
, NULL
);
1936 return (LRESULT
)prevColor
;
1941 TREEVIEW_GetBkColor(const TREEVIEW_INFO
*infoPtr
)
1944 return (LRESULT
)infoPtr
->clrBk
;
1948 TREEVIEW_SetBkColor(TREEVIEW_INFO
*infoPtr
, COLORREF newColor
)
1950 COLORREF prevColor
= infoPtr
->clrBk
;
1953 infoPtr
->clrBk
= newColor
;
1955 if (newColor
!= prevColor
)
1956 TREEVIEW_Invalidate(infoPtr
, NULL
);
1958 return (LRESULT
)prevColor
;
1963 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO
*infoPtr
)
1966 return (LRESULT
)infoPtr
->clrInsertMark
;
1970 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1972 COLORREF prevColor
= infoPtr
->clrInsertMark
;
1974 TRACE("0x%08x\n", color
);
1975 infoPtr
->clrInsertMark
= color
;
1977 return (LRESULT
)prevColor
;
1982 TREEVIEW_SetInsertMark(TREEVIEW_INFO
*infoPtr
, BOOL wParam
, HTREEITEM item
)
1984 TRACE("%d %p\n", wParam
, item
);
1986 if (!TREEVIEW_ValidItem(infoPtr
, item
))
1989 infoPtr
->insertBeforeorAfter
= wParam
;
1990 infoPtr
->insertMarkItem
= item
;
1992 TREEVIEW_Invalidate(infoPtr
, NULL
);
1998 /************************************************************************
1999 * Some serious braindamage here. lParam is a pointer to both the
2000 * input HTREEITEM and the output RECT.
2003 TREEVIEW_GetItemRect(const TREEVIEW_INFO
*infoPtr
, BOOL fTextRect
, LPRECT lpRect
)
2005 TREEVIEW_ITEM
*item
;
2006 const HTREEITEM
*pItem
= (HTREEITEM
*)lpRect
;
2014 if (!TREEVIEW_ValidItem(infoPtr
, item
) || !ISVISIBLE(item
))
2018 * If wParam is TRUE return the text size otherwise return
2019 * the whole item size
2023 /* Windows does not send TVN_GETDISPINFO here. */
2025 lpRect
->top
= item
->rect
.top
;
2026 lpRect
->bottom
= item
->rect
.bottom
;
2028 lpRect
->left
= item
->textOffset
;
2029 if (!item
->textWidth
)
2030 TREEVIEW_ComputeTextWidth(infoPtr
, item
, 0);
2032 lpRect
->right
= item
->textOffset
+ item
->textWidth
+ 4;
2036 *lpRect
= item
->rect
;
2039 TRACE("%s [%s]\n", fTextRect
? "text" : "item", wine_dbgstr_rect(lpRect
));
2044 static inline LRESULT
2045 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO
*infoPtr
)
2047 /* Surprise! This does not take integral height into account. */
2048 TRACE("client=%d, item=%d\n", infoPtr
->clientHeight
, infoPtr
->uItemHeight
);
2049 return infoPtr
->clientHeight
/ infoPtr
->uItemHeight
;
2054 TREEVIEW_GetItemT(const TREEVIEW_INFO
*infoPtr
, LPTVITEMEXW tvItem
, BOOL isW
)
2056 TREEVIEW_ITEM
*item
= tvItem
->hItem
;
2058 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2060 BOOL valid_item
= FALSE
;
2061 if (!item
) return FALSE
;
2065 infoPtr
= item
->infoPtr
;
2066 TRACE("got item from different tree %p, called from %p\n", item
->infoPtr
, infoPtr
);
2067 valid_item
= TREEVIEW_ValidItem(infoPtr
, item
);
2073 if (!valid_item
) return FALSE
;
2076 TREEVIEW_UpdateDispInfo(infoPtr
, item
, tvItem
->mask
);
2078 if (tvItem
->mask
& TVIF_CHILDREN
)
2080 if (item
->cChildren
==I_CHILDRENCALLBACK
)
2081 FIXME("I_CHILDRENCALLBACK not supported\n");
2082 tvItem
->cChildren
= item
->cChildren
;
2085 if (tvItem
->mask
& TVIF_HANDLE
)
2086 tvItem
->hItem
= item
;
2088 if (tvItem
->mask
& TVIF_IMAGE
)
2089 tvItem
->iImage
= item
->iImage
;
2091 if (tvItem
->mask
& TVIF_INTEGRAL
)
2092 tvItem
->iIntegral
= item
->iIntegral
;
2094 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2095 tvItem
->lParam
= item
->lParam
;
2097 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
2098 tvItem
->iSelectedImage
= item
->iSelectedImage
;
2100 if (tvItem
->mask
& TVIF_EXPANDEDIMAGE
)
2101 tvItem
->iExpandedImage
= item
->iExpandedImage
;
2103 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2104 tvItem
->state
= item
->state
;
2106 if (tvItem
->mask
& TVIF_TEXT
)
2108 if (item
->pszText
== NULL
)
2110 if (tvItem
->cchTextMax
> 0)
2111 tvItem
->pszText
[0] = '\0';
2115 if (item
->pszText
== LPSTR_TEXTCALLBACKW
)
2117 tvItem
->pszText
= LPSTR_TEXTCALLBACKW
;
2118 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2122 lstrcpynW(tvItem
->pszText
, item
->pszText
, tvItem
->cchTextMax
);
2127 if (item
->pszText
== LPSTR_TEXTCALLBACKW
)
2129 tvItem
->pszText
= (LPWSTR
)LPSTR_TEXTCALLBACKA
;
2130 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2134 WideCharToMultiByte(CP_ACP
, 0, item
->pszText
, -1,
2135 (LPSTR
)tvItem
->pszText
, tvItem
->cchTextMax
, NULL
, NULL
);
2140 if (tvItem
->mask
& TVIF_STATEEX
)
2142 FIXME("Extended item state not supported, returning 0.\n");
2143 tvItem
->uStateEx
= 0;
2146 TRACE("item <%p>, txt %p, img %d, mask 0x%x\n",
2147 item
, tvItem
->pszText
, tvItem
->iImage
, tvItem
->mask
);
2152 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2153 * which is wrong. */
2155 TREEVIEW_SetItemT(TREEVIEW_INFO
*infoPtr
, const TVITEMEXW
*tvItem
, BOOL isW
)
2157 TREEVIEW_ITEM
*item
;
2158 TREEVIEW_ITEM originalItem
;
2160 item
= tvItem
->hItem
;
2162 TRACE("item %d, mask 0x%x\n", TREEVIEW_GetItemIndex(infoPtr
, item
),
2165 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2168 /* store the original item values */
2169 originalItem
= *item
;
2171 if (!TREEVIEW_DoSetItemT(infoPtr
, item
, tvItem
, isW
))
2174 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2175 if ((tvItem
->mask
& TVIF_TEXT
2176 || (tvItem
->mask
& TVIF_STATE
&& tvItem
->stateMask
& TVIS_BOLD
))
2179 TREEVIEW_UpdateDispInfo(infoPtr
, item
, TVIF_TEXT
);
2180 TREEVIEW_ComputeTextWidth(infoPtr
, item
, 0);
2183 if (tvItem
->mask
!= 0 && ISVISIBLE(item
))
2185 /* The refresh updates everything, but we can't wait until then. */
2186 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, item
);
2188 /* if any of the item's values changed and it's not a callback, redraw the item */
2189 if (item_changed(&originalItem
, item
, tvItem
))
2191 if (tvItem
->mask
& TVIF_INTEGRAL
)
2193 TREEVIEW_RecalculateVisibleOrder(infoPtr
, item
);
2194 TREEVIEW_UpdateScrollBars(infoPtr
);
2196 TREEVIEW_Invalidate(infoPtr
, NULL
);
2200 TREEVIEW_UpdateScrollBars(infoPtr
);
2201 TREEVIEW_Invalidate(infoPtr
, item
);
2210 TREEVIEW_GetItemState(const TREEVIEW_INFO
*infoPtr
, HTREEITEM item
, UINT mask
)
2214 if (!item
|| !TREEVIEW_ValidItem(infoPtr
, item
))
2217 return (item
->state
& mask
);
2221 TREEVIEW_GetNextItem(const TREEVIEW_INFO
*infoPtr
, UINT which
, HTREEITEM item
)
2223 TREEVIEW_ITEM
*retval
;
2227 /* handle all the global data here */
2230 case TVGN_CHILD
: /* Special case: child of 0 is root */
2235 retval
= infoPtr
->root
->firstChild
;
2239 retval
= infoPtr
->selectedItem
;
2242 case TVGN_FIRSTVISIBLE
:
2243 retval
= infoPtr
->firstVisible
;
2246 case TVGN_DROPHILITE
:
2247 retval
= infoPtr
->dropItem
;
2250 case TVGN_LASTVISIBLE
:
2251 retval
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
2257 TRACE("flags:0x%x, returns %p\n", which
, retval
);
2258 return (LRESULT
)retval
;
2261 if (item
== TVI_ROOT
) item
= infoPtr
->root
;
2263 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2269 retval
= item
->nextSibling
;
2272 retval
= item
->prevSibling
;
2275 retval
= (item
->parent
!= infoPtr
->root
) ? item
->parent
: NULL
;
2278 retval
= item
->firstChild
;
2280 case TVGN_NEXTVISIBLE
:
2281 retval
= TREEVIEW_GetNextListItem(infoPtr
, item
);
2283 case TVGN_PREVIOUSVISIBLE
:
2284 retval
= TREEVIEW_GetPrevListItem(infoPtr
, item
);
2287 TRACE("Unknown msg 0x%x, item %p\n", which
, item
);
2291 TRACE("flags: 0x%x, item %p;returns %p\n", which
, item
, retval
);
2292 return (LRESULT
)retval
;
2297 TREEVIEW_GetCount(const TREEVIEW_INFO
*infoPtr
)
2299 TRACE(" %d\n", infoPtr
->uNumItems
);
2300 return (LRESULT
)infoPtr
->uNumItems
;
2304 TREEVIEW_ToggleItemState(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2306 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
2308 static const unsigned int state_table
[] = { 0, 2, 1 };
2312 state
= STATEIMAGEINDEX(item
->state
);
2313 TRACE("state: 0x%x\n", state
);
2314 item
->state
&= ~TVIS_STATEIMAGEMASK
;
2317 state
= state_table
[state
];
2319 item
->state
|= INDEXTOSTATEIMAGEMASK(state
);
2321 TRACE("state: 0x%x\n", state
);
2322 TREEVIEW_Invalidate(infoPtr
, item
);
2327 /* Painting *************************************************************/
2329 /* Draw the lines and expand button for an item. Also draws one section
2330 * of the line from item's parent to item's parent's next sibling. */
2332 TREEVIEW_DrawItemLines(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, const TREEVIEW_ITEM
*item
)
2334 LONG centerx
, centery
;
2335 BOOL lar
= ((infoPtr
->dwStyle
2336 & (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
2339 COLORREF clrBk
= GETBKCOLOR(infoPtr
->clrBk
);
2341 if (!lar
&& item
->iLevel
== 0)
2344 hbr
= CreateSolidBrush(clrBk
);
2345 hbrOld
= SelectObject(hdc
, hbr
);
2347 centerx
= (item
->linesOffset
+ item
->stateOffset
) / 2;
2348 centery
= (item
->rect
.top
+ item
->rect
.bottom
) / 2;
2350 if (infoPtr
->dwStyle
& TVS_HASLINES
)
2352 HPEN hOldPen
, hNewPen
;
2356 /* Get a dotted grey pen */
2357 lb
.lbStyle
= BS_SOLID
;
2358 lb
.lbColor
= GETLINECOLOR(infoPtr
->clrLine
);
2359 hNewPen
= ExtCreatePen(PS_COSMETIC
|PS_ALTERNATE
, 1, &lb
, 0, NULL
);
2360 hOldPen
= SelectObject(hdc
, hNewPen
);
2362 /* Make sure the center is on a dot (using +2 instead
2363 * of +1 gives us pixel-by-pixel compat with native) */
2364 centery
= (centery
+ 2) & ~1;
2366 MoveToEx(hdc
, item
->stateOffset
, centery
, NULL
);
2367 LineTo(hdc
, centerx
- 1, centery
);
2369 if (item
->prevSibling
|| item
->parent
!= infoPtr
->root
)
2371 MoveToEx(hdc
, centerx
, item
->rect
.top
, NULL
);
2372 LineTo(hdc
, centerx
, centery
);
2375 if (item
->nextSibling
)
2377 MoveToEx(hdc
, centerx
, centery
, NULL
);
2378 LineTo(hdc
, centerx
, item
->rect
.bottom
+ 1);
2381 /* Draw the line from our parent to its next sibling. */
2382 parent
= item
->parent
;
2383 while (parent
!= infoPtr
->root
)
2385 int pcenterx
= (parent
->linesOffset
+ parent
->stateOffset
) / 2;
2387 if (parent
->nextSibling
2388 /* skip top-levels unless TVS_LINESATROOT */
2389 && parent
->stateOffset
> parent
->linesOffset
)
2391 MoveToEx(hdc
, pcenterx
, item
->rect
.top
, NULL
);
2392 LineTo(hdc
, pcenterx
, item
->rect
.bottom
+ 1);
2395 parent
= parent
->parent
;
2398 SelectObject(hdc
, hOldPen
);
2399 DeleteObject(hNewPen
);
2403 * Display the (+/-) signs
2406 if (infoPtr
->dwStyle
& TVS_HASBUTTONS
)
2408 if (item
->cChildren
)
2410 HTHEME theme
= GetWindowTheme(infoPtr
->hwnd
);
2413 RECT glyphRect
= item
->rect
;
2414 glyphRect
.left
= item
->linesOffset
;
2415 glyphRect
.right
= item
->stateOffset
;
2416 DrawThemeBackground (theme
, hdc
, TVP_GLYPH
,
2417 (item
->state
& TVIS_EXPANDED
) ? GLPS_OPENED
: GLPS_CLOSED
,
2422 LONG height
= item
->rect
.bottom
- item
->rect
.top
;
2423 LONG width
= item
->stateOffset
- item
->linesOffset
;
2424 LONG rectsize
= min(height
, width
) / 4;
2425 /* plussize = ceil(rectsize * 3/4) */
2426 LONG plussize
= (rectsize
+ 1) * 3 / 4;
2428 HPEN new_pen
= CreatePen(PS_SOLID
, 0, GETLINECOLOR(infoPtr
->clrLine
));
2429 HPEN old_pen
= SelectObject(hdc
, new_pen
);
2431 Rectangle(hdc
, centerx
- rectsize
- 1, centery
- rectsize
- 1,
2432 centerx
+ rectsize
+ 2, centery
+ rectsize
+ 2);
2434 SelectObject(hdc
, old_pen
);
2435 DeleteObject(new_pen
);
2437 /* draw +/- signs with current text color */
2438 new_pen
= CreatePen(PS_SOLID
, 0, GETTXTCOLOR(infoPtr
->clrText
));
2439 old_pen
= SelectObject(hdc
, new_pen
);
2441 if (height
< 18 || width
< 18)
2443 MoveToEx(hdc
, centerx
- plussize
+ 1, centery
, NULL
);
2444 LineTo(hdc
, centerx
+ plussize
, centery
);
2446 if (!(item
->state
& TVIS_EXPANDED
) ||
2447 (item
->state
& TVIS_EXPANDPARTIAL
))
2449 MoveToEx(hdc
, centerx
, centery
- plussize
+ 1, NULL
);
2450 LineTo(hdc
, centerx
, centery
+ plussize
);
2455 Rectangle(hdc
, centerx
- plussize
+ 1, centery
- 1,
2456 centerx
+ plussize
, centery
+ 2);
2458 if (!(item
->state
& TVIS_EXPANDED
) ||
2459 (item
->state
& TVIS_EXPANDPARTIAL
))
2461 Rectangle(hdc
, centerx
- 1, centery
- plussize
+ 1,
2462 centerx
+ 2, centery
+ plussize
);
2463 SetPixel(hdc
, centerx
- 1, centery
, clrBk
);
2464 SetPixel(hdc
, centerx
+ 1, centery
, clrBk
);
2468 SelectObject(hdc
, old_pen
);
2469 DeleteObject(new_pen
);
2473 SelectObject(hdc
, hbrOld
);
2478 TREEVIEW_DrawItem(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, TREEVIEW_ITEM
*item
)
2482 COLORREF oldTextColor
, oldTextBkColor
;
2484 BOOL inFocus
= (GetFocus() == infoPtr
->hwnd
);
2485 NMTVCUSTOMDRAW nmcdhdr
;
2487 TREEVIEW_UpdateDispInfo(infoPtr
, item
, CALLBACK_MASK_ALL
);
2489 /* - If item is drop target or it is selected and window is in focus -
2490 * use blue background (COLOR_HIGHLIGHT).
2491 * - If item is selected, window is not in focus, but it has style
2492 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2493 * - Otherwise - use background color
2495 if ((item
->state
& TVIS_DROPHILITED
) || ((item
== infoPtr
->focusedItem
) && !(item
->state
& TVIS_SELECTED
)) ||
2496 ((item
->state
& TVIS_SELECTED
) && (!infoPtr
->focusedItem
|| item
== infoPtr
->focusedItem
) &&
2497 (inFocus
|| (infoPtr
->dwStyle
& TVS_SHOWSELALWAYS
))))
2499 if ((item
->state
& TVIS_DROPHILITED
) || inFocus
)
2501 nmcdhdr
.clrTextBk
= comctl32_color
.clrHighlight
;
2502 nmcdhdr
.clrText
= comctl32_color
.clrHighlightText
;
2506 nmcdhdr
.clrTextBk
= comctl32_color
.clrBtnFace
;
2507 nmcdhdr
.clrText
= GETTXTCOLOR(infoPtr
->clrText
);
2512 nmcdhdr
.clrTextBk
= GETBKCOLOR(infoPtr
->clrBk
);
2513 if ((infoPtr
->dwStyle
& TVS_TRACKSELECT
) && (item
== infoPtr
->hotItem
))
2514 nmcdhdr
.clrText
= comctl32_color
.clrHighlight
;
2516 nmcdhdr
.clrText
= GETTXTCOLOR(infoPtr
->clrText
);
2519 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, item
));
2521 /* The custom draw handler can query the text rectangle,
2523 /* should already be known, set to 0 when changed */
2524 if (!item
->textWidth
)
2525 TREEVIEW_ComputeTextWidth(infoPtr
, item
, hdc
);
2529 if (infoPtr
->cdmode
& CDRF_NOTIFYITEMDRAW
)
2531 cditem
= TREEVIEW_SendCustomDrawItemNotify
2532 (infoPtr
, hdc
, item
, CDDS_ITEMPREPAINT
, &nmcdhdr
);
2533 TRACE("prepaint:cditem-app returns 0x%x\n", cditem
);
2535 if (cditem
& CDRF_SKIPDEFAULT
)
2537 SelectObject(hdc
, hOldFont
);
2542 if (cditem
& CDRF_NEWFONT
)
2543 TREEVIEW_ComputeTextWidth(infoPtr
, item
, hdc
);
2545 TREEVIEW_DrawItemLines(infoPtr
, hdc
, item
);
2547 /* Set colors. Custom draw handler can change these so we do this after it. */
2548 oldTextColor
= SetTextColor(hdc
, nmcdhdr
.clrText
);
2549 oldTextBkColor
= SetBkColor(hdc
, nmcdhdr
.clrTextBk
);
2551 centery
= (item
->rect
.top
+ item
->rect
.bottom
) / 2;
2554 * Display the images associated with this item
2559 /* State images are displayed to the left of the Normal image
2560 * image number is in state; zero should be `display no image'.
2562 imageIndex
= STATEIMAGEINDEX(item
->state
);
2564 if (infoPtr
->himlState
&& imageIndex
)
2566 ImageList_Draw(infoPtr
->himlState
, imageIndex
, hdc
,
2568 centery
- infoPtr
->stateImageHeight
/ 2,
2572 /* Now, draw the normal image; can be either selected,
2573 * non-selected or expanded image.
2576 if ((item
->state
& TVIS_SELECTED
) && (item
->iSelectedImage
>= 0))
2578 /* The item is currently selected */
2579 imageIndex
= item
->iSelectedImage
;
2581 else if ((item
->state
& TVIS_EXPANDED
) && (item
->iExpandedImage
!= (WORD
)I_IMAGENONE
))
2583 /* The item is currently not selected but expanded */
2584 imageIndex
= item
->iExpandedImage
;
2588 /* The item is not selected and not expanded */
2589 imageIndex
= item
->iImage
;
2592 if (infoPtr
->himlNormal
)
2594 UINT style
= item
->state
& TVIS_CUT
? ILD_SELECTED
: ILD_NORMAL
;
2596 style
|= item
->state
& TVIS_OVERLAYMASK
;
2598 ImageList_DrawEx(infoPtr
->himlNormal
, imageIndex
, hdc
,
2599 item
->imageOffset
, centery
- infoPtr
->normalImageHeight
/ 2,
2600 0, 0, infoPtr
->clrBk
, item
->state
& TVIS_CUT
? GETBKCOLOR(infoPtr
->clrBk
) : CLR_DEFAULT
,
2607 * Display the text associated with this item
2610 /* Don't paint item's text if it's being edited */
2611 if (!infoPtr
->hwndEdit
|| (infoPtr
->selectedItem
!= item
))
2619 rcText
.top
= item
->rect
.top
;
2620 rcText
.bottom
= item
->rect
.bottom
;
2621 rcText
.left
= item
->textOffset
;
2622 rcText
.right
= rcText
.left
+ item
->textWidth
+ 4;
2624 TRACE("drawing text %s at (%s)\n",
2625 debugstr_w(item
->pszText
), wine_dbgstr_rect(&rcText
));
2628 GetTextExtentPoint32W(hdc
, item
->pszText
, strlenW(item
->pszText
), &sz
);
2630 align
= SetTextAlign(hdc
, TA_LEFT
| TA_TOP
);
2631 ExtTextOutW(hdc
, rcText
.left
+ 2, (rcText
.top
+ rcText
.bottom
- sz
.cy
) / 2,
2632 ETO_CLIPPED
| ETO_OPAQUE
,
2635 lstrlenW(item
->pszText
),
2637 SetTextAlign(hdc
, align
);
2639 /* Draw focus box around the selected item */
2640 if ((item
== infoPtr
->selectedItem
) && inFocus
)
2642 DrawFocusRect(hdc
,&rcText
);
2647 /* Draw insertion mark if necessary */
2649 if (infoPtr
->insertMarkItem
)
2650 TRACE("item:%d,mark:%p\n",
2651 TREEVIEW_GetItemIndex(infoPtr
, item
),
2652 infoPtr
->insertMarkItem
);
2654 if (item
== infoPtr
->insertMarkItem
)
2656 HPEN hNewPen
, hOldPen
;
2660 hNewPen
= CreatePen(PS_SOLID
, 2, GETINSCOLOR(infoPtr
->clrInsertMark
));
2661 hOldPen
= SelectObject(hdc
, hNewPen
);
2663 if (infoPtr
->insertBeforeorAfter
)
2664 offset
= item
->rect
.bottom
- 1;
2666 offset
= item
->rect
.top
+ 1;
2668 left
= item
->textOffset
- 2;
2669 right
= item
->textOffset
+ item
->textWidth
+ 2;
2671 MoveToEx(hdc
, left
, offset
- 3, NULL
);
2672 LineTo(hdc
, left
, offset
+ 4);
2674 MoveToEx(hdc
, left
, offset
, NULL
);
2675 LineTo(hdc
, right
+ 1, offset
);
2677 MoveToEx(hdc
, right
, offset
+ 3, NULL
);
2678 LineTo(hdc
, right
, offset
- 4);
2680 SelectObject(hdc
, hOldPen
);
2681 DeleteObject(hNewPen
);
2684 /* Restore the hdc state */
2685 SetTextColor(hdc
, oldTextColor
);
2686 SetBkColor(hdc
, oldTextBkColor
);
2687 SelectObject(hdc
, hOldFont
);
2689 if (cditem
& CDRF_NOTIFYPOSTPAINT
)
2691 cditem
= TREEVIEW_SendCustomDrawItemNotify
2692 (infoPtr
, hdc
, item
, CDDS_ITEMPOSTPAINT
, &nmcdhdr
);
2693 TRACE("postpaint:cditem-app returns 0x%x\n", cditem
);
2697 /* Computes treeHeight and treeWidth and updates the scroll bars.
2700 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
)
2702 TREEVIEW_ITEM
*item
;
2703 HWND hwnd
= infoPtr
->hwnd
;
2707 LONG scrollX
= infoPtr
->scrollX
;
2709 infoPtr
->treeWidth
= 0;
2710 infoPtr
->treeHeight
= 0;
2712 /* We iterate through all visible items in order to get the tree height
2714 item
= infoPtr
->root
->firstChild
;
2716 while (item
!= NULL
)
2718 if (ISVISIBLE(item
))
2720 /* actually we draw text at textOffset + 2 */
2721 if (2+item
->textOffset
+item
->textWidth
> infoPtr
->treeWidth
)
2722 infoPtr
->treeWidth
= item
->textOffset
+item
->textWidth
+2;
2724 /* This is scroll-adjusted, but we fix this below. */
2725 infoPtr
->treeHeight
= item
->rect
.bottom
;
2728 item
= TREEVIEW_GetNextListItem(infoPtr
, item
);
2731 /* Fix the scroll adjusted treeHeight and treeWidth. */
2732 if (infoPtr
->root
->firstChild
)
2733 infoPtr
->treeHeight
-= infoPtr
->root
->firstChild
->rect
.top
;
2735 infoPtr
->treeWidth
+= infoPtr
->scrollX
;
2737 if (infoPtr
->dwStyle
& TVS_NOSCROLL
) return;
2739 /* Adding one scroll bar may take up enough space that it forces us
2740 * to add the other as well. */
2741 if (infoPtr
->treeHeight
> infoPtr
->clientHeight
)
2745 if (infoPtr
->treeWidth
2746 > infoPtr
->clientWidth
- GetSystemMetrics(SM_CXVSCROLL
))
2749 else if (infoPtr
->treeWidth
> infoPtr
->clientWidth
|| infoPtr
->scrollX
> 0)
2752 if (!vert
&& horz
&& infoPtr
->treeHeight
2753 > infoPtr
->clientHeight
- GetSystemMetrics(SM_CYVSCROLL
))
2756 if (horz
&& (infoPtr
->dwStyle
& TVS_NOHSCROLL
)) horz
= FALSE
;
2758 si
.cbSize
= sizeof(SCROLLINFO
);
2759 si
.fMask
= SIF_POS
|SIF_RANGE
|SIF_PAGE
;
2764 si
.nPage
= TREEVIEW_GetVisibleCount(infoPtr
);
2765 if ( si
.nPage
&& NULL
!= infoPtr
->firstVisible
)
2767 si
.nPos
= infoPtr
->firstVisible
->visibleOrder
;
2768 si
.nMax
= infoPtr
->maxVisibleOrder
- 1;
2770 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
2772 if (!(infoPtr
->uInternalStatus
& TV_VSCROLL
))
2773 ShowScrollBar(hwnd
, SB_VERT
, TRUE
);
2774 infoPtr
->uInternalStatus
|= TV_VSCROLL
;
2778 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2779 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2780 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2785 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2786 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2787 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2792 si
.nPage
= infoPtr
->clientWidth
;
2793 si
.nPos
= infoPtr
->scrollX
;
2794 si
.nMax
= infoPtr
->treeWidth
- 1;
2796 if (si
.nPos
> si
.nMax
- max( si
.nPage
-1, 0 ))
2798 si
.nPos
= si
.nMax
- max( si
.nPage
-1, 0 );
2802 if (!(infoPtr
->uInternalStatus
& TV_HSCROLL
))
2803 ShowScrollBar(hwnd
, SB_HORZ
, TRUE
);
2804 infoPtr
->uInternalStatus
|= TV_HSCROLL
;
2806 SetScrollInfo(hwnd
, SB_HORZ
, &si
, TRUE
);
2807 TREEVIEW_HScroll(infoPtr
,
2808 MAKEWPARAM(SB_THUMBPOSITION
, scrollX
));
2812 if (infoPtr
->uInternalStatus
& TV_HSCROLL
)
2813 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
2814 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2817 if (infoPtr
->scrollX
!= 0)
2819 TREEVIEW_HScroll(infoPtr
,
2820 MAKEWPARAM(SB_THUMBPOSITION
, scrollX
));
2825 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2829 TREEVIEW_FillBkgnd(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, const RECT
*rc
)
2832 COLORREF clrBk
= GETBKCOLOR(infoPtr
->clrBk
);
2834 hBrush
= CreateSolidBrush(clrBk
);
2835 FillRect(hdc
, rc
, hBrush
);
2836 DeleteObject(hBrush
);
2839 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2841 TREEVIEW_EraseBackground(const TREEVIEW_INFO
*infoPtr
, HDC hdc
)
2845 TRACE("%p\n", infoPtr
);
2847 GetClientRect(infoPtr
->hwnd
, &rect
);
2848 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rect
);
2854 TREEVIEW_Refresh(TREEVIEW_INFO
*infoPtr
, HDC hdc
, const RECT
*rc
)
2856 HWND hwnd
= infoPtr
->hwnd
;
2858 TREEVIEW_ITEM
*item
;
2860 if (infoPtr
->clientHeight
== 0 || infoPtr
->clientWidth
== 0)
2862 TRACE("empty window\n");
2866 infoPtr
->cdmode
= TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_PREPAINT
,
2869 if (infoPtr
->cdmode
== CDRF_SKIPDEFAULT
)
2871 ReleaseDC(hwnd
, hdc
);
2875 for (item
= infoPtr
->root
->firstChild
;
2877 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
2879 if (ISVISIBLE(item
))
2881 /* Avoid unneeded calculations */
2882 if (item
->rect
.top
> rect
.bottom
)
2884 if (item
->rect
.bottom
< rect
.top
)
2887 TREEVIEW_DrawItem(infoPtr
, hdc
, item
);
2892 // FIXME: This is correct, but is causes and infinite loop of WM_PAINT
2893 // messages, resulting in continuous painting of the scroll bar in reactos.
2894 // Comment out until the real bug is found. CORE-4912
2897 TREEVIEW_UpdateScrollBars(infoPtr
);
2900 if (infoPtr
->cdmode
& CDRF_NOTIFYPOSTPAINT
)
2902 TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_POSTPAINT
, hdc
, rect
);
2906 TREEVIEW_InvalidateItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
2908 if (item
) InvalidateRect(infoPtr
->hwnd
, &item
->rect
, TRUE
);
2912 TREEVIEW_Invalidate(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
2915 InvalidateRect(infoPtr
->hwnd
, &item
->rect
, TRUE
);
2917 InvalidateRect(infoPtr
->hwnd
, NULL
, TRUE
);
2921 TREEVIEW_InitCheckboxes(TREEVIEW_INFO
*infoPtr
)
2924 HBITMAP hbm
, hbmOld
;
2928 infoPtr
->himlState
= ImageList_Create(16, 16, ILC_COLOR
| ILC_MASK
, 3, 0);
2930 hdcScreen
= GetDC(0);
2932 hdc
= CreateCompatibleDC(hdcScreen
);
2933 hbm
= CreateCompatibleBitmap(hdcScreen
, 48, 16);
2934 hbmOld
= SelectObject(hdc
, hbm
);
2936 SetRect(&rc
, 0, 0, 48, 16);
2937 FillRect(hdc
, &rc
, (HBRUSH
)(COLOR_WINDOW
+1));
2939 SetRect(&rc
, 18, 2, 30, 14);
2940 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
2941 DFCS_BUTTONCHECK
|DFCS_FLAT
);
2943 SetRect(&rc
, 34, 2, 46, 14);
2944 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
2945 DFCS_BUTTONCHECK
|DFCS_FLAT
|DFCS_CHECKED
);
2947 SelectObject(hdc
, hbmOld
);
2948 nIndex
= ImageList_AddMasked(infoPtr
->himlState
, hbm
,
2949 comctl32_color
.clrWindow
);
2950 TRACE("checkbox index %d\n", nIndex
);
2954 ReleaseDC(0, hdcScreen
);
2956 infoPtr
->stateImageWidth
= 16;
2957 infoPtr
->stateImageHeight
= 16;
2961 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2963 TREEVIEW_ITEM
*child
= item
->firstChild
;
2965 item
->state
&= ~TVIS_STATEIMAGEMASK
;
2966 item
->state
|= INDEXTOSTATEIMAGEMASK(1);
2970 TREEVIEW_ITEM
*next
= child
->nextSibling
;
2971 TREEVIEW_ResetImageStateIndex(infoPtr
, child
);
2977 TREEVIEW_Paint(TREEVIEW_INFO
*infoPtr
, HDC hdc_ref
)
2983 TRACE("(%p %p)\n", infoPtr
, hdc_ref
);
2985 if ((infoPtr
->dwStyle
& TVS_CHECKBOXES
) && !infoPtr
->himlState
)
2987 TREEVIEW_InitCheckboxes(infoPtr
);
2988 TREEVIEW_ResetImageStateIndex(infoPtr
, infoPtr
->root
);
2990 TREEVIEW_EndEditLabelNow(infoPtr
, TRUE
);
2991 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
2992 TREEVIEW_UpdateScrollBars(infoPtr
);
2993 TREEVIEW_Invalidate(infoPtr
, NULL
);
2999 GetClientRect(infoPtr
->hwnd
, &rc
);
3000 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rc
);
3004 hdc
= BeginPaint(infoPtr
->hwnd
, &ps
);
3007 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rc
);
3010 if(infoPtr
->bRedraw
) /* WM_SETREDRAW sets bRedraw */
3011 TREEVIEW_Refresh(infoPtr
, hdc
, &rc
);
3014 EndPaint(infoPtr
->hwnd
, &ps
);
3020 TREEVIEW_PrintClient(TREEVIEW_INFO
*infoPtr
, HDC hdc
, DWORD options
)
3022 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc
, options
);
3024 if ((options
& PRF_CHECKVISIBLE
) && !IsWindowVisible(infoPtr
->hwnd
))
3027 if (options
& PRF_ERASEBKGND
)
3028 TREEVIEW_EraseBackground(infoPtr
, hdc
);
3030 if (options
& PRF_CLIENT
)
3033 GetClientRect(infoPtr
->hwnd
, &rc
);
3034 TREEVIEW_Refresh(infoPtr
, hdc
, &rc
);
3040 /* Sorting **************************************************************/
3042 /***************************************************************************
3043 * Forward the DPA local callback to the treeview owner callback
3046 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM
*first
, const TREEVIEW_ITEM
*second
,
3047 const TVSORTCB
*pCallBackSort
)
3049 /* Forward the call to the client-defined callback */
3050 return pCallBackSort
->lpfnCompare(first
->lParam
,
3052 pCallBackSort
->lParam
);
3055 /***************************************************************************
3056 * Treeview native sort routine: sort on item text.
3059 TREEVIEW_SortOnName(TREEVIEW_ITEM
*first
, TREEVIEW_ITEM
*second
,
3060 const TREEVIEW_INFO
*infoPtr
)
3062 TREEVIEW_UpdateDispInfo(infoPtr
, first
, TVIF_TEXT
);
3063 TREEVIEW_UpdateDispInfo(infoPtr
, second
, TVIF_TEXT
);
3065 if(first
->pszText
&& second
->pszText
)
3066 return lstrcmpiW(first
->pszText
, second
->pszText
);
3067 else if(first
->pszText
)
3069 else if(second
->pszText
)
3075 /* Returns the number of physical children belonging to item. */
3077 TREEVIEW_CountChildren(const TREEVIEW_ITEM
*item
)
3082 for (hti
= item
->firstChild
; hti
!= NULL
; hti
= hti
->nextSibling
)
3088 /* Returns a DPA containing a pointer to each physical child of item in
3089 * sibling order. If item has no children, an empty DPA is returned. */
3091 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM
*item
)
3095 HDPA list
= DPA_Create(8);
3096 if (list
== 0) return NULL
;
3098 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
3100 if (DPA_InsertPtr(list
, INT_MAX
, child
) == -1)
3110 /***************************************************************************
3111 * Setup the treeview structure with regards of the sort method
3112 * and sort the children of the TV item specified in lParam
3113 * fRecurse: currently unused. Should be zero.
3114 * parent: if pSort!=NULL, should equal pSort->hParent.
3115 * otherwise, item which child items are to be sorted.
3116 * pSort: sort method info. if NULL, sort on item text.
3117 * if non-NULL, sort on item's lParam content, and let the
3118 * application decide what that means. See also TVM_SORTCHILDRENCB.
3122 TREEVIEW_Sort(TREEVIEW_INFO
*infoPtr
, HTREEITEM parent
,
3126 PFNDPACOMPARE pfnCompare
;
3129 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3130 if (parent
== TVI_ROOT
|| parent
== NULL
)
3131 parent
= infoPtr
->root
;
3133 /* Check for a valid handle to the parent item */
3134 if (!TREEVIEW_ValidItem(infoPtr
, parent
))
3136 ERR("invalid item hParent=%p\n", parent
);
3142 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_CallBackCompare
;
3143 lpCompare
= (LPARAM
)pSort
;
3147 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_SortOnName
;
3148 lpCompare
= (LPARAM
)infoPtr
;
3151 cChildren
= TREEVIEW_CountChildren(parent
);
3153 /* Make sure there is something to sort */
3156 /* TREEVIEW_ITEM rechaining */
3159 HTREEITEM nextItem
= 0;
3160 HTREEITEM prevItem
= 0;
3162 HDPA sortList
= TREEVIEW_BuildChildDPA(parent
);
3164 if (sortList
== NULL
)
3167 /* let DPA sort the list */
3168 DPA_Sort(sortList
, pfnCompare
, lpCompare
);
3170 /* The order of DPA entries has been changed, so fixup the
3171 * nextSibling and prevSibling pointers. */
3173 item
= DPA_GetPtr(sortList
, count
++);
3174 while ((nextItem
= DPA_GetPtr(sortList
, count
++)) != NULL
)
3176 /* link the two current item together */
3177 item
->nextSibling
= nextItem
;
3178 nextItem
->prevSibling
= item
;
3180 if (prevItem
== NULL
)
3182 /* this is the first item, update the parent */
3183 parent
->firstChild
= item
;
3184 item
->prevSibling
= NULL
;
3188 /* fix the back chaining */
3189 item
->prevSibling
= prevItem
;
3192 /* get ready for the next one */
3197 /* the last item is pointed to by item and never has a sibling */
3198 item
->nextSibling
= NULL
;
3199 parent
->lastChild
= item
;
3201 DPA_Destroy(sortList
);
3203 TREEVIEW_VerifyTree(infoPtr
);
3205 if (parent
->state
& TVIS_EXPANDED
)
3207 int visOrder
= infoPtr
->firstVisible
->visibleOrder
;
3209 if (parent
== infoPtr
->root
)
3210 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
3212 TREEVIEW_RecalculateVisibleOrder(infoPtr
, parent
);
3214 if (TREEVIEW_IsChildOf(parent
, infoPtr
->firstVisible
))
3216 TREEVIEW_ITEM
*item
;
3218 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
3219 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
3221 if (item
->visibleOrder
== visOrder
)
3225 if (!item
) item
= parent
->firstChild
;
3226 TREEVIEW_SetFirstVisible(infoPtr
, item
, FALSE
);
3229 TREEVIEW_Invalidate(infoPtr
, NULL
);
3238 /***************************************************************************
3239 * Setup the treeview structure with regards of the sort method
3240 * and sort the children of the TV item specified in lParam
3243 TREEVIEW_SortChildrenCB(TREEVIEW_INFO
*infoPtr
, LPTVSORTCB pSort
)
3245 return TREEVIEW_Sort(infoPtr
, pSort
->hParent
, pSort
);
3249 /***************************************************************************
3250 * Sort the children of the TV item specified in lParam.
3253 TREEVIEW_SortChildren(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
3255 return TREEVIEW_Sort(infoPtr
, (HTREEITEM
)lParam
, NULL
);
3259 /* Expansion/Collapse ***************************************************/
3262 TREEVIEW_SendExpanding(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
3265 return !TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_ITEMEXPANDINGW
, action
,
3266 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
3267 | TVIF_IMAGE
| TVIF_SELECTEDIMAGE
,
3272 TREEVIEW_SendExpanded(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
3275 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_ITEMEXPANDEDW
, action
,
3276 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
3277 | TVIF_IMAGE
| TVIF_SELECTEDIMAGE
,
3282 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3283 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3285 TREEVIEW_Collapse(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
3286 BOOL bRemoveChildren
, BOOL bUser
)
3288 UINT action
= TVE_COLLAPSE
| (bRemoveChildren
? TVE_COLLAPSERESET
: 0);
3289 BOOL bSetSelection
, bSetFirstVisible
;
3291 LONG scrollDist
= 0;
3292 TREEVIEW_ITEM
*nextItem
= NULL
, *tmpItem
;
3295 TRACE("TVE_COLLAPSE %p %s\n", item
, TREEVIEW_ItemName(item
));
3297 if (!TREEVIEW_HasChildren(infoPtr
, item
))
3301 TREEVIEW_SendExpanding(infoPtr
, item
, action
);
3303 if (item
->firstChild
== NULL
)
3306 wasExpanded
= (item
->state
& TVIS_EXPANDED
) != 0;
3307 item
->state
&= ~TVIS_EXPANDED
;
3309 if (wasExpanded
&& bUser
)
3310 TREEVIEW_SendExpanded(infoPtr
, item
, action
);
3312 bSetSelection
= (infoPtr
->selectedItem
!= NULL
3313 && TREEVIEW_IsChildOf(item
, infoPtr
->selectedItem
));
3315 bSetFirstVisible
= (infoPtr
->firstVisible
!= NULL
3316 && TREEVIEW_IsChildOf(item
, infoPtr
->firstVisible
));
3321 if (tmpItem
->nextSibling
)
3323 nextItem
= tmpItem
->nextSibling
;
3326 tmpItem
= tmpItem
->parent
;
3330 scrollDist
= nextItem
->rect
.top
;
3332 if (bRemoveChildren
)
3334 INT old_cChildren
= item
->cChildren
;
3335 TRACE("TVE_COLLAPSERESET\n");
3336 item
->state
&= ~TVIS_EXPANDEDONCE
;
3337 TREEVIEW_RemoveAllChildren(infoPtr
, item
);
3338 item
->cChildren
= old_cChildren
;
3343 if (item
->firstChild
)
3345 TREEVIEW_ITEM
*i
, *sibling
;
3347 sibling
= TREEVIEW_GetNextListItem(infoPtr
, item
);
3349 for (i
= item
->firstChild
; i
!= sibling
;
3350 i
= TREEVIEW_GetNextListItem(infoPtr
, i
))
3352 i
->visibleOrder
= -1;
3356 TREEVIEW_RecalculateVisibleOrder(infoPtr
, item
);
3359 scrollDist
= -(scrollDist
- nextItem
->rect
.top
);
3363 /* Don't call DoSelectItem, it sends notifications. */
3364 if (TREEVIEW_ValidItem(infoPtr
, infoPtr
->selectedItem
))
3365 infoPtr
->selectedItem
->state
&= ~TVIS_SELECTED
;
3366 item
->state
|= TVIS_SELECTED
;
3367 infoPtr
->selectedItem
= item
;
3370 TREEVIEW_UpdateScrollBars(infoPtr
);
3372 scrollRect
.left
= 0;
3373 scrollRect
.right
= infoPtr
->clientWidth
;
3374 scrollRect
.bottom
= infoPtr
->clientHeight
;
3378 scrollRect
.top
= nextItem
->rect
.top
;
3380 ScrollWindowEx (infoPtr
->hwnd
, 0, scrollDist
, &scrollRect
, &scrollRect
,
3381 NULL
, NULL
, SW_ERASE
| SW_INVALIDATE
);
3382 TREEVIEW_Invalidate(infoPtr
, item
);
3384 scrollRect
.top
= item
->rect
.top
;
3385 InvalidateRect(infoPtr
->hwnd
, &scrollRect
, TRUE
);
3388 TREEVIEW_SetFirstVisible(infoPtr
,
3389 bSetFirstVisible
? item
: infoPtr
->firstVisible
,
3396 TREEVIEW_Expand(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
3397 BOOL partial
, BOOL user
)
3400 LONG orgNextTop
= 0;
3402 TREEVIEW_ITEM
*nextItem
, *tmpItem
;
3403 BOOL sendsNotifications
;
3405 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr
, item
, partial
, user
);
3407 if (!TREEVIEW_HasChildren(infoPtr
, item
))
3410 tmpItem
= item
; nextItem
= NULL
;
3413 if (tmpItem
->nextSibling
)
3415 nextItem
= tmpItem
->nextSibling
;
3418 tmpItem
= tmpItem
->parent
;
3422 orgNextTop
= nextItem
->rect
.top
;
3424 TRACE("TVE_EXPAND %p %s\n", item
, TREEVIEW_ItemName(item
));
3426 sendsNotifications
= user
|| ((item
->cChildren
!= 0) &&
3427 !(item
->state
& TVIS_EXPANDEDONCE
));
3428 if (sendsNotifications
)
3430 if (!TREEVIEW_SendExpanding(infoPtr
, item
, TVE_EXPAND
))
3432 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3436 if (!item
->firstChild
)
3439 item
->state
|= TVIS_EXPANDED
;
3442 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3444 if (ISVISIBLE(item
))
3446 TREEVIEW_RecalculateVisibleOrder(infoPtr
, item
);
3447 TREEVIEW_UpdateSubTree(infoPtr
, item
);
3448 TREEVIEW_UpdateScrollBars(infoPtr
);
3450 scrollRect
.left
= 0;
3451 scrollRect
.bottom
= infoPtr
->treeHeight
;
3452 scrollRect
.right
= infoPtr
->clientWidth
;
3455 scrollDist
= nextItem
->rect
.top
- orgNextTop
;
3456 scrollRect
.top
= orgNextTop
;
3458 ScrollWindowEx (infoPtr
->hwnd
, 0, scrollDist
, &scrollRect
, NULL
,
3459 NULL
, NULL
, SW_ERASE
| SW_INVALIDATE
);
3460 TREEVIEW_Invalidate (infoPtr
, item
);
3462 scrollRect
.top
= item
->rect
.top
;
3463 InvalidateRect(infoPtr
->hwnd
, &scrollRect
, FALSE
);
3466 /* Scroll up so that as many children as possible are visible.
3467 * This fails when expanding causes an HScroll bar to appear, but we
3468 * don't know that yet, so the last item is obscured. */
3469 if (item
->firstChild
!= NULL
)
3471 int nChildren
= item
->lastChild
->visibleOrder
3472 - item
->firstChild
->visibleOrder
+ 1;
3474 int visible_pos
= item
->visibleOrder
3475 - infoPtr
->firstVisible
->visibleOrder
;
3477 int rows_below
= TREEVIEW_GetVisibleCount(infoPtr
) - visible_pos
- 1;
3479 if (visible_pos
> 0 && nChildren
> rows_below
)
3481 int scroll
= nChildren
- rows_below
;
3483 if (scroll
> visible_pos
)
3484 scroll
= visible_pos
;
3488 TREEVIEW_ITEM
*newFirstVisible
3489 = TREEVIEW_GetListItem(infoPtr
, infoPtr
->firstVisible
,
3493 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
3499 if (sendsNotifications
) {
3500 TREEVIEW_SendExpanded(infoPtr
, item
, TVE_EXPAND
);
3501 item
->state
|= TVIS_EXPANDEDONCE
;
3507 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3508 to mouse messages and TVM_SELECTITEM.
3510 selection - previously selected item, used to collapse a part of a tree
3511 item - new selected item
3513 static void TREEVIEW_SingleExpand(TREEVIEW_INFO
*infoPtr
,
3514 HTREEITEM selection
, HTREEITEM item
)
3516 TREEVIEW_ITEM
*prev
, *curr
;
3518 if ((infoPtr
->dwStyle
& TVS_SINGLEEXPAND
) == 0 || infoPtr
->hwndEdit
|| !item
) return;
3520 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_SINGLEEXPAND
, TVC_UNKNOWN
, TVIF_HANDLE
| TVIF_PARAM
, item
, 0);
3523 * Close the previous item and its ancestors as long as they are not
3524 * ancestors of the current item
3526 for (prev
= selection
; prev
&& TREEVIEW_ValidItem(infoPtr
, prev
); prev
= prev
->parent
)
3528 for (curr
= item
; curr
&& TREEVIEW_ValidItem(infoPtr
, curr
); curr
= curr
->parent
)
3533 TREEVIEW_Collapse(infoPtr
, prev
, FALSE
, TRUE
);
3538 * Expand the current item
3540 TREEVIEW_Expand(infoPtr
, item
, FALSE
, TRUE
);
3544 TREEVIEW_Toggle(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
, BOOL user
)
3546 TRACE("item=%p, user=%d\n", item
, user
);
3548 if (item
->state
& TVIS_EXPANDED
)
3549 return TREEVIEW_Collapse(infoPtr
, item
, FALSE
, user
);
3551 return TREEVIEW_Expand(infoPtr
, item
, FALSE
, user
);
3555 TREEVIEW_ExpandAll(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
3557 TREEVIEW_Expand(infoPtr
, item
, FALSE
, TRUE
);
3559 for (item
= item
->firstChild
; item
!= NULL
; item
= item
->nextSibling
)
3561 if (TREEVIEW_HasChildren(infoPtr
, item
))
3562 TREEVIEW_ExpandAll(infoPtr
, item
);
3566 /* Note:If the specified item is the child of a collapsed parent item,
3567 the parent's list of child items is (recursively) expanded to reveal the
3568 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3569 know if it also applies here.
3573 TREEVIEW_ExpandMsg(TREEVIEW_INFO
*infoPtr
, UINT flag
, HTREEITEM item
)
3575 if (!TREEVIEW_ValidItem(infoPtr
, item
))
3578 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3579 TREEVIEW_ItemName(item
), TREEVIEW_GetItemIndex(infoPtr
, item
),
3582 switch (flag
& TVE_TOGGLE
)
3585 return TREEVIEW_Collapse(infoPtr
, item
, flag
& TVE_COLLAPSERESET
,
3589 return TREEVIEW_Expand(infoPtr
, item
, flag
& TVE_EXPANDPARTIAL
,
3593 return TREEVIEW_Toggle(infoPtr
, item
, FALSE
);
3600 /* Hit-Testing **********************************************************/
3602 static TREEVIEW_ITEM
*
3603 TREEVIEW_HitTestPoint(const TREEVIEW_INFO
*infoPtr
, POINT pt
)
3605 TREEVIEW_ITEM
*item
;
3608 if (!infoPtr
->firstVisible
)
3611 row
= pt
.y
/ infoPtr
->uItemHeight
+ infoPtr
->firstVisible
->visibleOrder
;
3613 for (item
= infoPtr
->firstVisible
; item
!= NULL
;
3614 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
3616 if (row
>= item
->visibleOrder
3617 && row
< item
->visibleOrder
+ item
->iIntegral
)
3625 TREEVIEW_HitTest(const TREEVIEW_INFO
*infoPtr
, LPTVHITTESTINFO lpht
)
3627 TREEVIEW_ITEM
*item
;
3633 GetClientRect(infoPtr
->hwnd
, &rect
);
3640 status
|= TVHT_TOLEFT
;
3642 else if (x
> rect
.right
)
3644 status
|= TVHT_TORIGHT
;
3649 status
|= TVHT_ABOVE
;
3651 else if (y
> rect
.bottom
)
3653 status
|= TVHT_BELOW
;
3658 lpht
->flags
= status
;
3662 item
= TREEVIEW_HitTestPoint(infoPtr
, lpht
->pt
);
3665 lpht
->flags
= TVHT_NOWHERE
;
3669 if (x
>= item
->textOffset
+ item
->textWidth
)
3671 lpht
->flags
= TVHT_ONITEMRIGHT
;
3673 else if (x
>= item
->textOffset
)
3675 lpht
->flags
= TVHT_ONITEMLABEL
;
3677 else if (x
>= item
->imageOffset
)
3679 lpht
->flags
= TVHT_ONITEMICON
;
3681 else if (x
>= item
->stateOffset
)
3683 lpht
->flags
= TVHT_ONITEMSTATEICON
;
3685 else if (x
>= item
->linesOffset
&& infoPtr
->dwStyle
& TVS_HASBUTTONS
)
3687 lpht
->flags
= TVHT_ONITEMBUTTON
;
3691 lpht
->flags
= TVHT_ONITEMINDENT
;
3695 TRACE("(%d,%d):result 0x%x\n", lpht
->pt
.x
, lpht
->pt
.y
, lpht
->flags
);
3697 return (LRESULT
)item
;
3700 /* Item Label Editing ***************************************************/
3703 TREEVIEW_GetEditControl(const TREEVIEW_INFO
*infoPtr
)
3705 return (LRESULT
)infoPtr
->hwndEdit
;
3708 static LRESULT CALLBACK
3709 TREEVIEW_Edit_SubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
3711 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3712 BOOL bCancel
= FALSE
;
3718 TRACE("WM_PAINT start\n");
3719 rc
= CallWindowProcW(infoPtr
->wpEditOrig
, hwnd
, uMsg
, wParam
,
3721 TRACE("WM_PAINT done\n");
3725 if (infoPtr
->bIgnoreEditKillFocus
)
3731 WNDPROC editProc
= infoPtr
->wpEditOrig
;
3732 infoPtr
->wpEditOrig
= 0;
3733 SetWindowLongPtrW(hwnd
, GWLP_WNDPROC
, (DWORD_PTR
)editProc
);
3734 return CallWindowProcW(editProc
, hwnd
, uMsg
, wParam
, lParam
);
3738 return DLGC_WANTARROWS
| DLGC_WANTALLKEYS
;
3741 if (wParam
== VK_ESCAPE
)
3746 else if (wParam
== VK_RETURN
)
3753 return CallWindowProcW(infoPtr
->wpEditOrig
, hwnd
, uMsg
, wParam
, lParam
);
3756 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3757 /* eg. Using a messagebox */
3759 infoPtr
->bIgnoreEditKillFocus
= TRUE
;
3760 TREEVIEW_EndEditLabelNow(infoPtr
, bCancel
|| !infoPtr
->bLabelChanged
);
3761 infoPtr
->bIgnoreEditKillFocus
= FALSE
;
3767 /* should handle edit control messages here */
3770 TREEVIEW_Command(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
3772 TRACE("code=0x%x, id=0x%x, handle=0x%lx\n", HIWORD(wParam
), LOWORD(wParam
), lParam
);
3774 switch (HIWORD(wParam
))
3779 * Adjust the edit window size
3782 TREEVIEW_ITEM
*editItem
= infoPtr
->editItem
;
3783 HDC hdc
= GetDC(infoPtr
->hwndEdit
);
3785 HFONT hFont
, hOldFont
= 0;
3787 TRACE("edit=%p\n", infoPtr
->hwndEdit
);
3789 if (!IsWindow(infoPtr
->hwndEdit
) || !hdc
) return FALSE
;
3791 infoPtr
->bLabelChanged
= TRUE
;
3793 GetWindowTextW(infoPtr
->hwndEdit
, buffer
, sizeof(buffer
)/sizeof(buffer
[0]));
3795 /* Select font to get the right dimension of the string */
3796 hFont
= (HFONT
)SendMessageW(infoPtr
->hwndEdit
, WM_GETFONT
, 0, 0);
3800 hOldFont
= SelectObject(hdc
, hFont
);
3803 if (GetTextExtentPoint32W(hdc
, buffer
, strlenW(buffer
), &sz
))
3805 TEXTMETRICW textMetric
;
3807 /* Add Extra spacing for the next character */
3808 GetTextMetricsW(hdc
, &textMetric
);
3809 sz
.cx
+= (textMetric
.tmMaxCharWidth
* 2);
3811 sz
.cx
= max(sz
.cx
, textMetric
.tmMaxCharWidth
* 3);
3813 infoPtr
->clientWidth
- editItem
->textOffset
+ 2);
3815 SetWindowPos(infoPtr
->hwndEdit
,
3820 editItem
->rect
.bottom
- editItem
->rect
.top
+ 3,
3821 SWP_NOMOVE
| SWP_DRAWFRAME
);
3826 SelectObject(hdc
, hOldFont
);
3829 ReleaseDC(infoPtr
->hwnd
, hdc
);
3833 /* apparently we should respect passed handle value */
3834 if (infoPtr
->hwndEdit
!= (HWND
)lParam
) return FALSE
;
3836 TREEVIEW_EndEditLabelNow(infoPtr
, FALSE
);
3840 return SendMessageW(infoPtr
->hwndNotify
, WM_COMMAND
, wParam
, lParam
);
3847 TREEVIEW_EditLabel(TREEVIEW_INFO
*infoPtr
, HTREEITEM hItem
)
3849 HWND hwnd
= infoPtr
->hwnd
;
3852 HINSTANCE hinst
= (HINSTANCE
)GetWindowLongPtrW(hwnd
, GWLP_HINSTANCE
);
3855 TEXTMETRICW textMetric
;
3857 TRACE("%p %p\n", hwnd
, hItem
);
3858 if (!(infoPtr
->dwStyle
& TVS_EDITLABELS
))
3861 if (!TREEVIEW_ValidItem(infoPtr
, hItem
))
3864 if (infoPtr
->hwndEdit
)
3865 return infoPtr
->hwndEdit
;
3867 infoPtr
->bLabelChanged
= FALSE
;
3869 /* make edit item visible */
3870 TREEVIEW_EnsureVisible(infoPtr
, hItem
, TRUE
);
3872 TREEVIEW_UpdateDispInfo(infoPtr
, hItem
, TVIF_TEXT
);
3875 /* Select the font to get appropriate metric dimensions */
3876 if (infoPtr
->hFont
!= 0)
3878 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
3881 /* Get string length in pixels */
3883 GetTextExtentPoint32W(hdc
, hItem
->pszText
, strlenW(hItem
->pszText
),
3886 GetTextExtentPoint32A(hdc
, "", 0, &sz
);
3888 /* Add Extra spacing for the next character */
3889 GetTextMetricsW(hdc
, &textMetric
);
3890 sz
.cx
+= (textMetric
.tmMaxCharWidth
* 2);
3892 sz
.cx
= max(sz
.cx
, textMetric
.tmMaxCharWidth
* 3);
3893 sz
.cx
= min(sz
.cx
, infoPtr
->clientWidth
- hItem
->textOffset
+ 2);
3895 if (infoPtr
->hFont
!= 0)
3897 SelectObject(hdc
, hOldFont
);
3900 ReleaseDC(hwnd
, hdc
);
3902 infoPtr
->editItem
= hItem
;
3904 hwndEdit
= CreateWindowExW(WS_EX_LEFT
,
3907 WS_CHILD
| WS_BORDER
| ES_AUTOHSCROLL
|
3908 WS_CLIPSIBLINGS
| ES_WANTRETURN
|
3909 ES_LEFT
, hItem
->textOffset
- 2,
3910 hItem
->rect
.top
- 1, sz
.cx
+ 3,
3911 hItem
->rect
.bottom
-
3912 hItem
->rect
.top
+ 3, hwnd
, 0, hinst
, 0);
3913 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3915 infoPtr
->hwndEdit
= hwndEdit
;
3917 /* Get a 2D border. */
3918 SetWindowLongW(hwndEdit
, GWL_EXSTYLE
,
3919 GetWindowLongW(hwndEdit
, GWL_EXSTYLE
) & ~WS_EX_CLIENTEDGE
);
3920 SetWindowLongW(hwndEdit
, GWL_STYLE
,
3921 GetWindowLongW(hwndEdit
, GWL_STYLE
) | WS_BORDER
);
3923 SendMessageW(hwndEdit
, WM_SETFONT
,
3924 (WPARAM
)TREEVIEW_FontForItem(infoPtr
, hItem
), FALSE
);
3926 infoPtr
->wpEditOrig
= (WNDPROC
)SetWindowLongPtrW(hwndEdit
, GWLP_WNDPROC
,
3928 TREEVIEW_Edit_SubclassProc
);
3930 SetWindowTextW(hwndEdit
, hItem
->pszText
);
3932 if (TREEVIEW_BeginLabelEditNotify(infoPtr
, hItem
))
3934 DestroyWindow(hwndEdit
);
3935 infoPtr
->hwndEdit
= 0;
3936 infoPtr
->editItem
= NULL
;
3941 SendMessageW(hwndEdit
, EM_SETSEL
, 0, -1);
3942 ShowWindow(hwndEdit
, SW_SHOW
);
3949 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
)
3951 TREEVIEW_ITEM
*editedItem
= infoPtr
->editItem
;
3954 WCHAR tmpText
[1024] = { '\0' };
3955 WCHAR
*newText
= tmpText
;
3958 if (!IsWindow(infoPtr
->hwndEdit
)) return FALSE
;
3961 tvdi
.item
.hItem
= editedItem
;
3962 tvdi
.item
.state
= editedItem
->state
;
3963 tvdi
.item
.lParam
= editedItem
->lParam
;
3967 if (!infoPtr
->bNtfUnicode
)
3968 iLength
= GetWindowTextA(infoPtr
->hwndEdit
, (LPSTR
)tmpText
, 1023);
3970 iLength
= GetWindowTextW(infoPtr
->hwndEdit
, tmpText
, 1023);
3972 if (iLength
>= 1023)
3974 ERR("Insufficient space to retrieve new item label\n");
3977 tvdi
.item
.mask
= TVIF_TEXT
;
3978 tvdi
.item
.pszText
= tmpText
;
3979 tvdi
.item
.cchTextMax
= iLength
+ 1;
3983 tvdi
.item
.pszText
= NULL
;
3984 tvdi
.item
.cchTextMax
= 0;
3987 bCommit
= TREEVIEW_SendRealNotify(infoPtr
, TVN_ENDLABELEDITW
, &tvdi
.hdr
);
3989 if (!bCancel
&& bCommit
) /* Apply the changes */
3991 if (!infoPtr
->bNtfUnicode
)
3993 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, (LPSTR
)tmpText
, -1, NULL
, 0 );
3994 newText
= Alloc(len
* sizeof(WCHAR
));
3995 MultiByteToWideChar( CP_ACP
, 0, (LPSTR
)tmpText
, -1, newText
, len
);
3999 if (strcmpW(newText
, editedItem
->pszText
) != 0)
4001 WCHAR
*ptr
= ReAlloc(editedItem
->pszText
, sizeof(WCHAR
)*(iLength
+ 1));
4004 ERR("OutOfMemory, cannot allocate space for label\n");
4005 if(newText
!= tmpText
) Free(newText
);
4006 DestroyWindow(infoPtr
->hwndEdit
);
4007 infoPtr
->hwndEdit
= 0;
4008 infoPtr
->editItem
= NULL
;
4013 editedItem
->pszText
= ptr
;
4014 editedItem
->cchTextMax
= iLength
+ 1;
4015 strcpyW(editedItem
->pszText
, newText
);
4016 TREEVIEW_ComputeTextWidth(infoPtr
, editedItem
, 0);
4019 if(newText
!= tmpText
) Free(newText
);
4022 ShowWindow(infoPtr
->hwndEdit
, SW_HIDE
);
4023 DestroyWindow(infoPtr
->hwndEdit
);
4024 infoPtr
->hwndEdit
= 0;
4025 infoPtr
->editItem
= NULL
;
4030 TREEVIEW_HandleTimer(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4032 if (wParam
!= TV_EDIT_TIMER
)
4034 ERR("got unknown timer\n");
4038 KillTimer(infoPtr
->hwnd
, TV_EDIT_TIMER
);
4039 infoPtr
->Timer
&= ~TV_EDIT_TIMER_SET
;
4041 TREEVIEW_EditLabel(infoPtr
, infoPtr
->selectedItem
);
4047 /* Mouse Tracking/Drag **************************************************/
4049 /***************************************************************************
4050 * This is quite unusual piece of code, but that's how it's implemented in
4054 TREEVIEW_TrackMouse(const TREEVIEW_INFO
*infoPtr
, POINT pt
)
4056 INT cxDrag
= GetSystemMetrics(SM_CXDRAG
);
4057 INT cyDrag
= GetSystemMetrics(SM_CYDRAG
);
4061 r
.top
= pt
.y
- cyDrag
;
4062 r
.left
= pt
.x
- cxDrag
;
4063 r
.bottom
= pt
.y
+ cyDrag
;
4064 r
.right
= pt
.x
+ cxDrag
;
4066 SetCapture(infoPtr
->hwnd
);
4070 if (PeekMessageW(&msg
, 0, 0, 0, PM_REMOVE
| PM_NOYIELD
))
4072 if (msg
.message
== WM_MOUSEMOVE
)
4074 pt
.x
= (short)LOWORD(msg
.lParam
);
4075 pt
.y
= (short)HIWORD(msg
.lParam
);
4076 if (PtInRect(&r
, pt
))
4084 else if (msg
.message
>= WM_LBUTTONDOWN
&&
4085 msg
.message
<= WM_RBUTTONDBLCLK
)
4090 DispatchMessageW(&msg
);
4093 if (GetCapture() != infoPtr
->hwnd
)
4103 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4105 TREEVIEW_ITEM
*item
;
4109 SetFocus(infoPtr
->hwnd
);
4111 if (infoPtr
->Timer
& TV_EDIT_TIMER_SET
)
4113 /* If there is pending 'edit label' event - kill it now */
4114 KillTimer(infoPtr
->hwnd
, TV_EDIT_TIMER
);
4117 hit
.pt
.x
= (short)LOWORD(lParam
);
4118 hit
.pt
.y
= (short)HIWORD(lParam
);
4120 item
= (TREEVIEW_ITEM
*)TREEVIEW_HitTest(infoPtr
, &hit
);
4123 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr
, item
));
4125 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_DBLCLK
) == FALSE
)
4129 case TVHT_ONITEMRIGHT
:
4130 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4133 case TVHT_ONITEMINDENT
:
4134 if (!(infoPtr
->dwStyle
& TVS_HASLINES
))
4140 int level
= hit
.pt
.x
/ infoPtr
->uIndent
;
4141 if (!(infoPtr
->dwStyle
& TVS_LINESATROOT
)) level
++;
4143 while (item
->iLevel
> level
)
4145 item
= item
->parent
;
4151 case TVHT_ONITEMLABEL
:
4152 case TVHT_ONITEMICON
:
4153 case TVHT_ONITEMBUTTON
:
4154 TREEVIEW_Toggle(infoPtr
, item
, TRUE
);
4157 case TVHT_ONITEMSTATEICON
:
4158 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
4159 TREEVIEW_ToggleItemState(infoPtr
, item
);
4161 TREEVIEW_Toggle(infoPtr
, item
, TRUE
);
4170 TREEVIEW_LButtonDown(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4172 HWND hwnd
= infoPtr
->hwnd
;
4174 BOOL bTrack
, bDoLabelEdit
;
4176 /* If Edit control is active - kill it and return.
4177 * The best way to do it is to set focus to itself.
4178 * Edit control subclassed procedure will automatically call
4181 if (infoPtr
->hwndEdit
)
4187 ht
.pt
.x
= (short)LOWORD(lParam
);
4188 ht
.pt
.y
= (short)HIWORD(lParam
);
4190 TREEVIEW_HitTest(infoPtr
, &ht
);
4191 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr
, ht
.hItem
));
4193 /* update focusedItem and redraw both items */
4194 if(ht
.hItem
&& (ht
.flags
& TVHT_ONITEM
))
4196 infoPtr
->focusedItem
= ht
.hItem
;
4197 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->focusedItem
);
4198 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->selectedItem
);
4201 bTrack
= (ht
.flags
& TVHT_ONITEM
)
4202 && !(infoPtr
->dwStyle
& TVS_DISABLEDRAGDROP
);
4205 * If the style allows editing and the node is already selected
4206 * and the click occurred on the item label...
4208 bDoLabelEdit
= (infoPtr
->dwStyle
& TVS_EDITLABELS
) &&
4209 (ht
.flags
& TVHT_ONITEMLABEL
) && (infoPtr
->selectedItem
== ht
.hItem
);
4211 /* Send NM_CLICK right away */
4213 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_CLICK
))
4216 if (ht
.flags
& TVHT_ONITEMBUTTON
)
4218 TREEVIEW_Toggle(infoPtr
, ht
.hItem
, TRUE
);
4222 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4223 if (TREEVIEW_TrackMouse(infoPtr
, ht
.pt
))
4225 TREEVIEW_SendTreeviewDnDNotify(infoPtr
, TVN_BEGINDRAGW
, ht
.hItem
, ht
.pt
);
4226 infoPtr
->dropItem
= ht
.hItem
;
4228 /* clean up focusedItem as we dragged and won't select this item */
4229 if(infoPtr
->focusedItem
)
4231 /* refresh the item that was focused */
4232 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->focusedItem
);
4233 infoPtr
->focusedItem
= NULL
;
4235 /* refresh the selected item to return the filled background */
4236 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->selectedItem
);
4243 if (bTrack
&& TREEVIEW_SendSimpleNotify(infoPtr
, NM_CLICK
))
4248 if (infoPtr
->Timer
& TV_EDIT_TIMER_SET
)
4249 KillTimer(hwnd
, TV_EDIT_TIMER
);
4251 SetTimer(hwnd
, TV_EDIT_TIMER
, GetDoubleClickTime(), 0);
4252 infoPtr
->Timer
|= TV_EDIT_TIMER_SET
;
4254 else if (ht
.flags
& (TVHT_ONITEMICON
|TVHT_ONITEMLABEL
)) /* select the item if the hit was inside of the icon or text */
4256 TREEVIEW_ITEM
*selection
= infoPtr
->selectedItem
;
4258 /* Select the current item */
4259 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, ht
.hItem
, TVC_BYMOUSE
);
4260 TREEVIEW_SingleExpand(infoPtr
, selection
, ht
.hItem
);
4262 else if (ht
.flags
& TVHT_ONITEMSTATEICON
)
4264 /* TVS_CHECKBOXES requires us to toggle the current state */
4265 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
4266 TREEVIEW_ToggleItemState(infoPtr
, ht
.hItem
);
4276 TREEVIEW_RButtonDown(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4280 if (infoPtr
->hwndEdit
)
4282 SetFocus(infoPtr
->hwnd
);
4286 ht
.pt
.x
= (short)LOWORD(lParam
);
4287 ht
.pt
.y
= (short)HIWORD(lParam
);
4289 TREEVIEW_HitTest(infoPtr
, &ht
);
4291 if (TREEVIEW_TrackMouse(infoPtr
, ht
.pt
))
4295 TREEVIEW_SendTreeviewDnDNotify(infoPtr
, TVN_BEGINRDRAGW
, ht
.hItem
, ht
.pt
);
4296 infoPtr
->dropItem
= ht
.hItem
;
4301 SetFocus(infoPtr
->hwnd
);
4302 if(!TREEVIEW_SendSimpleNotify(infoPtr
, NM_RCLICK
))
4304 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4305 SendMessageW(infoPtr
->hwndNotify
, WM_CONTEXTMENU
,
4306 (WPARAM
)infoPtr
->hwnd
, (LPARAM
)GetMessagePos());
4314 TREEVIEW_CreateDragImage(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4316 TREEVIEW_ITEM
*dragItem
= (HTREEITEM
)lParam
;
4320 HBITMAP hbmp
, hOldbmp
;
4327 if (!(infoPtr
->himlNormal
))
4330 if (!dragItem
|| !TREEVIEW_ValidItem(infoPtr
, dragItem
))
4333 TREEVIEW_UpdateDispInfo(infoPtr
, dragItem
, TVIF_TEXT
);
4335 hwtop
= GetDesktopWindow();
4336 htopdc
= GetDC(hwtop
);
4337 hdc
= CreateCompatibleDC(htopdc
);
4339 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
4341 if (dragItem
->pszText
)
4342 GetTextExtentPoint32W(hdc
, dragItem
->pszText
, strlenW(dragItem
->pszText
),
4345 GetTextExtentPoint32A(hdc
, "", 0, &size
);
4347 TRACE("%d %d %s\n", size
.cx
, size
.cy
, debugstr_w(dragItem
->pszText
));
4348 hbmp
= CreateCompatibleBitmap(htopdc
, size
.cx
, size
.cy
);
4349 hOldbmp
= SelectObject(hdc
, hbmp
);
4351 ImageList_GetIconSize(infoPtr
->himlNormal
, &cx
, &cy
);
4356 infoPtr
->dragList
= ImageList_Create(size
.cx
, size
.cy
, ILC_COLOR
, 10, 10);
4357 ImageList_Draw(infoPtr
->himlNormal
, dragItem
->iImage
, hdc
, 0, 0,
4361 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4362 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4365 /* draw item text */
4367 SetRect(&rc
, cx
, 0, size
.cx
, size
.cy
);
4369 if (dragItem
->pszText
)
4370 DrawTextW(hdc
, dragItem
->pszText
, strlenW(dragItem
->pszText
), &rc
,
4373 SelectObject(hdc
, hOldFont
);
4374 SelectObject(hdc
, hOldbmp
);
4376 ImageList_Add(infoPtr
->dragList
, hbmp
, 0);
4380 ReleaseDC(hwtop
, htopdc
);
4382 return (LRESULT
)infoPtr
->dragList
;
4385 /* Selection ************************************************************/
4388 TREEVIEW_DoSelectItem(TREEVIEW_INFO
*infoPtr
, INT action
, HTREEITEM newSelect
,
4391 TREEVIEW_ITEM
*prevSelect
;
4393 assert(newSelect
== NULL
|| TREEVIEW_ValidItem(infoPtr
, newSelect
));
4395 TRACE("Entering item %p (%s), flag 0x%x, cause 0x%x, state %d\n",
4396 newSelect
, TREEVIEW_ItemName(newSelect
), action
, cause
,
4397 newSelect
? newSelect
->state
: 0);
4399 /* reset and redraw focusedItem if focusedItem was set so we don't */
4400 /* have to worry about the previously focused item when we set a new one */
4401 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->focusedItem
);
4402 infoPtr
->focusedItem
= NULL
;
4406 case TVGN_CARET
|TVSI_NOSINGLEEXPAND
:
4407 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4410 prevSelect
= infoPtr
->selectedItem
;
4412 if (prevSelect
== newSelect
) {
4413 TREEVIEW_EnsureVisible(infoPtr
, infoPtr
->selectedItem
, FALSE
);
4417 if (TREEVIEW_SendTreeviewNotify(infoPtr
,
4420 TVIF_TEXT
| TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
,
4426 prevSelect
->state
&= ~TVIS_SELECTED
;
4428 newSelect
->state
|= TVIS_SELECTED
;
4430 infoPtr
->selectedItem
= newSelect
;
4432 TREEVIEW_EnsureVisible(infoPtr
, infoPtr
->selectedItem
, FALSE
);
4434 TREEVIEW_InvalidateItem(infoPtr
, prevSelect
);
4435 TREEVIEW_InvalidateItem(infoPtr
, newSelect
);
4437 TREEVIEW_SendTreeviewNotify(infoPtr
,
4440 TVIF_TEXT
| TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
,
4445 case TVGN_DROPHILITE
:
4446 prevSelect
= infoPtr
->dropItem
;
4449 prevSelect
->state
&= ~TVIS_DROPHILITED
;
4451 infoPtr
->dropItem
= newSelect
;
4454 newSelect
->state
|= TVIS_DROPHILITED
;
4456 TREEVIEW_Invalidate(infoPtr
, prevSelect
);
4457 TREEVIEW_Invalidate(infoPtr
, newSelect
);
4460 case TVGN_FIRSTVISIBLE
:
4461 if (newSelect
!= NULL
)
4463 TREEVIEW_EnsureVisible(infoPtr
, newSelect
, FALSE
);
4464 TREEVIEW_SetFirstVisible(infoPtr
, newSelect
, TRUE
);
4465 TREEVIEW_Invalidate(infoPtr
, NULL
);
4470 TRACE("Leaving state %d\n", newSelect
? newSelect
->state
: 0);
4474 /* FIXME: handle NM_KILLFOCUS etc */
4476 TREEVIEW_SelectItem(TREEVIEW_INFO
*infoPtr
, INT wParam
, HTREEITEM item
)
4478 TREEVIEW_ITEM
*selection
= infoPtr
->selectedItem
;
4480 if (item
&& !TREEVIEW_ValidItem(infoPtr
, item
))
4483 if (item
== infoPtr
->selectedItem
)
4486 TRACE("%p (%s) %d\n", item
, TREEVIEW_ItemName(item
), wParam
);
4488 if (!TREEVIEW_DoSelectItem(infoPtr
, wParam
, item
, TVC_UNKNOWN
))
4491 TREEVIEW_SingleExpand(infoPtr
, selection
, item
);
4496 /*************************************************************************
4497 * TREEVIEW_ProcessLetterKeys
4499 * Processes keyboard messages generated by pressing the letter keys
4501 * What this does is perform a case insensitive search from the
4502 * current position with the following quirks:
4503 * - If two chars or more are pressed in quick succession we search
4504 * for the corresponding string (e.g. 'abc').
4505 * - If there is a delay we wipe away the current search string and
4506 * restart with just that char.
4507 * - If the user keeps pressing the same character, whether slowly or
4508 * fast, so that the search string is entirely composed of this
4509 * character ('aaaaa' for instance), then we search for first item
4510 * that starting with that character.
4511 * - If the user types the above character in quick succession, then
4512 * we must also search for the corresponding string ('aaaaa'), and
4513 * go to that string if there is a match.
4521 * - The current implementation has a list of characters it will
4522 * accept and it ignores everything else. In particular it will
4523 * ignore accentuated characters which seems to match what
4524 * Windows does. But I'm not sure it makes sense to follow
4526 * - We don't sound a beep when the search fails.
4527 * - The search should start from the focused item, not from the selected
4528 * item. One reason for this is to allow for multiple selections in trees.
4529 * But currently infoPtr->focusedItem does not seem very usable.
4533 * TREEVIEW_ProcessLetterKeys
4535 static INT
TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO
*infoPtr
, WPARAM charCode
, LPARAM keyData
)
4538 HTREEITEM endidx
,idx
;
4540 WCHAR buffer
[MAX_PATH
];
4541 DWORD timestamp
,elapsed
;
4543 /* simple parameter checking */
4544 if (!charCode
|| !keyData
) return 0;
4546 /* only allow the valid WM_CHARs through */
4547 if (!isalnum(charCode
) &&
4548 charCode
!= '.' && charCode
!= '`' && charCode
!= '!' &&
4549 charCode
!= '@' && charCode
!= '#' && charCode
!= '$' &&
4550 charCode
!= '%' && charCode
!= '^' && charCode
!= '&' &&
4551 charCode
!= '*' && charCode
!= '(' && charCode
!= ')' &&
4552 charCode
!= '-' && charCode
!= '_' && charCode
!= '+' &&
4553 charCode
!= '=' && charCode
!= '\\'&& charCode
!= ']' &&
4554 charCode
!= '}' && charCode
!= '[' && charCode
!= '{' &&
4555 charCode
!= '/' && charCode
!= '?' && charCode
!= '>' &&
4556 charCode
!= '<' && charCode
!= ',' && charCode
!= '~')
4559 /* compute how much time elapsed since last keypress */
4560 timestamp
= GetTickCount();
4561 if (timestamp
> infoPtr
->lastKeyPressTimestamp
) {
4562 elapsed
=timestamp
-infoPtr
->lastKeyPressTimestamp
;
4564 elapsed
=infoPtr
->lastKeyPressTimestamp
-timestamp
;
4567 /* update the search parameters */
4568 infoPtr
->lastKeyPressTimestamp
=timestamp
;
4569 if (elapsed
< KEY_DELAY
) {
4570 if (infoPtr
->nSearchParamLength
< sizeof(infoPtr
->szSearchParam
) / sizeof(WCHAR
)) {
4571 infoPtr
->szSearchParam
[infoPtr
->nSearchParamLength
++]=charCode
;
4573 if (infoPtr
->charCode
!= charCode
) {
4574 infoPtr
->charCode
=charCode
=0;
4577 infoPtr
->charCode
=charCode
;
4578 infoPtr
->szSearchParam
[0]=charCode
;
4579 infoPtr
->nSearchParamLength
=1;
4580 /* Redundant with the 1 char string */
4584 /* and search from the current position */
4586 if (infoPtr
->selectedItem
!= NULL
) {
4587 endidx
=infoPtr
->selectedItem
;
4588 /* if looking for single character match,
4589 * then we must always move forward
4591 if (infoPtr
->nSearchParamLength
== 1)
4592 idx
=TREEVIEW_GetNextListItem(infoPtr
,endidx
);
4597 idx
=infoPtr
->root
->firstChild
;
4600 /* At the end point, sort out wrapping */
4603 /* If endidx is null, stop at the last item (ie top to bottom) */
4607 /* Otherwise, start again at the very beginning */
4608 idx
=infoPtr
->root
->firstChild
;
4610 /* But if we are stopping on the first child, end now! */
4611 if (idx
== endidx
) break;
4615 ZeroMemory(&item
, sizeof(item
));
4616 item
.mask
= TVIF_TEXT
;
4618 item
.pszText
= buffer
;
4619 item
.cchTextMax
= sizeof(buffer
);
4620 TREEVIEW_GetItemT( infoPtr
, &item
, TRUE
);
4622 /* check for a match */
4623 if (strncmpiW(item
.pszText
,infoPtr
->szSearchParam
,infoPtr
->nSearchParamLength
) == 0) {
4626 } else if ( (charCode
!= 0) && (nItem
== NULL
) &&
4627 (nItem
!= infoPtr
->selectedItem
) &&
4628 (strncmpiW(item
.pszText
,infoPtr
->szSearchParam
,1) == 0) ) {
4629 /* This would work but we must keep looking for a longer match */
4632 idx
=TREEVIEW_GetNextListItem(infoPtr
,idx
);
4633 } while (idx
!= endidx
);
4635 if (nItem
!= NULL
) {
4636 if (TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, nItem
, TVC_BYKEYBOARD
)) {
4637 TREEVIEW_EnsureVisible(infoPtr
, nItem
, FALSE
);
4644 /* Scrolling ************************************************************/
4647 TREEVIEW_EnsureVisible(TREEVIEW_INFO
*infoPtr
, HTREEITEM item
, BOOL bHScroll
)
4650 BOOL hasFirstVisible
= infoPtr
->firstVisible
!= NULL
;
4651 HTREEITEM newFirstVisible
= NULL
;
4652 int visible_pos
= -1;
4654 if (!TREEVIEW_ValidItem(infoPtr
, item
))
4657 if (!ISVISIBLE(item
))
4659 /* Expand parents as necessary. */
4662 /* see if we are trying to ensure that root is visible */
4663 if((item
!= infoPtr
->root
) && TREEVIEW_ValidItem(infoPtr
, item
))
4664 parent
= item
->parent
;
4666 parent
= item
; /* this item is the topmost item */
4668 while (parent
!= infoPtr
->root
)
4670 if (!(parent
->state
& TVIS_EXPANDED
))
4671 TREEVIEW_Expand(infoPtr
, parent
, FALSE
, TRUE
);
4673 parent
= parent
->parent
;
4677 viscount
= TREEVIEW_GetVisibleCount(infoPtr
);
4679 TRACE("%p (%s) %d - %d viscount(%d)\n", item
, TREEVIEW_ItemName(item
), item
->visibleOrder
,
4680 hasFirstVisible
? infoPtr
->firstVisible
->visibleOrder
: -1, viscount
);
4682 if (hasFirstVisible
)
4683 visible_pos
= item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
;
4685 if (visible_pos
< 0)
4687 /* item is before the start of the list: put it at the top. */
4688 newFirstVisible
= item
;
4690 else if (visible_pos
>= viscount
4691 /* Sometimes, before we are displayed, GVC is 0, causing us to
4692 * spuriously scroll up. */
4693 && visible_pos
> 0 && !(infoPtr
->dwStyle
& TVS_NOSCROLL
) )
4695 /* item is past the end of the list. */
4696 int scroll
= visible_pos
- viscount
;
4698 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, infoPtr
->firstVisible
,
4704 /* Scroll window so item's text is visible as much as possible */
4705 /* Calculation of amount of extra space is taken from EditLabel code */
4707 TEXTMETRICW textMetric
;
4708 HDC hdc
= GetWindowDC(infoPtr
->hwnd
);
4710 x
= item
->textWidth
;
4712 GetTextMetricsW(hdc
, &textMetric
);
4713 ReleaseDC(infoPtr
->hwnd
, hdc
);
4715 x
+= (textMetric
.tmMaxCharWidth
* 2);
4716 x
= max(x
, textMetric
.tmMaxCharWidth
* 3);
4718 if (item
->textOffset
< 0)
4719 pos
= item
->textOffset
;
4720 else if (item
->textOffset
+ x
> infoPtr
->clientWidth
)
4722 if (x
> infoPtr
->clientWidth
)
4723 pos
= item
->textOffset
;
4725 pos
= item
->textOffset
+ x
- infoPtr
->clientWidth
;
4730 TREEVIEW_HScroll(infoPtr
, MAKEWPARAM(SB_THUMBPOSITION
, infoPtr
->scrollX
+ pos
));
4733 if (newFirstVisible
!= NULL
&& newFirstVisible
!= infoPtr
->firstVisible
)
4735 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
4744 TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*infoPtr
,
4745 TREEVIEW_ITEM
*newFirstVisible
,
4746 BOOL bUpdateScrollPos
)
4750 TRACE("%p: %s\n", newFirstVisible
, TREEVIEW_ItemName(newFirstVisible
));
4752 if (newFirstVisible
!= NULL
)
4754 /* Prevent an empty gap from appearing at the bottom... */
4755 gap_size
= TREEVIEW_GetVisibleCount(infoPtr
)
4756 - infoPtr
->maxVisibleOrder
+ newFirstVisible
->visibleOrder
;
4760 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, newFirstVisible
,
4763 /* ... unless we just don't have enough items. */
4764 if (newFirstVisible
== NULL
)
4765 newFirstVisible
= infoPtr
->root
->firstChild
;
4769 if (infoPtr
->firstVisible
!= newFirstVisible
)
4771 if (infoPtr
->firstVisible
== NULL
|| newFirstVisible
== NULL
)
4773 infoPtr
->firstVisible
= newFirstVisible
;
4774 TREEVIEW_Invalidate(infoPtr
, NULL
);
4778 TREEVIEW_ITEM
*item
;
4779 int scroll
= infoPtr
->uItemHeight
*
4780 (infoPtr
->firstVisible
->visibleOrder
4781 - newFirstVisible
->visibleOrder
);
4783 infoPtr
->firstVisible
= newFirstVisible
;
4785 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
4786 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
4788 item
->rect
.top
+= scroll
;
4789 item
->rect
.bottom
+= scroll
;
4792 if (bUpdateScrollPos
)
4793 SetScrollPos(infoPtr
->hwnd
, SB_VERT
,
4794 newFirstVisible
->visibleOrder
, TRUE
);
4796 ScrollWindowEx(infoPtr
->hwnd
, 0, scroll
, NULL
, NULL
, NULL
, NULL
, SW_ERASE
| SW_INVALIDATE
);
4801 /************************************************************************
4802 * VScroll is always in units of visible items. i.e. we always have a
4803 * visible item aligned to the top of the control. (Unless we have no
4807 TREEVIEW_VScroll(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4809 TREEVIEW_ITEM
*oldFirstVisible
= infoPtr
->firstVisible
;
4810 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
4812 int nScrollCode
= LOWORD(wParam
);
4814 TRACE("wp %lx\n", wParam
);
4816 if (!(infoPtr
->uInternalStatus
& TV_VSCROLL
))
4819 if (!oldFirstVisible
)
4821 assert(infoPtr
->root
->firstChild
== NULL
);
4825 switch (nScrollCode
)
4828 newFirstVisible
= infoPtr
->root
->firstChild
;
4832 newFirstVisible
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
4836 newFirstVisible
= TREEVIEW_GetPrevListItem(infoPtr
, oldFirstVisible
);
4840 newFirstVisible
= TREEVIEW_GetNextListItem(infoPtr
, oldFirstVisible
);
4844 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, oldFirstVisible
,
4845 -max(1, TREEVIEW_GetVisibleCount(infoPtr
)));
4849 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, oldFirstVisible
,
4850 max(1, TREEVIEW_GetVisibleCount(infoPtr
)));
4854 case SB_THUMBPOSITION
:
4855 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
,
4856 infoPtr
->root
->firstChild
,
4857 (LONG
)(SHORT
)HIWORD(wParam
));
4864 if (newFirstVisible
!= NULL
)
4866 if (newFirstVisible
!= oldFirstVisible
)
4867 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
,
4868 nScrollCode
!= SB_THUMBTRACK
);
4869 else if (nScrollCode
== SB_THUMBPOSITION
)
4870 SetScrollPos(infoPtr
->hwnd
, SB_VERT
,
4871 newFirstVisible
->visibleOrder
, TRUE
);
4878 TREEVIEW_HScroll(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4881 int scrollX
= infoPtr
->scrollX
;
4882 int nScrollCode
= LOWORD(wParam
);
4884 TRACE("wp %lx\n", wParam
);
4886 if (!(infoPtr
->uInternalStatus
& TV_HSCROLL
))
4889 maxWidth
= infoPtr
->treeWidth
- infoPtr
->clientWidth
;
4890 /* shall never occur */
4897 switch (nScrollCode
)
4900 scrollX
-= infoPtr
->uItemHeight
;
4903 scrollX
+= infoPtr
->uItemHeight
;
4906 scrollX
-= infoPtr
->clientWidth
;
4909 scrollX
+= infoPtr
->clientWidth
;
4913 case SB_THUMBPOSITION
:
4914 scrollX
= (int)(SHORT
)HIWORD(wParam
);
4921 if (scrollX
> maxWidth
)
4923 else if (scrollX
< 0)
4927 if (scrollX
!= infoPtr
->scrollX
)
4929 TREEVIEW_ITEM
*item
;
4930 LONG scroll_pixels
= infoPtr
->scrollX
- scrollX
;
4932 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
4933 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
4935 item
->linesOffset
+= scroll_pixels
;
4936 item
->stateOffset
+= scroll_pixels
;
4937 item
->imageOffset
+= scroll_pixels
;
4938 item
->textOffset
+= scroll_pixels
;
4941 ScrollWindow(infoPtr
->hwnd
, scroll_pixels
, 0, NULL
, NULL
);
4942 infoPtr
->scrollX
= scrollX
;
4943 UpdateWindow(infoPtr
->hwnd
);
4946 if (nScrollCode
!= SB_THUMBTRACK
)
4947 SetScrollPos(infoPtr
->hwnd
, SB_HORZ
, scrollX
, TRUE
);
4953 TREEVIEW_MouseWheel(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
4956 UINT pulScrollLines
= 3;
4958 if (wParam
& (MK_SHIFT
| MK_CONTROL
))
4959 return DefWindowProcW(infoPtr
->hwnd
, WM_MOUSEWHEEL
, wParam
, lParam
);
4961 if (infoPtr
->firstVisible
== NULL
)
4964 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
, 0, &pulScrollLines
, 0);
4966 wheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4967 /* if scrolling changes direction, ignore left overs */
4968 if ((wheelDelta
< 0 && infoPtr
->wheelRemainder
< 0) ||
4969 (wheelDelta
> 0 && infoPtr
->wheelRemainder
> 0))
4970 infoPtr
->wheelRemainder
+= wheelDelta
;
4972 infoPtr
->wheelRemainder
= wheelDelta
;
4974 if (infoPtr
->wheelRemainder
&& pulScrollLines
)
4980 lineScroll
= pulScrollLines
* (float)infoPtr
->wheelRemainder
/ WHEEL_DELTA
;
4981 infoPtr
->wheelRemainder
-= WHEEL_DELTA
* lineScroll
/ (int)pulScrollLines
;
4983 newDy
= infoPtr
->firstVisible
->visibleOrder
- lineScroll
;
4984 maxDy
= infoPtr
->maxVisibleOrder
;
4992 TREEVIEW_VScroll(infoPtr
, MAKEWPARAM(SB_THUMBPOSITION
, newDy
));
4997 /* Create/Destroy *******************************************************/
5000 TREEVIEW_Create(HWND hwnd
, const CREATESTRUCTW
*lpcs
)
5003 TREEVIEW_INFO
*infoPtr
;
5006 TRACE("wnd %p, style 0x%x\n", hwnd
, GetWindowLongW(hwnd
, GWL_STYLE
));
5008 infoPtr
= Alloc(sizeof(TREEVIEW_INFO
));
5010 if (infoPtr
== NULL
)
5012 ERR("could not allocate info memory!\n");
5016 SetWindowLongPtrW(hwnd
, 0, (DWORD_PTR
)infoPtr
);
5018 infoPtr
->hwnd
= hwnd
;
5019 infoPtr
->dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
5021 infoPtr
->uNumItems
= 0;
5022 infoPtr
->cdmode
= 0;
5023 infoPtr
->uScrollTime
= 300; /* milliseconds */
5024 infoPtr
->bRedraw
= TRUE
;
5026 GetClientRect(hwnd
, &rcClient
);
5028 /* No scroll bars yet. */
5029 infoPtr
->clientWidth
= rcClient
.right
;
5030 infoPtr
->clientHeight
= rcClient
.bottom
;
5031 infoPtr
->uInternalStatus
= 0;
5033 infoPtr
->treeWidth
= 0;
5034 infoPtr
->treeHeight
= 0;
5036 infoPtr
->uIndent
= MINIMUM_INDENT
;
5037 infoPtr
->selectedItem
= NULL
;
5038 infoPtr
->focusedItem
= NULL
;
5039 infoPtr
->hotItem
= NULL
;
5040 infoPtr
->editItem
= NULL
;
5041 infoPtr
->firstVisible
= NULL
;
5042 infoPtr
->maxVisibleOrder
= 0;
5043 infoPtr
->dropItem
= NULL
;
5044 infoPtr
->insertMarkItem
= NULL
;
5045 infoPtr
->insertBeforeorAfter
= 0;
5048 infoPtr
->scrollX
= 0;
5049 infoPtr
->wheelRemainder
= 0;
5051 infoPtr
->clrBk
= CLR_NONE
; /* use system color */
5052 infoPtr
->clrText
= CLR_NONE
; /* use system color */
5053 infoPtr
->clrLine
= CLR_DEFAULT
;
5054 infoPtr
->clrInsertMark
= CLR_DEFAULT
;
5058 infoPtr
->hwndEdit
= NULL
;
5059 infoPtr
->wpEditOrig
= NULL
;
5060 infoPtr
->bIgnoreEditKillFocus
= FALSE
;
5061 infoPtr
->bLabelChanged
= FALSE
;
5063 infoPtr
->himlNormal
= NULL
;
5064 infoPtr
->himlState
= NULL
;
5065 infoPtr
->normalImageWidth
= 0;
5066 infoPtr
->normalImageHeight
= 0;
5067 infoPtr
->stateImageWidth
= 0;
5068 infoPtr
->stateImageHeight
= 0;
5070 infoPtr
->items
= DPA_Create(16);
5072 SystemParametersInfoW(SPI_GETICONTITLELOGFONT
, sizeof(lf
), &lf
, 0);
5073 infoPtr
->hFont
= infoPtr
->hDefaultFont
= CreateFontIndirectW(&lf
);
5074 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
5075 infoPtr
->hUnderlineFont
= TREEVIEW_CreateUnderlineFont(infoPtr
->hFont
);
5076 infoPtr
->hBoldUnderlineFont
= TREEVIEW_CreateBoldUnderlineFont(infoPtr
->hFont
);
5077 infoPtr
->hcurHand
= LoadCursorW(NULL
, (LPWSTR
)IDC_HAND
);
5079 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
5081 infoPtr
->root
= TREEVIEW_AllocateItem(infoPtr
);
5082 infoPtr
->root
->state
= TVIS_EXPANDED
;
5083 infoPtr
->root
->iLevel
= -1;
5084 infoPtr
->root
->visibleOrder
= -1;
5086 infoPtr
->hwndNotify
= lpcs
->hwndParent
;
5087 infoPtr
->hwndToolTip
= 0;
5089 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5090 TREEVIEW_NotifyFormat(infoPtr
, infoPtr
->hwndNotify
, NF_REQUERY
);
5092 if (!(infoPtr
->dwStyle
& TVS_NOTOOLTIPS
))
5093 infoPtr
->hwndToolTip
= CreateWindowExW(0, TOOLTIPS_CLASSW
, NULL
, WS_POPUP
,
5094 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
5097 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5098 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
5099 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
5101 OpenThemeData (hwnd
, themeClass
);
5108 TREEVIEW_Destroy(TREEVIEW_INFO
*infoPtr
)
5112 /* free item data */
5113 TREEVIEW_RemoveTree(infoPtr
);
5114 /* root isn't freed with other items */
5115 TREEVIEW_FreeItem(infoPtr
, infoPtr
->root
);
5116 DPA_Destroy(infoPtr
->items
);
5118 /* tool tip is automatically destroyed: we are its owner */
5120 /* Restore original wndproc */
5121 if (infoPtr
->hwndEdit
)
5122 SetWindowLongPtrW(infoPtr
->hwndEdit
, GWLP_WNDPROC
,
5123 (DWORD_PTR
)infoPtr
->wpEditOrig
);
5125 CloseThemeData (GetWindowTheme (infoPtr
->hwnd
));
5127 /* Deassociate treeview from the window before doing anything drastic. */
5128 SetWindowLongPtrW(infoPtr
->hwnd
, 0, 0);
5130 DeleteObject(infoPtr
->hDefaultFont
);
5131 DeleteObject(infoPtr
->hBoldFont
);
5132 DeleteObject(infoPtr
->hUnderlineFont
);
5133 DeleteObject(infoPtr
->hBoldUnderlineFont
);
5139 /* Miscellaneous Messages ***********************************************/
5142 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO
*infoPtr
, WPARAM key
)
5150 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5151 SCROLL_ENTRY(SB_VERT
, SB_PAGEUP
), /* VK_PRIOR */
5152 SCROLL_ENTRY(SB_VERT
, SB_PAGEDOWN
), /* VK_NEXT */
5153 SCROLL_ENTRY(SB_VERT
, SB_BOTTOM
), /* VK_END */
5154 SCROLL_ENTRY(SB_VERT
, SB_TOP
), /* VK_HOME */
5155 SCROLL_ENTRY(SB_HORZ
, SB_LINEUP
), /* VK_LEFT */
5156 SCROLL_ENTRY(SB_VERT
, SB_LINEUP
), /* VK_UP */
5157 SCROLL_ENTRY(SB_HORZ
, SB_LINEDOWN
), /* VK_RIGHT */
5158 SCROLL_ENTRY(SB_VERT
, SB_LINEDOWN
) /* VK_DOWN */
5162 if (key
>= VK_PRIOR
&& key
<= VK_DOWN
)
5164 unsigned char code
= scroll
[key
- VK_PRIOR
].code
;
5166 (((code
& (1 << 7)) == (SB_HORZ
<< 7))
5168 : TREEVIEW_VScroll
)(infoPtr
, code
& 0x7F);
5174 /************************************************************************
5177 * VK_UP Move selection to the previous non-hidden item.
5178 * VK_DOWN Move selection to the next non-hidden item.
5179 * VK_HOME Move selection to the first item.
5180 * VK_END Move selection to the last item.
5181 * VK_LEFT If expanded then collapse, otherwise move to parent.
5182 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5184 * VK_SUBTRACT Collapse.
5185 * VK_MULTIPLY Expand all.
5186 * VK_PRIOR Move up GetVisibleCount items.
5187 * VK_NEXT Move down GetVisibleCount items.
5188 * VK_BACK Move to parent.
5189 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5192 TREEVIEW_KeyDown(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
5194 /* If it is non-NULL and different, it will be selected and visible. */
5195 TREEVIEW_ITEM
*newSelection
= NULL
;
5196 TREEVIEW_ITEM
*prevItem
= infoPtr
->selectedItem
;
5197 NMTVKEYDOWN nmkeydown
;
5199 TRACE("%lx\n", wParam
);
5201 nmkeydown
.wVKey
= wParam
;
5202 nmkeydown
.flags
= 0;
5203 TREEVIEW_SendRealNotify(infoPtr
, TVN_KEYDOWN
, &nmkeydown
.hdr
);
5205 if (prevItem
== NULL
)
5208 if (GetAsyncKeyState(VK_CONTROL
) & 0x8000)
5209 return TREEVIEW_ScrollKeyDown(infoPtr
, wParam
);
5214 newSelection
= TREEVIEW_GetPrevListItem(infoPtr
, prevItem
);
5216 newSelection
= infoPtr
->root
->firstChild
;
5220 newSelection
= TREEVIEW_GetNextListItem(infoPtr
, prevItem
);
5224 TREEVIEW_SendSimpleNotify(infoPtr
, NM_RETURN
);
5228 newSelection
= infoPtr
->root
->firstChild
;
5232 newSelection
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
5236 if (prevItem
->state
& TVIS_EXPANDED
)
5238 TREEVIEW_Collapse(infoPtr
, prevItem
, FALSE
, TRUE
);
5240 else if (prevItem
->parent
!= infoPtr
->root
)
5242 newSelection
= prevItem
->parent
;
5247 if (TREEVIEW_HasChildren(infoPtr
, prevItem
))
5249 if (!(prevItem
->state
& TVIS_EXPANDED
))
5250 TREEVIEW_Expand(infoPtr
, prevItem
, FALSE
, TRUE
);
5253 newSelection
= prevItem
->firstChild
;
5260 TREEVIEW_ExpandAll(infoPtr
, prevItem
);
5264 TREEVIEW_Expand(infoPtr
, prevItem
, FALSE
, TRUE
);
5268 TREEVIEW_Collapse(infoPtr
, prevItem
, FALSE
, TRUE
);
5273 = TREEVIEW_GetListItem(infoPtr
, prevItem
,
5274 -TREEVIEW_GetVisibleCount(infoPtr
));
5279 = TREEVIEW_GetListItem(infoPtr
, prevItem
,
5280 TREEVIEW_GetVisibleCount(infoPtr
));
5284 newSelection
= prevItem
->parent
;
5285 if (newSelection
== infoPtr
->root
)
5286 newSelection
= NULL
;
5290 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
5291 TREEVIEW_ToggleItemState(infoPtr
, prevItem
);
5295 if (newSelection
&& newSelection
!= prevItem
)
5297 if (TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
,
5300 TREEVIEW_EnsureVisible(infoPtr
, newSelection
, FALSE
);
5308 TREEVIEW_MouseLeave (TREEVIEW_INFO
* infoPtr
)
5310 /* remove hot effect from item */
5311 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->hotItem
);
5312 infoPtr
->hotItem
= NULL
;
5318 TREEVIEW_MouseMove (TREEVIEW_INFO
* infoPtr
, LPARAM lParam
)
5321 TRACKMOUSEEVENT trackinfo
;
5322 TREEVIEW_ITEM
* item
;
5324 if (!(infoPtr
->dwStyle
& TVS_TRACKSELECT
)) return 0;
5326 /* fill in the TRACKMOUSEEVENT struct */
5327 trackinfo
.cbSize
= sizeof(TRACKMOUSEEVENT
);
5328 trackinfo
.dwFlags
= TME_QUERY
;
5329 trackinfo
.hwndTrack
= infoPtr
->hwnd
;
5331 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5332 _TrackMouseEvent(&trackinfo
);
5334 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5335 if(!(trackinfo
.dwFlags
& TME_LEAVE
))
5337 trackinfo
.dwFlags
= TME_LEAVE
; /* notify upon leaving */
5338 trackinfo
.hwndTrack
= infoPtr
->hwnd
;
5339 /* do it as fast as possible, minimal systimer latency will be used */
5340 trackinfo
.dwHoverTime
= 1;
5342 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5343 /* and can properly deactivate the hot item */
5344 _TrackMouseEvent(&trackinfo
);
5347 pt
.x
= (short)LOWORD(lParam
);
5348 pt
.y
= (short)HIWORD(lParam
);
5350 item
= TREEVIEW_HitTestPoint(infoPtr
, pt
);
5352 if (item
!= infoPtr
->hotItem
)
5354 /* redraw old hot item */
5355 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->hotItem
);
5356 infoPtr
->hotItem
= item
;
5357 /* redraw new hot item */
5358 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->hotItem
);
5364 /* Draw themed border */
5365 static BOOL
TREEVIEW_NCPaint (const TREEVIEW_INFO
*infoPtr
, HRGN region
, LPARAM lParam
)
5367 HTHEME theme
= GetWindowTheme (infoPtr
->hwnd
);
5371 int cxEdge
= GetSystemMetrics (SM_CXEDGE
),
5372 cyEdge
= GetSystemMetrics (SM_CYEDGE
);
5375 return DefWindowProcW (infoPtr
->hwnd
, WM_NCPAINT
, (WPARAM
)region
, lParam
);
5377 GetWindowRect(infoPtr
->hwnd
, &r
);
5379 cliprgn
= CreateRectRgn (r
.left
+ cxEdge
, r
.top
+ cyEdge
,
5380 r
.right
- cxEdge
, r
.bottom
- cyEdge
);
5381 if (region
!= (HRGN
)1)
5382 CombineRgn (cliprgn
, cliprgn
, region
, RGN_AND
);
5383 OffsetRect(&r
, -r
.left
, -r
.top
);
5385 dc
= GetDCEx(infoPtr
->hwnd
, region
, DCX_WINDOW
|DCX_INTERSECTRGN
);
5386 OffsetRect(&r
, -r
.left
, -r
.top
);
5388 if (IsThemeBackgroundPartiallyTransparent (theme
, 0, 0))
5389 DrawThemeParentBackground(infoPtr
->hwnd
, dc
, &r
);
5390 DrawThemeBackground (theme
, dc
, 0, 0, &r
, 0);
5391 ReleaseDC(infoPtr
->hwnd
, dc
);
5393 /* Call default proc to get the scrollbars etc. painted */
5394 DefWindowProcW (infoPtr
->hwnd
, WM_NCPAINT
, (WPARAM
)cliprgn
, 0);
5400 TREEVIEW_Notify(const TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5402 LPNMHDR lpnmh
= (LPNMHDR
)lParam
;
5404 if (lpnmh
->code
== PGN_CALCSIZE
) {
5405 LPNMPGCALCSIZE lppgc
= (LPNMPGCALCSIZE
)lParam
;
5407 if (lppgc
->dwFlag
== PGF_CALCWIDTH
) {
5408 lppgc
->iWidth
= infoPtr
->treeWidth
;
5409 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5410 infoPtr
->treeWidth
, infoPtr
->clientWidth
);
5413 lppgc
->iHeight
= infoPtr
->treeHeight
;
5414 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5415 infoPtr
->treeHeight
, infoPtr
->clientHeight
);
5419 return DefWindowProcW(infoPtr
->hwnd
, WM_NOTIFY
, wParam
, lParam
);
5423 TREEVIEW_Size(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5425 if (wParam
== SIZE_RESTORED
)
5427 infoPtr
->clientWidth
= (short)LOWORD(lParam
);
5428 infoPtr
->clientHeight
= (short)HIWORD(lParam
);
5430 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
5431 TREEVIEW_SetFirstVisible(infoPtr
, infoPtr
->firstVisible
, TRUE
);
5432 TREEVIEW_UpdateScrollBars(infoPtr
);
5436 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam
, lParam
);
5439 TREEVIEW_Invalidate(infoPtr
, NULL
);
5444 TREEVIEW_StyleChanged(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5446 TRACE("(%lx %lx)\n", wParam
, lParam
);
5448 if (wParam
== GWL_STYLE
)
5450 DWORD dwNewStyle
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
5452 if ((infoPtr
->dwStyle
^ dwNewStyle
) & TVS_CHECKBOXES
)
5454 if (dwNewStyle
& TVS_CHECKBOXES
)
5456 TREEVIEW_InitCheckboxes(infoPtr
);
5457 TRACE("checkboxes enabled\n");
5459 /* set all items to state image index 1 */
5460 TREEVIEW_ResetImageStateIndex(infoPtr
, infoPtr
->root
);
5464 FIXME("tried to disable checkboxes\n");
5468 if ((infoPtr
->dwStyle
^ dwNewStyle
) & TVS_NOTOOLTIPS
)
5470 if (infoPtr
->dwStyle
& TVS_NOTOOLTIPS
)
5472 infoPtr
->hwndToolTip
= COMCTL32_CreateToolTip(infoPtr
->hwnd
);
5473 TRACE("tooltips enabled\n");
5477 DestroyWindow(infoPtr
->hwndToolTip
);
5478 infoPtr
->hwndToolTip
= 0;
5479 TRACE("tooltips disabled\n");
5483 infoPtr
->dwStyle
= dwNewStyle
;
5486 TREEVIEW_EndEditLabelNow(infoPtr
, TRUE
);
5487 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
5488 TREEVIEW_UpdateScrollBars(infoPtr
);
5489 TREEVIEW_Invalidate(infoPtr
, NULL
);
5495 TREEVIEW_SetCursor(const TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5498 TREEVIEW_ITEM
* item
;
5502 ScreenToClient(infoPtr
->hwnd
, &pt
);
5504 item
= TREEVIEW_HitTestPoint(infoPtr
, pt
);
5506 memset(&nmmouse
, 0, sizeof(nmmouse
));
5509 nmmouse
.dwItemSpec
= (DWORD_PTR
)item
;
5510 nmmouse
.dwItemData
= item
->lParam
;
5514 nmmouse
.dwHitInfo
= lParam
;
5515 if (TREEVIEW_SendRealNotify(infoPtr
, NM_SETCURSOR
, &nmmouse
.hdr
))
5518 if (item
&& (infoPtr
->dwStyle
& TVS_TRACKSELECT
))
5520 SetCursor(infoPtr
->hcurHand
);
5524 return DefWindowProcW(infoPtr
->hwnd
, WM_SETCURSOR
, wParam
, lParam
);
5528 TREEVIEW_SetFocus(TREEVIEW_INFO
*infoPtr
)
5532 if (!infoPtr
->selectedItem
)
5534 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, infoPtr
->firstVisible
,
5538 TREEVIEW_Invalidate(infoPtr
, infoPtr
->selectedItem
);
5539 TREEVIEW_SendSimpleNotify(infoPtr
, NM_SETFOCUS
);
5544 TREEVIEW_KillFocus(const TREEVIEW_INFO
*infoPtr
)
5548 TREEVIEW_Invalidate(infoPtr
, infoPtr
->selectedItem
);
5549 UpdateWindow(infoPtr
->hwnd
);
5550 TREEVIEW_SendSimpleNotify(infoPtr
, NM_KILLFOCUS
);
5554 /* update theme after a WM_THEMECHANGED message */
5555 static LRESULT
TREEVIEW_ThemeChanged(const TREEVIEW_INFO
*infoPtr
)
5557 HTHEME theme
= GetWindowTheme (infoPtr
->hwnd
);
5558 CloseThemeData (theme
);
5559 OpenThemeData (infoPtr
->hwnd
, themeClass
);
5564 static LRESULT WINAPI
5565 TREEVIEW_WindowProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
5567 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(hwnd
);
5569 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd
, uMsg
, wParam
, lParam
);
5571 if (infoPtr
) TREEVIEW_VerifyTree(infoPtr
);
5574 if (uMsg
== WM_CREATE
)
5575 TREEVIEW_Create(hwnd
, (LPCREATESTRUCTW
)lParam
);
5582 case TVM_CREATEDRAGIMAGE
:
5583 return TREEVIEW_CreateDragImage(infoPtr
, lParam
);
5585 case TVM_DELETEITEM
:
5586 return TREEVIEW_DeleteItem(infoPtr
, (HTREEITEM
)lParam
);
5588 case TVM_EDITLABELA
:
5589 case TVM_EDITLABELW
:
5590 return (LRESULT
)TREEVIEW_EditLabel(infoPtr
, (HTREEITEM
)lParam
);
5592 case TVM_ENDEDITLABELNOW
:
5593 return TREEVIEW_EndEditLabelNow(infoPtr
, (BOOL
)wParam
);
5595 case TVM_ENSUREVISIBLE
:
5596 return TREEVIEW_EnsureVisible(infoPtr
, (HTREEITEM
)lParam
, TRUE
);
5599 return TREEVIEW_ExpandMsg(infoPtr
, (UINT
)wParam
, (HTREEITEM
)lParam
);
5601 case TVM_GETBKCOLOR
:
5602 return TREEVIEW_GetBkColor(infoPtr
);
5605 return TREEVIEW_GetCount(infoPtr
);
5607 case TVM_GETEDITCONTROL
:
5608 return TREEVIEW_GetEditControl(infoPtr
);
5610 case TVM_GETIMAGELIST
:
5611 return TREEVIEW_GetImageList(infoPtr
, wParam
);
5614 return TREEVIEW_GetIndent(infoPtr
);
5616 case TVM_GETINSERTMARKCOLOR
:
5617 return TREEVIEW_GetInsertMarkColor(infoPtr
);
5619 case TVM_GETISEARCHSTRINGA
:
5620 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5623 case TVM_GETISEARCHSTRINGW
:
5624 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5629 return TREEVIEW_GetItemT(infoPtr
, (LPTVITEMEXW
)lParam
,
5630 uMsg
== TVM_GETITEMW
);
5631 case TVM_GETITEMHEIGHT
:
5632 return TREEVIEW_GetItemHeight(infoPtr
);
5634 case TVM_GETITEMRECT
:
5635 return TREEVIEW_GetItemRect(infoPtr
, (BOOL
)wParam
, (LPRECT
)lParam
);
5637 case TVM_GETITEMSTATE
:
5638 return TREEVIEW_GetItemState(infoPtr
, (HTREEITEM
)wParam
, (UINT
)lParam
);
5640 case TVM_GETLINECOLOR
:
5641 return TREEVIEW_GetLineColor(infoPtr
);
5643 case TVM_GETNEXTITEM
:
5644 return TREEVIEW_GetNextItem(infoPtr
, (UINT
)wParam
, (HTREEITEM
)lParam
);
5646 case TVM_GETSCROLLTIME
:
5647 return TREEVIEW_GetScrollTime(infoPtr
);
5649 case TVM_GETTEXTCOLOR
:
5650 return TREEVIEW_GetTextColor(infoPtr
);
5652 case TVM_GETTOOLTIPS
:
5653 return TREEVIEW_GetToolTips(infoPtr
);
5655 case TVM_GETUNICODEFORMAT
:
5656 return TREEVIEW_GetUnicodeFormat(infoPtr
);
5658 case TVM_GETVISIBLECOUNT
:
5659 return TREEVIEW_GetVisibleCount(infoPtr
);
5662 return TREEVIEW_HitTest(infoPtr
, (LPTVHITTESTINFO
)lParam
);
5664 case TVM_INSERTITEMA
:
5665 case TVM_INSERTITEMW
:
5666 return TREEVIEW_InsertItemT(infoPtr
, (LPTVINSERTSTRUCTW
)lParam
,
5667 uMsg
== TVM_INSERTITEMW
);
5668 case TVM_SELECTITEM
:
5669 return TREEVIEW_SelectItem(infoPtr
, (INT
)wParam
, (HTREEITEM
)lParam
);
5671 case TVM_SETBKCOLOR
:
5672 return TREEVIEW_SetBkColor(infoPtr
, (COLORREF
)lParam
);
5674 case TVM_SETIMAGELIST
:
5675 return TREEVIEW_SetImageList(infoPtr
, wParam
, (HIMAGELIST
)lParam
);
5678 return TREEVIEW_SetIndent(infoPtr
, (UINT
)wParam
);
5680 case TVM_SETINSERTMARK
:
5681 return TREEVIEW_SetInsertMark(infoPtr
, (BOOL
)wParam
, (HTREEITEM
)lParam
);
5683 case TVM_SETINSERTMARKCOLOR
:
5684 return TREEVIEW_SetInsertMarkColor(infoPtr
, (COLORREF
)lParam
);
5688 return TREEVIEW_SetItemT(infoPtr
, (LPTVITEMEXW
)lParam
,
5689 uMsg
== TVM_SETITEMW
);
5690 case TVM_SETLINECOLOR
:
5691 return TREEVIEW_SetLineColor(infoPtr
, (COLORREF
)lParam
);
5693 case TVM_SETITEMHEIGHT
:
5694 return TREEVIEW_SetItemHeight(infoPtr
, (INT
)(SHORT
)wParam
);
5696 case TVM_SETSCROLLTIME
:
5697 return TREEVIEW_SetScrollTime(infoPtr
, (UINT
)wParam
);
5699 case TVM_SETTEXTCOLOR
:
5700 return TREEVIEW_SetTextColor(infoPtr
, (COLORREF
)lParam
);
5702 case TVM_SETTOOLTIPS
:
5703 return TREEVIEW_SetToolTips(infoPtr
, (HWND
)wParam
);
5705 case TVM_SETUNICODEFORMAT
:
5706 return TREEVIEW_SetUnicodeFormat(infoPtr
, (BOOL
)wParam
);
5708 case TVM_SORTCHILDREN
:
5709 return TREEVIEW_SortChildren(infoPtr
, lParam
);
5711 case TVM_SORTCHILDRENCB
:
5712 return TREEVIEW_SortChildrenCB(infoPtr
, (LPTVSORTCB
)lParam
);
5715 return TREEVIEW_ProcessLetterKeys(infoPtr
, wParam
, lParam
);
5718 return TREEVIEW_Command(infoPtr
, wParam
, lParam
);
5721 return TREEVIEW_Destroy(infoPtr
);
5726 return TREEVIEW_EraseBackground(infoPtr
, (HDC
)wParam
);
5729 return DLGC_WANTARROWS
| DLGC_WANTCHARS
;
5732 return TREEVIEW_GetFont(infoPtr
);
5735 return TREEVIEW_HScroll(infoPtr
, wParam
);
5738 return TREEVIEW_KeyDown(infoPtr
, wParam
);
5741 return TREEVIEW_KillFocus(infoPtr
);
5743 case WM_LBUTTONDBLCLK
:
5744 return TREEVIEW_LButtonDoubleClick(infoPtr
, lParam
);
5746 case WM_LBUTTONDOWN
:
5747 return TREEVIEW_LButtonDown(infoPtr
, lParam
);
5749 /* WM_MBUTTONDOWN */
5752 return TREEVIEW_MouseLeave(infoPtr
);
5755 return TREEVIEW_MouseMove(infoPtr
, lParam
);
5757 case WM_NCLBUTTONDOWN
:
5758 if (infoPtr
->hwndEdit
)
5759 SetFocus(infoPtr
->hwnd
);
5763 return TREEVIEW_NCPaint (infoPtr
, (HRGN
)wParam
, lParam
);
5766 return TREEVIEW_Notify(infoPtr
, wParam
, lParam
);
5768 case WM_NOTIFYFORMAT
:
5769 return TREEVIEW_NotifyFormat(infoPtr
, (HWND
)wParam
, (UINT
)lParam
);
5771 case WM_PRINTCLIENT
:
5772 return TREEVIEW_PrintClient(infoPtr
, (HDC
)wParam
, lParam
);
5775 return TREEVIEW_Paint(infoPtr
, (HDC
)wParam
);
5777 case WM_RBUTTONDOWN
:
5778 return TREEVIEW_RButtonDown(infoPtr
, lParam
);
5781 return TREEVIEW_SetCursor(infoPtr
, wParam
, lParam
);
5784 return TREEVIEW_SetFocus(infoPtr
);
5787 return TREEVIEW_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
5790 return TREEVIEW_SetRedraw(infoPtr
, wParam
);
5793 return TREEVIEW_Size(infoPtr
, wParam
, lParam
);
5795 case WM_STYLECHANGED
:
5796 return TREEVIEW_StyleChanged(infoPtr
, wParam
, lParam
);
5798 case WM_SYSCOLORCHANGE
:
5799 COMCTL32_RefreshSysColors();
5805 return TREEVIEW_HandleTimer(infoPtr
, wParam
);
5807 case WM_THEMECHANGED
:
5808 return TREEVIEW_ThemeChanged (infoPtr
);
5811 return TREEVIEW_VScroll(infoPtr
, wParam
);
5813 /* WM_WININICHANGE */
5816 return TREEVIEW_MouseWheel(infoPtr
, wParam
, lParam
);
5819 TRACE("drawItem\n");
5823 /* This mostly catches MFC and Delphi messages. :( */
5824 if ((uMsg
>= WM_USER
) && (uMsg
< WM_APP
) && !COMCTL32_IsReflectedMessage(uMsg
))
5825 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg
, wParam
, lParam
);
5827 return DefWindowProcW(hwnd
, uMsg
, wParam
, lParam
);
5832 /* Class Registration ***************************************************/
5835 TREEVIEW_Register(void)
5841 ZeroMemory(&wndClass
, sizeof(WNDCLASSW
));
5842 wndClass
.style
= CS_GLOBALCLASS
| CS_DBLCLKS
;
5843 wndClass
.lpfnWndProc
= TREEVIEW_WindowProc
;
5844 wndClass
.cbClsExtra
= 0;
5845 wndClass
.cbWndExtra
= sizeof(TREEVIEW_INFO
*);
5847 wndClass
.hCursor
= LoadCursorW(0, (LPWSTR
)IDC_ARROW
);
5848 wndClass
.hbrBackground
= 0;
5849 wndClass
.lpszClassName
= WC_TREEVIEWW
;
5851 RegisterClassW(&wndClass
);
5856 TREEVIEW_Unregister(void)
5858 UnregisterClassW(WC_TREEVIEWW
, NULL
);
5862 /* Tree Verification ****************************************************/
5865 TREEVIEW_VerifyChildren(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
);
5867 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO
*infoPtr
,
5868 const TREEVIEW_ITEM
*item
)
5870 assert(infoPtr
!= NULL
);
5871 assert(item
!= NULL
);
5873 /* both NULL, or both non-null */
5874 assert((item
->firstChild
== NULL
) == (item
->lastChild
== NULL
));
5876 assert(item
->firstChild
!= item
);
5877 assert(item
->lastChild
!= item
);
5879 if (item
->firstChild
)
5881 assert(item
->firstChild
->parent
== item
);
5882 assert(item
->firstChild
->prevSibling
== NULL
);
5885 if (item
->lastChild
)
5887 assert(item
->lastChild
->parent
== item
);
5888 assert(item
->lastChild
->nextSibling
== NULL
);
5891 assert(item
->nextSibling
!= item
);
5892 if (item
->nextSibling
)
5894 assert(item
->nextSibling
->parent
== item
->parent
);
5895 assert(item
->nextSibling
->prevSibling
== item
);
5898 assert(item
->prevSibling
!= item
);
5899 if (item
->prevSibling
)
5901 assert(item
->prevSibling
->parent
== item
->parent
);
5902 assert(item
->prevSibling
->nextSibling
== item
);
5907 TREEVIEW_VerifyItem(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
5909 assert(item
!= NULL
);
5911 assert(item
->parent
!= NULL
);
5912 assert(item
->parent
!= item
);
5913 assert(item
->iLevel
== item
->parent
->iLevel
+ 1);
5915 assert(DPA_GetPtrIndex(infoPtr
->items
, item
) != -1);
5917 TREEVIEW_VerifyItemCommon(infoPtr
, item
);
5919 TREEVIEW_VerifyChildren(infoPtr
, item
);
5923 TREEVIEW_VerifyChildren(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
5925 const TREEVIEW_ITEM
*child
;
5926 assert(item
!= NULL
);
5928 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
5929 TREEVIEW_VerifyItem(infoPtr
, child
);
5933 TREEVIEW_VerifyRoot(TREEVIEW_INFO
*infoPtr
)
5935 TREEVIEW_ITEM
*root
= infoPtr
->root
;
5937 assert(root
!= NULL
);
5938 assert(root
->iLevel
== -1);
5939 assert(root
->parent
== NULL
);
5940 assert(root
->prevSibling
== NULL
);
5942 TREEVIEW_VerifyItemCommon(infoPtr
, root
);
5944 TREEVIEW_VerifyChildren(infoPtr
, root
);
5948 TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
)
5950 if (!TRACE_ON(treeview
)) return;
5952 assert(infoPtr
!= NULL
);
5953 TREEVIEW_VerifyRoot(infoPtr
);