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 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
23 * Note2: All items always! have valid (allocated) pszText field.
24 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
25 * of size TEXT_CALLBACK_SIZE in DoSetItem.
26 * We use callbackMask to keep track of fields to be updated.
29 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
30 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
32 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_NOSCROLL,
33 * TVS_RTLREADING, TVS_TRACKSELECT
35 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
37 * Make the insertion mark look right.
38 * Scroll (instead of repaint) as much as possible.
42 #include "wine/port.h"
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
56 #include "wine/debug.h"
58 /* internal structures */
60 typedef struct _TREEITEM
/* HTREEITEM is a _TREEINFO *. */
71 int iIntegral
; /* item height multiplier (1 is normal) */
72 int iLevel
; /* indentation level:0=root level */
73 HTREEITEM parent
; /* handle to parent or 0 if at root */
74 HTREEITEM firstChild
; /* handle to first child or 0 if no child */
76 HTREEITEM prevSibling
; /* handle to prev item in list, 0 if first */
77 HTREEITEM nextSibling
; /* handle to next item in list, 0 if last */
83 LONG textWidth
; /* horizontal text extent for pszText */
84 LONG visibleOrder
; /* visible ordering, 0 is first visible item */
88 typedef struct tagTREEVIEW_INFO
91 HWND hwndNotify
; /* Owner window to send notifications to */
96 UINT uNumItems
; /* number of valid TREEVIEW_ITEMs */
97 INT cdmode
; /* last custom draw setting */
98 UINT uScrollTime
; /* max. time for scrolling in milliseconds */
99 BOOL bRedraw
; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
101 UINT uItemHeight
; /* item height */
104 LONG clientWidth
; /* width of control window */
105 LONG clientHeight
; /* height of control window */
107 LONG treeWidth
; /* width of visible tree items */
108 LONG treeHeight
; /* height of visible tree items */
110 UINT uIndent
; /* indentation in pixels */
111 HTREEITEM selectedItem
; /* handle to selected item or 0 if none */
112 HTREEITEM hotItem
; /* handle currently under cursor, 0 if none */
113 HTREEITEM focusedItem
; /* item that was under the cursor when WM_LBUTTONDOWN was received */
115 HTREEITEM firstVisible
; /* handle to first visible item */
116 LONG maxVisibleOrder
;
117 HTREEITEM dropItem
; /* handle to item selected by drag cursor */
118 HTREEITEM insertMarkItem
; /* item after which insertion mark is placed */
119 BOOL insertBeforeorAfter
; /* flag used by TVM_SETINSERTMARK */
120 HIMAGELIST dragList
; /* Bitmap of dragged item */
125 COLORREF clrInsertMark
;
131 WNDPROC wpEditOrig
; /* orig window proc for subclassing edit */
132 BOOL bIgnoreEditKillFocus
;
135 BOOL bNtfUnicode
; /* TRUE if should send NOTIFY with W */
136 BOOL bUnicode
; /* set by CCM_SETUNICODEFORMAT */
137 HIMAGELIST himlNormal
;
138 int normalImageHeight
;
139 int normalImageWidth
;
140 HIMAGELIST himlState
;
141 int stateImageHeight
;
145 DWORD lastKeyPressTimestamp
; /* Added */
146 WPARAM charCode
; /* Added */
147 INT nSearchParamLength
; /* Added */
148 CHAR szSearchParam
[ MAX_PATH
]; /* Added */
152 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
153 #define KEY_DELAY 450
155 /* bitflags for infoPtr->uInternalStatus */
157 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
158 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
159 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
160 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
161 #define TV_RDRAG 0x10 /* dito Rbutton */
162 #define TV_RDRAGGING 0x20
164 /* bitflags for infoPtr->timer */
166 #define TV_EDIT_TIMER 2
167 #define TV_EDIT_TIMER_SET 2
170 VOID
TREEVIEW_Register (VOID
);
171 VOID
TREEVIEW_Unregister (VOID
);
174 WINE_DEFAULT_DEBUG_CHANNEL(treeview
);
177 #define TEXT_CALLBACK_SIZE 260
179 #define TREEVIEW_LEFT_MARGIN 8
181 #define MINIMUM_INDENT 19
183 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
185 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
186 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
187 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
190 typedef VOID (*TREEVIEW_ItemEnumFunc
)(TREEVIEW_INFO
*, TREEVIEW_ITEM
*,LPVOID
);
193 static VOID
TREEVIEW_Invalidate(TREEVIEW_INFO
*, TREEVIEW_ITEM
*);
195 static LRESULT
TREEVIEW_DoSelectItem(TREEVIEW_INFO
*, INT
, HTREEITEM
, INT
);
196 static VOID
TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*, TREEVIEW_ITEM
*, BOOL
);
197 static LRESULT
TREEVIEW_EnsureVisible(TREEVIEW_INFO
*, HTREEITEM
, BOOL
);
198 static LRESULT
TREEVIEW_RButtonUp(TREEVIEW_INFO
*, LPPOINT
);
199 static LRESULT
TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
);
200 static VOID
TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
);
201 static LRESULT
TREEVIEW_HScroll(TREEVIEW_INFO
*, WPARAM
);
202 static INT
TREEVIEW_NotifyFormat (TREEVIEW_INFO
*infoPtr
, HWND wParam
, UINT lParam
);
205 /* Random Utilities *****************************************************/
209 TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
)
214 /* The definition is at the end of the file. */
215 static void TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
);
218 /* Returns the treeview private data if hwnd is a treeview.
219 * Otherwise returns an undefined value. */
220 static TREEVIEW_INFO
*
221 TREEVIEW_GetInfoPtr(HWND hwnd
)
223 return (TREEVIEW_INFO
*)GetWindowLongA(hwnd
, 0);
226 /* Don't call this. Nothing wants an item index. */
228 TREEVIEW_GetItemIndex(TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
230 assert(infoPtr
!= NULL
);
232 return DPA_GetPtrIndex(infoPtr
->items
, handle
);
235 /***************************************************************************
236 * This method checks that handle is an item for this tree.
239 TREEVIEW_ValidItem(TREEVIEW_INFO
*infoPtr
, HTREEITEM handle
)
241 if (TREEVIEW_GetItemIndex(infoPtr
, handle
) == -1)
243 TRACE("invalid item %p\n", handle
);
251 TREEVIEW_CreateBoldFont(HFONT hOrigFont
)
255 GetObjectA(hOrigFont
, sizeof(font
), &font
);
256 font
.lfWeight
= FW_BOLD
;
257 return CreateFontIndirectA(&font
);
261 TREEVIEW_FontForItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
263 return (item
->state
& TVIS_BOLD
) ? infoPtr
->hBoldFont
: infoPtr
->hFont
;
266 /* for trace/debugging purposes only */
268 TREEVIEW_ItemName(TREEVIEW_ITEM
*item
)
270 if (item
== NULL
) return "<null item>";
271 if (item
->pszText
== LPSTR_TEXTCALLBACKA
) return "<callback>";
272 if (item
->pszText
== NULL
) return "<null>";
273 return item
->pszText
;
276 /* An item is not a child of itself. */
278 TREEVIEW_IsChildOf(TREEVIEW_ITEM
*parent
, TREEVIEW_ITEM
*child
)
282 child
= child
->parent
;
283 if (child
== parent
) return TRUE
;
284 } while (child
!= NULL
);
290 /* Tree Traversal *******************************************************/
292 /***************************************************************************
293 * This method returns the last expanded sibling or child child item
296 static TREEVIEW_ITEM
*
297 TREEVIEW_GetLastListItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
)
302 while (wineItem
->lastChild
)
304 if (wineItem
->state
& TVIS_EXPANDED
)
305 wineItem
= wineItem
->lastChild
;
310 if (wineItem
== infoPtr
->root
)
316 /***************************************************************************
317 * This method returns the previous non-hidden item in the list not
318 * considering the tree hierarchy.
320 static TREEVIEW_ITEM
*
321 TREEVIEW_GetPrevListItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*tvItem
)
323 if (tvItem
->prevSibling
)
325 /* This item has a prevSibling, get the last item in the sibling's tree. */
326 TREEVIEW_ITEM
*upItem
= tvItem
->prevSibling
;
328 if ((upItem
->state
& TVIS_EXPANDED
) && upItem
->lastChild
!= NULL
)
329 return TREEVIEW_GetLastListItem(infoPtr
, upItem
->lastChild
);
335 /* this item does not have a prevSibling, get the parent */
336 return (tvItem
->parent
!= infoPtr
->root
) ? tvItem
->parent
: NULL
;
341 /***************************************************************************
342 * This method returns the next physical item in the treeview not
343 * considering the tree hierarchy.
345 static TREEVIEW_ITEM
*
346 TREEVIEW_GetNextListItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*tvItem
)
348 assert(tvItem
!= NULL
);
351 * If this item has children and is expanded, return the first child
353 if ((tvItem
->state
& TVIS_EXPANDED
) && tvItem
->firstChild
!= NULL
)
355 return tvItem
->firstChild
;
360 * try to get the sibling
362 if (tvItem
->nextSibling
)
363 return tvItem
->nextSibling
;
366 * Otherwise, get the parent's sibling.
368 while (tvItem
->parent
)
370 tvItem
= tvItem
->parent
;
372 if (tvItem
->nextSibling
)
373 return tvItem
->nextSibling
;
379 /***************************************************************************
380 * This method returns the nth item starting at the given item. It returns
381 * the last item (or first) we we run out of items.
383 * Will scroll backward if count is <0.
384 * forward if count is >0.
386 static TREEVIEW_ITEM
*
387 TREEVIEW_GetListItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
390 TREEVIEW_ITEM
*(*next_item
)(TREEVIEW_INFO
*, TREEVIEW_ITEM
*);
391 TREEVIEW_ITEM
*previousItem
;
393 assert(wineItem
!= NULL
);
397 next_item
= TREEVIEW_GetNextListItem
;
402 next_item
= TREEVIEW_GetPrevListItem
;
409 previousItem
= wineItem
;
410 wineItem
= next_item(infoPtr
, wineItem
);
412 } while (--count
&& wineItem
!= NULL
);
415 return wineItem
? wineItem
: previousItem
;
418 /* Notifications ************************************************************/
420 static INT
get_notifycode(TREEVIEW_INFO
*infoPtr
, INT code
)
422 if (!infoPtr
->bNtfUnicode
) {
424 case TVN_SELCHANGINGW
: return TVN_SELCHANGINGA
;
425 case TVN_SELCHANGEDW
: return TVN_SELCHANGEDA
;
426 case TVN_GETDISPINFOW
: return TVN_GETDISPINFOA
;
427 case TVN_SETDISPINFOW
: return TVN_SETDISPINFOA
;
428 case TVN_ITEMEXPANDINGW
: return TVN_ITEMEXPANDINGA
;
429 case TVN_ITEMEXPANDEDW
: return TVN_ITEMEXPANDEDA
;
430 case TVN_BEGINDRAGW
: return TVN_BEGINDRAGA
;
431 case TVN_BEGINRDRAGW
: return TVN_BEGINRDRAGA
;
432 case TVN_DELETEITEMW
: return TVN_DELETEITEMA
;
433 case TVN_BEGINLABELEDITW
: return TVN_BEGINLABELEDITA
;
434 case TVN_ENDLABELEDITW
: return TVN_ENDLABELEDITA
;
435 case TVN_GETINFOTIPW
: return TVN_GETINFOTIPA
;
442 TREEVIEW_SendRealNotify(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
444 TRACE("wParam=%d, lParam=%ld\n", wParam
, lParam
);
445 return SendMessageW(infoPtr
->hwndNotify
, WM_NOTIFY
, wParam
, lParam
);
449 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO
*infoPtr
, UINT code
)
452 HWND hwnd
= infoPtr
->hwnd
;
455 nmhdr
.hwndFrom
= hwnd
;
456 nmhdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
457 nmhdr
.code
= get_notifycode(infoPtr
, code
);
459 return (BOOL
)TREEVIEW_SendRealNotify(infoPtr
,
460 (WPARAM
)nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
464 TREEVIEW_TVItemFromItem(TREEVIEW_INFO
*infoPtr
, UINT mask
, TVITEMA
*tvItem
, TREEVIEW_ITEM
*item
)
467 tvItem
->hItem
= item
;
468 tvItem
->state
= item
->state
;
469 tvItem
->stateMask
= 0;
470 tvItem
->iImage
= item
->iImage
;
471 tvItem
->cchTextMax
= item
->cchTextMax
;
472 tvItem
->iImage
= item
->iImage
;
473 tvItem
->iSelectedImage
= item
->iSelectedImage
;
474 tvItem
->cChildren
= item
->cChildren
;
475 tvItem
->lParam
= item
->lParam
;
477 /* **** **** **** **** WARNING **** **** **** **** */
478 /* This control stores all the data in A format */
479 /* we will convert it to W if the notify format */
481 /* **** **** **** **** WARNING **** **** **** **** */
482 if (infoPtr
->bNtfUnicode
) {
483 INT len
= MultiByteToWideChar( CP_ACP
, 0, item
->pszText
, -1, NULL
, 0 );
485 tvItem
->pszText
= (LPSTR
)COMCTL32_Alloc (len
*sizeof(WCHAR
));
486 MultiByteToWideChar( CP_ACP
, 0, item
->pszText
, -1, (LPWSTR
)tvItem
->pszText
, len
*sizeof(WCHAR
) );
490 tvItem
->pszText
= item
->pszText
;
494 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO
*infoPtr
, UINT code
, UINT action
,
495 UINT mask
, HTREEITEM oldItem
, HTREEITEM newItem
)
497 HWND hwnd
= infoPtr
->hwnd
;
501 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
502 code
, action
, oldItem
, newItem
);
504 ZeroMemory(&nmhdr
, sizeof(NMTREEVIEWA
));
506 nmhdr
.hdr
.hwndFrom
= hwnd
;
507 nmhdr
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
508 nmhdr
.hdr
.code
= get_notifycode(infoPtr
, code
);
509 nmhdr
.action
= action
;
512 TREEVIEW_TVItemFromItem(infoPtr
, mask
, &nmhdr
.itemOld
, oldItem
);
515 TREEVIEW_TVItemFromItem(infoPtr
, mask
, &nmhdr
.itemNew
, newItem
);
520 ret
= (BOOL
)TREEVIEW_SendRealNotify(infoPtr
,
521 (WPARAM
)GetWindowLongA(hwnd
, GWL_ID
),
523 if (infoPtr
->bNtfUnicode
) {
524 COMCTL32_Free(nmhdr
.itemOld
.pszText
);
525 COMCTL32_Free(nmhdr
.itemNew
.pszText
);
531 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO
*infoPtr
, UINT code
,
532 HTREEITEM dragItem
, POINT pt
)
534 HWND hwnd
= infoPtr
->hwnd
;
537 TRACE("code:%d dragitem:%p\n", code
, dragItem
);
539 nmhdr
.hdr
.hwndFrom
= hwnd
;
540 nmhdr
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
541 nmhdr
.hdr
.code
= get_notifycode(infoPtr
, code
);
543 nmhdr
.itemNew
.mask
= TVIF_STATE
| TVIF_PARAM
| TVIF_HANDLE
;
544 nmhdr
.itemNew
.hItem
= dragItem
;
545 nmhdr
.itemNew
.state
= dragItem
->state
;
546 nmhdr
.itemNew
.lParam
= dragItem
->lParam
;
548 nmhdr
.ptDrag
.x
= pt
.x
;
549 nmhdr
.ptDrag
.y
= pt
.y
;
551 return (BOOL
)TREEVIEW_SendRealNotify(infoPtr
,
552 (WPARAM
)GetWindowLongA(hwnd
, GWL_ID
),
558 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO
*infoPtr
, DWORD dwDrawStage
,
561 HWND hwnd
= infoPtr
->hwnd
;
562 NMTVCUSTOMDRAW nmcdhdr
;
565 TRACE("drawstage:%lx hdc:%p\n", dwDrawStage
, hdc
);
567 nmcd
= &nmcdhdr
.nmcd
;
568 nmcd
->hdr
.hwndFrom
= hwnd
;
569 nmcd
->hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
570 nmcd
->hdr
.code
= NM_CUSTOMDRAW
;
571 nmcd
->dwDrawStage
= dwDrawStage
;
574 nmcd
->dwItemSpec
= 0;
575 nmcd
->uItemState
= 0;
576 nmcd
->lItemlParam
= 0;
577 nmcdhdr
.clrText
= infoPtr
->clrText
;
578 nmcdhdr
.clrTextBk
= infoPtr
->clrBk
;
581 return (BOOL
)TREEVIEW_SendRealNotify(infoPtr
,
582 (WPARAM
)GetWindowLongA(hwnd
, GWL_ID
),
588 /* FIXME: need to find out when the flags in uItemState need to be set */
591 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO
*infoPtr
, HDC hdc
,
592 TREEVIEW_ITEM
*wineItem
, UINT uItemDrawState
)
594 HWND hwnd
= infoPtr
->hwnd
;
595 NMTVCUSTOMDRAW nmcdhdr
;
597 DWORD dwDrawStage
, dwItemSpec
;
601 dwDrawStage
= CDDS_ITEM
| uItemDrawState
;
602 dwItemSpec
= (DWORD
)wineItem
;
604 if (wineItem
->state
& TVIS_SELECTED
)
605 uItemState
|= CDIS_SELECTED
;
606 if (wineItem
== infoPtr
->selectedItem
)
607 uItemState
|= CDIS_FOCUS
;
608 if (wineItem
== infoPtr
->hotItem
)
609 uItemState
|= CDIS_HOT
;
611 nmcd
= &nmcdhdr
.nmcd
;
612 nmcd
->hdr
.hwndFrom
= hwnd
;
613 nmcd
->hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
614 nmcd
->hdr
.code
= NM_CUSTOMDRAW
;
615 nmcd
->dwDrawStage
= dwDrawStage
;
617 nmcd
->rc
= wineItem
->rect
;
618 nmcd
->dwItemSpec
= dwItemSpec
;
619 nmcd
->uItemState
= uItemState
;
620 nmcd
->lItemlParam
= wineItem
->lParam
;
621 nmcdhdr
.clrText
= infoPtr
->clrText
;
622 nmcdhdr
.clrTextBk
= infoPtr
->clrBk
;
623 nmcdhdr
.iLevel
= wineItem
->iLevel
;
625 TRACE("drawstage:%lx hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
626 nmcd
->dwDrawStage
, nmcd
->hdc
, nmcd
->dwItemSpec
,
627 nmcd
->uItemState
, nmcd
->lItemlParam
);
629 retval
= TREEVIEW_SendRealNotify(infoPtr
,
630 (WPARAM
)GetWindowLongA(hwnd
, GWL_ID
),
633 infoPtr
->clrText
= nmcdhdr
.clrText
;
634 infoPtr
->clrBk
= nmcdhdr
.clrTextBk
;
639 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*editItem
)
641 HWND hwnd
= infoPtr
->hwnd
;
642 LPSTR allocated
= NULL
;
646 tvdi
.hdr
.hwndFrom
= hwnd
;
647 tvdi
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
648 tvdi
.hdr
.code
= get_notifycode(infoPtr
, TVN_BEGINLABELEDITW
);
650 tvdi
.item
.mask
= TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
| TVIF_TEXT
;
651 tvdi
.item
.hItem
= editItem
;
652 tvdi
.item
.state
= editItem
->state
;
653 tvdi
.item
.lParam
= editItem
->lParam
;
654 if (infoPtr
->bNtfUnicode
) {
655 INT len
= MultiByteToWideChar( CP_ACP
, 0, editItem
->pszText
, -1, NULL
, 0 );
657 tvdi
.item
.pszText
= allocated
= (LPSTR
)COMCTL32_Alloc (len
*sizeof(WCHAR
));
658 MultiByteToWideChar( CP_ACP
, 0, editItem
->pszText
, -1, (LPWSTR
)tvdi
.item
.pszText
, len
*sizeof(WCHAR
) );
659 tvdi
.item
.cchTextMax
= len
*sizeof(WCHAR
);
662 tvdi
.item
.pszText
= editItem
->pszText
; /* ??? */
663 tvdi
.item
.cchTextMax
= editItem
->cchTextMax
; /* ??? */
667 tvdi
.item
.pszText
= editItem
->pszText
;
668 tvdi
.item
.cchTextMax
= editItem
->cchTextMax
;
671 ret
= (BOOL
)TREEVIEW_SendRealNotify(infoPtr
,
675 COMCTL32_Free(allocated
);
680 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
683 NMTVDISPINFOA callback
;
684 HWND hwnd
= infoPtr
->hwnd
;
686 mask
&= wineItem
->callbackMask
;
688 if (mask
== 0) return;
690 callback
.hdr
.hwndFrom
= hwnd
;
691 callback
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
692 callback
.hdr
.code
= get_notifycode(infoPtr
, TVN_GETDISPINFOW
);
694 /* 'state' always contains valid value, as well as 'lParam'.
695 * All other parameters are uninitialized.
697 callback
.item
.pszText
= wineItem
->pszText
;
698 callback
.item
.cchTextMax
= wineItem
->cchTextMax
;
699 callback
.item
.mask
= mask
;
700 callback
.item
.hItem
= wineItem
;
701 callback
.item
.state
= wineItem
->state
;
702 callback
.item
.lParam
= wineItem
->lParam
;
704 /* If text is changed we need to recalculate textWidth */
705 if (mask
& TVIF_TEXT
)
706 wineItem
->textWidth
= 0;
708 TREEVIEW_SendRealNotify(infoPtr
,
709 (WPARAM
)callback
.hdr
.idFrom
, (LPARAM
)&callback
);
711 /* It may have changed due to a call to SetItem. */
712 mask
&= wineItem
->callbackMask
;
714 if ((mask
& TVIF_TEXT
) && callback
.item
.pszText
!= wineItem
->pszText
)
716 /* Instead of copying text into our buffer user specified its own */
717 if (infoPtr
->bNtfUnicode
) {
720 int len
= WideCharToMultiByte( CP_ACP
, 0,
721 (LPWSTR
)callback
.item
.pszText
, -1,
722 NULL
, 0, NULL
, NULL
);
723 buflen
= max((len
+1)*sizeof(WCHAR
), TEXT_CALLBACK_SIZE
);
724 newText
= (LPWSTR
)COMCTL32_ReAlloc(wineItem
->pszText
, buflen
);
726 TRACE("returned wstr %s, len=%d, buflen=%d\n",
727 debugstr_w((LPWSTR
)callback
.item
.pszText
), len
, buflen
);
731 wineItem
->pszText
= (LPSTR
)newText
;
732 WideCharToMultiByte( CP_ACP
, 0,
733 (LPWSTR
)callback
.item
.pszText
, -1,
734 wineItem
->pszText
, buflen
,
736 wineItem
->cchTextMax
= buflen
;
738 /* If ReAlloc fails we have nothing to do, but keep original text */
741 int len
= max(lstrlenA(callback
.item
.pszText
) + 1,
743 LPSTR newText
= COMCTL32_ReAlloc(wineItem
->pszText
, len
);
745 TRACE("returned str %s, len=%d\n",
746 debugstr_a(callback
.item
.pszText
), len
);
750 wineItem
->pszText
= newText
;
751 strcpy(wineItem
->pszText
, callback
.item
.pszText
);
752 wineItem
->cchTextMax
= len
;
754 /* If ReAlloc fails we have nothing to do, but keep original text */
757 else if (mask
& TVIF_TEXT
) {
758 /* User put text into our buffer, that is ok unless W string */
759 if (infoPtr
->bNtfUnicode
) {
761 LPSTR oldText
= NULL
;
763 int len
= WideCharToMultiByte( CP_ACP
, 0,
764 (LPWSTR
)callback
.item
.pszText
, -1,
765 NULL
, 0, NULL
, NULL
);
766 buflen
= max((len
+1)*sizeof(WCHAR
), TEXT_CALLBACK_SIZE
);
767 newText
= (LPWSTR
)COMCTL32_Alloc(buflen
);
769 TRACE("same buffer wstr %s, len=%d, buflen=%d\n",
770 debugstr_w((LPWSTR
)callback
.item
.pszText
), len
, buflen
);
774 oldText
= wineItem
->pszText
;
775 wineItem
->pszText
= (LPSTR
)newText
;
776 WideCharToMultiByte( CP_ACP
, 0,
777 (LPWSTR
)callback
.item
.pszText
, -1,
778 wineItem
->pszText
, buflen
, NULL
, NULL
);
779 wineItem
->cchTextMax
= buflen
;
781 COMCTL32_Free(oldText
);
786 if (mask
& TVIF_IMAGE
)
787 wineItem
->iImage
= callback
.item
.iImage
;
789 if (mask
& TVIF_SELECTEDIMAGE
)
790 wineItem
->iSelectedImage
= callback
.item
.iSelectedImage
;
792 if (mask
& TVIF_CHILDREN
)
793 wineItem
->cChildren
= callback
.item
.cChildren
;
795 /* These members are now permanently set. */
796 if (callback
.item
.mask
& TVIF_DI_SETITEM
)
797 wineItem
->callbackMask
&= ~callback
.item
.mask
;
800 /***************************************************************************
801 * This function uses cChildren field to decide whether the item has
803 * Note: if this returns TRUE, the child items may not actually exist,
804 * they could be virtual.
806 * Just use wineItem->firstChild to check for physical children.
809 TREEVIEW_HasChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
)
811 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, TVIF_CHILDREN
);
813 return wineItem
->cChildren
> 0;
817 /* Item Position ********************************************************/
819 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
821 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO
*infoPtr
,
824 /* Same effect, different optimisation. */
826 BOOL lar
= ((infoPtr
->dwStyle
& TVS_LINESATROOT
)
827 && (infoPtr
->dwStyle
& (TVS_HASLINES
|TVS_HASBUTTONS
)));
829 BOOL lar
= ((infoPtr
->dwStyle
830 & (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
834 item
->linesOffset
= infoPtr
->uIndent
* (item
->iLevel
+ lar
- 1)
836 item
->stateOffset
= item
->linesOffset
+ infoPtr
->uIndent
;
837 item
->imageOffset
= item
->stateOffset
838 + (STATEIMAGEINDEX(item
->state
) ? infoPtr
->stateImageWidth
: 0);
839 item
->textOffset
= item
->imageOffset
+ infoPtr
->normalImageWidth
;
843 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
, HDC hDC
)
849 /* DRAW's OM docker creates items like this */
850 if (item
->pszText
== NULL
)
856 if (item
->textWidth
!= 0 && !(item
->callbackMask
& TVIF_TEXT
))
865 hdc
= GetDC(infoPtr
->hwnd
);
866 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, item
));
869 GetTextExtentPoint32A(hdc
, item
->pszText
, strlen(item
->pszText
), &sz
);
870 item
->textWidth
= sz
.cx
;
874 SelectObject(hdc
, hOldFont
);
880 TREEVIEW_ComputeItemRect(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
882 item
->rect
.top
= infoPtr
->uItemHeight
*
883 (item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
);
885 item
->rect
.bottom
= item
->rect
.top
886 + infoPtr
->uItemHeight
* item
->iIntegral
- 1;
889 item
->rect
.right
= infoPtr
->clientWidth
;
892 /* We know that only items after start need their order updated. */
894 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*start
)
901 start
= infoPtr
->root
->firstChild
;
905 order
= start
->visibleOrder
;
907 for (item
= start
; item
!= NULL
;
908 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
910 item
->visibleOrder
= order
;
911 order
+= item
->iIntegral
;
914 infoPtr
->maxVisibleOrder
= order
;
916 for (item
= start
; item
!= NULL
;
917 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
919 TREEVIEW_ComputeItemRect(infoPtr
, item
);
924 /* Update metrics of all items in selected subtree.
925 * root must be expanded
928 TREEVIEW_UpdateSubTree(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*root
)
930 TREEVIEW_ITEM
*sibling
;
934 if (!root
->firstChild
|| !(root
->state
& TVIS_EXPANDED
))
937 root
->state
&= ~TVIS_EXPANDED
;
938 sibling
= TREEVIEW_GetNextListItem(infoPtr
, root
);
939 root
->state
|= TVIS_EXPANDED
;
941 hdc
= GetDC(infoPtr
->hwnd
);
942 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
944 for (; root
!= sibling
;
945 root
= TREEVIEW_GetNextListItem(infoPtr
, root
))
947 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, root
);
949 if (root
->callbackMask
& TVIF_TEXT
)
950 TREEVIEW_UpdateDispInfo(infoPtr
, root
, TVIF_TEXT
);
952 if (root
->textWidth
== 0)
954 SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, root
));
955 TREEVIEW_ComputeTextWidth(infoPtr
, root
, hdc
);
959 SelectObject(hdc
, hOldFont
);
960 ReleaseDC(infoPtr
->hwnd
, hdc
);
963 /* Item Allocation **********************************************************/
965 static TREEVIEW_ITEM
*
966 TREEVIEW_AllocateItem(TREEVIEW_INFO
*infoPtr
)
968 TREEVIEW_ITEM
*newItem
= COMCTL32_Alloc(sizeof(TREEVIEW_ITEM
));
973 if (DPA_InsertPtr(infoPtr
->items
, INT_MAX
, newItem
) == -1)
975 COMCTL32_Free(newItem
);
982 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
983 * free item->pszText. */
985 TREEVIEW_FreeItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
987 DPA_DeletePtr(infoPtr
->items
, DPA_GetPtrIndex(infoPtr
->items
, item
));
989 if (infoPtr
->selectedItem
== item
)
990 infoPtr
->selectedItem
= NULL
;
994 /* Item Insertion *******************************************************/
996 /***************************************************************************
997 * This method inserts newItem before sibling as a child of parent.
998 * sibling can be NULL, but only if parent has no children.
1001 TREEVIEW_InsertBefore(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
1002 TREEVIEW_ITEM
*parent
)
1004 assert(newItem
!= NULL
);
1005 assert(parent
!= NULL
);
1007 if (sibling
!= NULL
)
1009 assert(sibling
->parent
== parent
);
1011 if (sibling
->prevSibling
!= NULL
)
1012 sibling
->prevSibling
->nextSibling
= newItem
;
1014 newItem
->prevSibling
= sibling
->prevSibling
;
1015 sibling
->prevSibling
= newItem
;
1018 newItem
->prevSibling
= NULL
;
1020 newItem
->nextSibling
= sibling
;
1022 if (parent
->firstChild
== sibling
)
1023 parent
->firstChild
= newItem
;
1025 if (parent
->lastChild
== NULL
)
1026 parent
->lastChild
= newItem
;
1029 /***************************************************************************
1030 * This method inserts newItem after sibling as a child of parent.
1031 * sibling can be NULL, but only if parent has no children.
1034 TREEVIEW_InsertAfter(TREEVIEW_ITEM
*newItem
, TREEVIEW_ITEM
*sibling
,
1035 TREEVIEW_ITEM
*parent
)
1037 assert(newItem
!= NULL
);
1038 assert(parent
!= NULL
);
1040 if (sibling
!= NULL
)
1042 assert(sibling
->parent
== parent
);
1044 if (sibling
->nextSibling
!= NULL
)
1045 sibling
->nextSibling
->prevSibling
= newItem
;
1047 newItem
->nextSibling
= sibling
->nextSibling
;
1048 sibling
->nextSibling
= newItem
;
1051 newItem
->nextSibling
= NULL
;
1053 newItem
->prevSibling
= sibling
;
1055 if (parent
->lastChild
== sibling
)
1056 parent
->lastChild
= newItem
;
1058 if (parent
->firstChild
== NULL
)
1059 parent
->firstChild
= newItem
;
1063 TREEVIEW_DoSetItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
1064 const TVITEMEXA
*tvItem
)
1066 UINT callbackClear
= 0;
1067 UINT callbackSet
= 0;
1069 /* Do this first in case it fails. */
1070 if (tvItem
->mask
& TVIF_TEXT
)
1072 wineItem
->textWidth
= 0; /* force width recalculation */
1073 if (tvItem
->pszText
!= LPSTR_TEXTCALLBACKA
)
1075 int len
= lstrlenA(tvItem
->pszText
) + 1;
1076 LPSTR newText
= COMCTL32_ReAlloc(wineItem
->pszText
, len
);
1078 if (newText
== NULL
) return FALSE
;
1080 callbackClear
|= TVIF_TEXT
;
1082 wineItem
->pszText
= newText
;
1083 wineItem
->cchTextMax
= len
;
1084 lstrcpynA(wineItem
->pszText
, tvItem
->pszText
, len
);
1085 TRACE("setting text %s, item %p\n",
1086 debugstr_a(wineItem
->pszText
), wineItem
);
1090 callbackSet
|= TVIF_TEXT
;
1092 wineItem
->pszText
= COMCTL32_ReAlloc(wineItem
->pszText
,
1093 TEXT_CALLBACK_SIZE
);
1094 wineItem
->cchTextMax
= TEXT_CALLBACK_SIZE
;
1095 TRACE("setting callback, item %p\n",
1100 if (tvItem
->mask
& TVIF_CHILDREN
)
1102 wineItem
->cChildren
= tvItem
->cChildren
;
1104 if (wineItem
->cChildren
== I_CHILDRENCALLBACK
)
1105 callbackSet
|= TVIF_CHILDREN
;
1107 callbackClear
|= TVIF_CHILDREN
;
1110 if (tvItem
->mask
& TVIF_IMAGE
)
1112 wineItem
->iImage
= tvItem
->iImage
;
1114 if (wineItem
->iImage
== I_IMAGECALLBACK
)
1115 callbackSet
|= TVIF_IMAGE
;
1117 callbackClear
|= TVIF_IMAGE
;
1120 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
1122 wineItem
->iSelectedImage
= tvItem
->iSelectedImage
;
1124 if (wineItem
->iSelectedImage
== I_IMAGECALLBACK
)
1125 callbackSet
|= TVIF_SELECTEDIMAGE
;
1127 callbackClear
|= TVIF_SELECTEDIMAGE
;
1130 if (tvItem
->mask
& TVIF_PARAM
)
1131 wineItem
->lParam
= tvItem
->lParam
;
1133 /* If the application sets TVIF_INTEGRAL without
1134 * supplying a TVITEMEX structure, it's toast. */
1135 if (tvItem
->mask
& TVIF_INTEGRAL
)
1136 wineItem
->iIntegral
= tvItem
->iIntegral
;
1138 if (tvItem
->mask
& TVIF_STATE
)
1140 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem
->state
, tvItem
->state
,
1142 wineItem
->state
&= ~tvItem
->stateMask
;
1143 wineItem
->state
|= (tvItem
->state
& tvItem
->stateMask
);
1146 wineItem
->callbackMask
|= callbackSet
;
1147 wineItem
->callbackMask
&= ~callbackClear
;
1152 /* Note that the new item is pre-zeroed. */
1154 TREEVIEW_InsertItemA(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
1156 const TVINSERTSTRUCTA
*ptdi
= (LPTVINSERTSTRUCTA
) lParam
;
1157 const TVITEMEXA
*tvItem
= &ptdi
->DUMMYUNIONNAME
.itemex
;
1158 HTREEITEM insertAfter
;
1159 TREEVIEW_ITEM
*newItem
, *parentItem
;
1160 BOOL bTextUpdated
= FALSE
;
1162 if (ptdi
->hParent
== TVI_ROOT
|| ptdi
->hParent
== 0)
1164 parentItem
= infoPtr
->root
;
1168 parentItem
= ptdi
->hParent
;
1170 if (!TREEVIEW_ValidItem(infoPtr
, parentItem
))
1172 WARN("invalid parent %p\n", parentItem
);
1173 return (LRESULT
)(HTREEITEM
)NULL
;
1177 insertAfter
= ptdi
->hInsertAfter
;
1179 /* Validate this now for convenience. */
1180 switch ((DWORD
)insertAfter
)
1182 case (DWORD
)TVI_FIRST
:
1183 case (DWORD
)TVI_LAST
:
1184 case (DWORD
)TVI_SORT
:
1188 if (!TREEVIEW_ValidItem(infoPtr
, insertAfter
) ||
1189 insertAfter
->parent
!= parentItem
)
1191 WARN("invalid insert after %p\n", insertAfter
);
1192 insertAfter
= TVI_LAST
;
1196 TRACE("parent %p position %p: '%s'\n", parentItem
, insertAfter
,
1197 (tvItem
->mask
& TVIF_TEXT
)
1198 ? ((tvItem
->pszText
== LPSTR_TEXTCALLBACKA
) ? "<callback>"
1202 newItem
= TREEVIEW_AllocateItem(infoPtr
);
1203 if (newItem
== NULL
)
1204 return (LRESULT
)(HTREEITEM
)NULL
;
1206 newItem
->parent
= parentItem
;
1207 newItem
->iIntegral
= 1;
1209 if (!TREEVIEW_DoSetItem(infoPtr
, newItem
, tvItem
))
1210 return (LRESULT
)(HTREEITEM
)NULL
;
1212 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1214 infoPtr
->uNumItems
++;
1216 switch ((DWORD
)insertAfter
)
1218 case (DWORD
)TVI_FIRST
:
1219 TREEVIEW_InsertBefore(newItem
, parentItem
->firstChild
, parentItem
);
1220 if (infoPtr
->firstVisible
== parentItem
->firstChild
)
1221 TREEVIEW_SetFirstVisible(infoPtr
, newItem
, TRUE
);
1224 case (DWORD
)TVI_LAST
:
1225 TREEVIEW_InsertAfter(newItem
, parentItem
->lastChild
, parentItem
);
1228 /* hInsertAfter names a specific item we want to insert after */
1230 TREEVIEW_InsertAfter(newItem
, insertAfter
, insertAfter
->parent
);
1233 case (DWORD
)TVI_SORT
:
1235 TREEVIEW_ITEM
*aChild
;
1236 TREEVIEW_ITEM
*previousChild
= NULL
;
1237 BOOL bItemInserted
= FALSE
;
1239 aChild
= parentItem
->firstChild
;
1241 bTextUpdated
= TRUE
;
1242 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1244 /* Iterate the parent children to see where we fit in */
1245 while (aChild
!= NULL
)
1249 TREEVIEW_UpdateDispInfo(infoPtr
, aChild
, TVIF_TEXT
);
1250 comp
= lstrcmpA(newItem
->pszText
, aChild
->pszText
);
1252 if (comp
< 0) /* we are smaller than the current one */
1254 TREEVIEW_InsertBefore(newItem
, aChild
, parentItem
);
1255 bItemInserted
= TRUE
;
1258 else if (comp
> 0) /* we are bigger than the current one */
1260 previousChild
= aChild
;
1262 /* This will help us to exit if there is no more sibling */
1263 aChild
= (aChild
->nextSibling
== 0)
1265 : aChild
->nextSibling
;
1267 /* Look at the next item */
1273 * An item with this name is already existing, therefore,
1274 * we add after the one we found
1276 TREEVIEW_InsertAfter(newItem
, aChild
, parentItem
);
1277 bItemInserted
= TRUE
;
1283 * we reach the end of the child list and the item has not
1284 * yet been inserted, therefore, insert it after the last child.
1286 if ((!bItemInserted
) && (aChild
== NULL
))
1287 TREEVIEW_InsertAfter(newItem
, previousChild
, parentItem
);
1294 TRACE("new item %p; parent %p, mask %x\n", newItem
,
1295 newItem
->parent
, tvItem
->mask
);
1297 newItem
->iLevel
= newItem
->parent
->iLevel
+ 1;
1299 if (newItem
->parent
->cChildren
== 0)
1300 newItem
->parent
->cChildren
= 1;
1302 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
1304 if (STATEIMAGEINDEX(newItem
->state
) == 0)
1305 newItem
->state
|= INDEXTOSTATEIMAGEMASK(1);
1308 if (infoPtr
->firstVisible
== NULL
)
1309 infoPtr
->firstVisible
= newItem
;
1311 TREEVIEW_VerifyTree(infoPtr
);
1313 if (parentItem
== infoPtr
->root
||
1314 (ISVISIBLE(parentItem
) && parentItem
->state
& TVIS_EXPANDED
))
1316 TREEVIEW_ITEM
*item
;
1317 TREEVIEW_ITEM
*prev
= TREEVIEW_GetPrevListItem(infoPtr
, newItem
);
1319 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1320 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, newItem
);
1323 TREEVIEW_UpdateDispInfo(infoPtr
, newItem
, TVIF_TEXT
);
1325 TREEVIEW_ComputeTextWidth(infoPtr
, newItem
, 0);
1326 TREEVIEW_UpdateScrollBars(infoPtr
);
1328 * if the item was inserted in a visible part of the tree,
1329 * invalidate it, as well as those after it
1331 for (item
= newItem
;
1333 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
1334 TREEVIEW_Invalidate(infoPtr
, item
);
1338 newItem
->visibleOrder
= -1;
1340 /* refresh treeview if newItem is the first item inserted under parentItem */
1341 if (ISVISIBLE(parentItem
) && newItem
->prevSibling
== newItem
->nextSibling
)
1343 /* parent got '+' - update it */
1344 TREEVIEW_Invalidate(infoPtr
, parentItem
);
1348 return (LRESULT
)newItem
;
1353 TREEVIEW_InsertItemW(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
1355 TVINSERTSTRUCTW
*tvisW
;
1356 TVINSERTSTRUCTA tvisA
;
1359 tvisW
= (LPTVINSERTSTRUCTW
) lParam
;
1361 tvisA
.hParent
= tvisW
->hParent
;
1362 tvisA
.hInsertAfter
= tvisW
->hInsertAfter
;
1364 tvisA
.DUMMYUNIONNAME
.item
.mask
= tvisW
->DUMMYUNIONNAME
.item
.mask
;
1365 tvisA
.DUMMYUNIONNAME
.item
.hItem
= tvisW
->DUMMYUNIONNAME
.item
.hItem
;
1366 tvisA
.DUMMYUNIONNAME
.item
.state
= tvisW
->DUMMYUNIONNAME
.item
.state
;
1367 tvisA
.DUMMYUNIONNAME
.item
.stateMask
= tvisW
->DUMMYUNIONNAME
.item
.stateMask
;
1368 tvisA
.DUMMYUNIONNAME
.item
.cchTextMax
=
1369 tvisW
->DUMMYUNIONNAME
.item
.cchTextMax
;
1371 if (tvisW
->DUMMYUNIONNAME
.item
.pszText
)
1373 if (tvisW
->DUMMYUNIONNAME
.item
.pszText
!= LPSTR_TEXTCALLBACKW
)
1375 int len
= WideCharToMultiByte( CP_ACP
, 0, tvisW
->DUMMYUNIONNAME
.item
.pszText
, -1,
1376 NULL
, 0, NULL
, NULL
);
1377 tvisA
.DUMMYUNIONNAME
.item
.pszText
= COMCTL32_Alloc(len
);
1378 WideCharToMultiByte( CP_ACP
, 0, tvisW
->DUMMYUNIONNAME
.item
.pszText
, -1,
1379 tvisA
.DUMMYUNIONNAME
.item
.pszText
, len
, NULL
, NULL
);
1383 tvisA
.DUMMYUNIONNAME
.item
.pszText
= LPSTR_TEXTCALLBACKA
;
1384 tvisA
.DUMMYUNIONNAME
.item
.cchTextMax
= 0;
1388 tvisA
.DUMMYUNIONNAME
.item
.iImage
= tvisW
->DUMMYUNIONNAME
.item
.iImage
;
1389 tvisA
.DUMMYUNIONNAME
.item
.iSelectedImage
=
1390 tvisW
->DUMMYUNIONNAME
.item
.iSelectedImage
;
1391 tvisA
.DUMMYUNIONNAME
.item
.cChildren
= tvisW
->DUMMYUNIONNAME
.item
.cChildren
;
1392 tvisA
.DUMMYUNIONNAME
.item
.lParam
= tvisW
->DUMMYUNIONNAME
.item
.lParam
;
1394 lRes
= TREEVIEW_InsertItemA(infoPtr
, (LPARAM
)&tvisA
);
1396 if (tvisA
.DUMMYUNIONNAME
.item
.pszText
!= LPSTR_TEXTCALLBACKA
)
1398 COMCTL32_Free(tvisA
.DUMMYUNIONNAME
.item
.pszText
);
1406 /* Item Deletion ************************************************************/
1408 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
);
1411 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*parentItem
)
1413 TREEVIEW_ITEM
*kill
= parentItem
->firstChild
;
1415 while (kill
!= NULL
)
1417 TREEVIEW_ITEM
*next
= kill
->nextSibling
;
1419 TREEVIEW_RemoveItem(infoPtr
, kill
);
1424 assert(parentItem
->cChildren
<= 0); /* I_CHILDRENCALLBACK or 0 */
1425 assert(parentItem
->firstChild
== NULL
);
1426 assert(parentItem
->lastChild
== NULL
);
1430 TREEVIEW_UnlinkItem(TREEVIEW_ITEM
*item
)
1432 TREEVIEW_ITEM
*parentItem
= item
->parent
;
1434 assert(item
!= NULL
);
1435 assert(item
->parent
!= NULL
); /* i.e. it must not be the root */
1437 if (parentItem
->firstChild
== item
)
1438 parentItem
->firstChild
= item
->nextSibling
;
1440 if (parentItem
->lastChild
== item
)
1441 parentItem
->lastChild
= item
->prevSibling
;
1443 if (parentItem
->firstChild
== NULL
&& parentItem
->lastChild
== NULL
1444 && parentItem
->cChildren
> 0)
1445 parentItem
->cChildren
= 0;
1447 if (item
->prevSibling
)
1448 item
->prevSibling
->nextSibling
= item
->nextSibling
;
1450 if (item
->nextSibling
)
1451 item
->nextSibling
->prevSibling
= item
->prevSibling
;
1455 TREEVIEW_RemoveItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
)
1457 TRACE("%p, (%s)\n", wineItem
, TREEVIEW_ItemName(wineItem
));
1459 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_DELETEITEMW
, TVC_UNKNOWN
,
1460 TVIF_HANDLE
| TVIF_PARAM
, wineItem
, 0);
1462 if (wineItem
->firstChild
)
1463 TREEVIEW_RemoveAllChildren(infoPtr
, wineItem
);
1465 TREEVIEW_UnlinkItem(wineItem
);
1467 infoPtr
->uNumItems
--;
1469 if (wineItem
->pszText
!= LPSTR_TEXTCALLBACKA
)
1470 COMCTL32_Free(wineItem
->pszText
);
1472 TREEVIEW_FreeItem(infoPtr
, wineItem
);
1476 /* Empty out the tree. */
1478 TREEVIEW_RemoveTree(TREEVIEW_INFO
*infoPtr
)
1480 TREEVIEW_RemoveAllChildren(infoPtr
, infoPtr
->root
);
1482 assert(infoPtr
->uNumItems
== 0); /* root isn't counted in uNumItems */
1486 TREEVIEW_DeleteItem(TREEVIEW_INFO
*infoPtr
, HTREEITEM wineItem
)
1488 TREEVIEW_ITEM
*oldSelection
= infoPtr
->selectedItem
;
1489 TREEVIEW_ITEM
*newSelection
= oldSelection
;
1490 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
1491 TREEVIEW_ITEM
*parent
, *prev
= NULL
;
1492 BOOL visible
= FALSE
;
1494 if (wineItem
== TVI_ROOT
)
1496 TRACE("TVI_ROOT\n");
1497 parent
= infoPtr
->root
;
1498 newSelection
= NULL
;
1500 TREEVIEW_RemoveTree(infoPtr
);
1504 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
1507 TRACE("%p (%s)\n", wineItem
, TREEVIEW_ItemName(wineItem
));
1508 parent
= wineItem
->parent
;
1510 if (ISVISIBLE(wineItem
))
1512 prev
= TREEVIEW_GetPrevListItem(infoPtr
, wineItem
);
1516 if (infoPtr
->selectedItem
!= NULL
1517 && (wineItem
== infoPtr
->selectedItem
1518 || TREEVIEW_IsChildOf(wineItem
, infoPtr
->selectedItem
)))
1520 if (wineItem
->nextSibling
)
1521 newSelection
= wineItem
->nextSibling
;
1522 else if (wineItem
->parent
!= infoPtr
->root
)
1523 newSelection
= wineItem
->parent
;
1526 if (infoPtr
->firstVisible
== wineItem
)
1528 if (wineItem
->nextSibling
)
1529 newFirstVisible
= wineItem
->nextSibling
;
1530 else if (wineItem
->prevSibling
)
1531 newFirstVisible
= wineItem
->prevSibling
;
1532 else if (wineItem
->parent
!= infoPtr
->root
)
1533 newFirstVisible
= wineItem
->parent
;
1536 newFirstVisible
= infoPtr
->firstVisible
;
1538 TREEVIEW_RemoveItem(infoPtr
, wineItem
);
1541 /* Don't change if somebody else already has. */
1542 if (oldSelection
== infoPtr
->selectedItem
)
1544 if (TREEVIEW_ValidItem(infoPtr
, newSelection
))
1545 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
, TVC_UNKNOWN
);
1547 infoPtr
->selectedItem
= 0;
1550 /* Validate insertMark dropItem.
1551 * hotItem ??? - used for comparison only.
1553 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->insertMarkItem
))
1554 infoPtr
->insertMarkItem
= 0;
1556 if (!TREEVIEW_ValidItem(infoPtr
, infoPtr
->dropItem
))
1557 infoPtr
->dropItem
= 0;
1559 if (!TREEVIEW_ValidItem(infoPtr
, newFirstVisible
))
1560 newFirstVisible
= infoPtr
->root
->firstChild
;
1562 TREEVIEW_VerifyTree(infoPtr
);
1567 TREEVIEW_RecalculateVisibleOrder(infoPtr
, prev
);
1568 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
1569 TREEVIEW_UpdateScrollBars(infoPtr
);
1570 TREEVIEW_Invalidate(infoPtr
, NULL
);
1572 else if (ISVISIBLE(parent
) && !TREEVIEW_HasChildren(infoPtr
, parent
))
1574 /* parent lost '+/-' - update it */
1575 TREEVIEW_Invalidate(infoPtr
, parent
);
1582 /* Get/Set Messages *********************************************************/
1584 TREEVIEW_SetRedraw(TREEVIEW_INFO
* infoPtr
, WPARAM wParam
, LPARAM lParam
)
1587 infoPtr
->bRedraw
= TRUE
;
1589 infoPtr
->bRedraw
= FALSE
;
1595 TREEVIEW_GetIndent(TREEVIEW_INFO
*infoPtr
)
1598 return infoPtr
->uIndent
;
1602 TREEVIEW_SetIndent(TREEVIEW_INFO
*infoPtr
, UINT newIndent
)
1606 if (newIndent
< MINIMUM_INDENT
)
1607 newIndent
= MINIMUM_INDENT
;
1609 if (infoPtr
->uIndent
!= newIndent
)
1611 infoPtr
->uIndent
= newIndent
;
1612 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1613 TREEVIEW_UpdateScrollBars(infoPtr
);
1614 TREEVIEW_Invalidate(infoPtr
, NULL
);
1622 TREEVIEW_GetToolTips(TREEVIEW_INFO
*infoPtr
)
1625 return (LRESULT
)infoPtr
->hwndToolTip
;
1629 TREEVIEW_SetToolTips(TREEVIEW_INFO
*infoPtr
, HWND hwndTT
)
1634 prevToolTip
= infoPtr
->hwndToolTip
;
1635 infoPtr
->hwndToolTip
= hwndTT
;
1637 return (LRESULT
)prevToolTip
;
1642 TREEVIEW_GetScrollTime(TREEVIEW_INFO
*infoPtr
)
1644 return infoPtr
->uScrollTime
;
1648 TREEVIEW_SetScrollTime(TREEVIEW_INFO
*infoPtr
, UINT uScrollTime
)
1650 UINT uOldScrollTime
= infoPtr
->uScrollTime
;
1652 infoPtr
->uScrollTime
= min(uScrollTime
, 100);
1654 return uOldScrollTime
;
1659 TREEVIEW_GetImageList(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
1665 case (WPARAM
)TVSIL_NORMAL
:
1666 return (LRESULT
)infoPtr
->himlNormal
;
1668 case (WPARAM
)TVSIL_STATE
:
1669 return (LRESULT
)infoPtr
->himlState
;
1677 TREEVIEW_SetImageList(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, HIMAGELIST himlNew
)
1679 HIMAGELIST himlOld
= 0;
1680 int oldWidth
= infoPtr
->normalImageWidth
;
1681 int oldHeight
= infoPtr
->normalImageHeight
;
1684 TRACE("%x,%p\n", wParam
, himlNew
);
1688 case (WPARAM
)TVSIL_NORMAL
:
1689 himlOld
= infoPtr
->himlNormal
;
1690 infoPtr
->himlNormal
= himlNew
;
1692 if (himlNew
!= NULL
)
1693 ImageList_GetIconSize(himlNew
, &infoPtr
->normalImageWidth
,
1694 &infoPtr
->normalImageHeight
);
1697 infoPtr
->normalImageWidth
= 0;
1698 infoPtr
->normalImageHeight
= 0;
1703 case (WPARAM
)TVSIL_STATE
:
1704 himlOld
= infoPtr
->himlState
;
1705 infoPtr
->himlState
= himlNew
;
1707 if (himlNew
!= NULL
)
1708 ImageList_GetIconSize(himlNew
, &infoPtr
->stateImageWidth
,
1709 &infoPtr
->stateImageHeight
);
1712 infoPtr
->stateImageWidth
= 0;
1713 infoPtr
->stateImageHeight
= 0;
1719 if (oldWidth
!= infoPtr
->normalImageWidth
||
1720 oldHeight
!= infoPtr
->normalImageHeight
)
1722 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1723 TREEVIEW_UpdateScrollBars(infoPtr
);
1726 TREEVIEW_Invalidate(infoPtr
, NULL
);
1728 return (LRESULT
)himlOld
;
1731 /* Compute the natural height (based on the font size) for items. */
1733 TREEVIEW_NaturalHeight(TREEVIEW_INFO
*infoPtr
)
1737 HFONT hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
1739 GetTextMetricsA(hdc
, &tm
);
1741 SelectObject(hdc
, hOldFont
);
1744 /* The 16 is a hack because our fonts are tiny. */
1745 /* add 2 for the focus border and 1 more for margin some apps assume */
1746 return max(16, tm
.tmHeight
+ tm
.tmExternalLeading
+ 3);
1750 TREEVIEW_SetItemHeight(TREEVIEW_INFO
*infoPtr
, INT newHeight
)
1752 INT prevHeight
= infoPtr
->uItemHeight
;
1754 TRACE("%d \n", newHeight
);
1755 if (newHeight
== -1)
1757 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1758 infoPtr
->bHeightSet
= FALSE
;
1762 infoPtr
->uItemHeight
= newHeight
;
1763 infoPtr
->bHeightSet
= TRUE
;
1766 /* Round down, unless we support odd ("non even") heights. */
1767 if (!(infoPtr
->dwStyle
) & TVS_NONEVENHEIGHT
)
1768 infoPtr
->uItemHeight
&= ~1;
1770 if (infoPtr
->uItemHeight
!= prevHeight
)
1772 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1773 TREEVIEW_UpdateScrollBars(infoPtr
);
1774 TREEVIEW_Invalidate(infoPtr
, NULL
);
1781 TREEVIEW_GetItemHeight(TREEVIEW_INFO
*infoPtr
)
1784 return infoPtr
->uItemHeight
;
1789 TREEVIEW_GetFont(TREEVIEW_INFO
*infoPtr
)
1791 TRACE("%p\n", infoPtr
->hFont
);
1792 return (LRESULT
)infoPtr
->hFont
;
1797 TREEVIEW_ResetTextWidth(LPVOID pItem
, DWORD unused
)
1801 ((TREEVIEW_ITEM
*)pItem
)->textWidth
= 0;
1807 TREEVIEW_SetFont(TREEVIEW_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
1809 UINT uHeight
= infoPtr
->uItemHeight
;
1811 TRACE("%p %i\n", hFont
, bRedraw
);
1813 infoPtr
->hFont
= hFont
? hFont
: GetStockObject(SYSTEM_FONT
);
1815 DeleteObject(infoPtr
->hBoldFont
);
1816 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
1818 if (!infoPtr
->bHeightSet
)
1819 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
1821 if (uHeight
!= infoPtr
->uItemHeight
)
1822 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
1824 DPA_EnumCallback(infoPtr
->items
, TREEVIEW_ResetTextWidth
, 0);
1826 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
1827 TREEVIEW_UpdateScrollBars(infoPtr
);
1830 TREEVIEW_Invalidate(infoPtr
, NULL
);
1837 TREEVIEW_GetLineColor(TREEVIEW_INFO
*infoPtr
)
1840 return (LRESULT
)infoPtr
->clrLine
;
1844 TREEVIEW_SetLineColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1846 COLORREF prevColor
= infoPtr
->clrLine
;
1849 infoPtr
->clrLine
= color
;
1850 return (LRESULT
)prevColor
;
1855 TREEVIEW_GetTextColor(TREEVIEW_INFO
*infoPtr
)
1858 return (LRESULT
)infoPtr
->clrText
;
1862 TREEVIEW_SetTextColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1864 COLORREF prevColor
= infoPtr
->clrText
;
1867 infoPtr
->clrText
= color
;
1869 if (infoPtr
->clrText
!= prevColor
)
1870 TREEVIEW_Invalidate(infoPtr
, NULL
);
1872 return (LRESULT
)prevColor
;
1877 TREEVIEW_GetBkColor(TREEVIEW_INFO
*infoPtr
)
1880 return (LRESULT
)infoPtr
->clrBk
;
1884 TREEVIEW_SetBkColor(TREEVIEW_INFO
*infoPtr
, COLORREF newColor
)
1886 COLORREF prevColor
= infoPtr
->clrBk
;
1889 infoPtr
->clrBk
= newColor
;
1891 if (newColor
!= prevColor
)
1892 TREEVIEW_Invalidate(infoPtr
, NULL
);
1894 return (LRESULT
)prevColor
;
1899 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO
*infoPtr
)
1902 return (LRESULT
)infoPtr
->clrInsertMark
;
1906 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO
*infoPtr
, COLORREF color
)
1908 COLORREF prevColor
= infoPtr
->clrInsertMark
;
1910 TRACE("%lx\n", color
);
1911 infoPtr
->clrInsertMark
= color
;
1913 return (LRESULT
)prevColor
;
1918 TREEVIEW_SetInsertMark(TREEVIEW_INFO
*infoPtr
, BOOL wParam
, HTREEITEM item
)
1920 TRACE("%d %p\n", wParam
, item
);
1922 if (!TREEVIEW_ValidItem(infoPtr
, item
))
1925 infoPtr
->insertBeforeorAfter
= wParam
;
1926 infoPtr
->insertMarkItem
= item
;
1928 TREEVIEW_Invalidate(infoPtr
, NULL
);
1934 /************************************************************************
1935 * Some serious braindamage here. lParam is a pointer to both the
1936 * input HTREEITEM and the output RECT.
1939 TREEVIEW_GetItemRect(TREEVIEW_INFO
*infoPtr
, BOOL fTextRect
, LPRECT lpRect
)
1941 TREEVIEW_ITEM
*wineItem
;
1942 const HTREEITEM
*pItem
= (HTREEITEM
*)lpRect
;
1946 * validate parameters
1952 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
) || !ISVISIBLE(wineItem
))
1956 * If wParam is TRUE return the text size otherwise return
1957 * the whole item size
1961 /* Windows does not send TVN_GETDISPINFO here. */
1963 lpRect
->top
= wineItem
->rect
.top
;
1964 lpRect
->bottom
= wineItem
->rect
.bottom
;
1966 lpRect
->left
= wineItem
->textOffset
;
1967 lpRect
->right
= wineItem
->textOffset
+ wineItem
->textWidth
;
1971 *lpRect
= wineItem
->rect
;
1974 TRACE("%s [L:%ld R:%ld T:%ld B:%ld]\n", fTextRect
? "text" : "item",
1975 lpRect
->left
, lpRect
->right
, lpRect
->top
, lpRect
->bottom
);
1980 static inline LRESULT
1981 TREEVIEW_GetVisibleCount(TREEVIEW_INFO
*infoPtr
)
1983 /* Suprise! This does not take integral height into account. */
1984 return infoPtr
->clientHeight
/ infoPtr
->uItemHeight
;
1989 TREEVIEW_GetItemA(TREEVIEW_INFO
*infoPtr
, LPTVITEMEXA tvItem
)
1991 TREEVIEW_ITEM
*wineItem
;
1993 wineItem
= tvItem
->hItem
;
1994 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
1997 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, tvItem
->mask
);
1999 if (tvItem
->mask
& TVIF_CHILDREN
)
2000 tvItem
->cChildren
= wineItem
->cChildren
;
2002 if (tvItem
->mask
& TVIF_HANDLE
)
2003 tvItem
->hItem
= wineItem
;
2005 if (tvItem
->mask
& TVIF_IMAGE
)
2006 tvItem
->iImage
= wineItem
->iImage
;
2008 if (tvItem
->mask
& TVIF_INTEGRAL
)
2009 tvItem
->iIntegral
= wineItem
->iIntegral
;
2011 /* undocumented: windows ignores TVIF_PARAM and
2012 * * always sets lParam
2014 tvItem
->lParam
= wineItem
->lParam
;
2016 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
)
2017 tvItem
->iSelectedImage
= wineItem
->iSelectedImage
;
2019 if (tvItem
->mask
& TVIF_STATE
)
2020 tvItem
->state
= wineItem
->state
& tvItem
->stateMask
;
2022 if (tvItem
->mask
& TVIF_TEXT
)
2023 lstrcpynA(tvItem
->pszText
, wineItem
->pszText
, tvItem
->cchTextMax
);
2025 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2026 wineItem
, tvItem
->pszText
, &tvItem
->iImage
, tvItem
->mask
);
2031 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2032 * which is wrong. */
2034 TREEVIEW_SetItemA(TREEVIEW_INFO
*infoPtr
, LPTVITEMEXA tvItem
)
2036 TREEVIEW_ITEM
*wineItem
;
2037 TREEVIEW_ITEM originalItem
;
2039 wineItem
= tvItem
->hItem
;
2041 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr
, wineItem
),
2044 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
2047 if (!TREEVIEW_DoSetItem(infoPtr
, wineItem
, tvItem
))
2050 /* store the orignal item values */
2051 originalItem
= *wineItem
;
2053 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2054 if ((tvItem
->mask
& TVIF_TEXT
2055 || (tvItem
->mask
& TVIF_STATE
&& tvItem
->stateMask
& TVIS_BOLD
))
2056 && ISVISIBLE(wineItem
))
2058 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, TVIF_TEXT
);
2059 TREEVIEW_ComputeTextWidth(infoPtr
, wineItem
, 0);
2062 if (tvItem
->mask
!= 0 && ISVISIBLE(wineItem
))
2064 /* The refresh updates everything, but we can't wait until then. */
2065 TREEVIEW_ComputeItemInternalMetrics(infoPtr
, wineItem
);
2067 /* if any of the items values changed, redraw the item */
2068 if(memcmp(&originalItem
, wineItem
, sizeof(TREEVIEW_ITEM
)))
2070 if (tvItem
->mask
& TVIF_INTEGRAL
)
2072 TREEVIEW_RecalculateVisibleOrder(infoPtr
, wineItem
);
2073 TREEVIEW_UpdateScrollBars(infoPtr
);
2075 TREEVIEW_Invalidate(infoPtr
, NULL
);
2079 TREEVIEW_UpdateScrollBars(infoPtr
);
2080 TREEVIEW_Invalidate(infoPtr
, wineItem
);
2089 TREEVIEW_GetItemW(TREEVIEW_INFO
*infoPtr
, LPTVITEMEXW tvItem
)
2091 TREEVIEW_ITEM
*wineItem
;
2093 iItem
= (INT
)tvItem
->hItem
;
2095 wineItem
= tvItem
->hItem
;
2096 if(!TREEVIEW_ValidItem (infoPtr
, wineItem
))
2099 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, tvItem
->mask
);
2101 if (tvItem
->mask
& TVIF_CHILDREN
) {
2102 if (TVIF_CHILDREN
==I_CHILDRENCALLBACK
)
2103 FIXME("I_CHILDRENCALLBACK not supported\n");
2104 tvItem
->cChildren
= wineItem
->cChildren
;
2107 if (tvItem
->mask
& TVIF_HANDLE
) {
2108 tvItem
->hItem
= wineItem
;
2110 if (tvItem
->mask
& TVIF_IMAGE
) {
2111 tvItem
->iImage
= wineItem
->iImage
;
2113 if (tvItem
->mask
& TVIF_INTEGRAL
) {
2114 tvItem
->iIntegral
= wineItem
->iIntegral
;
2116 /* undocumented: windows ignores TVIF_PARAM and
2117 * always sets lParam */
2118 tvItem
->lParam
= wineItem
->lParam
;
2119 if (tvItem
->mask
& TVIF_SELECTEDIMAGE
) {
2120 tvItem
->iSelectedImage
= wineItem
->iSelectedImage
;
2122 if (tvItem
->mask
& TVIF_STATE
) {
2123 tvItem
->state
= wineItem
->state
& tvItem
->stateMask
;
2126 if (tvItem
->mask
& TVIF_TEXT
) {
2127 if (wineItem
->pszText
== LPSTR_TEXTCALLBACKA
) {
2128 tvItem
->pszText
= LPSTR_TEXTCALLBACKW
;
2129 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2131 else if (wineItem
->pszText
) {
2132 TRACE("orig str %s at %p\n",
2133 debugstr_a(wineItem
->pszText
), wineItem
->pszText
);
2134 MultiByteToWideChar(CP_ACP
, 0, wineItem
->pszText
,
2135 -1 , tvItem
->pszText
, tvItem
->cchTextMax
);
2139 TRACE("item %d<%p>, txt %p<%s>, img %p, action %x\n",
2140 iItem
, tvItem
, tvItem
->pszText
, debugstr_w(tvItem
->pszText
),
2141 &tvItem
->iImage
, tvItem
->mask
);
2146 TREEVIEW_SetItemW(TREEVIEW_INFO
*infoPtr
, LPTVITEMEXW tvItem
)
2152 tvItemA
.mask
= tvItem
->mask
;
2153 tvItemA
.hItem
= tvItem
->hItem
;
2154 tvItemA
.state
= tvItem
->state
;
2155 tvItemA
.stateMask
= tvItem
->stateMask
;
2156 if (tvItem
->mask
& TVIF_TEXT
) {
2157 len
= WideCharToMultiByte(CP_ACP
, 0, tvItem
->pszText
, -1,
2158 NULL
,0 , NULL
,NULL
);
2161 tvItemA
.pszText
= HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
2162 len
= WideCharToMultiByte(CP_ACP
, 0, tvItem
->pszText
, -1,
2163 tvItemA
.pszText
,len
*sizeof(WCHAR
),
2167 tvItemA
.pszText
= NULL
;
2169 tvItemA
.cchTextMax
= tvItem
->cchTextMax
;
2170 tvItemA
.iImage
= tvItem
->iImage
;
2171 tvItemA
.iSelectedImage
= tvItem
->iSelectedImage
;
2172 tvItemA
.cChildren
= tvItem
->cChildren
;
2173 tvItemA
.lParam
= tvItem
->lParam
;
2174 tvItemA
.iIntegral
= tvItem
->iIntegral
;
2176 rc
= TREEVIEW_SetItemA(infoPtr
,&tvItemA
);
2177 HeapFree(GetProcessHeap(),0,tvItemA
.pszText
);
2182 TREEVIEW_GetItemState(TREEVIEW_INFO
*infoPtr
, HTREEITEM wineItem
, UINT mask
)
2186 if (!wineItem
|| !TREEVIEW_ValidItem(infoPtr
, wineItem
))
2189 return (wineItem
->state
& mask
);
2193 TREEVIEW_GetNextItem(TREEVIEW_INFO
*infoPtr
, UINT which
, HTREEITEM wineItem
)
2195 TREEVIEW_ITEM
*retval
;
2199 /* handle all the global data here */
2202 case TVGN_CHILD
: /* Special case: child of 0 is root */
2207 retval
= infoPtr
->root
->firstChild
;
2211 retval
= infoPtr
->selectedItem
;
2214 case TVGN_FIRSTVISIBLE
:
2215 retval
= infoPtr
->firstVisible
;
2218 case TVGN_DROPHILITE
:
2219 retval
= infoPtr
->dropItem
;
2222 case TVGN_LASTVISIBLE
:
2223 retval
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
2229 TRACE("flags:%x, returns %p\n", which
, retval
);
2230 return (LRESULT
)retval
;
2233 if (wineItem
== TVI_ROOT
) wineItem
= infoPtr
->root
;
2235 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
2241 retval
= wineItem
->nextSibling
;
2244 retval
= wineItem
->prevSibling
;
2247 retval
= (wineItem
->parent
!= infoPtr
->root
) ? wineItem
->parent
: NULL
;
2250 retval
= wineItem
->firstChild
;
2252 case TVGN_NEXTVISIBLE
:
2253 retval
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
);
2255 case TVGN_PREVIOUSVISIBLE
:
2256 retval
= TREEVIEW_GetPrevListItem(infoPtr
, wineItem
);
2259 TRACE("Unknown msg %x,item %p\n", which
, wineItem
);
2263 TRACE("flags:%x, item %p;returns %p\n", which
, wineItem
, retval
);
2264 return (LRESULT
)retval
;
2269 TREEVIEW_GetCount(TREEVIEW_INFO
*infoPtr
)
2271 TRACE(" %d\n", infoPtr
->uNumItems
);
2272 return (LRESULT
)infoPtr
->uNumItems
;
2276 TREEVIEW_ToggleItemState(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2278 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
2280 static const unsigned int state_table
[] = { 0, 2, 1 };
2284 state
= STATEIMAGEINDEX(item
->state
);
2285 TRACE("state:%x\n", state
);
2286 item
->state
&= ~TVIS_STATEIMAGEMASK
;
2289 state
= state_table
[state
];
2291 item
->state
|= INDEXTOSTATEIMAGEMASK(state
);
2293 TRACE("state:%x\n", state
);
2294 TREEVIEW_Invalidate(infoPtr
, item
);
2299 /* Painting *************************************************************/
2301 /* Draw the lines and expand button for an item. Also draws one section
2302 * of the line from item's parent to item's parent's next sibling. */
2304 TREEVIEW_DrawItemLines(TREEVIEW_INFO
*infoPtr
, HDC hdc
, TREEVIEW_ITEM
*item
)
2306 LONG centerx
, centery
;
2307 BOOL lar
= ((infoPtr
->dwStyle
2308 & (TVS_LINESATROOT
|TVS_HASLINES
|TVS_HASBUTTONS
))
2311 if (!lar
&& item
->iLevel
== 0)
2314 centerx
= (item
->linesOffset
+ item
->stateOffset
) / 2;
2315 centery
= (item
->rect
.top
+ item
->rect
.bottom
) / 2;
2317 if (infoPtr
->dwStyle
& TVS_HASLINES
)
2319 HPEN hOldPen
, hNewPen
;
2323 * Get a dotted grey pen
2325 hNewPen
= CreatePen(PS_ALTERNATE
, 0, infoPtr
->clrLine
);
2326 hOldPen
= SelectObject(hdc
, hNewPen
);
2328 MoveToEx(hdc
, item
->stateOffset
, centery
, NULL
);
2329 LineTo(hdc
, centerx
- 1, centery
);
2331 if (item
->prevSibling
|| item
->parent
!= infoPtr
->root
)
2333 MoveToEx(hdc
, centerx
, item
->rect
.top
, NULL
);
2334 LineTo(hdc
, centerx
, centery
);
2337 if (item
->nextSibling
)
2339 MoveToEx(hdc
, centerx
, centery
, NULL
);
2340 LineTo(hdc
, centerx
, item
->rect
.bottom
+ 1);
2343 /* Draw the line from our parent to its next sibling. */
2344 parent
= item
->parent
;
2345 while (parent
!= infoPtr
->root
)
2347 int pcenterx
= (parent
->linesOffset
+ parent
->stateOffset
) / 2;
2349 if (parent
->nextSibling
2350 /* skip top-levels unless TVS_LINESATROOT */
2351 && parent
->stateOffset
> parent
->linesOffset
)
2353 MoveToEx(hdc
, pcenterx
, item
->rect
.top
, NULL
);
2354 LineTo(hdc
, pcenterx
, item
->rect
.bottom
+ 1);
2357 parent
= parent
->parent
;
2360 SelectObject(hdc
, hOldPen
);
2361 DeleteObject(hNewPen
);
2365 * Display the (+/-) signs
2368 if (infoPtr
->dwStyle
& TVS_HASBUTTONS
)
2370 if (item
->cChildren
)
2372 LONG height
= item
->rect
.bottom
- item
->rect
.top
;
2373 LONG width
= item
->stateOffset
- item
->linesOffset
;
2374 LONG rectsize
= min(height
, width
) / 4;
2375 /* plussize = ceil(rectsize * 3/4) */
2376 LONG plussize
= (rectsize
+ 1) * 3 / 4;
2378 HPEN hNewPen
= CreatePen(PS_SOLID
, 0, infoPtr
->clrLine
);
2379 HPEN hOldPen
= SelectObject(hdc
, hNewPen
);
2380 HBRUSH hbr
= CreateSolidBrush(infoPtr
->clrBk
);
2381 HBRUSH hbrOld
= SelectObject(hdc
, hbr
);
2383 Rectangle(hdc
, centerx
- rectsize
, centery
- rectsize
,
2384 centerx
+ rectsize
+ 1, centery
+ rectsize
+ 1);
2386 SelectObject(hdc
, hbrOld
);
2389 SelectObject(hdc
, hOldPen
);
2390 DeleteObject(hNewPen
);
2392 MoveToEx(hdc
, centerx
- plussize
+ 1, centery
, NULL
);
2393 LineTo(hdc
, centerx
+ plussize
, centery
);
2395 if (!(item
->state
& TVIS_EXPANDED
))
2397 MoveToEx(hdc
, centerx
, centery
- plussize
+ 1, NULL
);
2398 LineTo(hdc
, centerx
, centery
+ plussize
);
2405 TREEVIEW_DrawItem(TREEVIEW_INFO
*infoPtr
, HDC hdc
, TREEVIEW_ITEM
*wineItem
)
2411 hOldFont
= SelectObject(hdc
, TREEVIEW_FontForItem(infoPtr
, wineItem
));
2413 TREEVIEW_UpdateDispInfo(infoPtr
, wineItem
, CALLBACK_MASK_ALL
);
2415 /* The custom draw handler can query the text rectangle,
2417 TREEVIEW_ComputeTextWidth(infoPtr
, wineItem
, hdc
);
2421 if (infoPtr
->cdmode
& CDRF_NOTIFYITEMDRAW
)
2423 cditem
= TREEVIEW_SendCustomDrawItemNotify
2424 (infoPtr
, hdc
, wineItem
, CDDS_ITEMPREPAINT
);
2425 TRACE("prepaint:cditem-app returns 0x%x\n", cditem
);
2427 if (cditem
& CDRF_SKIPDEFAULT
)
2429 SelectObject(hdc
, hOldFont
);
2434 if (cditem
& CDRF_NEWFONT
)
2435 TREEVIEW_ComputeTextWidth(infoPtr
, wineItem
, hdc
);
2437 TREEVIEW_DrawItemLines(infoPtr
, hdc
, wineItem
);
2439 centery
= (wineItem
->rect
.top
+ wineItem
->rect
.bottom
) / 2;
2442 * Display the images associated with this item
2447 /* State images are displayed to the left of the Normal image
2448 * image number is in state; zero should be `display no image'.
2450 imageIndex
= STATEIMAGEINDEX(wineItem
->state
);
2452 if (infoPtr
->himlState
&& imageIndex
)
2454 ImageList_Draw(infoPtr
->himlState
, imageIndex
, hdc
,
2455 wineItem
->stateOffset
,
2456 centery
- infoPtr
->stateImageHeight
/ 2,
2460 /* Now, draw the normal image; can be either selected or
2461 * non-selected image.
2464 if ((wineItem
->state
& TVIS_SELECTED
) && (wineItem
->iSelectedImage
))
2466 /* The item is currently selected */
2467 imageIndex
= wineItem
->iSelectedImage
;
2471 /* The item is not selected */
2472 imageIndex
= wineItem
->iImage
;
2475 if (infoPtr
->himlNormal
)
2477 int ovlIdx
= wineItem
->state
& TVIS_OVERLAYMASK
;
2479 ImageList_Draw(infoPtr
->himlNormal
, imageIndex
, hdc
,
2480 wineItem
->imageOffset
,
2481 centery
- infoPtr
->normalImageHeight
/ 2,
2482 ILD_NORMAL
| ovlIdx
);
2488 * Display the text associated with this item
2491 /* Don't paint item's text if it's being edited */
2492 if (!infoPtr
->hwndEdit
|| (infoPtr
->selectedItem
!= wineItem
))
2494 if (wineItem
->pszText
)
2496 COLORREF oldTextColor
= 0;
2499 BOOL inFocus
= (GetFocus() == infoPtr
->hwnd
);
2502 oldBkMode
= SetBkMode(hdc
, TRANSPARENT
);
2504 /* - If item is drop target or it is selected and window is in focus -
2505 * use blue background (COLOR_HIGHLIGHT).
2506 * - If item is selected, window is not in focus, but it has style
2507 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2508 * - Otherwise - don't fill background
2510 if ((wineItem
->state
& TVIS_DROPHILITED
) || ((wineItem
== infoPtr
->focusedItem
) && !(wineItem
->state
& TVIS_SELECTED
)) ||
2511 ((wineItem
->state
& TVIS_SELECTED
) && (!infoPtr
->focusedItem
) &&
2512 (inFocus
|| (infoPtr
->dwStyle
& TVS_SHOWSELALWAYS
))))
2514 if ((wineItem
->state
& TVIS_DROPHILITED
) || inFocus
)
2516 hbrBk
= CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT
));
2518 SetTextColor(hdc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2522 hbrBk
= CreateSolidBrush(GetSysColor(COLOR_BTNFACE
));
2524 if (infoPtr
->clrText
== -1)
2526 SetTextColor(hdc
, GetSysColor(COLOR_WINDOWTEXT
));
2528 oldTextColor
= SetTextColor(hdc
, infoPtr
->clrText
);
2533 if (infoPtr
->clrText
== -1)
2535 SetTextColor(hdc
, GetSysColor(COLOR_WINDOWTEXT
));
2537 oldTextColor
= SetTextColor(hdc
, infoPtr
->clrText
);
2540 rcText
.top
= wineItem
->rect
.top
;
2541 rcText
.bottom
= wineItem
->rect
.bottom
;
2542 rcText
.left
= wineItem
->textOffset
;
2543 rcText
.right
= rcText
.left
+ wineItem
->textWidth
+ 4;
2547 FillRect(hdc
, &rcText
, hbrBk
);
2548 DeleteObject(hbrBk
);
2551 /* Draw the box around the selected item */
2552 if ((wineItem
== infoPtr
->selectedItem
) && inFocus
)
2554 DrawFocusRect(hdc
,&rcText
);
2557 InflateRect(&rcText
, -2, -1); /* allow for the focus rect */
2559 TRACE("drawing text %s at (%ld,%ld)-(%ld,%ld)\n",
2560 debugstr_a(wineItem
->pszText
),
2561 rcText
.left
, rcText
.top
, rcText
.right
, rcText
.bottom
);
2566 lstrlenA(wineItem
->pszText
),
2568 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
| DT_NOPREFIX
);
2570 /* Restore the hdc state */
2571 SetTextColor(hdc
, oldTextColor
);
2573 if (oldBkMode
!= TRANSPARENT
)
2574 SetBkMode(hdc
, oldBkMode
);
2578 /* Draw insertion mark if necessary */
2580 if (infoPtr
->insertMarkItem
)
2581 TRACE("item:%d,mark:%d\n",
2582 TREEVIEW_GetItemIndex(infoPtr
, wineItem
),
2583 (int)infoPtr
->insertMarkItem
);
2585 if (wineItem
== infoPtr
->insertMarkItem
)
2587 HPEN hNewPen
, hOldPen
;
2591 hNewPen
= CreatePen(PS_SOLID
, 2, infoPtr
->clrInsertMark
);
2592 hOldPen
= SelectObject(hdc
, hNewPen
);
2594 if (infoPtr
->insertBeforeorAfter
)
2595 offset
= wineItem
->rect
.bottom
- 1;
2597 offset
= wineItem
->rect
.top
+ 1;
2599 left
= wineItem
->textOffset
- 2;
2600 right
= wineItem
->textOffset
+ wineItem
->textWidth
+ 2;
2602 MoveToEx(hdc
, left
, offset
- 3, NULL
);
2603 LineTo(hdc
, left
, offset
+ 4);
2605 MoveToEx(hdc
, left
, offset
, NULL
);
2606 LineTo(hdc
, right
+ 1, offset
);
2608 MoveToEx(hdc
, right
, offset
+ 3, NULL
);
2609 LineTo(hdc
, right
, offset
- 4);
2611 SelectObject(hdc
, hOldPen
);
2612 DeleteObject(hNewPen
);
2615 if (cditem
& CDRF_NOTIFYPOSTPAINT
)
2617 cditem
= TREEVIEW_SendCustomDrawItemNotify
2618 (infoPtr
, hdc
, wineItem
, CDDS_ITEMPOSTPAINT
);
2619 TRACE("postpaint:cditem-app returns 0x%x\n", cditem
);
2622 SelectObject(hdc
, hOldFont
);
2625 /* Computes treeHeight and treeWidth and updates the scroll bars.
2628 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO
*infoPtr
)
2630 TREEVIEW_ITEM
*wineItem
;
2631 HWND hwnd
= infoPtr
->hwnd
;
2635 LONG scrollX
= infoPtr
->scrollX
;
2637 infoPtr
->treeWidth
= 0;
2638 infoPtr
->treeHeight
= 0;
2640 /* We iterate through all visible items in order to get the tree height
2642 wineItem
= infoPtr
->root
->firstChild
;
2644 while (wineItem
!= NULL
)
2646 if (ISVISIBLE(wineItem
))
2648 /* actually we draw text at textOffset + 2 */
2649 if (2+wineItem
->textOffset
+wineItem
->textWidth
> infoPtr
->treeWidth
)
2650 infoPtr
->treeWidth
= wineItem
->textOffset
+wineItem
->textWidth
+2;
2652 /* This is scroll-adjusted, but we fix this below. */
2653 infoPtr
->treeHeight
= wineItem
->rect
.bottom
;
2656 wineItem
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
);
2659 /* Fix the scroll adjusted treeHeight and treeWidth. */
2660 if (infoPtr
->root
->firstChild
)
2661 infoPtr
->treeHeight
-= infoPtr
->root
->firstChild
->rect
.top
;
2663 infoPtr
->treeWidth
+= infoPtr
->scrollX
;
2665 if (infoPtr
->dwStyle
& TVS_NOSCROLL
) return;
2667 /* Adding one scroll bar may take up enough space that it forces us
2668 * to add the other as well. */
2669 if (infoPtr
->treeHeight
> infoPtr
->clientHeight
)
2673 if (infoPtr
->treeWidth
2674 > infoPtr
->clientWidth
- GetSystemMetrics(SM_CXVSCROLL
))
2677 else if (infoPtr
->treeWidth
> infoPtr
->clientWidth
)
2680 if (!vert
&& horz
&& infoPtr
->treeHeight
2681 > infoPtr
->clientHeight
- GetSystemMetrics(SM_CYVSCROLL
))
2684 if (horz
&& (infoPtr
->dwStyle
& TVS_NOHSCROLL
)) horz
= FALSE
;
2686 si
.cbSize
= sizeof(SCROLLINFO
);
2687 si
.fMask
= SIF_POS
|SIF_RANGE
|SIF_PAGE
;
2692 si
.nPage
= TREEVIEW_GetVisibleCount(infoPtr
);
2695 si
.nPos
= infoPtr
->firstVisible
->visibleOrder
;
2696 si
.nMax
= infoPtr
->maxVisibleOrder
- 1;
2698 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
2700 if (!(infoPtr
->uInternalStatus
& TV_VSCROLL
))
2701 ShowScrollBar(hwnd
, SB_VERT
, TRUE
);
2702 infoPtr
->uInternalStatus
|= TV_VSCROLL
;
2706 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2707 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2708 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2713 if (infoPtr
->uInternalStatus
& TV_VSCROLL
)
2714 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
2715 infoPtr
->uInternalStatus
&= ~TV_VSCROLL
;
2720 si
.nPage
= infoPtr
->clientWidth
;
2721 si
.nPos
= infoPtr
->scrollX
;
2722 si
.nMax
= infoPtr
->treeWidth
- 1;
2724 if (si
.nPos
> si
.nMax
- max( si
.nPage
-1, 0 ))
2726 si
.nPos
= si
.nMax
- max( si
.nPage
-1, 0 );
2730 if (!(infoPtr
->uInternalStatus
& TV_HSCROLL
))
2731 ShowScrollBar(hwnd
, SB_HORZ
, TRUE
);
2732 infoPtr
->uInternalStatus
|= TV_HSCROLL
;
2734 SetScrollInfo(hwnd
, SB_HORZ
, &si
, TRUE
);
2738 if (infoPtr
->uInternalStatus
& TV_HSCROLL
)
2739 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
2740 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2745 if (infoPtr
->scrollX
!= scrollX
)
2747 TREEVIEW_HScroll(infoPtr
,
2748 MAKEWPARAM(SB_THUMBPOSITION
, scrollX
));
2752 infoPtr
->uInternalStatus
&= ~TV_HSCROLL
;
2755 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2757 TREEVIEW_EraseBackground(TREEVIEW_INFO
*infoPtr
, HDC hDC
)
2759 HBRUSH hBrush
= CreateSolidBrush(infoPtr
->clrBk
);
2762 GetClientRect(infoPtr
->hwnd
, &rect
);
2763 FillRect(hDC
, &rect
, hBrush
);
2764 DeleteObject(hBrush
);
2770 TREEVIEW_Refresh(TREEVIEW_INFO
*infoPtr
, HDC hdc
, RECT
*rc
)
2772 HWND hwnd
= infoPtr
->hwnd
;
2774 TREEVIEW_ITEM
*wineItem
;
2776 if (infoPtr
->clientHeight
== 0 || infoPtr
->clientWidth
== 0)
2778 TRACE("empty window\n");
2782 infoPtr
->cdmode
= TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_PREPAINT
,
2785 if (infoPtr
->cdmode
== CDRF_SKIPDEFAULT
)
2787 ReleaseDC(hwnd
, hdc
);
2791 for (wineItem
= infoPtr
->root
->firstChild
;
2793 wineItem
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
))
2795 if (ISVISIBLE(wineItem
))
2797 /* Avoid unneeded calculations */
2798 if (wineItem
->rect
.top
> rect
.bottom
)
2800 if (wineItem
->rect
.bottom
< rect
.top
)
2803 TREEVIEW_DrawItem(infoPtr
, hdc
, wineItem
);
2807 TREEVIEW_UpdateScrollBars(infoPtr
);
2809 if (infoPtr
->cdmode
& CDRF_NOTIFYPOSTPAINT
)
2811 TREEVIEW_SendCustomDrawNotify(infoPtr
, CDDS_POSTPAINT
, hdc
, rect
);
2815 TREEVIEW_Invalidate(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2818 InvalidateRect(infoPtr
->hwnd
, &item
->rect
, TRUE
);
2820 InvalidateRect(infoPtr
->hwnd
, NULL
, TRUE
);
2824 TREEVIEW_Paint(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
2835 if (!GetUpdateRect(infoPtr
->hwnd
, &rc
, TRUE
))
2839 hbitmap
= GetCurrentObject(hdc
, OBJ_BITMAP
);
2840 if (!hbitmap
) return 0;
2841 GetObjectA(hbitmap
, sizeof(BITMAP
), &bitmap
);
2842 rc
.left
= 0; rc
.top
= 0;
2843 rc
.right
= bitmap
.bmWidth
;
2844 rc
.bottom
= bitmap
.bmHeight
;
2845 TREEVIEW_EraseBackground(infoPtr
, (HDC
)wParam
);
2850 hdc
= BeginPaint(infoPtr
->hwnd
, &ps
);
2854 if(infoPtr
->bRedraw
) /* WM_SETREDRAW sets bRedraw */
2855 TREEVIEW_Refresh(infoPtr
, hdc
, &rc
);
2858 EndPaint(infoPtr
->hwnd
, &ps
);
2864 /* Sorting **************************************************************/
2866 /***************************************************************************
2867 * Forward the DPA local callback to the treeview owner callback
2870 TREEVIEW_CallBackCompare(TREEVIEW_ITEM
*first
, TREEVIEW_ITEM
*second
, LPTVSORTCB pCallBackSort
)
2872 /* Forward the call to the client-defined callback */
2873 return pCallBackSort
->lpfnCompare(first
->lParam
,
2875 pCallBackSort
->lParam
);
2878 /***************************************************************************
2879 * Treeview native sort routine: sort on item text.
2882 TREEVIEW_SortOnName(TREEVIEW_ITEM
*first
, TREEVIEW_ITEM
*second
,
2883 TREEVIEW_INFO
*infoPtr
)
2885 TREEVIEW_UpdateDispInfo(infoPtr
, first
, TVIF_TEXT
);
2886 TREEVIEW_UpdateDispInfo(infoPtr
, second
, TVIF_TEXT
);
2888 return strcasecmp(first
->pszText
, second
->pszText
);
2891 /* Returns the number of physical children belonging to item. */
2893 TREEVIEW_CountChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2898 for (hti
= item
->firstChild
; hti
!= NULL
; hti
= hti
->nextSibling
)
2904 /* Returns a DPA containing a pointer to each physical child of item in
2905 * sibling order. If item has no children, an empty DPA is returned. */
2907 TREEVIEW_BuildChildDPA(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
2909 HTREEITEM child
= item
->firstChild
;
2911 HDPA list
= DPA_Create(8);
2912 if (list
== 0) return NULL
;
2914 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
2916 if (DPA_InsertPtr(list
, INT_MAX
, child
) == -1)
2926 /***************************************************************************
2927 * Setup the treeview structure with regards of the sort method
2928 * and sort the children of the TV item specified in lParam
2929 * fRecurse: currently unused. Should be zero.
2930 * parent: if pSort!=NULL, should equal pSort->hParent.
2931 * otherwise, item which child items are to be sorted.
2932 * pSort: sort method info. if NULL, sort on item text.
2933 * if non-NULL, sort on item's lParam content, and let the
2934 * application decide what that means. See also TVM_SORTCHILDRENCB.
2938 TREEVIEW_Sort(TREEVIEW_INFO
*infoPtr
, BOOL fRecurse
, HTREEITEM parent
,
2942 PFNDPACOMPARE pfnCompare
;
2945 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2946 if (parent
== TVI_ROOT
|| parent
== NULL
)
2947 parent
= infoPtr
->root
;
2949 /* Check for a valid handle to the parent item */
2950 if (!TREEVIEW_ValidItem(infoPtr
, parent
))
2952 ERR("invalid item hParent=%x\n", (INT
)parent
);
2958 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_CallBackCompare
;
2959 lpCompare
= (LPARAM
)pSort
;
2963 pfnCompare
= (PFNDPACOMPARE
)TREEVIEW_SortOnName
;
2964 lpCompare
= (LPARAM
)infoPtr
;
2967 cChildren
= TREEVIEW_CountChildren(infoPtr
, parent
);
2969 /* Make sure there is something to sort */
2972 /* TREEVIEW_ITEM rechaining */
2975 HTREEITEM nextItem
= 0;
2976 HTREEITEM prevItem
= 0;
2978 HDPA sortList
= TREEVIEW_BuildChildDPA(infoPtr
, parent
);
2980 if (sortList
== NULL
)
2983 /* let DPA sort the list */
2984 DPA_Sort(sortList
, pfnCompare
, lpCompare
);
2986 /* The order of DPA entries has been changed, so fixup the
2987 * nextSibling and prevSibling pointers. */
2989 item
= (HTREEITEM
)DPA_GetPtr(sortList
, count
++);
2990 while ((nextItem
= (HTREEITEM
)DPA_GetPtr(sortList
, count
++)) != NULL
)
2992 /* link the two current item toghether */
2993 item
->nextSibling
= nextItem
;
2994 nextItem
->prevSibling
= item
;
2996 if (prevItem
== NULL
)
2998 /* this is the first item, update the parent */
2999 parent
->firstChild
= item
;
3000 item
->prevSibling
= NULL
;
3004 /* fix the back chaining */
3005 item
->prevSibling
= prevItem
;
3008 /* get ready for the next one */
3013 /* the last item is pointed to by item and never has a sibling */
3014 item
->nextSibling
= NULL
;
3015 parent
->lastChild
= item
;
3017 DPA_Destroy(sortList
);
3019 TREEVIEW_VerifyTree(infoPtr
);
3021 if (parent
->state
& TVIS_EXPANDED
)
3023 int visOrder
= infoPtr
->firstVisible
->visibleOrder
;
3025 if (parent
== infoPtr
->root
)
3026 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
3028 TREEVIEW_RecalculateVisibleOrder(infoPtr
, parent
);
3030 if (TREEVIEW_IsChildOf(parent
, infoPtr
->firstVisible
))
3032 TREEVIEW_ITEM
*item
;
3034 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
3035 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
3037 if (item
->visibleOrder
== visOrder
)
3041 TREEVIEW_SetFirstVisible(infoPtr
, item
, FALSE
);
3044 TREEVIEW_Invalidate(infoPtr
, NULL
);
3053 /***************************************************************************
3054 * Setup the treeview structure with regards of the sort method
3055 * and sort the children of the TV item specified in lParam
3058 TREEVIEW_SortChildrenCB(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPTVSORTCB pSort
)
3060 return TREEVIEW_Sort(infoPtr
, wParam
, pSort
->hParent
, pSort
);
3064 /***************************************************************************
3065 * Sort the children of the TV item specified in lParam.
3068 TREEVIEW_SortChildren(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
3070 return TREEVIEW_Sort(infoPtr
, (BOOL
)wParam
, (HTREEITEM
)lParam
, NULL
);
3074 /* Expansion/Collapse ***************************************************/
3077 TREEVIEW_SendExpanding(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
3080 return !TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_ITEMEXPANDINGW
, action
,
3081 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
3082 | TVIF_IMAGE
| TVIF_SELECTEDIMAGE
,
3087 TREEVIEW_SendExpanded(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
3090 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_ITEMEXPANDEDW
, action
,
3091 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
3092 | TVIF_IMAGE
| TVIF_SELECTEDIMAGE
,
3097 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3098 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3100 TREEVIEW_Collapse(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
3101 BOOL bRemoveChildren
, BOOL bUser
)
3103 UINT action
= TVE_COLLAPSE
| (bRemoveChildren
? TVE_COLLAPSERESET
: 0);
3104 BOOL bSetSelection
, bSetFirstVisible
;
3106 TRACE("TVE_COLLAPSE %p %s\n", wineItem
, TREEVIEW_ItemName(wineItem
));
3108 if (!(wineItem
->state
& TVIS_EXPANDED
) || wineItem
->firstChild
== NULL
)
3112 TREEVIEW_SendExpanding(infoPtr
, wineItem
, action
);
3114 wineItem
->state
&= ~TVIS_EXPANDED
;
3117 TREEVIEW_SendExpanded(infoPtr
, wineItem
, action
);
3119 bSetSelection
= (infoPtr
->selectedItem
!= NULL
3120 && TREEVIEW_IsChildOf(wineItem
, infoPtr
->selectedItem
));
3122 bSetFirstVisible
= (infoPtr
->firstVisible
!= NULL
3123 && TREEVIEW_IsChildOf(wineItem
, infoPtr
->firstVisible
));
3125 if (bRemoveChildren
)
3127 TRACE("TVE_COLLAPSERESET\n");
3128 wineItem
->state
&= ~TVIS_EXPANDEDONCE
;
3129 TREEVIEW_RemoveAllChildren(infoPtr
, wineItem
);
3132 if (wineItem
->firstChild
)
3134 TREEVIEW_ITEM
*item
, *sibling
;
3136 sibling
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
);
3138 for (item
= wineItem
->firstChild
; item
!= sibling
;
3139 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
3141 item
->visibleOrder
= -1;
3145 TREEVIEW_RecalculateVisibleOrder(infoPtr
, wineItem
);
3147 TREEVIEW_SetFirstVisible(infoPtr
, bSetFirstVisible
? wineItem
3148 : infoPtr
->firstVisible
, TRUE
);
3152 /* Don't call DoSelectItem, it sends notifications. */
3153 if (TREEVIEW_ValidItem(infoPtr
, infoPtr
->selectedItem
))
3154 infoPtr
->selectedItem
->state
&= ~TVIS_SELECTED
;
3155 wineItem
->state
|= TVIS_SELECTED
;
3156 infoPtr
->selectedItem
= wineItem
;
3158 TREEVIEW_EnsureVisible(infoPtr
, wineItem
, FALSE
);
3161 TREEVIEW_UpdateScrollBars(infoPtr
);
3162 TREEVIEW_Invalidate(infoPtr
, NULL
);
3168 TREEVIEW_Expand(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
,
3169 BOOL bExpandPartial
, BOOL bUser
)
3173 if (!TREEVIEW_HasChildren(infoPtr
, wineItem
)
3174 || wineItem
->state
& TVIS_EXPANDED
)
3177 TRACE("TVE_EXPAND %p %s\n", wineItem
, TREEVIEW_ItemName(wineItem
));
3179 if (bUser
|| !(wineItem
->state
& TVIS_EXPANDEDONCE
))
3181 if (!TREEVIEW_SendExpanding(infoPtr
, wineItem
, TVE_EXPAND
))
3183 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3187 wineItem
->state
|= TVIS_EXPANDED
;
3188 TREEVIEW_SendExpanded(infoPtr
, wineItem
, TVE_EXPAND
);
3189 wineItem
->state
|= TVIS_EXPANDEDONCE
;
3193 /* this item has already been expanded */
3194 wineItem
->state
|= TVIS_EXPANDED
;
3198 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3200 TREEVIEW_RecalculateVisibleOrder(infoPtr
, wineItem
);
3201 TREEVIEW_UpdateSubTree(infoPtr
, wineItem
);
3202 TREEVIEW_UpdateScrollBars(infoPtr
);
3204 /* Scroll up so that as many children as possible are visible.
3205 * This looses when expanding causes an HScroll bar to appear, but we
3206 * don't know that yet, so the last item is obscured. */
3207 if (wineItem
->firstChild
!= NULL
)
3209 int nChildren
= wineItem
->lastChild
->visibleOrder
3210 - wineItem
->firstChild
->visibleOrder
+ 1;
3212 int visible_pos
= wineItem
->visibleOrder
3213 - infoPtr
->firstVisible
->visibleOrder
;
3215 int rows_below
= TREEVIEW_GetVisibleCount(infoPtr
) - visible_pos
- 1;
3217 if (visible_pos
> 0 && nChildren
> rows_below
)
3219 int scroll
= nChildren
- rows_below
;
3221 if (scroll
> visible_pos
)
3222 scroll
= visible_pos
;
3226 TREEVIEW_ITEM
*newFirstVisible
3227 = TREEVIEW_GetListItem(infoPtr
, infoPtr
->firstVisible
,
3231 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
3236 TREEVIEW_Invalidate(infoPtr
, NULL
);
3242 TREEVIEW_Toggle(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*wineItem
, BOOL bUser
)
3246 if (wineItem
->state
& TVIS_EXPANDED
)
3247 return TREEVIEW_Collapse(infoPtr
, wineItem
, FALSE
, bUser
);
3249 return TREEVIEW_Expand(infoPtr
, wineItem
, FALSE
, bUser
);
3253 TREEVIEW_ExpandAll(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
3255 TREEVIEW_Expand(infoPtr
, item
, FALSE
, TRUE
);
3257 for (item
= item
->firstChild
; item
!= NULL
; item
= item
->nextSibling
)
3259 if (TREEVIEW_HasChildren(infoPtr
, item
))
3260 TREEVIEW_ExpandAll(infoPtr
, item
);
3264 /* Note:If the specified item is the child of a collapsed parent item,
3265 the parent's list of child items is (recursively) expanded to reveal the
3266 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3267 know if it also applies here.
3271 TREEVIEW_ExpandMsg(TREEVIEW_INFO
*infoPtr
, UINT flag
, HTREEITEM wineItem
)
3273 if (!TREEVIEW_ValidItem(infoPtr
, wineItem
))
3276 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3277 TREEVIEW_ItemName(wineItem
), flag
,
3278 TREEVIEW_GetItemIndex(infoPtr
, wineItem
), wineItem
->state
);
3280 switch (flag
& TVE_TOGGLE
)
3283 return TREEVIEW_Collapse(infoPtr
, wineItem
, flag
& TVE_COLLAPSERESET
,
3287 return TREEVIEW_Expand(infoPtr
, wineItem
, flag
& TVE_EXPANDPARTIAL
,
3291 return TREEVIEW_Toggle(infoPtr
, wineItem
, TRUE
);
3298 TRACE("Exiting, Item %p state is now %d...\n", wineItem
, wineItem
->state
);
3302 /* Hit-Testing **********************************************************/
3304 static TREEVIEW_ITEM
*
3305 TREEVIEW_HitTestPoint(TREEVIEW_INFO
*infoPtr
, POINT pt
)
3307 TREEVIEW_ITEM
*wineItem
;
3310 if (!infoPtr
->firstVisible
)
3313 row
= pt
.y
/ infoPtr
->uItemHeight
+ infoPtr
->firstVisible
->visibleOrder
;
3315 for (wineItem
= infoPtr
->firstVisible
; wineItem
!= NULL
;
3316 wineItem
= TREEVIEW_GetNextListItem(infoPtr
, wineItem
))
3318 if (row
>= wineItem
->visibleOrder
3319 && row
< wineItem
->visibleOrder
+ wineItem
->iIntegral
)
3327 TREEVIEW_HitTest(TREEVIEW_INFO
*infoPtr
, LPTVHITTESTINFO lpht
)
3329 TREEVIEW_ITEM
*wineItem
;
3335 GetClientRect(infoPtr
->hwnd
, &rect
);
3342 status
|= TVHT_TOLEFT
;
3344 else if (x
> rect
.right
)
3346 status
|= TVHT_TORIGHT
;
3351 status
|= TVHT_ABOVE
;
3353 else if (y
> rect
.bottom
)
3355 status
|= TVHT_BELOW
;
3360 lpht
->flags
= status
;
3361 return (LRESULT
)(HTREEITEM
)NULL
;
3364 wineItem
= TREEVIEW_HitTestPoint(infoPtr
, lpht
->pt
);
3367 lpht
->flags
= TVHT_NOWHERE
;
3368 return (LRESULT
)(HTREEITEM
)NULL
;
3371 if (x
>= wineItem
->textOffset
+ wineItem
->textWidth
)
3373 lpht
->flags
= TVHT_ONITEMRIGHT
;
3375 else if (x
>= wineItem
->textOffset
)
3377 lpht
->flags
= TVHT_ONITEMLABEL
;
3379 else if (x
>= wineItem
->imageOffset
)
3381 lpht
->flags
= TVHT_ONITEMICON
;
3383 else if (x
>= wineItem
->stateOffset
)
3385 lpht
->flags
= TVHT_ONITEMSTATEICON
;
3387 else if (x
>= wineItem
->linesOffset
&& infoPtr
->dwStyle
& TVS_HASBUTTONS
)
3389 lpht
->flags
= TVHT_ONITEMBUTTON
;
3393 lpht
->flags
= TVHT_ONITEMINDENT
;
3396 lpht
->hItem
= wineItem
;
3397 TRACE("(%ld,%ld):result %x\n", lpht
->pt
.x
, lpht
->pt
.y
, lpht
->flags
);
3399 return (LRESULT
)wineItem
;
3402 /* Item Label Editing ***************************************************/
3405 TREEVIEW_GetEditControl(TREEVIEW_INFO
*infoPtr
)
3407 return (LRESULT
)infoPtr
->hwndEdit
;
3410 static LRESULT CALLBACK
3411 TREEVIEW_Edit_SubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
3413 TREEVIEW_INFO
*infoPtr
;
3414 BOOL bCancel
= FALSE
;
3421 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3423 TRACE("WM_PAINT start\n");
3424 rc
= CallWindowProcA(infoPtr
->wpEditOrig
, hwnd
, uMsg
, wParam
,
3426 TRACE("WM_PAINT done\n");
3432 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3433 if (infoPtr
->bIgnoreEditKillFocus
)
3440 return DLGC_WANTARROWS
| DLGC_WANTALLKEYS
;
3443 if (wParam
== (WPARAM
)VK_ESCAPE
)
3448 else if (wParam
== (WPARAM
)VK_RETURN
)
3456 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3458 return CallWindowProcA(infoPtr
->wpEditOrig
, hwnd
, uMsg
, wParam
,
3463 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3464 /* eg. Using a messagebox */
3466 infoPtr
= TREEVIEW_GetInfoPtr(GetParent(hwnd
));
3467 infoPtr
->bIgnoreEditKillFocus
= TRUE
;
3468 TREEVIEW_EndEditLabelNow(infoPtr
, bCancel
|| !infoPtr
->bLabelChanged
);
3469 infoPtr
->bIgnoreEditKillFocus
= FALSE
;
3475 /* should handle edit control messages here */
3478 TREEVIEW_Command(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
3480 TRACE("%x %ld\n", wParam
, lParam
);
3482 switch (HIWORD(wParam
))
3487 * Adjust the edit window size
3490 TREEVIEW_ITEM
*editItem
= infoPtr
->selectedItem
;
3491 HDC hdc
= GetDC(infoPtr
->hwndEdit
);
3494 HFONT hFont
, hOldFont
= 0;
3496 infoPtr
->bLabelChanged
= TRUE
;
3498 len
= GetWindowTextA(infoPtr
->hwndEdit
, buffer
, sizeof(buffer
));
3500 /* Select font to get the right dimension of the string */
3501 hFont
= (HFONT
)SendMessageA(infoPtr
->hwndEdit
, WM_GETFONT
, 0, 0);
3504 hOldFont
= SelectObject(hdc
, hFont
);
3507 if (GetTextExtentPoint32A(hdc
, buffer
, strlen(buffer
), &sz
))
3509 TEXTMETRICA textMetric
;
3511 /* Add Extra spacing for the next character */
3512 GetTextMetricsA(hdc
, &textMetric
);
3513 sz
.cx
+= (textMetric
.tmMaxCharWidth
* 2);
3515 sz
.cx
= max(sz
.cx
, textMetric
.tmMaxCharWidth
* 3);
3517 infoPtr
->clientWidth
- editItem
->textOffset
+ 2);
3519 SetWindowPos(infoPtr
->hwndEdit
,
3524 editItem
->rect
.bottom
- editItem
->rect
.top
+ 3,
3525 SWP_NOMOVE
| SWP_DRAWFRAME
);
3530 SelectObject(hdc
, hOldFont
);
3533 ReleaseDC(infoPtr
->hwnd
, hdc
);
3538 return SendMessageA(GetParent(infoPtr
->hwnd
), WM_COMMAND
, wParam
, lParam
);
3545 TREEVIEW_EditLabelA(TREEVIEW_INFO
*infoPtr
, HTREEITEM hItem
)
3547 HWND hwnd
= infoPtr
->hwnd
;
3550 TREEVIEW_ITEM
*editItem
= hItem
;
3551 HINSTANCE hinst
= (HINSTANCE
)GetWindowLongA(hwnd
, GWL_HINSTANCE
);
3554 TEXTMETRICA textMetric
;
3556 TRACE("%x %p\n", (unsigned)hwnd
, hItem
);
3557 if (!TREEVIEW_ValidItem(infoPtr
, editItem
))
3560 if (infoPtr
->hwndEdit
)
3561 return infoPtr
->hwndEdit
;
3563 infoPtr
->bLabelChanged
= FALSE
;
3565 /* Make sure that edit item is selected */
3566 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, hItem
, TVC_UNKNOWN
);
3567 TREEVIEW_EnsureVisible(infoPtr
, hItem
, TRUE
);
3569 TREEVIEW_UpdateDispInfo(infoPtr
, editItem
, TVIF_TEXT
);
3572 /* Select the font to get appropriate metric dimensions */
3573 if (infoPtr
->hFont
!= 0)
3575 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
3578 /* Get string length in pixels */
3579 GetTextExtentPoint32A(hdc
, editItem
->pszText
, strlen(editItem
->pszText
),
3582 /* Add Extra spacing for the next character */
3583 GetTextMetricsA(hdc
, &textMetric
);
3584 sz
.cx
+= (textMetric
.tmMaxCharWidth
* 2);
3586 sz
.cx
= max(sz
.cx
, textMetric
.tmMaxCharWidth
* 3);
3587 sz
.cx
= min(sz
.cx
, infoPtr
->clientWidth
- editItem
->textOffset
+ 2);
3589 if (infoPtr
->hFont
!= 0)
3591 SelectObject(hdc
, hOldFont
);
3594 ReleaseDC(hwnd
, hdc
);
3595 hwndEdit
= CreateWindowExA(WS_EX_LEFT
,
3598 WS_CHILD
| WS_BORDER
| ES_AUTOHSCROLL
|
3599 WS_CLIPSIBLINGS
| ES_WANTRETURN
|
3600 ES_LEFT
, editItem
->textOffset
- 2,
3601 editItem
->rect
.top
- 1, sz
.cx
+ 3,
3602 editItem
->rect
.bottom
-
3603 editItem
->rect
.top
+ 3, hwnd
, 0, hinst
, 0);
3604 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3606 infoPtr
->hwndEdit
= hwndEdit
;
3608 /* Get a 2D border. */
3609 SetWindowLongA(hwndEdit
, GWL_EXSTYLE
,
3610 GetWindowLongA(hwndEdit
, GWL_EXSTYLE
) & ~WS_EX_CLIENTEDGE
);
3611 SetWindowLongA(hwndEdit
, GWL_STYLE
,
3612 GetWindowLongA(hwndEdit
, GWL_STYLE
) | WS_BORDER
);
3614 SendMessageA(hwndEdit
, WM_SETFONT
,
3615 (WPARAM
)TREEVIEW_FontForItem(infoPtr
, editItem
), FALSE
);
3617 infoPtr
->wpEditOrig
= (WNDPROC
)SetWindowLongA(hwndEdit
, GWL_WNDPROC
,
3619 TREEVIEW_Edit_SubclassProc
);
3621 if (TREEVIEW_BeginLabelEditNotify(infoPtr
, editItem
))
3623 DestroyWindow(hwndEdit
);
3624 infoPtr
->hwndEdit
= 0;
3628 infoPtr
->selectedItem
= hItem
;
3629 SetWindowTextA(hwndEdit
, editItem
->pszText
);
3631 SendMessageA(hwndEdit
, EM_SETSEL
, 0, -1);
3632 ShowWindow(hwndEdit
, SW_SHOW
);
3639 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO
*infoPtr
, BOOL bCancel
)
3641 HWND hwnd
= infoPtr
->hwnd
;
3642 TREEVIEW_ITEM
*editedItem
= infoPtr
->selectedItem
;
3645 char tmpText
[1024] = { '\0' };
3648 if (!infoPtr
->hwndEdit
)
3651 tvdi
.hdr
.hwndFrom
= hwnd
;
3652 tvdi
.hdr
.idFrom
= GetWindowLongA(hwnd
, GWL_ID
);
3653 tvdi
.hdr
.code
= get_notifycode(infoPtr
, TVN_ENDLABELEDITW
);
3655 tvdi
.item
.hItem
= editedItem
;
3656 tvdi
.item
.state
= editedItem
->state
;
3657 tvdi
.item
.lParam
= editedItem
->lParam
;
3661 iLength
= GetWindowTextA(infoPtr
->hwndEdit
, tmpText
, 1023);
3663 if (iLength
>= 1023)
3665 ERR("Insufficient space to retrieve new item label\n");
3668 tvdi
.item
.pszText
= tmpText
;
3669 tvdi
.item
.cchTextMax
= iLength
+ 1;
3673 tvdi
.item
.pszText
= NULL
;
3674 tvdi
.item
.cchTextMax
= 0;
3677 bCommit
= (BOOL
)TREEVIEW_SendRealNotify(infoPtr
,
3678 (WPARAM
)tvdi
.hdr
.idFrom
, (LPARAM
)&tvdi
);
3680 if (!bCancel
&& bCommit
) /* Apply the changes */
3682 if (strcmp(tmpText
, editedItem
->pszText
) != 0)
3684 if (NULL
== COMCTL32_ReAlloc(editedItem
->pszText
, iLength
+ 1))
3686 ERR("OutOfMemory, cannot allocate space for label\n");
3687 DestroyWindow(infoPtr
->hwndEdit
);
3688 infoPtr
->hwndEdit
= 0;
3693 editedItem
->cchTextMax
= iLength
+ 1;
3694 lstrcpyA(editedItem
->pszText
, tmpText
);
3699 ShowWindow(infoPtr
->hwndEdit
, SW_HIDE
);
3700 DestroyWindow(infoPtr
->hwndEdit
);
3701 infoPtr
->hwndEdit
= 0;
3706 TREEVIEW_HandleTimer(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
3708 if (wParam
!= TV_EDIT_TIMER
)
3710 ERR("got unknown timer\n");
3714 KillTimer(infoPtr
->hwnd
, TV_EDIT_TIMER
);
3715 infoPtr
->Timer
&= ~TV_EDIT_TIMER_SET
;
3717 TREEVIEW_EditLabelA(infoPtr
, infoPtr
->selectedItem
);
3723 /* Mouse Tracking/Drag **************************************************/
3725 /***************************************************************************
3726 * This is quite unusual piece of code, but that's how it's implemented in
3730 TREEVIEW_TrackMouse(TREEVIEW_INFO
*infoPtr
, POINT pt
)
3732 INT cxDrag
= GetSystemMetrics(SM_CXDRAG
);
3733 INT cyDrag
= GetSystemMetrics(SM_CYDRAG
);
3737 r
.top
= pt
.y
- cyDrag
;
3738 r
.left
= pt
.x
- cxDrag
;
3739 r
.bottom
= pt
.y
+ cyDrag
;
3740 r
.right
= pt
.x
+ cxDrag
;
3742 SetCapture(infoPtr
->hwnd
);
3746 if (PeekMessageA(&msg
, 0, 0, 0, PM_REMOVE
| PM_NOYIELD
))
3748 if (msg
.message
== WM_MOUSEMOVE
)
3750 pt
.x
= SLOWORD(msg
.lParam
);
3751 pt
.y
= SHIWORD(msg
.lParam
);
3752 if (PtInRect(&r
, pt
))
3760 else if (msg
.message
>= WM_LBUTTONDOWN
&&
3761 msg
.message
<= WM_RBUTTONDBLCLK
)
3763 if (msg
.message
== WM_RBUTTONUP
)
3764 TREEVIEW_RButtonUp(infoPtr
, &pt
);
3768 DispatchMessageA(&msg
);
3771 if (GetCapture() != infoPtr
->hwnd
)
3781 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
3783 TREEVIEW_ITEM
*wineItem
;
3787 SetFocus(infoPtr
->hwnd
);
3789 if (infoPtr
->Timer
& TV_EDIT_TIMER_SET
)
3791 /* If there is pending 'edit label' event - kill it now */
3792 KillTimer(infoPtr
->hwnd
, TV_EDIT_TIMER
);
3795 hit
.pt
.x
= SLOWORD(lParam
);
3796 hit
.pt
.y
= SHIWORD(lParam
);
3798 wineItem
= (TREEVIEW_ITEM
*)TREEVIEW_HitTest(infoPtr
, &hit
);
3801 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr
, wineItem
));
3803 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_DBLCLK
) == FALSE
)
3807 case TVHT_ONITEMRIGHT
:
3808 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3811 case TVHT_ONITEMINDENT
:
3812 if (!(infoPtr
->dwStyle
& TVS_HASLINES
))
3818 int level
= hit
.pt
.x
/ infoPtr
->uIndent
;
3819 if (!(infoPtr
->dwStyle
& TVS_LINESATROOT
)) level
++;
3821 while (wineItem
->iLevel
> level
)
3823 wineItem
= wineItem
->parent
;
3829 case TVHT_ONITEMLABEL
:
3830 case TVHT_ONITEMICON
:
3831 case TVHT_ONITEMBUTTON
:
3832 TREEVIEW_Toggle(infoPtr
, wineItem
, TRUE
);
3835 case TVHT_ONITEMSTATEICON
:
3836 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
3837 TREEVIEW_ToggleItemState(infoPtr
, wineItem
);
3839 TREEVIEW_Toggle(infoPtr
, wineItem
, TRUE
);
3848 TREEVIEW_LButtonDown(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
3850 HWND hwnd
= infoPtr
->hwnd
;
3855 /* If Edit control is active - kill it and return.
3856 * The best way to do it is to set focus to itself.
3857 * Edit control subclassed procedure will automatically call
3860 if (infoPtr
->hwndEdit
)
3866 ht
.pt
.x
= SLOWORD(lParam
);
3867 ht
.pt
.y
= SHIWORD(lParam
);
3869 TREEVIEW_HitTest(infoPtr
, &ht
);
3870 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr
, ht
.hItem
));
3872 /* update focusedItem and redraw both items */
3873 if(ht
.hItem
&& (ht
.flags
& TVHT_ONITEM
))
3875 infoPtr
->focusedItem
= ht
.hItem
;
3876 InvalidateRect(hwnd
, &(((HTREEITEM
)(ht
.hItem
))->rect
), TRUE
);
3878 if(infoPtr
->selectedItem
)
3879 InvalidateRect(hwnd
, &(infoPtr
->selectedItem
->rect
), TRUE
);
3882 bTrack
= (ht
.flags
& TVHT_ONITEM
)
3883 && !(infoPtr
->dwStyle
& TVS_DISABLEDRAGDROP
);
3885 /* Send NM_CLICK right away */
3887 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_CLICK
))
3890 if (ht
.flags
& TVHT_ONITEMBUTTON
)
3892 TREEVIEW_Toggle(infoPtr
, ht
.hItem
, TRUE
);
3896 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
3897 if (TREEVIEW_TrackMouse(infoPtr
, ht
.pt
))
3899 TREEVIEW_SendTreeviewDnDNotify(infoPtr
, TVN_BEGINDRAGW
, ht
.hItem
, ht
.pt
);
3900 infoPtr
->dropItem
= ht
.hItem
;
3902 /* clean up focusedItem as we dragged and won't select this item */
3903 if(infoPtr
->focusedItem
)
3905 /* refresh the item that was focused */
3906 tempItem
= infoPtr
->focusedItem
;
3907 infoPtr
->focusedItem
= 0;
3908 InvalidateRect(infoPtr
->hwnd
, &tempItem
->rect
, TRUE
);
3910 /* refresh the selected item to return the filled background */
3911 InvalidateRect(infoPtr
->hwnd
, &(infoPtr
->selectedItem
->rect
), TRUE
);
3918 if (TREEVIEW_SendSimpleNotify(infoPtr
, NM_CLICK
))
3922 * If the style allows editing and the node is already selected
3923 * and the click occurred on the item label...
3925 if ((infoPtr
->dwStyle
& TVS_EDITLABELS
) &&
3926 (ht
.flags
& TVHT_ONITEMLABEL
) && (infoPtr
->selectedItem
== ht
.hItem
))
3928 if (infoPtr
->Timer
& TV_EDIT_TIMER_SET
)
3929 KillTimer(hwnd
, TV_EDIT_TIMER
);
3931 SetTimer(hwnd
, TV_EDIT_TIMER
, GetDoubleClickTime(), 0);
3932 infoPtr
->Timer
|= TV_EDIT_TIMER_SET
;
3934 else if (ht
.flags
& TVHT_ONITEM
) /* select the item if the hit was inside of the icon or text */
3937 * if we are TVS_SINGLEEXPAND then we want this single click to
3938 * do a bunch of things.
3940 if((infoPtr
->dwStyle
& TVS_SINGLEEXPAND
) &&
3941 (infoPtr
->hwndEdit
== 0))
3943 TREEVIEW_ITEM
*SelItem
;
3946 * Send the notification
3948 TREEVIEW_SendTreeviewNotify(infoPtr
, TVN_SINGLEEXPAND
, TVC_UNKNOWN
, TVIF_HANDLE
| TVIF_PARAM
, ht
.hItem
, 0);
3951 * Close the previous selection all the way to the root
3952 * as long as the new selection is not a child
3954 if((infoPtr
->selectedItem
)
3955 && (infoPtr
->selectedItem
!= ht
.hItem
))
3957 BOOL closeit
= TRUE
;
3960 /* determine if the hitItem is a child of the currently selected item */
3961 while(closeit
&& SelItem
&& TREEVIEW_ValidItem(infoPtr
, SelItem
) && (SelItem
!= infoPtr
->root
))
3963 closeit
= (SelItem
!= infoPtr
->selectedItem
);
3964 SelItem
= SelItem
->parent
;
3969 if(TREEVIEW_ValidItem(infoPtr
, infoPtr
->selectedItem
))
3970 SelItem
= infoPtr
->selectedItem
;
3972 while(SelItem
&& (SelItem
!= ht
.hItem
) && TREEVIEW_ValidItem(infoPtr
, SelItem
) && (SelItem
!= infoPtr
->root
))
3974 TREEVIEW_Collapse(infoPtr
, SelItem
, FALSE
, FALSE
);
3975 SelItem
= SelItem
->parent
;
3981 * Expand the current item
3983 TREEVIEW_Expand(infoPtr
, ht
.hItem
, TVE_TOGGLE
, FALSE
);
3986 /* Select the current item */
3987 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, ht
.hItem
, TVC_BYMOUSE
);
3989 else if (ht
.flags
& TVHT_ONITEMSTATEICON
)
3991 /* TVS_CHECKBOXES requires us to toggle the current state */
3992 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
3993 TREEVIEW_ToggleItemState(infoPtr
, ht
.hItem
);
4003 TREEVIEW_RButtonDown(TREEVIEW_INFO
*infoPtr
, LPARAM lParam
)
4007 if (infoPtr
->hwndEdit
)
4009 SetFocus(infoPtr
->hwnd
);
4013 ht
.pt
.x
= SLOWORD(lParam
);
4014 ht
.pt
.y
= SHIWORD(lParam
);
4016 TREEVIEW_HitTest(infoPtr
, &ht
);
4018 if (TREEVIEW_TrackMouse(infoPtr
, ht
.pt
))
4022 TREEVIEW_SendTreeviewDnDNotify(infoPtr
, TVN_BEGINRDRAGW
, ht
.hItem
, ht
.pt
);
4023 infoPtr
->dropItem
= ht
.hItem
;
4028 SetFocus(infoPtr
->hwnd
);
4029 TREEVIEW_SendSimpleNotify(infoPtr
, NM_RCLICK
);
4036 TREEVIEW_RButtonUp(TREEVIEW_INFO
*infoPtr
, LPPOINT pPt
)
4043 TREEVIEW_CreateDragImage(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
4045 TREEVIEW_ITEM
*dragItem
= (HTREEITEM
)lParam
;
4049 HBITMAP hbmp
, hOldbmp
;
4056 if (!(infoPtr
->himlNormal
))
4059 if (!dragItem
|| !TREEVIEW_ValidItem(infoPtr
, dragItem
))
4062 TREEVIEW_UpdateDispInfo(infoPtr
, dragItem
, TVIF_TEXT
);
4064 hwtop
= GetDesktopWindow();
4065 htopdc
= GetDC(hwtop
);
4066 hdc
= CreateCompatibleDC(htopdc
);
4068 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
4069 GetTextExtentPoint32A(hdc
, dragItem
->pszText
, lstrlenA(dragItem
->pszText
),
4071 TRACE("%ld %ld %s %d\n", size
.cx
, size
.cy
, dragItem
->pszText
,
4072 lstrlenA(dragItem
->pszText
));
4073 hbmp
= CreateCompatibleBitmap(htopdc
, size
.cx
, size
.cy
);
4074 hOldbmp
= SelectObject(hdc
, hbmp
);
4076 ImageList_GetIconSize(infoPtr
->himlNormal
, &cx
, &cy
);
4081 infoPtr
->dragList
= ImageList_Create(size
.cx
, size
.cy
, ILC_COLOR
, 10, 10);
4082 ImageList_Draw(infoPtr
->himlNormal
, dragItem
->iImage
, hdc
, 0, 0,
4086 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4087 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4090 /* draw item text */
4092 SetRect(&rc
, cx
, 0, size
.cx
, size
.cy
);
4093 DrawTextA(hdc
, dragItem
->pszText
, lstrlenA(dragItem
->pszText
), &rc
,
4095 SelectObject(hdc
, hOldFont
);
4096 SelectObject(hdc
, hOldbmp
);
4098 ImageList_Add(infoPtr
->dragList
, hbmp
, 0);
4102 ReleaseDC(hwtop
, htopdc
);
4104 return (LRESULT
)infoPtr
->dragList
;
4107 /* Selection ************************************************************/
4110 TREEVIEW_DoSelectItem(TREEVIEW_INFO
*infoPtr
, INT action
, HTREEITEM newSelect
,
4113 TREEVIEW_ITEM
*prevSelect
;
4116 assert(newSelect
== NULL
|| TREEVIEW_ValidItem(infoPtr
, newSelect
));
4118 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4119 newSelect
, TREEVIEW_ItemName(newSelect
), action
, cause
,
4120 newSelect
? newSelect
->state
: 0);
4122 /* reset and redraw focusedItem if focusedItem was set so we don't */
4123 /* have to worry about the previously focused item when we set a new one */
4124 if(infoPtr
->focusedItem
)
4126 rcFocused
= (infoPtr
->focusedItem
)->rect
;
4127 infoPtr
->focusedItem
= 0;
4128 InvalidateRect(infoPtr
->hwnd
, &rcFocused
, TRUE
);
4134 prevSelect
= infoPtr
->selectedItem
;
4136 if (prevSelect
== newSelect
)
4139 if (TREEVIEW_SendTreeviewNotify(infoPtr
,
4142 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
,
4148 prevSelect
->state
&= ~TVIS_SELECTED
;
4150 newSelect
->state
|= TVIS_SELECTED
;
4152 infoPtr
->selectedItem
= newSelect
;
4154 TREEVIEW_EnsureVisible(infoPtr
, infoPtr
->selectedItem
, FALSE
);
4156 TREEVIEW_SendTreeviewNotify(infoPtr
,
4159 TVIF_HANDLE
| TVIF_STATE
| TVIF_PARAM
,
4162 TREEVIEW_Invalidate(infoPtr
, prevSelect
);
4163 TREEVIEW_Invalidate(infoPtr
, newSelect
);
4166 case TVGN_DROPHILITE
:
4167 prevSelect
= infoPtr
->dropItem
;
4170 prevSelect
->state
&= ~TVIS_DROPHILITED
;
4172 infoPtr
->dropItem
= newSelect
;
4175 newSelect
->state
|= TVIS_DROPHILITED
;
4177 TREEVIEW_Invalidate(infoPtr
, prevSelect
);
4178 TREEVIEW_Invalidate(infoPtr
, newSelect
);
4181 case TVGN_FIRSTVISIBLE
:
4182 TREEVIEW_EnsureVisible(infoPtr
, newSelect
, FALSE
);
4183 TREEVIEW_SetFirstVisible(infoPtr
, newSelect
, TRUE
);
4184 TREEVIEW_Invalidate(infoPtr
, NULL
);
4188 TRACE("Leaving state %d\n", newSelect
? newSelect
->state
: 0);
4192 /* FIXME: handle NM_KILLFOCUS etc */
4194 TREEVIEW_SelectItem(TREEVIEW_INFO
*infoPtr
, INT wParam
, HTREEITEM item
)
4196 if (item
!= NULL
&& !TREEVIEW_ValidItem(infoPtr
, item
))
4199 TRACE("%p (%s) %d\n", item
, TREEVIEW_ItemName(item
), wParam
);
4201 if (!TREEVIEW_DoSelectItem(infoPtr
, wParam
, item
, TVC_UNKNOWN
))
4207 /*************************************************************************
4208 * TREEVIEW_ProcessLetterKeys
4210 * Processes keyboard messages generated by pressing the letter keys
4212 * What this does is perform a case insensitive search from the
4213 * current position with the following quirks:
4214 * - If two chars or more are pressed in quick succession we search
4215 * for the corresponding string (e.g. 'abc').
4216 * - If there is a delay we wipe away the current search string and
4217 * restart with just that char.
4218 * - If the user keeps pressing the same character, whether slowly or
4219 * fast, so that the search string is entirely composed of this
4220 * character ('aaaaa' for instance), then we search for first item
4221 * that starting with that character.
4222 * - If the user types the above character in quick succession, then
4223 * we must also search for the corresponding string ('aaaaa'), and
4224 * go to that string if there is a match.
4232 * - The current implementation has a list of characters it will
4233 * accept and it ignores averything else. In particular it will
4234 * ignore accentuated characters which seems to match what
4235 * Windows does. But I'm not sure it makes sense to follow
4237 * - We don't sound a beep when the search fails.
4238 * - The search should start from the focused item, not from the selected
4239 * item. One reason for this is to allow for multiple selections in trees.
4240 * But currently infoPtr->focusedItem does not seem very usable.
4244 * TREEVIEW_ProcessLetterKeys
4246 static INT
TREEVIEW_ProcessLetterKeys(
4247 HWND hwnd
, /* handle to the window */
4248 WPARAM charCode
, /* the character code, the actual character */
4249 LPARAM keyData
/* key data */
4252 TREEVIEW_INFO
*infoPtr
;
4254 HTREEITEM endidx
,idx
;
4256 CHAR buffer
[MAX_PATH
];
4257 DWORD timestamp
,elapsed
;
4259 /* simple parameter checking */
4260 if (!hwnd
|| !charCode
|| !keyData
)
4263 infoPtr
=(TREEVIEW_INFO
*)GetWindowLongA(hwnd
, 0);
4267 /* only allow the valid WM_CHARs through */
4268 if (!isalnum(charCode
) &&
4269 charCode
!= '.' && charCode
!= '`' && charCode
!= '!' &&
4270 charCode
!= '@' && charCode
!= '#' && charCode
!= '$' &&
4271 charCode
!= '%' && charCode
!= '^' && charCode
!= '&' &&
4272 charCode
!= '*' && charCode
!= '(' && charCode
!= ')' &&
4273 charCode
!= '-' && charCode
!= '_' && charCode
!= '+' &&
4274 charCode
!= '=' && charCode
!= '\\'&& charCode
!= ']' &&
4275 charCode
!= '}' && charCode
!= '[' && charCode
!= '{' &&
4276 charCode
!= '/' && charCode
!= '?' && charCode
!= '>' &&
4277 charCode
!= '<' && charCode
!= ',' && charCode
!= '~')
4280 /* compute how much time elapsed since last keypress */
4281 timestamp
= GetTickCount();
4282 if (timestamp
> infoPtr
->lastKeyPressTimestamp
) {
4283 elapsed
=timestamp
-infoPtr
->lastKeyPressTimestamp
;
4285 elapsed
=infoPtr
->lastKeyPressTimestamp
-timestamp
;
4288 /* update the search parameters */
4289 infoPtr
->lastKeyPressTimestamp
=timestamp
;
4290 if (elapsed
< KEY_DELAY
) {
4291 if (infoPtr
->nSearchParamLength
< sizeof(infoPtr
->szSearchParam
)) {
4292 infoPtr
->szSearchParam
[infoPtr
->nSearchParamLength
++]=charCode
;
4294 if (infoPtr
->charCode
!= charCode
) {
4295 infoPtr
->charCode
=charCode
=0;
4298 infoPtr
->charCode
=charCode
;
4299 infoPtr
->szSearchParam
[0]=charCode
;
4300 infoPtr
->nSearchParamLength
=1;
4301 /* Redundant with the 1 char string */
4305 /* and search from the current position */
4307 if (infoPtr
->selectedItem
!= NULL
) {
4308 endidx
=infoPtr
->selectedItem
;
4309 /* if looking for single character match,
4310 * then we must always move forward
4312 if (infoPtr
->nSearchParamLength
== 1)
4313 idx
=TREEVIEW_GetNextListItem(infoPtr
,endidx
);
4318 idx
=infoPtr
->root
->firstChild
;
4324 idx
=infoPtr
->root
->firstChild
;
4328 ZeroMemory(&item
, sizeof(item
));
4329 item
.mask
= TVIF_TEXT
;
4331 item
.pszText
= buffer
;
4332 item
.cchTextMax
= sizeof(buffer
);
4333 TREEVIEW_GetItemA( infoPtr
, &item
);
4335 /* check for a match */
4336 if (strncasecmp(item
.pszText
,infoPtr
->szSearchParam
,infoPtr
->nSearchParamLength
) == 0) {
4339 } else if ( (charCode
!= 0) && (nItem
== NULL
) &&
4340 (nItem
!= infoPtr
->selectedItem
) &&
4341 (strncasecmp(item
.pszText
,infoPtr
->szSearchParam
,1) == 0) ) {
4342 /* This would work but we must keep looking for a longer match */
4345 idx
=TREEVIEW_GetNextListItem(infoPtr
,idx
);
4346 } while (idx
!= endidx
);
4348 if (nItem
!= NULL
) {
4349 if (TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, nItem
, TVC_BYKEYBOARD
)) {
4350 TREEVIEW_EnsureVisible(infoPtr
, nItem
, FALSE
);
4357 /* Scrolling ************************************************************/
4360 TREEVIEW_EnsureVisible(TREEVIEW_INFO
*infoPtr
, HTREEITEM item
, BOOL bHScroll
)
4362 HTREEITEM newFirstVisible
= NULL
;
4365 if (!TREEVIEW_ValidItem(infoPtr
, item
))
4368 if (!ISVISIBLE(item
))
4370 /* Expand parents as necessary. */
4373 /* see if we are trying to ensure that root is vislble */
4374 if((item
!= infoPtr
->root
) && TREEVIEW_ValidItem(infoPtr
, item
))
4375 parent
= item
->parent
;
4377 parent
= item
; /* this item is the topmost item */
4379 while (parent
!= infoPtr
->root
)
4381 if (!(parent
->state
& TVIS_EXPANDED
))
4382 TREEVIEW_Expand(infoPtr
, parent
, FALSE
, FALSE
);
4384 parent
= parent
->parent
;
4388 TRACE("%p (%s) %ld - %ld\n", item
, TREEVIEW_ItemName(item
), item
->visibleOrder
,
4389 infoPtr
->firstVisible
->visibleOrder
);
4391 visible_pos
= item
->visibleOrder
- infoPtr
->firstVisible
->visibleOrder
;
4393 if (visible_pos
< 0)
4395 /* item is before the start of the list: put it at the top. */
4396 newFirstVisible
= item
;
4398 else if (visible_pos
>= TREEVIEW_GetVisibleCount(infoPtr
)
4399 /* Sometimes, before we are displayed, GVC is 0, causing us to
4400 * spuriously scroll up. */
4403 /* item is past the end of the list. */
4404 int scroll
= visible_pos
- TREEVIEW_GetVisibleCount(infoPtr
);
4406 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, infoPtr
->firstVisible
,
4412 /* Scroll window so item's text is visible as much as possible */
4413 /* Calculation of amount of extra space is taken from EditLabel code */
4415 TEXTMETRICA textMetric
;
4416 HDC hdc
= GetWindowDC(infoPtr
->hwnd
);
4418 x
= item
->textWidth
;
4420 GetTextMetricsA(hdc
, &textMetric
);
4421 ReleaseDC(infoPtr
->hwnd
, hdc
);
4423 x
+= (textMetric
.tmMaxCharWidth
* 2);
4424 x
= max(x
, textMetric
.tmMaxCharWidth
* 3);
4426 if (item
->textOffset
< 0)
4427 pos
= item
->textOffset
;
4428 else if (item
->textOffset
+ x
> infoPtr
->clientWidth
)
4430 if (x
> infoPtr
->clientWidth
)
4431 pos
= item
->textOffset
;
4433 pos
= item
->textOffset
+ x
- infoPtr
->clientWidth
;
4438 TREEVIEW_HScroll(infoPtr
, MAKEWPARAM(SB_THUMBPOSITION
, infoPtr
->scrollX
+ pos
));
4441 if (newFirstVisible
!= NULL
&& newFirstVisible
!= infoPtr
->firstVisible
)
4443 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
, TRUE
);
4452 TREEVIEW_SetFirstVisible(TREEVIEW_INFO
*infoPtr
,
4453 TREEVIEW_ITEM
*newFirstVisible
,
4454 BOOL bUpdateScrollPos
)
4458 TRACE("%p: %s\n", newFirstVisible
, TREEVIEW_ItemName(newFirstVisible
));
4460 if (newFirstVisible
!= NULL
)
4462 /* Prevent an empty gap from appearing at the bottom... */
4463 gap_size
= TREEVIEW_GetVisibleCount(infoPtr
)
4464 - infoPtr
->maxVisibleOrder
+ newFirstVisible
->visibleOrder
;
4468 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, newFirstVisible
,
4471 /* ... unless we just don't have enough items. */
4472 if (newFirstVisible
== NULL
)
4473 newFirstVisible
= infoPtr
->root
->firstChild
;
4477 if (infoPtr
->firstVisible
!= newFirstVisible
)
4479 if (infoPtr
->firstVisible
== NULL
|| newFirstVisible
== NULL
)
4481 infoPtr
->firstVisible
= newFirstVisible
;
4482 TREEVIEW_Invalidate(infoPtr
, NULL
);
4486 TREEVIEW_ITEM
*item
;
4487 int scroll
= infoPtr
->uItemHeight
*
4488 (infoPtr
->firstVisible
->visibleOrder
4489 - newFirstVisible
->visibleOrder
);
4491 infoPtr
->firstVisible
= newFirstVisible
;
4493 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
4494 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
4496 item
->rect
.top
+= scroll
;
4497 item
->rect
.bottom
+= scroll
;
4500 if (bUpdateScrollPos
)
4501 SetScrollPos(infoPtr
->hwnd
, SB_VERT
,
4502 newFirstVisible
->visibleOrder
, TRUE
);
4504 ScrollWindow(infoPtr
->hwnd
, 0, scroll
, NULL
, NULL
);
4505 UpdateWindow(infoPtr
->hwnd
);
4510 /************************************************************************
4511 * VScroll is always in units of visible items. i.e. we always have a
4512 * visible item aligned to the top of the control. (Unless we have no
4516 TREEVIEW_VScroll(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4518 TREEVIEW_ITEM
*oldFirstVisible
= infoPtr
->firstVisible
;
4519 TREEVIEW_ITEM
*newFirstVisible
= NULL
;
4521 int nScrollCode
= LOWORD(wParam
);
4523 TRACE("wp %x\n", wParam
);
4525 if (!(infoPtr
->uInternalStatus
& TV_VSCROLL
))
4528 if (infoPtr
->hwndEdit
)
4529 SetFocus(infoPtr
->hwnd
);
4531 if (!oldFirstVisible
)
4533 assert(infoPtr
->root
->firstChild
== NULL
);
4537 switch (nScrollCode
)
4540 newFirstVisible
= infoPtr
->root
->firstChild
;
4544 newFirstVisible
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
4548 newFirstVisible
= TREEVIEW_GetPrevListItem(infoPtr
, oldFirstVisible
);
4552 newFirstVisible
= TREEVIEW_GetNextListItem(infoPtr
, oldFirstVisible
);
4556 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, oldFirstVisible
,
4557 -max(1, TREEVIEW_GetVisibleCount(infoPtr
)));
4561 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
, oldFirstVisible
,
4562 max(1, TREEVIEW_GetVisibleCount(infoPtr
)));
4566 case SB_THUMBPOSITION
:
4567 newFirstVisible
= TREEVIEW_GetListItem(infoPtr
,
4568 infoPtr
->root
->firstChild
,
4569 (LONG
)(SHORT
)HIWORD(wParam
));
4576 if (newFirstVisible
!= NULL
)
4578 if (newFirstVisible
!= oldFirstVisible
)
4579 TREEVIEW_SetFirstVisible(infoPtr
, newFirstVisible
,
4580 nScrollCode
!= SB_THUMBTRACK
);
4581 else if (nScrollCode
== SB_THUMBPOSITION
)
4582 SetScrollPos(infoPtr
->hwnd
, SB_VERT
,
4583 newFirstVisible
->visibleOrder
, TRUE
);
4590 TREEVIEW_HScroll(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4593 int scrollX
= infoPtr
->scrollX
;
4594 int nScrollCode
= LOWORD(wParam
);
4596 TRACE("wp %x\n", wParam
);
4598 if (!(infoPtr
->uInternalStatus
& TV_HSCROLL
))
4601 if (infoPtr
->hwndEdit
)
4602 SetFocus(infoPtr
->hwnd
);
4604 maxWidth
= infoPtr
->treeWidth
- infoPtr
->clientWidth
;
4605 /* shall never occur */
4612 switch (nScrollCode
)
4615 scrollX
-= infoPtr
->uItemHeight
;
4618 scrollX
+= infoPtr
->uItemHeight
;
4621 scrollX
-= infoPtr
->clientWidth
;
4624 scrollX
+= infoPtr
->clientWidth
;
4628 case SB_THUMBPOSITION
:
4629 scrollX
= (int)(SHORT
)HIWORD(wParam
);
4636 if (scrollX
> maxWidth
)
4638 else if (scrollX
< 0)
4642 if (scrollX
!= infoPtr
->scrollX
)
4644 TREEVIEW_ITEM
*item
;
4645 LONG scroll_pixels
= infoPtr
->scrollX
- scrollX
;
4647 for (item
= infoPtr
->root
->firstChild
; item
!= NULL
;
4648 item
= TREEVIEW_GetNextListItem(infoPtr
, item
))
4650 item
->linesOffset
+= scroll_pixels
;
4651 item
->stateOffset
+= scroll_pixels
;
4652 item
->imageOffset
+= scroll_pixels
;
4653 item
->textOffset
+= scroll_pixels
;
4656 ScrollWindow(infoPtr
->hwnd
, scroll_pixels
, 0, NULL
, NULL
);
4657 infoPtr
->scrollX
= scrollX
;
4658 UpdateWindow(infoPtr
->hwnd
);
4661 if (nScrollCode
!= SB_THUMBTRACK
)
4662 SetScrollPos(infoPtr
->hwnd
, SB_HORZ
, scrollX
, TRUE
);
4668 TREEVIEW_MouseWheel(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4671 UINT pulScrollLines
= 3;
4673 if (infoPtr
->firstVisible
== NULL
)
4676 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
, 0, &pulScrollLines
, 0);
4678 gcWheelDelta
= -(short)HIWORD(wParam
);
4679 pulScrollLines
*= (gcWheelDelta
/ WHEEL_DELTA
);
4681 if (abs(gcWheelDelta
) >= WHEEL_DELTA
&& pulScrollLines
)
4683 int newDy
= infoPtr
->firstVisible
->visibleOrder
+ pulScrollLines
;
4684 int maxDy
= infoPtr
->maxVisibleOrder
;
4692 TREEVIEW_VScroll(infoPtr
, MAKEWPARAM(SB_THUMBPOSITION
, newDy
));
4697 /* Create/Destroy *******************************************************/
4700 TREEVIEW_Create(HWND hwnd
)
4703 TREEVIEW_INFO
*infoPtr
;
4705 TRACE("wnd %p, style %lx\n", hwnd
, GetWindowLongA(hwnd
, GWL_STYLE
));
4707 infoPtr
= (TREEVIEW_INFO
*)COMCTL32_Alloc(sizeof(TREEVIEW_INFO
));
4709 if (infoPtr
== NULL
)
4711 ERR("could not allocate info memory!\n");
4715 SetWindowLongA(hwnd
, 0, (DWORD
)infoPtr
);
4717 infoPtr
->hwnd
= hwnd
;
4718 infoPtr
->dwStyle
= GetWindowLongA(hwnd
, GWL_STYLE
);
4719 infoPtr
->uInternalStatus
= 0;
4721 infoPtr
->uNumItems
= 0;
4722 infoPtr
->cdmode
= 0;
4723 infoPtr
->uScrollTime
= 300; /* milliseconds */
4724 infoPtr
->bRedraw
= TRUE
;
4726 GetClientRect(hwnd
, &rcClient
);
4728 /* No scroll bars yet. */
4729 infoPtr
->clientWidth
= rcClient
.right
;
4730 infoPtr
->clientHeight
= rcClient
.bottom
;
4732 infoPtr
->treeWidth
= 0;
4733 infoPtr
->treeHeight
= 0;
4735 infoPtr
->uIndent
= 19;
4736 infoPtr
->selectedItem
= 0;
4737 infoPtr
->focusedItem
= 0;
4739 infoPtr
->firstVisible
= 0;
4740 infoPtr
->maxVisibleOrder
= 0;
4741 infoPtr
->dropItem
= 0;
4742 infoPtr
->insertMarkItem
= 0;
4743 infoPtr
->insertBeforeorAfter
= 0;
4746 infoPtr
->scrollX
= 0;
4748 infoPtr
->clrBk
= GetSysColor(COLOR_WINDOW
);
4749 infoPtr
->clrText
= -1; /* use system color */
4750 infoPtr
->clrLine
= RGB(128, 128, 128);
4751 infoPtr
->clrInsertMark
= GetSysColor(COLOR_BTNTEXT
);
4755 infoPtr
->hwndEdit
= 0;
4756 infoPtr
->wpEditOrig
= NULL
;
4757 infoPtr
->bIgnoreEditKillFocus
= FALSE
;
4758 infoPtr
->bLabelChanged
= FALSE
;
4760 infoPtr
->himlNormal
= NULL
;
4761 infoPtr
->himlState
= NULL
;
4762 infoPtr
->normalImageWidth
= 0;
4763 infoPtr
->normalImageHeight
= 0;
4764 infoPtr
->stateImageWidth
= 0;
4765 infoPtr
->stateImageHeight
= 0;
4767 infoPtr
->items
= DPA_Create(16);
4769 infoPtr
->hFont
= GetStockObject(DEFAULT_GUI_FONT
);
4770 infoPtr
->hBoldFont
= TREEVIEW_CreateBoldFont(infoPtr
->hFont
);
4772 infoPtr
->uItemHeight
= TREEVIEW_NaturalHeight(infoPtr
);
4774 infoPtr
->root
= TREEVIEW_AllocateItem(infoPtr
);
4775 infoPtr
->root
->state
= TVIS_EXPANDED
;
4776 infoPtr
->root
->iLevel
= -1;
4777 infoPtr
->root
->visibleOrder
= -1;
4779 infoPtr
->hwndNotify
= GetParent(hwnd
);
4781 infoPtr
->bTransparent
= ( GetWindowLongA( hwnd
, GWL_STYLE
) & TBSTYLE_FLAT
);
4784 infoPtr
->hwndToolTip
= 0;
4786 infoPtr
->bUnicode
= IsWindowUnicode (hwnd
);
4788 /* Determine what type of notify should be issued */
4789 /* sets infoPtr->bNtfUnicode */
4790 TREEVIEW_NotifyFormat(infoPtr
, infoPtr
->hwndNotify
, NF_REQUERY
);
4792 if (!(infoPtr
->dwStyle
& TVS_NOTOOLTIPS
))
4793 infoPtr
->hwndToolTip
= COMCTL32_CreateToolTip(hwnd
);
4795 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
4798 HBITMAP hbm
, hbmOld
;
4802 infoPtr
->himlState
=
4803 ImageList_Create(16, 16, ILC_COLOR
| ILC_MASK
, 3, 0);
4805 hdc
= CreateCompatibleDC(0);
4806 hbm
= CreateCompatibleBitmap(hdc
, 48, 16);
4807 hbmOld
= SelectObject(hdc
, hbm
);
4809 rc
.left
= 0; rc
.top
= 0;
4810 rc
.right
= 48; rc
.bottom
= 16;
4811 FillRect(hdc
, &rc
, (HBRUSH
)(COLOR_WINDOW
+1));
4813 rc
.left
= 18; rc
.top
= 2;
4814 rc
.right
= 30; rc
.bottom
= 14;
4815 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
4816 DFCS_BUTTONCHECK
|DFCS_FLAT
);
4818 rc
.left
= 34; rc
.right
= 46;
4819 DrawFrameControl(hdc
, &rc
, DFC_BUTTON
,
4820 DFCS_BUTTONCHECK
|DFCS_FLAT
|DFCS_CHECKED
);
4822 nIndex
= ImageList_AddMasked(infoPtr
->himlState
, hbm
,
4823 GetSysColor(COLOR_WINDOW
));
4824 TRACE("chckbox index %d\n", nIndex
);
4825 SelectObject(hdc
, hbmOld
);
4829 infoPtr
->stateImageWidth
= 16;
4830 infoPtr
->stateImageHeight
= 16;
4837 TREEVIEW_Destroy(TREEVIEW_INFO
*infoPtr
)
4841 TREEVIEW_RemoveTree(infoPtr
);
4843 /* tool tip is automatically destroyed: we are its owner */
4845 /* Restore original wndproc */
4846 if (infoPtr
->hwndEdit
)
4847 SetWindowLongA(infoPtr
->hwndEdit
, GWL_WNDPROC
,
4848 (LONG
)infoPtr
->wpEditOrig
);
4850 /* Deassociate treeview from the window before doing anything drastic. */
4851 SetWindowLongA(infoPtr
->hwnd
, 0, (LONG
)NULL
);
4853 DeleteObject(infoPtr
->hBoldFont
);
4854 COMCTL32_Free(infoPtr
);
4859 /* Miscellaneous Messages ***********************************************/
4862 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO
*infoPtr
, WPARAM key
)
4870 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4871 SCROLL_ENTRY(SB_VERT
, SB_PAGEUP
), /* VK_PRIOR */
4872 SCROLL_ENTRY(SB_VERT
, SB_PAGEDOWN
), /* VK_NEXT */
4873 SCROLL_ENTRY(SB_VERT
, SB_BOTTOM
), /* VK_END */
4874 SCROLL_ENTRY(SB_VERT
, SB_TOP
), /* VK_HOME */
4875 SCROLL_ENTRY(SB_HORZ
, SB_LINEUP
), /* VK_LEFT */
4876 SCROLL_ENTRY(SB_VERT
, SB_LINEUP
), /* VK_UP */
4877 SCROLL_ENTRY(SB_HORZ
, SB_LINEDOWN
), /* VK_RIGHT */
4878 SCROLL_ENTRY(SB_VERT
, SB_LINEDOWN
) /* VK_DOWN */
4882 if (key
>= VK_PRIOR
&& key
<= VK_DOWN
)
4884 unsigned char code
= scroll
[key
- VK_PRIOR
].code
;
4886 (((code
& (1 << 7)) == (SB_HORZ
<< 7))
4888 : TREEVIEW_VScroll
)(infoPtr
, code
& 0x7F);
4894 /************************************************************************
4897 * VK_UP Move selection to the previous non-hidden item.
4898 * VK_DOWN Move selection to the next non-hidden item.
4899 * VK_HOME Move selection to the first item.
4900 * VK_END Move selection to the last item.
4901 * VK_LEFT If expanded then collapse, otherwise move to parent.
4902 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4904 * VK_SUBTRACT Collapse.
4905 * VK_MULTIPLY Expand all.
4906 * VK_PRIOR Move up GetVisibleCount items.
4907 * VK_NEXT Move down GetVisibleCount items.
4908 * VK_BACK Move to parent.
4909 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
4912 TREEVIEW_KeyDown(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
)
4914 /* If it is non-NULL and different, it will be selected and visible. */
4915 TREEVIEW_ITEM
*newSelection
= NULL
;
4917 TREEVIEW_ITEM
*prevItem
= infoPtr
->selectedItem
;
4919 TRACE("%x\n", wParam
);
4921 if (prevItem
== NULL
)
4924 if (GetAsyncKeyState(VK_CONTROL
) & 0x8000)
4925 return TREEVIEW_ScrollKeyDown(infoPtr
, wParam
);
4930 newSelection
= TREEVIEW_GetPrevListItem(infoPtr
, prevItem
);
4932 newSelection
= infoPtr
->root
->firstChild
;
4936 newSelection
= TREEVIEW_GetNextListItem(infoPtr
, prevItem
);
4940 newSelection
= infoPtr
->root
->firstChild
;
4944 newSelection
= TREEVIEW_GetLastListItem(infoPtr
, infoPtr
->root
);
4948 if (prevItem
->state
& TVIS_EXPANDED
)
4950 TREEVIEW_Collapse(infoPtr
, prevItem
, FALSE
, TRUE
);
4952 else if (prevItem
->parent
!= infoPtr
->root
)
4954 newSelection
= prevItem
->parent
;
4959 if (TREEVIEW_HasChildren(infoPtr
, prevItem
))
4961 if (!(prevItem
->state
& TVIS_EXPANDED
))
4962 TREEVIEW_Expand(infoPtr
, prevItem
, FALSE
, TRUE
);
4965 newSelection
= prevItem
->firstChild
;
4972 TREEVIEW_ExpandAll(infoPtr
, prevItem
);
4976 if (!(prevItem
->state
& TVIS_EXPANDED
))
4977 TREEVIEW_Expand(infoPtr
, prevItem
, FALSE
, TRUE
);
4981 if (prevItem
->state
& TVIS_EXPANDED
)
4982 TREEVIEW_Collapse(infoPtr
, prevItem
, FALSE
, TRUE
);
4987 = TREEVIEW_GetListItem(infoPtr
, prevItem
,
4988 -TREEVIEW_GetVisibleCount(infoPtr
));
4993 = TREEVIEW_GetListItem(infoPtr
, prevItem
,
4994 TREEVIEW_GetVisibleCount(infoPtr
));
4998 newSelection
= prevItem
->parent
;
4999 if (newSelection
== infoPtr
->root
)
5000 newSelection
= NULL
;
5004 if (infoPtr
->dwStyle
& TVS_CHECKBOXES
)
5005 TREEVIEW_ToggleItemState(infoPtr
, prevItem
);
5009 if (newSelection
&& newSelection
!= prevItem
)
5011 if (TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, newSelection
,
5014 TREEVIEW_EnsureVisible(infoPtr
, newSelection
, FALSE
);
5022 TREEVIEW_Notify(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5024 LPNMHDR lpnmh
= (LPNMHDR
)lParam
;
5026 if (lpnmh
->code
== PGN_CALCSIZE
) {
5027 LPNMPGCALCSIZE lppgc
= (LPNMPGCALCSIZE
)lParam
;
5029 if (lppgc
->dwFlag
== PGF_CALCWIDTH
) {
5030 lppgc
->iWidth
= infoPtr
->treeWidth
;
5031 TRACE("got PGN_CALCSIZE, returning horz size = %ld, client=%ld\n",
5032 infoPtr
->treeWidth
, infoPtr
->clientWidth
);
5035 lppgc
->iHeight
= infoPtr
->treeHeight
;
5036 TRACE("got PGN_CALCSIZE, returning vert size = %ld, client=%ld\n",
5037 infoPtr
->treeHeight
, infoPtr
->clientHeight
);
5041 return DefWindowProcA(infoPtr
->hwnd
, WM_NOTIFY
, wParam
, lParam
);
5044 static INT
TREEVIEW_NotifyFormat (TREEVIEW_INFO
*infoPtr
, HWND hwndFrom
, UINT nCommand
)
5048 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom
, nCommand
);
5050 if (nCommand
!= NF_REQUERY
) return 0;
5052 format
= SendMessageW(hwndFrom
, WM_NOTIFYFORMAT
, (WPARAM
)infoPtr
->hwnd
, NF_QUERY
);
5053 TRACE("format=%d\n", format
);
5055 if (format
!= NFR_ANSI
&& format
!= NFR_UNICODE
) return 0;
5057 infoPtr
->bNtfUnicode
= (format
== NFR_UNICODE
);
5063 TREEVIEW_Size(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5065 if (wParam
== SIZE_RESTORED
)
5067 infoPtr
->clientWidth
= SLOWORD(lParam
);
5068 infoPtr
->clientHeight
= SHIWORD(lParam
);
5070 TREEVIEW_RecalculateVisibleOrder(infoPtr
, NULL
);
5071 TREEVIEW_SetFirstVisible(infoPtr
, infoPtr
->firstVisible
, TRUE
);
5072 TREEVIEW_UpdateScrollBars(infoPtr
);
5076 FIXME("WM_SIZE flag %x %lx not handled\n", wParam
, lParam
);
5079 TREEVIEW_Invalidate(infoPtr
, NULL
);
5084 TREEVIEW_StyleChanged(TREEVIEW_INFO
*infoPtr
, WPARAM wParam
, LPARAM lParam
)
5086 TRACE("(%x %lx)\n", wParam
, lParam
);
5088 if (wParam
== GWL_STYLE
)
5090 DWORD dwNewStyle
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
5092 /* we have to take special care about tooltips */
5093 if ((infoPtr
->dwStyle
^ dwNewStyle
) & TVS_NOTOOLTIPS
)
5095 if (infoPtr
->dwStyle
& TVS_NOTOOLTIPS
)
5097 infoPtr
->hwndToolTip
= COMCTL32_CreateToolTip(infoPtr
->hwnd
);
5102 DestroyWindow(infoPtr
->hwndToolTip
);
5103 infoPtr
->hwndToolTip
= 0;
5107 infoPtr
->dwStyle
= dwNewStyle
;
5110 TREEVIEW_UpdateSubTree(infoPtr
, infoPtr
->root
);
5111 TREEVIEW_UpdateScrollBars(infoPtr
);
5112 TREEVIEW_Invalidate(infoPtr
, NULL
);
5118 TREEVIEW_SetFocus(TREEVIEW_INFO
*infoPtr
)
5122 if (!infoPtr
->selectedItem
)
5124 TREEVIEW_DoSelectItem(infoPtr
, TVGN_CARET
, infoPtr
->firstVisible
,
5128 TREEVIEW_SendSimpleNotify(infoPtr
, NM_SETFOCUS
);
5129 TREEVIEW_Invalidate(infoPtr
, infoPtr
->selectedItem
);
5134 TREEVIEW_KillFocus(TREEVIEW_INFO
*infoPtr
)
5138 TREEVIEW_SendSimpleNotify(infoPtr
, NM_KILLFOCUS
);
5139 TREEVIEW_Invalidate(infoPtr
, infoPtr
->selectedItem
);
5144 static LRESULT WINAPI
5145 TREEVIEW_WindowProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
5147 TREEVIEW_INFO
*infoPtr
= TREEVIEW_GetInfoPtr(hwnd
);
5148 if (infoPtr
) TREEVIEW_VerifyTree(infoPtr
);
5151 if (uMsg
== WM_CREATE
)
5152 TREEVIEW_Create(hwnd
);
5159 case TVM_CREATEDRAGIMAGE
:
5160 return TREEVIEW_CreateDragImage(infoPtr
, wParam
, lParam
);
5162 case TVM_DELETEITEM
:
5163 return TREEVIEW_DeleteItem(infoPtr
, (HTREEITEM
)lParam
);
5165 case TVM_EDITLABELA
:
5166 return (LRESULT
)TREEVIEW_EditLabelA(infoPtr
, (HTREEITEM
)lParam
);
5168 case TVM_EDITLABELW
:
5169 FIXME("Unimplemented msg TVM_EDITLABELW\n");
5172 case TVM_ENDEDITLABELNOW
:
5173 return TREEVIEW_EndEditLabelNow(infoPtr
, (BOOL
)wParam
);
5175 case TVM_ENSUREVISIBLE
:
5176 return TREEVIEW_EnsureVisible(infoPtr
, (HTREEITEM
)lParam
, TRUE
);
5179 return TREEVIEW_ExpandMsg(infoPtr
, (UINT
)wParam
, (HTREEITEM
)lParam
);
5181 case TVM_GETBKCOLOR
:
5182 return TREEVIEW_GetBkColor(infoPtr
);
5185 return TREEVIEW_GetCount(infoPtr
);
5187 case TVM_GETEDITCONTROL
:
5188 return TREEVIEW_GetEditControl(infoPtr
);
5190 case TVM_GETIMAGELIST
:
5191 return TREEVIEW_GetImageList(infoPtr
, wParam
);
5194 return TREEVIEW_GetIndent(infoPtr
);
5196 case TVM_GETINSERTMARKCOLOR
:
5197 return TREEVIEW_GetInsertMarkColor(infoPtr
);
5199 case TVM_GETISEARCHSTRINGA
:
5200 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5203 case TVM_GETISEARCHSTRINGW
:
5204 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5208 return TREEVIEW_GetItemA(infoPtr
, (LPTVITEMEXA
)lParam
);
5211 return TREEVIEW_GetItemW(infoPtr
, (LPTVITEMEXW
)lParam
);
5213 case TVM_GETITEMHEIGHT
:
5214 return TREEVIEW_GetItemHeight(infoPtr
);
5216 case TVM_GETITEMRECT
:
5217 return TREEVIEW_GetItemRect(infoPtr
, (BOOL
)wParam
, (LPRECT
)lParam
);
5219 case TVM_GETITEMSTATE
:
5220 return TREEVIEW_GetItemState(infoPtr
, (HTREEITEM
)wParam
, (UINT
)lParam
);
5222 case TVM_GETLINECOLOR
:
5223 return TREEVIEW_GetLineColor(infoPtr
);
5225 case TVM_GETNEXTITEM
:
5226 return TREEVIEW_GetNextItem(infoPtr
, (UINT
)wParam
, (HTREEITEM
)lParam
);
5228 case TVM_GETSCROLLTIME
:
5229 return TREEVIEW_GetScrollTime(infoPtr
);
5231 case TVM_GETTEXTCOLOR
:
5232 return TREEVIEW_GetTextColor(infoPtr
);
5234 case TVM_GETTOOLTIPS
:
5235 return TREEVIEW_GetToolTips(infoPtr
);
5237 case TVM_GETUNICODEFORMAT
:
5238 FIXME("Unimplemented msg TVM_GETUNICODEFORMAT\n");
5241 case TVM_GETVISIBLECOUNT
:
5242 return TREEVIEW_GetVisibleCount(infoPtr
);
5245 return TREEVIEW_HitTest(infoPtr
, (LPTVHITTESTINFO
)lParam
);
5247 case TVM_INSERTITEMA
:
5248 return TREEVIEW_InsertItemA(infoPtr
, lParam
);
5250 case TVM_INSERTITEMW
:
5251 return TREEVIEW_InsertItemW(infoPtr
, lParam
);
5253 case TVM_SELECTITEM
:
5254 return TREEVIEW_SelectItem(infoPtr
, (INT
)wParam
, (HTREEITEM
)lParam
);
5256 case TVM_SETBKCOLOR
:
5257 return TREEVIEW_SetBkColor(infoPtr
, (COLORREF
)lParam
);
5259 case TVM_SETIMAGELIST
:
5260 return TREEVIEW_SetImageList(infoPtr
, wParam
, (HIMAGELIST
)lParam
);
5263 return TREEVIEW_SetIndent(infoPtr
, (UINT
)wParam
);
5265 case TVM_SETINSERTMARK
:
5266 return TREEVIEW_SetInsertMark(infoPtr
, (BOOL
)wParam
, (HTREEITEM
)lParam
);
5268 case TVM_SETINSERTMARKCOLOR
:
5269 return TREEVIEW_SetInsertMarkColor(infoPtr
, (COLORREF
)lParam
);
5272 return TREEVIEW_SetItemA(infoPtr
, (LPTVITEMEXA
)lParam
);
5275 return TREEVIEW_SetItemW(infoPtr
, (LPTVITEMEXW
)lParam
);
5278 case TVM_SETLINECOLOR
:
5279 return TREEVIEW_SetLineColor(infoPtr
, (COLORREF
)lParam
);
5281 case TVM_SETITEMHEIGHT
:
5282 return TREEVIEW_SetItemHeight(infoPtr
, (INT
)(SHORT
)wParam
);
5284 case TVM_SETSCROLLTIME
:
5285 return TREEVIEW_SetScrollTime(infoPtr
, (UINT
)wParam
);
5287 case TVM_SETTEXTCOLOR
:
5288 return TREEVIEW_SetTextColor(infoPtr
, (COLORREF
)lParam
);
5290 case TVM_SETTOOLTIPS
:
5291 return TREEVIEW_SetToolTips(infoPtr
, (HWND
)wParam
);
5293 case TVM_SETUNICODEFORMAT
:
5294 FIXME("Unimplemented msg TVM_SETUNICODEFORMAT\n");
5297 case TVM_SORTCHILDREN
:
5298 return TREEVIEW_SortChildren(infoPtr
, wParam
, lParam
);
5300 case TVM_SORTCHILDRENCB
:
5301 return TREEVIEW_SortChildrenCB(infoPtr
, wParam
, (LPTVSORTCB
)lParam
);
5304 return TREEVIEW_ProcessLetterKeys( hwnd
, wParam
, lParam
);
5307 return TREEVIEW_Command(infoPtr
, wParam
, lParam
);
5310 return TREEVIEW_Destroy(infoPtr
);
5315 return TREEVIEW_EraseBackground(infoPtr
, (HDC
)wParam
);
5318 return DLGC_WANTARROWS
| DLGC_WANTCHARS
;
5321 return TREEVIEW_GetFont(infoPtr
);
5324 return TREEVIEW_HScroll(infoPtr
, wParam
);
5327 return TREEVIEW_KeyDown(infoPtr
, wParam
);
5330 return TREEVIEW_KillFocus(infoPtr
);
5332 case WM_LBUTTONDBLCLK
:
5333 return TREEVIEW_LButtonDoubleClick(infoPtr
, lParam
);
5335 case WM_LBUTTONDOWN
:
5336 return TREEVIEW_LButtonDown(infoPtr
, lParam
);
5338 /* WM_MBUTTONDOWN */
5343 return TREEVIEW_Notify(infoPtr
, wParam
, lParam
);
5345 case WM_NOTIFYFORMAT
:
5346 return TREEVIEW_NotifyFormat(infoPtr
, (HWND
)wParam
, (UINT
)lParam
);
5349 return TREEVIEW_Paint(infoPtr
, wParam
);
5351 /* WM_PRINTCLIENT */
5353 case WM_RBUTTONDOWN
:
5354 return TREEVIEW_RButtonDown(infoPtr
, lParam
);
5357 return TREEVIEW_SetFocus(infoPtr
);
5360 return TREEVIEW_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
5363 return TREEVIEW_SetRedraw(infoPtr
, wParam
, lParam
);
5366 return TREEVIEW_Size(infoPtr
, wParam
, lParam
);
5368 case WM_STYLECHANGED
:
5369 return TREEVIEW_StyleChanged(infoPtr
, wParam
, lParam
);
5371 /* WM_SYSCOLORCHANGE */
5376 return TREEVIEW_HandleTimer(infoPtr
, wParam
);
5379 return TREEVIEW_VScroll(infoPtr
, wParam
);
5381 /* WM_WININICHANGE */
5384 if (wParam
& (MK_SHIFT
| MK_CONTROL
))
5386 return TREEVIEW_MouseWheel(infoPtr
, wParam
);
5389 TRACE("drawItem\n");
5393 /* This mostly catches MFC and Delphi messages. :( */
5394 if ((uMsg
>= WM_USER
) && (uMsg
< WM_APP
))
5395 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg
, wParam
, lParam
);
5397 return DefWindowProcA(hwnd
, uMsg
, wParam
, lParam
);
5403 /* Class Registration ***************************************************/
5406 TREEVIEW_Register(void)
5412 ZeroMemory(&wndClass
, sizeof(WNDCLASSA
));
5413 wndClass
.style
= CS_GLOBALCLASS
| CS_DBLCLKS
;
5414 wndClass
.lpfnWndProc
= (WNDPROC
)TREEVIEW_WindowProc
;
5415 wndClass
.cbClsExtra
= 0;
5416 wndClass
.cbWndExtra
= sizeof(TREEVIEW_INFO
*);
5418 wndClass
.hCursor
= LoadCursorA(0, IDC_ARROWA
);
5419 wndClass
.hbrBackground
= 0;
5420 wndClass
.lpszClassName
= WC_TREEVIEWA
;
5422 RegisterClassA(&wndClass
);
5427 TREEVIEW_Unregister(void)
5429 UnregisterClassA(WC_TREEVIEWA
, NULL
);
5433 /* Tree Verification ****************************************************/
5437 TREEVIEW_VerifyChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
);
5439 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO
*infoPtr
,
5440 TREEVIEW_ITEM
*item
)
5442 assert(infoPtr
!= NULL
);
5443 assert(item
!= NULL
);
5445 /* both NULL, or both non-null */
5446 assert((item
->firstChild
== NULL
) == (item
->lastChild
== NULL
));
5448 assert(item
->firstChild
!= item
);
5449 assert(item
->lastChild
!= item
);
5451 if (item
->firstChild
)
5453 assert(item
->firstChild
->parent
== item
);
5454 assert(item
->firstChild
->prevSibling
== NULL
);
5457 if (item
->lastChild
)
5459 assert(item
->lastChild
->parent
== item
);
5460 assert(item
->lastChild
->nextSibling
== NULL
);
5463 assert(item
->nextSibling
!= item
);
5464 if (item
->nextSibling
)
5466 assert(item
->nextSibling
->parent
== item
->parent
);
5467 assert(item
->nextSibling
->prevSibling
== item
);
5470 assert(item
->prevSibling
!= item
);
5471 if (item
->prevSibling
)
5473 assert(item
->prevSibling
->parent
== item
->parent
);
5474 assert(item
->prevSibling
->nextSibling
== item
);
5479 TREEVIEW_VerifyItem(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
5481 assert(item
!= NULL
);
5483 assert(item
->parent
!= NULL
);
5484 assert(item
->parent
!= item
);
5485 assert(item
->iLevel
== item
->parent
->iLevel
+ 1);
5487 assert(DPA_GetPtrIndex(infoPtr
->items
, item
) != -1);
5489 TREEVIEW_VerifyItemCommon(infoPtr
, item
);
5491 TREEVIEW_VerifyChildren(infoPtr
, item
);
5495 TREEVIEW_VerifyChildren(TREEVIEW_INFO
*infoPtr
, TREEVIEW_ITEM
*item
)
5497 TREEVIEW_ITEM
*child
;
5498 assert(item
!= NULL
);
5500 for (child
= item
->firstChild
; child
!= NULL
; child
= child
->nextSibling
)
5501 TREEVIEW_VerifyItem(infoPtr
, child
);
5505 TREEVIEW_VerifyRoot(TREEVIEW_INFO
*infoPtr
)
5507 TREEVIEW_ITEM
*root
= infoPtr
->root
;
5509 assert(root
!= NULL
);
5510 assert(root
->iLevel
== -1);
5511 assert(root
->parent
== NULL
);
5512 assert(root
->prevSibling
== NULL
);
5514 TREEVIEW_VerifyItemCommon(infoPtr
, root
);
5516 TREEVIEW_VerifyChildren(infoPtr
, root
);
5520 TREEVIEW_VerifyTree(TREEVIEW_INFO
*infoPtr
)
5522 assert(infoPtr
!= NULL
);
5524 TREEVIEW_VerifyRoot(infoPtr
);