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.
44 #include "wine/port.h"
53 #define NONAMELESSUNION
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
66 #include "wine/exception.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(treeview
);
70 /* internal structures */
71 typedef struct tagTREEVIEW_INFO
74 HWND hwndNotify
; /* Owner window to send notifications to */
79 UINT uNumItems
; /* number of valid TREEVIEW_ITEMs */
80 INT cdmode
; /* last custom draw setting */
81 UINT uScrollTime
; /* max. time for scrolling in milliseconds */
82 BOOL bRedraw
; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
84 UINT uItemHeight
; /* item height */
87 LONG clientWidth
; /* width of control window */
88 LONG clientHeight
; /* height of control window */
90 LONG treeWidth
; /* width of visible tree items */
91 LONG treeHeight
; /* height of visible tree items */
93 UINT uIndent
; /* indentation in pixels */
94 HTREEITEM selectedItem
; /* handle to selected item or 0 if none */
95 HTREEITEM hotItem
; /* handle currently under cursor, 0 if none */
96 HTREEITEM focusedItem
; /* item that was under the cursor when WM_LBUTTONDOWN was received */
97 HTREEITEM editItem
; /* item being edited with builtin edit box */
99 HTREEITEM firstVisible
; /* handle to first visible item */
100 LONG maxVisibleOrder
;
101 HTREEITEM dropItem
; /* handle to item selected by drag cursor */
102 HTREEITEM insertMarkItem
; /* item after which insertion mark is placed */
103 BOOL insertBeforeorAfter
; /* flag used by TVM_SETINSERTMARK */
104 HIMAGELIST dragList
; /* Bitmap of dragged item */
110 COLORREF clrInsertMark
;
114 HFONT hUnderlineFont
;
115 HFONT hBoldUnderlineFont
;
120 WNDPROC wpEditOrig
; /* orig window proc for subclassing edit */
121 BOOL bIgnoreEditKillFocus
;
124 BOOL bNtfUnicode
; /* TRUE if should send NOTIFY with W */
125 HIMAGELIST himlNormal
;
126 int normalImageHeight
;
127 int normalImageWidth
;
128 HIMAGELIST himlState
;
129 int stateImageHeight
;
133 DWORD lastKeyPressTimestamp
;
135 INT nSearchParamLength
;
136 WCHAR szSearchParam
[ MAX_PATH
];
139 typedef struct _TREEITEM
/* HTREEITEM is a _TREEINFO *. */
141 HTREEITEM parent
; /* handle to parent or 0 if at root */
142 HTREEITEM nextSibling
; /* handle to next item in list, 0 if last */
143 HTREEITEM firstChild
; /* handle to first child or 0 if no child */
155 int iIntegral
; /* item height multiplier (1 is normal) */
156 int iLevel
; /* indentation level:0=root level */
158 HTREEITEM prevSibling
; /* handle to prev item in list, 0 if first */
164 LONG textWidth
; /* horizontal text extent for pszText */
165 LONG visibleOrder
; /* visible ordering, 0 is first visible item */
166 const TREEVIEW_INFO
*infoPtr
; /* tree data this item belongs to */
169 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
170 #define KEY_DELAY 450
172 /* bitflags for infoPtr->uInternalStatus */
174 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
175 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
176 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
177 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
178 #define TV_RDRAG 0x10 /* ditto Rbutton */
179 #define TV_RDRAGGING 0x20
181 /* bitflags for infoPtr->timer */
183 #define TV_EDIT_TIMER 2
184 #define TV_EDIT_TIMER_SET 2
186 #define TEXT_CALLBACK_SIZE 260
188 #define TREEVIEW_LEFT_MARGIN 8
190 #define MINIMUM_INDENT 19
192 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
194 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
195 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
196 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
198 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
199 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
200 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
201 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
203 static const WCHAR themeClass
[] = { 'T','r','e','e','v','i','e','w',0 };
206 typedef VOID (*TREEVIEW_ItemEnumFunc
)(TREEVIEW_INFO
*, TREEVIEW_ITEM
*,LPVOID
);
209 static VOID
TREEVIEW_Invalidate(const TREEVIEW_INFO
*, const TREEVIEW_ITEM
*);
211 static LRESULT
TREEVIEW_DoSelectItem(TREEVIEW_INFO
*, INT
, HTREEITEM
, INT
);
212 static VOID
TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*, TREEVIEW_ITEM
*, BOOL
);
213 static LRESULT
TREEVIEW_EnsureVisible(TREEVIEW_INFO
*, HTREEITEM
, BOOL
);
214 static LRESULT
TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
);
215 static VOID
TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
);
216 static LRESULT
TREEVIEW_HScroll(TREEVIEW_INFO
*, WPARAM
);
218 /* Random Utilities *****************************************************/
219 static void TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
);
221 /* Returns the treeview private data if hwnd is a treeview.
222 * Otherwise returns an undefined value. */
223 static inline TREEVIEW_INFO
*
224 TREEVIEW_GetInfoPtr(HWND hwnd
)
226 return (TREEVIEW_INFO
*)GetWindowLongPtrW(hwnd
, 0);
229 /* Don't call this. Nothing wants an item index. */
231 TREEVIEW_GetItemIndex(const TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
233 return DPA_GetPtrIndex(infoPtr
->items
, handle
);
236 /* Checks if item has changed and needs to be redrawn */
237 static inline BOOL
item_changed (const TREEVIEW_ITEM
*tiOld
, const TREEVIEW_ITEM
*tiNew
,
238 const TVITEMEXW
*tvChange
)
240 /* Number of children has changed */
241 if ((tvChange
->mask
& TVIF_CHILDREN
) && (tiOld
->cChildren
!= tiNew
->cChildren
))
244 /* Image has changed and it's not a callback */
245 if ((tvChange
->mask
& TVIF_IMAGE
) && (tiOld
->iImage
!= tiNew
->iImage
) &&
246 tiNew
->iImage
!= I_IMAGECALLBACK
)
249 /* Selected image has changed and it's not a callback */
250 if ((tvChange
->mask
& TVIF_SELECTEDIMAGE
) && (tiOld
->iSelectedImage
!= tiNew
->iSelectedImage
) &&
251 tiNew
->iSelectedImage
!= I_IMAGECALLBACK
)
254 if ((tvChange
->mask
& TVIF_EXPANDEDIMAGE
) && (tiOld
->iExpandedImage
!= tiNew
->iExpandedImage
) &&
255 tiNew
->iExpandedImage
!= I_IMAGECALLBACK
)
258 /* Text has changed and it's not a callback */
259 if ((tvChange
->mask
& TVIF_TEXT
) && (tiOld
->pszText
!= tiNew
->pszText
) &&
260 tiNew
->pszText
!= LPSTR_TEXTCALLBACKW
)
263 /* Indent has changed */
264 if ((tvChange
->mask
& TVIF_INTEGRAL
) && (tiOld
->iIntegral
!= tiNew
->iIntegral
))
267 /* Item state has changed */
268 if ((tvChange
->mask
& TVIF_STATE
) && ((tiOld
->state
^ tiNew
->state
) & tvChange
->stateMask
))
274 /***************************************************************************
275 * This method checks that handle is an item for this tree.
278 TREEVIEW_ValidItem(const TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
280 if (TREEVIEW_GetItemIndex(infoPtr
, handle
) == -1)
282 TRACE("invalid item %p\n", handle
);
290 TREEVIEW_CreateBoldFont(HFONT hOrigFont
)
294 GetObjectW(hOrigFont
, sizeof(font
), &font
);
295 font
.lfWeight
= FW_BOLD
;
296 return CreateFontIndirectW(&font
);
300 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont
)
304 GetObjectW(hOrigFont
, sizeof(font
), &font
);
305 font
.lfUnderline
= TRUE
;
306 return CreateFontIndirectW(&font
);
310 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont
)
314 GetObjectW(hfont
, sizeof(font
), &font
);
315 font
.lfWeight
= FW_BOLD
;
316 font
.lfUnderline
= TRUE
;
317 return CreateFontIndirectW(&font
);
321 TREEVIEW_FontForItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
323 if ((infoPtr
->dwStyle
& TVS_TRACKSELECT
) && (item
== infoPtr
->hotItem
))
324 return item
->state
& TVIS_BOLD
? infoPtr
->hBoldUnderlineFont
: infoPtr
->hUnderlineFont
;
325 if (item
->state
& TVIS_BOLD
)
326 return infoPtr
->hBoldFont
;
327 return infoPtr
->hFont
;
330 /* for trace/debugging purposes only */
332 TREEVIEW_ItemName(const TREEVIEW_ITEM
*item
)
334 if (item
== NULL
) return "<null item>";
335 if (item
->pszText
== LPSTR_TEXTCALLBACKW
) return "<callback>";
336 if (item
->pszText
== NULL
) return "<null>";
337 return debugstr_w(item
->pszText
);
340 /* An item is not a child of itself. */
342 TREEVIEW_IsChildOf(const TREEVIEW_ITEM
*parent
, const TREEVIEW_ITEM
*child
)
346 child
= child
->parent
;
347 if (child
== parent
) return TRUE
;
348 } while (child
!= NULL
);
354 /* Tree Traversal *******************************************************/
356 /***************************************************************************
357 * This method returns the last expanded sibling or child child item
360 static TREEVIEW_ITEM
*
361 TREEVIEW_GetLastListItem(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
363 if (!item
) return NULL
;
365 while (item
->lastChild
)
367 if (item
->state
& TVIS_EXPANDED
)
368 item
= item
->lastChild
;
373 if (item
== infoPtr
->root
)
379 /***************************************************************************
380 * This method returns the previous non-hidden item in the list not
381 * considering the tree hierarchy.
383 static TREEVIEW_ITEM
*
384 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*tvItem
)
386 if (tvItem
->prevSibling
)
388 /* This item has a prevSibling, get the last item in the sibling's tree. */
389 TREEVIEW_ITEM
*upItem
= tvItem
->prevSibling
;
391 if ((upItem
->state
& TVIS_EXPANDED
) && upItem
->lastChild
!= NULL
)
392 return TREEVIEW_GetLastListItem(infoPtr
, upItem
->lastChild
);
398 /* this item does not have a prevSibling, get the parent */
399 return (tvItem
->parent
!= infoPtr
->root
) ? tvItem
->parent
: NULL
;
404 /***************************************************************************
405 * This method returns the next physical item in the treeview not
406 * considering the tree hierarchy.
408 static TREEVIEW_ITEM
*
409 TREEVIEW_GetNextListItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*tvItem
)
412 * If this item has children and is expanded, return the first child
414 if ((tvItem
->state
& TVIS_EXPANDED
) && tvItem
->firstChild
!= NULL
)
416 return tvItem
->firstChild
;
421 * try to get the sibling
423 if (tvItem
->nextSibling
)
424 return tvItem
->nextSibling
;
427 * Otherwise, get the parent's sibling.
429 while (tvItem
->parent
)
431 tvItem
= tvItem
->parent
;
433 if (tvItem
->nextSibling
)
434 return tvItem
->nextSibling
;
440 /***************************************************************************
441 * This method returns the nth item starting at the given item. It returns
442 * the last item (or first) we we run out of items.
444 * Will scroll backward if count is <0.
445 * forward if count is >0.
447 static TREEVIEW_ITEM
*
448 TREEVIEW_GetListItem(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
451 TREEVIEW_ITEM
*(*next_item
)(const TREEVIEW_INFO
*, const TREEVIEW_ITEM
*);
452 TREEVIEW_ITEM
*previousItem
;
454 assert(item
!= NULL
);
458 next_item
= TREEVIEW_GetNextListItem
;
463 next_item
= TREEVIEW_GetPrevListItem
;
471 item
= next_item(infoPtr
, item
);
473 } while (--count
&& item
!= NULL
);
476 return item
? item
: previousItem
;
479 /* Notifications ************************************************************/
481 static INT
get_notifycode(const TREEVIEW_INFO
*infoPtr
, INT code
)
483 if (!infoPtr
->bNtfUnicode
) {
485 case TVN_SELCHANGINGW
: return TVN_SELCHANGINGA
;
486 case TVN_SELCHANGEDW
: return TVN_SELCHANGEDA
;
487 case TVN_GETDISPINFOW
: return TVN_GETDISPINFOA
;
488 case TVN_SETDISPINFOW
: return TVN_SETDISPINFOA
;
489 case TVN_ITEMEXPANDINGW
: return TVN_ITEMEXPANDINGA
;
490 case TVN_ITEMEXPANDEDW
: return TVN_ITEMEXPANDEDA
;
491 case TVN_BEGINDRAGW
: return TVN_BEGINDRAGA
;
492 case TVN_BEGINRDRAGW
: return TVN_BEGINRDRAGA
;
493 case TVN_DELETEITEMW
: return TVN_DELETEITEMA
;
494 case TVN_BEGINLABELEDITW
: return TVN_BEGINLABELEDITA
;
495 case TVN_ENDLABELEDITW
: return TVN_ENDLABELEDITA
;
496 case TVN_GETINFOTIPW
: return TVN_GETINFOTIPA
;
503 TREEVIEW_SendRealNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
, NMHDR
*hdr
)
505 TRACE("code=%d, hdr=%p\n", code
, hdr
);
507 hdr
->hwndFrom
= infoPtr
->hwnd
;
508 hdr
->idFrom
= GetWindowLongPtrW(infoPtr
->hwnd
, GWLP_ID
);
509 hdr
->code
= get_notifycode(infoPtr
, code
);
511 return SendMessageW(infoPtr
->hwndNotify
, WM_NOTIFY
, hdr
->idFrom
, (LPARAM
)hdr
);
515 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
)
518 return TREEVIEW_SendRealNotify(infoPtr
, code
, &hdr
);
522 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO
*infoPtr
, UINT mask
, TVITEMW
*tvItem
, TREEVIEW_ITEM
*item
)
525 tvItem
->hItem
= item
;
526 tvItem
->state
= item
->state
;
527 tvItem
->stateMask
= 0;
528 tvItem
->iImage
= item
->iImage
;
529 tvItem
->iSelectedImage
= item
->iSelectedImage
;
530 tvItem
->cChildren
= item
->cChildren
;
531 tvItem
->lParam
= item
->lParam
;
535 if (!infoPtr
->bNtfUnicode
)
537 tvItem
->cchTextMax
= WideCharToMultiByte( CP_ACP
, 0, item
->pszText
, -1, NULL
, 0, NULL
, NULL
);
538 tvItem
->pszText
= Alloc (tvItem
->cchTextMax
);
539 WideCharToMultiByte( CP_ACP
, 0, item
->pszText
, -1, (LPSTR
)tvItem
->pszText
, tvItem
->cchTextMax
, 0, 0 );
543 tvItem
->cchTextMax
= item
->cchTextMax
;
544 tvItem
->pszText
= item
->pszText
;
549 tvItem
->cchTextMax
= 0;
550 tvItem
->pszText
= NULL
;
555 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
, UINT action
,
556 UINT mask
, HTREEITEM oldItem
, HTREEITEM newItem
)
561 TRACE("code:%d action:0x%x olditem:%p newitem:%p\n",
562 code
, action
, oldItem
, newItem
);
564 memset(&nmhdr
, 0, sizeof(NMTREEVIEWW
));
565 nmhdr
.action
= action
;
568 TREEVIEW_TVItemFromItem(infoPtr
, mask
, &nmhdr
.itemOld
, oldItem
);
571 TREEVIEW_TVItemFromItem(infoPtr
, mask
, &nmhdr
.itemNew
, newItem
);
576 ret
= TREEVIEW_SendRealNotify(infoPtr
, code
, &nmhdr
.hdr
);
577 if (!infoPtr
->bNtfUnicode
)
579 Free(nmhdr
.itemOld
.pszText
);
580 Free(nmhdr
.itemNew
.pszText
);
586 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
,
587 HTREEITEM dragItem
, POINT pt
)
591 TRACE("code:%d dragitem:%p\n", code
, dragItem
);
594 nmhdr
.itemNew
.mask
= TVIF_STATE
| TVIF_PARAM
| TVIF_HANDLE
;
595 nmhdr
.itemNew
.hItem
= dragItem
;
596 nmhdr
.itemNew
.state
= dragItem
->state
;
597 nmhdr
.itemNew
.lParam
= dragItem
->lParam
;
599 nmhdr
.ptDrag
.x
= pt
.x
;
600 nmhdr
.ptDrag
.y
= pt
.y
;
602 return TREEVIEW_SendRealNotify(infoPtr
, code
, &nmhdr
.hdr
);
607 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO
*infoPtr
, DWORD dwDrawStage
,
610 NMTVCUSTOMDRAW nmcdhdr
;
613 TRACE("drawstage:0x%x hdc:%p\n", dwDrawStage
, hdc
);
615 nmcd
= &nmcdhdr
.nmcd
;
616 nmcd
->dwDrawStage
= dwDrawStage
;
619 nmcd
->dwItemSpec
= 0;
620 nmcd
->uItemState
= 0;
621 nmcd
->lItemlParam
= 0;
622 nmcdhdr
.clrText
= infoPtr
->clrText
;
623 nmcdhdr
.clrTextBk
= infoPtr
->clrBk
;
626 return TREEVIEW_SendRealNotify(infoPtr
, NM_CUSTOMDRAW
, &nmcdhdr
.nmcd
.hdr
);
629 /* FIXME: need to find out when the flags in uItemState need to be set */
632 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO
*infoPtr
, HDC hdc
,
633 TREEVIEW_ITEM
*item
, UINT uItemDrawState
,
634 NMTVCUSTOMDRAW
*nmcdhdr
)
638 DWORD_PTR dwItemSpec
;
641 dwDrawStage
= CDDS_ITEM
| uItemDrawState
;
642 dwItemSpec
= (DWORD_PTR
)item
;
644 if (item
->state
& TVIS_SELECTED
)
645 uItemState
|= CDIS_SELECTED
;
646 if (item
== infoPtr
->selectedItem
)
647 uItemState
|= CDIS_FOCUS
;
648 if (item
== infoPtr
->hotItem
)
649 uItemState
|= CDIS_HOT
;
651 nmcd
= &nmcdhdr
->nmcd
;
652 nmcd
->dwDrawStage
= dwDrawStage
;
654 nmcd
->rc
= item
->rect
;
655 nmcd
->dwItemSpec
= dwItemSpec
;
656 nmcd
->uItemState
= uItemState
;
657 nmcd
->lItemlParam
= item
->lParam
;
658 nmcdhdr
->iLevel
= item
->iLevel
;
660 TRACE("drawstage:0x%x hdc:%p item:%lx, itemstate:0x%x, lItemlParam:0x%lx\n",
661 nmcd
->dwDrawStage
, nmcd
->hdc
, nmcd
->dwItemSpec
,
662 nmcd
->uItemState
, nmcd
->lItemlParam
);
664 return TREEVIEW_SendRealNotify(infoPtr
, NM_CUSTOMDRAW
, &nmcdhdr
->nmcd
.hdr
);
668 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*editItem
)
673 TREEVIEW_TVItemFromItem(infoPtr
, TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
| TVIF_TEXT
,
674 &tvdi
.item
, editItem
);
676 ret
= TREEVIEW_SendRealNotify(infoPtr
, TVN_BEGINLABELEDITW
, &tvdi
.hdr
);
678 if (!infoPtr
->bNtfUnicode
)
679 Free(tvdi
.item
.pszText
);
685 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
688 NMTVDISPINFOEXW callback
;
690 TRACE("mask=0x%x, callbackmask=0x%x\n", mask
, item
->callbackMask
);
691 mask
&= item
->callbackMask
;
693 if (mask
== 0) return;
695 /* 'state' always contains valid value, as well as 'lParam'.
696 * All other parameters are uninitialized.
698 callback
.item
.pszText
= item
->pszText
;
699 callback
.item
.cchTextMax
= item
->cchTextMax
;
700 callback
.item
.mask
= mask
;
701 callback
.item
.hItem
= item
;
702 callback
.item
.state
= item
->state
;
703 callback
.item
.lParam
= item
->lParam
;
705 /* If text is changed we need to recalculate textWidth */
706 if (mask
& TVIF_TEXT
)
709 TREEVIEW_SendRealNotify(infoPtr
, TVN_GETDISPINFOW
, &callback
.hdr
);
710 TRACE("resulting code 0x%08x\n", callback
.hdr
.code
);
712 /* It may have changed due to a call to SetItem. */
713 mask
&= item
->callbackMask
;
715 if ((mask
& TVIF_TEXT
) && callback
.item
.pszText
!= item
->pszText
)
717 /* Instead of copying text into our buffer user specified his own */
718 if (!infoPtr
->bNtfUnicode
&& (callback
.hdr
.code
== TVN_GETDISPINFOA
)) {
721 int len
= MultiByteToWideChar( CP_ACP
, 0,
722 (LPSTR
)callback
.item
.pszText
, -1,
724 buflen
= max((len
)*sizeof(WCHAR
), TEXT_CALLBACK_SIZE
);
725 newText
= ReAlloc(item
->pszText
, buflen
);
727 TRACE("returned str %s, len=%d, buflen=%d\n",
728 debugstr_a((LPSTR
)callback
.item
.pszText
), len
, buflen
);
732 item
->pszText
= newText
;
733 MultiByteToWideChar( CP_ACP
, 0,
734 (LPSTR
)callback
.item
.pszText
, -1,
735 item
->pszText
, buflen
/sizeof(WCHAR
));
736 item
->cchTextMax
= buflen
/sizeof(WCHAR
);
738 /* If ReAlloc fails we have nothing to do, but keep original text */
741 int len
= max(lstrlenW(callback
.item
.pszText
) + 1,
743 LPWSTR newText
= ReAlloc(item
->pszText
, len
);
745 TRACE("returned wstr %s, len=%d\n",
746 debugstr_w(callback
.item
.pszText
), len
);
750 item
->pszText
= newText
;
751 strcpyW(item
->pszText
, callback
.item
.pszText
);
752 item
->cchTextMax
= len
;
754 /* If ReAlloc fails we have nothing to do, but keep original text */
757 else if (mask
& TVIF_TEXT
) {
758 /* User put text into our buffer, that is ok unless A string */
759 if (!infoPtr
->bNtfUnicode
&& (callback
.hdr
.code
== TVN_GETDISPINFOA
)) {
762 int len
= MultiByteToWideChar( CP_ACP
, 0,
763 (LPSTR
)callback
.item
.pszText
, -1,
765 buflen
= max((len
)*sizeof(WCHAR
), TEXT_CALLBACK_SIZE
);
766 newText
= Alloc(buflen
);
768 TRACE("same buffer str %s, len=%d, buflen=%d\n",
769 debugstr_a((LPSTR
)callback
.item
.pszText
), len
, buflen
);
773 LPWSTR oldText
= item
->pszText
;
774 item
->pszText
= newText
;
775 MultiByteToWideChar( CP_ACP
, 0,
776 (LPSTR
)callback
.item
.pszText
, -1,
777 item
->pszText
, buflen
/sizeof(WCHAR
));
778 item
->cchTextMax
= buflen
/sizeof(WCHAR
);
784 if (mask
& TVIF_IMAGE
)
785 item
->iImage
= callback
.item
.iImage
;
787 if (mask
& TVIF_SELECTEDIMAGE
)
788 item
->iSelectedImage
= callback
.item
.iSelectedImage
;
790 if (mask
& TVIF_EXPANDEDIMAGE
)
791 item
->iExpandedImage
= callback
.item
.iExpandedImage
;
793 if (mask
& TVIF_CHILDREN
)
794 item
->cChildren
= callback
.item
.cChildren
;
796 if (callback
.item
.mask
& TVIF_STATE
)
798 item
->state
&= ~callback
.item
.stateMask
;
799 item
->state
|= (callback
.item
.state
& callback
.item
.stateMask
);
802 /* These members are now permanently set. */
803 if (callback
.item
.mask
& TVIF_DI_SETITEM
)
804 item
->callbackMask
&= ~callback
.item
.mask
;
807 /***************************************************************************
808 * This function uses cChildren field to decide whether the item has
810 * Note: if this returns TRUE, the child items may not actually exist,
811 * they could be virtual.
813 * Just use item->firstChild to check for physical children.
816 TREEVIEW_HasChildren(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
818 TREEVIEW_UpdateDispInfo(infoPtr
, item
, TVIF_CHILDREN
);
819 /* Protect for a case when callback field is not changed by a host,
820 otherwise negative values trigger normal notifications. */
821 return item
->cChildren
!= 0 && item
->cChildren
!= I_CHILDRENCALLBACK
;
824 static INT
TREEVIEW_NotifyFormat (TREEVIEW_INFO
*infoPtr
, HWND hwndFrom
, UINT nCommand
)
828 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom
, nCommand
);
830 if (nCommand
!= NF_REQUERY
) return 0;
832 format
= SendMessageW(hwndFrom
, WM_NOTIFYFORMAT
, (WPARAM
)infoPtr
->hwnd
, NF_QUERY
);
833 TRACE("format=%d\n", format
);
835 /* Invalid format returned by NF_QUERY defaults to ANSI*/
836 if (format
!= NFR_ANSI
&& format
!= NFR_UNICODE
)
839 infoPtr
->bNtfUnicode
= (format
== NFR_UNICODE
);
844 /* Item Position ********************************************************/
846 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
848 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
850 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
851 BOOL lar
= ((infoPtr
->dwStyle
& (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
854 item
->linesOffset
= infoPtr
->uIndent
* (lar
? item
->iLevel
: item
->iLevel
- 1)
856 item
->stateOffset
= item
->linesOffset
+ infoPtr
->uIndent
;
857 item
->imageOffset
= item
->stateOffset
858 + (STATEIMAGEINDEX(item
->state
) ? infoPtr
->stateImageWidth
: 0);
859 item
->textOffset
= item
->imageOffset
+ infoPtr
->normalImageWidth
;
863 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
, HDC hDC
)
869 /* DRAW's OM docker creates items like this */
870 if (item
->pszText
== NULL
)
882 hdc
= GetDC(infoPtr
->hwnd
);
883 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, item
));
886 GetTextExtentPoint32W(hdc
, item
->pszText
, strlenW(item
->pszText
), &sz
);
887 item
->textWidth
= sz
.cx
;
891 SelectObject(hdc
, hOldFont
);
897 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
899 item
->rect
.top
= infoPtr
->uItemHeight
*
900 (item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
);
902 item
->rect
.bottom
= item
->rect
.top
903 + infoPtr
->uItemHeight
* item
->iIntegral
- 1;
906 item
->rect
.right
= infoPtr
->clientWidth
;
909 /* We know that only items after start need their order updated. */
911 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*start
)
918 start
= infoPtr
->root
->firstChild
;
922 order
= start
->visibleOrder
;
924 for (item
= start
; item
!= NULL
;
925 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
927 if (!ISVISIBLE(item
) && order
> 0)
928 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, item
);
929 item
->visibleOrder
= order
;
930 order
+= item
->iIntegral
;
933 infoPtr
->maxVisibleOrder
= order
;
935 for (item
= start
; item
!= NULL
;
936 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
938 TREEVIEW_ComputeItemRect(infoPtr
, item
);
943 /* Update metrics of all items in selected subtree.
944 * root must be expanded
947 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*root
)
949 TREEVIEW_ITEM
*sibling
;
953 if (!root
->firstChild
|| !(root
->state
& TVIS_EXPANDED
))
956 root
->state
&= ~TVIS_EXPANDED
;
957 sibling
= TREEVIEW_GetNextListItem(infoPtr
, root
);
958 root
->state
|= TVIS_EXPANDED
;
960 hdc
= GetDC(infoPtr
->hwnd
);
961 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
963 for (; root
!= sibling
;
964 root
= TREEVIEW_GetNextListItem(infoPtr
, root
))
966 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, root
);
968 if (root
->callbackMask
& TVIF_TEXT
)
969 TREEVIEW_UpdateDispInfo(infoPtr
, root
, TVIF_TEXT
);
971 if (root
->textWidth
== 0)
973 SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, root
));
974 TREEVIEW_ComputeTextWidth(infoPtr
, root
, hdc
);
978 SelectObject(hdc
, hOldFont
);
979 ReleaseDC(infoPtr
->hwnd
, hdc
);
982 /* Item Allocation **********************************************************/
984 static TREEVIEW_ITEM
*
985 TREEVIEW_AllocateItem(const TREEVIEW_INFO
*infoPtr
)
987 TREEVIEW_ITEM
*newItem
= Alloc(sizeof(TREEVIEW_ITEM
));
992 /* I_IMAGENONE would make more sense but this is neither what is
993 * documented (MSDN doesn't specify) nor what Windows actually does
994 * (it sets it to zero)... and I can so imagine an application using
995 * inc/dec to toggle the images. */
997 newItem
->iSelectedImage
= 0;
998 newItem
->iExpandedImage
= (WORD
)I_IMAGENONE
;
999 newItem
->infoPtr
= infoPtr
;
1001 if (DPA_InsertPtr(infoPtr
->items
, INT_MAX
, newItem
) == -1)
1010 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1011 * free item->pszText. */
1013 TREEVIEW_FreeItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
1015 DPA_DeletePtr(infoPtr
->items
, DPA_GetPtrIndex(infoPtr
->items
, item
));
1016 if (infoPtr
->selectedItem
== item
)
1017 infoPtr
->selectedItem
= NULL
;
1018 if (infoPtr
->hotItem
== item
)
1019 infoPtr
->hotItem
= NULL
;
1020 if (infoPtr
->focusedItem
== item
)
1021 infoPtr
->focusedItem
= NULL
;
1022 if (infoPtr
->firstVisible
== item
)
1023 infoPtr
->firstVisible
= NULL
;
1024 if (infoPtr
->dropItem
== item
)
1025 infoPtr
->dropItem
= NULL
;
1026 if (infoPtr
->insertMarkItem
== item
)
1027 infoPtr
->insertMarkItem
= NULL
;
1032 /* Item Insertion *******************************************************/
1034 /***************************************************************************
1035 * This method inserts newItem before sibling as a child of parent.
1036 * sibling can be NULL, but only if parent has no children.
1039 TREEVIEW_InsertBefore(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
1040 TREEVIEW_ITEM
*parent
)
1042 assert(parent
!= NULL
);
1044 if (sibling
!= NULL
)
1046 assert(sibling
->parent
== parent
);
1048 if (sibling
->prevSibling
!= NULL
)
1049 sibling
->prevSibling
->nextSibling
= newItem
;
1051 newItem
->prevSibling
= sibling
->prevSibling
;
1052 sibling
->prevSibling
= newItem
;
1055 newItem
->prevSibling
= NULL
;
1057 newItem
->nextSibling
= sibling
;
1059 if (parent
->firstChild
== sibling
)
1060 parent
->firstChild
= newItem
;
1062 if (parent
->lastChild
== NULL
)
1063 parent
->lastChild
= newItem
;
1066 /***************************************************************************
1067 * This method inserts newItem after sibling as a child of parent.
1068 * sibling can be NULL, but only if parent has no children.
1071 TREEVIEW_InsertAfter(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
1072 TREEVIEW_ITEM
*parent
)
1074 assert(parent
!= NULL
);
1076 if (sibling
!= NULL
)
1078 assert(sibling
->parent
== parent
);
1080 if (sibling
->nextSibling
!= NULL
)
1081 sibling
->nextSibling
->prevSibling
= newItem
;
1083 newItem
->nextSibling
= sibling
->nextSibling
;
1084 sibling
->nextSibling
= newItem
;
1087 newItem
->nextSibling
= NULL
;
1089 newItem
->prevSibling
= sibling
;
1091 if (parent
->lastChild
== sibling
)
1092 parent
->lastChild
= newItem
;
1094 if (parent
->firstChild
== NULL
)
1095 parent
->firstChild
= newItem
;
1099 TREEVIEW_DoSetItemT(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
1100 const TVITEMEXW
*tvItem
, BOOL isW
)
1102 UINT callbackClear
= 0;
1103 UINT callbackSet
= 0;
1105 TRACE("item %p\n", item
);
1106 /* Do this first in case it fails. */
1107 if (tvItem
->mask
& TVIF_TEXT
)
1109 item
->textWidth
= 0; /* force width recalculation */
1110 if (tvItem
->pszText
!= LPSTR_TEXTCALLBACKW
&& tvItem
->pszText
!= NULL
) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1115 len
= lstrlenW(tvItem
->pszText
) + 1;
1117 len
= MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)tvItem
->pszText
, -1, NULL
, 0);
1119 newText
= ReAlloc(item
->pszText
, len
* sizeof(WCHAR
));
1121 if (newText
== NULL
) return FALSE
;
1123 callbackClear
|= TVIF_TEXT
;
1125 item
->pszText
= newText
;
1126 item
->cchTextMax
= len
;
1128 lstrcpynW(item
->pszText
, tvItem
->pszText
, len
);
1130 MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)tvItem
->pszText
, -1,
1131 item
->pszText
, len
);
1133 TRACE("setting text %s, item %p\n", debugstr_w(item
->pszText
), item
);
1137 callbackSet
|= TVIF_TEXT
;
1139 item
->pszText
= ReAlloc(item
->pszText
,
1140 TEXT_CALLBACK_SIZE
* sizeof(WCHAR
));
1141 item
->cchTextMax
= TEXT_CALLBACK_SIZE
;
1142 TRACE("setting callback, item %p\n", item
);
1146 if (tvItem
->mask
& TVIF_CHILDREN
)
1148 item
->cChildren
= tvItem
->cChildren
;
1150 if (item
->cChildren
== I_CHILDRENCALLBACK
)
1151 callbackSet
|= TVIF_CHILDREN
;
1153 callbackClear
|= TVIF_CHILDREN
;
1156 if (tvItem
->mask
& TVIF_IMAGE
)
1158 item
->iImage
= tvItem
->iImage
;
1160 if (item
->iImage
== I_IMAGECALLBACK
)
1161 callbackSet
|= TVIF_IMAGE
;
1163 callbackClear
|= TVIF_IMAGE
;
1166 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
1168 item
->iSelectedImage
= tvItem
->iSelectedImage
;
1170 if (item
->iSelectedImage
== I_IMAGECALLBACK
)
1171 callbackSet
|= TVIF_SELECTEDIMAGE
;
1173 callbackClear
|= TVIF_SELECTEDIMAGE
;
1176 if (tvItem
->mask
& TVIF_EXPANDEDIMAGE
)
1178 item
->iExpandedImage
= tvItem
->iExpandedImage
;
1180 if (item
->iExpandedImage
== I_IMAGECALLBACK
)
1181 callbackSet
|= TVIF_EXPANDEDIMAGE
;
1183 callbackClear
|= TVIF_EXPANDEDIMAGE
;
1186 if (tvItem
->mask
& TVIF_PARAM
)
1187 item
->lParam
= tvItem
->lParam
;
1189 /* If the application sets TVIF_INTEGRAL without
1190 * supplying a TVITEMEX structure, it's toast. */
1191 if (tvItem
->mask
& TVIF_INTEGRAL
)
1192 item
->iIntegral
= tvItem
->iIntegral
;
1194 if (tvItem
->mask
& TVIF_STATE
)
1196 TRACE("prevstate 0x%x, state 0x%x, mask 0x%x\n", item
->state
, tvItem
->state
,
1198 item
->state
&= ~tvItem
->stateMask
;
1199 item
->state
|= (tvItem
->state
& tvItem
->stateMask
);
1202 if (tvItem
->mask
& TVIF_STATEEX
)
1204 FIXME("New extended state: 0x%x\n", tvItem
->uStateEx
);
1207 item
->callbackMask
|= callbackSet
;
1208 item
->callbackMask
&= ~callbackClear
;
1213 /* Note that the new item is pre-zeroed. */
1215 TREEVIEW_InsertItemT(TREEVIEW_INFO
*infoPtr
, const TVINSERTSTRUCTW
*ptdi
, BOOL isW
)
1217 const TVITEMEXW
*tvItem
= &ptdi
->u
.itemex
;
1218 HTREEITEM insertAfter
;
1219 TREEVIEW_ITEM
*newItem
, *parentItem
;
1220 BOOL bTextUpdated
= FALSE
;
1222 if (ptdi
->hParent
== TVI_ROOT
|| ptdi
->hParent
== 0)
1224 parentItem
= infoPtr
->root
;
1228 parentItem
= ptdi
->hParent
;
1230 if (!TREEVIEW_ValidItem(infoPtr
, parentItem
))
1232 WARN("invalid parent %p\n", parentItem
);
1237 insertAfter
= ptdi
->hInsertAfter
;
1239 /* Validate this now for convenience. */
1240 switch ((DWORD_PTR
)insertAfter
)
1242 case (DWORD_PTR
)TVI_FIRST
:
1243 case (DWORD_PTR
)TVI_LAST
:
1244 case (DWORD_PTR
)TVI_SORT
:
1248 if (!TREEVIEW_ValidItem(infoPtr
, insertAfter
) ||
1249 insertAfter
->parent
!= parentItem
)
1251 WARN("invalid insert after %p\n", insertAfter
);
1252 insertAfter
= TVI_LAST
;
1256 TRACE("parent %p position %p: %s\n", parentItem
, insertAfter
,
1257 (tvItem
->mask
& TVIF_TEXT
)
1258 ? ((tvItem
->pszText
== LPSTR_TEXTCALLBACKW
) ? "<callback>"
1259 : (isW
? debugstr_w(tvItem
->pszText
) : debugstr_a((LPSTR
)tvItem
->pszText
)))
1262 newItem
= TREEVIEW_AllocateItem(infoPtr
);
1263 if (newItem
== NULL
)
1266 newItem
->parent
= parentItem
;
1267 newItem
->iIntegral
= 1;
1268 newItem
->visibleOrder
= -1;
1270 if (!TREEVIEW_DoSetItemT(infoPtr
, newItem
, tvItem
, isW
))
1273 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1275 infoPtr
->uNumItems
++;
1277 switch ((DWORD_PTR
)insertAfter
)
1279 case (DWORD_PTR
)TVI_FIRST
:
1281 TREEVIEW_ITEM
*originalFirst
= parentItem
->firstChild
;
1282 TREEVIEW_InsertBefore(newItem
, parentItem
->firstChild
, parentItem
);
1283 if (infoPtr
->firstVisible
== originalFirst
)
1284 TREEVIEW_SetFirstVisible(infoPtr
, newItem
, TRUE
);
1288 case (DWORD_PTR
)TVI_LAST
:
1289 TREEVIEW_InsertAfter(newItem
, parentItem
->lastChild
, parentItem
);
1292 /* hInsertAfter names a specific item we want to insert after */
1294 TREEVIEW_InsertAfter(newItem
, insertAfter
, insertAfter
->parent
);
1297 case (DWORD_PTR
)TVI_SORT
:
1299 TREEVIEW_ITEM
*aChild
;
1300 TREEVIEW_ITEM
*previousChild
= NULL
;
1301 TREEVIEW_ITEM
*originalFirst
= parentItem
->firstChild
;
1302 BOOL bItemInserted
= FALSE
;
1304 aChild
= parentItem
->firstChild
;
1306 bTextUpdated
= TRUE
;
1307 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1309 /* Iterate the parent children to see where we fit in */
1310 while (aChild
!= NULL
)
1314 TREEVIEW_UpdateDispInfo(infoPtr
, aChild
, TVIF_TEXT
);
1315 comp
= lstrcmpW(newItem
->pszText
, aChild
->pszText
);
1317 if (comp
< 0) /* we are smaller than the current one */
1319 TREEVIEW_InsertBefore(newItem
, aChild
, parentItem
);
1320 if (infoPtr
->firstVisible
== originalFirst
&&
1321 aChild
== originalFirst
)
1322 TREEVIEW_SetFirstVisible(infoPtr
, newItem
, TRUE
);
1323 bItemInserted
= TRUE
;
1326 else if (comp
> 0) /* we are bigger than the current one */
1328 previousChild
= aChild
;
1330 /* This will help us to exit if there is no more sibling */
1331 aChild
= (aChild
->nextSibling
== 0)
1333 : aChild
->nextSibling
;
1335 /* Look at the next item */
1341 * An item with this name is already existing, therefore,
1342 * we add after the one we found
1344 TREEVIEW_InsertAfter(newItem
, aChild
, parentItem
);
1345 bItemInserted
= TRUE
;
1351 * we reach the end of the child list and the item has not
1352 * yet been inserted, therefore, insert it after the last child.
1354 if ((!bItemInserted
) && (aChild
== NULL
))
1355 TREEVIEW_InsertAfter(newItem
, previousChild
, parentItem
);
1362 TRACE("new item %p; parent %p, mask 0x%x\n", newItem
,
1363 newItem
->parent
, tvItem
->mask
);
1365 newItem
->iLevel
= newItem
->parent
->iLevel
+ 1;
1367 if (newItem
->parent
->cChildren
== 0)
1368 newItem
->parent
->cChildren
= 1;
1370 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
1372 if (STATEIMAGEINDEX(newItem
->state
) == 0)
1373 newItem
->state
|= INDEXTOSTATEIMAGEMASK(1);
1376 if (infoPtr
->firstVisible
== NULL
)
1377 infoPtr
->firstVisible
= newItem
;
1379 TREEVIEW_VerifyTree(infoPtr
);
1381 if (!infoPtr
->bRedraw
) return (LRESULT
)newItem
;
1383 if (parentItem
== infoPtr
->root
||
1384 (ISVISIBLE(parentItem
) && parentItem
->state
& TVIS_EXPANDED
))
1386 TREEVIEW_ITEM
*item
;
1387 TREEVIEW_ITEM
*prev
= TREEVIEW_GetPrevListItem(infoPtr
, newItem
);
1389 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1390 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, newItem
);
1393 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1395 TREEVIEW_ComputeTextWidth(infoPtr
, newItem
, 0);
1396 TREEVIEW_UpdateScrollBars(infoPtr
);
1398 * if the item was inserted in a visible part of the tree,
1399 * invalidate it, as well as those after it
1401 for (item
= newItem
;
1403 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
1404 TREEVIEW_Invalidate(infoPtr
, item
);
1408 /* refresh treeview if newItem is the first item inserted under parentItem */
1409 if (ISVISIBLE(parentItem
) && newItem
->prevSibling
== newItem
->nextSibling
)
1411 /* parent got '+' - update it */
1412 TREEVIEW_Invalidate(infoPtr
, parentItem
);
1416 return (LRESULT
)newItem
;
1419 /* Item Deletion ************************************************************/
1421 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
);
1424 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*parentItem
)
1426 TREEVIEW_ITEM
*kill
= parentItem
->firstChild
;
1428 while (kill
!= NULL
)
1430 TREEVIEW_ITEM
*next
= kill
->nextSibling
;
1432 TREEVIEW_RemoveItem(infoPtr
, kill
);
1437 assert(parentItem
->cChildren
<= 0); /* I_CHILDRENCALLBACK or 0 */
1438 assert(parentItem
->firstChild
== NULL
);
1439 assert(parentItem
->lastChild
== NULL
);
1443 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM
*item
)
1445 TREEVIEW_ITEM
*parentItem
;
1447 assert(item
!= NULL
);
1448 assert(item
->parent
!= NULL
); /* i.e. it must not be the root */
1450 parentItem
= item
->parent
;
1452 if (parentItem
->firstChild
== item
)
1453 parentItem
->firstChild
= item
->nextSibling
;
1455 if (parentItem
->lastChild
== item
)
1456 parentItem
->lastChild
= item
->prevSibling
;
1458 if (parentItem
->firstChild
== NULL
&& parentItem
->lastChild
== NULL
1459 && parentItem
->cChildren
> 0)
1460 parentItem
->cChildren
= 0;
1462 if (item
->prevSibling
)
1463 item
->prevSibling
->nextSibling
= item
->nextSibling
;
1465 if (item
->nextSibling
)
1466 item
->nextSibling
->prevSibling
= item
->prevSibling
;
1470 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
1472 TRACE("%p, (%s)\n", item
, TREEVIEW_ItemName(item
));
1474 if (item
->firstChild
)
1475 TREEVIEW_RemoveAllChildren(infoPtr
, item
);
1477 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_DELETEITEMW
, TVC_UNKNOWN
,
1478 TVIF_HANDLE
| TVIF_PARAM
, item
, 0);
1480 TREEVIEW_UnlinkItem(item
);
1482 infoPtr
->uNumItems
--;
1484 if (item
->pszText
!= LPSTR_TEXTCALLBACKW
)
1485 Free(item
->pszText
);
1487 TREEVIEW_FreeItem(infoPtr
, item
);
1491 /* Empty out the tree. */
1493 TREEVIEW_RemoveTree(TREEVIEW_INFO
*infoPtr
)
1495 TREEVIEW_RemoveAllChildren(infoPtr
, infoPtr
->root
);
1497 assert(infoPtr
->uNumItems
== 0); /* root isn't counted in uNumItems */
1501 TREEVIEW_DeleteItem(TREEVIEW_INFO
*infoPtr
, HTREEITEM item
)
1503 TREEVIEW_ITEM
*newSelection
= NULL
;
1504 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
1505 TREEVIEW_ITEM
*parent
, *prev
= NULL
;
1506 BOOL visible
= FALSE
;
1508 if (item
== TVI_ROOT
|| !item
)
1510 TRACE("TVI_ROOT\n");
1511 parent
= infoPtr
->root
;
1512 newSelection
= NULL
;
1514 TREEVIEW_RemoveTree(infoPtr
);
1518 if (!TREEVIEW_ValidItem(infoPtr
, item
))
1521 TRACE("%p (%s)\n", item
, TREEVIEW_ItemName(item
));
1522 parent
= item
->parent
;
1524 if (ISVISIBLE(item
))
1526 prev
= TREEVIEW_GetPrevListItem(infoPtr
, item
);
1530 if (infoPtr
->selectedItem
!= NULL
1531 && (item
== infoPtr
->selectedItem
1532 || TREEVIEW_IsChildOf(item
, infoPtr
->selectedItem
)))
1534 if (item
->nextSibling
)
1535 newSelection
= item
->nextSibling
;
1536 else if (item
->parent
!= infoPtr
->root
)
1537 newSelection
= item
->parent
;
1539 newSelection
= item
->prevSibling
;
1540 TRACE("newSelection = %p\n", newSelection
);
1543 if (infoPtr
->firstVisible
== item
)
1545 if (item
->nextSibling
)
1546 newFirstVisible
= item
->nextSibling
;
1547 else if (item
->prevSibling
)
1548 newFirstVisible
= item
->prevSibling
;
1549 else if (item
->parent
!= infoPtr
->root
)
1550 newFirstVisible
= item
->parent
;
1551 TREEVIEW_SetFirstVisible(infoPtr
, NULL
, TRUE
);
1554 newFirstVisible
= infoPtr
->firstVisible
;
1556 TREEVIEW_RemoveItem(infoPtr
, item
);
1559 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1560 if (!infoPtr
->selectedItem
&& newSelection
)
1562 if (TREEVIEW_ValidItem(infoPtr
, newSelection
))
1563 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
, TVC_UNKNOWN
);
1566 /* Validate insertMark dropItem.
1567 * hotItem ??? - used for comparison only.
1569 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->insertMarkItem
))
1570 infoPtr
->insertMarkItem
= 0;
1572 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->dropItem
))
1573 infoPtr
->dropItem
= 0;
1575 if (!TREEVIEW_ValidItem(infoPtr
, newFirstVisible
))
1576 newFirstVisible
= infoPtr
->root
->firstChild
;
1578 TREEVIEW_VerifyTree(infoPtr
);
1580 if (!infoPtr
->bRedraw
) return TRUE
;
1584 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
1585 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1586 TREEVIEW_UpdateScrollBars(infoPtr
);
1587 TREEVIEW_Invalidate(infoPtr
, NULL
);
1589 else if (ISVISIBLE(parent
) && !TREEVIEW_HasChildren(infoPtr
, parent
))
1591 /* parent lost '+/-' - update it */
1592 TREEVIEW_Invalidate(infoPtr
, parent
);
1599 /* Get/Set Messages *********************************************************/
1601 TREEVIEW_SetRedraw(TREEVIEW_INFO
* infoPtr
, WPARAM wParam
)
1603 infoPtr
->bRedraw
= wParam
!= 0;
1605 if (infoPtr
->bRedraw
)
1607 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1608 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1609 TREEVIEW_UpdateScrollBars(infoPtr
);
1610 TREEVIEW_Invalidate(infoPtr
, NULL
);
1616 TREEVIEW_GetIndent(const TREEVIEW_INFO
*infoPtr
)
1619 return infoPtr
->uIndent
;
1623 TREEVIEW_SetIndent(TREEVIEW_INFO
*infoPtr
, UINT newIndent
)
1627 if (newIndent
< MINIMUM_INDENT
)
1628 newIndent
= MINIMUM_INDENT
;
1630 if (infoPtr
->uIndent
!= newIndent
)
1632 infoPtr
->uIndent
= newIndent
;
1633 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1634 TREEVIEW_UpdateScrollBars(infoPtr
);
1635 TREEVIEW_Invalidate(infoPtr
, NULL
);
1643 TREEVIEW_GetToolTips(const TREEVIEW_INFO
*infoPtr
)
1646 return (LRESULT
)infoPtr
->hwndToolTip
;
1650 TREEVIEW_SetToolTips(TREEVIEW_INFO
*infoPtr
, HWND hwndTT
)
1655 prevToolTip
= infoPtr
->hwndToolTip
;
1656 infoPtr
->hwndToolTip
= hwndTT
;
1658 return (LRESULT
)prevToolTip
;
1662 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO
*infoPtr
, BOOL fUnicode
)
1664 BOOL rc
= infoPtr
->bNtfUnicode
;
1665 infoPtr
->bNtfUnicode
= fUnicode
;
1670 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO
*infoPtr
)
1672 return infoPtr
->bNtfUnicode
;
1676 TREEVIEW_GetScrollTime(const TREEVIEW_INFO
*infoPtr
)
1678 return infoPtr
->uScrollTime
;
1682 TREEVIEW_SetScrollTime(TREEVIEW_INFO
*infoPtr
, UINT uScrollTime
)
1684 UINT uOldScrollTime
= infoPtr
->uScrollTime
;
1686 infoPtr
->uScrollTime
= min(uScrollTime
, 100);
1688 return uOldScrollTime
;
1693 TREEVIEW_GetImageList(const TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
1700 return (LRESULT
)infoPtr
->himlNormal
;
1703 return (LRESULT
)infoPtr
->himlState
;
1710 #define TVHEIGHT_MIN 16
1711 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1713 /* Compute the natural height for items. */
1715 TREEVIEW_NaturalHeight(const TREEVIEW_INFO
*infoPtr
)
1719 HFONT hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
1722 /* Height is the maximum of:
1723 * 16 (a hack because our fonts are tiny), and
1724 * The text height + border & margin, and
1725 * The size of the normal image list
1727 GetTextMetricsW(hdc
, &tm
);
1728 SelectObject(hdc
, hOldFont
);
1731 height
= TVHEIGHT_MIN
;
1732 if (height
< tm
.tmHeight
+ tm
.tmExternalLeading
+ TVHEIGHT_FONT_ADJUST
)
1733 height
= tm
.tmHeight
+ tm
.tmExternalLeading
+ TVHEIGHT_FONT_ADJUST
;
1734 if (height
< infoPtr
->normalImageHeight
)
1735 height
= infoPtr
->normalImageHeight
;
1737 /* Round down, unless we support odd ("non even") heights. */
1738 if (!(infoPtr
->dwStyle
& TVS_NONEVENHEIGHT
))
1745 TREEVIEW_SetImageList(TREEVIEW_INFO
*infoPtr
, UINT type
, HIMAGELIST himlNew
)
1747 HIMAGELIST himlOld
= 0;
1748 int oldWidth
= infoPtr
->normalImageWidth
;
1749 int oldHeight
= infoPtr
->normalImageHeight
;
1751 TRACE("%u,%p\n", type
, himlNew
);
1756 himlOld
= infoPtr
->himlNormal
;
1757 infoPtr
->himlNormal
= himlNew
;
1760 ImageList_GetIconSize(himlNew
, &infoPtr
->normalImageWidth
,
1761 &infoPtr
->normalImageHeight
);
1764 infoPtr
->normalImageWidth
= 0;
1765 infoPtr
->normalImageHeight
= 0;
1771 himlOld
= infoPtr
->himlState
;
1772 infoPtr
->himlState
= himlNew
;
1775 ImageList_GetIconSize(himlNew
, &infoPtr
->stateImageWidth
,
1776 &infoPtr
->stateImageHeight
);
1779 infoPtr
->stateImageWidth
= 0;
1780 infoPtr
->stateImageHeight
= 0;
1786 ERR("unknown imagelist type %u\n", type
);
1789 if (oldWidth
!= infoPtr
->normalImageWidth
||
1790 oldHeight
!= infoPtr
->normalImageHeight
)
1792 BOOL bRecalcVisible
= FALSE
;
1794 if (oldHeight
!= infoPtr
->normalImageHeight
&&
1795 !infoPtr
->bHeightSet
)
1797 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1798 bRecalcVisible
= TRUE
;
1801 if (infoPtr
->normalImageWidth
> MINIMUM_INDENT
&&
1802 infoPtr
->normalImageWidth
!= infoPtr
->uIndent
)
1804 infoPtr
->uIndent
= infoPtr
->normalImageWidth
;
1805 bRecalcVisible
= TRUE
;
1809 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1811 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1812 TREEVIEW_UpdateScrollBars(infoPtr
);
1815 TREEVIEW_Invalidate(infoPtr
, NULL
);
1817 return (LRESULT
)himlOld
;
1821 TREEVIEW_SetItemHeight(TREEVIEW_INFO
*infoPtr
, INT newHeight
)
1823 INT prevHeight
= infoPtr
->uItemHeight
;
1825 TRACE("new=%d, old=%d\n", newHeight
, prevHeight
);
1826 if (newHeight
== -1)
1828 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1829 infoPtr
->bHeightSet
= FALSE
;
1833 if (newHeight
== 0) newHeight
= 1;
1834 infoPtr
->uItemHeight
= newHeight
;
1835 infoPtr
->bHeightSet
= TRUE
;
1838 /* Round down, unless we support odd ("non even") heights. */
1839 if (!(infoPtr
->dwStyle
& TVS_NONEVENHEIGHT
) && infoPtr
->uItemHeight
!= 1)
1841 infoPtr
->uItemHeight
&= ~1;
1842 TRACE("after rounding=%d\n", infoPtr
->uItemHeight
);
1845 if (infoPtr
->uItemHeight
!= prevHeight
)
1847 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1848 TREEVIEW_UpdateScrollBars(infoPtr
);
1849 TREEVIEW_Invalidate(infoPtr
, NULL
);
1856 TREEVIEW_GetItemHeight(const TREEVIEW_INFO
*infoPtr
)
1859 return infoPtr
->uItemHeight
;
1864 TREEVIEW_GetFont(const TREEVIEW_INFO
*infoPtr
)
1866 TRACE("%p\n", infoPtr
->hFont
);
1867 return (LRESULT
)infoPtr
->hFont
;
1872 TREEVIEW_ResetTextWidth(LPVOID pItem
, LPVOID unused
)
1876 ((TREEVIEW_ITEM
*)pItem
)->textWidth
= 0;
1882 TREEVIEW_SetFont(TREEVIEW_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
1884 UINT uHeight
= infoPtr
->uItemHeight
;
1886 TRACE("%p %i\n", hFont
, bRedraw
);
1888 infoPtr
->hFont
= hFont
? hFont
: infoPtr
->hDefaultFont
;
1890 DeleteObject(infoPtr
->hBoldFont
);
1891 DeleteObject(infoPtr
->hUnderlineFont
);
1892 DeleteObject(infoPtr
->hBoldUnderlineFont
);
1893 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
1894 infoPtr
->hUnderlineFont
= TREEVIEW_CreateUnderlineFont(infoPtr
->hFont
);
1895 infoPtr
->hBoldUnderlineFont
= TREEVIEW_CreateBoldUnderlineFont(infoPtr
->hFont
);
1897 if (!infoPtr
->bHeightSet
)
1898 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1900 if (uHeight
!= infoPtr
->uItemHeight
)
1901 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1903 DPA_EnumCallback(infoPtr
->items
, TREEVIEW_ResetTextWidth
, 0);
1905 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1906 TREEVIEW_UpdateScrollBars(infoPtr
);
1909 TREEVIEW_Invalidate(infoPtr
, NULL
);
1916 TREEVIEW_GetLineColor(const TREEVIEW_INFO
*infoPtr
)
1919 return (LRESULT
)infoPtr
->clrLine
;
1923 TREEVIEW_SetLineColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1925 COLORREF prevColor
= infoPtr
->clrLine
;
1928 infoPtr
->clrLine
= color
;
1929 return (LRESULT
)prevColor
;
1934 TREEVIEW_GetTextColor(const TREEVIEW_INFO
*infoPtr
)
1937 return (LRESULT
)infoPtr
->clrText
;
1941 TREEVIEW_SetTextColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1943 COLORREF prevColor
= infoPtr
->clrText
;
1946 infoPtr
->clrText
= color
;
1948 if (infoPtr
->clrText
!= prevColor
)
1949 TREEVIEW_Invalidate(infoPtr
, NULL
);
1951 return (LRESULT
)prevColor
;
1956 TREEVIEW_GetBkColor(const TREEVIEW_INFO
*infoPtr
)
1959 return (LRESULT
)infoPtr
->clrBk
;
1963 TREEVIEW_SetBkColor(TREEVIEW_INFO
*infoPtr
, COLORREF newColor
)
1965 COLORREF prevColor
= infoPtr
->clrBk
;
1968 infoPtr
->clrBk
= newColor
;
1970 if (newColor
!= prevColor
)
1971 TREEVIEW_Invalidate(infoPtr
, NULL
);
1973 return (LRESULT
)prevColor
;
1978 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO
*infoPtr
)
1981 return (LRESULT
)infoPtr
->clrInsertMark
;
1985 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1987 COLORREF prevColor
= infoPtr
->clrInsertMark
;
1989 TRACE("0x%08x\n", color
);
1990 infoPtr
->clrInsertMark
= color
;
1992 return (LRESULT
)prevColor
;
1997 TREEVIEW_SetInsertMark(TREEVIEW_INFO
*infoPtr
, BOOL wParam
, HTREEITEM item
)
1999 TRACE("%d %p\n", wParam
, item
);
2001 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2004 infoPtr
->insertBeforeorAfter
= wParam
;
2005 infoPtr
->insertMarkItem
= item
;
2007 TREEVIEW_Invalidate(infoPtr
, NULL
);
2013 /************************************************************************
2014 * Some serious braindamage here. lParam is a pointer to both the
2015 * input HTREEITEM and the output RECT.
2018 TREEVIEW_GetItemRect(const TREEVIEW_INFO
*infoPtr
, BOOL fTextRect
, LPRECT lpRect
)
2020 TREEVIEW_ITEM
*item
;
2021 const HTREEITEM
*pItem
= (HTREEITEM
*)lpRect
;
2029 if (!TREEVIEW_ValidItem(infoPtr
, item
) || !ISVISIBLE(item
))
2033 * If wParam is TRUE return the text size otherwise return
2034 * the whole item size
2038 /* Windows does not send TVN_GETDISPINFO here. */
2040 lpRect
->top
= item
->rect
.top
;
2041 lpRect
->bottom
= item
->rect
.bottom
;
2043 lpRect
->left
= item
->textOffset
;
2044 if (!item
->textWidth
)
2045 TREEVIEW_ComputeTextWidth(infoPtr
, item
, 0);
2047 lpRect
->right
= item
->textOffset
+ item
->textWidth
+ 4;
2051 *lpRect
= item
->rect
;
2054 TRACE("%s [%s]\n", fTextRect
? "text" : "item", wine_dbgstr_rect(lpRect
));
2059 static inline LRESULT
2060 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO
*infoPtr
)
2062 /* Surprise! This does not take integral height into account. */
2063 TRACE("client=%d, item=%d\n", infoPtr
->clientHeight
, infoPtr
->uItemHeight
);
2064 return infoPtr
->clientHeight
/ infoPtr
->uItemHeight
;
2069 TREEVIEW_GetItemT(const TREEVIEW_INFO
*infoPtr
, LPTVITEMEXW tvItem
, BOOL isW
)
2071 TREEVIEW_ITEM
*item
= tvItem
->hItem
;
2073 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2075 BOOL valid_item
= FALSE
;
2076 if (!item
) return FALSE
;
2080 infoPtr
= item
->infoPtr
;
2081 TRACE("got item from different tree %p, called from %p\n", item
->infoPtr
, infoPtr
);
2082 valid_item
= TREEVIEW_ValidItem(infoPtr
, item
);
2088 if (!valid_item
) return FALSE
;
2091 TREEVIEW_UpdateDispInfo(infoPtr
, item
, tvItem
->mask
);
2093 if (tvItem
->mask
& TVIF_CHILDREN
)
2095 if (item
->cChildren
==I_CHILDRENCALLBACK
)
2096 FIXME("I_CHILDRENCALLBACK not supported\n");
2097 tvItem
->cChildren
= item
->cChildren
;
2100 if (tvItem
->mask
& TVIF_HANDLE
)
2101 tvItem
->hItem
= item
;
2103 if (tvItem
->mask
& TVIF_IMAGE
)
2104 tvItem
->iImage
= item
->iImage
;
2106 if (tvItem
->mask
& TVIF_INTEGRAL
)
2107 tvItem
->iIntegral
= item
->iIntegral
;
2109 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2110 tvItem
->lParam
= item
->lParam
;
2112 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
2113 tvItem
->iSelectedImage
= item
->iSelectedImage
;
2115 if (tvItem
->mask
& TVIF_EXPANDEDIMAGE
)
2116 tvItem
->iExpandedImage
= item
->iExpandedImage
;
2118 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2119 tvItem
->state
= item
->state
;
2121 if (tvItem
->mask
& TVIF_TEXT
)
2123 if (item
->pszText
== NULL
)
2125 if (tvItem
->cchTextMax
> 0)
2126 tvItem
->pszText
[0] = '\0';
2130 if (item
->pszText
== LPSTR_TEXTCALLBACKW
)
2132 tvItem
->pszText
= LPSTR_TEXTCALLBACKW
;
2133 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2137 lstrcpynW(tvItem
->pszText
, item
->pszText
, tvItem
->cchTextMax
);
2142 if (item
->pszText
== LPSTR_TEXTCALLBACKW
)
2144 tvItem
->pszText
= (LPWSTR
)LPSTR_TEXTCALLBACKA
;
2145 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2149 WideCharToMultiByte(CP_ACP
, 0, item
->pszText
, -1,
2150 (LPSTR
)tvItem
->pszText
, tvItem
->cchTextMax
, NULL
, NULL
);
2155 if (tvItem
->mask
& TVIF_STATEEX
)
2157 FIXME("Extended item state not supported, returning 0.\n");
2158 tvItem
->uStateEx
= 0;
2161 TRACE("item <%p>, txt %p, img %d, mask 0x%x\n",
2162 item
, tvItem
->pszText
, tvItem
->iImage
, tvItem
->mask
);
2167 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2168 * which is wrong. */
2170 TREEVIEW_SetItemT(TREEVIEW_INFO
*infoPtr
, const TVITEMEXW
*tvItem
, BOOL isW
)
2172 TREEVIEW_ITEM
*item
;
2173 TREEVIEW_ITEM originalItem
;
2175 item
= tvItem
->hItem
;
2177 TRACE("item %d, mask 0x%x\n", TREEVIEW_GetItemIndex(infoPtr
, item
),
2180 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2183 /* store the original item values */
2184 originalItem
= *item
;
2186 if (!TREEVIEW_DoSetItemT(infoPtr
, item
, tvItem
, isW
))
2189 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2190 if ((tvItem
->mask
& TVIF_TEXT
2191 || (tvItem
->mask
& TVIF_STATE
&& tvItem
->stateMask
& TVIS_BOLD
))
2194 TREEVIEW_UpdateDispInfo(infoPtr
, item
, TVIF_TEXT
);
2195 TREEVIEW_ComputeTextWidth(infoPtr
, item
, 0);
2198 if (tvItem
->mask
!= 0 && ISVISIBLE(item
))
2200 /* The refresh updates everything, but we can't wait until then. */
2201 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, item
);
2203 /* if any of the item's values changed and it's not a callback, redraw the item */
2204 if (item_changed(&originalItem
, item
, tvItem
))
2206 if (tvItem
->mask
& TVIF_INTEGRAL
)
2208 TREEVIEW_RecalculateVisibleOrder(infoPtr
, item
);
2209 TREEVIEW_UpdateScrollBars(infoPtr
);
2211 TREEVIEW_Invalidate(infoPtr
, NULL
);
2215 TREEVIEW_UpdateScrollBars(infoPtr
);
2216 TREEVIEW_Invalidate(infoPtr
, item
);
2225 TREEVIEW_GetItemState(const TREEVIEW_INFO
*infoPtr
, HTREEITEM item
, UINT mask
)
2229 if (!item
|| !TREEVIEW_ValidItem(infoPtr
, item
))
2232 return (item
->state
& mask
);
2236 TREEVIEW_GetNextItem(const TREEVIEW_INFO
*infoPtr
, UINT which
, HTREEITEM item
)
2238 TREEVIEW_ITEM
*retval
;
2242 /* handle all the global data here */
2245 case TVGN_CHILD
: /* Special case: child of 0 is root */
2250 retval
= infoPtr
->root
->firstChild
;
2254 retval
= infoPtr
->selectedItem
;
2257 case TVGN_FIRSTVISIBLE
:
2258 retval
= infoPtr
->firstVisible
;
2261 case TVGN_DROPHILITE
:
2262 retval
= infoPtr
->dropItem
;
2265 case TVGN_LASTVISIBLE
:
2266 retval
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
2272 TRACE("flags:0x%x, returns %p\n", which
, retval
);
2273 return (LRESULT
)retval
;
2276 if (item
== TVI_ROOT
) item
= infoPtr
->root
;
2278 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2284 retval
= item
->nextSibling
;
2287 retval
= item
->prevSibling
;
2290 retval
= (item
->parent
!= infoPtr
->root
) ? item
->parent
: NULL
;
2293 retval
= item
->firstChild
;
2295 case TVGN_NEXTVISIBLE
:
2296 retval
= TREEVIEW_GetNextListItem(infoPtr
, item
);
2298 case TVGN_PREVIOUSVISIBLE
:
2299 retval
= TREEVIEW_GetPrevListItem(infoPtr
, item
);
2302 TRACE("Unknown msg 0x%x, item %p\n", which
, item
);
2306 TRACE("flags: 0x%x, item %p;returns %p\n", which
, item
, retval
);
2307 return (LRESULT
)retval
;
2312 TREEVIEW_GetCount(const TREEVIEW_INFO
*infoPtr
)
2314 TRACE(" %d\n", infoPtr
->uNumItems
);
2315 return (LRESULT
)infoPtr
->uNumItems
;
2319 TREEVIEW_ToggleItemState(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2321 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
2323 static const unsigned int state_table
[] = { 0, 2, 1 };
2327 state
= STATEIMAGEINDEX(item
->state
);
2328 TRACE("state: 0x%x\n", state
);
2329 item
->state
&= ~TVIS_STATEIMAGEMASK
;
2332 state
= state_table
[state
];
2334 item
->state
|= INDEXTOSTATEIMAGEMASK(state
);
2336 TRACE("state: 0x%x\n", state
);
2337 TREEVIEW_Invalidate(infoPtr
, item
);
2342 /* Painting *************************************************************/
2344 /* Draw the lines and expand button for an item. Also draws one section
2345 * of the line from item's parent to item's parent's next sibling. */
2347 TREEVIEW_DrawItemLines(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, const TREEVIEW_ITEM
*item
)
2349 LONG centerx
, centery
;
2350 BOOL lar
= ((infoPtr
->dwStyle
2351 & (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
2354 COLORREF clrBk
= GETBKCOLOR(infoPtr
->clrBk
);
2356 if (!lar
&& item
->iLevel
== 0)
2359 hbr
= CreateSolidBrush(clrBk
);
2360 hbrOld
= SelectObject(hdc
, hbr
);
2362 centerx
= (item
->linesOffset
+ item
->stateOffset
) / 2;
2363 centery
= (item
->rect
.top
+ item
->rect
.bottom
) / 2;
2365 if (infoPtr
->dwStyle
& TVS_HASLINES
)
2367 HPEN hOldPen
, hNewPen
;
2371 /* Get a dotted grey pen */
2372 lb
.lbStyle
= BS_SOLID
;
2373 lb
.lbColor
= GETLINECOLOR(infoPtr
->clrLine
);
2374 hNewPen
= ExtCreatePen(PS_COSMETIC
|PS_ALTERNATE
, 1, &lb
, 0, NULL
);
2375 hOldPen
= SelectObject(hdc
, hNewPen
);
2377 /* Make sure the center is on a dot (using +2 instead
2378 * of +1 gives us pixel-by-pixel compat with native) */
2379 centery
= (centery
+ 2) & ~1;
2381 MoveToEx(hdc
, item
->stateOffset
, centery
, NULL
);
2382 LineTo(hdc
, centerx
- 1, centery
);
2384 if (item
->prevSibling
|| item
->parent
!= infoPtr
->root
)
2386 MoveToEx(hdc
, centerx
, item
->rect
.top
, NULL
);
2387 LineTo(hdc
, centerx
, centery
);
2390 if (item
->nextSibling
)
2392 MoveToEx(hdc
, centerx
, centery
, NULL
);
2393 LineTo(hdc
, centerx
, item
->rect
.bottom
+ 1);
2396 /* Draw the line from our parent to its next sibling. */
2397 parent
= item
->parent
;
2398 while (parent
!= infoPtr
->root
)
2400 int pcenterx
= (parent
->linesOffset
+ parent
->stateOffset
) / 2;
2402 if (parent
->nextSibling
2403 /* skip top-levels unless TVS_LINESATROOT */
2404 && parent
->stateOffset
> parent
->linesOffset
)
2406 MoveToEx(hdc
, pcenterx
, item
->rect
.top
, NULL
);
2407 LineTo(hdc
, pcenterx
, item
->rect
.bottom
+ 1);
2410 parent
= parent
->parent
;
2413 SelectObject(hdc
, hOldPen
);
2414 DeleteObject(hNewPen
);
2418 * Display the (+/-) signs
2421 if (infoPtr
->dwStyle
& TVS_HASBUTTONS
)
2423 if (item
->cChildren
)
2425 HTHEME theme
= GetWindowTheme(infoPtr
->hwnd
);
2428 RECT glyphRect
= item
->rect
;
2429 glyphRect
.left
= item
->linesOffset
;
2430 glyphRect
.right
= item
->stateOffset
;
2431 DrawThemeBackground (theme
, hdc
, TVP_GLYPH
,
2432 (item
->state
& TVIS_EXPANDED
) ? GLPS_OPENED
: GLPS_CLOSED
,
2437 LONG height
= item
->rect
.bottom
- item
->rect
.top
;
2438 LONG width
= item
->stateOffset
- item
->linesOffset
;
2439 LONG rectsize
= min(height
, width
) / 4;
2440 /* plussize = ceil(rectsize * 3/4) */
2441 LONG plussize
= (rectsize
+ 1) * 3 / 4;
2443 HPEN new_pen
= CreatePen(PS_SOLID
, 0, GETLINECOLOR(infoPtr
->clrLine
));
2444 HPEN old_pen
= SelectObject(hdc
, new_pen
);
2446 Rectangle(hdc
, centerx
- rectsize
- 1, centery
- rectsize
- 1,
2447 centerx
+ rectsize
+ 2, centery
+ rectsize
+ 2);
2449 SelectObject(hdc
, old_pen
);
2450 DeleteObject(new_pen
);
2452 /* draw +/- signs with current text color */
2453 new_pen
= CreatePen(PS_SOLID
, 0, GETTXTCOLOR(infoPtr
->clrText
));
2454 old_pen
= SelectObject(hdc
, new_pen
);
2456 if (height
< 18 || width
< 18)
2458 MoveToEx(hdc
, centerx
- plussize
+ 1, centery
, NULL
);
2459 LineTo(hdc
, centerx
+ plussize
, centery
);
2461 if (!(item
->state
& TVIS_EXPANDED
) ||
2462 (item
->state
& TVIS_EXPANDPARTIAL
))
2464 MoveToEx(hdc
, centerx
, centery
- plussize
+ 1, NULL
);
2465 LineTo(hdc
, centerx
, centery
+ plussize
);
2470 Rectangle(hdc
, centerx
- plussize
+ 1, centery
- 1,
2471 centerx
+ plussize
, centery
+ 2);
2473 if (!(item
->state
& TVIS_EXPANDED
) ||
2474 (item
->state
& TVIS_EXPANDPARTIAL
))
2476 Rectangle(hdc
, centerx
- 1, centery
- plussize
+ 1,
2477 centerx
+ 2, centery
+ plussize
);
2478 SetPixel(hdc
, centerx
- 1, centery
, clrBk
);
2479 SetPixel(hdc
, centerx
+ 1, centery
, clrBk
);
2483 SelectObject(hdc
, old_pen
);
2484 DeleteObject(new_pen
);
2488 SelectObject(hdc
, hbrOld
);
2493 TREEVIEW_DrawItem(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, TREEVIEW_ITEM
*item
)
2497 COLORREF oldTextColor
, oldTextBkColor
;
2499 BOOL inFocus
= (GetFocus() == infoPtr
->hwnd
);
2500 NMTVCUSTOMDRAW nmcdhdr
;
2502 TREEVIEW_UpdateDispInfo(infoPtr
, item
, CALLBACK_MASK_ALL
);
2504 /* - If item is drop target or it is selected and window is in focus -
2505 * use blue background (COLOR_HIGHLIGHT).
2506 * - If item is selected, window is not in focus, but it has style
2507 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2508 * - Otherwise - use background color
2510 if ((item
->state
& TVIS_DROPHILITED
) || ((item
== infoPtr
->focusedItem
) && !(item
->state
& TVIS_SELECTED
)) ||
2511 ((item
->state
& TVIS_SELECTED
) && (!infoPtr
->focusedItem
|| item
== infoPtr
->focusedItem
) &&
2512 (inFocus
|| (infoPtr
->dwStyle
& TVS_SHOWSELALWAYS
))))
2514 if ((item
->state
& TVIS_DROPHILITED
) || inFocus
)
2516 nmcdhdr
.clrTextBk
= comctl32_color
.clrHighlight
;
2517 nmcdhdr
.clrText
= comctl32_color
.clrHighlightText
;
2521 nmcdhdr
.clrTextBk
= comctl32_color
.clrBtnFace
;
2522 nmcdhdr
.clrText
= GETTXTCOLOR(infoPtr
->clrText
);
2527 nmcdhdr
.clrTextBk
= GETBKCOLOR(infoPtr
->clrBk
);
2528 if ((infoPtr
->dwStyle
& TVS_TRACKSELECT
) && (item
== infoPtr
->hotItem
))
2529 nmcdhdr
.clrText
= comctl32_color
.clrHighlight
;
2531 nmcdhdr
.clrText
= GETTXTCOLOR(infoPtr
->clrText
);
2534 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, item
));
2536 /* The custom draw handler can query the text rectangle,
2538 /* should already be known, set to 0 when changed */
2539 if (!item
->textWidth
)
2540 TREEVIEW_ComputeTextWidth(infoPtr
, item
, hdc
);
2544 if (infoPtr
->cdmode
& CDRF_NOTIFYITEMDRAW
)
2546 cditem
= TREEVIEW_SendCustomDrawItemNotify
2547 (infoPtr
, hdc
, item
, CDDS_ITEMPREPAINT
, &nmcdhdr
);
2548 TRACE("prepaint:cditem-app returns 0x%x\n", cditem
);
2550 if (cditem
& CDRF_SKIPDEFAULT
)
2552 SelectObject(hdc
, hOldFont
);
2557 if (cditem
& CDRF_NEWFONT
)
2558 TREEVIEW_ComputeTextWidth(infoPtr
, item
, hdc
);
2560 TREEVIEW_DrawItemLines(infoPtr
, hdc
, item
);
2562 /* Set colors. Custom draw handler can change these so we do this after it. */
2563 oldTextColor
= SetTextColor(hdc
, nmcdhdr
.clrText
);
2564 oldTextBkColor
= SetBkColor(hdc
, nmcdhdr
.clrTextBk
);
2566 centery
= (item
->rect
.top
+ item
->rect
.bottom
) / 2;
2569 * Display the images associated with this item
2574 /* State images are displayed to the left of the Normal image
2575 * image number is in state; zero should be `display no image'.
2577 imageIndex
= STATEIMAGEINDEX(item
->state
);
2579 if (infoPtr
->himlState
&& imageIndex
)
2581 ImageList_Draw(infoPtr
->himlState
, imageIndex
, hdc
,
2583 centery
- infoPtr
->stateImageHeight
/ 2,
2587 /* Now, draw the normal image; can be either selected,
2588 * non-selected or expanded image.
2591 if ((item
->state
& TVIS_SELECTED
) && (item
->iSelectedImage
>= 0))
2593 /* The item is currently selected */
2594 imageIndex
= item
->iSelectedImage
;
2596 else if ((item
->state
& TVIS_EXPANDED
) && (item
->iExpandedImage
!= (WORD
)I_IMAGENONE
))
2598 /* The item is currently not selected but expanded */
2599 imageIndex
= item
->iExpandedImage
;
2603 /* The item is not selected and not expanded */
2604 imageIndex
= item
->iImage
;
2607 if (infoPtr
->himlNormal
)
2609 UINT style
= item
->state
& TVIS_CUT
? ILD_SELECTED
: ILD_NORMAL
;
2611 style
|= item
->state
& TVIS_OVERLAYMASK
;
2613 ImageList_DrawEx(infoPtr
->himlNormal
, imageIndex
, hdc
,
2614 item
->imageOffset
, centery
- infoPtr
->normalImageHeight
/ 2,
2615 0, 0, infoPtr
->clrBk
, item
->state
& TVIS_CUT
? GETBKCOLOR(infoPtr
->clrBk
) : CLR_DEFAULT
,
2622 * Display the text associated with this item
2625 /* Don't paint item's text if it's being edited */
2626 if (!infoPtr
->hwndEdit
|| (infoPtr
->selectedItem
!= item
))
2634 rcText
.top
= item
->rect
.top
;
2635 rcText
.bottom
= item
->rect
.bottom
;
2636 rcText
.left
= item
->textOffset
;
2637 rcText
.right
= rcText
.left
+ item
->textWidth
+ 4;
2639 TRACE("drawing text %s at (%s)\n",
2640 debugstr_w(item
->pszText
), wine_dbgstr_rect(&rcText
));
2643 GetTextExtentPoint32W(hdc
, item
->pszText
, strlenW(item
->pszText
), &sz
);
2645 align
= SetTextAlign(hdc
, TA_LEFT
| TA_TOP
);
2646 ExtTextOutW(hdc
, rcText
.left
+ 2, (rcText
.top
+ rcText
.bottom
- sz
.cy
) / 2,
2647 ETO_CLIPPED
| ETO_OPAQUE
,
2650 lstrlenW(item
->pszText
),
2652 SetTextAlign(hdc
, align
);
2654 /* Draw focus box around the selected item */
2655 if ((item
== infoPtr
->selectedItem
) && inFocus
)
2657 DrawFocusRect(hdc
,&rcText
);
2662 /* Draw insertion mark if necessary */
2664 if (infoPtr
->insertMarkItem
)
2665 TRACE("item:%d,mark:%p\n",
2666 TREEVIEW_GetItemIndex(infoPtr
, item
),
2667 infoPtr
->insertMarkItem
);
2669 if (item
== infoPtr
->insertMarkItem
)
2671 HPEN hNewPen
, hOldPen
;
2675 hNewPen
= CreatePen(PS_SOLID
, 2, GETINSCOLOR(infoPtr
->clrInsertMark
));
2676 hOldPen
= SelectObject(hdc
, hNewPen
);
2678 if (infoPtr
->insertBeforeorAfter
)
2679 offset
= item
->rect
.bottom
- 1;
2681 offset
= item
->rect
.top
+ 1;
2683 left
= item
->textOffset
- 2;
2684 right
= item
->textOffset
+ item
->textWidth
+ 2;
2686 MoveToEx(hdc
, left
, offset
- 3, NULL
);
2687 LineTo(hdc
, left
, offset
+ 4);
2689 MoveToEx(hdc
, left
, offset
, NULL
);
2690 LineTo(hdc
, right
+ 1, offset
);
2692 MoveToEx(hdc
, right
, offset
+ 3, NULL
);
2693 LineTo(hdc
, right
, offset
- 4);
2695 SelectObject(hdc
, hOldPen
);
2696 DeleteObject(hNewPen
);
2699 /* Restore the hdc state */
2700 SetTextColor(hdc
, oldTextColor
);
2701 SetBkColor(hdc
, oldTextBkColor
);
2702 SelectObject(hdc
, hOldFont
);
2704 if (cditem
& CDRF_NOTIFYPOSTPAINT
)
2706 cditem
= TREEVIEW_SendCustomDrawItemNotify
2707 (infoPtr
, hdc
, item
, CDDS_ITEMPOSTPAINT
, &nmcdhdr
);
2708 TRACE("postpaint:cditem-app returns 0x%x\n", cditem
);
2712 /* Computes treeHeight and treeWidth and updates the scroll bars.
2715 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
)
2717 TREEVIEW_ITEM
*item
;
2718 HWND hwnd
= infoPtr
->hwnd
;
2722 LONG scrollX
= infoPtr
->scrollX
;
2724 infoPtr
->treeWidth
= 0;
2725 infoPtr
->treeHeight
= 0;
2727 /* We iterate through all visible items in order to get the tree height
2729 item
= infoPtr
->root
->firstChild
;
2731 while (item
!= NULL
)
2733 if (ISVISIBLE(item
))
2735 /* actually we draw text at textOffset + 2 */
2736 if (2+item
->textOffset
+item
->textWidth
> infoPtr
->treeWidth
)
2737 infoPtr
->treeWidth
= item
->textOffset
+item
->textWidth
+2;
2739 /* This is scroll-adjusted, but we fix this below. */
2740 infoPtr
->treeHeight
= item
->rect
.bottom
;
2743 item
= TREEVIEW_GetNextListItem(infoPtr
, item
);
2746 /* Fix the scroll adjusted treeHeight and treeWidth. */
2747 if (infoPtr
->root
->firstChild
)
2748 infoPtr
->treeHeight
-= infoPtr
->root
->firstChild
->rect
.top
;
2750 infoPtr
->treeWidth
+= infoPtr
->scrollX
;
2752 if (infoPtr
->dwStyle
& TVS_NOSCROLL
) return;
2754 /* Adding one scroll bar may take up enough space that it forces us
2755 * to add the other as well. */
2756 if (infoPtr
->treeHeight
> infoPtr
->clientHeight
)
2760 if (infoPtr
->treeWidth
2761 > infoPtr
->clientWidth
- GetSystemMetrics(SM_CXVSCROLL
))
2764 else if (infoPtr
->treeWidth
> infoPtr
->clientWidth
|| infoPtr
->scrollX
> 0)
2767 if (!vert
&& horz
&& infoPtr
->treeHeight
2768 > infoPtr
->clientHeight
- GetSystemMetrics(SM_CYVSCROLL
))
2771 if (horz
&& (infoPtr
->dwStyle
& TVS_NOHSCROLL
)) horz
= FALSE
;
2773 si
.cbSize
= sizeof(SCROLLINFO
);
2774 si
.fMask
= SIF_POS
|SIF_RANGE
|SIF_PAGE
;
2779 si
.nPage
= TREEVIEW_GetVisibleCount(infoPtr
);
2780 if ( si
.nPage
&& NULL
!= infoPtr
->firstVisible
)
2782 si
.nPos
= infoPtr
->firstVisible
->visibleOrder
;
2783 si
.nMax
= infoPtr
->maxVisibleOrder
- 1;
2785 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
2787 if (!(infoPtr
->uInternalStatus
& TV_VSCROLL
))
2788 ShowScrollBar(hwnd
, SB_VERT
, TRUE
);
2789 infoPtr
->uInternalStatus
|= TV_VSCROLL
;
2793 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2794 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2795 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2800 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2801 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2802 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2807 si
.nPage
= infoPtr
->clientWidth
;
2808 si
.nPos
= infoPtr
->scrollX
;
2809 si
.nMax
= infoPtr
->treeWidth
- 1;
2811 if (si
.nPos
> si
.nMax
- max( si
.nPage
-1, 0 ))
2813 si
.nPos
= si
.nMax
- max( si
.nPage
-1, 0 );
2817 if (!(infoPtr
->uInternalStatus
& TV_HSCROLL
))
2818 ShowScrollBar(hwnd
, SB_HORZ
, TRUE
);
2819 infoPtr
->uInternalStatus
|= TV_HSCROLL
;
2821 SetScrollInfo(hwnd
, SB_HORZ
, &si
, TRUE
);
2822 TREEVIEW_HScroll(infoPtr
,
2823 MAKEWPARAM(SB_THUMBPOSITION
, scrollX
));
2827 if (infoPtr
->uInternalStatus
& TV_HSCROLL
)
2828 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
2829 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2832 if (infoPtr
->scrollX
!= 0)
2834 TREEVIEW_HScroll(infoPtr
,
2835 MAKEWPARAM(SB_THUMBPOSITION
, scrollX
));
2840 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2844 TREEVIEW_FillBkgnd(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, const RECT
*rc
)
2847 COLORREF clrBk
= GETBKCOLOR(infoPtr
->clrBk
);
2849 hBrush
= CreateSolidBrush(clrBk
);
2850 FillRect(hdc
, rc
, hBrush
);
2851 DeleteObject(hBrush
);
2854 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2856 TREEVIEW_EraseBackground(const TREEVIEW_INFO
*infoPtr
, HDC hdc
)
2860 TRACE("%p\n", infoPtr
);
2862 GetClientRect(infoPtr
->hwnd
, &rect
);
2863 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rect
);
2869 TREEVIEW_Refresh(TREEVIEW_INFO
*infoPtr
, HDC hdc
, const RECT
*rc
)
2871 HWND hwnd
= infoPtr
->hwnd
;
2873 TREEVIEW_ITEM
*item
;
2875 if (infoPtr
->clientHeight
== 0 || infoPtr
->clientWidth
== 0)
2877 TRACE("empty window\n");
2881 infoPtr
->cdmode
= TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_PREPAINT
,
2884 if (infoPtr
->cdmode
== CDRF_SKIPDEFAULT
)
2886 ReleaseDC(hwnd
, hdc
);
2890 for (item
= infoPtr
->root
->firstChild
;
2892 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
2894 if (ISVISIBLE(item
))
2896 /* Avoid unneeded calculations */
2897 if (item
->rect
.top
> rect
.bottom
)
2899 if (item
->rect
.bottom
< rect
.top
)
2902 TREEVIEW_DrawItem(infoPtr
, hdc
, item
);
2906 TREEVIEW_UpdateScrollBars(infoPtr
);
2908 if (infoPtr
->cdmode
& CDRF_NOTIFYPOSTPAINT
)
2910 TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_POSTPAINT
, hdc
, rect
);
2914 TREEVIEW_InvalidateItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
2916 if (item
) InvalidateRect(infoPtr
->hwnd
, &item
->rect
, TRUE
);
2920 TREEVIEW_Invalidate(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
2923 InvalidateRect(infoPtr
->hwnd
, &item
->rect
, TRUE
);
2925 InvalidateRect(infoPtr
->hwnd
, NULL
, TRUE
);
2929 TREEVIEW_InitCheckboxes(TREEVIEW_INFO
*infoPtr
)
2932 HBITMAP hbm
, hbmOld
;
2936 infoPtr
->himlState
= ImageList_Create(16, 16, ILC_COLOR
| ILC_MASK
, 3, 0);
2938 hdcScreen
= GetDC(0);
2940 hdc
= CreateCompatibleDC(hdcScreen
);
2941 hbm
= CreateCompatibleBitmap(hdcScreen
, 48, 16);
2942 hbmOld
= SelectObject(hdc
, hbm
);
2944 SetRect(&rc
, 0, 0, 48, 16);
2945 FillRect(hdc
, &rc
, (HBRUSH
)(COLOR_WINDOW
+1));
2947 SetRect(&rc
, 18, 2, 30, 14);
2948 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
2949 DFCS_BUTTONCHECK
|DFCS_FLAT
);
2951 SetRect(&rc
, 34, 2, 46, 14);
2952 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
2953 DFCS_BUTTONCHECK
|DFCS_FLAT
|DFCS_CHECKED
);
2955 SelectObject(hdc
, hbmOld
);
2956 nIndex
= ImageList_AddMasked(infoPtr
->himlState
, hbm
,
2957 comctl32_color
.clrWindow
);
2958 TRACE("checkbox index %d\n", nIndex
);
2962 ReleaseDC(0, hdcScreen
);
2964 infoPtr
->stateImageWidth
= 16;
2965 infoPtr
->stateImageHeight
= 16;
2969 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2971 TREEVIEW_ITEM
*child
= item
->firstChild
;
2973 item
->state
&= ~TVIS_STATEIMAGEMASK
;
2974 item
->state
|= INDEXTOSTATEIMAGEMASK(1);
2978 TREEVIEW_ITEM
*next
= child
->nextSibling
;
2979 TREEVIEW_ResetImageStateIndex(infoPtr
, child
);
2985 TREEVIEW_Paint(TREEVIEW_INFO
*infoPtr
, HDC hdc_ref
)
2991 TRACE("(%p %p)\n", infoPtr
, hdc_ref
);
2993 if ((infoPtr
->dwStyle
& TVS_CHECKBOXES
) && !infoPtr
->himlState
)
2995 TREEVIEW_InitCheckboxes(infoPtr
);
2996 TREEVIEW_ResetImageStateIndex(infoPtr
, infoPtr
->root
);
2998 TREEVIEW_EndEditLabelNow(infoPtr
, TRUE
);
2999 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
3000 TREEVIEW_UpdateScrollBars(infoPtr
);
3001 TREEVIEW_Invalidate(infoPtr
, NULL
);
3007 GetClientRect(infoPtr
->hwnd
, &rc
);
3008 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rc
);
3012 hdc
= BeginPaint(infoPtr
->hwnd
, &ps
);
3015 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rc
);
3018 if(infoPtr
->bRedraw
) /* WM_SETREDRAW sets bRedraw */
3019 TREEVIEW_Refresh(infoPtr
, hdc
, &rc
);
3022 EndPaint(infoPtr
->hwnd
, &ps
);
3028 TREEVIEW_PrintClient(TREEVIEW_INFO
*infoPtr
, HDC hdc
, DWORD options
)
3030 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc
, options
);
3032 if ((options
& PRF_CHECKVISIBLE
) && !IsWindowVisible(infoPtr
->hwnd
))
3035 if (options
& PRF_ERASEBKGND
)
3036 TREEVIEW_EraseBackground(infoPtr
, hdc
);
3038 if (options
& PRF_CLIENT
)
3041 GetClientRect(infoPtr
->hwnd
, &rc
);
3042 TREEVIEW_Refresh(infoPtr
, hdc
, &rc
);
3048 /* Sorting **************************************************************/
3050 /***************************************************************************
3051 * Forward the DPA local callback to the treeview owner callback
3054 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM
*first
, const TREEVIEW_ITEM
*second
,
3055 const TVSORTCB
*pCallBackSort
)
3057 /* Forward the call to the client-defined callback */
3058 return pCallBackSort
->lpfnCompare(first
->lParam
,
3060 pCallBackSort
->lParam
);
3063 /***************************************************************************
3064 * Treeview native sort routine: sort on item text.
3067 TREEVIEW_SortOnName(TREEVIEW_ITEM
*first
, TREEVIEW_ITEM
*second
,
3068 const TREEVIEW_INFO
*infoPtr
)
3070 TREEVIEW_UpdateDispInfo(infoPtr
, first
, TVIF_TEXT
);
3071 TREEVIEW_UpdateDispInfo(infoPtr
, second
, TVIF_TEXT
);
3073 if(first
->pszText
&& second
->pszText
)
3074 return lstrcmpiW(first
->pszText
, second
->pszText
);
3075 else if(first
->pszText
)
3077 else if(second
->pszText
)
3083 /* Returns the number of physical children belonging to item. */
3085 TREEVIEW_CountChildren(const TREEVIEW_ITEM
*item
)
3090 for (hti
= item
->firstChild
; hti
!= NULL
; hti
= hti
->nextSibling
)
3096 /* Returns a DPA containing a pointer to each physical child of item in
3097 * sibling order. If item has no children, an empty DPA is returned. */
3099 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM
*item
)
3103 HDPA list
= DPA_Create(8);
3104 if (list
== 0) return NULL
;
3106 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
3108 if (DPA_InsertPtr(list
, INT_MAX
, child
) == -1)
3118 /***************************************************************************
3119 * Setup the treeview structure with regards of the sort method
3120 * and sort the children of the TV item specified in lParam
3121 * fRecurse: currently unused. Should be zero.
3122 * parent: if pSort!=NULL, should equal pSort->hParent.
3123 * otherwise, item which child items are to be sorted.
3124 * pSort: sort method info. if NULL, sort on item text.
3125 * if non-NULL, sort on item's lParam content, and let the
3126 * application decide what that means. See also TVM_SORTCHILDRENCB.
3130 TREEVIEW_Sort(TREEVIEW_INFO
*infoPtr
, HTREEITEM parent
,
3134 PFNDPACOMPARE pfnCompare
;
3137 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3138 if (parent
== TVI_ROOT
|| parent
== NULL
)
3139 parent
= infoPtr
->root
;
3141 /* Check for a valid handle to the parent item */
3142 if (!TREEVIEW_ValidItem(infoPtr
, parent
))
3144 ERR("invalid item hParent=%p\n", parent
);
3150 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_CallBackCompare
;
3151 lpCompare
= (LPARAM
)pSort
;
3155 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_SortOnName
;
3156 lpCompare
= (LPARAM
)infoPtr
;
3159 cChildren
= TREEVIEW_CountChildren(parent
);
3161 /* Make sure there is something to sort */
3164 /* TREEVIEW_ITEM rechaining */
3167 HTREEITEM nextItem
= 0;
3168 HTREEITEM prevItem
= 0;
3170 HDPA sortList
= TREEVIEW_BuildChildDPA(parent
);
3172 if (sortList
== NULL
)
3175 /* let DPA sort the list */
3176 DPA_Sort(sortList
, pfnCompare
, lpCompare
);
3178 /* The order of DPA entries has been changed, so fixup the
3179 * nextSibling and prevSibling pointers. */
3181 item
= DPA_GetPtr(sortList
, count
++);
3182 while ((nextItem
= DPA_GetPtr(sortList
, count
++)) != NULL
)
3184 /* link the two current item together */
3185 item
->nextSibling
= nextItem
;
3186 nextItem
->prevSibling
= item
;
3188 if (prevItem
== NULL
)
3190 /* this is the first item, update the parent */
3191 parent
->firstChild
= item
;
3192 item
->prevSibling
= NULL
;
3196 /* fix the back chaining */
3197 item
->prevSibling
= prevItem
;
3200 /* get ready for the next one */
3205 /* the last item is pointed to by item and never has a sibling */
3206 item
->nextSibling
= NULL
;
3207 parent
->lastChild
= item
;
3209 DPA_Destroy(sortList
);
3211 TREEVIEW_VerifyTree(infoPtr
);
3213 if (parent
->state
& TVIS_EXPANDED
)
3215 int visOrder
= infoPtr
->firstVisible
->visibleOrder
;
3217 if (parent
== infoPtr
->root
)
3218 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
3220 TREEVIEW_RecalculateVisibleOrder(infoPtr
, parent
);
3222 if (TREEVIEW_IsChildOf(parent
, infoPtr
->firstVisible
))
3224 TREEVIEW_ITEM
*item
;
3226 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
3227 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
3229 if (item
->visibleOrder
== visOrder
)
3233 if (!item
) item
= parent
->firstChild
;
3234 TREEVIEW_SetFirstVisible(infoPtr
, item
, FALSE
);
3237 TREEVIEW_Invalidate(infoPtr
, NULL
);
3246 /***************************************************************************
3247 * Setup the treeview structure with regards of the sort method
3248 * and sort the children of the TV item specified in lParam
3251 TREEVIEW_SortChildrenCB(TREEVIEW_INFO
*infoPtr
, LPTVSORTCB pSort
)
3253 return TREEVIEW_Sort(infoPtr
, pSort
->hParent
, pSort
);
3257 /***************************************************************************
3258 * Sort the children of the TV item specified in lParam.
3261 TREEVIEW_SortChildren(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
3263 return TREEVIEW_Sort(infoPtr
, (HTREEITEM
)lParam
, NULL
);
3267 /* Expansion/Collapse ***************************************************/
3270 TREEVIEW_SendExpanding(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
3273 return !TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_ITEMEXPANDINGW
, action
,
3274 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
3275 | TVIF_IMAGE
| TVIF_SELECTEDIMAGE
,
3280 TREEVIEW_SendExpanded(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
3283 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_ITEMEXPANDEDW
, action
,
3284 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
3285 | TVIF_IMAGE
| TVIF_SELECTEDIMAGE
,
3290 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3291 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3293 TREEVIEW_Collapse(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
3294 BOOL bRemoveChildren
, BOOL bUser
)
3296 UINT action
= TVE_COLLAPSE
| (bRemoveChildren
? TVE_COLLAPSERESET
: 0);
3297 BOOL bSetSelection
, bSetFirstVisible
;
3299 LONG scrollDist
= 0;
3300 TREEVIEW_ITEM
*nextItem
= NULL
, *tmpItem
;
3303 TRACE("TVE_COLLAPSE %p %s\n", item
, TREEVIEW_ItemName(item
));
3305 if (!TREEVIEW_HasChildren(infoPtr
, item
))
3309 TREEVIEW_SendExpanding(infoPtr
, item
, action
);
3311 if (item
->firstChild
== NULL
)
3314 wasExpanded
= (item
->state
& TVIS_EXPANDED
) != 0;
3315 item
->state
&= ~TVIS_EXPANDED
;
3317 if (wasExpanded
&& bUser
)
3318 TREEVIEW_SendExpanded(infoPtr
, item
, action
);
3320 bSetSelection
= (infoPtr
->selectedItem
!= NULL
3321 && TREEVIEW_IsChildOf(item
, infoPtr
->selectedItem
));
3323 bSetFirstVisible
= (infoPtr
->firstVisible
!= NULL
3324 && TREEVIEW_IsChildOf(item
, infoPtr
->firstVisible
));
3329 if (tmpItem
->nextSibling
)
3331 nextItem
= tmpItem
->nextSibling
;
3334 tmpItem
= tmpItem
->parent
;
3338 scrollDist
= nextItem
->rect
.top
;
3340 if (bRemoveChildren
)
3342 INT old_cChildren
= item
->cChildren
;
3343 TRACE("TVE_COLLAPSERESET\n");
3344 item
->state
&= ~TVIS_EXPANDEDONCE
;
3345 TREEVIEW_RemoveAllChildren(infoPtr
, item
);
3346 item
->cChildren
= old_cChildren
;
3351 if (item
->firstChild
)
3353 TREEVIEW_ITEM
*i
, *sibling
;
3355 sibling
= TREEVIEW_GetNextListItem(infoPtr
, item
);
3357 for (i
= item
->firstChild
; i
!= sibling
;
3358 i
= TREEVIEW_GetNextListItem(infoPtr
, i
))
3360 i
->visibleOrder
= -1;
3364 TREEVIEW_RecalculateVisibleOrder(infoPtr
, item
);
3367 scrollDist
= -(scrollDist
- nextItem
->rect
.top
);
3371 /* Don't call DoSelectItem, it sends notifications. */
3372 if (TREEVIEW_ValidItem(infoPtr
, infoPtr
->selectedItem
))
3373 infoPtr
->selectedItem
->state
&= ~TVIS_SELECTED
;
3374 item
->state
|= TVIS_SELECTED
;
3375 infoPtr
->selectedItem
= item
;
3378 TREEVIEW_UpdateScrollBars(infoPtr
);
3380 scrollRect
.left
= 0;
3381 scrollRect
.right
= infoPtr
->clientWidth
;
3382 scrollRect
.bottom
= infoPtr
->clientHeight
;
3386 scrollRect
.top
= nextItem
->rect
.top
;
3388 ScrollWindowEx (infoPtr
->hwnd
, 0, scrollDist
, &scrollRect
, &scrollRect
,
3389 NULL
, NULL
, SW_ERASE
| SW_INVALIDATE
);
3390 TREEVIEW_Invalidate(infoPtr
, item
);
3392 scrollRect
.top
= item
->rect
.top
;
3393 InvalidateRect(infoPtr
->hwnd
, &scrollRect
, TRUE
);
3396 TREEVIEW_SetFirstVisible(infoPtr
,
3397 bSetFirstVisible
? item
: infoPtr
->firstVisible
,
3404 TREEVIEW_Expand(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
3405 BOOL partial
, BOOL user
)
3408 LONG orgNextTop
= 0;
3410 TREEVIEW_ITEM
*nextItem
, *tmpItem
;
3411 BOOL sendsNotifications
;
3413 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr
, item
, partial
, user
);
3415 if (!TREEVIEW_HasChildren(infoPtr
, item
))
3418 tmpItem
= item
; nextItem
= NULL
;
3421 if (tmpItem
->nextSibling
)
3423 nextItem
= tmpItem
->nextSibling
;
3426 tmpItem
= tmpItem
->parent
;
3430 orgNextTop
= nextItem
->rect
.top
;
3432 TRACE("TVE_EXPAND %p %s\n", item
, TREEVIEW_ItemName(item
));
3434 sendsNotifications
= user
|| ((item
->cChildren
!= 0) &&
3435 !(item
->state
& TVIS_EXPANDEDONCE
));
3436 if (sendsNotifications
)
3438 if (!TREEVIEW_SendExpanding(infoPtr
, item
, TVE_EXPAND
))
3440 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3444 if (!item
->firstChild
)
3447 item
->state
|= TVIS_EXPANDED
;
3450 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3452 if (ISVISIBLE(item
))
3454 TREEVIEW_RecalculateVisibleOrder(infoPtr
, item
);
3455 TREEVIEW_UpdateSubTree(infoPtr
, item
);
3456 TREEVIEW_UpdateScrollBars(infoPtr
);
3458 scrollRect
.left
= 0;
3459 scrollRect
.bottom
= infoPtr
->treeHeight
;
3460 scrollRect
.right
= infoPtr
->clientWidth
;
3463 scrollDist
= nextItem
->rect
.top
- orgNextTop
;
3464 scrollRect
.top
= orgNextTop
;
3466 ScrollWindowEx (infoPtr
->hwnd
, 0, scrollDist
, &scrollRect
, NULL
,
3467 NULL
, NULL
, SW_ERASE
| SW_INVALIDATE
);
3468 TREEVIEW_Invalidate (infoPtr
, item
);
3470 scrollRect
.top
= item
->rect
.top
;
3471 InvalidateRect(infoPtr
->hwnd
, &scrollRect
, FALSE
);
3474 /* Scroll up so that as many children as possible are visible.
3475 * This fails when expanding causes an HScroll bar to appear, but we
3476 * don't know that yet, so the last item is obscured. */
3477 if (item
->firstChild
!= NULL
)
3479 int nChildren
= item
->lastChild
->visibleOrder
3480 - item
->firstChild
->visibleOrder
+ 1;
3482 int visible_pos
= item
->visibleOrder
3483 - infoPtr
->firstVisible
->visibleOrder
;
3485 int rows_below
= TREEVIEW_GetVisibleCount(infoPtr
) - visible_pos
- 1;
3487 if (visible_pos
> 0 && nChildren
> rows_below
)
3489 int scroll
= nChildren
- rows_below
;
3491 if (scroll
> visible_pos
)
3492 scroll
= visible_pos
;
3496 TREEVIEW_ITEM
*newFirstVisible
3497 = TREEVIEW_GetListItem(infoPtr
, infoPtr
->firstVisible
,
3501 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
3507 if (sendsNotifications
) {
3508 TREEVIEW_SendExpanded(infoPtr
, item
, TVE_EXPAND
);
3509 item
->state
|= TVIS_EXPANDEDONCE
;
3515 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3516 to mouse messages and TVM_SELECTITEM.
3518 selection - previously selected item, used to collapse a part of a tree
3519 item - new selected item
3521 static void TREEVIEW_SingleExpand(TREEVIEW_INFO
*infoPtr
,
3522 HTREEITEM selection
, HTREEITEM item
)
3524 TREEVIEW_ITEM
*prev
, *curr
;
3526 if ((infoPtr
->dwStyle
& TVS_SINGLEEXPAND
) == 0 || infoPtr
->hwndEdit
|| !item
) return;
3528 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_SINGLEEXPAND
, TVC_UNKNOWN
, TVIF_HANDLE
| TVIF_PARAM
, item
, 0);
3531 * Close the previous item and its ancestors as long as they are not
3532 * ancestors of the current item
3534 for (prev
= selection
; prev
&& TREEVIEW_ValidItem(infoPtr
, prev
); prev
= prev
->parent
)
3536 for (curr
= item
; curr
&& TREEVIEW_ValidItem(infoPtr
, curr
); curr
= curr
->parent
)
3541 TREEVIEW_Collapse(infoPtr
, prev
, FALSE
, TRUE
);
3546 * Expand the current item
3548 TREEVIEW_Expand(infoPtr
, item
, FALSE
, TRUE
);
3552 TREEVIEW_Toggle(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
, BOOL user
)
3554 TRACE("item=%p, user=%d\n", item
, user
);
3556 if (item
->state
& TVIS_EXPANDED
)
3557 return TREEVIEW_Collapse(infoPtr
, item
, FALSE
, user
);
3559 return TREEVIEW_Expand(infoPtr
, item
, FALSE
, user
);
3563 TREEVIEW_ExpandAll(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
3565 TREEVIEW_Expand(infoPtr
, item
, FALSE
, TRUE
);
3567 for (item
= item
->firstChild
; item
!= NULL
; item
= item
->nextSibling
)
3569 if (TREEVIEW_HasChildren(infoPtr
, item
))
3570 TREEVIEW_ExpandAll(infoPtr
, item
);
3574 /* Note:If the specified item is the child of a collapsed parent item,
3575 the parent's list of child items is (recursively) expanded to reveal the
3576 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3577 know if it also applies here.
3581 TREEVIEW_ExpandMsg(TREEVIEW_INFO
*infoPtr
, UINT flag
, HTREEITEM item
)
3583 if (!TREEVIEW_ValidItem(infoPtr
, item
))
3586 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3587 TREEVIEW_ItemName(item
), TREEVIEW_GetItemIndex(infoPtr
, item
),
3590 switch (flag
& TVE_TOGGLE
)
3593 return TREEVIEW_Collapse(infoPtr
, item
, flag
& TVE_COLLAPSERESET
,
3597 return TREEVIEW_Expand(infoPtr
, item
, flag
& TVE_EXPANDPARTIAL
,
3601 return TREEVIEW_Toggle(infoPtr
, item
, FALSE
);
3608 /* Hit-Testing **********************************************************/
3610 static TREEVIEW_ITEM
*
3611 TREEVIEW_HitTestPoint(const TREEVIEW_INFO
*infoPtr
, POINT pt
)
3613 TREEVIEW_ITEM
*item
;
3616 if (!infoPtr
->firstVisible
)
3619 row
= pt
.y
/ infoPtr
->uItemHeight
+ infoPtr
->firstVisible
->visibleOrder
;
3621 for (item
= infoPtr
->firstVisible
; item
!= NULL
;
3622 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
3624 if (row
>= item
->visibleOrder
3625 && row
< item
->visibleOrder
+ item
->iIntegral
)
3633 TREEVIEW_HitTest(const TREEVIEW_INFO
*infoPtr
, LPTVHITTESTINFO lpht
)
3635 TREEVIEW_ITEM
*item
;
3641 GetClientRect(infoPtr
->hwnd
, &rect
);
3648 status
|= TVHT_TOLEFT
;
3650 else if (x
> rect
.right
)
3652 status
|= TVHT_TORIGHT
;
3657 status
|= TVHT_ABOVE
;
3659 else if (y
> rect
.bottom
)
3661 status
|= TVHT_BELOW
;
3666 lpht
->flags
= status
;
3670 item
= TREEVIEW_HitTestPoint(infoPtr
, lpht
->pt
);
3673 lpht
->flags
= TVHT_NOWHERE
;
3677 if (x
>= item
->textOffset
+ item
->textWidth
)
3679 lpht
->flags
= TVHT_ONITEMRIGHT
;
3681 else if (x
>= item
->textOffset
)
3683 lpht
->flags
= TVHT_ONITEMLABEL
;
3685 else if (x
>= item
->imageOffset
)
3687 lpht
->flags
= TVHT_ONITEMICON
;
3689 else if (x
>= item
->stateOffset
)
3691 lpht
->flags
= TVHT_ONITEMSTATEICON
;
3693 else if (x
>= item
->linesOffset
&& infoPtr
->dwStyle
& TVS_HASBUTTONS
)
3695 lpht
->flags
= TVHT_ONITEMBUTTON
;
3699 lpht
->flags
= TVHT_ONITEMINDENT
;
3703 TRACE("(%d,%d):result 0x%x\n", lpht
->pt
.x
, lpht
->pt
.y
, lpht
->flags
);
3705 return (LRESULT
)item
;
3708 /* Item Label Editing ***************************************************/
3711 TREEVIEW_GetEditControl(const TREEVIEW_INFO
*infoPtr
)
3713 return (LRESULT
)infoPtr
->hwndEdit
;
3716 static LRESULT CALLBACK
3717 TREEVIEW_Edit_SubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
3719 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3720 BOOL bCancel
= FALSE
;
3726 TRACE("WM_PAINT start\n");
3727 rc
= CallWindowProcW(infoPtr
->wpEditOrig
, hwnd
, uMsg
, wParam
,
3729 TRACE("WM_PAINT done\n");
3733 if (infoPtr
->bIgnoreEditKillFocus
)
3739 WNDPROC editProc
= infoPtr
->wpEditOrig
;
3740 infoPtr
->wpEditOrig
= 0;
3741 SetWindowLongPtrW(hwnd
, GWLP_WNDPROC
, (DWORD_PTR
)editProc
);
3742 return CallWindowProcW(editProc
, hwnd
, uMsg
, wParam
, lParam
);
3746 return DLGC_WANTARROWS
| DLGC_WANTALLKEYS
;
3749 if (wParam
== VK_ESCAPE
)
3754 else if (wParam
== VK_RETURN
)
3761 return CallWindowProcW(infoPtr
->wpEditOrig
, hwnd
, uMsg
, wParam
, lParam
);
3764 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3765 /* eg. Using a messagebox */
3767 infoPtr
->bIgnoreEditKillFocus
= TRUE
;
3768 TREEVIEW_EndEditLabelNow(infoPtr
, bCancel
|| !infoPtr
->bLabelChanged
);
3769 infoPtr
->bIgnoreEditKillFocus
= FALSE
;
3775 /* should handle edit control messages here */
3778 TREEVIEW_Command(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
3780 TRACE("code=0x%x, id=0x%x, handle=0x%lx\n", HIWORD(wParam
), LOWORD(wParam
), lParam
);
3782 switch (HIWORD(wParam
))
3787 * Adjust the edit window size
3790 TREEVIEW_ITEM
*editItem
= infoPtr
->editItem
;
3791 HDC hdc
= GetDC(infoPtr
->hwndEdit
);
3793 HFONT hFont
, hOldFont
= 0;
3795 TRACE("edit=%p\n", infoPtr
->hwndEdit
);
3797 if (!IsWindow(infoPtr
->hwndEdit
) || !hdc
) return FALSE
;
3799 infoPtr
->bLabelChanged
= TRUE
;
3801 GetWindowTextW(infoPtr
->hwndEdit
, buffer
, sizeof(buffer
)/sizeof(buffer
[0]));
3803 /* Select font to get the right dimension of the string */
3804 hFont
= (HFONT
)SendMessageW(infoPtr
->hwndEdit
, WM_GETFONT
, 0, 0);
3808 hOldFont
= SelectObject(hdc
, hFont
);
3811 if (GetTextExtentPoint32W(hdc
, buffer
, strlenW(buffer
), &sz
))
3813 TEXTMETRICW textMetric
;
3815 /* Add Extra spacing for the next character */
3816 GetTextMetricsW(hdc
, &textMetric
);
3817 sz
.cx
+= (textMetric
.tmMaxCharWidth
* 2);
3819 sz
.cx
= max(sz
.cx
, textMetric
.tmMaxCharWidth
* 3);
3821 infoPtr
->clientWidth
- editItem
->textOffset
+ 2);
3823 SetWindowPos(infoPtr
->hwndEdit
,
3828 editItem
->rect
.bottom
- editItem
->rect
.top
+ 3,
3829 SWP_NOMOVE
| SWP_DRAWFRAME
);
3834 SelectObject(hdc
, hOldFont
);
3837 ReleaseDC(infoPtr
->hwnd
, hdc
);
3841 /* apparently we should respect passed handle value */
3842 if (infoPtr
->hwndEdit
!= (HWND
)lParam
) return FALSE
;
3844 TREEVIEW_EndEditLabelNow(infoPtr
, FALSE
);
3848 return SendMessageW(infoPtr
->hwndNotify
, WM_COMMAND
, wParam
, lParam
);
3855 TREEVIEW_EditLabel(TREEVIEW_INFO
*infoPtr
, HTREEITEM hItem
)
3857 HWND hwnd
= infoPtr
->hwnd
;
3860 HINSTANCE hinst
= (HINSTANCE
)GetWindowLongPtrW(hwnd
, GWLP_HINSTANCE
);
3863 TEXTMETRICW textMetric
;
3865 TRACE("%p %p\n", hwnd
, hItem
);
3866 if (!(infoPtr
->dwStyle
& TVS_EDITLABELS
))
3869 if (!TREEVIEW_ValidItem(infoPtr
, hItem
))
3872 if (infoPtr
->hwndEdit
)
3873 return infoPtr
->hwndEdit
;
3875 infoPtr
->bLabelChanged
= FALSE
;
3877 /* make edit item visible */
3878 TREEVIEW_EnsureVisible(infoPtr
, hItem
, TRUE
);
3880 TREEVIEW_UpdateDispInfo(infoPtr
, hItem
, TVIF_TEXT
);
3883 /* Select the font to get appropriate metric dimensions */
3884 if (infoPtr
->hFont
!= 0)
3886 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
3889 /* Get string length in pixels */
3891 GetTextExtentPoint32W(hdc
, hItem
->pszText
, strlenW(hItem
->pszText
),
3894 GetTextExtentPoint32A(hdc
, "", 0, &sz
);
3896 /* Add Extra spacing for the next character */
3897 GetTextMetricsW(hdc
, &textMetric
);
3898 sz
.cx
+= (textMetric
.tmMaxCharWidth
* 2);
3900 sz
.cx
= max(sz
.cx
, textMetric
.tmMaxCharWidth
* 3);
3901 sz
.cx
= min(sz
.cx
, infoPtr
->clientWidth
- hItem
->textOffset
+ 2);
3903 if (infoPtr
->hFont
!= 0)
3905 SelectObject(hdc
, hOldFont
);
3908 ReleaseDC(hwnd
, hdc
);
3910 infoPtr
->editItem
= hItem
;
3912 hwndEdit
= CreateWindowExW(WS_EX_LEFT
,
3915 WS_CHILD
| WS_BORDER
| ES_AUTOHSCROLL
|
3916 WS_CLIPSIBLINGS
| ES_WANTRETURN
|
3917 ES_LEFT
, hItem
->textOffset
- 2,
3918 hItem
->rect
.top
- 1, sz
.cx
+ 3,
3919 hItem
->rect
.bottom
-
3920 hItem
->rect
.top
+ 3, hwnd
, 0, hinst
, 0);
3921 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3923 infoPtr
->hwndEdit
= hwndEdit
;
3925 /* Get a 2D border. */
3926 SetWindowLongW(hwndEdit
, GWL_EXSTYLE
,
3927 GetWindowLongW(hwndEdit
, GWL_EXSTYLE
) & ~WS_EX_CLIENTEDGE
);
3928 SetWindowLongW(hwndEdit
, GWL_STYLE
,
3929 GetWindowLongW(hwndEdit
, GWL_STYLE
) | WS_BORDER
);
3931 SendMessageW(hwndEdit
, WM_SETFONT
,
3932 (WPARAM
)TREEVIEW_FontForItem(infoPtr
, hItem
), FALSE
);
3934 infoPtr
->wpEditOrig
= (WNDPROC
)SetWindowLongPtrW(hwndEdit
, GWLP_WNDPROC
,
3936 TREEVIEW_Edit_SubclassProc
);
3938 SetWindowTextW(hwndEdit
, hItem
->pszText
);
3940 if (TREEVIEW_BeginLabelEditNotify(infoPtr
, hItem
))
3942 DestroyWindow(hwndEdit
);
3943 infoPtr
->hwndEdit
= 0;
3944 infoPtr
->editItem
= NULL
;
3949 SendMessageW(hwndEdit
, EM_SETSEL
, 0, -1);
3950 ShowWindow(hwndEdit
, SW_SHOW
);
3957 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
)
3959 TREEVIEW_ITEM
*editedItem
= infoPtr
->editItem
;
3962 WCHAR tmpText
[1024] = { '\0' };
3963 WCHAR
*newText
= tmpText
;
3966 if (!IsWindow(infoPtr
->hwndEdit
)) return FALSE
;
3969 tvdi
.item
.hItem
= editedItem
;
3970 tvdi
.item
.state
= editedItem
->state
;
3971 tvdi
.item
.lParam
= editedItem
->lParam
;
3975 if (!infoPtr
->bNtfUnicode
)
3976 iLength
= GetWindowTextA(infoPtr
->hwndEdit
, (LPSTR
)tmpText
, 1023);
3978 iLength
= GetWindowTextW(infoPtr
->hwndEdit
, tmpText
, 1023);
3980 if (iLength
>= 1023)
3982 ERR("Insufficient space to retrieve new item label\n");
3985 tvdi
.item
.mask
= TVIF_TEXT
;
3986 tvdi
.item
.pszText
= tmpText
;
3987 tvdi
.item
.cchTextMax
= iLength
+ 1;
3991 tvdi
.item
.pszText
= NULL
;
3992 tvdi
.item
.cchTextMax
= 0;
3995 bCommit
= TREEVIEW_SendRealNotify(infoPtr
, TVN_ENDLABELEDITW
, &tvdi
.hdr
);
3997 if (!bCancel
&& bCommit
) /* Apply the changes */
3999 if (!infoPtr
->bNtfUnicode
)
4001 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, (LPSTR
)tmpText
, -1, NULL
, 0 );
4002 newText
= Alloc(len
* sizeof(WCHAR
));
4003 MultiByteToWideChar( CP_ACP
, 0, (LPSTR
)tmpText
, -1, newText
, len
);
4007 if (strcmpW(newText
, editedItem
->pszText
) != 0)
4009 WCHAR
*ptr
= ReAlloc(editedItem
->pszText
, sizeof(WCHAR
)*(iLength
+ 1));
4012 ERR("OutOfMemory, cannot allocate space for label\n");
4013 if(newText
!= tmpText
) Free(newText
);
4014 DestroyWindow(infoPtr
->hwndEdit
);
4015 infoPtr
->hwndEdit
= 0;
4016 infoPtr
->editItem
= NULL
;
4021 editedItem
->pszText
= ptr
;
4022 editedItem
->cchTextMax
= iLength
+ 1;
4023 strcpyW(editedItem
->pszText
, newText
);
4024 TREEVIEW_ComputeTextWidth(infoPtr
, editedItem
, 0);
4027 if(newText
!= tmpText
) Free(newText
);
4030 ShowWindow(infoPtr
->hwndEdit
, SW_HIDE
);
4031 DestroyWindow(infoPtr
->hwndEdit
);
4032 infoPtr
->hwndEdit
= 0;
4033 infoPtr
->editItem
= NULL
;
4038 TREEVIEW_HandleTimer(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4040 if (wParam
!= TV_EDIT_TIMER
)
4042 ERR("got unknown timer\n");
4046 KillTimer(infoPtr
->hwnd
, TV_EDIT_TIMER
);
4047 infoPtr
->Timer
&= ~TV_EDIT_TIMER_SET
;
4049 TREEVIEW_EditLabel(infoPtr
, infoPtr
->selectedItem
);
4055 /* Mouse Tracking/Drag **************************************************/
4057 /***************************************************************************
4058 * This is quite unusual piece of code, but that's how it's implemented in
4062 TREEVIEW_TrackMouse(const TREEVIEW_INFO
*infoPtr
, POINT pt
)
4064 INT cxDrag
= GetSystemMetrics(SM_CXDRAG
);
4065 INT cyDrag
= GetSystemMetrics(SM_CYDRAG
);
4069 r
.top
= pt
.y
- cyDrag
;
4070 r
.left
= pt
.x
- cxDrag
;
4071 r
.bottom
= pt
.y
+ cyDrag
;
4072 r
.right
= pt
.x
+ cxDrag
;
4074 SetCapture(infoPtr
->hwnd
);
4078 if (PeekMessageW(&msg
, 0, 0, 0, PM_REMOVE
| PM_NOYIELD
))
4080 if (msg
.message
== WM_MOUSEMOVE
)
4082 pt
.x
= (short)LOWORD(msg
.lParam
);
4083 pt
.y
= (short)HIWORD(msg
.lParam
);
4084 if (PtInRect(&r
, pt
))
4092 else if (msg
.message
>= WM_LBUTTONDOWN
&&
4093 msg
.message
<= WM_RBUTTONDBLCLK
)
4098 DispatchMessageW(&msg
);
4101 if (GetCapture() != infoPtr
->hwnd
)
4111 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4113 TREEVIEW_ITEM
*item
;
4117 SetFocus(infoPtr
->hwnd
);
4119 if (infoPtr
->Timer
& TV_EDIT_TIMER_SET
)
4121 /* If there is pending 'edit label' event - kill it now */
4122 KillTimer(infoPtr
->hwnd
, TV_EDIT_TIMER
);
4125 hit
.pt
.x
= (short)LOWORD(lParam
);
4126 hit
.pt
.y
= (short)HIWORD(lParam
);
4128 item
= (TREEVIEW_ITEM
*)TREEVIEW_HitTest(infoPtr
, &hit
);
4131 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr
, item
));
4133 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_DBLCLK
) == FALSE
)
4137 case TVHT_ONITEMRIGHT
:
4138 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4141 case TVHT_ONITEMINDENT
:
4142 if (!(infoPtr
->dwStyle
& TVS_HASLINES
))
4148 int level
= hit
.pt
.x
/ infoPtr
->uIndent
;
4149 if (!(infoPtr
->dwStyle
& TVS_LINESATROOT
)) level
++;
4151 while (item
->iLevel
> level
)
4153 item
= item
->parent
;
4159 case TVHT_ONITEMLABEL
:
4160 case TVHT_ONITEMICON
:
4161 case TVHT_ONITEMBUTTON
:
4162 TREEVIEW_Toggle(infoPtr
, item
, TRUE
);
4165 case TVHT_ONITEMSTATEICON
:
4166 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
4167 TREEVIEW_ToggleItemState(infoPtr
, item
);
4169 TREEVIEW_Toggle(infoPtr
, item
, TRUE
);
4178 TREEVIEW_LButtonDown(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4180 HWND hwnd
= infoPtr
->hwnd
;
4182 BOOL bTrack
, bDoLabelEdit
;
4184 /* If Edit control is active - kill it and return.
4185 * The best way to do it is to set focus to itself.
4186 * Edit control subclassed procedure will automatically call
4189 if (infoPtr
->hwndEdit
)
4195 ht
.pt
.x
= (short)LOWORD(lParam
);
4196 ht
.pt
.y
= (short)HIWORD(lParam
);
4198 TREEVIEW_HitTest(infoPtr
, &ht
);
4199 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr
, ht
.hItem
));
4201 /* update focusedItem and redraw both items */
4202 if(ht
.hItem
&& (ht
.flags
& TVHT_ONITEM
))
4204 infoPtr
->focusedItem
= ht
.hItem
;
4205 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->focusedItem
);
4206 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->selectedItem
);
4209 bTrack
= (ht
.flags
& TVHT_ONITEM
)
4210 && !(infoPtr
->dwStyle
& TVS_DISABLEDRAGDROP
);
4213 * If the style allows editing and the node is already selected
4214 * and the click occurred on the item label...
4216 bDoLabelEdit
= (infoPtr
->dwStyle
& TVS_EDITLABELS
) &&
4217 (ht
.flags
& TVHT_ONITEMLABEL
) && (infoPtr
->selectedItem
== ht
.hItem
);
4219 /* Send NM_CLICK right away */
4221 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_CLICK
))
4224 if (ht
.flags
& TVHT_ONITEMBUTTON
)
4226 TREEVIEW_Toggle(infoPtr
, ht
.hItem
, TRUE
);
4230 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4231 if (TREEVIEW_TrackMouse(infoPtr
, ht
.pt
))
4233 TREEVIEW_SendTreeviewDnDNotify(infoPtr
, TVN_BEGINDRAGW
, ht
.hItem
, ht
.pt
);
4234 infoPtr
->dropItem
= ht
.hItem
;
4236 /* clean up focusedItem as we dragged and won't select this item */
4237 if(infoPtr
->focusedItem
)
4239 /* refresh the item that was focused */
4240 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->focusedItem
);
4241 infoPtr
->focusedItem
= NULL
;
4243 /* refresh the selected item to return the filled background */
4244 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->selectedItem
);
4251 if (bTrack
&& TREEVIEW_SendSimpleNotify(infoPtr
, NM_CLICK
))
4256 if (infoPtr
->Timer
& TV_EDIT_TIMER_SET
)
4257 KillTimer(hwnd
, TV_EDIT_TIMER
);
4259 SetTimer(hwnd
, TV_EDIT_TIMER
, GetDoubleClickTime(), 0);
4260 infoPtr
->Timer
|= TV_EDIT_TIMER_SET
;
4262 else if (ht
.flags
& (TVHT_ONITEMICON
|TVHT_ONITEMLABEL
)) /* select the item if the hit was inside of the icon or text */
4264 TREEVIEW_ITEM
*selection
= infoPtr
->selectedItem
;
4266 /* Select the current item */
4267 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, ht
.hItem
, TVC_BYMOUSE
);
4268 TREEVIEW_SingleExpand(infoPtr
, selection
, ht
.hItem
);
4270 else if (ht
.flags
& TVHT_ONITEMSTATEICON
)
4272 /* TVS_CHECKBOXES requires us to toggle the current state */
4273 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
4274 TREEVIEW_ToggleItemState(infoPtr
, ht
.hItem
);
4284 TREEVIEW_RButtonDown(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4288 if (infoPtr
->hwndEdit
)
4290 SetFocus(infoPtr
->hwnd
);
4294 ht
.pt
.x
= (short)LOWORD(lParam
);
4295 ht
.pt
.y
= (short)HIWORD(lParam
);
4297 TREEVIEW_HitTest(infoPtr
, &ht
);
4299 if (TREEVIEW_TrackMouse(infoPtr
, ht
.pt
))
4303 TREEVIEW_SendTreeviewDnDNotify(infoPtr
, TVN_BEGINRDRAGW
, ht
.hItem
, ht
.pt
);
4304 infoPtr
->dropItem
= ht
.hItem
;
4309 SetFocus(infoPtr
->hwnd
);
4310 if(!TREEVIEW_SendSimpleNotify(infoPtr
, NM_RCLICK
))
4312 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4313 SendMessageW(infoPtr
->hwndNotify
, WM_CONTEXTMENU
,
4314 (WPARAM
)infoPtr
->hwnd
, (LPARAM
)GetMessagePos());
4322 TREEVIEW_CreateDragImage(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4324 TREEVIEW_ITEM
*dragItem
= (HTREEITEM
)lParam
;
4328 HBITMAP hbmp
, hOldbmp
;
4335 if (!(infoPtr
->himlNormal
))
4338 if (!dragItem
|| !TREEVIEW_ValidItem(infoPtr
, dragItem
))
4341 TREEVIEW_UpdateDispInfo(infoPtr
, dragItem
, TVIF_TEXT
);
4343 hwtop
= GetDesktopWindow();
4344 htopdc
= GetDC(hwtop
);
4345 hdc
= CreateCompatibleDC(htopdc
);
4347 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
4349 if (dragItem
->pszText
)
4350 GetTextExtentPoint32W(hdc
, dragItem
->pszText
, strlenW(dragItem
->pszText
),
4353 GetTextExtentPoint32A(hdc
, "", 0, &size
);
4355 TRACE("%d %d %s\n", size
.cx
, size
.cy
, debugstr_w(dragItem
->pszText
));
4356 hbmp
= CreateCompatibleBitmap(htopdc
, size
.cx
, size
.cy
);
4357 hOldbmp
= SelectObject(hdc
, hbmp
);
4359 ImageList_GetIconSize(infoPtr
->himlNormal
, &cx
, &cy
);
4364 infoPtr
->dragList
= ImageList_Create(size
.cx
, size
.cy
, ILC_COLOR
, 10, 10);
4365 ImageList_Draw(infoPtr
->himlNormal
, dragItem
->iImage
, hdc
, 0, 0,
4369 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4370 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4373 /* draw item text */
4375 SetRect(&rc
, cx
, 0, size
.cx
, size
.cy
);
4377 if (dragItem
->pszText
)
4378 DrawTextW(hdc
, dragItem
->pszText
, strlenW(dragItem
->pszText
), &rc
,
4381 SelectObject(hdc
, hOldFont
);
4382 SelectObject(hdc
, hOldbmp
);
4384 ImageList_Add(infoPtr
->dragList
, hbmp
, 0);
4388 ReleaseDC(hwtop
, htopdc
);
4390 return (LRESULT
)infoPtr
->dragList
;
4393 /* Selection ************************************************************/
4396 TREEVIEW_DoSelectItem(TREEVIEW_INFO
*infoPtr
, INT action
, HTREEITEM newSelect
,
4399 TREEVIEW_ITEM
*prevSelect
;
4401 assert(newSelect
== NULL
|| TREEVIEW_ValidItem(infoPtr
, newSelect
));
4403 TRACE("Entering item %p (%s), flag 0x%x, cause 0x%x, state %d\n",
4404 newSelect
, TREEVIEW_ItemName(newSelect
), action
, cause
,
4405 newSelect
? newSelect
->state
: 0);
4407 /* reset and redraw focusedItem if focusedItem was set so we don't */
4408 /* have to worry about the previously focused item when we set a new one */
4409 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->focusedItem
);
4410 infoPtr
->focusedItem
= NULL
;
4414 case TVGN_CARET
|TVSI_NOSINGLEEXPAND
:
4415 FIXME("TVSI_NOSINGLEEXPAND specified.\n");
4418 prevSelect
= infoPtr
->selectedItem
;
4420 if (prevSelect
== newSelect
) {
4421 TREEVIEW_EnsureVisible(infoPtr
, infoPtr
->selectedItem
, FALSE
);
4425 if (TREEVIEW_SendTreeviewNotify(infoPtr
,
4428 TVIF_TEXT
| TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
,
4434 prevSelect
->state
&= ~TVIS_SELECTED
;
4436 newSelect
->state
|= TVIS_SELECTED
;
4438 infoPtr
->selectedItem
= newSelect
;
4440 TREEVIEW_EnsureVisible(infoPtr
, infoPtr
->selectedItem
, FALSE
);
4442 TREEVIEW_InvalidateItem(infoPtr
, prevSelect
);
4443 TREEVIEW_InvalidateItem(infoPtr
, newSelect
);
4445 TREEVIEW_SendTreeviewNotify(infoPtr
,
4448 TVIF_TEXT
| TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
,
4453 case TVGN_DROPHILITE
:
4454 prevSelect
= infoPtr
->dropItem
;
4457 prevSelect
->state
&= ~TVIS_DROPHILITED
;
4459 infoPtr
->dropItem
= newSelect
;
4462 newSelect
->state
|= TVIS_DROPHILITED
;
4464 TREEVIEW_Invalidate(infoPtr
, prevSelect
);
4465 TREEVIEW_Invalidate(infoPtr
, newSelect
);
4468 case TVGN_FIRSTVISIBLE
:
4469 if (newSelect
!= NULL
)
4471 TREEVIEW_EnsureVisible(infoPtr
, newSelect
, FALSE
);
4472 TREEVIEW_SetFirstVisible(infoPtr
, newSelect
, TRUE
);
4473 TREEVIEW_Invalidate(infoPtr
, NULL
);
4478 TRACE("Leaving state %d\n", newSelect
? newSelect
->state
: 0);
4482 /* FIXME: handle NM_KILLFOCUS etc */
4484 TREEVIEW_SelectItem(TREEVIEW_INFO
*infoPtr
, INT wParam
, HTREEITEM item
)
4486 TREEVIEW_ITEM
*selection
= infoPtr
->selectedItem
;
4488 if (item
&& !TREEVIEW_ValidItem(infoPtr
, item
))
4491 if (item
== infoPtr
->selectedItem
)
4494 TRACE("%p (%s) %d\n", item
, TREEVIEW_ItemName(item
), wParam
);
4496 if (!TREEVIEW_DoSelectItem(infoPtr
, wParam
, item
, TVC_UNKNOWN
))
4499 TREEVIEW_SingleExpand(infoPtr
, selection
, item
);
4504 /*************************************************************************
4505 * TREEVIEW_ProcessLetterKeys
4507 * Processes keyboard messages generated by pressing the letter keys
4509 * What this does is perform a case insensitive search from the
4510 * current position with the following quirks:
4511 * - If two chars or more are pressed in quick succession we search
4512 * for the corresponding string (e.g. 'abc').
4513 * - If there is a delay we wipe away the current search string and
4514 * restart with just that char.
4515 * - If the user keeps pressing the same character, whether slowly or
4516 * fast, so that the search string is entirely composed of this
4517 * character ('aaaaa' for instance), then we search for first item
4518 * that starting with that character.
4519 * - If the user types the above character in quick succession, then
4520 * we must also search for the corresponding string ('aaaaa'), and
4521 * go to that string if there is a match.
4529 * - The current implementation has a list of characters it will
4530 * accept and it ignores everything else. In particular it will
4531 * ignore accentuated characters which seems to match what
4532 * Windows does. But I'm not sure it makes sense to follow
4534 * - We don't sound a beep when the search fails.
4535 * - The search should start from the focused item, not from the selected
4536 * item. One reason for this is to allow for multiple selections in trees.
4537 * But currently infoPtr->focusedItem does not seem very usable.
4541 * TREEVIEW_ProcessLetterKeys
4543 static INT
TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO
*infoPtr
, WPARAM charCode
, LPARAM keyData
)
4546 HTREEITEM endidx
,idx
;
4548 WCHAR buffer
[MAX_PATH
];
4549 DWORD timestamp
,elapsed
;
4551 /* simple parameter checking */
4552 if (!charCode
|| !keyData
) return 0;
4554 /* only allow the valid WM_CHARs through */
4555 if (!isalnum(charCode
) &&
4556 charCode
!= '.' && charCode
!= '`' && charCode
!= '!' &&
4557 charCode
!= '@' && charCode
!= '#' && charCode
!= '$' &&
4558 charCode
!= '%' && charCode
!= '^' && charCode
!= '&' &&
4559 charCode
!= '*' && charCode
!= '(' && charCode
!= ')' &&
4560 charCode
!= '-' && charCode
!= '_' && charCode
!= '+' &&
4561 charCode
!= '=' && charCode
!= '\\'&& charCode
!= ']' &&
4562 charCode
!= '}' && charCode
!= '[' && charCode
!= '{' &&
4563 charCode
!= '/' && charCode
!= '?' && charCode
!= '>' &&
4564 charCode
!= '<' && charCode
!= ',' && charCode
!= '~')
4567 /* compute how much time elapsed since last keypress */
4568 timestamp
= GetTickCount();
4569 if (timestamp
> infoPtr
->lastKeyPressTimestamp
) {
4570 elapsed
=timestamp
-infoPtr
->lastKeyPressTimestamp
;
4572 elapsed
=infoPtr
->lastKeyPressTimestamp
-timestamp
;
4575 /* update the search parameters */
4576 infoPtr
->lastKeyPressTimestamp
=timestamp
;
4577 if (elapsed
< KEY_DELAY
) {
4578 if (infoPtr
->nSearchParamLength
< sizeof(infoPtr
->szSearchParam
) / sizeof(WCHAR
)) {
4579 infoPtr
->szSearchParam
[infoPtr
->nSearchParamLength
++]=charCode
;
4581 if (infoPtr
->charCode
!= charCode
) {
4582 infoPtr
->charCode
=charCode
=0;
4585 infoPtr
->charCode
=charCode
;
4586 infoPtr
->szSearchParam
[0]=charCode
;
4587 infoPtr
->nSearchParamLength
=1;
4588 /* Redundant with the 1 char string */
4592 /* and search from the current position */
4594 if (infoPtr
->selectedItem
!= NULL
) {
4595 endidx
=infoPtr
->selectedItem
;
4596 /* if looking for single character match,
4597 * then we must always move forward
4599 if (infoPtr
->nSearchParamLength
== 1)
4600 idx
=TREEVIEW_GetNextListItem(infoPtr
,endidx
);
4605 idx
=infoPtr
->root
->firstChild
;
4608 /* At the end point, sort out wrapping */
4611 /* If endidx is null, stop at the last item (ie top to bottom) */
4615 /* Otherwise, start again at the very beginning */
4616 idx
=infoPtr
->root
->firstChild
;
4618 /* But if we are stopping on the first child, end now! */
4619 if (idx
== endidx
) break;
4623 ZeroMemory(&item
, sizeof(item
));
4624 item
.mask
= TVIF_TEXT
;
4626 item
.pszText
= buffer
;
4627 item
.cchTextMax
= sizeof(buffer
);
4628 TREEVIEW_GetItemT( infoPtr
, &item
, TRUE
);
4630 /* check for a match */
4631 if (strncmpiW(item
.pszText
,infoPtr
->szSearchParam
,infoPtr
->nSearchParamLength
) == 0) {
4634 } else if ( (charCode
!= 0) && (nItem
== NULL
) &&
4635 (nItem
!= infoPtr
->selectedItem
) &&
4636 (strncmpiW(item
.pszText
,infoPtr
->szSearchParam
,1) == 0) ) {
4637 /* This would work but we must keep looking for a longer match */
4640 idx
=TREEVIEW_GetNextListItem(infoPtr
,idx
);
4641 } while (idx
!= endidx
);
4643 if (nItem
!= NULL
) {
4644 if (TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, nItem
, TVC_BYKEYBOARD
)) {
4645 TREEVIEW_EnsureVisible(infoPtr
, nItem
, FALSE
);
4652 /* Scrolling ************************************************************/
4655 TREEVIEW_EnsureVisible(TREEVIEW_INFO
*infoPtr
, HTREEITEM item
, BOOL bHScroll
)
4658 BOOL hasFirstVisible
= infoPtr
->firstVisible
!= NULL
;
4659 HTREEITEM newFirstVisible
= NULL
;
4660 int visible_pos
= -1;
4662 if (!TREEVIEW_ValidItem(infoPtr
, item
))
4665 if (!ISVISIBLE(item
))
4667 /* Expand parents as necessary. */
4670 /* see if we are trying to ensure that root is visible */
4671 if((item
!= infoPtr
->root
) && TREEVIEW_ValidItem(infoPtr
, item
))
4672 parent
= item
->parent
;
4674 parent
= item
; /* this item is the topmost item */
4676 while (parent
!= infoPtr
->root
)
4678 if (!(parent
->state
& TVIS_EXPANDED
))
4679 TREEVIEW_Expand(infoPtr
, parent
, FALSE
, TRUE
);
4681 parent
= parent
->parent
;
4685 viscount
= TREEVIEW_GetVisibleCount(infoPtr
);
4687 TRACE("%p (%s) %d - %d viscount(%d)\n", item
, TREEVIEW_ItemName(item
), item
->visibleOrder
,
4688 hasFirstVisible
? infoPtr
->firstVisible
->visibleOrder
: -1, viscount
);
4690 if (hasFirstVisible
)
4691 visible_pos
= item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
;
4693 if (visible_pos
< 0)
4695 /* item is before the start of the list: put it at the top. */
4696 newFirstVisible
= item
;
4698 else if (visible_pos
>= viscount
4699 /* Sometimes, before we are displayed, GVC is 0, causing us to
4700 * spuriously scroll up. */
4701 && visible_pos
> 0 && !(infoPtr
->dwStyle
& TVS_NOSCROLL
) )
4703 /* item is past the end of the list. */
4704 int scroll
= visible_pos
- viscount
;
4706 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, infoPtr
->firstVisible
,
4712 /* Scroll window so item's text is visible as much as possible */
4713 /* Calculation of amount of extra space is taken from EditLabel code */
4715 TEXTMETRICW textMetric
;
4716 HDC hdc
= GetWindowDC(infoPtr
->hwnd
);
4718 x
= item
->textWidth
;
4720 GetTextMetricsW(hdc
, &textMetric
);
4721 ReleaseDC(infoPtr
->hwnd
, hdc
);
4723 x
+= (textMetric
.tmMaxCharWidth
* 2);
4724 x
= max(x
, textMetric
.tmMaxCharWidth
* 3);
4726 if (item
->textOffset
< 0)
4727 pos
= item
->textOffset
;
4728 else if (item
->textOffset
+ x
> infoPtr
->clientWidth
)
4730 if (x
> infoPtr
->clientWidth
)
4731 pos
= item
->textOffset
;
4733 pos
= item
->textOffset
+ x
- infoPtr
->clientWidth
;
4738 TREEVIEW_HScroll(infoPtr
, MAKEWPARAM(SB_THUMBPOSITION
, infoPtr
->scrollX
+ pos
));
4741 if (newFirstVisible
!= NULL
&& newFirstVisible
!= infoPtr
->firstVisible
)
4743 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
4752 TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*infoPtr
,
4753 TREEVIEW_ITEM
*newFirstVisible
,
4754 BOOL bUpdateScrollPos
)
4758 TRACE("%p: %s\n", newFirstVisible
, TREEVIEW_ItemName(newFirstVisible
));
4760 if (newFirstVisible
!= NULL
)
4762 /* Prevent an empty gap from appearing at the bottom... */
4763 gap_size
= TREEVIEW_GetVisibleCount(infoPtr
)
4764 - infoPtr
->maxVisibleOrder
+ newFirstVisible
->visibleOrder
;
4768 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, newFirstVisible
,
4771 /* ... unless we just don't have enough items. */
4772 if (newFirstVisible
== NULL
)
4773 newFirstVisible
= infoPtr
->root
->firstChild
;
4777 if (infoPtr
->firstVisible
!= newFirstVisible
)
4779 if (infoPtr
->firstVisible
== NULL
|| newFirstVisible
== NULL
)
4781 infoPtr
->firstVisible
= newFirstVisible
;
4782 TREEVIEW_Invalidate(infoPtr
, NULL
);
4786 TREEVIEW_ITEM
*item
;
4787 int scroll
= infoPtr
->uItemHeight
*
4788 (infoPtr
->firstVisible
->visibleOrder
4789 - newFirstVisible
->visibleOrder
);
4791 infoPtr
->firstVisible
= newFirstVisible
;
4793 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
4794 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
4796 item
->rect
.top
+= scroll
;
4797 item
->rect
.bottom
+= scroll
;
4800 if (bUpdateScrollPos
)
4801 SetScrollPos(infoPtr
->hwnd
, SB_VERT
,
4802 newFirstVisible
->visibleOrder
, TRUE
);
4804 ScrollWindowEx(infoPtr
->hwnd
, 0, scroll
, NULL
, NULL
, NULL
, NULL
, SW_ERASE
| SW_INVALIDATE
);
4809 /************************************************************************
4810 * VScroll is always in units of visible items. i.e. we always have a
4811 * visible item aligned to the top of the control. (Unless we have no
4815 TREEVIEW_VScroll(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4817 TREEVIEW_ITEM
*oldFirstVisible
= infoPtr
->firstVisible
;
4818 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
4820 int nScrollCode
= LOWORD(wParam
);
4822 TRACE("wp %lx\n", wParam
);
4824 if (!(infoPtr
->uInternalStatus
& TV_VSCROLL
))
4827 if (!oldFirstVisible
)
4829 assert(infoPtr
->root
->firstChild
== NULL
);
4833 switch (nScrollCode
)
4836 newFirstVisible
= infoPtr
->root
->firstChild
;
4840 newFirstVisible
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
4844 newFirstVisible
= TREEVIEW_GetPrevListItem(infoPtr
, oldFirstVisible
);
4848 newFirstVisible
= TREEVIEW_GetNextListItem(infoPtr
, oldFirstVisible
);
4852 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, oldFirstVisible
,
4853 -max(1, TREEVIEW_GetVisibleCount(infoPtr
)));
4857 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, oldFirstVisible
,
4858 max(1, TREEVIEW_GetVisibleCount(infoPtr
)));
4862 case SB_THUMBPOSITION
:
4863 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
,
4864 infoPtr
->root
->firstChild
,
4865 (LONG
)(SHORT
)HIWORD(wParam
));
4872 if (newFirstVisible
!= NULL
)
4874 if (newFirstVisible
!= oldFirstVisible
)
4875 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
,
4876 nScrollCode
!= SB_THUMBTRACK
);
4877 else if (nScrollCode
== SB_THUMBPOSITION
)
4878 SetScrollPos(infoPtr
->hwnd
, SB_VERT
,
4879 newFirstVisible
->visibleOrder
, TRUE
);
4886 TREEVIEW_HScroll(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4889 int scrollX
= infoPtr
->scrollX
;
4890 int nScrollCode
= LOWORD(wParam
);
4892 TRACE("wp %lx\n", wParam
);
4894 if (!(infoPtr
->uInternalStatus
& TV_HSCROLL
))
4897 maxWidth
= infoPtr
->treeWidth
- infoPtr
->clientWidth
;
4898 /* shall never occur */
4905 switch (nScrollCode
)
4908 scrollX
-= infoPtr
->uItemHeight
;
4911 scrollX
+= infoPtr
->uItemHeight
;
4914 scrollX
-= infoPtr
->clientWidth
;
4917 scrollX
+= infoPtr
->clientWidth
;
4921 case SB_THUMBPOSITION
:
4922 scrollX
= (int)(SHORT
)HIWORD(wParam
);
4929 if (scrollX
> maxWidth
)
4931 else if (scrollX
< 0)
4935 if (scrollX
!= infoPtr
->scrollX
)
4937 TREEVIEW_ITEM
*item
;
4938 LONG scroll_pixels
= infoPtr
->scrollX
- scrollX
;
4940 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
4941 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
4943 item
->linesOffset
+= scroll_pixels
;
4944 item
->stateOffset
+= scroll_pixels
;
4945 item
->imageOffset
+= scroll_pixels
;
4946 item
->textOffset
+= scroll_pixels
;
4949 ScrollWindow(infoPtr
->hwnd
, scroll_pixels
, 0, NULL
, NULL
);
4950 infoPtr
->scrollX
= scrollX
;
4951 UpdateWindow(infoPtr
->hwnd
);
4954 if (nScrollCode
!= SB_THUMBTRACK
)
4955 SetScrollPos(infoPtr
->hwnd
, SB_HORZ
, scrollX
, TRUE
);
4961 TREEVIEW_MouseWheel(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
4964 UINT pulScrollLines
= 3;
4966 if (wParam
& (MK_SHIFT
| MK_CONTROL
))
4967 return DefWindowProcW(infoPtr
->hwnd
, WM_MOUSEWHEEL
, wParam
, lParam
);
4969 if (infoPtr
->firstVisible
== NULL
)
4972 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
, 0, &pulScrollLines
, 0);
4974 wheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4975 /* if scrolling changes direction, ignore left overs */
4976 if ((wheelDelta
< 0 && infoPtr
->wheelRemainder
< 0) ||
4977 (wheelDelta
> 0 && infoPtr
->wheelRemainder
> 0))
4978 infoPtr
->wheelRemainder
+= wheelDelta
;
4980 infoPtr
->wheelRemainder
= wheelDelta
;
4982 if (infoPtr
->wheelRemainder
&& pulScrollLines
)
4988 lineScroll
= pulScrollLines
* (float)infoPtr
->wheelRemainder
/ WHEEL_DELTA
;
4989 infoPtr
->wheelRemainder
-= WHEEL_DELTA
* lineScroll
/ (int)pulScrollLines
;
4991 newDy
= infoPtr
->firstVisible
->visibleOrder
- lineScroll
;
4992 maxDy
= infoPtr
->maxVisibleOrder
;
5000 TREEVIEW_VScroll(infoPtr
, MAKEWPARAM(SB_THUMBPOSITION
, newDy
));
5005 /* Create/Destroy *******************************************************/
5008 TREEVIEW_Create(HWND hwnd
, const CREATESTRUCTW
*lpcs
)
5011 TREEVIEW_INFO
*infoPtr
;
5014 TRACE("wnd %p, style 0x%x\n", hwnd
, GetWindowLongW(hwnd
, GWL_STYLE
));
5016 infoPtr
= Alloc(sizeof(TREEVIEW_INFO
));
5018 if (infoPtr
== NULL
)
5020 ERR("could not allocate info memory!\n");
5024 SetWindowLongPtrW(hwnd
, 0, (DWORD_PTR
)infoPtr
);
5026 infoPtr
->hwnd
= hwnd
;
5027 infoPtr
->dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
5029 infoPtr
->uNumItems
= 0;
5030 infoPtr
->cdmode
= 0;
5031 infoPtr
->uScrollTime
= 300; /* milliseconds */
5032 infoPtr
->bRedraw
= TRUE
;
5034 GetClientRect(hwnd
, &rcClient
);
5036 /* No scroll bars yet. */
5037 infoPtr
->clientWidth
= rcClient
.right
;
5038 infoPtr
->clientHeight
= rcClient
.bottom
;
5039 infoPtr
->uInternalStatus
= 0;
5041 infoPtr
->treeWidth
= 0;
5042 infoPtr
->treeHeight
= 0;
5044 infoPtr
->uIndent
= MINIMUM_INDENT
;
5045 infoPtr
->selectedItem
= NULL
;
5046 infoPtr
->focusedItem
= NULL
;
5047 infoPtr
->hotItem
= NULL
;
5048 infoPtr
->editItem
= NULL
;
5049 infoPtr
->firstVisible
= NULL
;
5050 infoPtr
->maxVisibleOrder
= 0;
5051 infoPtr
->dropItem
= NULL
;
5052 infoPtr
->insertMarkItem
= NULL
;
5053 infoPtr
->insertBeforeorAfter
= 0;
5056 infoPtr
->scrollX
= 0;
5057 infoPtr
->wheelRemainder
= 0;
5059 infoPtr
->clrBk
= CLR_NONE
; /* use system color */
5060 infoPtr
->clrText
= CLR_NONE
; /* use system color */
5061 infoPtr
->clrLine
= CLR_DEFAULT
;
5062 infoPtr
->clrInsertMark
= CLR_DEFAULT
;
5066 infoPtr
->hwndEdit
= NULL
;
5067 infoPtr
->wpEditOrig
= NULL
;
5068 infoPtr
->bIgnoreEditKillFocus
= FALSE
;
5069 infoPtr
->bLabelChanged
= FALSE
;
5071 infoPtr
->himlNormal
= NULL
;
5072 infoPtr
->himlState
= NULL
;
5073 infoPtr
->normalImageWidth
= 0;
5074 infoPtr
->normalImageHeight
= 0;
5075 infoPtr
->stateImageWidth
= 0;
5076 infoPtr
->stateImageHeight
= 0;
5078 infoPtr
->items
= DPA_Create(16);
5080 SystemParametersInfoW(SPI_GETICONTITLELOGFONT
, sizeof(lf
), &lf
, 0);
5081 infoPtr
->hFont
= infoPtr
->hDefaultFont
= CreateFontIndirectW(&lf
);
5082 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
5083 infoPtr
->hUnderlineFont
= TREEVIEW_CreateUnderlineFont(infoPtr
->hFont
);
5084 infoPtr
->hBoldUnderlineFont
= TREEVIEW_CreateBoldUnderlineFont(infoPtr
->hFont
);
5085 infoPtr
->hcurHand
= LoadCursorW(NULL
, (LPWSTR
)IDC_HAND
);
5087 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
5089 infoPtr
->root
= TREEVIEW_AllocateItem(infoPtr
);
5090 infoPtr
->root
->state
= TVIS_EXPANDED
;
5091 infoPtr
->root
->iLevel
= -1;
5092 infoPtr
->root
->visibleOrder
= -1;
5094 infoPtr
->hwndNotify
= lpcs
->hwndParent
;
5095 infoPtr
->hwndToolTip
= 0;
5097 /* Determine what type of notify should be issued (sets infoPtr->bNtfUnicode) */
5098 TREEVIEW_NotifyFormat(infoPtr
, infoPtr
->hwndNotify
, NF_REQUERY
);
5100 if (!(infoPtr
->dwStyle
& TVS_NOTOOLTIPS
))
5101 infoPtr
->hwndToolTip
= CreateWindowExW(0, TOOLTIPS_CLASSW
, NULL
, WS_POPUP
,
5102 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
5105 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5106 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
5107 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
5109 OpenThemeData (hwnd
, themeClass
);
5116 TREEVIEW_Destroy(TREEVIEW_INFO
*infoPtr
)
5120 /* free item data */
5121 TREEVIEW_RemoveTree(infoPtr
);
5122 /* root isn't freed with other items */
5123 TREEVIEW_FreeItem(infoPtr
, infoPtr
->root
);
5124 DPA_Destroy(infoPtr
->items
);
5126 /* tool tip is automatically destroyed: we are its owner */
5128 /* Restore original wndproc */
5129 if (infoPtr
->hwndEdit
)
5130 SetWindowLongPtrW(infoPtr
->hwndEdit
, GWLP_WNDPROC
,
5131 (DWORD_PTR
)infoPtr
->wpEditOrig
);
5133 CloseThemeData (GetWindowTheme (infoPtr
->hwnd
));
5135 /* Deassociate treeview from the window before doing anything drastic. */
5136 SetWindowLongPtrW(infoPtr
->hwnd
, 0, 0);
5138 DeleteObject(infoPtr
->hDefaultFont
);
5139 DeleteObject(infoPtr
->hBoldFont
);
5140 DeleteObject(infoPtr
->hUnderlineFont
);
5141 DeleteObject(infoPtr
->hBoldUnderlineFont
);
5147 /* Miscellaneous Messages ***********************************************/
5150 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO
*infoPtr
, WPARAM key
)
5158 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5159 SCROLL_ENTRY(SB_VERT
, SB_PAGEUP
), /* VK_PRIOR */
5160 SCROLL_ENTRY(SB_VERT
, SB_PAGEDOWN
), /* VK_NEXT */
5161 SCROLL_ENTRY(SB_VERT
, SB_BOTTOM
), /* VK_END */
5162 SCROLL_ENTRY(SB_VERT
, SB_TOP
), /* VK_HOME */
5163 SCROLL_ENTRY(SB_HORZ
, SB_LINEUP
), /* VK_LEFT */
5164 SCROLL_ENTRY(SB_VERT
, SB_LINEUP
), /* VK_UP */
5165 SCROLL_ENTRY(SB_HORZ
, SB_LINEDOWN
), /* VK_RIGHT */
5166 SCROLL_ENTRY(SB_VERT
, SB_LINEDOWN
) /* VK_DOWN */
5170 if (key
>= VK_PRIOR
&& key
<= VK_DOWN
)
5172 unsigned char code
= scroll
[key
- VK_PRIOR
].code
;
5174 (((code
& (1 << 7)) == (SB_HORZ
<< 7))
5176 : TREEVIEW_VScroll
)(infoPtr
, code
& 0x7F);
5182 /************************************************************************
5185 * VK_UP Move selection to the previous non-hidden item.
5186 * VK_DOWN Move selection to the next non-hidden item.
5187 * VK_HOME Move selection to the first item.
5188 * VK_END Move selection to the last item.
5189 * VK_LEFT If expanded then collapse, otherwise move to parent.
5190 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5192 * VK_SUBTRACT Collapse.
5193 * VK_MULTIPLY Expand all.
5194 * VK_PRIOR Move up GetVisibleCount items.
5195 * VK_NEXT Move down GetVisibleCount items.
5196 * VK_BACK Move to parent.
5197 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5200 TREEVIEW_KeyDown(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
5202 /* If it is non-NULL and different, it will be selected and visible. */
5203 TREEVIEW_ITEM
*newSelection
= NULL
;
5204 TREEVIEW_ITEM
*prevItem
= infoPtr
->selectedItem
;
5205 NMTVKEYDOWN nmkeydown
;
5207 TRACE("%lx\n", wParam
);
5209 nmkeydown
.wVKey
= wParam
;
5210 nmkeydown
.flags
= 0;
5211 TREEVIEW_SendRealNotify(infoPtr
, TVN_KEYDOWN
, &nmkeydown
.hdr
);
5213 if (prevItem
== NULL
)
5216 if (GetAsyncKeyState(VK_CONTROL
) & 0x8000)
5217 return TREEVIEW_ScrollKeyDown(infoPtr
, wParam
);
5222 newSelection
= TREEVIEW_GetPrevListItem(infoPtr
, prevItem
);
5224 newSelection
= infoPtr
->root
->firstChild
;
5228 newSelection
= TREEVIEW_GetNextListItem(infoPtr
, prevItem
);
5232 TREEVIEW_SendSimpleNotify(infoPtr
, NM_RETURN
);
5236 newSelection
= infoPtr
->root
->firstChild
;
5240 newSelection
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
5244 if (prevItem
->state
& TVIS_EXPANDED
)
5246 TREEVIEW_Collapse(infoPtr
, prevItem
, FALSE
, TRUE
);
5248 else if (prevItem
->parent
!= infoPtr
->root
)
5250 newSelection
= prevItem
->parent
;
5255 if (TREEVIEW_HasChildren(infoPtr
, prevItem
))
5257 if (!(prevItem
->state
& TVIS_EXPANDED
))
5258 TREEVIEW_Expand(infoPtr
, prevItem
, FALSE
, TRUE
);
5261 newSelection
= prevItem
->firstChild
;
5268 TREEVIEW_ExpandAll(infoPtr
, prevItem
);
5272 TREEVIEW_Expand(infoPtr
, prevItem
, FALSE
, TRUE
);
5276 TREEVIEW_Collapse(infoPtr
, prevItem
, FALSE
, TRUE
);
5281 = TREEVIEW_GetListItem(infoPtr
, prevItem
,
5282 -TREEVIEW_GetVisibleCount(infoPtr
));
5287 = TREEVIEW_GetListItem(infoPtr
, prevItem
,
5288 TREEVIEW_GetVisibleCount(infoPtr
));
5292 newSelection
= prevItem
->parent
;
5293 if (newSelection
== infoPtr
->root
)
5294 newSelection
= NULL
;
5298 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
5299 TREEVIEW_ToggleItemState(infoPtr
, prevItem
);
5303 if (newSelection
&& newSelection
!= prevItem
)
5305 if (TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
,
5308 TREEVIEW_EnsureVisible(infoPtr
, newSelection
, FALSE
);
5316 TREEVIEW_MouseLeave (TREEVIEW_INFO
* infoPtr
)
5318 /* remove hot effect from item */
5319 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->hotItem
);
5320 infoPtr
->hotItem
= NULL
;
5326 TREEVIEW_MouseMove (TREEVIEW_INFO
* infoPtr
, LPARAM lParam
)
5329 TRACKMOUSEEVENT trackinfo
;
5330 TREEVIEW_ITEM
* item
;
5332 if (!(infoPtr
->dwStyle
& TVS_TRACKSELECT
)) return 0;
5334 /* fill in the TRACKMOUSEEVENT struct */
5335 trackinfo
.cbSize
= sizeof(TRACKMOUSEEVENT
);
5336 trackinfo
.dwFlags
= TME_QUERY
;
5337 trackinfo
.hwndTrack
= infoPtr
->hwnd
;
5339 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5340 _TrackMouseEvent(&trackinfo
);
5342 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5343 if(!(trackinfo
.dwFlags
& TME_LEAVE
))
5345 trackinfo
.dwFlags
= TME_LEAVE
; /* notify upon leaving */
5346 trackinfo
.hwndTrack
= infoPtr
->hwnd
;
5347 /* do it as fast as possible, minimal systimer latency will be used */
5348 trackinfo
.dwHoverTime
= 1;
5350 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5351 /* and can properly deactivate the hot item */
5352 _TrackMouseEvent(&trackinfo
);
5355 pt
.x
= (short)LOWORD(lParam
);
5356 pt
.y
= (short)HIWORD(lParam
);
5358 item
= TREEVIEW_HitTestPoint(infoPtr
, pt
);
5360 if (item
!= infoPtr
->hotItem
)
5362 /* redraw old hot item */
5363 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->hotItem
);
5364 infoPtr
->hotItem
= item
;
5365 /* redraw new hot item */
5366 TREEVIEW_InvalidateItem(infoPtr
, infoPtr
->hotItem
);
5372 /* Draw themed border */
5373 static BOOL
TREEVIEW_NCPaint (const TREEVIEW_INFO
*infoPtr
, HRGN region
, LPARAM lParam
)
5375 HTHEME theme
= GetWindowTheme (infoPtr
->hwnd
);
5379 int cxEdge
= GetSystemMetrics (SM_CXEDGE
),
5380 cyEdge
= GetSystemMetrics (SM_CYEDGE
);
5383 return DefWindowProcW (infoPtr
->hwnd
, WM_NCPAINT
, (WPARAM
)region
, lParam
);
5385 GetWindowRect(infoPtr
->hwnd
, &r
);
5387 cliprgn
= CreateRectRgn (r
.left
+ cxEdge
, r
.top
+ cyEdge
,
5388 r
.right
- cxEdge
, r
.bottom
- cyEdge
);
5389 if (region
!= (HRGN
)1)
5390 CombineRgn (cliprgn
, cliprgn
, region
, RGN_AND
);
5391 OffsetRect(&r
, -r
.left
, -r
.top
);
5393 dc
= GetDCEx(infoPtr
->hwnd
, region
, DCX_WINDOW
|DCX_INTERSECTRGN
);
5394 OffsetRect(&r
, -r
.left
, -r
.top
);
5396 if (IsThemeBackgroundPartiallyTransparent (theme
, 0, 0))
5397 DrawThemeParentBackground(infoPtr
->hwnd
, dc
, &r
);
5398 DrawThemeBackground (theme
, dc
, 0, 0, &r
, 0);
5399 ReleaseDC(infoPtr
->hwnd
, dc
);
5401 /* Call default proc to get the scrollbars etc. painted */
5402 DefWindowProcW (infoPtr
->hwnd
, WM_NCPAINT
, (WPARAM
)cliprgn
, 0);
5408 TREEVIEW_Notify(const TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5410 LPNMHDR lpnmh
= (LPNMHDR
)lParam
;
5412 if (lpnmh
->code
== PGN_CALCSIZE
) {
5413 LPNMPGCALCSIZE lppgc
= (LPNMPGCALCSIZE
)lParam
;
5415 if (lppgc
->dwFlag
== PGF_CALCWIDTH
) {
5416 lppgc
->iWidth
= infoPtr
->treeWidth
;
5417 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5418 infoPtr
->treeWidth
, infoPtr
->clientWidth
);
5421 lppgc
->iHeight
= infoPtr
->treeHeight
;
5422 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5423 infoPtr
->treeHeight
, infoPtr
->clientHeight
);
5427 return DefWindowProcW(infoPtr
->hwnd
, WM_NOTIFY
, wParam
, lParam
);
5431 TREEVIEW_Size(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5433 if (wParam
== SIZE_RESTORED
)
5435 infoPtr
->clientWidth
= (short)LOWORD(lParam
);
5436 infoPtr
->clientHeight
= (short)HIWORD(lParam
);
5438 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
5439 TREEVIEW_SetFirstVisible(infoPtr
, infoPtr
->firstVisible
, TRUE
);
5440 TREEVIEW_UpdateScrollBars(infoPtr
);
5444 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam
, lParam
);
5447 TREEVIEW_Invalidate(infoPtr
, NULL
);
5452 TREEVIEW_StyleChanged(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5454 TRACE("(%lx %lx)\n", wParam
, lParam
);
5456 if (wParam
== GWL_STYLE
)
5458 DWORD dwNewStyle
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
5460 if ((infoPtr
->dwStyle
^ dwNewStyle
) & TVS_CHECKBOXES
)
5462 if (dwNewStyle
& TVS_CHECKBOXES
)
5464 TREEVIEW_InitCheckboxes(infoPtr
);
5465 TRACE("checkboxes enabled\n");
5467 /* set all items to state image index 1 */
5468 TREEVIEW_ResetImageStateIndex(infoPtr
, infoPtr
->root
);
5472 FIXME("tried to disable checkboxes\n");
5476 if ((infoPtr
->dwStyle
^ dwNewStyle
) & TVS_NOTOOLTIPS
)
5478 if (infoPtr
->dwStyle
& TVS_NOTOOLTIPS
)
5480 infoPtr
->hwndToolTip
= COMCTL32_CreateToolTip(infoPtr
->hwnd
);
5481 TRACE("tooltips enabled\n");
5485 DestroyWindow(infoPtr
->hwndToolTip
);
5486 infoPtr
->hwndToolTip
= 0;
5487 TRACE("tooltips disabled\n");
5491 infoPtr
->dwStyle
= dwNewStyle
;
5494 TREEVIEW_EndEditLabelNow(infoPtr
, TRUE
);
5495 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
5496 TREEVIEW_UpdateScrollBars(infoPtr
);
5497 TREEVIEW_Invalidate(infoPtr
, NULL
);
5503 TREEVIEW_SetCursor(const TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5506 TREEVIEW_ITEM
* item
;
5510 ScreenToClient(infoPtr
->hwnd
, &pt
);
5512 item
= TREEVIEW_HitTestPoint(infoPtr
, pt
);
5514 memset(&nmmouse
, 0, sizeof(nmmouse
));
5517 nmmouse
.dwItemSpec
= (DWORD_PTR
)item
;
5518 nmmouse
.dwItemData
= item
->lParam
;
5522 nmmouse
.dwHitInfo
= lParam
;
5523 if (TREEVIEW_SendRealNotify(infoPtr
, NM_SETCURSOR
, &nmmouse
.hdr
))
5526 if (item
&& (infoPtr
->dwStyle
& TVS_TRACKSELECT
))
5528 SetCursor(infoPtr
->hcurHand
);
5532 return DefWindowProcW(infoPtr
->hwnd
, WM_SETCURSOR
, wParam
, lParam
);
5536 TREEVIEW_SetFocus(TREEVIEW_INFO
*infoPtr
)
5540 if (!infoPtr
->selectedItem
)
5542 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, infoPtr
->firstVisible
,
5546 TREEVIEW_Invalidate(infoPtr
, infoPtr
->selectedItem
);
5547 TREEVIEW_SendSimpleNotify(infoPtr
, NM_SETFOCUS
);
5552 TREEVIEW_KillFocus(const TREEVIEW_INFO
*infoPtr
)
5556 TREEVIEW_Invalidate(infoPtr
, infoPtr
->selectedItem
);
5557 UpdateWindow(infoPtr
->hwnd
);
5558 TREEVIEW_SendSimpleNotify(infoPtr
, NM_KILLFOCUS
);
5562 /* update theme after a WM_THEMECHANGED message */
5563 static LRESULT
TREEVIEW_ThemeChanged(const TREEVIEW_INFO
*infoPtr
)
5565 HTHEME theme
= GetWindowTheme (infoPtr
->hwnd
);
5566 CloseThemeData (theme
);
5567 OpenThemeData (infoPtr
->hwnd
, themeClass
);
5572 static LRESULT WINAPI
5573 TREEVIEW_WindowProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
5575 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(hwnd
);
5577 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd
, uMsg
, wParam
, lParam
);
5579 if (infoPtr
) TREEVIEW_VerifyTree(infoPtr
);
5582 if (uMsg
== WM_CREATE
)
5583 TREEVIEW_Create(hwnd
, (LPCREATESTRUCTW
)lParam
);
5590 case TVM_CREATEDRAGIMAGE
:
5591 return TREEVIEW_CreateDragImage(infoPtr
, lParam
);
5593 case TVM_DELETEITEM
:
5594 return TREEVIEW_DeleteItem(infoPtr
, (HTREEITEM
)lParam
);
5596 case TVM_EDITLABELA
:
5597 case TVM_EDITLABELW
:
5598 return (LRESULT
)TREEVIEW_EditLabel(infoPtr
, (HTREEITEM
)lParam
);
5600 case TVM_ENDEDITLABELNOW
:
5601 return TREEVIEW_EndEditLabelNow(infoPtr
, (BOOL
)wParam
);
5603 case TVM_ENSUREVISIBLE
:
5604 return TREEVIEW_EnsureVisible(infoPtr
, (HTREEITEM
)lParam
, TRUE
);
5607 return TREEVIEW_ExpandMsg(infoPtr
, (UINT
)wParam
, (HTREEITEM
)lParam
);
5609 case TVM_GETBKCOLOR
:
5610 return TREEVIEW_GetBkColor(infoPtr
);
5613 return TREEVIEW_GetCount(infoPtr
);
5615 case TVM_GETEDITCONTROL
:
5616 return TREEVIEW_GetEditControl(infoPtr
);
5618 case TVM_GETIMAGELIST
:
5619 return TREEVIEW_GetImageList(infoPtr
, wParam
);
5622 return TREEVIEW_GetIndent(infoPtr
);
5624 case TVM_GETINSERTMARKCOLOR
:
5625 return TREEVIEW_GetInsertMarkColor(infoPtr
);
5627 case TVM_GETISEARCHSTRINGA
:
5628 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5631 case TVM_GETISEARCHSTRINGW
:
5632 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5637 return TREEVIEW_GetItemT(infoPtr
, (LPTVITEMEXW
)lParam
,
5638 uMsg
== TVM_GETITEMW
);
5639 case TVM_GETITEMHEIGHT
:
5640 return TREEVIEW_GetItemHeight(infoPtr
);
5642 case TVM_GETITEMRECT
:
5643 return TREEVIEW_GetItemRect(infoPtr
, (BOOL
)wParam
, (LPRECT
)lParam
);
5645 case TVM_GETITEMSTATE
:
5646 return TREEVIEW_GetItemState(infoPtr
, (HTREEITEM
)wParam
, (UINT
)lParam
);
5648 case TVM_GETLINECOLOR
:
5649 return TREEVIEW_GetLineColor(infoPtr
);
5651 case TVM_GETNEXTITEM
:
5652 return TREEVIEW_GetNextItem(infoPtr
, (UINT
)wParam
, (HTREEITEM
)lParam
);
5654 case TVM_GETSCROLLTIME
:
5655 return TREEVIEW_GetScrollTime(infoPtr
);
5657 case TVM_GETTEXTCOLOR
:
5658 return TREEVIEW_GetTextColor(infoPtr
);
5660 case TVM_GETTOOLTIPS
:
5661 return TREEVIEW_GetToolTips(infoPtr
);
5663 case TVM_GETUNICODEFORMAT
:
5664 return TREEVIEW_GetUnicodeFormat(infoPtr
);
5666 case TVM_GETVISIBLECOUNT
:
5667 return TREEVIEW_GetVisibleCount(infoPtr
);
5670 return TREEVIEW_HitTest(infoPtr
, (LPTVHITTESTINFO
)lParam
);
5672 case TVM_INSERTITEMA
:
5673 case TVM_INSERTITEMW
:
5674 return TREEVIEW_InsertItemT(infoPtr
, (LPTVINSERTSTRUCTW
)lParam
,
5675 uMsg
== TVM_INSERTITEMW
);
5676 case TVM_SELECTITEM
:
5677 return TREEVIEW_SelectItem(infoPtr
, (INT
)wParam
, (HTREEITEM
)lParam
);
5679 case TVM_SETBKCOLOR
:
5680 return TREEVIEW_SetBkColor(infoPtr
, (COLORREF
)lParam
);
5682 case TVM_SETIMAGELIST
:
5683 return TREEVIEW_SetImageList(infoPtr
, wParam
, (HIMAGELIST
)lParam
);
5686 return TREEVIEW_SetIndent(infoPtr
, (UINT
)wParam
);
5688 case TVM_SETINSERTMARK
:
5689 return TREEVIEW_SetInsertMark(infoPtr
, (BOOL
)wParam
, (HTREEITEM
)lParam
);
5691 case TVM_SETINSERTMARKCOLOR
:
5692 return TREEVIEW_SetInsertMarkColor(infoPtr
, (COLORREF
)lParam
);
5696 return TREEVIEW_SetItemT(infoPtr
, (LPTVITEMEXW
)lParam
,
5697 uMsg
== TVM_SETITEMW
);
5698 case TVM_SETLINECOLOR
:
5699 return TREEVIEW_SetLineColor(infoPtr
, (COLORREF
)lParam
);
5701 case TVM_SETITEMHEIGHT
:
5702 return TREEVIEW_SetItemHeight(infoPtr
, (INT
)(SHORT
)wParam
);
5704 case TVM_SETSCROLLTIME
:
5705 return TREEVIEW_SetScrollTime(infoPtr
, (UINT
)wParam
);
5707 case TVM_SETTEXTCOLOR
:
5708 return TREEVIEW_SetTextColor(infoPtr
, (COLORREF
)lParam
);
5710 case TVM_SETTOOLTIPS
:
5711 return TREEVIEW_SetToolTips(infoPtr
, (HWND
)wParam
);
5713 case TVM_SETUNICODEFORMAT
:
5714 return TREEVIEW_SetUnicodeFormat(infoPtr
, (BOOL
)wParam
);
5716 case TVM_SORTCHILDREN
:
5717 return TREEVIEW_SortChildren(infoPtr
, lParam
);
5719 case TVM_SORTCHILDRENCB
:
5720 return TREEVIEW_SortChildrenCB(infoPtr
, (LPTVSORTCB
)lParam
);
5723 return TREEVIEW_ProcessLetterKeys(infoPtr
, wParam
, lParam
);
5726 return TREEVIEW_Command(infoPtr
, wParam
, lParam
);
5729 return TREEVIEW_Destroy(infoPtr
);
5734 return TREEVIEW_EraseBackground(infoPtr
, (HDC
)wParam
);
5737 return DLGC_WANTARROWS
| DLGC_WANTCHARS
;
5740 return TREEVIEW_GetFont(infoPtr
);
5743 return TREEVIEW_HScroll(infoPtr
, wParam
);
5746 return TREEVIEW_KeyDown(infoPtr
, wParam
);
5749 return TREEVIEW_KillFocus(infoPtr
);
5751 case WM_LBUTTONDBLCLK
:
5752 return TREEVIEW_LButtonDoubleClick(infoPtr
, lParam
);
5754 case WM_LBUTTONDOWN
:
5755 return TREEVIEW_LButtonDown(infoPtr
, lParam
);
5757 /* WM_MBUTTONDOWN */
5760 return TREEVIEW_MouseLeave(infoPtr
);
5763 return TREEVIEW_MouseMove(infoPtr
, lParam
);
5765 case WM_NCLBUTTONDOWN
:
5766 if (infoPtr
->hwndEdit
)
5767 SetFocus(infoPtr
->hwnd
);
5771 return TREEVIEW_NCPaint (infoPtr
, (HRGN
)wParam
, lParam
);
5774 return TREEVIEW_Notify(infoPtr
, wParam
, lParam
);
5776 case WM_NOTIFYFORMAT
:
5777 return TREEVIEW_NotifyFormat(infoPtr
, (HWND
)wParam
, (UINT
)lParam
);
5779 case WM_PRINTCLIENT
:
5780 return TREEVIEW_PrintClient(infoPtr
, (HDC
)wParam
, lParam
);
5783 return TREEVIEW_Paint(infoPtr
, (HDC
)wParam
);
5785 case WM_RBUTTONDOWN
:
5786 return TREEVIEW_RButtonDown(infoPtr
, lParam
);
5789 return TREEVIEW_SetCursor(infoPtr
, wParam
, lParam
);
5792 return TREEVIEW_SetFocus(infoPtr
);
5795 return TREEVIEW_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
5798 return TREEVIEW_SetRedraw(infoPtr
, wParam
);
5801 return TREEVIEW_Size(infoPtr
, wParam
, lParam
);
5803 case WM_STYLECHANGED
:
5804 return TREEVIEW_StyleChanged(infoPtr
, wParam
, lParam
);
5806 case WM_SYSCOLORCHANGE
:
5807 COMCTL32_RefreshSysColors();
5813 return TREEVIEW_HandleTimer(infoPtr
, wParam
);
5815 case WM_THEMECHANGED
:
5816 return TREEVIEW_ThemeChanged (infoPtr
);
5819 return TREEVIEW_VScroll(infoPtr
, wParam
);
5821 /* WM_WININICHANGE */
5824 return TREEVIEW_MouseWheel(infoPtr
, wParam
, lParam
);
5827 TRACE("drawItem\n");
5831 /* This mostly catches MFC and Delphi messages. :( */
5832 if ((uMsg
>= WM_USER
) && (uMsg
< WM_APP
) && !COMCTL32_IsReflectedMessage(uMsg
))
5833 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg
, wParam
, lParam
);
5835 return DefWindowProcW(hwnd
, uMsg
, wParam
, lParam
);
5840 /* Class Registration ***************************************************/
5843 TREEVIEW_Register(void)
5849 ZeroMemory(&wndClass
, sizeof(WNDCLASSW
));
5850 wndClass
.style
= CS_GLOBALCLASS
| CS_DBLCLKS
;
5851 wndClass
.lpfnWndProc
= TREEVIEW_WindowProc
;
5852 wndClass
.cbClsExtra
= 0;
5853 wndClass
.cbWndExtra
= sizeof(TREEVIEW_INFO
*);
5855 wndClass
.hCursor
= LoadCursorW(0, (LPWSTR
)IDC_ARROW
);
5856 wndClass
.hbrBackground
= 0;
5857 wndClass
.lpszClassName
= WC_TREEVIEWW
;
5859 RegisterClassW(&wndClass
);
5864 TREEVIEW_Unregister(void)
5866 UnregisterClassW(WC_TREEVIEWW
, NULL
);
5870 /* Tree Verification ****************************************************/
5873 TREEVIEW_VerifyChildren(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
);
5875 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO
*infoPtr
,
5876 const TREEVIEW_ITEM
*item
)
5878 assert(infoPtr
!= NULL
);
5879 assert(item
!= NULL
);
5881 /* both NULL, or both non-null */
5882 assert((item
->firstChild
== NULL
) == (item
->lastChild
== NULL
));
5884 assert(item
->firstChild
!= item
);
5885 assert(item
->lastChild
!= item
);
5887 if (item
->firstChild
)
5889 assert(item
->firstChild
->parent
== item
);
5890 assert(item
->firstChild
->prevSibling
== NULL
);
5893 if (item
->lastChild
)
5895 assert(item
->lastChild
->parent
== item
);
5896 assert(item
->lastChild
->nextSibling
== NULL
);
5899 assert(item
->nextSibling
!= item
);
5900 if (item
->nextSibling
)
5902 assert(item
->nextSibling
->parent
== item
->parent
);
5903 assert(item
->nextSibling
->prevSibling
== item
);
5906 assert(item
->prevSibling
!= item
);
5907 if (item
->prevSibling
)
5909 assert(item
->prevSibling
->parent
== item
->parent
);
5910 assert(item
->prevSibling
->nextSibling
== item
);
5915 TREEVIEW_VerifyItem(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
5917 assert(item
!= NULL
);
5919 assert(item
->parent
!= NULL
);
5920 assert(item
->parent
!= item
);
5921 assert(item
->iLevel
== item
->parent
->iLevel
+ 1);
5923 assert(DPA_GetPtrIndex(infoPtr
->items
, item
) != -1);
5925 TREEVIEW_VerifyItemCommon(infoPtr
, item
);
5927 TREEVIEW_VerifyChildren(infoPtr
, item
);
5931 TREEVIEW_VerifyChildren(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
5933 const TREEVIEW_ITEM
*child
;
5934 assert(item
!= NULL
);
5936 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
5937 TREEVIEW_VerifyItem(infoPtr
, child
);
5941 TREEVIEW_VerifyRoot(TREEVIEW_INFO
*infoPtr
)
5943 TREEVIEW_ITEM
*root
= infoPtr
->root
;
5945 assert(root
!= NULL
);
5946 assert(root
->iLevel
== -1);
5947 assert(root
->parent
== NULL
);
5948 assert(root
->prevSibling
== NULL
);
5950 TREEVIEW_VerifyItemCommon(infoPtr
, root
);
5952 TREEVIEW_VerifyChildren(infoPtr
, root
);
5956 TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
)
5958 if (!TRACE_ON(treeview
)) return;
5960 assert(infoPtr
!= NULL
);
5961 TREEVIEW_VerifyRoot(infoPtr
);