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
7 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
9 * Note2: All items always! have valid (allocated) pszText field.
10 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
11 * of size TEXT_CALLBACK_SIZE in DoSetItem.
12 * We use callbackMask to keep track of fields to be updated.
15 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
16 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
18 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_NOSCROLL,
19 * TVS_RTLREADING, TVS_TRACKSELECT
21 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
23 * Make the insertion mark look right.
24 * Scroll (instead of repaint) as much as possible.
34 #include "debugtools.h"
36 /* internal structures */
38 typedef struct _TREEITEM
/* HTREEITEM is a _TREEINFO *. */
49 int iIntegral
; /* item height multiplier (1 is normal) */
50 int iLevel
; /* indentation level:0=root level */
51 HTREEITEM parent
; /* handle to parent or 0 if at root*/
52 HTREEITEM firstChild
; /* handle to first child or 0 if no child*/
54 HTREEITEM prevSibling
; /* handle to prev item in list, 0 if first */
55 HTREEITEM nextSibling
; /* handle to next item in list, 0 if last */
61 LONG textWidth
; /* horizontal text extent for pszText */
62 LONG visibleOrder
; /* visible ordering, 0 is first visible item */
66 typedef struct tagTREEVIEW_INFO
73 UINT uNumItems
; /* number of valid TREEVIEW_ITEMs */
74 INT cdmode
; /* last custom draw setting */
75 UINT uScrollTime
; /* max. time for scrolling in milliseconds*/
76 BOOL bRedraw
; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
78 UINT uItemHeight
; /* item height */
81 LONG clientWidth
; /* width of control window */
82 LONG clientHeight
; /* height of control window */
84 LONG treeWidth
; /* width of visible tree items */
85 LONG treeHeight
; /* height of visible tree items */
87 UINT uIndent
; /* indentation in pixels */
88 HTREEITEM selectedItem
; /* handle to selected item or 0 if none */
89 HTREEITEM hotItem
; /* handle currently under cursor, 0 if none */
90 HTREEITEM focusedItem
; /* item that was under the cursor when WM_LBUTTONDOWN was received */
92 HTREEITEM firstVisible
; /* handle to first visible item */
94 HTREEITEM dropItem
; /* handle to item selected by drag cursor */
95 HTREEITEM insertMarkItem
; /* item after which insertion mark is placed */
96 BOOL insertBeforeorAfter
; /* flag used by TVM_SETINSERTMARK */
97 HIMAGELIST dragList
; /* Bitmap of dragged item */
102 COLORREF clrInsertMark
;
108 WNDPROC wpEditOrig
; /* orig window proc for subclassing edit */
109 BOOL bIgnoreEditKillFocus
;
112 HIMAGELIST himlNormal
;
113 int normalImageHeight
;
114 int normalImageWidth
;
115 HIMAGELIST himlState
;
116 int stateImageHeight
;
123 /* bitflags for infoPtr->uInternalStatus */
125 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
126 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
127 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
128 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
129 #define TV_RDRAG 0x10 /* dito Rbutton */
130 #define TV_RDRAGGING 0x20
132 /* bitflags for infoPtr->timer */
134 #define TV_EDIT_TIMER 2
135 #define TV_EDIT_TIMER_SET 2
138 VOID
TREEVIEW_Register (VOID
);
139 VOID
TREEVIEW_Unregister (VOID
);
142 DEFAULT_DEBUG_CHANNEL(treeview
);
145 #define TEXT_CALLBACK_SIZE 260
147 #define TREEVIEW_LEFT_MARGIN 8
149 #define MINIMUM_INDENT 19
151 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
153 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
154 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
155 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
158 typedef VOID (*TREEVIEW_ItemEnumFunc
)(TREEVIEW_INFO
*, TREEVIEW_ITEM
*,LPVOID
);
161 static VOID
TREEVIEW_QueueRefresh(TREEVIEW_INFO
*);
162 static VOID
TREEVIEW_QueueItemRefresh(TREEVIEW_INFO
*, TREEVIEW_ITEM
*);
164 static LRESULT
TREEVIEW_DoSelectItem(TREEVIEW_INFO
*, INT
, HTREEITEM
, INT
);
165 static VOID
TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*, TREEVIEW_ITEM
*, BOOL
);
166 static LRESULT
TREEVIEW_EnsureVisible(TREEVIEW_INFO
*, HTREEITEM
, BOOL
);
167 static LRESULT
TREEVIEW_RButtonUp(TREEVIEW_INFO
*, LPPOINT
);
168 static LRESULT
TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
);
169 static VOID
TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
);
170 static LRESULT
TREEVIEW_HScroll(TREEVIEW_INFO
*, WPARAM
);
173 /* Random Utilities *****************************************************/
177 TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
)
182 /* The definition is at the end of the file. */
183 static void TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
);
186 /* Returns the treeview private data if hwnd is a treeview.
187 * Otherwise returns an undefined value. */
188 static TREEVIEW_INFO
*
189 TREEVIEW_GetInfoPtr(HWND hwnd
)
191 return (TREEVIEW_INFO
*)GetWindowLongA(hwnd
, 0);
194 /* Don't call this. Nothing wants an item index. */
196 TREEVIEW_GetItemIndex(TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
198 assert(infoPtr
!= NULL
);
200 return DPA_GetPtrIndex(infoPtr
->items
, handle
);
203 /***************************************************************************
204 * This method checks that handle is an item for this tree.
207 TREEVIEW_ValidItem(TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
209 if (TREEVIEW_GetItemIndex(infoPtr
, handle
) == -1)
211 TRACE("invalid item %p\n", handle
);
219 TREEVIEW_CreateBoldFont(HFONT hOrigFont
)
223 GetObjectA(hOrigFont
, sizeof(font
), &font
);
224 font
.lfWeight
= FW_BOLD
;
225 return CreateFontIndirectA(&font
);
229 TREEVIEW_FontForItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
231 return (item
->state
& TVIS_BOLD
) ? infoPtr
->hBoldFont
: infoPtr
->hFont
;
234 /* for trace/debugging purposes only */
236 TREEVIEW_ItemName(TREEVIEW_ITEM
*item
)
238 if (item
== NULL
) return "<null item>";
239 if (item
->pszText
== LPSTR_TEXTCALLBACKA
) return "<callback>";
240 if (item
->pszText
== NULL
) return "<null>";
241 return item
->pszText
;
244 /* An item is not a child of itself. */
246 TREEVIEW_IsChildOf(TREEVIEW_ITEM
*parent
, TREEVIEW_ITEM
*child
)
250 child
= child
->parent
;
251 if (child
== parent
) return TRUE
;
252 } while (child
!= NULL
);
258 /* Tree Traversal *******************************************************/
260 /***************************************************************************
261 * This method returns the last expanded sibling or child child item
264 static TREEVIEW_ITEM
*
265 TREEVIEW_GetLastListItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
)
270 while (wineItem
->lastChild
)
272 if (wineItem
->state
& TVIS_EXPANDED
)
273 wineItem
= wineItem
->lastChild
;
278 if (wineItem
== infoPtr
->root
)
284 /***************************************************************************
285 * This method returns the previous non-hidden item in the list not
286 * considering the tree hierarchy.
288 static TREEVIEW_ITEM
*
289 TREEVIEW_GetPrevListItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*tvItem
)
291 if (tvItem
->prevSibling
)
293 /* This item has a prevSibling, get the last item in the sibling's tree. */
294 TREEVIEW_ITEM
*upItem
= tvItem
->prevSibling
;
296 if ((upItem
->state
& TVIS_EXPANDED
) && upItem
->lastChild
!= NULL
)
297 return TREEVIEW_GetLastListItem(infoPtr
, upItem
->lastChild
);
303 /* this item does not have a prevSibling, get the parent */
304 return (tvItem
->parent
!= infoPtr
->root
) ? tvItem
->parent
: NULL
;
309 /***************************************************************************
310 * This method returns the next physical item in the treeview not
311 * considering the tree hierarchy.
313 static TREEVIEW_ITEM
*
314 TREEVIEW_GetNextListItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*tvItem
)
316 assert(tvItem
!= NULL
);
319 * If this item has children and is expanded, return the first child
321 if ((tvItem
->state
& TVIS_EXPANDED
) && tvItem
->firstChild
!= NULL
)
323 return tvItem
->firstChild
;
328 * try to get the sibling
330 if (tvItem
->nextSibling
)
331 return tvItem
->nextSibling
;
334 * Otherwise, get the parent's sibling.
336 while (tvItem
->parent
)
338 tvItem
= tvItem
->parent
;
340 if (tvItem
->nextSibling
)
341 return tvItem
->nextSibling
;
347 /***************************************************************************
348 * This method returns the nth item starting at the given item. It returns
349 * the last item (or first) we we run out of items.
351 * Will scroll backward if count is <0.
352 * forward if count is >0.
354 static TREEVIEW_ITEM
*
355 TREEVIEW_GetListItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
358 TREEVIEW_ITEM
*(*next_item
)(TREEVIEW_INFO
*, TREEVIEW_ITEM
*);
359 TREEVIEW_ITEM
*previousItem
;
361 assert(wineItem
!= NULL
);
365 next_item
= TREEVIEW_GetNextListItem
;
370 next_item
= TREEVIEW_GetPrevListItem
;
377 previousItem
= wineItem
;
378 wineItem
= next_item(infoPtr
, wineItem
);
380 } while (--count
&& wineItem
!= NULL
);
383 return wineItem
? wineItem
: previousItem
;
386 /* Notifications ************************************************************/
389 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO
*infoPtr
, UINT code
)
392 HWND hwnd
= infoPtr
->hwnd
;
395 nmhdr
.hwndFrom
= hwnd
;
396 nmhdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
399 return (BOOL
)SendMessageA(GetParent(hwnd
), WM_NOTIFY
,
400 (WPARAM
)nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
404 TREEVIEW_TVItemFromItem(UINT mask
, TVITEMA
*tvItem
, TREEVIEW_ITEM
*item
)
407 tvItem
->hItem
= item
;
408 tvItem
->state
= item
->state
;
409 tvItem
->stateMask
= 0;
410 tvItem
->iImage
= item
->iImage
;
411 tvItem
->pszText
= item
->pszText
;
412 tvItem
->cchTextMax
= item
->cchTextMax
;
413 tvItem
->iImage
= item
->iImage
;
414 tvItem
->iSelectedImage
= item
->iSelectedImage
;
415 tvItem
->cChildren
= item
->cChildren
;
416 tvItem
->lParam
= item
->lParam
;
420 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO
*infoPtr
, UINT code
, UINT action
,
421 UINT mask
, HTREEITEM oldItem
, HTREEITEM newItem
)
423 HWND hwnd
= infoPtr
->hwnd
;
426 TRACE("code:%x action:%x olditem:%p newitem:%p\n",
427 code
, action
, oldItem
, newItem
);
429 ZeroMemory(&nmhdr
, sizeof(NMTREEVIEWA
));
431 nmhdr
.hdr
.hwndFrom
= hwnd
;
432 nmhdr
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
433 nmhdr
.hdr
.code
= code
;
434 nmhdr
.action
= action
;
437 TREEVIEW_TVItemFromItem(mask
, &nmhdr
.itemOld
, oldItem
);
440 TREEVIEW_TVItemFromItem(mask
, &nmhdr
.itemNew
, newItem
);
445 return (BOOL
)SendMessageA(GetParent(hwnd
), WM_NOTIFY
,
446 (WPARAM
)GetWindowLongA(hwnd
, GWL_ID
),
451 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO
*infoPtr
, UINT code
,
452 HTREEITEM dragItem
, POINT pt
)
454 HWND hwnd
= infoPtr
->hwnd
;
457 TRACE("code:%x dragitem:%p\n", code
, dragItem
);
459 nmhdr
.hdr
.hwndFrom
= hwnd
;
460 nmhdr
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
461 nmhdr
.hdr
.code
= code
;
463 nmhdr
.itemNew
.mask
= TVIF_STATE
| TVIF_PARAM
| TVIF_HANDLE
;
464 nmhdr
.itemNew
.hItem
= dragItem
;
465 nmhdr
.itemNew
.state
= dragItem
->state
;
466 nmhdr
.itemNew
.lParam
= dragItem
->lParam
;
468 nmhdr
.ptDrag
.x
= pt
.x
;
469 nmhdr
.ptDrag
.y
= pt
.y
;
471 return (BOOL
)SendMessageA(GetParent(hwnd
), WM_NOTIFY
,
472 (WPARAM
)GetWindowLongA(hwnd
, GWL_ID
),
478 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO
*infoPtr
, DWORD dwDrawStage
,
481 HWND hwnd
= infoPtr
->hwnd
;
482 NMTVCUSTOMDRAW nmcdhdr
;
485 TRACE("drawstage:%lx hdc:%x\n", dwDrawStage
, hdc
);
487 nmcd
= &nmcdhdr
.nmcd
;
488 nmcd
->hdr
.hwndFrom
= hwnd
;
489 nmcd
->hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
490 nmcd
->hdr
.code
= NM_CUSTOMDRAW
;
491 nmcd
->dwDrawStage
= dwDrawStage
;
494 nmcd
->dwItemSpec
= 0;
495 nmcd
->uItemState
= 0;
496 nmcd
->lItemlParam
= 0;
497 nmcdhdr
.clrText
= infoPtr
->clrText
;
498 nmcdhdr
.clrTextBk
= infoPtr
->clrBk
;
501 return (BOOL
)SendMessageA(GetParent(hwnd
), WM_NOTIFY
,
502 (WPARAM
)GetWindowLongA(hwnd
, GWL_ID
),
508 /* FIXME: need to find out when the flags in uItemState need to be set */
511 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO
*infoPtr
, HDC hdc
,
512 TREEVIEW_ITEM
*wineItem
, UINT uItemDrawState
)
514 HWND hwnd
= infoPtr
->hwnd
;
515 NMTVCUSTOMDRAW nmcdhdr
;
517 DWORD dwDrawStage
, dwItemSpec
;
521 dwDrawStage
= CDDS_ITEM
| uItemDrawState
;
522 dwItemSpec
= (DWORD
)wineItem
;
524 if (wineItem
->state
& TVIS_SELECTED
)
525 uItemState
|= CDIS_SELECTED
;
526 if (wineItem
== infoPtr
->selectedItem
)
527 uItemState
|= CDIS_FOCUS
;
528 if (wineItem
== infoPtr
->hotItem
)
529 uItemState
|= CDIS_HOT
;
531 nmcd
= &nmcdhdr
.nmcd
;
532 nmcd
->hdr
.hwndFrom
= hwnd
;
533 nmcd
->hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
534 nmcd
->hdr
.code
= NM_CUSTOMDRAW
;
535 nmcd
->dwDrawStage
= dwDrawStage
;
537 nmcd
->rc
= wineItem
->rect
;
538 nmcd
->dwItemSpec
= dwItemSpec
;
539 nmcd
->uItemState
= uItemState
;
540 nmcd
->lItemlParam
= wineItem
->lParam
;
541 nmcdhdr
.clrText
= infoPtr
->clrText
;
542 nmcdhdr
.clrTextBk
= infoPtr
->clrBk
;
543 nmcdhdr
.iLevel
= wineItem
->iLevel
;
545 TRACE("drawstage:%lx hdc:%x item:%lx, itemstate:%x, lItemlParam:%lx\n",
546 nmcd
->dwDrawStage
, nmcd
->hdc
, nmcd
->dwItemSpec
,
547 nmcd
->uItemState
, nmcd
->lItemlParam
);
549 retval
= SendMessageA(GetParent(hwnd
), WM_NOTIFY
,
550 (WPARAM
)GetWindowLongA(hwnd
, GWL_ID
),
553 infoPtr
->clrText
= nmcdhdr
.clrText
;
554 infoPtr
->clrBk
= nmcdhdr
.clrTextBk
;
559 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*editItem
)
561 HWND hwnd
= infoPtr
->hwnd
;
564 tvdi
.hdr
.hwndFrom
= hwnd
;
565 tvdi
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
566 tvdi
.hdr
.code
= TVN_BEGINLABELEDITA
;
568 tvdi
.item
.mask
= TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
| TVIF_TEXT
;
569 tvdi
.item
.hItem
= editItem
;
570 tvdi
.item
.state
= editItem
->state
;
571 tvdi
.item
.lParam
= editItem
->lParam
;
572 tvdi
.item
.pszText
= editItem
->pszText
;
573 tvdi
.item
.cchTextMax
= editItem
->cchTextMax
;
575 return SendMessageA(GetParent(hwnd
), WM_NOTIFY
, tvdi
.hdr
.idFrom
,
580 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
583 NMTVDISPINFOA callback
;
584 HWND hwnd
= infoPtr
->hwnd
;
586 mask
&= wineItem
->callbackMask
;
588 if (mask
== 0) return;
590 callback
.hdr
.hwndFrom
= hwnd
;
591 callback
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
592 callback
.hdr
.code
= TVN_GETDISPINFOA
;
594 /* 'state' always contains valid value, as well as 'lParam'.
595 * All other parameters are uninitialized.
597 callback
.item
.pszText
= wineItem
->pszText
;
598 callback
.item
.cchTextMax
= wineItem
->cchTextMax
;
599 callback
.item
.mask
= mask
;
600 callback
.item
.hItem
= wineItem
;
601 callback
.item
.state
= wineItem
->state
;
602 callback
.item
.lParam
= wineItem
->lParam
;
604 /* If text is changed we need to recalculate textWidth */
605 if (mask
& TVIF_TEXT
)
606 wineItem
->textWidth
= 0;
608 SendMessageA(GetParent(hwnd
), WM_NOTIFY
, callback
.hdr
.idFrom
, (LPARAM
)&callback
);
610 /* It may have changed due to a call to SetItem. */
611 mask
&= wineItem
->callbackMask
;
613 if ((mask
& TVIF_TEXT
) && callback
.item
.pszText
!= wineItem
->pszText
)
615 /* Instead of copying text into our buffer user specified its own */
616 int len
= max(lstrlenA(callback
.item
.pszText
) + 1, TEXT_CALLBACK_SIZE
);
617 LPSTR newText
= COMCTL32_ReAlloc(wineItem
->pszText
, len
);
621 wineItem
->pszText
= newText
;
622 strcpy(wineItem
->pszText
, callback
.item
.pszText
);
623 wineItem
->cchTextMax
= len
;
625 /* If ReAlloc fails we have nothing to do, but keep original text */
628 if (mask
& TVIF_IMAGE
)
629 wineItem
->iImage
= callback
.item
.iImage
;
631 if (mask
& TVIF_SELECTEDIMAGE
)
632 wineItem
->iSelectedImage
= callback
.item
.iSelectedImage
;
634 if (mask
& TVIF_CHILDREN
)
635 wineItem
->cChildren
= callback
.item
.cChildren
;
637 /* These members are now permanently set. */
638 if (callback
.item
.mask
& TVIF_DI_SETITEM
)
639 wineItem
->callbackMask
&= ~callback
.item
.mask
;
642 /***************************************************************************
643 * This function uses cChildren field to decide whether the item has
645 * Note: if this returns TRUE, the child items may not actually exist,
646 * they could be virtual.
648 * Just use wineItem->firstChild to check for physical children.
651 TREEVIEW_HasChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
)
653 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, TVIF_CHILDREN
);
655 return wineItem
->cChildren
> 0;
659 /* Item Position ********************************************************/
661 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
663 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO
*infoPtr
,
666 /* Same effect, different optimisation. */
668 BOOL lar
= ((infoPtr
->dwStyle
& TVS_LINESATROOT
)
669 && (infoPtr
->dwStyle
& (TVS_HASLINES
|TVS_HASBUTTONS
)));
671 BOOL lar
= ((infoPtr
->dwStyle
672 & (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
676 item
->linesOffset
= infoPtr
->uIndent
* (item
->iLevel
+ lar
- 1)
678 item
->stateOffset
= item
->linesOffset
+ infoPtr
->uIndent
;
679 item
->imageOffset
= item
->stateOffset
680 + (STATEIMAGEINDEX(item
->state
) ? infoPtr
->stateImageWidth
: 0);
681 item
->textOffset
= item
->imageOffset
+ infoPtr
->normalImageWidth
;
685 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
, HDC hDC
)
691 /* DRAW's OM docker creates items like this */
692 if (item
->pszText
== NULL
)
698 if (item
->textWidth
!= 0 && !(item
->callbackMask
& TVIF_TEXT
))
707 hdc
= GetDC(infoPtr
->hwnd
);
708 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, item
));
711 GetTextExtentPoint32A(hdc
, item
->pszText
, strlen(item
->pszText
), &sz
);
712 item
->textWidth
= sz
.cx
;
716 SelectObject(hdc
, hOldFont
);
722 TREEVIEW_ComputeItemRect(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
724 item
->rect
.top
= infoPtr
->uItemHeight
*
725 (item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
);
727 item
->rect
.bottom
= item
->rect
.top
728 + infoPtr
->uItemHeight
* item
->iIntegral
;
731 item
->rect
.right
= infoPtr
->clientWidth
;
734 /* We know that only items after start need their order updated. */
736 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*start
)
743 start
= infoPtr
->root
->firstChild
;
747 order
= start
->visibleOrder
;
749 for (item
= start
; item
!= NULL
;
750 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
752 item
->visibleOrder
= order
;
753 order
+= item
->iIntegral
;
756 infoPtr
->maxVisibleOrder
= order
;
758 for (item
= start
; item
!= NULL
;
759 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
761 TREEVIEW_ComputeItemRect(infoPtr
, item
);
766 /* Update metrics of all items in selected subtree.
767 * root must be expanded
770 TREEVIEW_UpdateSubTree(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*root
)
772 TREEVIEW_ITEM
*sibling
;
776 if (!root
->firstChild
|| !(root
->state
& TVIS_EXPANDED
))
779 root
->state
&= ~TVIS_EXPANDED
;
780 sibling
= TREEVIEW_GetNextListItem(infoPtr
, root
);
781 root
->state
|= TVIS_EXPANDED
;
783 hdc
= GetDC(infoPtr
->hwnd
);
784 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
786 for (; root
!= sibling
;
787 root
= TREEVIEW_GetNextListItem(infoPtr
, root
))
789 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, root
);
791 if (root
->callbackMask
& TVIF_TEXT
)
792 TREEVIEW_UpdateDispInfo(infoPtr
, root
, TVIF_TEXT
);
794 if (root
->textWidth
== 0)
796 SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, root
));
797 TREEVIEW_ComputeTextWidth(infoPtr
, root
, hdc
);
801 SelectObject(hdc
, hOldFont
);
802 ReleaseDC(infoPtr
->hwnd
, hdc
);
805 /* Item Allocation **********************************************************/
807 static TREEVIEW_ITEM
*
808 TREEVIEW_AllocateItem(TREEVIEW_INFO
*infoPtr
)
810 TREEVIEW_ITEM
*newItem
= COMCTL32_Alloc(sizeof(TREEVIEW_ITEM
));
815 if (DPA_InsertPtr(infoPtr
->items
, INT_MAX
, newItem
) == -1)
817 COMCTL32_Free(newItem
);
824 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
825 * free item->pszText. */
827 TREEVIEW_FreeItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
829 DPA_DeletePtr(infoPtr
->items
, DPA_GetPtrIndex(infoPtr
->items
, item
));
834 /* Item Insertion *******************************************************/
836 /***************************************************************************
837 * This method inserts newItem before sibling as a child of parent.
838 * sibling can be NULL, but only if parent has no children.
841 TREEVIEW_InsertBefore(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
842 TREEVIEW_ITEM
*parent
)
844 assert(newItem
!= NULL
);
845 assert(parent
!= NULL
);
849 assert(sibling
->parent
== parent
);
851 if (sibling
->prevSibling
!= NULL
)
852 sibling
->prevSibling
->nextSibling
= newItem
;
854 newItem
->prevSibling
= sibling
->prevSibling
;
855 sibling
->prevSibling
= newItem
;
858 newItem
->prevSibling
= NULL
;
860 newItem
->nextSibling
= sibling
;
862 if (parent
->firstChild
== sibling
)
863 parent
->firstChild
= newItem
;
865 if (parent
->lastChild
== NULL
)
866 parent
->lastChild
= newItem
;
869 /***************************************************************************
870 * This method inserts newItem after sibling as a child of parent.
871 * sibling can be NULL, but only if parent has no children.
874 TREEVIEW_InsertAfter(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
875 TREEVIEW_ITEM
*parent
)
877 assert(newItem
!= NULL
);
878 assert(parent
!= NULL
);
882 assert(sibling
->parent
== parent
);
884 if (sibling
->nextSibling
!= NULL
)
885 sibling
->nextSibling
->prevSibling
= newItem
;
887 newItem
->nextSibling
= sibling
->nextSibling
;
888 sibling
->nextSibling
= newItem
;
891 newItem
->nextSibling
= NULL
;
893 newItem
->prevSibling
= sibling
;
895 if (parent
->lastChild
== sibling
)
896 parent
->lastChild
= newItem
;
898 if (parent
->firstChild
== NULL
)
899 parent
->firstChild
= newItem
;
903 TREEVIEW_DoSetItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
904 const TVITEMEXA
*tvItem
)
906 UINT callbackClear
= 0;
907 UINT callbackSet
= 0;
909 /* Do this first in case it fails. */
910 if (tvItem
->mask
& TVIF_TEXT
)
912 if (tvItem
->pszText
!= LPSTR_TEXTCALLBACKA
)
914 int len
= lstrlenA(tvItem
->pszText
) + 1;
915 LPSTR newText
= COMCTL32_ReAlloc(wineItem
->pszText
, len
);
917 if (newText
== NULL
) return FALSE
;
919 callbackClear
|= TVIF_TEXT
;
921 wineItem
->pszText
= newText
;
922 wineItem
->cchTextMax
= len
;
923 lstrcpynA(wineItem
->pszText
, tvItem
->pszText
, len
);
927 callbackSet
|= TVIF_TEXT
;
929 wineItem
->pszText
= COMCTL32_ReAlloc(wineItem
->pszText
,
931 wineItem
->cchTextMax
= TEXT_CALLBACK_SIZE
;
935 if (tvItem
->mask
& TVIF_CHILDREN
)
937 wineItem
->cChildren
= tvItem
->cChildren
;
939 if (wineItem
->cChildren
== I_CHILDRENCALLBACK
)
940 callbackSet
|= TVIF_CHILDREN
;
942 callbackClear
|= TVIF_CHILDREN
;
945 if (tvItem
->mask
& TVIF_IMAGE
)
947 wineItem
->iImage
= tvItem
->iImage
;
949 if (wineItem
->iImage
== I_IMAGECALLBACK
)
950 callbackSet
|= TVIF_IMAGE
;
952 callbackClear
|= TVIF_IMAGE
;
955 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
957 wineItem
->iSelectedImage
= tvItem
->iSelectedImage
;
959 if (wineItem
->iSelectedImage
== I_IMAGECALLBACK
)
960 callbackSet
|= TVIF_SELECTEDIMAGE
;
962 callbackClear
|= TVIF_SELECTEDIMAGE
;
965 if (tvItem
->mask
& TVIF_PARAM
)
966 wineItem
->lParam
= tvItem
->lParam
;
968 /* If the application sets TVIF_INTEGRAL without
969 * supplying a TVITEMEX structure, it's toast. */
970 if (tvItem
->mask
& TVIF_INTEGRAL
)
971 wineItem
->iIntegral
= tvItem
->iIntegral
;
973 if (tvItem
->mask
& TVIF_STATE
)
975 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem
->state
, tvItem
->state
,
977 wineItem
->state
&= ~tvItem
->stateMask
;
978 wineItem
->state
|= (tvItem
->state
& tvItem
->stateMask
);
981 wineItem
->callbackMask
|= callbackSet
;
982 wineItem
->callbackMask
&= ~callbackClear
;
987 /* Note that the new item is pre-zeroed. */
989 TREEVIEW_InsertItemA(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
991 const TVINSERTSTRUCTA
*ptdi
= (LPTVINSERTSTRUCTA
) lParam
;
992 const TVITEMEXA
*tvItem
= &ptdi
->DUMMYUNIONNAME
.itemex
;
993 HTREEITEM insertAfter
;
994 TREEVIEW_ITEM
*newItem
, *parentItem
;
995 BOOL bTextUpdated
= FALSE
;
997 if (ptdi
->hParent
== TVI_ROOT
|| ptdi
->hParent
== 0)
999 parentItem
= infoPtr
->root
;
1003 parentItem
= ptdi
->hParent
;
1005 if (!TREEVIEW_ValidItem(infoPtr
, parentItem
))
1007 WARN("invalid parent %p\n", parentItem
);
1008 return (LRESULT
)(HTREEITEM
)NULL
;
1012 insertAfter
= ptdi
->hInsertAfter
;
1014 /* Validate this now for convenience. */
1015 switch ((DWORD
)insertAfter
)
1017 case (DWORD
)TVI_FIRST
:
1018 case (DWORD
)TVI_LAST
:
1019 case (DWORD
)TVI_SORT
:
1023 if (!TREEVIEW_ValidItem(infoPtr
, insertAfter
) ||
1024 insertAfter
->parent
!= parentItem
)
1026 WARN("invalid insert after %p\n", insertAfter
);
1027 insertAfter
= TVI_LAST
;
1031 TRACE("parent %p position %p: %s\n", parentItem
, insertAfter
,
1032 (tvItem
->mask
& TVIF_TEXT
)
1033 ? ((tvItem
->pszText
== LPSTR_TEXTCALLBACKA
) ? "<callback>"
1037 newItem
= TREEVIEW_AllocateItem(infoPtr
);
1038 if (newItem
== NULL
)
1039 return (LRESULT
)(HTREEITEM
)NULL
;
1041 newItem
->parent
= parentItem
;
1042 newItem
->iIntegral
= 1;
1044 if (!TREEVIEW_DoSetItem(infoPtr
, newItem
, tvItem
))
1045 return (LRESULT
)(HTREEITEM
)NULL
;
1047 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1049 infoPtr
->uNumItems
++;
1051 switch ((DWORD
)insertAfter
)
1053 case (DWORD
)TVI_FIRST
:
1054 TREEVIEW_InsertBefore(newItem
, parentItem
->firstChild
, parentItem
);
1055 if (infoPtr
->firstVisible
== parentItem
->firstChild
)
1056 TREEVIEW_SetFirstVisible(infoPtr
, newItem
, TRUE
);
1059 case (DWORD
)TVI_LAST
:
1060 TREEVIEW_InsertAfter(newItem
, parentItem
->lastChild
, parentItem
);
1063 /* hInsertAfter names a specific item we want to insert after */
1065 TREEVIEW_InsertAfter(newItem
, insertAfter
, insertAfter
->parent
);
1068 case (DWORD
)TVI_SORT
:
1070 TREEVIEW_ITEM
*aChild
;
1071 TREEVIEW_ITEM
*previousChild
= NULL
;
1072 BOOL bItemInserted
= FALSE
;
1074 aChild
= parentItem
->firstChild
;
1076 bTextUpdated
= TRUE
;
1077 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1079 /* Iterate the parent children to see where we fit in */
1080 while (aChild
!= NULL
)
1084 TREEVIEW_UpdateDispInfo(infoPtr
, aChild
, TVIF_TEXT
);
1085 comp
= lstrcmpA(newItem
->pszText
, aChild
->pszText
);
1087 if (comp
< 0) /* we are smaller than the current one */
1089 TREEVIEW_InsertBefore(newItem
, aChild
, parentItem
);
1090 bItemInserted
= TRUE
;
1093 else if (comp
> 0) /* we are bigger than the current one */
1095 previousChild
= aChild
;
1097 /* This will help us to exit if there is no more sibling */
1098 aChild
= (aChild
->nextSibling
== 0)
1100 : aChild
->nextSibling
;
1102 /* Look at the next item */
1108 * An item with this name is already existing, therefore,
1109 * we add after the one we found
1111 TREEVIEW_InsertAfter(newItem
, aChild
, parentItem
);
1112 bItemInserted
= TRUE
;
1118 * we reach the end of the child list and the item as not
1119 * yet been inserted, therefore, insert it after the last child.
1121 if ((!bItemInserted
) && (aChild
== NULL
))
1122 TREEVIEW_InsertAfter(newItem
, previousChild
, parentItem
);
1129 TRACE("new item %p; parent %p, mask %x\n", newItem
,
1130 newItem
->parent
, tvItem
->mask
);
1132 newItem
->iLevel
= newItem
->parent
->iLevel
+ 1;
1134 if (newItem
->parent
->cChildren
== 0)
1135 newItem
->parent
->cChildren
= 1;
1137 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
1139 if (STATEIMAGEINDEX(newItem
->state
) == 0)
1140 newItem
->state
|= INDEXTOSTATEIMAGEMASK(1);
1143 if (infoPtr
->firstVisible
== NULL
)
1144 infoPtr
->firstVisible
= newItem
;
1146 TREEVIEW_VerifyTree(infoPtr
);
1148 if (parentItem
== infoPtr
->root
||
1149 (ISVISIBLE(parentItem
) && parentItem
->state
& TVIS_EXPANDED
))
1151 TREEVIEW_ITEM
*prev
= TREEVIEW_GetPrevListItem(infoPtr
, newItem
);
1153 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1154 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, newItem
);
1157 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1159 TREEVIEW_ComputeTextWidth(infoPtr
, newItem
, 0);
1160 TREEVIEW_UpdateScrollBars(infoPtr
);
1161 TREEVIEW_QueueRefresh(infoPtr
);
1165 newItem
->visibleOrder
= -1;
1167 /* refresh treeview if newItem is the first item inserted under parentItem */
1168 if (ISVISIBLE(parentItem
) && newItem
->prevSibling
== newItem
->nextSibling
)
1170 /* parent got '+' - update it */
1171 TREEVIEW_QueueItemRefresh(infoPtr
, parentItem
);
1175 return (LRESULT
)newItem
;
1180 TREEVIEW_InsertItemW(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
1182 TVINSERTSTRUCTW
*tvisW
;
1183 TVINSERTSTRUCTA tvisA
;
1186 tvisW
= (LPTVINSERTSTRUCTW
) lParam
;
1188 tvisA
.hParent
= tvisW
->hParent
;
1189 tvisA
.hInsertAfter
= tvisW
->hInsertAfter
;
1191 tvisA
.DUMMYUNIONNAME
.item
.mask
= tvisW
->DUMMYUNIONNAME
.item
.mask
;
1192 tvisA
.DUMMYUNIONNAME
.item
.hItem
= tvisW
->DUMMYUNIONNAME
.item
.hItem
;
1193 tvisA
.DUMMYUNIONNAME
.item
.state
= tvisW
->DUMMYUNIONNAME
.item
.state
;
1194 tvisA
.DUMMYUNIONNAME
.item
.stateMask
= tvisW
->DUMMYUNIONNAME
.item
.stateMask
;
1195 tvisA
.DUMMYUNIONNAME
.item
.cchTextMax
=
1196 tvisW
->DUMMYUNIONNAME
.item
.cchTextMax
;
1198 if (tvisW
->DUMMYUNIONNAME
.item
.pszText
)
1200 if (tvisW
->DUMMYUNIONNAME
.item
.pszText
!= LPSTR_TEXTCALLBACKW
)
1202 int len
= lstrlenW(tvisW
->DUMMYUNIONNAME
.item
.pszText
) + 1;
1204 tvisA
.DUMMYUNIONNAME
.item
.pszText
= COMCTL32_Alloc(len
);
1205 lstrcpyWtoA(tvisA
.DUMMYUNIONNAME
.item
.pszText
,
1206 tvisW
->DUMMYUNIONNAME
.item
.pszText
);
1210 tvisA
.DUMMYUNIONNAME
.item
.pszText
= LPSTR_TEXTCALLBACKA
;
1211 tvisA
.DUMMYUNIONNAME
.item
.cchTextMax
= 0;
1215 tvisA
.DUMMYUNIONNAME
.item
.iImage
= tvisW
->DUMMYUNIONNAME
.item
.iImage
;
1216 tvisA
.DUMMYUNIONNAME
.item
.iSelectedImage
=
1217 tvisW
->DUMMYUNIONNAME
.item
.iSelectedImage
;
1218 tvisA
.DUMMYUNIONNAME
.item
.cChildren
= tvisW
->DUMMYUNIONNAME
.item
.cChildren
;
1219 tvisA
.DUMMYUNIONNAME
.item
.lParam
= tvisW
->DUMMYUNIONNAME
.item
.lParam
;
1221 lRes
= TREEVIEW_InsertItemA(infoPtr
, (LPARAM
)&tvisA
);
1223 if (tvisA
.DUMMYUNIONNAME
.item
.pszText
!= LPSTR_TEXTCALLBACKA
)
1225 COMCTL32_Free(tvisA
.DUMMYUNIONNAME
.item
.pszText
);
1233 /* Item Deletion ************************************************************/
1235 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
);
1238 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*parentItem
)
1240 TREEVIEW_ITEM
*kill
= parentItem
->firstChild
;
1242 while (kill
!= NULL
)
1244 TREEVIEW_ITEM
*next
= kill
->nextSibling
;
1246 TREEVIEW_RemoveItem(infoPtr
, kill
);
1251 assert(parentItem
->cChildren
<= 0); /* I_CHILDRENCALLBACK or 0 */
1252 assert(parentItem
->firstChild
== NULL
);
1253 assert(parentItem
->lastChild
== NULL
);
1257 TREEVIEW_UnlinkItem(TREEVIEW_ITEM
*item
)
1259 TREEVIEW_ITEM
*parentItem
= item
->parent
;
1261 assert(item
!= NULL
);
1262 assert(item
->parent
!= NULL
); /* i.e. it must not be the root */
1264 if (parentItem
->firstChild
== item
)
1265 parentItem
->firstChild
= item
->nextSibling
;
1267 if (parentItem
->lastChild
== item
)
1268 parentItem
->lastChild
= item
->prevSibling
;
1270 if (parentItem
->firstChild
== NULL
&& parentItem
->lastChild
== NULL
1271 && parentItem
->cChildren
> 0)
1272 parentItem
->cChildren
= 0;
1274 if (item
->prevSibling
)
1275 item
->prevSibling
->nextSibling
= item
->nextSibling
;
1277 if (item
->nextSibling
)
1278 item
->nextSibling
->prevSibling
= item
->prevSibling
;
1282 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
)
1284 TRACE("%p, (%s)\n", wineItem
, TREEVIEW_ItemName(wineItem
));
1286 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_DELETEITEMA
,
1287 TVIF_HANDLE
| TVIF_PARAM
, 0, wineItem
, 0);
1289 if (wineItem
->firstChild
)
1290 TREEVIEW_RemoveAllChildren(infoPtr
, wineItem
);
1292 TREEVIEW_UnlinkItem(wineItem
);
1294 infoPtr
->uNumItems
--;
1296 if (wineItem
->pszText
!= LPSTR_TEXTCALLBACKA
)
1297 COMCTL32_Free(wineItem
->pszText
);
1299 TREEVIEW_FreeItem(infoPtr
, wineItem
);
1303 /* Empty out the tree. */
1305 TREEVIEW_RemoveTree(TREEVIEW_INFO
*infoPtr
)
1307 TREEVIEW_RemoveAllChildren(infoPtr
, infoPtr
->root
);
1309 assert(infoPtr
->uNumItems
== 0); /* root isn't counted in uNumItems */
1313 TREEVIEW_DeleteItem(TREEVIEW_INFO
*infoPtr
, HTREEITEM wineItem
)
1315 TREEVIEW_ITEM
*oldSelection
= infoPtr
->selectedItem
;
1316 TREEVIEW_ITEM
*newSelection
= oldSelection
;
1317 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
1318 TREEVIEW_ITEM
*parent
, *prev
= NULL
;
1319 BOOL visible
= FALSE
;
1321 if (wineItem
== TVI_ROOT
)
1323 TRACE("TVI_ROOT\n");
1324 parent
= infoPtr
->root
;
1325 newSelection
= NULL
;
1327 TREEVIEW_RemoveTree(infoPtr
);
1331 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
1334 TRACE("%p (%s)\n", wineItem
, TREEVIEW_ItemName(wineItem
));
1335 parent
= wineItem
->parent
;
1337 if (ISVISIBLE(wineItem
))
1339 prev
= TREEVIEW_GetPrevListItem(infoPtr
, wineItem
);
1343 if (infoPtr
->selectedItem
!= NULL
1344 && (wineItem
== infoPtr
->selectedItem
1345 || TREEVIEW_IsChildOf(wineItem
, infoPtr
->selectedItem
)))
1347 if (wineItem
->nextSibling
)
1348 newSelection
= wineItem
->nextSibling
;
1349 else if (wineItem
->parent
!= infoPtr
->root
)
1350 newSelection
= wineItem
->parent
;
1353 if (infoPtr
->firstVisible
== wineItem
)
1355 if (wineItem
->nextSibling
)
1356 newFirstVisible
= wineItem
->nextSibling
;
1357 else if (wineItem
->prevSibling
)
1358 newFirstVisible
= wineItem
->prevSibling
;
1359 else if (wineItem
->parent
!= infoPtr
->root
)
1360 newFirstVisible
= wineItem
->parent
;
1363 newFirstVisible
= infoPtr
->firstVisible
;
1365 TREEVIEW_RemoveItem(infoPtr
, wineItem
);
1368 /* Don't change if somebody else already has. */
1369 if (oldSelection
== infoPtr
->selectedItem
)
1371 if (TREEVIEW_ValidItem(infoPtr
, newSelection
))
1372 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
, TVC_UNKNOWN
);
1374 infoPtr
->selectedItem
= 0;
1377 /* Validate insertMark dropItem.
1378 * hotItem ??? - used for comparision only.
1380 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->insertMarkItem
))
1381 infoPtr
->insertMarkItem
= 0;
1383 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->dropItem
))
1384 infoPtr
->dropItem
= 0;
1386 if (!TREEVIEW_ValidItem(infoPtr
, newFirstVisible
))
1387 newFirstVisible
= infoPtr
->root
->firstChild
;
1389 TREEVIEW_VerifyTree(infoPtr
);
1394 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1395 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
1396 TREEVIEW_UpdateScrollBars(infoPtr
);
1397 TREEVIEW_QueueRefresh(infoPtr
);
1399 else if (ISVISIBLE(parent
) && !TREEVIEW_HasChildren(infoPtr
, parent
))
1401 /* parent lost '+/-' - update it */
1402 TREEVIEW_QueueItemRefresh(infoPtr
, parent
);
1409 /* Get/Set Messages *********************************************************/
1411 TREEVIEW_SetRedraw(TREEVIEW_INFO
* infoPtr
, WPARAM wParam
, LPARAM lParam
)
1414 infoPtr
->bRedraw
= TRUE
;
1416 infoPtr
->bRedraw
= FALSE
;
1422 TREEVIEW_GetIndent(TREEVIEW_INFO
*infoPtr
)
1425 return infoPtr
->uIndent
;
1429 TREEVIEW_SetIndent(TREEVIEW_INFO
*infoPtr
, UINT newIndent
)
1433 if (newIndent
< MINIMUM_INDENT
)
1434 newIndent
= MINIMUM_INDENT
;
1436 if (infoPtr
->uIndent
!= newIndent
)
1438 infoPtr
->uIndent
= newIndent
;
1439 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1440 TREEVIEW_UpdateScrollBars(infoPtr
);
1441 TREEVIEW_QueueRefresh(infoPtr
);
1449 TREEVIEW_GetToolTips(TREEVIEW_INFO
*infoPtr
)
1452 return infoPtr
->hwndToolTip
;
1456 TREEVIEW_SetToolTips(TREEVIEW_INFO
*infoPtr
, HWND hwndTT
)
1461 prevToolTip
= infoPtr
->hwndToolTip
;
1462 infoPtr
->hwndToolTip
= hwndTT
;
1469 TREEVIEW_GetScrollTime(TREEVIEW_INFO
*infoPtr
)
1471 return infoPtr
->uScrollTime
;
1475 TREEVIEW_SetScrollTime(TREEVIEW_INFO
*infoPtr
, UINT uScrollTime
)
1477 UINT uOldScrollTime
= infoPtr
->uScrollTime
;
1479 infoPtr
->uScrollTime
= min(uScrollTime
, 100);
1481 return uOldScrollTime
;
1486 TREEVIEW_GetImageList(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
1492 case (WPARAM
)TVSIL_NORMAL
:
1493 return (LRESULT
)infoPtr
->himlNormal
;
1495 case (WPARAM
)TVSIL_STATE
:
1496 return (LRESULT
)infoPtr
->himlState
;
1504 TREEVIEW_SetImageList(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, HIMAGELIST himlNew
)
1506 HIMAGELIST himlOld
= 0;
1507 int oldWidth
= infoPtr
->normalImageWidth
;
1508 int oldHeight
= infoPtr
->normalImageHeight
;
1511 TRACE("%x,%p\n", wParam
, himlNew
);
1515 case (WPARAM
)TVSIL_NORMAL
:
1516 himlOld
= infoPtr
->himlNormal
;
1517 infoPtr
->himlNormal
= himlNew
;
1519 if (himlNew
!= NULL
)
1520 ImageList_GetIconSize(himlNew
, &infoPtr
->normalImageWidth
,
1521 &infoPtr
->normalImageHeight
);
1524 infoPtr
->normalImageWidth
= 0;
1525 infoPtr
->normalImageHeight
= 0;
1530 case (WPARAM
)TVSIL_STATE
:
1531 himlOld
= infoPtr
->himlState
;
1532 infoPtr
->himlState
= himlNew
;
1534 if (himlNew
!= NULL
)
1535 ImageList_GetIconSize(himlNew
, &infoPtr
->stateImageWidth
,
1536 &infoPtr
->stateImageHeight
);
1539 infoPtr
->stateImageWidth
= 0;
1540 infoPtr
->stateImageHeight
= 0;
1546 if (oldWidth
!= infoPtr
->normalImageWidth
||
1547 oldHeight
!= infoPtr
->normalImageHeight
)
1549 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1550 TREEVIEW_UpdateScrollBars(infoPtr
);
1553 TREEVIEW_QueueRefresh(infoPtr
);
1555 return (LRESULT
)himlOld
;
1558 /* Compute the natural height (based on the font size) for items. */
1560 TREEVIEW_NaturalHeight(TREEVIEW_INFO
*infoPtr
)
1564 HFONT hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
1566 GetTextMetricsA(hdc
, &tm
);
1568 SelectObject(hdc
, hOldFont
);
1571 /* The 16 is a hack because our fonts are tiny. */
1572 return max(16, tm
.tmHeight
+ tm
.tmExternalLeading
);
1576 TREEVIEW_SetItemHeight(TREEVIEW_INFO
*infoPtr
, INT newHeight
)
1578 INT prevHeight
= infoPtr
->uItemHeight
;
1580 TRACE("%d \n", newHeight
);
1581 if (newHeight
== -1)
1583 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1584 infoPtr
->bHeightSet
= FALSE
;
1588 infoPtr
->uItemHeight
= newHeight
;
1589 infoPtr
->bHeightSet
= TRUE
;
1592 /* Round down, unless we support odd ("non even") heights. */
1593 if (!(infoPtr
->dwStyle
) & TVS_NONEVENHEIGHT
)
1594 infoPtr
->uItemHeight
&= ~1;
1596 if (infoPtr
->uItemHeight
!= prevHeight
)
1598 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1599 TREEVIEW_UpdateScrollBars(infoPtr
);
1600 TREEVIEW_QueueRefresh(infoPtr
);
1607 TREEVIEW_GetItemHeight(TREEVIEW_INFO
*infoPtr
)
1610 return infoPtr
->uItemHeight
;
1615 TREEVIEW_GetFont(TREEVIEW_INFO
*infoPtr
)
1617 TRACE("%x\n", infoPtr
->hFont
);
1618 return infoPtr
->hFont
;
1623 TREEVIEW_ResetTextWidth(LPVOID pItem
, DWORD unused
)
1627 ((TREEVIEW_ITEM
*)pItem
)->textWidth
= 0;
1633 TREEVIEW_SetFont(TREEVIEW_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
1635 UINT uHeight
= infoPtr
->uItemHeight
;
1637 TRACE("%x %i\n", hFont
, bRedraw
);
1639 infoPtr
->hFont
= hFont
? hFont
: GetStockObject(SYSTEM_FONT
);
1641 DeleteObject(infoPtr
->hBoldFont
);
1642 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
1644 if (!infoPtr
->bHeightSet
)
1645 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1647 if (uHeight
!= infoPtr
->uItemHeight
)
1648 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1650 DPA_EnumCallback(infoPtr
->items
, TREEVIEW_ResetTextWidth
, 0);
1652 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1653 TREEVIEW_UpdateScrollBars(infoPtr
);
1656 TREEVIEW_QueueRefresh(infoPtr
);
1663 TREEVIEW_GetLineColor(TREEVIEW_INFO
*infoPtr
)
1666 return (LRESULT
)infoPtr
->clrLine
;
1670 TREEVIEW_SetLineColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1672 COLORREF prevColor
= infoPtr
->clrLine
;
1675 infoPtr
->clrLine
= color
;
1676 return (LRESULT
)prevColor
;
1681 TREEVIEW_GetTextColor(TREEVIEW_INFO
*infoPtr
)
1684 return (LRESULT
)infoPtr
->clrText
;
1688 TREEVIEW_SetTextColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1690 COLORREF prevColor
= infoPtr
->clrText
;
1693 infoPtr
->clrText
= color
;
1695 if (infoPtr
->clrText
!= prevColor
)
1696 TREEVIEW_QueueRefresh(infoPtr
);
1698 return (LRESULT
)prevColor
;
1703 TREEVIEW_GetBkColor(TREEVIEW_INFO
*infoPtr
)
1706 return (LRESULT
)infoPtr
->clrBk
;
1710 TREEVIEW_SetBkColor(TREEVIEW_INFO
*infoPtr
, COLORREF newColor
)
1712 COLORREF prevColor
= infoPtr
->clrBk
;
1715 infoPtr
->clrBk
= newColor
;
1717 if (newColor
!= prevColor
)
1718 TREEVIEW_QueueRefresh(infoPtr
);
1720 return (LRESULT
)prevColor
;
1725 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO
*infoPtr
)
1728 return (LRESULT
)infoPtr
->clrInsertMark
;
1732 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1734 COLORREF prevColor
= infoPtr
->clrInsertMark
;
1736 TRACE("%lx\n", color
);
1737 infoPtr
->clrInsertMark
= color
;
1739 return (LRESULT
)prevColor
;
1744 TREEVIEW_SetInsertMark(TREEVIEW_INFO
*infoPtr
, BOOL wParam
, HTREEITEM item
)
1746 TRACE("%d %p\n", wParam
, item
);
1748 if (!TREEVIEW_ValidItem(infoPtr
, item
))
1751 infoPtr
->insertBeforeorAfter
= wParam
;
1752 infoPtr
->insertMarkItem
= item
;
1754 TREEVIEW_QueueRefresh(infoPtr
);
1760 /************************************************************************
1761 * Some serious braindamage here. lParam is a pointer to both the
1762 * input HTREEITEM and the output RECT.
1765 TREEVIEW_GetItemRect(TREEVIEW_INFO
*infoPtr
, BOOL fTextRect
, LPRECT lpRect
)
1767 TREEVIEW_ITEM
*wineItem
;
1768 const HTREEITEM
*pItem
= (HTREEITEM
*)lpRect
;
1772 * validate parameters
1778 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
) || !ISVISIBLE(wineItem
))
1782 * If wParam is TRUE return the text size otherwise return
1783 * the whole item size
1787 /* Windows does not send TVN_GETDISPINFO here. */
1789 lpRect
->top
= wineItem
->rect
.top
;
1790 lpRect
->bottom
= wineItem
->rect
.bottom
;
1792 lpRect
->left
= wineItem
->textOffset
;
1793 lpRect
->right
= wineItem
->textOffset
+ wineItem
->textWidth
;
1797 *lpRect
= wineItem
->rect
;
1800 TRACE("%s [L:%d R:%d T:%d B:%d]\n", fTextRect
? "text" : "item",
1801 lpRect
->left
, lpRect
->right
, lpRect
->top
, lpRect
->bottom
);
1806 static inline LRESULT
1807 TREEVIEW_GetVisibleCount(TREEVIEW_INFO
*infoPtr
)
1809 /* Suprise! This does not take integral height into account. */
1810 return infoPtr
->clientHeight
/ infoPtr
->uItemHeight
;
1815 TREEVIEW_GetItemA(TREEVIEW_INFO
*infoPtr
, LPTVITEMEXA tvItem
)
1817 TREEVIEW_ITEM
*wineItem
;
1819 wineItem
= tvItem
->hItem
;
1820 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
1823 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, tvItem
->mask
);
1825 if (tvItem
->mask
& TVIF_CHILDREN
)
1826 tvItem
->cChildren
= wineItem
->cChildren
;
1828 if (tvItem
->mask
& TVIF_HANDLE
)
1829 tvItem
->hItem
= wineItem
;
1831 if (tvItem
->mask
& TVIF_IMAGE
)
1832 tvItem
->iImage
= wineItem
->iImage
;
1834 if (tvItem
->mask
& TVIF_INTEGRAL
)
1835 tvItem
->iIntegral
= wineItem
->iIntegral
;
1837 /* undocumented: windows ignores TVIF_PARAM and
1838 * * always sets lParam
1840 tvItem
->lParam
= wineItem
->lParam
;
1842 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
1843 tvItem
->iSelectedImage
= wineItem
->iSelectedImage
;
1845 if (tvItem
->mask
& TVIF_STATE
)
1846 tvItem
->state
= wineItem
->state
& tvItem
->stateMask
;
1848 if (tvItem
->mask
& TVIF_TEXT
)
1849 lstrcpynA(tvItem
->pszText
, wineItem
->pszText
, tvItem
->cchTextMax
);
1851 TRACE("item <%p>, txt %p, img %p, mask %x\n",
1852 wineItem
, tvItem
->pszText
, &tvItem
->iImage
, tvItem
->mask
);
1857 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
1858 * which is wrong. */
1860 TREEVIEW_SetItemA(TREEVIEW_INFO
*infoPtr
, LPTVITEMEXA tvItem
)
1862 TREEVIEW_ITEM
*wineItem
;
1863 TREEVIEW_ITEM originalItem
;
1865 wineItem
= tvItem
->hItem
;
1867 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr
, wineItem
),
1870 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
1873 if (!TREEVIEW_DoSetItem(infoPtr
, wineItem
, tvItem
))
1876 /* store the orignal item values */
1877 originalItem
= *wineItem
;
1879 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
1880 if ((tvItem
->mask
& TVIF_TEXT
1881 || (tvItem
->mask
& TVIF_STATE
&& tvItem
->stateMask
& TVIS_BOLD
))
1882 && ISVISIBLE(wineItem
))
1884 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, TVIF_TEXT
);
1885 TREEVIEW_ComputeTextWidth(infoPtr
, wineItem
, 0);
1888 if (tvItem
->mask
!= 0 && ISVISIBLE(wineItem
))
1890 /* The refresh updates everything, but we can't wait until then. */
1891 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, wineItem
);
1893 /* if any of the items values changed, redraw the item */
1894 if(memcmp(&originalItem
, wineItem
, sizeof(TREEVIEW_ITEM
)))
1896 if (tvItem
->mask
& TVIF_INTEGRAL
)
1898 TREEVIEW_RecalculateVisibleOrder(infoPtr
, wineItem
);
1899 TREEVIEW_UpdateScrollBars(infoPtr
);
1901 TREEVIEW_QueueRefresh(infoPtr
);
1905 TREEVIEW_UpdateScrollBars(infoPtr
);
1906 TREEVIEW_QueueItemRefresh(infoPtr
, wineItem
);
1915 TREEVIEW_GetItemW(TREEVIEW_INFO
*infoPtr
, LPTVITEMEXA tvItem
)
1917 TREEVIEW_ITEM
*wineItem
;
1919 iItem
= (INT
)tvItem
->hItem
;
1921 wineItem
= tvItem
->hItem
;
1922 if(!TREEVIEW_ValidItem (infoPtr
, wineItem
))
1925 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, tvItem
->mask
);
1927 if (tvItem
->mask
& TVIF_CHILDREN
) {
1928 if (TVIF_CHILDREN
==I_CHILDRENCALLBACK
)
1929 FIXME("I_CHILDRENCALLBACK not supported\n");
1930 tvItem
->cChildren
= wineItem
->cChildren
;
1933 if (tvItem
->mask
& TVIF_HANDLE
) {
1934 tvItem
->hItem
= wineItem
;
1936 if (tvItem
->mask
& TVIF_IMAGE
) {
1937 tvItem
->iImage
= wineItem
->iImage
;
1939 if (tvItem
->mask
& TVIF_INTEGRAL
) {
1940 tvItem
->iIntegral
= wineItem
->iIntegral
;
1942 /* undocumented: windows ignores TVIF_PARAM and
1943 * always sets lParam */
1944 tvItem
->lParam
= wineItem
->lParam
;
1945 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
) {
1946 tvItem
->iSelectedImage
= wineItem
->iSelectedImage
;
1948 if (tvItem
->mask
& TVIF_STATE
) {
1949 tvItem
->state
= wineItem
->state
& tvItem
->stateMask
;
1952 if (tvItem
->mask
& TVIF_TEXT
) {
1953 if (wineItem
->pszText
== LPSTR_TEXTCALLBACKW
) {
1954 tvItem
->pszText
= LPSTR_TEXTCALLBACKW
; /* FIXME:send notification? */
1955 ERR(" GetItem called with LPSTR_TEXTCALLBACK\n");
1957 else if (wineItem
->pszText
) {
1958 lstrcpynAtoW(tvItem
->pszText
, wineItem
->pszText
, tvItem
->cchTextMax
);
1962 wineItem
->pszText
= NULL
;
1963 TRACE("item %d<%p>, txt %p, img %p, action %x\n",
1964 iItem
, tvItem
, tvItem
->pszText
, &tvItem
->iImage
, tvItem
->mask
);
1969 TREEVIEW_GetItemState(TREEVIEW_INFO
*infoPtr
, HTREEITEM wineItem
, UINT mask
)
1973 if (!wineItem
|| !TREEVIEW_ValidItem(infoPtr
, wineItem
))
1976 return (wineItem
->state
& mask
);
1980 TREEVIEW_GetNextItem(TREEVIEW_INFO
*infoPtr
, UINT which
, HTREEITEM wineItem
)
1982 TREEVIEW_ITEM
*retval
;
1986 /* handle all the global data here */
1989 case TVGN_CHILD
: /* Special case: child of 0 is root */
1994 retval
= infoPtr
->root
->firstChild
;
1998 retval
= infoPtr
->selectedItem
;
2001 case TVGN_FIRSTVISIBLE
:
2002 retval
= infoPtr
->firstVisible
;
2005 case TVGN_DROPHILITE
:
2006 retval
= infoPtr
->dropItem
;
2009 case TVGN_LASTVISIBLE
:
2010 retval
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
2016 TRACE("flags:%x, returns %p\n", which
, retval
);
2017 return (LRESULT
)retval
;
2020 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
2026 retval
= wineItem
->nextSibling
;
2029 retval
= wineItem
->prevSibling
;
2032 retval
= (wineItem
->parent
!= infoPtr
->root
) ? wineItem
->parent
: NULL
;
2035 retval
= wineItem
->firstChild
;
2037 case TVGN_NEXTVISIBLE
:
2038 retval
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
);
2040 case TVGN_PREVIOUSVISIBLE
:
2041 retval
= TREEVIEW_GetPrevListItem(infoPtr
, wineItem
);
2044 TRACE("Unknown msg %x,item %p\n", which
, wineItem
);
2048 TRACE("flags:%x, item %p;returns %p\n", which
, wineItem
, retval
);
2049 return (LRESULT
)retval
;
2054 TREEVIEW_GetCount(TREEVIEW_INFO
*infoPtr
)
2056 TRACE(" %d\n", infoPtr
->uNumItems
);
2057 return (LRESULT
)infoPtr
->uNumItems
;
2061 TREEVIEW_ToggleItemState(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2063 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
2065 static const unsigned int state_table
[] = { 0, 2, 1 };
2069 state
= STATEIMAGEINDEX(item
->state
);
2070 TRACE("state:%x\n", state
);
2071 item
->state
&= ~TVIS_STATEIMAGEMASK
;
2074 state
= state_table
[state
];
2076 item
->state
|= INDEXTOSTATEIMAGEMASK(state
);
2078 TRACE("state:%x\n", state
);
2079 TREEVIEW_QueueItemRefresh(infoPtr
, item
);
2084 /* Painting *************************************************************/
2086 /* Draw the lines and expand button for an item. Also draws one section
2087 * of the line from item's parent to item's parent's next sibling. */
2089 TREEVIEW_DrawItemLines(TREEVIEW_INFO
*infoPtr
, HDC hdc
, TREEVIEW_ITEM
*item
)
2091 LONG centerx
, centery
;
2092 BOOL lar
= ((infoPtr
->dwStyle
2093 & (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
2096 if (!lar
&& item
->iLevel
== 0)
2099 centerx
= (item
->linesOffset
+ item
->stateOffset
) / 2;
2100 centery
= (item
->rect
.top
+ item
->rect
.bottom
) / 2;
2102 if (infoPtr
->dwStyle
& TVS_HASLINES
)
2104 HPEN hOldPen
, hNewPen
;
2108 * Get a dotted grey pen
2110 hNewPen
= CreatePen(PS_DOT
, 0, infoPtr
->clrLine
);
2111 hOldPen
= SelectObject(hdc
, hNewPen
);
2113 MoveToEx(hdc
, item
->stateOffset
, centery
, NULL
);
2114 LineTo(hdc
, centerx
- 1, centery
);
2116 if (item
->prevSibling
|| item
->parent
!= infoPtr
->root
)
2118 MoveToEx(hdc
, centerx
, item
->rect
.top
, NULL
);
2119 LineTo(hdc
, centerx
, centery
);
2122 if (item
->nextSibling
)
2124 MoveToEx(hdc
, centerx
, centery
, NULL
);
2125 LineTo(hdc
, centerx
, item
->rect
.bottom
+ 1);
2128 /* Draw the line from our parent to its next sibling. */
2129 parent
= item
->parent
;
2130 while (parent
!= infoPtr
->root
)
2132 int pcenterx
= (parent
->linesOffset
+ parent
->stateOffset
) / 2;
2134 if (parent
->nextSibling
2135 /* skip top-levels unless TVS_LINESATROOT */
2136 && parent
->stateOffset
> parent
->linesOffset
)
2138 MoveToEx(hdc
, pcenterx
, item
->rect
.top
, NULL
);
2139 LineTo(hdc
, pcenterx
, item
->rect
.bottom
+ 1);
2142 parent
= parent
->parent
;
2145 SelectObject(hdc
, hOldPen
);
2146 DeleteObject(hNewPen
);
2150 * Display the (+/-) signs
2153 if (infoPtr
->dwStyle
& TVS_HASBUTTONS
)
2155 if (item
->cChildren
)
2157 LONG height
= item
->rect
.bottom
- item
->rect
.top
;
2158 LONG width
= item
->stateOffset
- item
->linesOffset
;
2159 LONG rectsize
= min(height
, width
) / 4;
2160 /* plussize = ceil(rectsize * 3/4) */
2161 LONG plussize
= (rectsize
+ 1) * 3 / 4;
2163 HPEN hNewPen
= CreatePen(PS_SOLID
, 0, infoPtr
->clrLine
);
2164 HPEN hOldPen
= SelectObject(hdc
, hNewPen
);
2165 HBRUSH hbr
= CreateSolidBrush(infoPtr
->clrBk
);
2166 HBRUSH hbrOld
= SelectObject(hdc
, hbr
);
2168 Rectangle(hdc
, centerx
- rectsize
, centery
- rectsize
,
2169 centerx
+ rectsize
+ 1, centery
+ rectsize
+ 1);
2171 SelectObject(hdc
, hbrOld
);
2174 SelectObject(hdc
, hOldPen
);
2175 DeleteObject(hNewPen
);
2177 MoveToEx(hdc
, centerx
- plussize
+ 1, centery
, NULL
);
2178 LineTo(hdc
, centerx
+ plussize
, centery
);
2180 if (!(item
->state
& TVIS_EXPANDED
))
2182 MoveToEx(hdc
, centerx
, centery
- plussize
+ 1, NULL
);
2183 LineTo(hdc
, centerx
, centery
+ plussize
);
2190 TREEVIEW_DrawItem(TREEVIEW_INFO
*infoPtr
, HDC hdc
, TREEVIEW_ITEM
*wineItem
)
2196 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, wineItem
));
2198 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, CALLBACK_MASK_ALL
);
2200 /* The custom draw handler can query the text rectangle,
2202 TREEVIEW_ComputeTextWidth(infoPtr
, wineItem
, hdc
);
2206 if (infoPtr
->cdmode
& CDRF_NOTIFYITEMDRAW
)
2208 cditem
= TREEVIEW_SendCustomDrawItemNotify
2209 (infoPtr
, hdc
, wineItem
, CDDS_ITEMPREPAINT
);
2210 TRACE("prepaint:cditem-app returns 0x%x\n", cditem
);
2212 if (cditem
& CDRF_SKIPDEFAULT
)
2214 SelectObject(hdc
, hOldFont
);
2219 if (cditem
& CDRF_NEWFONT
)
2220 TREEVIEW_ComputeTextWidth(infoPtr
, wineItem
, hdc
);
2222 TREEVIEW_DrawItemLines(infoPtr
, hdc
, wineItem
);
2224 centery
= (wineItem
->rect
.top
+ wineItem
->rect
.bottom
) / 2;
2227 * Display the images associated with this item
2232 /* State images are displayed to the left of the Normal image
2233 * image number is in state; zero should be `display no image'.
2235 imageIndex
= STATEIMAGEINDEX(wineItem
->state
);
2237 if (infoPtr
->himlState
&& imageIndex
)
2239 ImageList_Draw(infoPtr
->himlState
, imageIndex
, hdc
,
2240 wineItem
->stateOffset
,
2241 centery
- infoPtr
->stateImageHeight
/ 2,
2245 /* Now, draw the normal image; can be either selected or
2246 * non-selected image.
2249 if ((wineItem
->state
& TVIS_SELECTED
) && (wineItem
->iSelectedImage
))
2251 /* The item is curently selected */
2252 imageIndex
= wineItem
->iSelectedImage
;
2256 /* The item is not selected */
2257 imageIndex
= wineItem
->iImage
;
2260 if (infoPtr
->himlNormal
)
2262 int ovlIdx
= wineItem
->state
& TVIS_OVERLAYMASK
;
2264 ImageList_Draw(infoPtr
->himlNormal
, imageIndex
, hdc
,
2265 wineItem
->imageOffset
,
2266 centery
- infoPtr
->normalImageHeight
/ 2,
2267 ILD_NORMAL
| ovlIdx
);
2273 * Display the text associated with this item
2276 /* Don't paint item's text if it's being edited */
2277 if (!infoPtr
->hwndEdit
|| (infoPtr
->selectedItem
!= wineItem
))
2279 if (wineItem
->pszText
)
2281 COLORREF oldTextColor
= 0;
2284 BOOL inFocus
= (GetFocus() == infoPtr
->hwnd
);
2287 oldBkMode
= SetBkMode(hdc
, TRANSPARENT
);
2289 /* - If item is drop target or it is selected and window is in focus -
2290 * use blue background (COLOR_HIGHLIGHT).
2291 * - If item is selected, window is not in focus, but it has style
2292 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2293 * - Otherwise - don't fill background
2295 if ((wineItem
->state
& TVIS_DROPHILITED
) || ((wineItem
== infoPtr
->focusedItem
) && !(wineItem
->state
& TVIS_SELECTED
)) ||
2296 ((wineItem
->state
& TVIS_SELECTED
) && (!infoPtr
->focusedItem
) &&
2297 (inFocus
|| (infoPtr
->dwStyle
& TVS_SHOWSELALWAYS
))))
2299 if ((wineItem
->state
& TVIS_DROPHILITED
) || inFocus
)
2301 hbrBk
= CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT
));
2303 SetTextColor(hdc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2307 hbrBk
= CreateSolidBrush(GetSysColor(COLOR_BTNFACE
));
2309 if (infoPtr
->clrText
== -1)
2311 SetTextColor(hdc
, GetSysColor(COLOR_WINDOWTEXT
));
2313 oldTextColor
= SetTextColor(hdc
, infoPtr
->clrText
);
2318 if (infoPtr
->clrText
== -1)
2320 SetTextColor(hdc
, GetSysColor(COLOR_WINDOWTEXT
));
2322 oldTextColor
= SetTextColor(hdc
, infoPtr
->clrText
);
2325 rcText
.top
= wineItem
->rect
.top
;
2326 rcText
.bottom
= wineItem
->rect
.bottom
;
2327 rcText
.left
= wineItem
->textOffset
;
2328 rcText
.right
= rcText
.left
+ wineItem
->textWidth
+ 4;
2332 FillRect(hdc
, &rcText
, hbrBk
);
2333 DeleteObject(hbrBk
);
2336 /* Draw the box arround the selected item */
2337 if ((wineItem
== infoPtr
->selectedItem
) && inFocus
)
2339 HPEN hNewPen
= CreatePen(PS_DOT
, 0,
2340 GetSysColor(COLOR_WINDOWTEXT
));
2341 HPEN hOldPen
= SelectObject(hdc
, hNewPen
);
2342 INT rop
= SetROP2(hdc
, R2_XORPEN
);
2345 points
[4].x
= points
[0].x
= rcText
.left
;
2346 points
[4].y
= points
[0].y
= rcText
.top
;
2347 points
[1].x
= rcText
.right
- 1;
2348 points
[1].y
= rcText
.top
;
2349 points
[2].x
= rcText
.right
- 1;
2350 points
[2].y
= rcText
.bottom
- 1;
2351 points
[3].x
= rcText
.left
;
2352 points
[3].y
= rcText
.bottom
- 1;
2354 Polyline(hdc
, points
, 5);
2357 SelectObject(hdc
, hOldPen
);
2358 DeleteObject(hNewPen
);
2367 lstrlenA(wineItem
->pszText
),
2369 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
| DT_NOPREFIX
);
2371 /* Restore the hdc state */
2372 SetTextColor(hdc
, oldTextColor
);
2374 if (oldBkMode
!= TRANSPARENT
)
2375 SetBkMode(hdc
, oldBkMode
);
2379 /* Draw insertion mark if necessary */
2381 if (infoPtr
->insertMarkItem
)
2382 TRACE("item:%d,mark:%d\n",
2383 TREEVIEW_GetItemIndex(infoPtr
, wineItem
),
2384 (int)infoPtr
->insertMarkItem
);
2386 if (wineItem
== infoPtr
->insertMarkItem
)
2388 HPEN hNewPen
, hOldPen
;
2392 hNewPen
= CreatePen(PS_SOLID
, 2, infoPtr
->clrInsertMark
);
2393 hOldPen
= SelectObject(hdc
, hNewPen
);
2395 if (infoPtr
->insertBeforeorAfter
)
2396 offset
= wineItem
->rect
.bottom
- 1;
2398 offset
= wineItem
->rect
.top
+ 1;
2400 left
= wineItem
->textOffset
- 2;
2401 right
= wineItem
->textOffset
+ wineItem
->textWidth
+ 2;
2403 MoveToEx(hdc
, left
, offset
- 3, NULL
);
2404 LineTo(hdc
, left
, offset
+ 4);
2406 MoveToEx(hdc
, left
, offset
, NULL
);
2407 LineTo(hdc
, right
+ 1, offset
);
2409 MoveToEx(hdc
, right
, offset
+ 3, NULL
);
2410 LineTo(hdc
, right
, offset
- 4);
2412 SelectObject(hdc
, hOldPen
);
2413 DeleteObject(hNewPen
);
2416 if (cditem
& CDRF_NOTIFYPOSTPAINT
)
2418 cditem
= TREEVIEW_SendCustomDrawItemNotify
2419 (infoPtr
, hdc
, wineItem
, CDDS_ITEMPOSTPAINT
);
2420 TRACE("postpaint:cditem-app returns 0x%x\n", cditem
);
2423 SelectObject(hdc
, hOldFont
);
2426 /* Computes treeHeight and treeWidth and updates the scroll bars.
2429 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
)
2431 TREEVIEW_ITEM
*wineItem
;
2432 HWND hwnd
= infoPtr
->hwnd
;
2436 LONG scrollX
= infoPtr
->scrollX
;
2438 infoPtr
->treeWidth
= 0;
2439 infoPtr
->treeHeight
= 0;
2441 /* We iterate through all visible items in order to get the tree height
2443 wineItem
= infoPtr
->root
->firstChild
;
2445 while (wineItem
!= NULL
)
2447 if (ISVISIBLE(wineItem
))
2449 /* actually we draw text at textOffset + 2 */
2450 if (2+wineItem
->textOffset
+wineItem
->textWidth
> infoPtr
->treeWidth
)
2451 infoPtr
->treeWidth
= wineItem
->textOffset
+wineItem
->textWidth
+2;
2453 /* This is scroll-adjusted, but we fix this below. */
2454 infoPtr
->treeHeight
= wineItem
->rect
.bottom
;
2457 wineItem
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
);
2460 /* Fix the scroll adjusted treeHeight and treeWidth. */
2461 if (infoPtr
->root
->firstChild
)
2462 infoPtr
->treeHeight
-= infoPtr
->root
->firstChild
->rect
.top
;
2464 infoPtr
->treeWidth
+= infoPtr
->scrollX
;
2466 /* Adding one scroll bar may take up enough space that it forces us
2467 * to add the other as well. */
2468 if (infoPtr
->treeHeight
> infoPtr
->clientHeight
)
2472 if (infoPtr
->treeWidth
2473 > infoPtr
->clientWidth
- GetSystemMetrics(SM_CXVSCROLL
))
2476 else if (infoPtr
->treeWidth
> infoPtr
->clientWidth
)
2479 if (!vert
&& horz
&& infoPtr
->treeHeight
2480 > infoPtr
->clientHeight
- GetSystemMetrics(SM_CYVSCROLL
))
2483 si
.cbSize
= sizeof(SCROLLINFO
);
2484 si
.fMask
= SIF_POS
|SIF_RANGE
|SIF_PAGE
;
2489 infoPtr
->uInternalStatus
|= TV_VSCROLL
;
2491 si
.nPage
= TREEVIEW_GetVisibleCount(infoPtr
);
2492 si
.nPos
= infoPtr
->firstVisible
->visibleOrder
;
2493 si
.nMax
= infoPtr
->maxVisibleOrder
- 1;
2495 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
2499 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2500 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2502 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2507 infoPtr
->uInternalStatus
|= TV_HSCROLL
;
2509 si
.nPage
= infoPtr
->clientWidth
;
2510 si
.nPos
= infoPtr
->scrollX
;
2511 si
.nMax
= infoPtr
->treeWidth
- 1;
2513 if (si
.nPos
> si
.nMax
- max( si
.nPage
-1, 0 ))
2515 si
.nPos
= si
.nMax
- max( si
.nPage
-1, 0 );
2519 SetScrollInfo(hwnd
, SB_HORZ
, &si
, TRUE
);
2523 if (infoPtr
->uInternalStatus
& TV_HSCROLL
)
2524 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
2529 if (infoPtr
->scrollX
!= scrollX
)
2531 TREEVIEW_HScroll(infoPtr
,
2532 MAKEWPARAM(SB_THUMBPOSITION
, scrollX
));
2536 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2539 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2541 TREEVIEW_EraseBackground(TREEVIEW_INFO
*infoPtr
, HDC hDC
)
2543 HBRUSH hBrush
= CreateSolidBrush(infoPtr
->clrBk
);
2546 GetClientRect(infoPtr
->hwnd
, &rect
);
2547 FillRect(hDC
, &rect
, hBrush
);
2548 DeleteObject(hBrush
);
2554 TREEVIEW_Refresh(TREEVIEW_INFO
*infoPtr
, HDC hdc
, RECT
*rc
)
2556 HWND hwnd
= infoPtr
->hwnd
;
2558 TREEVIEW_ITEM
*wineItem
;
2560 if (infoPtr
->clientHeight
== 0 || infoPtr
->clientWidth
== 0)
2562 TRACE("empty window\n");
2566 infoPtr
->cdmode
= TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_PREPAINT
,
2569 if (infoPtr
->cdmode
== CDRF_SKIPDEFAULT
)
2571 ReleaseDC(hwnd
, hdc
);
2575 for (wineItem
= infoPtr
->root
->firstChild
;
2577 wineItem
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
))
2579 if (ISVISIBLE(wineItem
))
2581 /* Avoid unneeded calculations */
2582 if (wineItem
->rect
.top
> rect
.bottom
)
2584 if (wineItem
->rect
.bottom
< rect
.top
)
2587 TREEVIEW_DrawItem(infoPtr
, hdc
, wineItem
);
2591 TREEVIEW_UpdateScrollBars(infoPtr
);
2593 if (infoPtr
->cdmode
& CDRF_NOTIFYPOSTPAINT
)
2595 TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_POSTPAINT
, hdc
, rect
);
2599 TREEVIEW_QueueRefresh(TREEVIEW_INFO
*infoPtr
)
2601 InvalidateRect(infoPtr
->hwnd
, NULL
, TRUE
);
2604 /* It be that item->rect is out of date. If so, we invalidate the wrong area,
2605 * but then whoever updates item->rect knows that they must invalidate after
2608 TREEVIEW_QueueItemRefresh(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2611 InvalidateRect(infoPtr
->hwnd
, &item
->rect
, TRUE
);
2615 TREEVIEW_Paint(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
2626 GetUpdateRect(infoPtr
->hwnd
, &rc
, TRUE
);
2630 hdc
= BeginPaint(infoPtr
->hwnd
, &ps
);
2634 if(infoPtr
->bRedraw
) /* WM_SETREDRAW sets bRedraw */
2635 TREEVIEW_Refresh(infoPtr
, hdc
, &rc
);
2638 EndPaint(infoPtr
->hwnd
, &ps
);
2644 /* Sorting **************************************************************/
2646 /***************************************************************************
2647 * Forward the DPA local callback to the treeview owner callback
2650 TREEVIEW_CallBackCompare(TREEVIEW_ITEM
*first
, TREEVIEW_ITEM
*second
, LPTVSORTCB pCallBackSort
)
2652 /* Forward the call to the client-defined callback */
2653 return pCallBackSort
->lpfnCompare(first
->lParam
,
2655 pCallBackSort
->lParam
);
2658 /***************************************************************************
2659 * Treeview native sort routine: sort on item text.
2662 TREEVIEW_SortOnName(TREEVIEW_ITEM
*first
, TREEVIEW_ITEM
*second
,
2663 TREEVIEW_INFO
*infoPtr
)
2665 TREEVIEW_UpdateDispInfo(infoPtr
, first
, TVIF_TEXT
);
2666 TREEVIEW_UpdateDispInfo(infoPtr
, second
, TVIF_TEXT
);
2668 return strcasecmp(first
->pszText
, second
->pszText
);
2671 /* Returns the number of physical children belonging to item. */
2673 TREEVIEW_CountChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2678 for (hti
= item
->firstChild
; hti
!= NULL
; hti
= hti
->nextSibling
)
2684 /* Returns a DPA containing a pointer to each physical child of item in
2685 * sibling order. If item has no children, an empty DPA is returned. */
2687 TREEVIEW_BuildChildDPA(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2689 HTREEITEM child
= item
->firstChild
;
2691 HDPA list
= DPA_Create(8);
2692 if (list
== 0) return NULL
;
2694 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
2696 if (DPA_InsertPtr(list
, INT_MAX
, child
) == -1)
2706 /***************************************************************************
2707 * Setup the treeview structure with regards of the sort method
2708 * and sort the children of the TV item specified in lParam
2709 * fRecurse: currently unused. Should be zero.
2710 * parent: if pSort!=NULL, should equal pSort->hParent.
2711 * otherwise, item which child items are to be sorted.
2712 * pSort: sort method info. if NULL, sort on item text.
2713 * if non-NULL, sort on item's lParam content, and let the
2714 * application decide what that means. See also TVM_SORTCHILDRENCB.
2718 TREEVIEW_Sort(TREEVIEW_INFO
*infoPtr
, BOOL fRecurse
, HTREEITEM parent
,
2722 PFNDPACOMPARE pfnCompare
;
2725 /* undocumented feature: TVI_ROOT means `sort the whole tree' */
2726 if (parent
== TVI_ROOT
)
2727 parent
= infoPtr
->root
;
2729 /* Check for a valid handle to the parent item */
2730 if (!TREEVIEW_ValidItem(infoPtr
, parent
))
2732 ERR("invalid item hParent=%x\n", (INT
)parent
);
2738 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_CallBackCompare
;
2739 lpCompare
= (LPARAM
)pSort
;
2743 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_SortOnName
;
2744 lpCompare
= (LPARAM
)infoPtr
;
2747 cChildren
= TREEVIEW_CountChildren(infoPtr
, parent
);
2749 /* Make sure there is something to sort */
2752 /* TREEVIEW_ITEM rechaining */
2755 HTREEITEM nextItem
= 0;
2756 HTREEITEM prevItem
= 0;
2758 HDPA sortList
= TREEVIEW_BuildChildDPA(infoPtr
, parent
);
2760 if (sortList
== NULL
)
2763 /* let DPA sort the list */
2764 DPA_Sort(sortList
, pfnCompare
, lpCompare
);
2766 /* The order of DPA entries has been changed, so fixup the
2767 * nextSibling and prevSibling pointers. */
2769 item
= (HTREEITEM
)DPA_GetPtr(sortList
, count
++);
2770 while ((nextItem
= (HTREEITEM
)DPA_GetPtr(sortList
, count
++)) != NULL
)
2772 /* link the two current item toghether */
2773 item
->nextSibling
= nextItem
;
2774 nextItem
->prevSibling
= item
;
2776 if (prevItem
== NULL
)
2778 /* this is the first item, update the parent */
2779 parent
->firstChild
= item
;
2780 item
->prevSibling
= NULL
;
2784 /* fix the back chaining */
2785 item
->prevSibling
= prevItem
;
2788 /* get ready for the next one */
2793 /* the last item is pointed to by item and never has a sibling */
2794 item
->nextSibling
= NULL
;
2795 parent
->lastChild
= item
;
2797 DPA_Destroy(sortList
);
2799 TREEVIEW_VerifyTree(infoPtr
);
2801 if (parent
->state
& TVIS_EXPANDED
)
2803 int visOrder
= infoPtr
->firstVisible
->visibleOrder
;
2805 TREEVIEW_RecalculateVisibleOrder(infoPtr
, parent
);
2807 if (TREEVIEW_IsChildOf(parent
, infoPtr
->firstVisible
))
2809 TREEVIEW_ITEM
*item
;
2811 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
2812 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
2814 if (item
->visibleOrder
== visOrder
)
2818 TREEVIEW_SetFirstVisible(infoPtr
, item
, FALSE
);
2821 TREEVIEW_QueueRefresh(infoPtr
);
2830 /***************************************************************************
2831 * Setup the treeview structure with regards of the sort method
2832 * and sort the children of the TV item specified in lParam
2835 TREEVIEW_SortChildrenCB(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPTVSORTCB pSort
)
2837 return TREEVIEW_Sort(infoPtr
, wParam
, pSort
->hParent
, pSort
);
2841 /***************************************************************************
2842 * Sort the children of the TV item specified in lParam.
2845 TREEVIEW_SortChildren(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
2847 return TREEVIEW_Sort(infoPtr
, (BOOL
)wParam
, (HTREEITEM
)lParam
, NULL
);
2851 /* Expansion/Collapse ***************************************************/
2854 TREEVIEW_SendExpanding(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
2857 return !TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_ITEMEXPANDINGA
, action
,
2858 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
2859 | TVIF_IMAGE
| TVIF_SELECTEDIMAGE
,
2864 TREEVIEW_SendExpanded(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
2867 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_ITEMEXPANDEDA
, action
,
2868 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
2869 | TVIF_IMAGE
| TVIF_SELECTEDIMAGE
,
2874 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
2875 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
2877 TREEVIEW_Collapse(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
2878 BOOL bRemoveChildren
, BOOL bUser
)
2880 UINT action
= TVE_COLLAPSE
| (bRemoveChildren
? TVE_COLLAPSERESET
: 0);
2881 BOOL bSetSelection
, bSetFirstVisible
;
2883 TRACE("TVE_COLLAPSE %p %s\n", wineItem
, TREEVIEW_ItemName(wineItem
));
2885 if (!(wineItem
->state
& TVIS_EXPANDED
) || wineItem
->firstChild
== NULL
)
2889 TREEVIEW_SendExpanding(infoPtr
, wineItem
, action
);
2891 wineItem
->state
&= ~TVIS_EXPANDED
;
2894 TREEVIEW_SendExpanded(infoPtr
, wineItem
, action
);
2896 bSetSelection
= (infoPtr
->selectedItem
!= NULL
2897 && TREEVIEW_IsChildOf(wineItem
, infoPtr
->selectedItem
));
2899 bSetFirstVisible
= (infoPtr
->firstVisible
!= NULL
2900 && TREEVIEW_IsChildOf(wineItem
, infoPtr
->firstVisible
));
2902 if (bRemoveChildren
)
2904 TRACE("TVE_COLLAPSERESET\n");
2905 wineItem
->state
&= ~TVIS_EXPANDEDONCE
;
2906 TREEVIEW_RemoveAllChildren(infoPtr
, wineItem
);
2909 if (wineItem
->firstChild
)
2911 TREEVIEW_ITEM
*item
, *sibling
;
2913 sibling
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
);
2915 for (item
= wineItem
->firstChild
; item
!= sibling
;
2916 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
2918 item
->visibleOrder
= -1;
2922 TREEVIEW_RecalculateVisibleOrder(infoPtr
, wineItem
);
2924 TREEVIEW_SetFirstVisible(infoPtr
, bSetFirstVisible
? wineItem
2925 : infoPtr
->firstVisible
, TRUE
);
2929 /* Don't call DoSelectItem, it sends notifications. */
2930 if (TREEVIEW_ValidItem(infoPtr
, infoPtr
->selectedItem
))
2931 infoPtr
->selectedItem
->state
&= ~TVIS_SELECTED
;
2932 wineItem
->state
|= TVIS_SELECTED
;
2933 infoPtr
->selectedItem
= wineItem
;
2935 TREEVIEW_EnsureVisible(infoPtr
, wineItem
, FALSE
);
2938 TREEVIEW_UpdateScrollBars(infoPtr
);
2939 TREEVIEW_QueueRefresh(infoPtr
);
2945 TREEVIEW_Expand(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
2946 BOOL bExpandPartial
, BOOL bUser
)
2950 if (!TREEVIEW_HasChildren(infoPtr
, wineItem
)
2951 || wineItem
->state
& TVIS_EXPANDED
)
2954 TRACE("TVE_EXPAND %p %s\n", wineItem
, TREEVIEW_ItemName(wineItem
));
2956 if (bUser
|| !(wineItem
->state
& TVIS_EXPANDEDONCE
))
2958 if (!TREEVIEW_SendExpanding(infoPtr
, wineItem
, TVE_EXPAND
))
2960 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
2964 wineItem
->state
|= TVIS_EXPANDED
;
2965 TREEVIEW_SendExpanded(infoPtr
, wineItem
, TVE_EXPAND
);
2966 wineItem
->state
|= TVIS_EXPANDEDONCE
;
2970 /* this item has already been expanded */
2971 wineItem
->state
|= TVIS_EXPANDED
;
2975 FIXME("TVE_EXPANDPARTIAL not implemented\n");
2977 TREEVIEW_RecalculateVisibleOrder(infoPtr
, wineItem
);
2978 TREEVIEW_UpdateSubTree(infoPtr
, wineItem
);
2979 TREEVIEW_UpdateScrollBars(infoPtr
);
2981 /* Scroll up so that as many children as possible are visible.
2982 * This looses when expanding causes an HScroll bar to appear, but we
2983 * don't know that yet, so the last item is obscured. */
2984 if (wineItem
->firstChild
!= NULL
)
2986 int nChildren
= wineItem
->lastChild
->visibleOrder
2987 - wineItem
->firstChild
->visibleOrder
+ 1;
2989 int visible_pos
= wineItem
->visibleOrder
2990 - infoPtr
->firstVisible
->visibleOrder
;
2992 int rows_below
= TREEVIEW_GetVisibleCount(infoPtr
) - visible_pos
- 1;
2994 if (visible_pos
> 0 && nChildren
> rows_below
)
2996 int scroll
= nChildren
- rows_below
;
2998 if (scroll
> visible_pos
)
2999 scroll
= visible_pos
;
3003 TREEVIEW_ITEM
*newFirstVisible
3004 = TREEVIEW_GetListItem(infoPtr
, infoPtr
->firstVisible
,
3008 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
3013 TREEVIEW_QueueRefresh(infoPtr
);
3019 TREEVIEW_Toggle(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
, BOOL bUser
)
3023 if (wineItem
->state
& TVIS_EXPANDED
)
3024 return TREEVIEW_Collapse(infoPtr
, wineItem
, FALSE
, bUser
);
3026 return TREEVIEW_Expand(infoPtr
, wineItem
, FALSE
, bUser
);
3030 TREEVIEW_ExpandAll(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
3032 TREEVIEW_Expand(infoPtr
, item
, FALSE
, TRUE
);
3034 for (item
= item
->firstChild
; item
!= NULL
; item
= item
->nextSibling
)
3036 if (TREEVIEW_HasChildren(infoPtr
, item
))
3037 TREEVIEW_ExpandAll(infoPtr
, item
);
3041 /* Note:If the specified item is the child of a collapsed parent item,
3042 the parent's list of child items is (recursively) expanded to reveal the
3043 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3044 know if it also applies here.
3048 TREEVIEW_ExpandMsg(TREEVIEW_INFO
*infoPtr
, UINT flag
, HTREEITEM wineItem
)
3050 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
3053 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3054 TREEVIEW_ItemName(wineItem
), flag
,
3055 TREEVIEW_GetItemIndex(infoPtr
, wineItem
), wineItem
->state
);
3057 switch (flag
& TVE_TOGGLE
)
3060 return TREEVIEW_Collapse(infoPtr
, wineItem
, flag
& TVE_COLLAPSERESET
,
3064 return TREEVIEW_Expand(infoPtr
, wineItem
, flag
& TVE_EXPANDPARTIAL
,
3068 return TREEVIEW_Toggle(infoPtr
, wineItem
, TRUE
);
3075 TRACE("Exiting, Item %p state is now %d...\n", wineItem
, wineItem
->state
);
3079 /* Hit-Testing **********************************************************/
3081 static TREEVIEW_ITEM
*
3082 TREEVIEW_HitTestPoint(TREEVIEW_INFO
*infoPtr
, POINT pt
)
3084 TREEVIEW_ITEM
*wineItem
;
3087 if (!infoPtr
->firstVisible
)
3090 row
= pt
.y
/ infoPtr
->uItemHeight
+ infoPtr
->firstVisible
->visibleOrder
;
3092 for (wineItem
= infoPtr
->firstVisible
; wineItem
!= NULL
;
3093 wineItem
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
))
3095 if (row
>= wineItem
->visibleOrder
3096 && row
< wineItem
->visibleOrder
+ wineItem
->iIntegral
)
3104 TREEVIEW_HitTest(TREEVIEW_INFO
*infoPtr
, LPTVHITTESTINFO lpht
)
3106 TREEVIEW_ITEM
*wineItem
;
3112 GetClientRect(infoPtr
->hwnd
, &rect
);
3119 status
|= TVHT_TOLEFT
;
3121 else if (x
> rect
.right
)
3123 status
|= TVHT_TORIGHT
;
3128 status
|= TVHT_ABOVE
;
3130 else if (y
> rect
.bottom
)
3132 status
|= TVHT_BELOW
;
3137 lpht
->flags
= status
;
3138 return (LRESULT
)(HTREEITEM
)NULL
;
3141 wineItem
= TREEVIEW_HitTestPoint(infoPtr
, lpht
->pt
);
3144 lpht
->flags
= TVHT_NOWHERE
;
3145 return (LRESULT
)(HTREEITEM
)NULL
;
3148 if (x
>= wineItem
->textOffset
+ wineItem
->textWidth
)
3150 lpht
->flags
= TVHT_ONITEMRIGHT
;
3152 else if (x
>= wineItem
->textOffset
)
3154 lpht
->flags
= TVHT_ONITEMLABEL
;
3156 else if (x
>= wineItem
->imageOffset
)
3158 lpht
->flags
= TVHT_ONITEMICON
;
3160 else if (x
>= wineItem
->stateOffset
)
3162 lpht
->flags
= TVHT_ONITEMSTATEICON
;
3164 else if (x
>= wineItem
->linesOffset
&& infoPtr
->dwStyle
& TVS_HASBUTTONS
)
3166 lpht
->flags
= TVHT_ONITEMBUTTON
;
3170 lpht
->flags
= TVHT_ONITEMINDENT
;
3173 lpht
->hItem
= wineItem
;
3174 TRACE("(%ld,%ld):result %x\n", lpht
->pt
.x
, lpht
->pt
.y
, lpht
->flags
);
3176 return (LRESULT
)wineItem
;
3179 /* Item Label Editing ***************************************************/
3182 TREEVIEW_GetEditControl(TREEVIEW_INFO
*infoPtr
)
3184 return infoPtr
->hwndEdit
;
3187 static LRESULT CALLBACK
3188 TREEVIEW_Edit_SubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
3190 TREEVIEW_INFO
*infoPtr
;
3191 BOOL bCancel
= FALSE
;
3198 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3200 TRACE("WM_PAINT start\n");
3201 rc
= CallWindowProcA(infoPtr
->wpEditOrig
, hwnd
, uMsg
, wParam
,
3203 TRACE("WM_PAINT done\n");
3209 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3210 if (infoPtr
->bIgnoreEditKillFocus
)
3217 return DLGC_WANTARROWS
| DLGC_WANTALLKEYS
;
3220 if (wParam
== (WPARAM
)VK_ESCAPE
)
3225 else if (wParam
== (WPARAM
)VK_RETURN
)
3233 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3235 return CallWindowProcA(infoPtr
->wpEditOrig
, hwnd
, uMsg
, wParam
,
3240 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3241 /* eg. Using a messagebox */
3243 infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3244 infoPtr
->bIgnoreEditKillFocus
= TRUE
;
3245 TREEVIEW_EndEditLabelNow(infoPtr
, bCancel
|| !infoPtr
->bLabelChanged
);
3246 infoPtr
->bIgnoreEditKillFocus
= FALSE
;
3252 /* should handle edit control messages here */
3255 TREEVIEW_Command(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
3257 TRACE("%x %ld\n", wParam
, lParam
);
3259 switch (HIWORD(wParam
))
3264 * Adjust the edit window size
3267 TREEVIEW_ITEM
*editItem
= infoPtr
->selectedItem
;
3268 HDC hdc
= GetDC(infoPtr
->hwndEdit
);
3271 HFONT hFont
, hOldFont
= 0;
3273 infoPtr
->bLabelChanged
= TRUE
;
3275 len
= GetWindowTextA(infoPtr
->hwndEdit
, buffer
, sizeof(buffer
));
3277 /* Select font to get the right dimension of the string */
3278 hFont
= SendMessageA(infoPtr
->hwndEdit
, WM_GETFONT
, 0, 0);
3281 hOldFont
= SelectObject(hdc
, hFont
);
3284 if (GetTextExtentPoint32A(hdc
, buffer
, strlen(buffer
), &sz
))
3286 TEXTMETRICA textMetric
;
3288 /* Add Extra spacing for the next character */
3289 GetTextMetricsA(hdc
, &textMetric
);
3290 sz
.cx
+= (textMetric
.tmMaxCharWidth
* 2);
3292 sz
.cx
= max(sz
.cx
, textMetric
.tmMaxCharWidth
* 3);
3294 infoPtr
->clientWidth
- editItem
->textOffset
+ 2);
3296 SetWindowPos(infoPtr
->hwndEdit
,
3301 editItem
->rect
.bottom
- editItem
->rect
.top
+ 3,
3302 SWP_NOMOVE
| SWP_DRAWFRAME
);
3307 SelectObject(hdc
, hOldFont
);
3310 ReleaseDC(infoPtr
->hwnd
, hdc
);
3315 return SendMessageA(GetParent(infoPtr
->hwnd
), WM_COMMAND
, wParam
, lParam
);
3322 TREEVIEW_EditLabelA(TREEVIEW_INFO
*infoPtr
, HTREEITEM hItem
)
3324 HWND hwnd
= infoPtr
->hwnd
;
3327 TREEVIEW_ITEM
*editItem
= hItem
;
3328 HINSTANCE hinst
= GetWindowLongA(hwnd
, GWL_HINSTANCE
);
3331 TEXTMETRICA textMetric
;
3333 TRACE("%x %p\n", (unsigned)hwnd
, hItem
);
3334 if (!TREEVIEW_ValidItem(infoPtr
, editItem
))
3337 if (infoPtr
->hwndEdit
)
3338 return infoPtr
->hwndEdit
;
3340 infoPtr
->bLabelChanged
= FALSE
;
3342 /* Make sure that edit item is selected */
3343 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, hItem
, TVC_UNKNOWN
);
3344 TREEVIEW_EnsureVisible(infoPtr
, hItem
, TRUE
);
3346 TREEVIEW_UpdateDispInfo(infoPtr
, editItem
, TVIF_TEXT
);
3349 /* Select the font to get appropriate metric dimensions */
3350 if (infoPtr
->hFont
!= 0)
3352 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
3355 /*Get String Lenght in pixels */
3356 GetTextExtentPoint32A(hdc
, editItem
->pszText
, strlen(editItem
->pszText
),
3359 /*Add Extra spacing for the next character */
3360 GetTextMetricsA(hdc
, &textMetric
);
3361 sz
.cx
+= (textMetric
.tmMaxCharWidth
* 2);
3363 sz
.cx
= max(sz
.cx
, textMetric
.tmMaxCharWidth
* 3);
3364 sz
.cx
= min(sz
.cx
, infoPtr
->clientWidth
- editItem
->textOffset
+ 2);
3366 if (infoPtr
->hFont
!= 0)
3368 SelectObject(hdc
, hOldFont
);
3371 ReleaseDC(hwnd
, hdc
);
3372 hwndEdit
= CreateWindowExA(WS_EX_LEFT
,
3375 WS_CHILD
| WS_BORDER
| ES_AUTOHSCROLL
|
3376 WS_CLIPSIBLINGS
| ES_WANTRETURN
|
3377 ES_LEFT
, editItem
->textOffset
- 2,
3378 editItem
->rect
.top
- 1, sz
.cx
+ 3,
3379 editItem
->rect
.bottom
-
3380 editItem
->rect
.top
+ 3, hwnd
, 0, hinst
, 0);
3381 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3383 infoPtr
->hwndEdit
= hwndEdit
;
3385 /* Get a 2D border. */
3386 SetWindowLongA(hwndEdit
, GWL_EXSTYLE
,
3387 GetWindowLongA(hwndEdit
, GWL_EXSTYLE
) & ~WS_EX_CLIENTEDGE
);
3388 SetWindowLongA(hwndEdit
, GWL_STYLE
,
3389 GetWindowLongA(hwndEdit
, GWL_STYLE
) | WS_BORDER
);
3391 SendMessageA(hwndEdit
, WM_SETFONT
, TREEVIEW_FontForItem(infoPtr
, editItem
),
3394 infoPtr
->wpEditOrig
= (WNDPROC
)SetWindowLongA(hwndEdit
, GWL_WNDPROC
,
3396 TREEVIEW_Edit_SubclassProc
);
3398 if (TREEVIEW_BeginLabelEditNotify(infoPtr
, editItem
))
3400 DestroyWindow(hwndEdit
);
3401 infoPtr
->hwndEdit
= 0;
3405 infoPtr
->selectedItem
= hItem
;
3406 SetWindowTextA(hwndEdit
, editItem
->pszText
);
3408 SendMessageA(hwndEdit
, EM_SETSEL
, 0, -1);
3409 ShowWindow(hwndEdit
, SW_SHOW
);
3416 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
)
3418 HWND hwnd
= infoPtr
->hwnd
;
3419 TREEVIEW_ITEM
*editedItem
= infoPtr
->selectedItem
;
3422 char tmpText
[1024] = { '\0' };
3425 if (!infoPtr
->hwndEdit
)
3428 tvdi
.hdr
.hwndFrom
= hwnd
;
3429 tvdi
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
3430 tvdi
.hdr
.code
= TVN_ENDLABELEDITA
;
3432 tvdi
.item
.hItem
= editedItem
;
3433 tvdi
.item
.state
= editedItem
->state
;
3434 tvdi
.item
.lParam
= editedItem
->lParam
;
3438 iLength
= GetWindowTextA(infoPtr
->hwndEdit
, tmpText
, 1023);
3440 if (iLength
>= 1023)
3442 ERR("Insuficient space to retrieve new item label.");
3445 tvdi
.item
.pszText
= tmpText
;
3446 tvdi
.item
.cchTextMax
= iLength
+ 1;
3450 tvdi
.item
.pszText
= NULL
;
3451 tvdi
.item
.cchTextMax
= 0;
3454 bCommit
= (BOOL
)SendMessageA(GetParent(hwnd
),
3456 (WPARAM
)tvdi
.hdr
.idFrom
, (LPARAM
)&tvdi
);
3458 if (!bCancel
&& bCommit
) /* Apply the changes */
3460 if (strcmp(tmpText
, editedItem
->pszText
) != 0)
3462 if (NULL
== COMCTL32_ReAlloc(editedItem
->pszText
, iLength
+ 1))
3464 ERR("OutOfMemory, cannot allocate space for label");
3465 DestroyWindow(infoPtr
->hwndEdit
);
3466 infoPtr
->hwndEdit
= 0;
3471 editedItem
->cchTextMax
= iLength
+ 1;
3472 lstrcpyA(editedItem
->pszText
, tmpText
);
3477 ShowWindow(infoPtr
->hwndEdit
, SW_HIDE
);
3478 DestroyWindow(infoPtr
->hwndEdit
);
3479 infoPtr
->hwndEdit
= 0;
3484 TREEVIEW_HandleTimer(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
3486 if (wParam
!= TV_EDIT_TIMER
)
3488 ERR("got unknown timer\n");
3492 KillTimer(infoPtr
->hwnd
, TV_EDIT_TIMER
);
3493 infoPtr
->Timer
&= ~TV_EDIT_TIMER_SET
;
3495 TREEVIEW_EditLabelA(infoPtr
, infoPtr
->selectedItem
);
3501 /* Mouse Tracking/Drag **************************************************/
3503 /***************************************************************************
3504 * This is quite unusual piece of code, but that's how it's implemented in
3508 TREEVIEW_TrackMouse(TREEVIEW_INFO
*infoPtr
, POINT pt
)
3510 INT cxDrag
= GetSystemMetrics(SM_CXDRAG
);
3511 INT cyDrag
= GetSystemMetrics(SM_CYDRAG
);
3515 r
.top
= pt
.y
- cyDrag
;
3516 r
.left
= pt
.x
- cxDrag
;
3517 r
.bottom
= pt
.y
+ cyDrag
;
3518 r
.right
= pt
.x
+ cxDrag
;
3520 SetCapture(infoPtr
->hwnd
);
3524 if (PeekMessageA(&msg
, 0, 0, 0, PM_REMOVE
| PM_NOYIELD
))
3526 if (msg
.message
== WM_MOUSEMOVE
)
3528 pt
.x
= (LONG
)(INT16
)LOWORD(msg
.lParam
);
3529 pt
.y
= (LONG
)(INT16
)HIWORD(msg
.lParam
);
3530 if (PtInRect(&r
, pt
))
3538 else if (msg
.message
>= WM_LBUTTONDOWN
&&
3539 msg
.message
<= WM_RBUTTONDBLCLK
)
3541 if (msg
.message
== WM_RBUTTONUP
)
3542 TREEVIEW_RButtonUp(infoPtr
, &pt
);
3546 DispatchMessageA(&msg
);
3549 if (GetCapture() != infoPtr
->hwnd
)
3559 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
3561 TREEVIEW_ITEM
*wineItem
;
3565 SetFocus(infoPtr
->hwnd
);
3567 if (infoPtr
->Timer
& TV_EDIT_TIMER_SET
)
3569 /* If there is pending 'edit label' event - kill it now */
3570 KillTimer(infoPtr
->hwnd
, TV_EDIT_TIMER
);
3573 hit
.pt
.x
= (LONG
)(INT16
)LOWORD(lParam
);
3574 hit
.pt
.y
= (LONG
)(INT16
)HIWORD(lParam
);
3576 wineItem
= (TREEVIEW_ITEM
*)TREEVIEW_HitTest(infoPtr
, &hit
);
3579 TRACE("item %d \n", TREEVIEW_GetItemIndex(infoPtr
, wineItem
));
3581 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_DBLCLK
) == FALSE
)
3585 case TVHT_ONITEMRIGHT
:
3586 /* FIXME: we should not have send NM_DBLCLK in this case. */
3589 case TVHT_ONITEMINDENT
:
3590 if (!(infoPtr
->dwStyle
& TVS_HASLINES
))
3596 int level
= hit
.pt
.x
/ infoPtr
->uIndent
;
3597 if (!(infoPtr
->dwStyle
& TVS_LINESATROOT
)) level
++;
3599 while (wineItem
->iLevel
> level
)
3601 wineItem
= wineItem
->parent
;
3607 case TVHT_ONITEMLABEL
:
3608 case TVHT_ONITEMICON
:
3609 case TVHT_ONITEMBUTTON
:
3610 TREEVIEW_Toggle(infoPtr
, wineItem
, TRUE
);
3613 case TVHT_ONITEMSTATEICON
:
3614 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
3615 TREEVIEW_ToggleItemState(infoPtr
, wineItem
);
3617 TREEVIEW_Toggle(infoPtr
, wineItem
, TRUE
);
3626 TREEVIEW_LButtonDown(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
3628 HWND hwnd
= infoPtr
->hwnd
;
3633 /* If Edit control is active - kill it and return.
3634 * The best way to do it is to set focus to itself.
3635 * Edit control subclassed procedure will automatically call
3638 if (infoPtr
->hwndEdit
)
3644 ht
.pt
.x
= (LONG
)(INT16
)LOWORD(lParam
);
3645 ht
.pt
.y
= (LONG
)(INT16
)HIWORD(lParam
);
3647 TREEVIEW_HitTest(infoPtr
, &ht
);
3648 TRACE("item %d \n", TREEVIEW_GetItemIndex(infoPtr
, ht
.hItem
));
3650 /* update focusedItem and redraw both items */
3651 if(ht
.hItem
&& (ht
.flags
& TVHT_ONITEM
))
3653 infoPtr
->focusedItem
= ht
.hItem
;
3654 InvalidateRect(hwnd
, &(((HTREEITEM
)(ht
.hItem
))->rect
), TRUE
);
3656 if(infoPtr
->selectedItem
)
3657 InvalidateRect(hwnd
, &(infoPtr
->selectedItem
->rect
), TRUE
);
3660 bTrack
= (ht
.flags
& TVHT_ONITEM
)
3661 && !(infoPtr
->dwStyle
& TVS_DISABLEDRAGDROP
);
3663 /* Send NM_CLICK right away */
3665 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_CLICK
))
3668 if (ht
.flags
& TVHT_ONITEMBUTTON
)
3670 TREEVIEW_Toggle(infoPtr
, ht
.hItem
, TRUE
);
3674 { /* if TREEVIEW_TrackMouse == 1 dragging occured and the cursor left the dragged item's rectangle */
3675 if (TREEVIEW_TrackMouse(infoPtr
, ht
.pt
))
3677 TREEVIEW_SendTreeviewDnDNotify(infoPtr
, TVN_BEGINDRAGA
, ht
.hItem
,
3679 infoPtr
->dropItem
= ht
.hItem
;
3681 /* clean up focuedItem as we dragged and won't select this item */
3682 if(infoPtr
->focusedItem
)
3684 /* refresh the item that was focused */
3685 tempItem
= infoPtr
->focusedItem
;
3686 infoPtr
->focusedItem
= 0;
3687 InvalidateRect(infoPtr
->hwnd
, &tempItem
->rect
, TRUE
);
3689 /* refresh the selected item to return the filled background */
3690 InvalidateRect(infoPtr
->hwnd
, &(infoPtr
->selectedItem
->rect
), TRUE
);
3697 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_CLICK
))
3701 * If the style allow editing and the node is already selected
3702 * and the click occured on the item label...
3704 if ((infoPtr
->dwStyle
& TVS_EDITLABELS
) &&
3705 (ht
.flags
& TVHT_ONITEMLABEL
) && (infoPtr
->selectedItem
== ht
.hItem
))
3707 if (infoPtr
->Timer
& TV_EDIT_TIMER_SET
)
3708 KillTimer(hwnd
, TV_EDIT_TIMER
);
3710 SetTimer(hwnd
, TV_EDIT_TIMER
, GetDoubleClickTime(), 0);
3711 infoPtr
->Timer
|= TV_EDIT_TIMER_SET
;
3713 else if (ht
.flags
& TVHT_ONITEM
) /* select the item if the hit was inside of the icon or text */
3716 * if we are TVS_SINGLEEXPAND then we want this single click to
3717 * do a bunch of things.
3719 if((infoPtr
->dwStyle
& TVS_SINGLEEXPAND
) &&
3720 (infoPtr
->hwndEdit
== 0))
3722 TREEVIEW_ITEM
*SelItem
;
3725 * Send the notification
3727 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_SINGLEEXPAND
, TVIF_HANDLE
| TVIF_PARAM
,
3731 * Close the previous selection all the way to the root
3732 * as long as the new selection is not a child
3734 if((infoPtr
->selectedItem
)
3735 && (infoPtr
->selectedItem
!= ht
.hItem
))
3737 BOOL closeit
= TRUE
;
3740 /* determine of the hitItem is a child of the currently selected item */
3741 while(closeit
&& SelItem
&& TREEVIEW_ValidItem(infoPtr
, SelItem
) && (SelItem
!= infoPtr
->root
))
3743 closeit
= (SelItem
!= infoPtr
->selectedItem
);
3744 SelItem
= SelItem
->parent
;
3749 if(TREEVIEW_ValidItem(infoPtr
, infoPtr
->selectedItem
))
3750 SelItem
= infoPtr
->selectedItem
;
3752 while(SelItem
&& (SelItem
!= ht
.hItem
) && TREEVIEW_ValidItem(infoPtr
, SelItem
) && (SelItem
!= infoPtr
->root
))
3754 TREEVIEW_Collapse(infoPtr
, SelItem
, FALSE
, FALSE
);
3755 SelItem
= SelItem
->parent
;
3761 * Expand the current item
3763 TREEVIEW_Expand(infoPtr
, ht
.hItem
, TVE_TOGGLE
, FALSE
);
3766 /* Select the current item */
3767 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, ht
.hItem
, TVC_BYMOUSE
);
3769 else if (ht
.flags
& TVHT_ONITEMSTATEICON
)
3771 /* TVS_CHECKBOXES requires us to toggle the current state */
3772 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
3773 TREEVIEW_ToggleItemState(infoPtr
, ht
.hItem
);
3783 TREEVIEW_RButtonDown(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
3787 if (infoPtr
->hwndEdit
)
3789 SetFocus(infoPtr
->hwnd
);
3793 ht
.pt
.x
= (LONG
)(INT16
)LOWORD(lParam
);
3794 ht
.pt
.y
= (LONG
)(INT16
)HIWORD(lParam
);
3796 TREEVIEW_HitTest(infoPtr
, &ht
);
3798 if (TREEVIEW_TrackMouse(infoPtr
, ht
.pt
))
3802 TREEVIEW_SendTreeviewDnDNotify(infoPtr
, TVN_BEGINRDRAGA
, ht
.hItem
,
3804 infoPtr
->dropItem
= ht
.hItem
;
3809 SetFocus(infoPtr
->hwnd
);
3810 TREEVIEW_SendSimpleNotify(infoPtr
, NM_RCLICK
);
3817 TREEVIEW_RButtonUp(TREEVIEW_INFO
*infoPtr
, LPPOINT pPt
)
3819 HWND hwnd
= infoPtr
->hwnd
;
3822 /* Change to screen coordinate for WM_CONTEXTMENU */
3823 ClientToScreen(hwnd
, &pt
);
3825 SendMessageA(hwnd
, WM_CONTEXTMENU
, (WPARAM
)hwnd
, MAKELPARAM(pt
.x
, pt
.y
));
3831 TREEVIEW_CreateDragImage(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
3833 TREEVIEW_ITEM
*dragItem
= (HTREEITEM
)lParam
;
3837 HBITMAP hbmp
, hOldbmp
;
3844 if (!(infoPtr
->himlNormal
))
3847 if (!dragItem
|| !TREEVIEW_ValidItem(infoPtr
, dragItem
))
3850 TREEVIEW_UpdateDispInfo(infoPtr
, dragItem
, TVIF_TEXT
);
3852 hwtop
= GetDesktopWindow();
3853 htopdc
= GetDC(hwtop
);
3854 hdc
= CreateCompatibleDC(htopdc
);
3856 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
3857 GetTextExtentPoint32A(hdc
, dragItem
->pszText
, lstrlenA(dragItem
->pszText
),
3859 TRACE("%ld %ld %s %d\n", size
.cx
, size
.cy
, dragItem
->pszText
,
3860 lstrlenA(dragItem
->pszText
));
3861 hbmp
= CreateCompatibleBitmap(htopdc
, size
.cx
, size
.cy
);
3862 hOldbmp
= SelectObject(hdc
, hbmp
);
3864 ImageList_GetIconSize(infoPtr
->himlNormal
, &cx
, &cy
);
3869 infoPtr
->dragList
= ImageList_Create(size
.cx
, size
.cy
, ILC_COLOR
, 10, 10);
3870 ImageList_Draw(infoPtr
->himlNormal
, dragItem
->iImage
, hdc
, 0, 0,
3874 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
3875 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
3878 /* draw item text */
3880 SetRect(&rc
, cx
, 0, size
.cx
, size
.cy
);
3881 DrawTextA(hdc
, dragItem
->pszText
, lstrlenA(dragItem
->pszText
), &rc
,
3883 SelectObject(hdc
, hOldFont
);
3884 SelectObject(hdc
, hOldbmp
);
3886 ImageList_Add(infoPtr
->dragList
, hbmp
, 0);
3890 ReleaseDC(hwtop
, htopdc
);
3892 return (LRESULT
)infoPtr
->dragList
;
3895 /* Selection ************************************************************/
3898 TREEVIEW_DoSelectItem(TREEVIEW_INFO
*infoPtr
, INT action
, HTREEITEM newSelect
,
3901 TREEVIEW_ITEM
*prevSelect
;
3904 assert(newSelect
== NULL
|| TREEVIEW_ValidItem(infoPtr
, newSelect
));
3906 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
3907 newSelect
, TREEVIEW_ItemName(newSelect
), action
, cause
,
3908 newSelect
? newSelect
->state
: 0);
3910 /* reset and redraw focusedItem if focusedItem was set so we don't */
3911 /* have to worry about the previously focused item when we set a new one */
3912 if(infoPtr
->focusedItem
)
3914 rcFocused
= (infoPtr
->focusedItem
)->rect
;
3915 infoPtr
->focusedItem
= 0;
3916 InvalidateRect(infoPtr
->hwnd
, &rcFocused
, TRUE
);
3922 prevSelect
= infoPtr
->selectedItem
;
3924 if (prevSelect
== newSelect
)
3927 if (TREEVIEW_SendTreeviewNotify(infoPtr
,
3930 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
,
3936 prevSelect
->state
&= ~TVIS_SELECTED
;
3938 newSelect
->state
|= TVIS_SELECTED
;
3940 infoPtr
->selectedItem
= newSelect
;
3942 TREEVIEW_EnsureVisible(infoPtr
, infoPtr
->selectedItem
, FALSE
);
3944 TREEVIEW_SendTreeviewNotify(infoPtr
,
3947 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
,
3950 TREEVIEW_QueueItemRefresh(infoPtr
, prevSelect
);
3951 TREEVIEW_QueueItemRefresh(infoPtr
, newSelect
);
3954 case TVGN_DROPHILITE
:
3955 prevSelect
= infoPtr
->dropItem
;
3958 prevSelect
->state
&= ~TVIS_DROPHILITED
;
3960 infoPtr
->dropItem
= newSelect
;
3963 newSelect
->state
|= TVIS_DROPHILITED
;
3965 TREEVIEW_QueueItemRefresh(infoPtr
, prevSelect
);
3966 TREEVIEW_QueueItemRefresh(infoPtr
, newSelect
);
3969 case TVGN_FIRSTVISIBLE
:
3970 TREEVIEW_EnsureVisible(infoPtr
, newSelect
, FALSE
);
3971 TREEVIEW_SetFirstVisible(infoPtr
, newSelect
, TRUE
);
3972 TREEVIEW_QueueRefresh(infoPtr
);
3976 TRACE("Leaving state %d\n", newSelect
? newSelect
->state
: 0);
3980 /* FIXME: handle NM_KILLFOCUS etc */
3982 TREEVIEW_SelectItem(TREEVIEW_INFO
*infoPtr
, INT wParam
, HTREEITEM item
)
3984 if (item
!= NULL
&& !TREEVIEW_ValidItem(infoPtr
, item
))
3987 TRACE("%p (%s) %d\n", item
, TREEVIEW_ItemName(item
), wParam
);
3989 if (!TREEVIEW_DoSelectItem(infoPtr
, wParam
, item
, TVC_UNKNOWN
))
3995 /* Scrolling ************************************************************/
3998 TREEVIEW_EnsureVisible(TREEVIEW_INFO
*infoPtr
, HTREEITEM item
, BOOL bHScroll
)
4000 HTREEITEM newFirstVisible
= NULL
;
4003 if (!TREEVIEW_ValidItem(infoPtr
, item
))
4006 if (!ISVISIBLE(item
))
4008 /* Expand parents as necessary. */
4011 /* see if we are trying to ensure that root is vislble */
4012 if((item
!= infoPtr
->root
) && TREEVIEW_ValidItem(infoPtr
, item
))
4013 parent
= item
->parent
;
4015 parent
= item
; /* this item is the topmost item */
4017 while (parent
!= infoPtr
->root
)
4019 if (!(parent
->state
& TVIS_EXPANDED
))
4020 TREEVIEW_Expand(infoPtr
, parent
, FALSE
, FALSE
);
4022 parent
= parent
->parent
;
4026 TRACE("%p (%s) %ld - %ld\n", item
, TREEVIEW_ItemName(item
), item
->visibleOrder
,
4027 infoPtr
->firstVisible
->visibleOrder
);
4029 visible_pos
= item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
;
4031 if (visible_pos
< 0)
4033 /* item is before the start of the list: put it at the top. */
4034 newFirstVisible
= item
;
4036 else if (visible_pos
>= TREEVIEW_GetVisibleCount(infoPtr
)
4037 /* Sometimes, before we are displayed, GVC is 0, causing us to
4038 * spuriously scroll up. */
4041 /* item is past the end of the list. */
4042 int scroll
= visible_pos
- TREEVIEW_GetVisibleCount(infoPtr
);
4044 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, infoPtr
->firstVisible
,
4050 /* Scroll window so item's text is visible as much as possible */
4051 /* Calculation of amount of extra space is taken from EditLabel code */
4053 TEXTMETRICA textMetric
;
4054 HDC hdc
= GetWindowDC(infoPtr
->hwnd
);
4056 x
= item
->textWidth
;
4058 GetTextMetricsA(hdc
, &textMetric
);
4059 ReleaseDC(infoPtr
->hwnd
, hdc
);
4061 x
+= (textMetric
.tmMaxCharWidth
* 2);
4062 x
= max(x
, textMetric
.tmMaxCharWidth
* 3);
4064 if (item
->textOffset
< 0)
4065 pos
= item
->textOffset
;
4066 else if (item
->textOffset
+ x
> infoPtr
->clientWidth
)
4068 if (x
> infoPtr
->clientWidth
)
4069 pos
= item
->textOffset
;
4071 pos
= item
->textOffset
+ x
- infoPtr
->clientWidth
;
4076 TREEVIEW_HScroll(infoPtr
, MAKEWPARAM(SB_THUMBPOSITION
, infoPtr
->scrollX
+ pos
));
4079 if (newFirstVisible
!= NULL
&& newFirstVisible
!= infoPtr
->firstVisible
)
4081 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
4090 TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*infoPtr
,
4091 TREEVIEW_ITEM
*newFirstVisible
,
4092 BOOL bUpdateScrollPos
)
4096 TRACE("%p: %s\n", newFirstVisible
, TREEVIEW_ItemName(newFirstVisible
));
4098 if (newFirstVisible
!= NULL
)
4100 /* Prevent an empty gap from appearing at the bottom... */
4101 gap_size
= TREEVIEW_GetVisibleCount(infoPtr
)
4102 - infoPtr
->maxVisibleOrder
+ newFirstVisible
->visibleOrder
;
4106 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, newFirstVisible
,
4109 /* ... unless we just don't have enough items. */
4110 if (newFirstVisible
== NULL
)
4111 newFirstVisible
= infoPtr
->root
->firstChild
;
4115 if (infoPtr
->firstVisible
!= newFirstVisible
)
4117 if (infoPtr
->firstVisible
== NULL
|| newFirstVisible
== NULL
)
4119 infoPtr
->firstVisible
= newFirstVisible
;
4120 TREEVIEW_QueueRefresh(infoPtr
);
4124 TREEVIEW_ITEM
*item
;
4125 int scroll
= infoPtr
->uItemHeight
*
4126 (infoPtr
->firstVisible
->visibleOrder
4127 - newFirstVisible
->visibleOrder
);
4129 infoPtr
->firstVisible
= newFirstVisible
;
4131 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
4132 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
4134 item
->rect
.top
+= scroll
;
4135 item
->rect
.bottom
+= scroll
;
4138 if (bUpdateScrollPos
)
4139 SetScrollPos(infoPtr
->hwnd
, SB_VERT
,
4140 newFirstVisible
->visibleOrder
, TRUE
);
4142 ScrollWindow(infoPtr
->hwnd
, 0, scroll
, NULL
, NULL
);
4143 UpdateWindow(infoPtr
->hwnd
);
4148 /************************************************************************
4149 * VScroll is always in units of visible items. i.e. we always have a
4150 * visible item aligned to the top of the control. (Unless we have no
4154 TREEVIEW_VScroll(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4156 TREEVIEW_ITEM
*oldFirstVisible
= infoPtr
->firstVisible
;
4157 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
4159 int nScrollCode
= LOWORD(wParam
);
4161 TRACE("wp %x\n", wParam
);
4163 if (!(infoPtr
->uInternalStatus
& TV_VSCROLL
))
4166 if (infoPtr
->hwndEdit
)
4167 SetFocus(infoPtr
->hwnd
);
4169 if (!oldFirstVisible
)
4171 assert(infoPtr
->root
->firstChild
== NULL
);
4175 switch (nScrollCode
)
4178 newFirstVisible
= infoPtr
->root
->firstChild
;
4182 newFirstVisible
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
4186 newFirstVisible
= TREEVIEW_GetPrevListItem(infoPtr
, oldFirstVisible
);
4190 newFirstVisible
= TREEVIEW_GetNextListItem(infoPtr
, oldFirstVisible
);
4194 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, oldFirstVisible
,
4195 -max(1, TREEVIEW_GetVisibleCount(infoPtr
)));
4199 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, oldFirstVisible
,
4200 max(1, TREEVIEW_GetVisibleCount(infoPtr
)));
4204 case SB_THUMBPOSITION
:
4205 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
,
4206 infoPtr
->root
->firstChild
,
4207 (LONG
)(INT16
)HIWORD(wParam
));
4214 if (newFirstVisible
!= NULL
)
4216 if (newFirstVisible
!= oldFirstVisible
)
4217 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
,
4218 nScrollCode
!= SB_THUMBTRACK
);
4219 else if (nScrollCode
== SB_THUMBPOSITION
)
4220 SetScrollPos(infoPtr
->hwnd
, SB_VERT
,
4221 newFirstVisible
->visibleOrder
, TRUE
);
4228 TREEVIEW_HScroll(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4231 int scrollX
= infoPtr
->scrollX
;
4232 int nScrollCode
= LOWORD(wParam
);
4234 TRACE("wp %x\n", wParam
);
4236 if (!(infoPtr
->uInternalStatus
& TV_HSCROLL
))
4239 if (infoPtr
->hwndEdit
)
4240 SetFocus(infoPtr
->hwnd
);
4242 maxWidth
= infoPtr
->treeWidth
- infoPtr
->clientWidth
;
4243 /* shall never occure */
4250 switch (nScrollCode
)
4253 scrollX
-= infoPtr
->uItemHeight
;
4256 scrollX
+= infoPtr
->uItemHeight
;
4259 scrollX
-= infoPtr
->clientWidth
;
4262 scrollX
+= infoPtr
->clientWidth
;
4266 case SB_THUMBPOSITION
:
4267 scrollX
= (int)(INT16
)HIWORD(wParam
);
4274 if (scrollX
> maxWidth
)
4276 else if (scrollX
< 0)
4280 if (scrollX
!= infoPtr
->scrollX
)
4282 TREEVIEW_ITEM
*item
;
4283 LONG scroll_pixels
= infoPtr
->scrollX
- scrollX
;
4285 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
4286 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
4288 item
->linesOffset
+= scroll_pixels
;
4289 item
->stateOffset
+= scroll_pixels
;
4290 item
->imageOffset
+= scroll_pixels
;
4291 item
->textOffset
+= scroll_pixels
;
4294 ScrollWindow(infoPtr
->hwnd
, scroll_pixels
, 0, NULL
, NULL
);
4295 infoPtr
->scrollX
= scrollX
;
4296 UpdateWindow(infoPtr
->hwnd
);
4299 if (nScrollCode
!= SB_THUMBTRACK
)
4300 SetScrollPos(infoPtr
->hwnd
, SB_HORZ
, scrollX
, TRUE
);
4306 TREEVIEW_MouseWheel(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4309 UINT pulScrollLines
= 3;
4311 if (infoPtr
->firstVisible
== NULL
)
4314 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
, 0, &pulScrollLines
, 0);
4316 gcWheelDelta
= -(short)HIWORD(wParam
);
4317 pulScrollLines
*= (gcWheelDelta
/ WHEEL_DELTA
);
4319 if (abs(gcWheelDelta
) >= WHEEL_DELTA
&& pulScrollLines
)
4321 int newDy
= infoPtr
->firstVisible
->visibleOrder
+ pulScrollLines
;
4322 int maxDy
= infoPtr
->maxVisibleOrder
;
4330 TREEVIEW_VScroll(infoPtr
, MAKEWPARAM(SB_THUMBPOSITION
, newDy
));
4335 /* Create/Destroy *******************************************************/
4338 TREEVIEW_Create(HWND hwnd
)
4341 TREEVIEW_INFO
*infoPtr
;
4343 TRACE("wnd %x, style %lx\n", hwnd
, GetWindowLongA(hwnd
, GWL_STYLE
));
4345 infoPtr
= (TREEVIEW_INFO
*)COMCTL32_Alloc(sizeof(TREEVIEW_INFO
));
4347 if (infoPtr
== NULL
)
4349 ERR("could not allocate info memory!\n");
4353 SetWindowLongA(hwnd
, 0, (DWORD
)infoPtr
);
4355 infoPtr
->hwnd
= hwnd
;
4356 infoPtr
->dwStyle
= GetWindowLongA(hwnd
, GWL_STYLE
);
4357 infoPtr
->uInternalStatus
= 0;
4359 infoPtr
->uNumItems
= 0;
4360 infoPtr
->cdmode
= 0;
4361 infoPtr
->uScrollTime
= 300; /* milliseconds */
4362 infoPtr
->bRedraw
= TRUE
;
4364 GetClientRect(hwnd
, &rcClient
);
4366 /* No scroll bars yet. */
4367 infoPtr
->clientWidth
= rcClient
.right
;
4368 infoPtr
->clientHeight
= rcClient
.bottom
;
4370 infoPtr
->treeWidth
= 0;
4371 infoPtr
->treeHeight
= 0;
4373 infoPtr
->uIndent
= 19;
4374 infoPtr
->selectedItem
= 0;
4375 infoPtr
->focusedItem
= 0;
4377 infoPtr
->firstVisible
= 0;
4378 infoPtr
->maxVisibleOrder
= 0;
4379 infoPtr
->dropItem
= 0;
4380 infoPtr
->insertMarkItem
= 0;
4381 infoPtr
->insertBeforeorAfter
= 0;
4384 infoPtr
->scrollX
= 0;
4386 infoPtr
->clrBk
= GetSysColor(COLOR_WINDOW
);
4387 infoPtr
->clrText
= -1; /* use system color */
4388 infoPtr
->clrLine
= RGB(128, 128, 128);
4389 infoPtr
->clrInsertMark
= GetSysColor(COLOR_BTNTEXT
);
4393 infoPtr
->hwndEdit
= 0;
4394 infoPtr
->wpEditOrig
= NULL
;
4395 infoPtr
->bIgnoreEditKillFocus
= FALSE
;
4396 infoPtr
->bLabelChanged
= FALSE
;
4398 infoPtr
->himlNormal
= NULL
;
4399 infoPtr
->himlState
= NULL
;
4400 infoPtr
->normalImageWidth
= 0;
4401 infoPtr
->normalImageHeight
= 0;
4402 infoPtr
->stateImageWidth
= 0;
4403 infoPtr
->stateImageHeight
= 0;
4405 infoPtr
->items
= DPA_Create(16);
4407 infoPtr
->hFont
= GetStockObject(DEFAULT_GUI_FONT
);
4408 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
4410 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
4412 infoPtr
->root
= TREEVIEW_AllocateItem(infoPtr
);
4413 infoPtr
->root
->state
= TVIS_EXPANDED
;
4414 infoPtr
->root
->iLevel
= -1;
4415 infoPtr
->root
->visibleOrder
= -1;
4418 infoPtr
->hwndNotify
= GetParent32 (hwnd
);
4419 infoPtr
->bTransparent
= ( GetWindowLongA( hwnd
, GWL_STYLE
) & TBSTYLE_FLAT
);
4422 infoPtr
->hwndToolTip
= 0;
4423 if (!(infoPtr
->dwStyle
& TVS_NOTOOLTIPS
))
4424 infoPtr
->hwndToolTip
= COMCTL32_CreateToolTip(hwnd
);
4426 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
4429 HBITMAP hbm
, hbmOld
;
4433 infoPtr
->himlState
=
4434 ImageList_Create(16, 16, ILC_COLOR
| ILC_MASK
, 3, 0);
4436 hdc
= CreateCompatibleDC(0);
4437 hbm
= CreateCompatibleBitmap(hdc
, 48, 16);
4438 hbmOld
= SelectObject(hdc
, hbm
);
4440 rc
.left
= 0; rc
.top
= 0;
4441 rc
.right
= 48; rc
.bottom
= 16;
4442 FillRect(hdc
, &rc
, (HBRUSH
)(COLOR_WINDOW
+1));
4444 rc
.left
= 18; rc
.top
= 2;
4445 rc
.right
= 30; rc
.bottom
= 14;
4446 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
4447 DFCS_BUTTONCHECK
|DFCS_FLAT
);
4449 rc
.left
= 34; rc
.right
= 46;
4450 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
4451 DFCS_BUTTONCHECK
|DFCS_FLAT
|DFCS_CHECKED
);
4453 nIndex
= ImageList_AddMasked(infoPtr
->himlState
, hbm
,
4454 GetSysColor(COLOR_WINDOW
));
4455 TRACE("chckbox index %d\n", nIndex
);
4456 SelectObject(hdc
, hbmOld
);
4460 infoPtr
->stateImageWidth
= 16;
4461 infoPtr
->stateImageHeight
= 16;
4468 TREEVIEW_Destroy(TREEVIEW_INFO
*infoPtr
)
4472 TREEVIEW_RemoveTree(infoPtr
);
4474 /* tool tip is automatically destroyed: we are its owner */
4476 /* Restore original windproc. */
4477 if (infoPtr
->hwndEdit
)
4478 SetWindowLongA(infoPtr
->hwndEdit
, GWL_WNDPROC
,
4479 (LONG
)infoPtr
->wpEditOrig
);
4481 /* Deassociate treeview from the window before doing anything drastic. */
4482 SetWindowLongA(infoPtr
->hwnd
, 0, (LONG
)NULL
);
4484 DeleteObject(infoPtr
->hBoldFont
);
4485 COMCTL32_Free(infoPtr
);
4490 /* Miscellaneous Messages ***********************************************/
4493 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO
*infoPtr
, WPARAM key
)
4501 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4502 SCROLL_ENTRY(SB_VERT
, SB_PAGEUP
), /* VK_PRIOR */
4503 SCROLL_ENTRY(SB_VERT
, SB_PAGEDOWN
), /* VK_NEXT */
4504 SCROLL_ENTRY(SB_VERT
, SB_BOTTOM
), /* VK_END */
4505 SCROLL_ENTRY(SB_VERT
, SB_TOP
), /* VK_HOME */
4506 SCROLL_ENTRY(SB_HORZ
, SB_LINEUP
), /* VK_LEFT */
4507 SCROLL_ENTRY(SB_VERT
, SB_LINEUP
), /* VK_UP */
4508 SCROLL_ENTRY(SB_HORZ
, SB_LINEDOWN
), /* VK_RIGHT */
4509 SCROLL_ENTRY(SB_VERT
, SB_LINEDOWN
) /* VK_DOWN */
4513 if (key
>= VK_PRIOR
&& key
<= VK_DOWN
)
4515 unsigned char code
= scroll
[key
- VK_PRIOR
].code
;
4517 (((code
& (1 << 7)) == (SB_HORZ
<< 7))
4519 : TREEVIEW_VScroll
)(infoPtr
, code
& 0x7F);
4525 /************************************************************************
4528 * VK_UP Move selection to the previous non-hidden item.
4529 * VK_DOWN Move selection to the next non-hidden item.
4530 * VK_HOME Move selection to the first item.
4531 * VK_END Move selection to the last item.
4532 * VK_LEFT If expanded then collapse, otherwise move to parent.
4533 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4535 * VK_SUBTRACT Collapse.
4536 * VK_MULTIPLY Expand all.
4537 * VK_PRIOR Move up GetVisibleCount items.
4538 * VK_NEXT Move down GetVisibleCount items.
4539 * VK_BACK Move to parent.
4540 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
4543 TREEVIEW_KeyDown(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4545 /* If it is non-NULL and different, it will be selected and visible. */
4546 TREEVIEW_ITEM
*newSelection
= NULL
;
4548 TREEVIEW_ITEM
*prevItem
= infoPtr
->selectedItem
;
4550 TRACE("%x\n", wParam
);
4552 if (prevItem
== NULL
)
4555 if (GetAsyncKeyState(VK_CONTROL
) & 0x8000)
4556 return TREEVIEW_ScrollKeyDown(infoPtr
, wParam
);
4561 newSelection
= TREEVIEW_GetPrevListItem(infoPtr
, prevItem
);
4563 newSelection
= infoPtr
->root
->firstChild
;
4567 newSelection
= TREEVIEW_GetNextListItem(infoPtr
, prevItem
);
4571 newSelection
= infoPtr
->root
->firstChild
;
4575 newSelection
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
4579 if (prevItem
->state
& TVIS_EXPANDED
)
4581 TREEVIEW_Collapse(infoPtr
, prevItem
, FALSE
, TRUE
);
4583 else if (prevItem
->parent
!= infoPtr
->root
)
4585 newSelection
= prevItem
->parent
;
4590 if (TREEVIEW_HasChildren(infoPtr
, prevItem
))
4592 if (!(prevItem
->state
& TVIS_EXPANDED
))
4593 TREEVIEW_Expand(infoPtr
, prevItem
, FALSE
, TRUE
);
4596 newSelection
= prevItem
->firstChild
;
4603 TREEVIEW_ExpandAll(infoPtr
, prevItem
);
4607 if (!(prevItem
->state
& TVIS_EXPANDED
))
4608 TREEVIEW_Expand(infoPtr
, prevItem
, FALSE
, TRUE
);
4612 if (prevItem
->state
& TVIS_EXPANDED
)
4613 TREEVIEW_Collapse(infoPtr
, prevItem
, FALSE
, TRUE
);
4618 = TREEVIEW_GetListItem(infoPtr
, prevItem
,
4619 -TREEVIEW_GetVisibleCount(infoPtr
));
4624 = TREEVIEW_GetListItem(infoPtr
, prevItem
,
4625 TREEVIEW_GetVisibleCount(infoPtr
));
4629 newSelection
= prevItem
->parent
;
4630 if (newSelection
== infoPtr
->root
)
4631 newSelection
= NULL
;
4635 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
4636 TREEVIEW_ToggleItemState(infoPtr
, prevItem
);
4640 if (newSelection
&& newSelection
!= prevItem
)
4642 if (TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
,
4645 TREEVIEW_EnsureVisible(infoPtr
, newSelection
, FALSE
);
4653 TREEVIEW_Size(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
4655 if (wParam
== SIZE_RESTORED
)
4657 infoPtr
->clientWidth
= (LONG
)(INT16
)LOWORD(lParam
);
4658 infoPtr
->clientHeight
= (LONG
)(INT16
)HIWORD(lParam
);
4660 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
4661 TREEVIEW_SetFirstVisible(infoPtr
, infoPtr
->firstVisible
, TRUE
);
4662 TREEVIEW_UpdateScrollBars(infoPtr
);
4666 FIXME("WM_SIZE flag %x %lx not handled\n", wParam
, lParam
);
4669 TREEVIEW_QueueRefresh(infoPtr
);
4674 TREEVIEW_StyleChanged(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
4676 TRACE("(%x %lx)\n", wParam
, lParam
);
4678 if (wParam
== GWL_STYLE
)
4680 DWORD dwNewStyle
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
4682 /* we have to take special care about tooltips */
4683 if ((infoPtr
->dwStyle
^ dwNewStyle
) & TVS_NOTOOLTIPS
)
4685 if (infoPtr
->dwStyle
& TVS_NOTOOLTIPS
)
4687 infoPtr
->hwndToolTip
= COMCTL32_CreateToolTip(infoPtr
->hwnd
);
4692 DestroyWindow(infoPtr
->hwndToolTip
);
4693 infoPtr
->hwndToolTip
= 0;
4697 infoPtr
->dwStyle
= dwNewStyle
;
4700 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
4701 TREEVIEW_UpdateScrollBars(infoPtr
);
4702 TREEVIEW_QueueRefresh(infoPtr
);
4708 TREEVIEW_SetFocus(TREEVIEW_INFO
*infoPtr
)
4712 if (!infoPtr
->selectedItem
)
4714 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, infoPtr
->firstVisible
,
4718 TREEVIEW_SendSimpleNotify(infoPtr
, NM_SETFOCUS
);
4719 TREEVIEW_QueueItemRefresh(infoPtr
, infoPtr
->selectedItem
);
4724 TREEVIEW_KillFocus(TREEVIEW_INFO
*infoPtr
)
4728 TREEVIEW_SendSimpleNotify(infoPtr
, NM_KILLFOCUS
);
4729 TREEVIEW_QueueItemRefresh(infoPtr
, infoPtr
->selectedItem
);
4734 static LRESULT WINAPI
4735 TREEVIEW_WindowProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
4737 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(hwnd
);
4738 if (infoPtr
) TREEVIEW_VerifyTree(infoPtr
);
4741 if (uMsg
== WM_CREATE
)
4742 TREEVIEW_Create(hwnd
);
4749 case TVM_CREATEDRAGIMAGE
:
4750 return TREEVIEW_CreateDragImage(infoPtr
, wParam
, lParam
);
4752 case TVM_DELETEITEM
:
4753 return TREEVIEW_DeleteItem(infoPtr
, (HTREEITEM
)lParam
);
4755 case TVM_EDITLABELA
:
4756 return TREEVIEW_EditLabelA(infoPtr
, (HTREEITEM
)lParam
);
4758 case TVM_EDITLABELW
:
4759 FIXME("Unimplemented msg TVM_EDITLABELW\n");
4762 case TVM_ENDEDITLABELNOW
:
4763 return TREEVIEW_EndEditLabelNow(infoPtr
, (BOOL
)wParam
);
4765 case TVM_ENSUREVISIBLE
:
4766 return TREEVIEW_EnsureVisible(infoPtr
, (HTREEITEM
)lParam
, TRUE
);
4769 return TREEVIEW_ExpandMsg(infoPtr
, (UINT
)wParam
, (HTREEITEM
)lParam
);
4771 case TVM_GETBKCOLOR
:
4772 return TREEVIEW_GetBkColor(infoPtr
);
4775 return TREEVIEW_GetCount(infoPtr
);
4777 case TVM_GETEDITCONTROL
:
4778 return TREEVIEW_GetEditControl(infoPtr
);
4780 case TVM_GETIMAGELIST
:
4781 return TREEVIEW_GetImageList(infoPtr
, wParam
);
4784 return TREEVIEW_GetIndent(infoPtr
);
4786 case TVM_GETINSERTMARKCOLOR
:
4787 return TREEVIEW_GetInsertMarkColor(infoPtr
);
4789 case TVM_GETISEARCHSTRINGA
:
4790 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
4793 case TVM_GETISEARCHSTRINGW
:
4794 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
4798 return TREEVIEW_GetItemA(infoPtr
, (LPTVITEMEXA
)lParam
);
4801 return TREEVIEW_GetItemW(infoPtr
, (LPTVITEMEXA
)lParam
);
4803 case TVM_GETITEMHEIGHT
:
4804 return TREEVIEW_GetItemHeight(infoPtr
);
4806 case TVM_GETITEMRECT
:
4807 return TREEVIEW_GetItemRect(infoPtr
, (BOOL
)wParam
, (LPRECT
)lParam
);
4809 case TVM_GETITEMSTATE
:
4810 return TREEVIEW_GetItemState(infoPtr
, (HTREEITEM
)wParam
, (UINT
)lParam
);
4812 case TVM_GETLINECOLOR
:
4813 return TREEVIEW_GetLineColor(infoPtr
);
4815 case TVM_GETNEXTITEM
:
4816 return TREEVIEW_GetNextItem(infoPtr
, (UINT
)wParam
, (HTREEITEM
)lParam
);
4818 case TVM_GETSCROLLTIME
:
4819 return TREEVIEW_GetScrollTime(infoPtr
);
4821 case TVM_GETTEXTCOLOR
:
4822 return TREEVIEW_GetTextColor(infoPtr
);
4824 case TVM_GETTOOLTIPS
:
4825 return TREEVIEW_GetToolTips(infoPtr
);
4827 case TVM_GETUNICODEFORMAT
:
4828 FIXME("Unimplemented msg TVM_GETUNICODEFORMAT\n");
4831 case TVM_GETVISIBLECOUNT
:
4832 return TREEVIEW_GetVisibleCount(infoPtr
);
4835 return TREEVIEW_HitTest(infoPtr
, (LPTVHITTESTINFO
)lParam
);
4837 case TVM_INSERTITEMA
:
4838 return TREEVIEW_InsertItemA(infoPtr
, lParam
);
4840 case TVM_INSERTITEMW
:
4841 return TREEVIEW_InsertItemW(infoPtr
, lParam
);
4843 case TVM_SELECTITEM
:
4844 return TREEVIEW_SelectItem(infoPtr
, (INT
)wParam
, (HTREEITEM
)lParam
);
4846 case TVM_SETBKCOLOR
:
4847 return TREEVIEW_SetBkColor(infoPtr
, (COLORREF
)lParam
);
4849 case TVM_SETIMAGELIST
:
4850 return TREEVIEW_SetImageList(infoPtr
, wParam
, (HIMAGELIST
)lParam
);
4853 return TREEVIEW_SetIndent(infoPtr
, (UINT
)wParam
);
4855 case TVM_SETINSERTMARK
:
4856 return TREEVIEW_SetInsertMark(infoPtr
, (BOOL
)wParam
, (HTREEITEM
)lParam
);
4858 case TVM_SETINSERTMARKCOLOR
:
4859 return TREEVIEW_SetInsertMarkColor(infoPtr
, (COLORREF
)lParam
);
4862 return TREEVIEW_SetItemA(infoPtr
, (LPTVITEMEXA
)lParam
);
4865 FIXME("Unimplemented msg TVM_SETITEMW\n");
4868 case TVM_SETLINECOLOR
:
4869 return TREEVIEW_SetLineColor(infoPtr
, (COLORREF
)lParam
);
4871 case TVM_SETITEMHEIGHT
:
4872 return TREEVIEW_SetItemHeight(infoPtr
, (INT
)(SHORT
)wParam
);
4874 case TVM_SETSCROLLTIME
:
4875 return TREEVIEW_SetScrollTime(infoPtr
, (UINT
)wParam
);
4877 case TVM_SETTEXTCOLOR
:
4878 return TREEVIEW_SetTextColor(infoPtr
, (COLORREF
)lParam
);
4880 case TVM_SETTOOLTIPS
:
4881 return TREEVIEW_SetToolTips(infoPtr
, (HWND
)wParam
);
4883 case TVM_SETUNICODEFORMAT
:
4884 FIXME("Unimplemented msg TVM_SETUNICODEFORMAT\n");
4887 case TVM_SORTCHILDREN
:
4888 return TREEVIEW_SortChildren(infoPtr
, wParam
, lParam
);
4890 case TVM_SORTCHILDRENCB
:
4891 return TREEVIEW_SortChildrenCB(infoPtr
, wParam
, (LPTVSORTCB
)lParam
);
4896 return TREEVIEW_Command(infoPtr
, wParam
, lParam
);
4899 return TREEVIEW_Destroy(infoPtr
);
4904 return TREEVIEW_EraseBackground(infoPtr
, (HDC
)wParam
);
4907 return DLGC_WANTARROWS
| DLGC_WANTCHARS
;
4910 return TREEVIEW_GetFont(infoPtr
);
4913 return TREEVIEW_HScroll(infoPtr
, wParam
);
4916 return TREEVIEW_KeyDown(infoPtr
, wParam
);
4919 return TREEVIEW_KillFocus(infoPtr
);
4921 case WM_LBUTTONDBLCLK
:
4922 return TREEVIEW_LButtonDoubleClick(infoPtr
, lParam
);
4924 case WM_LBUTTONDOWN
:
4925 return TREEVIEW_LButtonDown(infoPtr
, lParam
);
4927 /* WM_MBUTTONDOWN */
4933 /* WM_NOTIFYFORMAT */
4936 return TREEVIEW_Paint(infoPtr
, wParam
);
4938 /* WM_PRINTCLIENT */
4940 case WM_RBUTTONDOWN
:
4941 return TREEVIEW_RButtonDown(infoPtr
, lParam
);
4944 return TREEVIEW_SetFocus(infoPtr
);
4947 return TREEVIEW_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
4950 return TREEVIEW_SetRedraw(infoPtr
, wParam
, lParam
);
4953 return TREEVIEW_Size(infoPtr
, wParam
, lParam
);
4955 case WM_STYLECHANGED
:
4956 return TREEVIEW_StyleChanged(infoPtr
, wParam
, lParam
);
4958 /* WM_SYSCOLORCHANGE */
4963 return TREEVIEW_HandleTimer(infoPtr
, wParam
);
4966 return TREEVIEW_VScroll(infoPtr
, wParam
);
4968 /* WM_WININICHANGE */
4971 if (wParam
& (MK_SHIFT
| MK_CONTROL
))
4973 return TREEVIEW_MouseWheel(infoPtr
, wParam
);
4976 TRACE("drawItem\n");
4980 /* This mostly catches MFC and Delphi messages. :( */
4981 if (uMsg
>= WM_USER
)
4982 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg
, wParam
, lParam
);
4984 return DefWindowProcA(hwnd
, uMsg
, wParam
, lParam
);
4990 /* Class Registration ***************************************************/
4993 TREEVIEW_Register(void)
4999 ZeroMemory(&wndClass
, sizeof(WNDCLASSA
));
5000 wndClass
.style
= CS_GLOBALCLASS
| CS_DBLCLKS
;
5001 wndClass
.lpfnWndProc
= (WNDPROC
)TREEVIEW_WindowProc
;
5002 wndClass
.cbClsExtra
= 0;
5003 wndClass
.cbWndExtra
= sizeof(TREEVIEW_INFO
*);
5005 wndClass
.hCursor
= LoadCursorA(0, IDC_ARROWA
);
5006 wndClass
.hbrBackground
= 0;
5007 wndClass
.lpszClassName
= WC_TREEVIEWA
;
5009 RegisterClassA(&wndClass
);
5014 TREEVIEW_Unregister(void)
5016 UnregisterClassA(WC_TREEVIEWA
, (HINSTANCE
) NULL
);
5020 /* Tree Verification ****************************************************/
5024 TREEVIEW_VerifyChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
);
5026 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO
*infoPtr
,
5027 TREEVIEW_ITEM
*item
)
5029 assert(infoPtr
!= NULL
);
5030 assert(item
!= NULL
);
5032 /* both NULL, or both non-null */
5033 assert((item
->firstChild
== NULL
) == (item
->lastChild
== NULL
));
5035 assert(item
->firstChild
!= item
);
5036 assert(item
->lastChild
!= item
);
5038 if (item
->firstChild
)
5040 assert(item
->firstChild
->parent
== item
);
5041 assert(item
->firstChild
->prevSibling
== NULL
);
5044 if (item
->lastChild
)
5046 assert(item
->lastChild
->parent
== item
);
5047 assert(item
->lastChild
->nextSibling
== NULL
);
5050 assert(item
->nextSibling
!= item
);
5051 if (item
->nextSibling
)
5053 assert(item
->nextSibling
->parent
== item
->parent
);
5054 assert(item
->nextSibling
->prevSibling
== item
);
5057 assert(item
->prevSibling
!= item
);
5058 if (item
->prevSibling
)
5060 assert(item
->prevSibling
->parent
== item
->parent
);
5061 assert(item
->prevSibling
->nextSibling
== item
);
5066 TREEVIEW_VerifyItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
5068 assert(item
!= NULL
);
5070 assert(item
->parent
!= NULL
);
5071 assert(item
->parent
!= item
);
5072 assert(item
->iLevel
== item
->parent
->iLevel
+ 1);
5074 assert(DPA_GetPtrIndex(infoPtr
->items
, item
) != -1);
5076 TREEVIEW_VerifyItemCommon(infoPtr
, item
);
5078 TREEVIEW_VerifyChildren(infoPtr
, item
);
5082 TREEVIEW_VerifyChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
5084 TREEVIEW_ITEM
*child
;
5085 assert(item
!= NULL
);
5087 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
5088 TREEVIEW_VerifyItem(infoPtr
, child
);
5092 TREEVIEW_VerifyRoot(TREEVIEW_INFO
*infoPtr
)
5094 TREEVIEW_ITEM
*root
= infoPtr
->root
;
5096 assert(root
!= NULL
);
5097 assert(root
->iLevel
== -1);
5098 assert(root
->parent
== NULL
);
5099 assert(root
->prevSibling
== NULL
);
5101 TREEVIEW_VerifyItemCommon(infoPtr
, root
);
5103 TREEVIEW_VerifyChildren(infoPtr
, root
);
5107 TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
)
5109 assert(infoPtr
!= NULL
);
5111 TREEVIEW_VerifyRoot(infoPtr
);