3 * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4 * Copyright 1998,1999 Alex Priem <alexp@sci.kun.nl>
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_EXPANDPARTIAL, TVIS_EX_FLAT,
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
45 #include <wine/exception.h>
47 WINE_DEFAULT_DEBUG_CHANNEL(treeview
);
49 /* internal structures */
50 typedef struct tagTREEVIEW_INFO
53 HWND hwndNotify
; /* Owner window to send notifications to */
58 UINT uNumItems
; /* number of valid TREEVIEW_ITEMs */
59 INT cdmode
; /* last custom draw setting */
60 UINT uScrollTime
; /* max. time for scrolling in milliseconds */
61 BOOL bRedraw
; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
63 UINT uItemHeight
; /* item height */
66 LONG clientWidth
; /* width of control window */
67 LONG clientHeight
; /* height of control window */
69 LONG treeWidth
; /* width of visible tree items */
70 LONG treeHeight
; /* height of visible tree items */
72 UINT uIndent
; /* indentation in pixels */
73 HTREEITEM selectedItem
; /* handle to selected item or 0 if none */
74 HTREEITEM hotItem
; /* handle currently under cursor, 0 if none */
75 HTREEITEM focusedItem
; /* item that was under the cursor when WM_LBUTTONDOWN was received */
76 HTREEITEM editItem
; /* item being edited with builtin edit box */
78 HTREEITEM firstVisible
; /* handle to item whose top edge is at y = 0 */
80 HTREEITEM dropItem
; /* handle to item selected by drag cursor */
81 HTREEITEM insertMarkItem
; /* item after which insertion mark is placed */
82 BOOL insertBeforeorAfter
; /* flag used by TVM_SETINSERTMARK */
83 HIMAGELIST dragList
; /* Bitmap of dragged item */
89 COLORREF clrInsertMark
;
94 HFONT hBoldUnderlineFont
;
99 WNDPROC wpEditOrig
; /* orig window proc for subclassing edit */
100 BOOL bIgnoreEditKillFocus
;
103 BOOL bNtfUnicode
; /* TRUE if should send NOTIFY with W */
104 HIMAGELIST himlNormal
;
105 int normalImageHeight
;
106 int normalImageWidth
;
107 HIMAGELIST himlState
;
108 int stateImageHeight
;
112 DWORD lastKeyPressTimestamp
;
114 INT nSearchParamLength
;
115 WCHAR szSearchParam
[ MAX_PATH
];
118 typedef struct _TREEITEM
/* HTREEITEM is a _TREEINFO *. */
120 HTREEITEM parent
; /* handle to parent or 0 if at root */
121 HTREEITEM nextSibling
; /* handle to next item in list, 0 if last */
122 HTREEITEM firstChild
; /* handle to first child or 0 if no child */
134 int iIntegral
; /* item height multiplier (1 is normal) */
135 int iLevel
; /* indentation level:0=root level */
137 HTREEITEM prevSibling
; /* handle to prev item in list, 0 if first */
143 LONG textWidth
; /* horizontal text extent for pszText */
144 LONG visibleOrder
; /* Depth-first numbering of the items whose ancestors are all expanded,
145 corresponding to a top-to-bottom ordering in the tree view.
146 Each item takes up "item.iIntegral" spots in the visible order.
147 0 is the root's first child. */
148 const TREEVIEW_INFO
*infoPtr
; /* tree data this item belongs to */
151 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
152 #define KEY_DELAY 450
154 /* bitflags for infoPtr->uInternalStatus */
156 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
157 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
158 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
159 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
160 #define TV_RDRAG 0x10 /* ditto Rbutton */
161 #define TV_RDRAGGING 0x20
163 /* bitflags for infoPtr->timer */
165 #define TV_EDIT_TIMER 2
166 #define TV_EDIT_TIMER_SET 2
168 #define TEXT_CALLBACK_SIZE 260
170 #define TREEVIEW_LEFT_MARGIN 8
172 #define MINIMUM_INDENT 19
174 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
176 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
177 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
178 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
180 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
181 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
182 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
183 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
185 static const WCHAR themeClass
[] = { 'T','r','e','e','v','i','e','w',0 };
188 typedef VOID (*TREEVIEW_ItemEnumFunc
)(TREEVIEW_INFO
*, TREEVIEW_ITEM
*,LPVOID
);
191 static VOID
TREEVIEW_Invalidate(const TREEVIEW_INFO
*, const TREEVIEW_ITEM
*);
193 static LRESULT
TREEVIEW_DoSelectItem(TREEVIEW_INFO
*, INT
, HTREEITEM
, INT
);
194 static VOID
TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*, TREEVIEW_ITEM
*, BOOL
);
195 static LRESULT
TREEVIEW_EnsureVisible(TREEVIEW_INFO
*, HTREEITEM
, BOOL
);
196 static LRESULT
TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
);
197 static VOID
TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
);
198 static LRESULT
TREEVIEW_HScroll(TREEVIEW_INFO
*, WPARAM
);
200 /* Random Utilities *****************************************************/
201 static void TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
);
203 /* Returns the treeview private data if hwnd is a treeview.
204 * Otherwise returns an undefined value. */
205 static inline TREEVIEW_INFO
*
206 TREEVIEW_GetInfoPtr(HWND hwnd
)
208 return (TREEVIEW_INFO
*)GetWindowLongPtrW(hwnd
, 0);
211 /* Don't call this. Nothing wants an item index. */
213 TREEVIEW_GetItemIndex(const TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
215 return DPA_GetPtrIndex(infoPtr
->items
, handle
);
218 /* Checks if item has changed and needs to be redrawn */
219 static inline BOOL
item_changed (const TREEVIEW_ITEM
*tiOld
, const TREEVIEW_ITEM
*tiNew
,
220 const TVITEMEXW
*tvChange
)
222 /* Number of children has changed */
223 if ((tvChange
->mask
& TVIF_CHILDREN
) && (tiOld
->cChildren
!= tiNew
->cChildren
))
226 /* Image has changed and it's not a callback */
227 if ((tvChange
->mask
& TVIF_IMAGE
) && (tiOld
->iImage
!= tiNew
->iImage
) &&
228 tiNew
->iImage
!= I_IMAGECALLBACK
)
231 /* Selected image has changed and it's not a callback */
232 if ((tvChange
->mask
& TVIF_SELECTEDIMAGE
) && (tiOld
->iSelectedImage
!= tiNew
->iSelectedImage
) &&
233 tiNew
->iSelectedImage
!= I_IMAGECALLBACK
)
236 if ((tvChange
->mask
& TVIF_EXPANDEDIMAGE
) && (tiOld
->iExpandedImage
!= tiNew
->iExpandedImage
) &&
237 tiNew
->iExpandedImage
!= I_IMAGECALLBACK
)
240 /* Text has changed and it's not a callback */
241 if ((tvChange
->mask
& TVIF_TEXT
) && (tiOld
->pszText
!= tiNew
->pszText
) &&
242 tiNew
->pszText
!= LPSTR_TEXTCALLBACKW
)
245 /* Indent has changed */
246 if ((tvChange
->mask
& TVIF_INTEGRAL
) && (tiOld
->iIntegral
!= tiNew
->iIntegral
))
249 /* Item state has changed */
250 if ((tvChange
->mask
& TVIF_STATE
) && ((tiOld
->state
^ tiNew
->state
) & tvChange
->stateMask
))
256 /***************************************************************************
257 * This method checks that handle is an item for this tree.
260 TREEVIEW_ValidItem(const TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
262 if (TREEVIEW_GetItemIndex(infoPtr
, handle
) == -1)
264 TRACE("invalid item %p\n", handle
);
272 TREEVIEW_CreateBoldFont(HFONT hOrigFont
)
276 GetObjectW(hOrigFont
, sizeof(font
), &font
);
277 font
.lfWeight
= FW_BOLD
;
278 return CreateFontIndirectW(&font
);
282 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont
)
286 GetObjectW(hOrigFont
, sizeof(font
), &font
);
287 font
.lfUnderline
= TRUE
;
288 return CreateFontIndirectW(&font
);
292 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont
)
296 GetObjectW(hfont
, sizeof(font
), &font
);
297 font
.lfWeight
= FW_BOLD
;
298 font
.lfUnderline
= TRUE
;
299 return CreateFontIndirectW(&font
);
303 TREEVIEW_FontForItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
305 if ((infoPtr
->dwStyle
& TVS_TRACKSELECT
) && (item
== infoPtr
->hotItem
))
306 return item
->state
& TVIS_BOLD
? infoPtr
->hBoldUnderlineFont
: infoPtr
->hUnderlineFont
;
307 if (item
->state
& TVIS_BOLD
)
308 return infoPtr
->hBoldFont
;
309 return infoPtr
->hFont
;
312 /* for trace/debugging purposes only */
314 TREEVIEW_ItemName(const TREEVIEW_ITEM
*item
)
316 if (item
== NULL
) return "<null item>";
317 if (item
->pszText
== LPSTR_TEXTCALLBACKW
) return "<callback>";
318 if (item
->pszText
== NULL
) return "<null>";
319 return debugstr_w(item
->pszText
);
322 /* An item is not a child of itself. */
324 TREEVIEW_IsChildOf(const TREEVIEW_ITEM
*parent
, const TREEVIEW_ITEM
*child
)
328 child
= child
->parent
;
329 if (child
== parent
) return TRUE
;
330 } while (child
!= NULL
);
336 /* Tree Traversal *******************************************************/
338 /***************************************************************************
339 * This method returns the last expanded sibling or child child item
342 static TREEVIEW_ITEM
*
343 TREEVIEW_GetLastListItem(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
345 if (!item
) return NULL
;
347 while (item
->lastChild
)
349 if (item
->state
& TVIS_EXPANDED
)
350 item
= item
->lastChild
;
355 if (item
== infoPtr
->root
)
361 /***************************************************************************
362 * This method returns the previous non-hidden item in the list not
363 * considering the tree hierarchy.
365 static TREEVIEW_ITEM
*
366 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*tvItem
)
368 if (tvItem
->prevSibling
)
370 /* This item has a prevSibling, get the last item in the sibling's tree. */
371 TREEVIEW_ITEM
*upItem
= tvItem
->prevSibling
;
373 if ((upItem
->state
& TVIS_EXPANDED
) && upItem
->lastChild
!= NULL
)
374 return TREEVIEW_GetLastListItem(infoPtr
, upItem
->lastChild
);
380 /* this item does not have a prevSibling, get the parent */
381 return (tvItem
->parent
!= infoPtr
->root
) ? tvItem
->parent
: NULL
;
386 /***************************************************************************
387 * This method returns the next physical item in the treeview not
388 * considering the tree hierarchy.
390 static TREEVIEW_ITEM
*
391 TREEVIEW_GetNextListItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*tvItem
)
394 * If this item has children and is expanded, return the first child
396 if ((tvItem
->state
& TVIS_EXPANDED
) && tvItem
->firstChild
!= NULL
)
398 return tvItem
->firstChild
;
403 * try to get the sibling
405 if (tvItem
->nextSibling
)
406 return tvItem
->nextSibling
;
409 * Otherwise, get the parent's sibling.
411 while (tvItem
->parent
)
413 tvItem
= tvItem
->parent
;
415 if (tvItem
->nextSibling
)
416 return tvItem
->nextSibling
;
422 /***************************************************************************
423 * This method returns the nth item starting at the given item. It returns
424 * the last item (or first) we we run out of items.
426 * Will scroll backward if count is <0.
427 * forward if count is >0.
429 static TREEVIEW_ITEM
*
430 TREEVIEW_GetListItem(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
433 TREEVIEW_ITEM
*(*next_item
)(const TREEVIEW_INFO
*, const TREEVIEW_ITEM
*);
434 TREEVIEW_ITEM
*previousItem
;
436 assert(item
!= NULL
);
440 next_item
= TREEVIEW_GetNextListItem
;
445 next_item
= TREEVIEW_GetPrevListItem
;
453 item
= next_item(infoPtr
, item
);
455 } while (--count
&& item
!= NULL
);
458 return item
? item
: previousItem
;
461 /* Notifications ************************************************************/
463 static INT
get_notifycode(const TREEVIEW_INFO
*infoPtr
, INT code
)
465 if (!infoPtr
->bNtfUnicode
) {
467 case TVN_SELCHANGINGW
: return TVN_SELCHANGINGA
;
468 case TVN_SELCHANGEDW
: return TVN_SELCHANGEDA
;
469 case TVN_GETDISPINFOW
: return TVN_GETDISPINFOA
;
470 case TVN_SETDISPINFOW
: return TVN_SETDISPINFOA
;
471 case TVN_ITEMEXPANDINGW
: return TVN_ITEMEXPANDINGA
;
472 case TVN_ITEMEXPANDEDW
: return TVN_ITEMEXPANDEDA
;
473 case TVN_BEGINDRAGW
: return TVN_BEGINDRAGA
;
474 case TVN_BEGINRDRAGW
: return TVN_BEGINRDRAGA
;
475 case TVN_DELETEITEMW
: return TVN_DELETEITEMA
;
476 case TVN_BEGINLABELEDITW
: return TVN_BEGINLABELEDITA
;
477 case TVN_ENDLABELEDITW
: return TVN_ENDLABELEDITA
;
478 case TVN_GETINFOTIPW
: return TVN_GETINFOTIPA
;
485 TREEVIEW_SendRealNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
, NMHDR
*hdr
)
487 TRACE("code=%d, hdr=%p\n", code
, hdr
);
489 hdr
->hwndFrom
= infoPtr
->hwnd
;
490 hdr
->idFrom
= GetWindowLongPtrW(infoPtr
->hwnd
, GWLP_ID
);
491 hdr
->code
= get_notifycode(infoPtr
, code
);
493 return SendMessageW(infoPtr
->hwndNotify
, WM_NOTIFY
, hdr
->idFrom
, (LPARAM
)hdr
);
497 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
)
500 return TREEVIEW_SendRealNotify(infoPtr
, code
, &hdr
);
504 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO
*infoPtr
, UINT mask
, TVITEMW
*tvItem
, TREEVIEW_ITEM
*item
)
507 tvItem
->hItem
= item
;
508 tvItem
->state
= item
->state
;
509 tvItem
->stateMask
= 0;
510 tvItem
->iImage
= item
->iImage
;
511 tvItem
->iSelectedImage
= item
->iSelectedImage
;
512 tvItem
->cChildren
= item
->cChildren
;
513 tvItem
->lParam
= item
->lParam
;
517 if (!infoPtr
->bNtfUnicode
)
519 tvItem
->cchTextMax
= WideCharToMultiByte( CP_ACP
, 0, item
->pszText
, -1, NULL
, 0, NULL
, NULL
);
520 tvItem
->pszText
= Alloc (tvItem
->cchTextMax
);
521 WideCharToMultiByte( CP_ACP
, 0, item
->pszText
, -1, (LPSTR
)tvItem
->pszText
, tvItem
->cchTextMax
, 0, 0 );
525 tvItem
->cchTextMax
= item
->cchTextMax
;
526 tvItem
->pszText
= item
->pszText
;
531 tvItem
->cchTextMax
= 0;
532 tvItem
->pszText
= NULL
;
537 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
, UINT action
,
538 UINT mask
, HTREEITEM oldItem
, HTREEITEM newItem
)
543 TRACE("code:%d action:0x%x olditem:%p newitem:%p\n",
544 code
, action
, oldItem
, newItem
);
546 memset(&nmhdr
, 0, sizeof(NMTREEVIEWW
));
547 nmhdr
.action
= action
;
550 TREEVIEW_TVItemFromItem(infoPtr
, mask
, &nmhdr
.itemOld
, oldItem
);
553 TREEVIEW_TVItemFromItem(infoPtr
, mask
, &nmhdr
.itemNew
, newItem
);
558 ret
= TREEVIEW_SendRealNotify(infoPtr
, code
, &nmhdr
.hdr
);
559 if (!infoPtr
->bNtfUnicode
)
561 Free(nmhdr
.itemOld
.pszText
);
562 Free(nmhdr
.itemNew
.pszText
);
568 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO
*infoPtr
, UINT code
,
569 HTREEITEM dragItem
, POINT pt
)
573 TRACE("code:%d dragitem:%p\n", code
, dragItem
);
576 nmhdr
.itemNew
.mask
= TVIF_STATE
| TVIF_PARAM
| TVIF_HANDLE
;
577 nmhdr
.itemNew
.hItem
= dragItem
;
578 nmhdr
.itemNew
.state
= dragItem
->state
;
579 nmhdr
.itemNew
.lParam
= dragItem
->lParam
;
581 nmhdr
.ptDrag
.x
= pt
.x
;
582 nmhdr
.ptDrag
.y
= pt
.y
;
584 return TREEVIEW_SendRealNotify(infoPtr
, code
, &nmhdr
.hdr
);
589 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO
*infoPtr
, DWORD dwDrawStage
,
592 NMTVCUSTOMDRAW nmcdhdr
;
595 TRACE("drawstage:0x%x hdc:%p\n", dwDrawStage
, hdc
);
597 nmcd
= &nmcdhdr
.nmcd
;
598 nmcd
->dwDrawStage
= dwDrawStage
;
601 nmcd
->dwItemSpec
= 0;
602 nmcd
->uItemState
= 0;
603 nmcd
->lItemlParam
= 0;
604 nmcdhdr
.clrText
= infoPtr
->clrText
;
605 nmcdhdr
.clrTextBk
= infoPtr
->clrBk
;
608 return TREEVIEW_SendRealNotify(infoPtr
, NM_CUSTOMDRAW
, &nmcdhdr
.nmcd
.hdr
);
611 /* FIXME: need to find out when the flags in uItemState need to be set */
614 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO
*infoPtr
, HDC hdc
,
615 TREEVIEW_ITEM
*item
, UINT uItemDrawState
,
616 NMTVCUSTOMDRAW
*nmcdhdr
)
620 DWORD_PTR dwItemSpec
;
623 dwDrawStage
= CDDS_ITEM
| uItemDrawState
;
624 dwItemSpec
= (DWORD_PTR
)item
;
626 if (item
->state
& TVIS_SELECTED
)
627 uItemState
|= CDIS_SELECTED
;
628 if (item
== infoPtr
->selectedItem
)
629 uItemState
|= CDIS_FOCUS
;
630 if (item
== infoPtr
->hotItem
)
631 uItemState
|= CDIS_HOT
;
633 nmcd
= &nmcdhdr
->nmcd
;
634 nmcd
->dwDrawStage
= dwDrawStage
;
636 nmcd
->rc
= item
->rect
;
637 nmcd
->dwItemSpec
= dwItemSpec
;
638 nmcd
->uItemState
= uItemState
;
639 nmcd
->lItemlParam
= item
->lParam
;
640 nmcdhdr
->iLevel
= item
->iLevel
;
642 TRACE("drawstage:0x%x hdc:%p item:%lx, itemstate:0x%x, lItemlParam:0x%lx\n",
643 nmcd
->dwDrawStage
, nmcd
->hdc
, nmcd
->dwItemSpec
,
644 nmcd
->uItemState
, nmcd
->lItemlParam
);
646 return TREEVIEW_SendRealNotify(infoPtr
, NM_CUSTOMDRAW
, &nmcdhdr
->nmcd
.hdr
);
650 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*editItem
)
655 TREEVIEW_TVItemFromItem(infoPtr
, TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
| TVIF_TEXT
,
656 &tvdi
.item
, editItem
);
658 ret
= TREEVIEW_SendRealNotify(infoPtr
, TVN_BEGINLABELEDITW
, &tvdi
.hdr
);
660 if (!infoPtr
->bNtfUnicode
)
661 Free(tvdi
.item
.pszText
);
667 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
670 NMTVDISPINFOEXW callback
;
672 TRACE("mask=0x%x, callbackmask=0x%x\n", mask
, item
->callbackMask
);
673 mask
&= item
->callbackMask
;
675 if (mask
== 0) return;
677 /* 'state' always contains valid value, as well as 'lParam'.
678 * All other parameters are uninitialized.
680 callback
.item
.pszText
= item
->pszText
;
681 callback
.item
.cchTextMax
= item
->cchTextMax
;
682 callback
.item
.mask
= mask
;
683 callback
.item
.hItem
= item
;
684 callback
.item
.state
= item
->state
;
685 callback
.item
.lParam
= item
->lParam
;
687 /* If text is changed we need to recalculate textWidth */
688 if (mask
& TVIF_TEXT
)
691 TREEVIEW_SendRealNotify(infoPtr
, TVN_GETDISPINFOW
, &callback
.hdr
);
692 TRACE("resulting code 0x%08x\n", callback
.hdr
.code
);
694 /* It may have changed due to a call to SetItem. */
695 mask
&= item
->callbackMask
;
697 if ((mask
& TVIF_TEXT
) && callback
.item
.pszText
!= item
->pszText
)
699 /* Instead of copying text into our buffer user specified his own */
700 if (!infoPtr
->bNtfUnicode
&& (callback
.hdr
.code
== TVN_GETDISPINFOA
)) {
703 int len
= MultiByteToWideChar( CP_ACP
, 0,
704 (LPSTR
)callback
.item
.pszText
, -1,
706 buflen
= max((len
)*sizeof(WCHAR
), TEXT_CALLBACK_SIZE
);
707 newText
= ReAlloc(item
->pszText
, buflen
);
709 TRACE("returned str %s, len=%d, buflen=%d\n",
710 debugstr_a((LPSTR
)callback
.item
.pszText
), len
, buflen
);
714 item
->pszText
= newText
;
715 MultiByteToWideChar( CP_ACP
, 0,
716 (LPSTR
)callback
.item
.pszText
, -1,
717 item
->pszText
, buflen
/sizeof(WCHAR
));
718 item
->cchTextMax
= buflen
/sizeof(WCHAR
);
720 /* If ReAlloc fails we have nothing to do, but keep original text */
723 int len
= max(lstrlenW(callback
.item
.pszText
) + 1,
725 LPWSTR newText
= ReAlloc(item
->pszText
, len
);
727 TRACE("returned wstr %s, len=%d\n",
728 debugstr_w(callback
.item
.pszText
), len
);
732 item
->pszText
= newText
;
733 strcpyW(item
->pszText
, callback
.item
.pszText
);
734 item
->cchTextMax
= len
;
736 /* If ReAlloc fails we have nothing to do, but keep original text */
739 else if (mask
& TVIF_TEXT
) {
740 /* User put text into our buffer, that is ok unless A string */
741 if (!infoPtr
->bNtfUnicode
&& (callback
.hdr
.code
== TVN_GETDISPINFOA
)) {
744 int len
= MultiByteToWideChar( CP_ACP
, 0,
745 (LPSTR
)callback
.item
.pszText
, -1,
747 buflen
= max((len
)*sizeof(WCHAR
), TEXT_CALLBACK_SIZE
);
748 newText
= Alloc(buflen
);
750 TRACE("same buffer str %s, len=%d, buflen=%d\n",
751 debugstr_a((LPSTR
)callback
.item
.pszText
), len
, buflen
);
755 LPWSTR oldText
= item
->pszText
;
756 item
->pszText
= newText
;
757 MultiByteToWideChar( CP_ACP
, 0,
758 (LPSTR
)callback
.item
.pszText
, -1,
759 item
->pszText
, buflen
/sizeof(WCHAR
));
760 item
->cchTextMax
= buflen
/sizeof(WCHAR
);
766 if (mask
& TVIF_IMAGE
)
767 item
->iImage
= callback
.item
.iImage
;
769 if (mask
& TVIF_SELECTEDIMAGE
)
770 item
->iSelectedImage
= callback
.item
.iSelectedImage
;
772 if (mask
& TVIF_EXPANDEDIMAGE
)
773 item
->iExpandedImage
= callback
.item
.iExpandedImage
;
775 if (mask
& TVIF_CHILDREN
)
776 item
->cChildren
= callback
.item
.cChildren
;
778 if (callback
.item
.mask
& TVIF_STATE
)
780 item
->state
&= ~callback
.item
.stateMask
;
781 item
->state
|= (callback
.item
.state
& callback
.item
.stateMask
);
784 /* These members are now permanently set. */
785 if (callback
.item
.mask
& TVIF_DI_SETITEM
)
786 item
->callbackMask
&= ~callback
.item
.mask
;
789 /***************************************************************************
790 * This function uses cChildren field to decide whether the item has
792 * Note: if this returns TRUE, the child items may not actually exist,
793 * they could be virtual.
795 * Just use item->firstChild to check for physical children.
798 TREEVIEW_HasChildren(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
800 TREEVIEW_UpdateDispInfo(infoPtr
, item
, TVIF_CHILDREN
);
801 /* Protect for a case when callback field is not changed by a host,
802 otherwise negative values trigger normal notifications. */
803 return item
->cChildren
!= 0 && item
->cChildren
!= I_CHILDRENCALLBACK
;
806 static INT
TREEVIEW_NotifyFormat (TREEVIEW_INFO
*infoPtr
, HWND hwndFrom
, UINT nCommand
)
810 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom
, nCommand
);
812 if (nCommand
!= NF_REQUERY
) return 0;
814 format
= SendMessageW(hwndFrom
, WM_NOTIFYFORMAT
, (WPARAM
)infoPtr
->hwnd
, NF_QUERY
);
815 TRACE("format=%d\n", format
);
817 /* Invalid format returned by NF_QUERY defaults to ANSI*/
818 if (format
!= NFR_ANSI
&& format
!= NFR_UNICODE
)
821 infoPtr
->bNtfUnicode
= (format
== NFR_UNICODE
);
826 /* Item Position ********************************************************/
828 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
830 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
832 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
833 BOOL lar
= ((infoPtr
->dwStyle
& (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
836 item
->linesOffset
= infoPtr
->uIndent
* (lar
? item
->iLevel
: item
->iLevel
- 1)
838 item
->stateOffset
= item
->linesOffset
+ infoPtr
->uIndent
;
839 item
->imageOffset
= item
->stateOffset
840 + (STATEIMAGEINDEX(item
->state
) ? infoPtr
->stateImageWidth
: 0);
841 item
->textOffset
= item
->imageOffset
+ infoPtr
->normalImageWidth
;
845 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
, HDC hDC
)
851 /* DRAW's OM docker creates items like this */
852 if (item
->pszText
== NULL
)
864 hdc
= GetDC(infoPtr
->hwnd
);
865 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, item
));
868 GetTextExtentPoint32W(hdc
, item
->pszText
, strlenW(item
->pszText
), &sz
);
869 item
->textWidth
= sz
.cx
;
873 SelectObject(hdc
, hOldFont
);
879 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
881 item
->rect
.top
= infoPtr
->uItemHeight
*
882 (item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
);
884 item
->rect
.bottom
= item
->rect
.top
885 + infoPtr
->uItemHeight
* item
->iIntegral
- 1;
888 item
->rect
.right
= infoPtr
->clientWidth
;
891 /* We know that only items after start need their order updated. */
893 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*start
)
900 start
= infoPtr
->root
->firstChild
;
904 order
= start
->visibleOrder
;
906 for (item
= start
; item
!= NULL
;
907 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
909 if (!ISVISIBLE(item
) && order
> 0)
910 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, item
);
911 item
->visibleOrder
= order
;
912 order
+= item
->iIntegral
;
915 infoPtr
->maxVisibleOrder
= order
;
917 for (item
= start
; item
!= NULL
;
918 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
920 TREEVIEW_ComputeItemRect(infoPtr
, item
);
925 /* Update metrics of all items in selected subtree.
926 * root must be expanded
929 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*root
)
931 TREEVIEW_ITEM
*sibling
;
935 if (!root
->firstChild
|| !(root
->state
& TVIS_EXPANDED
))
938 root
->state
&= ~TVIS_EXPANDED
;
939 sibling
= TREEVIEW_GetNextListItem(infoPtr
, root
);
940 root
->state
|= TVIS_EXPANDED
;
942 hdc
= GetDC(infoPtr
->hwnd
);
943 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
945 for (; root
!= sibling
;
946 root
= TREEVIEW_GetNextListItem(infoPtr
, root
))
948 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, root
);
950 if (root
->callbackMask
& TVIF_TEXT
)
951 TREEVIEW_UpdateDispInfo(infoPtr
, root
, TVIF_TEXT
);
953 if (root
->textWidth
== 0)
955 SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, root
));
956 TREEVIEW_ComputeTextWidth(infoPtr
, root
, hdc
);
960 SelectObject(hdc
, hOldFont
);
961 ReleaseDC(infoPtr
->hwnd
, hdc
);
964 /* Item Allocation **********************************************************/
966 static TREEVIEW_ITEM
*
967 TREEVIEW_AllocateItem(const TREEVIEW_INFO
*infoPtr
)
969 TREEVIEW_ITEM
*newItem
= Alloc(sizeof(TREEVIEW_ITEM
));
974 /* I_IMAGENONE would make more sense but this is neither what is
975 * documented (MSDN doesn't specify) nor what Windows actually does
976 * (it sets it to zero)... and I can so imagine an application using
977 * inc/dec to toggle the images. */
979 newItem
->iSelectedImage
= 0;
980 newItem
->iExpandedImage
= (WORD
)I_IMAGENONE
;
981 newItem
->infoPtr
= infoPtr
;
983 if (DPA_InsertPtr(infoPtr
->items
, INT_MAX
, newItem
) == -1)
992 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
993 * free item->pszText. */
995 TREEVIEW_FreeItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
997 DPA_DeletePtr(infoPtr
->items
, DPA_GetPtrIndex(infoPtr
->items
, item
));
998 if (infoPtr
->selectedItem
== item
)
999 infoPtr
->selectedItem
= NULL
;
1000 if (infoPtr
->hotItem
== item
)
1001 infoPtr
->hotItem
= NULL
;
1002 if (infoPtr
->focusedItem
== item
)
1003 infoPtr
->focusedItem
= NULL
;
1004 if (infoPtr
->firstVisible
== item
)
1005 infoPtr
->firstVisible
= NULL
;
1006 if (infoPtr
->dropItem
== item
)
1007 infoPtr
->dropItem
= NULL
;
1008 if (infoPtr
->insertMarkItem
== item
)
1009 infoPtr
->insertMarkItem
= NULL
;
1014 /* Item Insertion *******************************************************/
1016 /***************************************************************************
1017 * This method inserts newItem before sibling as a child of parent.
1018 * sibling can be NULL, but only if parent has no children.
1021 TREEVIEW_InsertBefore(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
1022 TREEVIEW_ITEM
*parent
)
1024 assert(parent
!= NULL
);
1026 if (sibling
!= NULL
)
1028 assert(sibling
->parent
== parent
);
1030 if (sibling
->prevSibling
!= NULL
)
1031 sibling
->prevSibling
->nextSibling
= newItem
;
1033 newItem
->prevSibling
= sibling
->prevSibling
;
1034 sibling
->prevSibling
= newItem
;
1037 newItem
->prevSibling
= NULL
;
1039 newItem
->nextSibling
= sibling
;
1041 if (parent
->firstChild
== sibling
)
1042 parent
->firstChild
= newItem
;
1044 if (parent
->lastChild
== NULL
)
1045 parent
->lastChild
= newItem
;
1048 /***************************************************************************
1049 * This method inserts newItem after sibling as a child of parent.
1050 * sibling can be NULL, but only if parent has no children.
1053 TREEVIEW_InsertAfter(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
1054 TREEVIEW_ITEM
*parent
)
1056 assert(parent
!= NULL
);
1058 if (sibling
!= NULL
)
1060 assert(sibling
->parent
== parent
);
1062 if (sibling
->nextSibling
!= NULL
)
1063 sibling
->nextSibling
->prevSibling
= newItem
;
1065 newItem
->nextSibling
= sibling
->nextSibling
;
1066 sibling
->nextSibling
= newItem
;
1069 newItem
->nextSibling
= NULL
;
1071 newItem
->prevSibling
= sibling
;
1073 if (parent
->lastChild
== sibling
)
1074 parent
->lastChild
= newItem
;
1076 if (parent
->firstChild
== NULL
)
1077 parent
->firstChild
= newItem
;
1081 TREEVIEW_DoSetItemT(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
,
1082 const TVITEMEXW
*tvItem
, BOOL isW
)
1084 UINT callbackClear
= 0;
1085 UINT callbackSet
= 0;
1087 TRACE("item %p\n", item
);
1088 /* Do this first in case it fails. */
1089 if (tvItem
->mask
& TVIF_TEXT
)
1091 item
->textWidth
= 0; /* force width recalculation */
1092 if (tvItem
->pszText
!= LPSTR_TEXTCALLBACKW
&& tvItem
->pszText
!= NULL
) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1097 len
= lstrlenW(tvItem
->pszText
) + 1;
1099 len
= MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)tvItem
->pszText
, -1, NULL
, 0);
1101 newText
= ReAlloc(item
->pszText
, len
* sizeof(WCHAR
));
1103 if (newText
== NULL
) return FALSE
;
1105 callbackClear
|= TVIF_TEXT
;
1107 item
->pszText
= newText
;
1108 item
->cchTextMax
= len
;
1110 lstrcpynW(item
->pszText
, tvItem
->pszText
, len
);
1112 MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)tvItem
->pszText
, -1,
1113 item
->pszText
, len
);
1115 TRACE("setting text %s, item %p\n", debugstr_w(item
->pszText
), item
);
1119 callbackSet
|= TVIF_TEXT
;
1121 item
->pszText
= ReAlloc(item
->pszText
,
1122 TEXT_CALLBACK_SIZE
* sizeof(WCHAR
));
1123 item
->cchTextMax
= TEXT_CALLBACK_SIZE
;
1124 TRACE("setting callback, item %p\n", item
);
1128 if (tvItem
->mask
& TVIF_CHILDREN
)
1130 item
->cChildren
= tvItem
->cChildren
;
1132 if (item
->cChildren
== I_CHILDRENCALLBACK
)
1133 callbackSet
|= TVIF_CHILDREN
;
1135 callbackClear
|= TVIF_CHILDREN
;
1138 if (tvItem
->mask
& TVIF_IMAGE
)
1140 item
->iImage
= tvItem
->iImage
;
1142 if (item
->iImage
== I_IMAGECALLBACK
)
1143 callbackSet
|= TVIF_IMAGE
;
1145 callbackClear
|= TVIF_IMAGE
;
1148 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
1150 item
->iSelectedImage
= tvItem
->iSelectedImage
;
1152 if (item
->iSelectedImage
== I_IMAGECALLBACK
)
1153 callbackSet
|= TVIF_SELECTEDIMAGE
;
1155 callbackClear
|= TVIF_SELECTEDIMAGE
;
1158 if (tvItem
->mask
& TVIF_EXPANDEDIMAGE
)
1160 item
->iExpandedImage
= tvItem
->iExpandedImage
;
1162 if (item
->iExpandedImage
== I_IMAGECALLBACK
)
1163 callbackSet
|= TVIF_EXPANDEDIMAGE
;
1165 callbackClear
|= TVIF_EXPANDEDIMAGE
;
1168 if (tvItem
->mask
& TVIF_PARAM
)
1169 item
->lParam
= tvItem
->lParam
;
1171 /* If the application sets TVIF_INTEGRAL without
1172 * supplying a TVITEMEX structure, it's toast. */
1173 if (tvItem
->mask
& TVIF_INTEGRAL
)
1174 item
->iIntegral
= tvItem
->iIntegral
;
1176 if (tvItem
->mask
& TVIF_STATE
)
1178 TRACE("prevstate 0x%x, state 0x%x, mask 0x%x\n", item
->state
, tvItem
->state
,
1180 item
->state
&= ~tvItem
->stateMask
;
1181 item
->state
|= (tvItem
->state
& tvItem
->stateMask
);
1184 if (tvItem
->mask
& TVIF_STATEEX
)
1186 FIXME("New extended state: 0x%x\n", tvItem
->uStateEx
);
1189 item
->callbackMask
|= callbackSet
;
1190 item
->callbackMask
&= ~callbackClear
;
1195 /* Note that the new item is pre-zeroed. */
1197 TREEVIEW_InsertItemT(TREEVIEW_INFO
*infoPtr
, const TVINSERTSTRUCTW
*ptdi
, BOOL isW
)
1199 const TVITEMEXW
*tvItem
= &ptdi
->u
.itemex
;
1200 HTREEITEM insertAfter
;
1201 TREEVIEW_ITEM
*newItem
, *parentItem
;
1202 BOOL bTextUpdated
= FALSE
;
1204 if (ptdi
->hParent
== TVI_ROOT
|| ptdi
->hParent
== 0)
1206 parentItem
= infoPtr
->root
;
1210 parentItem
= ptdi
->hParent
;
1212 if (!TREEVIEW_ValidItem(infoPtr
, parentItem
))
1214 WARN("invalid parent %p\n", parentItem
);
1219 insertAfter
= ptdi
->hInsertAfter
;
1221 /* Validate this now for convenience. */
1222 switch ((DWORD_PTR
)insertAfter
)
1224 case (DWORD_PTR
)TVI_FIRST
:
1225 case (DWORD_PTR
)TVI_LAST
:
1226 case (DWORD_PTR
)TVI_SORT
:
1230 if (!TREEVIEW_ValidItem(infoPtr
, insertAfter
) ||
1231 insertAfter
->parent
!= parentItem
)
1233 WARN("invalid insert after %p\n", insertAfter
);
1234 insertAfter
= TVI_LAST
;
1238 TRACE("parent %p position %p: %s\n", parentItem
, insertAfter
,
1239 (tvItem
->mask
& TVIF_TEXT
)
1240 ? ((tvItem
->pszText
== LPSTR_TEXTCALLBACKW
) ? "<callback>"
1241 : (isW
? debugstr_w(tvItem
->pszText
) : debugstr_a((LPSTR
)tvItem
->pszText
)))
1244 newItem
= TREEVIEW_AllocateItem(infoPtr
);
1245 if (newItem
== NULL
)
1248 newItem
->parent
= parentItem
;
1249 newItem
->iIntegral
= 1;
1250 newItem
->visibleOrder
= -1;
1252 if (!TREEVIEW_DoSetItemT(infoPtr
, newItem
, tvItem
, isW
))
1255 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1257 infoPtr
->uNumItems
++;
1259 switch ((DWORD_PTR
)insertAfter
)
1261 case (DWORD_PTR
)TVI_FIRST
:
1263 TREEVIEW_ITEM
*originalFirst
= parentItem
->firstChild
;
1264 TREEVIEW_InsertBefore(newItem
, parentItem
->firstChild
, parentItem
);
1265 if (infoPtr
->firstVisible
== originalFirst
)
1266 TREEVIEW_SetFirstVisible(infoPtr
, newItem
, TRUE
);
1270 case (DWORD_PTR
)TVI_LAST
:
1271 TREEVIEW_InsertAfter(newItem
, parentItem
->lastChild
, parentItem
);
1274 /* hInsertAfter names a specific item we want to insert after */
1276 TREEVIEW_InsertAfter(newItem
, insertAfter
, insertAfter
->parent
);
1279 case (DWORD_PTR
)TVI_SORT
:
1281 TREEVIEW_ITEM
*aChild
;
1282 TREEVIEW_ITEM
*previousChild
= NULL
;
1283 TREEVIEW_ITEM
*originalFirst
= parentItem
->firstChild
;
1284 BOOL bItemInserted
= FALSE
;
1286 aChild
= parentItem
->firstChild
;
1288 bTextUpdated
= TRUE
;
1289 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1291 /* Iterate the parent children to see where we fit in */
1292 while (aChild
!= NULL
)
1296 TREEVIEW_UpdateDispInfo(infoPtr
, aChild
, TVIF_TEXT
);
1297 comp
= lstrcmpW(newItem
->pszText
, aChild
->pszText
);
1299 if (comp
< 0) /* we are smaller than the current one */
1301 TREEVIEW_InsertBefore(newItem
, aChild
, parentItem
);
1302 if (infoPtr
->firstVisible
== originalFirst
&&
1303 aChild
== originalFirst
)
1304 TREEVIEW_SetFirstVisible(infoPtr
, newItem
, TRUE
);
1305 bItemInserted
= TRUE
;
1308 else if (comp
> 0) /* we are bigger than the current one */
1310 previousChild
= aChild
;
1312 /* This will help us to exit if there is no more sibling */
1313 aChild
= (aChild
->nextSibling
== 0)
1315 : aChild
->nextSibling
;
1317 /* Look at the next item */
1323 * An item with this name is already existing, therefore,
1324 * we add after the one we found
1326 TREEVIEW_InsertAfter(newItem
, aChild
, parentItem
);
1327 bItemInserted
= TRUE
;
1333 * we reach the end of the child list and the item has not
1334 * yet been inserted, therefore, insert it after the last child.
1336 if ((!bItemInserted
) && (aChild
== NULL
))
1337 TREEVIEW_InsertAfter(newItem
, previousChild
, parentItem
);
1344 TRACE("new item %p; parent %p, mask 0x%x\n", newItem
,
1345 newItem
->parent
, tvItem
->mask
);
1347 newItem
->iLevel
= newItem
->parent
->iLevel
+ 1;
1349 if (newItem
->parent
->cChildren
== 0)
1350 newItem
->parent
->cChildren
= 1;
1352 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
1354 if (STATEIMAGEINDEX(newItem
->state
) == 0)
1355 newItem
->state
|= INDEXTOSTATEIMAGEMASK(1);
1358 if (infoPtr
->firstVisible
== NULL
)
1359 infoPtr
->firstVisible
= newItem
;
1361 TREEVIEW_VerifyTree(infoPtr
);
1363 if (!infoPtr
->bRedraw
) return (LRESULT
)newItem
;
1365 if (parentItem
== infoPtr
->root
||
1366 (ISVISIBLE(parentItem
) && parentItem
->state
& TVIS_EXPANDED
))
1368 TREEVIEW_ITEM
*item
;
1369 TREEVIEW_ITEM
*prev
= TREEVIEW_GetPrevListItem(infoPtr
, newItem
);
1371 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1372 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, newItem
);
1375 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1377 TREEVIEW_ComputeTextWidth(infoPtr
, newItem
, 0);
1378 TREEVIEW_UpdateScrollBars(infoPtr
);
1380 * if the item was inserted in a visible part of the tree,
1381 * invalidate it, as well as those after it
1383 for (item
= newItem
;
1385 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
1386 TREEVIEW_Invalidate(infoPtr
, item
);
1390 /* refresh treeview if newItem is the first item inserted under parentItem */
1391 if (ISVISIBLE(parentItem
) && newItem
->prevSibling
== newItem
->nextSibling
)
1393 /* parent got '+' - update it */
1394 TREEVIEW_Invalidate(infoPtr
, parentItem
);
1398 return (LRESULT
)newItem
;
1401 /* Item Deletion ************************************************************/
1403 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
);
1406 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*parentItem
)
1408 TREEVIEW_ITEM
*kill
= parentItem
->firstChild
;
1410 while (kill
!= NULL
)
1412 TREEVIEW_ITEM
*next
= kill
->nextSibling
;
1414 TREEVIEW_RemoveItem(infoPtr
, kill
);
1419 assert(parentItem
->cChildren
<= 0); /* I_CHILDRENCALLBACK or 0 */
1420 assert(parentItem
->firstChild
== NULL
);
1421 assert(parentItem
->lastChild
== NULL
);
1425 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM
*item
)
1427 TREEVIEW_ITEM
*parentItem
;
1429 assert(item
!= NULL
);
1430 assert(item
->parent
!= NULL
); /* i.e. it must not be the root */
1432 parentItem
= item
->parent
;
1434 if (parentItem
->firstChild
== item
)
1435 parentItem
->firstChild
= item
->nextSibling
;
1437 if (parentItem
->lastChild
== item
)
1438 parentItem
->lastChild
= item
->prevSibling
;
1440 if (parentItem
->firstChild
== NULL
&& parentItem
->lastChild
== NULL
1441 && parentItem
->cChildren
> 0)
1442 parentItem
->cChildren
= 0;
1444 if (item
->prevSibling
)
1445 item
->prevSibling
->nextSibling
= item
->nextSibling
;
1447 if (item
->nextSibling
)
1448 item
->nextSibling
->prevSibling
= item
->prevSibling
;
1452 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
1454 TRACE("%p, (%s)\n", item
, TREEVIEW_ItemName(item
));
1456 if (item
->firstChild
)
1457 TREEVIEW_RemoveAllChildren(infoPtr
, item
);
1459 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_DELETEITEMW
, TVC_UNKNOWN
,
1460 TVIF_HANDLE
| TVIF_PARAM
, item
, 0);
1462 TREEVIEW_UnlinkItem(item
);
1464 infoPtr
->uNumItems
--;
1466 if (item
->pszText
!= LPSTR_TEXTCALLBACKW
)
1467 Free(item
->pszText
);
1469 TREEVIEW_FreeItem(infoPtr
, item
);
1473 /* Empty out the tree. */
1475 TREEVIEW_RemoveTree(TREEVIEW_INFO
*infoPtr
)
1477 TREEVIEW_RemoveAllChildren(infoPtr
, infoPtr
->root
);
1479 assert(infoPtr
->uNumItems
== 0); /* root isn't counted in uNumItems */
1483 TREEVIEW_DeleteItem(TREEVIEW_INFO
*infoPtr
, HTREEITEM item
)
1485 TREEVIEW_ITEM
*newSelection
= NULL
;
1486 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
1487 TREEVIEW_ITEM
*parent
, *prev
= NULL
;
1488 BOOL visible
= FALSE
;
1490 if (item
== TVI_ROOT
|| !item
)
1492 TRACE("TVI_ROOT\n");
1493 parent
= infoPtr
->root
;
1494 newSelection
= NULL
;
1496 TREEVIEW_RemoveTree(infoPtr
);
1500 if (!TREEVIEW_ValidItem(infoPtr
, item
))
1503 TRACE("%p (%s)\n", item
, TREEVIEW_ItemName(item
));
1504 parent
= item
->parent
;
1506 if (ISVISIBLE(item
))
1508 prev
= TREEVIEW_GetPrevListItem(infoPtr
, item
);
1512 if (infoPtr
->selectedItem
!= NULL
1513 && (item
== infoPtr
->selectedItem
1514 || TREEVIEW_IsChildOf(item
, infoPtr
->selectedItem
)))
1516 if (item
->nextSibling
)
1517 newSelection
= item
->nextSibling
;
1518 else if (item
->parent
!= infoPtr
->root
)
1519 newSelection
= item
->parent
;
1521 newSelection
= item
->prevSibling
;
1522 TRACE("newSelection = %p\n", newSelection
);
1525 if (infoPtr
->firstVisible
== item
)
1528 if (item
->nextSibling
)
1529 newFirstVisible
= item
->nextSibling
;
1530 else if (item
->prevSibling
)
1531 newFirstVisible
= item
->prevSibling
;
1532 else if (item
->parent
!= infoPtr
->root
)
1533 newFirstVisible
= item
->parent
;
1534 TREEVIEW_SetFirstVisible(infoPtr
, NULL
, TRUE
);
1537 newFirstVisible
= infoPtr
->firstVisible
;
1539 TREEVIEW_RemoveItem(infoPtr
, item
);
1542 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1543 if (!infoPtr
->selectedItem
&& newSelection
)
1545 if (TREEVIEW_ValidItem(infoPtr
, newSelection
))
1546 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
, TVC_UNKNOWN
);
1549 /* Validate insertMark dropItem.
1550 * hotItem ??? - used for comparison only.
1552 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->insertMarkItem
))
1553 infoPtr
->insertMarkItem
= 0;
1555 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->dropItem
))
1556 infoPtr
->dropItem
= 0;
1558 if (!TREEVIEW_ValidItem(infoPtr
, newFirstVisible
))
1559 newFirstVisible
= infoPtr
->root
->firstChild
;
1561 TREEVIEW_VerifyTree(infoPtr
);
1564 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
1566 if (!infoPtr
->bRedraw
) return TRUE
;
1570 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1571 TREEVIEW_UpdateScrollBars(infoPtr
);
1572 TREEVIEW_Invalidate(infoPtr
, NULL
);
1574 else if (ISVISIBLE(parent
) && !TREEVIEW_HasChildren(infoPtr
, parent
))
1576 /* parent lost '+/-' - update it */
1577 TREEVIEW_Invalidate(infoPtr
, parent
);
1584 /* Get/Set Messages *********************************************************/
1586 TREEVIEW_SetRedraw(TREEVIEW_INFO
* infoPtr
, WPARAM wParam
)
1588 infoPtr
->bRedraw
= wParam
!= 0;
1590 if (infoPtr
->bRedraw
)
1592 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1593 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1594 TREEVIEW_UpdateScrollBars(infoPtr
);
1595 TREEVIEW_Invalidate(infoPtr
, NULL
);
1601 TREEVIEW_GetIndent(const TREEVIEW_INFO
*infoPtr
)
1604 return infoPtr
->uIndent
;
1608 TREEVIEW_SetIndent(TREEVIEW_INFO
*infoPtr
, UINT newIndent
)
1612 if (newIndent
< MINIMUM_INDENT
)
1613 newIndent
= MINIMUM_INDENT
;
1615 if (infoPtr
->uIndent
!= newIndent
)
1617 infoPtr
->uIndent
= newIndent
;
1618 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1619 TREEVIEW_UpdateScrollBars(infoPtr
);
1620 TREEVIEW_Invalidate(infoPtr
, NULL
);
1628 TREEVIEW_GetToolTips(const TREEVIEW_INFO
*infoPtr
)
1631 return (LRESULT
)infoPtr
->hwndToolTip
;
1635 TREEVIEW_SetToolTips(TREEVIEW_INFO
*infoPtr
, HWND hwndTT
)
1640 prevToolTip
= infoPtr
->hwndToolTip
;
1641 infoPtr
->hwndToolTip
= hwndTT
;
1643 return (LRESULT
)prevToolTip
;
1647 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO
*infoPtr
, BOOL fUnicode
)
1649 BOOL rc
= infoPtr
->bNtfUnicode
;
1650 infoPtr
->bNtfUnicode
= fUnicode
;
1655 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO
*infoPtr
)
1657 return infoPtr
->bNtfUnicode
;
1661 TREEVIEW_GetScrollTime(const TREEVIEW_INFO
*infoPtr
)
1663 return infoPtr
->uScrollTime
;
1667 TREEVIEW_SetScrollTime(TREEVIEW_INFO
*infoPtr
, UINT uScrollTime
)
1669 UINT uOldScrollTime
= infoPtr
->uScrollTime
;
1671 infoPtr
->uScrollTime
= min(uScrollTime
, 100);
1673 return uOldScrollTime
;
1678 TREEVIEW_GetImageList(const TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
1685 return (LRESULT
)infoPtr
->himlNormal
;
1688 return (LRESULT
)infoPtr
->himlState
;
1695 #define TVHEIGHT_MIN 16
1696 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1698 /* Compute the natural height for items. */
1700 TREEVIEW_NaturalHeight(const TREEVIEW_INFO
*infoPtr
)
1704 HFONT hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
1707 /* Height is the maximum of:
1708 * 16 (a hack because our fonts are tiny), and
1709 * The text height + border & margin, and
1710 * The size of the normal image list
1712 GetTextMetricsW(hdc
, &tm
);
1713 SelectObject(hdc
, hOldFont
);
1716 height
= TVHEIGHT_MIN
;
1717 if (height
< tm
.tmHeight
+ tm
.tmExternalLeading
+ TVHEIGHT_FONT_ADJUST
)
1718 height
= tm
.tmHeight
+ tm
.tmExternalLeading
+ TVHEIGHT_FONT_ADJUST
;
1719 if (height
< infoPtr
->normalImageHeight
)
1720 height
= infoPtr
->normalImageHeight
;
1722 /* Round down, unless we support odd ("non even") heights. */
1723 if (!(infoPtr
->dwStyle
& TVS_NONEVENHEIGHT
))
1730 TREEVIEW_SetImageList(TREEVIEW_INFO
*infoPtr
, UINT type
, HIMAGELIST himlNew
)
1732 HIMAGELIST himlOld
= 0;
1733 int oldWidth
= infoPtr
->normalImageWidth
;
1734 int oldHeight
= infoPtr
->normalImageHeight
;
1736 TRACE("%u,%p\n", type
, himlNew
);
1741 himlOld
= infoPtr
->himlNormal
;
1742 infoPtr
->himlNormal
= himlNew
;
1745 ImageList_GetIconSize(himlNew
, &infoPtr
->normalImageWidth
,
1746 &infoPtr
->normalImageHeight
);
1749 infoPtr
->normalImageWidth
= 0;
1750 infoPtr
->normalImageHeight
= 0;
1756 himlOld
= infoPtr
->himlState
;
1757 infoPtr
->himlState
= himlNew
;
1760 ImageList_GetIconSize(himlNew
, &infoPtr
->stateImageWidth
,
1761 &infoPtr
->stateImageHeight
);
1764 infoPtr
->stateImageWidth
= 0;
1765 infoPtr
->stateImageHeight
= 0;
1771 ERR("unknown imagelist type %u\n", type
);
1774 if (oldWidth
!= infoPtr
->normalImageWidth
||
1775 oldHeight
!= infoPtr
->normalImageHeight
)
1777 BOOL bRecalcVisible
= FALSE
;
1779 if (oldHeight
!= infoPtr
->normalImageHeight
&&
1780 !infoPtr
->bHeightSet
)
1782 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1783 bRecalcVisible
= TRUE
;
1786 if (infoPtr
->normalImageWidth
> MINIMUM_INDENT
&&
1787 infoPtr
->normalImageWidth
!= infoPtr
->uIndent
)
1789 infoPtr
->uIndent
= infoPtr
->normalImageWidth
;
1790 bRecalcVisible
= TRUE
;
1794 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1796 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1797 TREEVIEW_UpdateScrollBars(infoPtr
);
1800 TREEVIEW_Invalidate(infoPtr
, NULL
);
1802 return (LRESULT
)himlOld
;
1806 TREEVIEW_SetItemHeight(TREEVIEW_INFO
*infoPtr
, INT newHeight
)
1808 INT prevHeight
= infoPtr
->uItemHeight
;
1810 TRACE("new=%d, old=%d\n", newHeight
, prevHeight
);
1811 if (newHeight
== -1)
1813 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1814 infoPtr
->bHeightSet
= FALSE
;
1818 if (newHeight
== 0) newHeight
= 1;
1819 infoPtr
->uItemHeight
= newHeight
;
1820 infoPtr
->bHeightSet
= TRUE
;
1823 /* Round down, unless we support odd ("non even") heights. */
1824 if (!(infoPtr
->dwStyle
& TVS_NONEVENHEIGHT
) && infoPtr
->uItemHeight
!= 1)
1826 infoPtr
->uItemHeight
&= ~1;
1827 TRACE("after rounding=%d\n", infoPtr
->uItemHeight
);
1830 if (infoPtr
->uItemHeight
!= prevHeight
)
1832 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1833 TREEVIEW_UpdateScrollBars(infoPtr
);
1834 TREEVIEW_Invalidate(infoPtr
, NULL
);
1841 TREEVIEW_GetItemHeight(const TREEVIEW_INFO
*infoPtr
)
1844 return infoPtr
->uItemHeight
;
1849 TREEVIEW_GetFont(const TREEVIEW_INFO
*infoPtr
)
1851 TRACE("%p\n", infoPtr
->hFont
);
1852 return (LRESULT
)infoPtr
->hFont
;
1857 TREEVIEW_ResetTextWidth(LPVOID pItem
, LPVOID unused
)
1861 ((TREEVIEW_ITEM
*)pItem
)->textWidth
= 0;
1867 TREEVIEW_SetFont(TREEVIEW_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
1869 UINT uHeight
= infoPtr
->uItemHeight
;
1871 TRACE("%p %i\n", hFont
, bRedraw
);
1873 infoPtr
->hFont
= hFont
? hFont
: infoPtr
->hDefaultFont
;
1875 DeleteObject(infoPtr
->hBoldFont
);
1876 DeleteObject(infoPtr
->hUnderlineFont
);
1877 DeleteObject(infoPtr
->hBoldUnderlineFont
);
1878 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
1879 infoPtr
->hUnderlineFont
= TREEVIEW_CreateUnderlineFont(infoPtr
->hFont
);
1880 infoPtr
->hBoldUnderlineFont
= TREEVIEW_CreateBoldUnderlineFont(infoPtr
->hFont
);
1882 if (!infoPtr
->bHeightSet
)
1883 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1885 if (uHeight
!= infoPtr
->uItemHeight
)
1886 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1888 DPA_EnumCallback(infoPtr
->items
, TREEVIEW_ResetTextWidth
, 0);
1890 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1891 TREEVIEW_UpdateScrollBars(infoPtr
);
1894 TREEVIEW_Invalidate(infoPtr
, NULL
);
1901 TREEVIEW_GetLineColor(const TREEVIEW_INFO
*infoPtr
)
1904 return (LRESULT
)infoPtr
->clrLine
;
1908 TREEVIEW_SetLineColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1910 COLORREF prevColor
= infoPtr
->clrLine
;
1913 infoPtr
->clrLine
= color
;
1914 return (LRESULT
)prevColor
;
1919 TREEVIEW_GetTextColor(const TREEVIEW_INFO
*infoPtr
)
1922 return (LRESULT
)infoPtr
->clrText
;
1926 TREEVIEW_SetTextColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1928 COLORREF prevColor
= infoPtr
->clrText
;
1931 infoPtr
->clrText
= color
;
1933 if (infoPtr
->clrText
!= prevColor
)
1934 TREEVIEW_Invalidate(infoPtr
, NULL
);
1936 return (LRESULT
)prevColor
;
1941 TREEVIEW_GetBkColor(const TREEVIEW_INFO
*infoPtr
)
1944 return (LRESULT
)infoPtr
->clrBk
;
1948 TREEVIEW_SetBkColor(TREEVIEW_INFO
*infoPtr
, COLORREF newColor
)
1950 COLORREF prevColor
= infoPtr
->clrBk
;
1953 infoPtr
->clrBk
= newColor
;
1955 if (newColor
!= prevColor
)
1956 TREEVIEW_Invalidate(infoPtr
, NULL
);
1958 return (LRESULT
)prevColor
;
1963 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO
*infoPtr
)
1966 return (LRESULT
)infoPtr
->clrInsertMark
;
1970 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1972 COLORREF prevColor
= infoPtr
->clrInsertMark
;
1974 TRACE("0x%08x\n", color
);
1975 infoPtr
->clrInsertMark
= color
;
1977 return (LRESULT
)prevColor
;
1982 TREEVIEW_SetInsertMark(TREEVIEW_INFO
*infoPtr
, BOOL wParam
, HTREEITEM item
)
1984 TRACE("%d %p\n", wParam
, item
);
1986 if (!TREEVIEW_ValidItem(infoPtr
, item
))
1989 infoPtr
->insertBeforeorAfter
= wParam
;
1990 infoPtr
->insertMarkItem
= item
;
1992 TREEVIEW_Invalidate(infoPtr
, NULL
);
1998 /************************************************************************
1999 * Some serious braindamage here. lParam is a pointer to both the
2000 * input HTREEITEM and the output RECT.
2003 TREEVIEW_GetItemRect(const TREEVIEW_INFO
*infoPtr
, BOOL fTextRect
, LPRECT lpRect
)
2005 TREEVIEW_ITEM
*item
;
2006 const HTREEITEM
*pItem
= (HTREEITEM
*)lpRect
;
2014 if (!TREEVIEW_ValidItem(infoPtr
, item
) || !ISVISIBLE(item
))
2018 * If wParam is TRUE return the text size otherwise return
2019 * the whole item size
2023 /* Windows does not send TVN_GETDISPINFO here. */
2025 lpRect
->top
= item
->rect
.top
;
2026 lpRect
->bottom
= item
->rect
.bottom
;
2028 lpRect
->left
= item
->textOffset
;
2029 if (!item
->textWidth
)
2030 TREEVIEW_ComputeTextWidth(infoPtr
, item
, 0);
2032 lpRect
->right
= item
->textOffset
+ item
->textWidth
+ 4;
2036 *lpRect
= item
->rect
;
2039 TRACE("%s [%s]\n", fTextRect
? "text" : "item", wine_dbgstr_rect(lpRect
));
2044 static inline LRESULT
2045 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO
*infoPtr
)
2047 /* Surprise! This does not take integral height into account. */
2048 TRACE("client=%d, item=%d\n", infoPtr
->clientHeight
, infoPtr
->uItemHeight
);
2049 return infoPtr
->clientHeight
/ infoPtr
->uItemHeight
;
2054 TREEVIEW_GetItemT(const TREEVIEW_INFO
*infoPtr
, LPTVITEMEXW tvItem
, BOOL isW
)
2056 TREEVIEW_ITEM
*item
= tvItem
->hItem
;
2058 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2060 BOOL valid_item
= FALSE
;
2061 if (!item
) return FALSE
;
2065 infoPtr
= item
->infoPtr
;
2066 TRACE("got item from different tree %p, called from %p\n", item
->infoPtr
, infoPtr
);
2067 valid_item
= TREEVIEW_ValidItem(infoPtr
, item
);
2073 if (!valid_item
) return FALSE
;
2076 TREEVIEW_UpdateDispInfo(infoPtr
, item
, tvItem
->mask
);
2078 if (tvItem
->mask
& TVIF_CHILDREN
)
2080 if (item
->cChildren
==I_CHILDRENCALLBACK
)
2081 FIXME("I_CHILDRENCALLBACK not supported\n");
2082 tvItem
->cChildren
= item
->cChildren
;
2085 if (tvItem
->mask
& TVIF_HANDLE
)
2086 tvItem
->hItem
= item
;
2088 if (tvItem
->mask
& TVIF_IMAGE
)
2089 tvItem
->iImage
= item
->iImage
;
2091 if (tvItem
->mask
& TVIF_INTEGRAL
)
2092 tvItem
->iIntegral
= item
->iIntegral
;
2094 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2095 tvItem
->lParam
= item
->lParam
;
2097 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
2098 tvItem
->iSelectedImage
= item
->iSelectedImage
;
2100 if (tvItem
->mask
& TVIF_EXPANDEDIMAGE
)
2101 tvItem
->iExpandedImage
= item
->iExpandedImage
;
2103 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2104 tvItem
->state
= item
->state
;
2106 if (tvItem
->mask
& TVIF_TEXT
)
2108 if (item
->pszText
== NULL
)
2110 if (tvItem
->cchTextMax
> 0)
2111 tvItem
->pszText
[0] = '\0';
2115 if (item
->pszText
== LPSTR_TEXTCALLBACKW
)
2117 tvItem
->pszText
= LPSTR_TEXTCALLBACKW
;
2118 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2122 lstrcpynW(tvItem
->pszText
, item
->pszText
, tvItem
->cchTextMax
);
2127 if (item
->pszText
== LPSTR_TEXTCALLBACKW
)
2129 tvItem
->pszText
= (LPWSTR
)LPSTR_TEXTCALLBACKA
;
2130 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2134 WideCharToMultiByte(CP_ACP
, 0, item
->pszText
, -1,
2135 (LPSTR
)tvItem
->pszText
, tvItem
->cchTextMax
, NULL
, NULL
);
2140 if (tvItem
->mask
& TVIF_STATEEX
)
2142 FIXME("Extended item state not supported, returning 0.\n");
2143 tvItem
->uStateEx
= 0;
2146 TRACE("item <%p>, txt %p, img %d, mask 0x%x\n",
2147 item
, tvItem
->pszText
, tvItem
->iImage
, tvItem
->mask
);
2152 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2153 * which is wrong. */
2155 TREEVIEW_SetItemT(TREEVIEW_INFO
*infoPtr
, const TVITEMEXW
*tvItem
, BOOL isW
)
2157 TREEVIEW_ITEM
*item
;
2158 TREEVIEW_ITEM originalItem
;
2160 item
= tvItem
->hItem
;
2162 TRACE("item %d, mask 0x%x\n", TREEVIEW_GetItemIndex(infoPtr
, item
),
2165 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2168 /* store the original item values */
2169 originalItem
= *item
;
2171 if (!TREEVIEW_DoSetItemT(infoPtr
, item
, tvItem
, isW
))
2174 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2175 if ((tvItem
->mask
& TVIF_TEXT
2176 || (tvItem
->mask
& TVIF_STATE
&& tvItem
->stateMask
& TVIS_BOLD
))
2179 TREEVIEW_UpdateDispInfo(infoPtr
, item
, TVIF_TEXT
);
2180 TREEVIEW_ComputeTextWidth(infoPtr
, item
, 0);
2183 if (tvItem
->mask
!= 0 && ISVISIBLE(item
))
2185 /* The refresh updates everything, but we can't wait until then. */
2186 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, item
);
2188 /* if any of the item's values changed and it's not a callback, redraw the item */
2189 if (item_changed(&originalItem
, item
, tvItem
))
2191 if (tvItem
->mask
& TVIF_INTEGRAL
)
2193 TREEVIEW_RecalculateVisibleOrder(infoPtr
, item
);
2194 TREEVIEW_UpdateScrollBars(infoPtr
);
2196 TREEVIEW_Invalidate(infoPtr
, NULL
);
2200 TREEVIEW_UpdateScrollBars(infoPtr
);
2201 TREEVIEW_Invalidate(infoPtr
, item
);
2210 TREEVIEW_GetItemState(const TREEVIEW_INFO
*infoPtr
, HTREEITEM item
, UINT mask
)
2214 if (!item
|| !TREEVIEW_ValidItem(infoPtr
, item
))
2217 return (item
->state
& mask
);
2221 TREEVIEW_GetNextItem(const TREEVIEW_INFO
*infoPtr
, UINT which
, HTREEITEM item
)
2223 TREEVIEW_ITEM
*retval
;
2227 /* handle all the global data here */
2230 case TVGN_CHILD
: /* Special case: child of 0 is root */
2235 retval
= infoPtr
->root
->firstChild
;
2239 retval
= infoPtr
->selectedItem
;
2242 case TVGN_FIRSTVISIBLE
:
2243 retval
= infoPtr
->firstVisible
;
2246 case TVGN_DROPHILITE
:
2247 retval
= infoPtr
->dropItem
;
2250 case TVGN_LASTVISIBLE
:
2251 retval
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
2257 TRACE("flags:0x%x, returns %p\n", which
, retval
);
2258 return (LRESULT
)retval
;
2261 if (item
== TVI_ROOT
) item
= infoPtr
->root
;
2263 if (!TREEVIEW_ValidItem(infoPtr
, item
))
2269 retval
= item
->nextSibling
;
2272 retval
= item
->prevSibling
;
2275 retval
= (item
->parent
!= infoPtr
->root
) ? item
->parent
: NULL
;
2278 retval
= item
->firstChild
;
2280 case TVGN_NEXTVISIBLE
:
2281 retval
= TREEVIEW_GetNextListItem(infoPtr
, item
);
2283 case TVGN_PREVIOUSVISIBLE
:
2284 retval
= TREEVIEW_GetPrevListItem(infoPtr
, item
);
2287 TRACE("Unknown msg 0x%x, item %p\n", which
, item
);
2291 TRACE("flags: 0x%x, item %p;returns %p\n", which
, item
, retval
);
2292 return (LRESULT
)retval
;
2297 TREEVIEW_GetCount(const TREEVIEW_INFO
*infoPtr
)
2299 TRACE(" %d\n", infoPtr
->uNumItems
);
2300 return (LRESULT
)infoPtr
->uNumItems
;
2304 TREEVIEW_ToggleItemState(const TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2306 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
2308 static const unsigned int state_table
[] = { 0, 2, 1 };
2312 state
= STATEIMAGEINDEX(item
->state
);
2313 TRACE("state: 0x%x\n", state
);
2314 item
->state
&= ~TVIS_STATEIMAGEMASK
;
2317 state
= state_table
[state
];
2319 item
->state
|= INDEXTOSTATEIMAGEMASK(state
);
2321 TRACE("state: 0x%x\n", state
);
2322 TREEVIEW_Invalidate(infoPtr
, item
);
2327 /* Painting *************************************************************/
2329 /* Draw the lines and expand button for an item. Also draws one section
2330 * of the line from item's parent to item's parent's next sibling. */
2332 TREEVIEW_DrawItemLines(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, const TREEVIEW_ITEM
*item
)
2334 LONG centerx
, centery
;
2335 BOOL lar
= ((infoPtr
->dwStyle
2336 & (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
2339 COLORREF clrBk
= GETBKCOLOR(infoPtr
->clrBk
);
2341 if (!lar
&& item
->iLevel
== 0)
2344 hbr
= CreateSolidBrush(clrBk
);
2345 hbrOld
= SelectObject(hdc
, hbr
);
2347 centerx
= (item
->linesOffset
+ item
->stateOffset
) / 2;
2348 centery
= (item
->rect
.top
+ item
->rect
.bottom
) / 2;
2350 if (infoPtr
->dwStyle
& TVS_HASLINES
)
2352 HPEN hOldPen
, hNewPen
;
2356 /* Get a dotted grey pen */
2357 lb
.lbStyle
= BS_SOLID
;
2358 lb
.lbColor
= GETLINECOLOR(infoPtr
->clrLine
);
2359 hNewPen
= ExtCreatePen(PS_COSMETIC
|PS_ALTERNATE
, 1, &lb
, 0, NULL
);
2360 hOldPen
= SelectObject(hdc
, hNewPen
);
2362 /* Make sure the center is on a dot (using +2 instead
2363 * of +1 gives us pixel-by-pixel compat with native) */
2364 centery
= (centery
+ 2) & ~1;
2366 MoveToEx(hdc
, item
->stateOffset
, centery
, NULL
);
2367 LineTo(hdc
, centerx
- 1, centery
);
2369 if (item
->prevSibling
|| item
->parent
!= infoPtr
->root
)
2371 MoveToEx(hdc
, centerx
, item
->rect
.top
, NULL
);
2372 LineTo(hdc
, centerx
, centery
);
2375 if (item
->nextSibling
)
2377 MoveToEx(hdc
, centerx
, centery
, NULL
);
2378 LineTo(hdc
, centerx
, item
->rect
.bottom
+ 1);
2381 /* Draw the line from our parent to its next sibling. */
2382 parent
= item
->parent
;
2383 while (parent
!= infoPtr
->root
)
2385 int pcenterx
= (parent
->linesOffset
+ parent
->stateOffset
) / 2;
2387 if (parent
->nextSibling
2388 /* skip top-levels unless TVS_LINESATROOT */
2389 && parent
->stateOffset
> parent
->linesOffset
)
2391 MoveToEx(hdc
, pcenterx
, item
->rect
.top
, NULL
);
2392 LineTo(hdc
, pcenterx
, item
->rect
.bottom
+ 1);
2395 parent
= parent
->parent
;
2398 SelectObject(hdc
, hOldPen
);
2399 DeleteObject(hNewPen
);
2403 * Display the (+/-) signs
2406 if (infoPtr
->dwStyle
& TVS_HASBUTTONS
)
2408 if (item
->cChildren
)
2410 HTHEME theme
= GetWindowTheme(infoPtr
->hwnd
);
2413 RECT glyphRect
= item
->rect
;
2414 glyphRect
.left
= item
->linesOffset
;
2415 glyphRect
.right
= item
->stateOffset
;
2416 DrawThemeBackground (theme
, hdc
, TVP_GLYPH
,
2417 (item
->state
& TVIS_EXPANDED
) ? GLPS_OPENED
: GLPS_CLOSED
,
2422 LONG height
= item
->rect
.bottom
- item
->rect
.top
;
2423 LONG width
= item
->stateOffset
- item
->linesOffset
;
2424 LONG rectsize
= min(height
, width
) / 4;
2425 /* plussize = ceil(rectsize * 3/4) */
2426 LONG plussize
= (rectsize
+ 1) * 3 / 4;
2428 HPEN new_pen
= CreatePen(PS_SOLID
, 0, GETLINECOLOR(infoPtr
->clrLine
));
2429 HPEN old_pen
= SelectObject(hdc
, new_pen
);
2431 Rectangle(hdc
, centerx
- rectsize
- 1, centery
- rectsize
- 1,
2432 centerx
+ rectsize
+ 2, centery
+ rectsize
+ 2);
2434 SelectObject(hdc
, old_pen
);
2435 DeleteObject(new_pen
);
2437 /* draw +/- signs with current text color */
2438 new_pen
= CreatePen(PS_SOLID
, 0, GETTXTCOLOR(infoPtr
->clrText
));
2439 old_pen
= SelectObject(hdc
, new_pen
);
2441 if (height
< 18 || width
< 18)
2443 MoveToEx(hdc
, centerx
- plussize
+ 1, centery
, NULL
);
2444 LineTo(hdc
, centerx
+ plussize
, centery
);
2446 if (!(item
->state
& TVIS_EXPANDED
) ||
2447 (item
->state
& TVIS_EXPANDPARTIAL
))
2449 MoveToEx(hdc
, centerx
, centery
- plussize
+ 1, NULL
);
2450 LineTo(hdc
, centerx
, centery
+ plussize
);
2455 Rectangle(hdc
, centerx
- plussize
+ 1, centery
- 1,
2456 centerx
+ plussize
, centery
+ 2);
2458 if (!(item
->state
& TVIS_EXPANDED
) ||
2459 (item
->state
& TVIS_EXPANDPARTIAL
))
2461 Rectangle(hdc
, centerx
- 1, centery
- plussize
+ 1,
2462 centerx
+ 2, centery
+ plussize
);
2463 SetPixel(hdc
, centerx
- 1, centery
, clrBk
);
2464 SetPixel(hdc
, centerx
+ 1, centery
, clrBk
);
2468 SelectObject(hdc
, old_pen
);
2469 DeleteObject(new_pen
);
2473 SelectObject(hdc
, hbrOld
);
2478 TREEVIEW_DrawItem(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, TREEVIEW_ITEM
*item
)
2482 COLORREF oldTextColor
, oldTextBkColor
;
2484 BOOL inFocus
= (GetFocus() == infoPtr
->hwnd
);
2485 NMTVCUSTOMDRAW nmcdhdr
;
2487 TREEVIEW_UpdateDispInfo(infoPtr
, item
, CALLBACK_MASK_ALL
);
2489 /* - If item is drop target or it is selected and window is in focus -
2490 * use blue background (COLOR_HIGHLIGHT).
2491 * - If item is selected, window is not in focus, but it has style
2492 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2493 * - Otherwise - use background color
2495 if ((item
->state
& TVIS_DROPHILITED
) || ((item
== infoPtr
->focusedItem
) && !(item
->state
& TVIS_SELECTED
)) ||
2496 ((item
->state
& TVIS_SELECTED
) && (!infoPtr
->focusedItem
|| item
== infoPtr
->focusedItem
) &&
2497 (inFocus
|| (infoPtr
->dwStyle
& TVS_SHOWSELALWAYS
))))
2499 if ((item
->state
& TVIS_DROPHILITED
) || inFocus
)
2501 nmcdhdr
.clrTextBk
= comctl32_color
.clrHighlight
;
2502 nmcdhdr
.clrText
= comctl32_color
.clrHighlightText
;
2506 nmcdhdr
.clrTextBk
= comctl32_color
.clrBtnFace
;
2507 nmcdhdr
.clrText
= GETTXTCOLOR(infoPtr
->clrText
);
2512 nmcdhdr
.clrTextBk
= GETBKCOLOR(infoPtr
->clrBk
);
2513 if ((infoPtr
->dwStyle
& TVS_TRACKSELECT
) && (item
== infoPtr
->hotItem
))
2514 nmcdhdr
.clrText
= comctl32_color
.clrHighlight
;
2516 nmcdhdr
.clrText
= GETTXTCOLOR(infoPtr
->clrText
);
2519 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, item
));
2521 /* The custom draw handler can query the text rectangle,
2523 /* should already be known, set to 0 when changed */
2524 if (!item
->textWidth
)
2525 TREEVIEW_ComputeTextWidth(infoPtr
, item
, hdc
);
2529 if (infoPtr
->cdmode
& CDRF_NOTIFYITEMDRAW
)
2531 cditem
= TREEVIEW_SendCustomDrawItemNotify
2532 (infoPtr
, hdc
, item
, CDDS_ITEMPREPAINT
, &nmcdhdr
);
2533 TRACE("prepaint:cditem-app returns 0x%x\n", cditem
);
2535 if (cditem
& CDRF_SKIPDEFAULT
)
2537 SelectObject(hdc
, hOldFont
);
2542 if (cditem
& CDRF_NEWFONT
)
2543 TREEVIEW_ComputeTextWidth(infoPtr
, item
, hdc
);
2545 TREEVIEW_DrawItemLines(infoPtr
, hdc
, item
);
2547 /* Set colors. Custom draw handler can change these so we do this after it. */
2548 oldTextColor
= SetTextColor(hdc
, nmcdhdr
.clrText
);
2549 oldTextBkColor
= SetBkColor(hdc
, nmcdhdr
.clrTextBk
);
2551 centery
= (item
->rect
.top
+ item
->rect
.bottom
) / 2;
2554 * Display the images associated with this item
2559 /* State images are displayed to the left of the Normal image
2560 * image number is in state; zero should be `display no image'.
2562 imageIndex
= STATEIMAGEINDEX(item
->state
);
2564 if (infoPtr
->himlState
&& imageIndex
)
2566 ImageList_Draw(infoPtr
->himlState
, imageIndex
, hdc
,
2568 centery
- infoPtr
->stateImageHeight
/ 2,
2572 /* Now, draw the normal image; can be either selected,
2573 * non-selected or expanded image.
2576 if ((item
->state
& TVIS_SELECTED
) && (item
->iSelectedImage
>= 0))
2578 /* The item is currently selected */
2579 imageIndex
= item
->iSelectedImage
;
2581 else if ((item
->state
& TVIS_EXPANDED
) && (item
->iExpandedImage
!= (WORD
)I_IMAGENONE
))
2583 /* The item is currently not selected but expanded */
2584 imageIndex
= item
->iExpandedImage
;
2588 /* The item is not selected and not expanded */
2589 imageIndex
= item
->iImage
;
2592 if (infoPtr
->himlNormal
)
2594 UINT style
= item
->state
& TVIS_CUT
? ILD_SELECTED
: ILD_NORMAL
;
2596 style
|= item
->state
& TVIS_OVERLAYMASK
;
2598 ImageList_DrawEx(infoPtr
->himlNormal
, imageIndex
, hdc
,
2599 item
->imageOffset
, centery
- infoPtr
->normalImageHeight
/ 2,
2600 0, 0, infoPtr
->clrBk
, item
->state
& TVIS_CUT
? GETBKCOLOR(infoPtr
->clrBk
) : CLR_DEFAULT
,
2607 * Display the text associated with this item
2610 /* Don't paint item's text if it's being edited */
2611 if (!infoPtr
->hwndEdit
|| (infoPtr
->selectedItem
!= item
))
2619 rcText
.top
= item
->rect
.top
;
2620 rcText
.bottom
= item
->rect
.bottom
;
2621 rcText
.left
= item
->textOffset
;
2622 rcText
.right
= rcText
.left
+ item
->textWidth
+ 4;
2624 TRACE("drawing text %s at (%s)\n",
2625 debugstr_w(item
->pszText
), wine_dbgstr_rect(&rcText
));
2628 GetTextExtentPoint32W(hdc
, item
->pszText
, strlenW(item
->pszText
), &sz
);
2630 align
= SetTextAlign(hdc
, TA_LEFT
| TA_TOP
);
2631 ExtTextOutW(hdc
, rcText
.left
+ 2, (rcText
.top
+ rcText
.bottom
- sz
.cy
) / 2,
2632 ETO_CLIPPED
| ETO_OPAQUE
,
2635 lstrlenW(item
->pszText
),
2637 SetTextAlign(hdc
, align
);
2639 /* Draw focus box around the selected item */
2640 if ((item
== infoPtr
->selectedItem
) && inFocus
)
2642 DrawFocusRect(hdc
,&rcText
);
2647 /* Draw insertion mark if necessary */
2649 if (infoPtr
->insertMarkItem
)
2650 TRACE("item:%d,mark:%p\n",
2651 TREEVIEW_GetItemIndex(infoPtr
, item
),
2652 infoPtr
->insertMarkItem
);
2654 if (item
== infoPtr
->insertMarkItem
)
2656 HPEN hNewPen
, hOldPen
;
2660 hNewPen
= CreatePen(PS_SOLID
, 2, GETINSCOLOR(infoPtr
->clrInsertMark
));
2661 hOldPen
= SelectObject(hdc
, hNewPen
);
2663 if (infoPtr
->insertBeforeorAfter
)
2664 offset
= item
->rect
.bottom
- 1;
2666 offset
= item
->rect
.top
+ 1;
2668 left
= item
->textOffset
- 2;
2669 right
= item
->textOffset
+ item
->textWidth
+ 2;
2671 MoveToEx(hdc
, left
, offset
- 3, NULL
);
2672 LineTo(hdc
, left
, offset
+ 4);
2674 MoveToEx(hdc
, left
, offset
, NULL
);
2675 LineTo(hdc
, right
+ 1, offset
);
2677 MoveToEx(hdc
, right
, offset
+ 3, NULL
);
2678 LineTo(hdc
, right
, offset
- 4);
2680 SelectObject(hdc
, hOldPen
);
2681 DeleteObject(hNewPen
);
2684 /* Restore the hdc state */
2685 SetTextColor(hdc
, oldTextColor
);
2686 SetBkColor(hdc
, oldTextBkColor
);
2687 SelectObject(hdc
, hOldFont
);
2689 if (cditem
& CDRF_NOTIFYPOSTPAINT
)
2691 cditem
= TREEVIEW_SendCustomDrawItemNotify
2692 (infoPtr
, hdc
, item
, CDDS_ITEMPOSTPAINT
, &nmcdhdr
);
2693 TRACE("postpaint:cditem-app returns 0x%x\n", cditem
);
2697 /* Computes treeHeight and treeWidth and updates the scroll bars.
2700 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
)
2702 TREEVIEW_ITEM
*item
;
2703 HWND hwnd
= infoPtr
->hwnd
;
2707 LONG scrollX
= infoPtr
->scrollX
;
2709 infoPtr
->treeWidth
= 0;
2710 infoPtr
->treeHeight
= 0;
2712 /* We iterate through all visible items in order to get the tree height
2714 item
= infoPtr
->root
->firstChild
;
2716 while (item
!= NULL
)
2718 if (ISVISIBLE(item
))
2720 /* actually we draw text at textOffset + 2 */
2721 if (2+item
->textOffset
+item
->textWidth
> infoPtr
->treeWidth
)
2722 infoPtr
->treeWidth
= item
->textOffset
+item
->textWidth
+2;
2724 /* This is scroll-adjusted, but we fix this below. */
2725 infoPtr
->treeHeight
= item
->rect
.bottom
;
2728 item
= TREEVIEW_GetNextListItem(infoPtr
, item
);
2731 /* Fix the scroll adjusted treeHeight and treeWidth. */
2732 if (infoPtr
->root
->firstChild
)
2733 infoPtr
->treeHeight
-= infoPtr
->root
->firstChild
->rect
.top
;
2735 infoPtr
->treeWidth
+= infoPtr
->scrollX
;
2737 if (infoPtr
->dwStyle
& TVS_NOSCROLL
) return;
2739 /* Adding one scroll bar may take up enough space that it forces us
2740 * to add the other as well. */
2741 if (infoPtr
->treeHeight
> infoPtr
->clientHeight
)
2745 if (infoPtr
->treeWidth
2746 > infoPtr
->clientWidth
- GetSystemMetrics(SM_CXVSCROLL
))
2749 else if (infoPtr
->treeWidth
> infoPtr
->clientWidth
|| infoPtr
->scrollX
> 0)
2752 if (!vert
&& horz
&& infoPtr
->treeHeight
2753 > infoPtr
->clientHeight
- GetSystemMetrics(SM_CYVSCROLL
))
2756 if (horz
&& (infoPtr
->dwStyle
& TVS_NOHSCROLL
)) horz
= FALSE
;
2758 si
.cbSize
= sizeof(SCROLLINFO
);
2759 si
.fMask
= SIF_POS
|SIF_RANGE
|SIF_PAGE
;
2764 si
.nPage
= TREEVIEW_GetVisibleCount(infoPtr
);
2765 if ( si
.nPage
&& NULL
!= infoPtr
->firstVisible
)
2767 si
.nPos
= infoPtr
->firstVisible
->visibleOrder
;
2768 si
.nMax
= infoPtr
->maxVisibleOrder
- 1;
2770 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
2772 if (!(infoPtr
->uInternalStatus
& TV_VSCROLL
))
2773 ShowScrollBar(hwnd
, SB_VERT
, TRUE
);
2774 infoPtr
->uInternalStatus
|= TV_VSCROLL
;
2778 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2779 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2780 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2785 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2786 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2787 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2792 si
.nPage
= infoPtr
->clientWidth
;
2793 si
.nPos
= infoPtr
->scrollX
;
2794 si
.nMax
= infoPtr
->treeWidth
- 1;
2796 if (si
.nPos
> si
.nMax
- max( si
.nPage
-1, 0 ))
2798 si
.nPos
= si
.nMax
- max( si
.nPage
-1, 0 );
2802 if (!(infoPtr
->uInternalStatus
& TV_HSCROLL
))
2803 ShowScrollBar(hwnd
, SB_HORZ
, TRUE
);
2804 infoPtr
->uInternalStatus
|= TV_HSCROLL
;
2806 SetScrollInfo(hwnd
, SB_HORZ
, &si
, TRUE
);
2807 TREEVIEW_HScroll(infoPtr
,
2808 MAKEWPARAM(SB_THUMBPOSITION
, scrollX
));
2812 if (infoPtr
->uInternalStatus
& TV_HSCROLL
)
2813 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
2814 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2817 if (infoPtr
->scrollX
!= 0)
2819 TREEVIEW_HScroll(infoPtr
,
2820 MAKEWPARAM(SB_THUMBPOSITION
, scrollX
));
2825 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2829 TREEVIEW_FillBkgnd(const TREEVIEW_INFO
*infoPtr
, HDC hdc
, const RECT
*rc
)
2832 COLORREF clrBk
= GETBKCOLOR(infoPtr
->clrBk
);
2834 hBrush
= CreateSolidBrush(clrBk
);
2835 FillRect(hdc
, rc
, hBrush
);
2836 DeleteObject(hBrush
);
2839 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2841 TREEVIEW_EraseBackground(const TREEVIEW_INFO
*infoPtr
, HDC hdc
)
2845 TRACE("%p\n", infoPtr
);
2847 GetClientRect(infoPtr
->hwnd
, &rect
);
2848 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rect
);
2854 TREEVIEW_Refresh(TREEVIEW_INFO
*infoPtr
, HDC hdc
, const RECT
*rc
)
2856 HWND hwnd
= infoPtr
->hwnd
;
2858 TREEVIEW_ITEM
*item
;
2860 if (infoPtr
->clientHeight
== 0 || infoPtr
->clientWidth
== 0)
2862 TRACE("empty window\n");
2866 infoPtr
->cdmode
= TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_PREPAINT
,
2869 if (infoPtr
->cdmode
== CDRF_SKIPDEFAULT
)
2871 ReleaseDC(hwnd
, hdc
);
2875 for (item
= infoPtr
->root
->firstChild
;
2877 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
2879 if (ISVISIBLE(item
))
2881 /* Avoid unneeded calculations */
2882 if (item
->rect
.top
> rect
.bottom
)
2884 if (item
->rect
.bottom
< rect
.top
)
2887 TREEVIEW_DrawItem(infoPtr
, hdc
, item
);
2892 // FIXME: This is correct, but is causes and infinite loop of WM_PAINT
2893 // messages, resulting in continuous painting of the scroll bar in reactos.
2894 // Comment out until the real bug is found. CORE-4912
2897 TREEVIEW_UpdateScrollBars(infoPtr
);
2900 if (infoPtr
->cdmode
& CDRF_NOTIFYPOSTPAINT
)
2902 TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_POSTPAINT
, hdc
, rect
);
2906 TREEVIEW_InvalidateItem(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
2908 if (item
) InvalidateRect(infoPtr
->hwnd
, &item
->rect
, TRUE
);
2912 TREEVIEW_Invalidate(const TREEVIEW_INFO
*infoPtr
, const TREEVIEW_ITEM
*item
)
2915 InvalidateRect(infoPtr
->hwnd
, &item
->rect
, TRUE
);
2917 InvalidateRect(infoPtr
->hwnd
, NULL
, TRUE
);
2921 TREEVIEW_InitCheckboxes(TREEVIEW_INFO
*infoPtr
)
2924 HBITMAP hbm
, hbmOld
;
2928 infoPtr
->himlState
= ImageList_Create(16, 16, ILC_COLOR
| ILC_MASK
, 3, 0);
2930 hdcScreen
= GetDC(0);
2932 hdc
= CreateCompatibleDC(hdcScreen
);
2933 hbm
= CreateCompatibleBitmap(hdcScreen
, 48, 16);
2934 hbmOld
= SelectObject(hdc
, hbm
);
2936 SetRect(&rc
, 0, 0, 48, 16);
2937 FillRect(hdc
, &rc
, (HBRUSH
)(COLOR_WINDOW
+1));
2939 SetRect(&rc
, 18, 2, 30, 14);
2940 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
2941 DFCS_BUTTONCHECK
|DFCS_FLAT
);
2943 SetRect(&rc
, 34, 2, 46, 14);
2944 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
2945 DFCS_BUTTONCHECK
|DFCS_FLAT
|DFCS_CHECKED
);
2947 SelectObject(hdc
, hbmOld
);
2948 nIndex
= ImageList_AddMasked(infoPtr
->himlState
, hbm
,
2949 comctl32_color
.clrWindow
);
2950 TRACE("checkbox index %d\n", nIndex
);
2954 ReleaseDC(0, hdcScreen
);
2956 infoPtr
->stateImageWidth
= 16;
2957 infoPtr
->stateImageHeight
= 16;
2961 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2963 TREEVIEW_ITEM
*child
= item
->firstChild
;
2965 item
->state
&= ~TVIS_STATEIMAGEMASK
;
2966 item
->state
|= INDEXTOSTATEIMAGEMASK(1);
2970 TREEVIEW_ITEM
*next
= child
->nextSibling
;
2971 TREEVIEW_ResetImageStateIndex(infoPtr
, child
);
2977 TREEVIEW_Paint(TREEVIEW_INFO
*infoPtr
, HDC hdc_ref
)
2983 TRACE("(%p %p)\n", infoPtr
, hdc_ref
);
2985 if ((infoPtr
->dwStyle
& TVS_CHECKBOXES
) && !infoPtr
->himlState
)
2987 TREEVIEW_InitCheckboxes(infoPtr
);
2988 TREEVIEW_ResetImageStateIndex(infoPtr
, infoPtr
->root
);
2990 TREEVIEW_EndEditLabelNow(infoPtr
, TRUE
);
2991 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
2992 TREEVIEW_UpdateScrollBars(infoPtr
);
2993 TREEVIEW_Invalidate(infoPtr
, NULL
);
2999 GetClientRect(infoPtr
->hwnd
, &rc
);
3000 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rc
);
3004 hdc
= BeginPaint(infoPtr
->hwnd
, &ps
);
3007 TREEVIEW_FillBkgnd(infoPtr
, hdc
, &rc
);
3010 if(infoPtr
->bRedraw
) /* WM_SETREDRAW sets bRedraw */
3011 TREEVIEW_Refresh(infoPtr
, hdc
, &rc
);
3014 EndPaint(infoPtr
->hwnd
, &ps
);
3020 TREEVIEW_PrintClient(TREEVIEW_INFO
*infoPtr
, HDC hdc
, DWORD options
)
3022 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc
, options
);
3024 if ((options
& PRF_CHECKVISIBLE
) && !IsWindowVisible(infoPtr
->hwnd
))
3027 if (options
& PRF_ERASEBKGND
)
3028 TREEVIEW_EraseBackground(infoPtr
, hdc
);
3030 if (options
& PRF_CLIENT
)
3033 GetClientRect(infoPtr
->hwnd
, &rc
);
3034 TREEVIEW_Refresh(infoPtr
, hdc
, &rc
);
3040 /* Sorting **************************************************************/
3042 /***************************************************************************
3043 * Forward the DPA local callback to the treeview owner callback
3046 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM
*first
, const TREEVIEW_ITEM
*second
,
3047 const TVSORTCB
*pCallBackSort
)
3049 /* Forward the call to the client-defined callback */
3050 return pCallBackSort
->lpfnCompare(first
->lParam
,
3052 pCallBackSort
->lParam
);
3055 /***************************************************************************
3056 * Treeview native sort routine: sort on item text.
3059 TREEVIEW_SortOnName(TREEVIEW_ITEM
*first
, TREEVIEW_ITEM
*second
,
3060 const TREEVIEW_INFO
*infoPtr
)
3062 TREEVIEW_UpdateDispInfo(infoPtr
, first
, TVIF_TEXT
);
3063 TREEVIEW_UpdateDispInfo(infoPtr
, second
, TVIF_TEXT
);
3065 if(first
->pszText
&& second
->pszText
)
3066 return lstrcmpiW(first
->pszText
, second
->pszText
);
3067 else if(first
->pszText
)
3069 else if(second
->pszText
)
3075 /* Returns the number of physical children belonging to item. */
3077 TREEVIEW_CountChildren(const TREEVIEW_ITEM
*item
)
3082 for (hti
= item
->firstChild
; hti
!= NULL
; hti
= hti
->nextSibling
)
3088 /* Returns a DPA containing a pointer to each physical child of item in
3089 * sibling order. If item has no children, an empty DPA is returned. */
3091 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM
*item
)
3095 HDPA list
= DPA_Create(8);
3096 if (list
== 0) return NULL
;
3098 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
3100 if (DPA_InsertPtr(list
, INT_MAX
, child
) == -1)
3110 /***************************************************************************
3111 * Setup the treeview structure with regards of the sort method
3112 * and sort the children of the TV item specified in lParam
3113 * fRecurse: currently unused. Should be zero.
3114 * parent: if pSort!=NULL, should equal pSort->hParent.
3115 * otherwise, item which child items are to be sorted.
3116 * pSort: sort method info. if NULL, sort on item text.
3117 * if non-NULL, sort on item's lParam content, and let the
3118 * application decide what that means. See also TVM_SORTCHILDRENCB.
3122 TREEVIEW_Sort(TREEVIEW_INFO
*infoPtr
, HTREEITEM parent
,
3126 PFNDPACOMPARE pfnCompare
;
3129 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3130 if (parent
== TVI_ROOT
|| parent
== NULL
)
3131 parent
= infoPtr
->root
;
3133 /* Check for a valid handle to the parent item */
3134 if (!TREEVIEW_ValidItem(infoPtr
, parent
))
3136 ERR("invalid item hParent=%p\n", parent
);
3142 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_CallBackCompare
;
3143 lpCompare
= (LPARAM
)pSort
;
3147 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_SortOnName
;
3148 lpCompare
= (LPARAM
)infoPtr
;
3151 cChildren
= TREEVIEW_CountChildren(parent
);
3153 /* Make sure there is something to sort */
3156 /* TREEVIEW_ITEM rechaining */
3159 HTREEITEM nextItem
= 0;
3160 HTREEITEM prevItem
= 0;
3162 HDPA sortList
= TREEVIEW_BuildChildDPA(parent
);
3164 if (sortList
== NULL
)
3167 /* let DPA sort the list */
3168 DPA_Sort(sortList
, pfnCompare
, lpCompare
);
3170 /* The order of DPA entries has been changed, so fixup the
3171 * nextSibling and prevSibling pointers. */
3173 item
= DPA_GetPtr(sortList
, count
++);
3174 while ((nextItem
= DPA_GetPtr(sortList
, count
++)) != NULL
)
3176 /* link the two current item together */
3177 item
->nextSibling
= nextItem
;
3178 nextItem
->prevSibling
= item
;
3180 if (prevItem
== NULL
)
3182 /* this is the first item, update the parent */
3183 parent
->firstChild
= item
;
3184 item
->prevSibling
= NULL
;
3188 /* fix the back chaining */
3189 item
->prevSibling
= prevItem
;
3192 /* get ready for the next one */
3197 /* the last item is pointed to by item and never has a sibling */
3198 item
->nextSibling
= NULL
;
3199 parent
->lastChild
= item
;
3201 DPA_Destroy(sortList
);
3203 TREEVIEW_VerifyTree(infoPtr
);
3205 if (parent
->state
& TVIS_EXPANDED
)
3207 int visOrder
= infoPtr
->firstVisible
->visibleOrder
;