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.
51 #include "wine/debug.h"
53 /* internal structures */
55 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
66 int iIntegral; /* item height multiplier (1 is normal) */
67 int iLevel; /* indentation level:0=root level */
68 HTREEITEM parent; /* handle to parent or 0 if at root */
69 HTREEITEM firstChild; /* handle to first child or 0 if no child */
71 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
72 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
78 LONG textWidth; /* horizontal text extent for pszText */
79 LONG visibleOrder; /* visible ordering, 0 is first visible item */
83 typedef struct tagTREEVIEW_INFO
86 HWND hwndNotify; /* Owner window to send notifications to */
91 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
92 INT cdmode; /* last custom draw setting */
93 UINT uScrollTime; /* max. time for scrolling in milliseconds */
94 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
96 UINT uItemHeight; /* item height */
99 LONG clientWidth; /* width of control window */
100 LONG clientHeight; /* height of control window */
102 LONG treeWidth; /* width of visible tree items */
103 LONG treeHeight; /* height of visible tree items */
105 UINT uIndent; /* indentation in pixels */
106 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
107 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
108 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
110 HTREEITEM firstVisible; /* handle to first visible item */
111 LONG maxVisibleOrder;
112 HTREEITEM dropItem; /* handle to item selected by drag cursor */
113 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
114 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
115 HIMAGELIST dragList; /* Bitmap of dragged item */
120 COLORREF clrInsertMark;
126 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
127 BOOL bIgnoreEditKillFocus;
130 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
131 BOOL bUnicode; /* set by CCM_SETUNICODEFORMAT */
132 HIMAGELIST himlNormal;
133 int normalImageHeight;
134 int normalImageWidth;
135 HIMAGELIST himlState;
136 int stateImageHeight;
140 DWORD lastKeyPressTimestamp; /* Added */
141 WPARAM charCode; /* Added */
142 INT nSearchParamLength; /* Added */
143 CHAR szSearchParam[ MAX_PATH ]; /* Added */
147 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
148 #define KEY_DELAY 450
150 /* bitflags for infoPtr->uInternalStatus */
152 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
153 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
154 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
155 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
156 #define TV_RDRAG 0x10 /* dito Rbutton */
157 #define TV_RDRAGGING 0x20
159 /* bitflags for infoPtr->timer */
161 #define TV_EDIT_TIMER 2
162 #define TV_EDIT_TIMER_SET 2
165 VOID TREEVIEW_Register (VOID);
166 VOID TREEVIEW_Unregister (VOID);
169 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
172 #define TEXT_CALLBACK_SIZE 260
174 #define TREEVIEW_LEFT_MARGIN 8
176 #define MINIMUM_INDENT 19
178 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
180 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
181 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
182 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
185 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
188 static VOID TREEVIEW_Invalidate(TREEVIEW_INFO *, TREEVIEW_ITEM *);
190 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
191 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
192 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
193 static LRESULT TREEVIEW_RButtonUp(TREEVIEW_INFO *, LPPOINT);
194 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
195 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
196 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
197 static LRESULT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
200 /* Random Utilities *****************************************************/
204 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
209 /* The definition is at the end of the file. */
210 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
213 /* Returns the treeview private data if hwnd is a treeview.
214 * Otherwise returns an undefined value. */
215 static TREEVIEW_INFO *
216 TREEVIEW_GetInfoPtr(HWND hwnd)
218 return (TREEVIEW_INFO *)GetWindowLongA(hwnd, 0);
221 /* Don't call this. Nothing wants an item index. */
223 TREEVIEW_GetItemIndex(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
225 assert(infoPtr != NULL);
227 return DPA_GetPtrIndex(infoPtr->items, handle);
230 /***************************************************************************
231 * This method checks that handle is an item for this tree.
234 TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
236 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
238 TRACE("invalid item %p\n", handle);
246 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
250 GetObjectA(hOrigFont, sizeof(font), &font);
251 font.lfWeight = FW_BOLD;
252 return CreateFontIndirectA(&font);
256 TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
258 return (item->state & TVIS_BOLD) ? infoPtr->hBoldFont : infoPtr->hFont;
261 /* for trace/debugging purposes only */
263 TREEVIEW_ItemName(TREEVIEW_ITEM *item)
265 if (item == NULL) return "<null item>";
266 if (item->pszText == LPSTR_TEXTCALLBACKA) return "<callback>";
267 if (item->pszText == NULL) return "<null>";
268 return item->pszText;
271 /* An item is not a child of itself. */
273 TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
277 child = child->parent;
278 if (child == parent) return TRUE;
279 } while (child != NULL);
285 /* Tree Traversal *******************************************************/
287 /***************************************************************************
288 * This method returns the last expanded sibling or child child item
291 static TREEVIEW_ITEM *
292 TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
297 while (wineItem->lastChild)
299 if (wineItem->state & TVIS_EXPANDED)
300 wineItem = wineItem->lastChild;
305 if (wineItem == infoPtr->root)
311 /***************************************************************************
312 * This method returns the previous non-hidden item in the list not
313 * considering the tree hierarchy.
315 static TREEVIEW_ITEM *
316 TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
318 if (tvItem->prevSibling)
320 /* This item has a prevSibling, get the last item in the sibling's tree. */
321 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
323 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
324 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
330 /* this item does not have a prevSibling, get the parent */
331 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
336 /***************************************************************************
337 * This method returns the next physical item in the treeview not
338 * considering the tree hierarchy.
340 static TREEVIEW_ITEM *
341 TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
343 assert(tvItem != NULL);
346 * If this item has children and is expanded, return the first child
348 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
350 return tvItem->firstChild;
355 * try to get the sibling
357 if (tvItem->nextSibling)
358 return tvItem->nextSibling;
361 * Otherwise, get the parent's sibling.
363 while (tvItem->parent)
365 tvItem = tvItem->parent;
367 if (tvItem->nextSibling)
368 return tvItem->nextSibling;
374 /***************************************************************************
375 * This method returns the nth item starting at the given item. It returns
376 * the last item (or first) we we run out of items.
378 * Will scroll backward if count is <0.
379 * forward if count is >0.
381 static TREEVIEW_ITEM *
382 TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
385 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
386 TREEVIEW_ITEM *previousItem;
388 assert(wineItem != NULL);
392 next_item = TREEVIEW_GetNextListItem;
397 next_item = TREEVIEW_GetPrevListItem;
404 previousItem = wineItem;
405 wineItem = next_item(infoPtr, wineItem);
407 } while (--count && wineItem != NULL);
410 return wineItem ? wineItem : previousItem;
413 /* Notifications ************************************************************/
416 TREEVIEW_SendRealNotify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
418 if (infoPtr->bNtfUnicode)
419 return (BOOL)SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
422 return (BOOL)SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
427 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
430 HWND hwnd = infoPtr->hwnd;
433 nmhdr.hwndFrom = hwnd;
434 nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
437 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
438 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
442 TREEVIEW_TVItemFromItem(TREEVIEW_INFO *infoPtr, UINT mask, TVITEMA *tvItem, TREEVIEW_ITEM *item)
445 tvItem->hItem = item;
446 tvItem->state = item->state;
447 tvItem->stateMask = 0;
448 tvItem->iImage = item->iImage;
449 tvItem->cchTextMax = item->cchTextMax;
450 tvItem->iImage = item->iImage;
451 tvItem->iSelectedImage = item->iSelectedImage;
452 tvItem->cChildren = item->cChildren;
453 tvItem->lParam = item->lParam;
455 /* **** **** **** **** WARNING **** **** **** **** */
456 /* This control stores all the data in A format */
457 /* we will convert it to W if the notify format */
459 /* **** **** **** **** WARNING **** **** **** **** */
460 if (infoPtr->bNtfUnicode) {
461 INT len = MultiByteToWideChar( CP_ACP, 0, item->pszText, -1, NULL, 0 );
463 tvItem->pszText = (LPSTR)COMCTL32_Alloc (len*sizeof(WCHAR));
464 MultiByteToWideChar( CP_ACP, 0, item->pszText, -1, (LPWSTR)tvItem->pszText, len*sizeof(WCHAR) );
468 tvItem->pszText = item->pszText;
472 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
473 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
475 HWND hwnd = infoPtr->hwnd;
479 TRACE("code:%x action:%x olditem:%p newitem:%p\n",
480 code, action, oldItem, newItem);
482 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWA));
484 nmhdr.hdr.hwndFrom = hwnd;
485 nmhdr.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
486 nmhdr.hdr.code = code;
487 nmhdr.action = action;
490 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
493 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
498 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
499 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
501 if (infoPtr->bNtfUnicode) {
502 COMCTL32_Free(nmhdr.itemOld.pszText);
503 COMCTL32_Free(nmhdr.itemNew.pszText);
509 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
510 HTREEITEM dragItem, POINT pt)
512 HWND hwnd = infoPtr->hwnd;
515 TRACE("code:%x dragitem:%p\n", code, dragItem);
517 nmhdr.hdr.hwndFrom = hwnd;
518 nmhdr.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
519 nmhdr.hdr.code = code;
521 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
522 nmhdr.itemNew.hItem = dragItem;
523 nmhdr.itemNew.state = dragItem->state;
524 nmhdr.itemNew.lParam = dragItem->lParam;
526 nmhdr.ptDrag.x = pt.x;
527 nmhdr.ptDrag.y = pt.y;
529 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
530 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
536 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
539 HWND hwnd = infoPtr->hwnd;
540 NMTVCUSTOMDRAW nmcdhdr;
543 TRACE("drawstage:%lx hdc:%x\n", dwDrawStage, hdc);
545 nmcd = &nmcdhdr.nmcd;
546 nmcd->hdr.hwndFrom = hwnd;
547 nmcd->hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
548 nmcd->hdr.code = NM_CUSTOMDRAW;
549 nmcd->dwDrawStage = dwDrawStage;
552 nmcd->dwItemSpec = 0;
553 nmcd->uItemState = 0;
554 nmcd->lItemlParam = 0;
555 nmcdhdr.clrText = infoPtr->clrText;
556 nmcdhdr.clrTextBk = infoPtr->clrBk;
559 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
560 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
566 /* FIXME: need to find out when the flags in uItemState need to be set */
569 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
570 TREEVIEW_ITEM *wineItem, UINT uItemDrawState)
572 HWND hwnd = infoPtr->hwnd;
573 NMTVCUSTOMDRAW nmcdhdr;
575 DWORD dwDrawStage, dwItemSpec;
579 dwDrawStage = CDDS_ITEM | uItemDrawState;
580 dwItemSpec = (DWORD)wineItem;
582 if (wineItem->state & TVIS_SELECTED)
583 uItemState |= CDIS_SELECTED;
584 if (wineItem == infoPtr->selectedItem)
585 uItemState |= CDIS_FOCUS;
586 if (wineItem == infoPtr->hotItem)
587 uItemState |= CDIS_HOT;
589 nmcd = &nmcdhdr.nmcd;
590 nmcd->hdr.hwndFrom = hwnd;
591 nmcd->hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
592 nmcd->hdr.code = NM_CUSTOMDRAW;
593 nmcd->dwDrawStage = dwDrawStage;
595 nmcd->rc = wineItem->rect;
596 nmcd->dwItemSpec = dwItemSpec;
597 nmcd->uItemState = uItemState;
598 nmcd->lItemlParam = wineItem->lParam;
599 nmcdhdr.clrText = infoPtr->clrText;
600 nmcdhdr.clrTextBk = infoPtr->clrBk;
601 nmcdhdr.iLevel = wineItem->iLevel;
603 TRACE("drawstage:%lx hdc:%x item:%lx, itemstate:%x, lItemlParam:%lx\n",
604 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
605 nmcd->uItemState, nmcd->lItemlParam);
607 retval = TREEVIEW_SendRealNotify(infoPtr,
608 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
611 infoPtr->clrText = nmcdhdr.clrText;
612 infoPtr->clrBk = nmcdhdr.clrTextBk;
617 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
619 HWND hwnd = infoPtr->hwnd;
620 LPSTR allocated = NULL;
624 tvdi.hdr.hwndFrom = hwnd;
625 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
626 tvdi.hdr.code = TVN_BEGINLABELEDITA;
628 tvdi.item.mask = TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT;
629 tvdi.item.hItem = editItem;
630 tvdi.item.state = editItem->state;
631 tvdi.item.lParam = editItem->lParam;
632 if (infoPtr->bNtfUnicode) {
633 INT len = MultiByteToWideChar( CP_ACP, 0, editItem->pszText, -1, NULL, 0 );
635 tvdi.item.pszText = allocated = (LPSTR)COMCTL32_Alloc (len*sizeof(WCHAR));
636 MultiByteToWideChar( CP_ACP, 0, editItem->pszText, -1, (LPWSTR)tvdi.item.pszText, len*sizeof(WCHAR) );
637 tvdi.item.cchTextMax = len*sizeof(WCHAR);
640 tvdi.item.pszText = editItem->pszText; /* ??? */
641 tvdi.item.cchTextMax = editItem->cchTextMax; /* ??? */
645 tvdi.item.pszText = editItem->pszText;
646 tvdi.item.cchTextMax = editItem->cchTextMax;
649 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
653 COMCTL32_Free(allocated);
658 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
661 NMTVDISPINFOA callback;
662 HWND hwnd = infoPtr->hwnd;
664 mask &= wineItem->callbackMask;
666 if (mask == 0) return;
668 callback.hdr.hwndFrom = hwnd;
669 callback.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
670 callback.hdr.code = (infoPtr->bNtfUnicode) ? TVN_GETDISPINFOW :
673 /* 'state' always contains valid value, as well as 'lParam'.
674 * All other parameters are uninitialized.
676 callback.item.pszText = wineItem->pszText;
677 callback.item.cchTextMax = wineItem->cchTextMax;
678 callback.item.mask = mask;
679 callback.item.hItem = wineItem;
680 callback.item.state = wineItem->state;
681 callback.item.lParam = wineItem->lParam;
683 /* If text is changed we need to recalculate textWidth */
684 if (mask & TVIF_TEXT)
685 wineItem->textWidth = 0;
687 TREEVIEW_SendRealNotify(infoPtr,
688 (WPARAM)callback.hdr.idFrom, (LPARAM)&callback);
690 /* It may have changed due to a call to SetItem. */
691 mask &= wineItem->callbackMask;
693 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
695 /* Instead of copying text into our buffer user specified its own */
696 if (infoPtr->bNtfUnicode) {
699 int len = WideCharToMultiByte( CP_ACP, 0,
700 (LPWSTR)callback.item.pszText, -1,
701 NULL, 0, NULL, NULL );
702 buflen = max((len+1)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
703 newText = (LPWSTR)COMCTL32_ReAlloc(wineItem->pszText, buflen);
705 TRACE("returned wstr %s, len=%d, buflen=%d\n",
706 debugstr_w((LPWSTR)callback.item.pszText), len, buflen);
710 wineItem->pszText = (LPSTR)newText;
711 WideCharToMultiByte( CP_ACP, 0,
712 (LPWSTR)callback.item.pszText, -1,
713 wineItem->pszText, buflen,
715 wineItem->cchTextMax = buflen;
717 /* If ReAlloc fails we have nothing to do, but keep original text */
720 int len = max(lstrlenA(callback.item.pszText) + 1,
722 LPSTR newText = COMCTL32_ReAlloc(wineItem->pszText, len);
724 TRACE("returned str %s, len=%d\n",
725 debugstr_a(callback.item.pszText), len);
729 wineItem->pszText = newText;
730 strcpy(wineItem->pszText, callback.item.pszText);
731 wineItem->cchTextMax = len;
733 /* If ReAlloc fails we have nothing to do, but keep original text */
736 else if (mask & TVIF_TEXT) {
737 /* User put text into our buffer, that is ok unless W string */
738 if (infoPtr->bNtfUnicode) {
740 LPSTR oldText = NULL;
742 int len = WideCharToMultiByte( CP_ACP, 0,
743 (LPWSTR)callback.item.pszText, -1,
744 NULL, 0, NULL, NULL );
745 buflen = max((len+1)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
746 newText = (LPWSTR)COMCTL32_Alloc(buflen);
748 TRACE("same buffer wstr %s, len=%d, buflen=%d\n",
749 debugstr_w((LPWSTR)callback.item.pszText), len, buflen);
753 oldText = wineItem->pszText;
754 wineItem->pszText = (LPSTR)newText;
755 WideCharToMultiByte( CP_ACP, 0,
756 (LPWSTR)callback.item.pszText, -1,
757 wineItem->pszText, buflen, NULL, NULL );
758 wineItem->cchTextMax = buflen;
760 COMCTL32_Free(oldText);
765 if (mask & TVIF_IMAGE)
766 wineItem->iImage = callback.item.iImage;
768 if (mask & TVIF_SELECTEDIMAGE)
769 wineItem->iSelectedImage = callback.item.iSelectedImage;
771 if (mask & TVIF_CHILDREN)
772 wineItem->cChildren = callback.item.cChildren;
774 /* These members are now permanently set. */
775 if (callback.item.mask & TVIF_DI_SETITEM)
776 wineItem->callbackMask &= ~callback.item.mask;
779 /***************************************************************************
780 * This function uses cChildren field to decide whether the item has
782 * Note: if this returns TRUE, the child items may not actually exist,
783 * they could be virtual.
785 * Just use wineItem->firstChild to check for physical children.
788 TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
790 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
792 return wineItem->cChildren > 0;
796 /* Item Position ********************************************************/
798 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
800 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
803 /* Same effect, different optimisation. */
805 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
806 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
808 BOOL lar = ((infoPtr->dwStyle
809 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
813 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
815 item->stateOffset = item->linesOffset + infoPtr->uIndent;
816 item->imageOffset = item->stateOffset
817 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
818 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
822 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
828 /* DRAW's OM docker creates items like this */
829 if (item->pszText == NULL)
835 if (item->textWidth != 0 && !(item->callbackMask & TVIF_TEXT))
844 hdc = GetDC(infoPtr->hwnd);
845 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
848 GetTextExtentPoint32A(hdc, item->pszText, strlen(item->pszText), &sz);
849 item->textWidth = sz.cx;
853 SelectObject(hdc, hOldFont);
859 TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
861 item->rect.top = infoPtr->uItemHeight *
862 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
864 item->rect.bottom = item->rect.top
865 + infoPtr->uItemHeight * item->iIntegral - 1;
868 item->rect.right = infoPtr->clientWidth;
871 /* We know that only items after start need their order updated. */
873 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
880 start = infoPtr->root->firstChild;
884 order = start->visibleOrder;
886 for (item = start; item != NULL;
887 item = TREEVIEW_GetNextListItem(infoPtr, item))
889 item->visibleOrder = order;
890 order += item->iIntegral;
893 infoPtr->maxVisibleOrder = order;
895 for (item = start; item != NULL;
896 item = TREEVIEW_GetNextListItem(infoPtr, item))
898 TREEVIEW_ComputeItemRect(infoPtr, item);
903 /* Update metrics of all items in selected subtree.
904 * root must be expanded
907 TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
909 TREEVIEW_ITEM *sibling;
913 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
916 root->state &= ~TVIS_EXPANDED;
917 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
918 root->state |= TVIS_EXPANDED;
920 hdc = GetDC(infoPtr->hwnd);
921 hOldFont = SelectObject(hdc, infoPtr->hFont);
923 for (; root != sibling;
924 root = TREEVIEW_GetNextListItem(infoPtr, root))
926 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
928 if (root->callbackMask & TVIF_TEXT)
929 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
931 if (root->textWidth == 0)
933 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
934 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
938 SelectObject(hdc, hOldFont);
939 ReleaseDC(infoPtr->hwnd, hdc);
942 /* Item Allocation **********************************************************/
944 static TREEVIEW_ITEM *
945 TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
947 TREEVIEW_ITEM *newItem = COMCTL32_Alloc(sizeof(TREEVIEW_ITEM));
952 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
954 COMCTL32_Free(newItem);
961 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
962 * free item->pszText. */
964 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
966 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
968 if (infoPtr->selectedItem == item)
969 infoPtr->selectedItem = NULL;
973 /* Item Insertion *******************************************************/
975 /***************************************************************************
976 * This method inserts newItem before sibling as a child of parent.
977 * sibling can be NULL, but only if parent has no children.
980 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
981 TREEVIEW_ITEM *parent)
983 assert(newItem != NULL);
984 assert(parent != NULL);
988 assert(sibling->parent == parent);
990 if (sibling->prevSibling != NULL)
991 sibling->prevSibling->nextSibling = newItem;
993 newItem->prevSibling = sibling->prevSibling;
994 sibling->prevSibling = newItem;
997 newItem->prevSibling = NULL;
999 newItem->nextSibling = sibling;
1001 if (parent->firstChild == sibling)
1002 parent->firstChild = newItem;
1004 if (parent->lastChild == NULL)
1005 parent->lastChild = newItem;
1008 /***************************************************************************
1009 * This method inserts newItem after sibling as a child of parent.
1010 * sibling can be NULL, but only if parent has no children.
1013 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1014 TREEVIEW_ITEM *parent)
1016 assert(newItem != NULL);
1017 assert(parent != NULL);
1019 if (sibling != NULL)
1021 assert(sibling->parent == parent);
1023 if (sibling->nextSibling != NULL)
1024 sibling->nextSibling->prevSibling = newItem;
1026 newItem->nextSibling = sibling->nextSibling;
1027 sibling->nextSibling = newItem;
1030 newItem->nextSibling = NULL;
1032 newItem->prevSibling = sibling;
1034 if (parent->lastChild == sibling)
1035 parent->lastChild = newItem;
1037 if (parent->firstChild == NULL)
1038 parent->firstChild = newItem;
1042 TREEVIEW_DoSetItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1043 const TVITEMEXA *tvItem)
1045 UINT callbackClear = 0;
1046 UINT callbackSet = 0;
1048 /* Do this first in case it fails. */
1049 if (tvItem->mask & TVIF_TEXT)
1051 wineItem->textWidth = 0; /* force width recalculation */
1052 if (tvItem->pszText != LPSTR_TEXTCALLBACKA)
1054 int len = lstrlenA(tvItem->pszText) + 1;
1055 LPSTR newText = COMCTL32_ReAlloc(wineItem->pszText, len);
1057 if (newText == NULL) return FALSE;
1059 callbackClear |= TVIF_TEXT;
1061 wineItem->pszText = newText;
1062 wineItem->cchTextMax = len;
1063 lstrcpynA(wineItem->pszText, tvItem->pszText, len);
1064 TRACE("setting text %s, item %p\n",
1065 debugstr_a(wineItem->pszText), wineItem);
1069 callbackSet |= TVIF_TEXT;
1071 wineItem->pszText = COMCTL32_ReAlloc(wineItem->pszText,
1072 TEXT_CALLBACK_SIZE);
1073 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1074 TRACE("setting callback, item %p\n",
1079 if (tvItem->mask & TVIF_CHILDREN)
1081 wineItem->cChildren = tvItem->cChildren;
1083 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1084 callbackSet |= TVIF_CHILDREN;
1086 callbackClear |= TVIF_CHILDREN;
1089 if (tvItem->mask & TVIF_IMAGE)
1091 wineItem->iImage = tvItem->iImage;
1093 if (wineItem->iImage == I_IMAGECALLBACK)
1094 callbackSet |= TVIF_IMAGE;
1096 callbackClear |= TVIF_IMAGE;
1099 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1101 wineItem->iSelectedImage = tvItem->iSelectedImage;
1103 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1104 callbackSet |= TVIF_SELECTEDIMAGE;
1106 callbackClear |= TVIF_SELECTEDIMAGE;
1109 if (tvItem->mask & TVIF_PARAM)
1110 wineItem->lParam = tvItem->lParam;
1112 /* If the application sets TVIF_INTEGRAL without
1113 * supplying a TVITEMEX structure, it's toast. */
1114 if (tvItem->mask & TVIF_INTEGRAL)
1115 wineItem->iIntegral = tvItem->iIntegral;
1117 if (tvItem->mask & TVIF_STATE)
1119 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1121 wineItem->state &= ~tvItem->stateMask;
1122 wineItem->state |= (tvItem->state & tvItem->stateMask);
1125 wineItem->callbackMask |= callbackSet;
1126 wineItem->callbackMask &= ~callbackClear;
1131 /* Note that the new item is pre-zeroed. */
1133 TREEVIEW_InsertItemA(TREEVIEW_INFO *infoPtr, LPARAM lParam)
1135 const TVINSERTSTRUCTA *ptdi = (LPTVINSERTSTRUCTA) lParam;
1136 const TVITEMEXA *tvItem = &ptdi->DUMMYUNIONNAME.itemex;
1137 HTREEITEM insertAfter;
1138 TREEVIEW_ITEM *newItem, *parentItem;
1139 BOOL bTextUpdated = FALSE;
1141 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1143 parentItem = infoPtr->root;
1147 parentItem = ptdi->hParent;
1149 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1151 WARN("invalid parent %p\n", parentItem);
1152 return (LRESULT)(HTREEITEM)NULL;
1156 insertAfter = ptdi->hInsertAfter;
1158 /* Validate this now for convenience. */
1159 switch ((DWORD)insertAfter)
1161 case (DWORD)TVI_FIRST:
1162 case (DWORD)TVI_LAST:
1163 case (DWORD)TVI_SORT:
1167 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1168 insertAfter->parent != parentItem)
1170 WARN("invalid insert after %p\n", insertAfter);
1171 insertAfter = TVI_LAST;
1175 TRACE("parent %p position %p: '%s'\n", parentItem, insertAfter,
1176 (tvItem->mask & TVIF_TEXT)
1177 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKA) ? "<callback>"
1181 newItem = TREEVIEW_AllocateItem(infoPtr);
1182 if (newItem == NULL)
1183 return (LRESULT)(HTREEITEM)NULL;
1185 newItem->parent = parentItem;
1186 newItem->iIntegral = 1;
1188 if (!TREEVIEW_DoSetItem(infoPtr, newItem, tvItem))
1189 return (LRESULT)(HTREEITEM)NULL;
1191 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1193 infoPtr->uNumItems++;
1195 switch ((DWORD)insertAfter)
1197 case (DWORD)TVI_FIRST:
1198 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1199 if (infoPtr->firstVisible == parentItem->firstChild)
1200 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1203 case (DWORD)TVI_LAST:
1204 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1207 /* hInsertAfter names a specific item we want to insert after */
1209 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1212 case (DWORD)TVI_SORT:
1214 TREEVIEW_ITEM *aChild;
1215 TREEVIEW_ITEM *previousChild = NULL;
1216 BOOL bItemInserted = FALSE;
1218 aChild = parentItem->firstChild;
1220 bTextUpdated = TRUE;
1221 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1223 /* Iterate the parent children to see where we fit in */
1224 while (aChild != NULL)
1228 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1229 comp = lstrcmpA(newItem->pszText, aChild->pszText);
1231 if (comp < 0) /* we are smaller than the current one */
1233 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1234 bItemInserted = TRUE;
1237 else if (comp > 0) /* we are bigger than the current one */
1239 previousChild = aChild;
1241 /* This will help us to exit if there is no more sibling */
1242 aChild = (aChild->nextSibling == 0)
1244 : aChild->nextSibling;
1246 /* Look at the next item */
1252 * An item with this name is already existing, therefore,
1253 * we add after the one we found
1255 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1256 bItemInserted = TRUE;
1262 * we reach the end of the child list and the item has not
1263 * yet been inserted, therefore, insert it after the last child.
1265 if ((!bItemInserted) && (aChild == NULL))
1266 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1273 TRACE("new item %p; parent %p, mask %x\n", newItem,
1274 newItem->parent, tvItem->mask);
1276 newItem->iLevel = newItem->parent->iLevel + 1;
1278 if (newItem->parent->cChildren == 0)
1279 newItem->parent->cChildren = 1;
1281 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1283 if (STATEIMAGEINDEX(newItem->state) == 0)
1284 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1287 if (infoPtr->firstVisible == NULL)
1288 infoPtr->firstVisible = newItem;
1290 TREEVIEW_VerifyTree(infoPtr);
1292 if (parentItem == infoPtr->root ||
1293 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1295 TREEVIEW_ITEM *item;
1296 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1298 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1299 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1302 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1304 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1305 TREEVIEW_UpdateScrollBars(infoPtr);
1307 * if the item was inserted in a visible part of the tree,
1308 * invalidate it, as well as those after it
1310 for (item = newItem;
1312 item = TREEVIEW_GetNextListItem(infoPtr, item))
1313 TREEVIEW_Invalidate(infoPtr, item);
1317 newItem->visibleOrder = -1;
1319 /* refresh treeview if newItem is the first item inserted under parentItem */
1320 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1322 /* parent got '+' - update it */
1323 TREEVIEW_Invalidate(infoPtr, parentItem);
1327 return (LRESULT)newItem;
1332 TREEVIEW_InsertItemW(TREEVIEW_INFO *infoPtr, LPARAM lParam)
1334 TVINSERTSTRUCTW *tvisW;
1335 TVINSERTSTRUCTA tvisA;
1338 tvisW = (LPTVINSERTSTRUCTW) lParam;
1340 tvisA.hParent = tvisW->hParent;
1341 tvisA.hInsertAfter = tvisW->hInsertAfter;
1343 tvisA.DUMMYUNIONNAME.item.mask = tvisW->DUMMYUNIONNAME.item.mask;
1344 tvisA.DUMMYUNIONNAME.item.hItem = tvisW->DUMMYUNIONNAME.item.hItem;
1345 tvisA.DUMMYUNIONNAME.item.state = tvisW->DUMMYUNIONNAME.item.state;
1346 tvisA.DUMMYUNIONNAME.item.stateMask = tvisW->DUMMYUNIONNAME.item.stateMask;
1347 tvisA.DUMMYUNIONNAME.item.cchTextMax =
1348 tvisW->DUMMYUNIONNAME.item.cchTextMax;
1350 if (tvisW->DUMMYUNIONNAME.item.pszText)
1352 if (tvisW->DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKW)
1354 int len = WideCharToMultiByte( CP_ACP, 0, tvisW->DUMMYUNIONNAME.item.pszText, -1,
1355 NULL, 0, NULL, NULL );
1356 tvisA.DUMMYUNIONNAME.item.pszText = COMCTL32_Alloc(len);
1357 WideCharToMultiByte( CP_ACP, 0, tvisW->DUMMYUNIONNAME.item.pszText, -1,
1358 tvisA.DUMMYUNIONNAME.item.pszText, len, NULL, NULL );
1362 tvisA.DUMMYUNIONNAME.item.pszText = LPSTR_TEXTCALLBACKA;
1363 tvisA.DUMMYUNIONNAME.item.cchTextMax = 0;
1367 tvisA.DUMMYUNIONNAME.item.iImage = tvisW->DUMMYUNIONNAME.item.iImage;
1368 tvisA.DUMMYUNIONNAME.item.iSelectedImage =
1369 tvisW->DUMMYUNIONNAME.item.iSelectedImage;
1370 tvisA.DUMMYUNIONNAME.item.cChildren = tvisW->DUMMYUNIONNAME.item.cChildren;
1371 tvisA.DUMMYUNIONNAME.item.lParam = tvisW->DUMMYUNIONNAME.item.lParam;
1373 lRes = TREEVIEW_InsertItemA(infoPtr, (LPARAM)&tvisA);
1375 if (tvisA.DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKA)
1377 COMCTL32_Free(tvisA.DUMMYUNIONNAME.item.pszText);
1385 /* Item Deletion ************************************************************/
1387 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1390 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1392 TREEVIEW_ITEM *kill = parentItem->firstChild;
1394 while (kill != NULL)
1396 TREEVIEW_ITEM *next = kill->nextSibling;
1398 TREEVIEW_RemoveItem(infoPtr, kill);
1403 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1404 assert(parentItem->firstChild == NULL);
1405 assert(parentItem->lastChild == NULL);
1409 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1411 TREEVIEW_ITEM *parentItem = item->parent;
1413 assert(item != NULL);
1414 assert(item->parent != NULL); /* i.e. it must not be the root */
1416 if (parentItem->firstChild == item)
1417 parentItem->firstChild = item->nextSibling;
1419 if (parentItem->lastChild == item)
1420 parentItem->lastChild = item->prevSibling;
1422 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1423 && parentItem->cChildren > 0)
1424 parentItem->cChildren = 0;
1426 if (item->prevSibling)
1427 item->prevSibling->nextSibling = item->nextSibling;
1429 if (item->nextSibling)
1430 item->nextSibling->prevSibling = item->prevSibling;
1434 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1436 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1438 TREEVIEW_SendTreeviewNotify(infoPtr,
1439 (infoPtr->bNtfUnicode) ? TVN_DELETEITEMW :
1441 TVIF_HANDLE | TVIF_PARAM, 0, wineItem, 0);
1443 if (wineItem->firstChild)
1444 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1446 TREEVIEW_UnlinkItem(wineItem);
1448 infoPtr->uNumItems--;
1450 if (wineItem->pszText != LPSTR_TEXTCALLBACKA)
1451 COMCTL32_Free(wineItem->pszText);
1453 TREEVIEW_FreeItem(infoPtr, wineItem);
1457 /* Empty out the tree. */
1459 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1461 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1463 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1467 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1469 TREEVIEW_ITEM *oldSelection = infoPtr->selectedItem;
1470 TREEVIEW_ITEM *newSelection = oldSelection;
1471 TREEVIEW_ITEM *newFirstVisible = NULL;
1472 TREEVIEW_ITEM *parent, *prev = NULL;
1473 BOOL visible = FALSE;
1475 if (wineItem == TVI_ROOT)
1477 TRACE("TVI_ROOT\n");
1478 parent = infoPtr->root;
1479 newSelection = NULL;
1481 TREEVIEW_RemoveTree(infoPtr);
1485 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1488 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1489 parent = wineItem->parent;
1491 if (ISVISIBLE(wineItem))
1493 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1497 if (infoPtr->selectedItem != NULL
1498 && (wineItem == infoPtr->selectedItem
1499 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1501 if (wineItem->nextSibling)
1502 newSelection = wineItem->nextSibling;
1503 else if (wineItem->parent != infoPtr->root)
1504 newSelection = wineItem->parent;
1507 if (infoPtr->firstVisible == wineItem)
1509 if (wineItem->nextSibling)
1510 newFirstVisible = wineItem->nextSibling;
1511 else if (wineItem->prevSibling)
1512 newFirstVisible = wineItem->prevSibling;
1513 else if (wineItem->parent != infoPtr->root)
1514 newFirstVisible = wineItem->parent;
1517 newFirstVisible = infoPtr->firstVisible;
1519 TREEVIEW_RemoveItem(infoPtr, wineItem);
1522 /* Don't change if somebody else already has. */
1523 if (oldSelection == infoPtr->selectedItem)
1525 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1526 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1528 infoPtr->selectedItem = 0;
1531 /* Validate insertMark dropItem.
1532 * hotItem ??? - used for comparison only.
1534 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1535 infoPtr->insertMarkItem = 0;
1537 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1538 infoPtr->dropItem = 0;
1540 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1541 newFirstVisible = infoPtr->root->firstChild;
1543 TREEVIEW_VerifyTree(infoPtr);
1548 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1549 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1550 TREEVIEW_UpdateScrollBars(infoPtr);
1551 TREEVIEW_Invalidate(infoPtr, NULL);
1553 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1555 /* parent lost '+/-' - update it */
1556 TREEVIEW_Invalidate(infoPtr, parent);
1563 /* Get/Set Messages *********************************************************/
1565 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1568 infoPtr->bRedraw = TRUE;
1570 infoPtr->bRedraw = FALSE;
1576 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1579 return infoPtr->uIndent;
1583 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1587 if (newIndent < MINIMUM_INDENT)
1588 newIndent = MINIMUM_INDENT;
1590 if (infoPtr->uIndent != newIndent)
1592 infoPtr->uIndent = newIndent;
1593 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1594 TREEVIEW_UpdateScrollBars(infoPtr);
1595 TREEVIEW_Invalidate(infoPtr, NULL);
1603 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1606 return infoPtr->hwndToolTip;
1610 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1615 prevToolTip = infoPtr->hwndToolTip;
1616 infoPtr->hwndToolTip = hwndTT;
1623 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1625 return infoPtr->uScrollTime;
1629 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1631 UINT uOldScrollTime = infoPtr->uScrollTime;
1633 infoPtr->uScrollTime = min(uScrollTime, 100);
1635 return uOldScrollTime;
1640 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1646 case (WPARAM)TVSIL_NORMAL:
1647 return (LRESULT)infoPtr->himlNormal;
1649 case (WPARAM)TVSIL_STATE:
1650 return (LRESULT)infoPtr->himlState;
1658 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1660 HIMAGELIST himlOld = 0;
1661 int oldWidth = infoPtr->normalImageWidth;
1662 int oldHeight = infoPtr->normalImageHeight;
1665 TRACE("%x,%p\n", wParam, himlNew);
1669 case (WPARAM)TVSIL_NORMAL:
1670 himlOld = infoPtr->himlNormal;
1671 infoPtr->himlNormal = himlNew;
1673 if (himlNew != NULL)
1674 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1675 &infoPtr->normalImageHeight);
1678 infoPtr->normalImageWidth = 0;
1679 infoPtr->normalImageHeight = 0;
1684 case (WPARAM)TVSIL_STATE:
1685 himlOld = infoPtr->himlState;
1686 infoPtr->himlState = himlNew;
1688 if (himlNew != NULL)
1689 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1690 &infoPtr->stateImageHeight);
1693 infoPtr->stateImageWidth = 0;
1694 infoPtr->stateImageHeight = 0;
1700 if (oldWidth != infoPtr->normalImageWidth ||
1701 oldHeight != infoPtr->normalImageHeight)
1703 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1704 TREEVIEW_UpdateScrollBars(infoPtr);
1707 TREEVIEW_Invalidate(infoPtr, NULL);
1709 return (LRESULT)himlOld;
1712 /* Compute the natural height (based on the font size) for items. */
1714 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1718 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1720 GetTextMetricsA(hdc, &tm);
1722 SelectObject(hdc, hOldFont);
1725 /* The 16 is a hack because our fonts are tiny. */
1726 /* add 2 for the focus border and 1 more for margin some apps assume */
1727 return max(16, tm.tmHeight + tm.tmExternalLeading + 3);
1731 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1733 INT prevHeight = infoPtr->uItemHeight;
1735 TRACE("%d \n", newHeight);
1736 if (newHeight == -1)
1738 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1739 infoPtr->bHeightSet = FALSE;
1743 infoPtr->uItemHeight = newHeight;
1744 infoPtr->bHeightSet = TRUE;
1747 /* Round down, unless we support odd ("non even") heights. */
1748 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1749 infoPtr->uItemHeight &= ~1;
1751 if (infoPtr->uItemHeight != prevHeight)
1753 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1754 TREEVIEW_UpdateScrollBars(infoPtr);
1755 TREEVIEW_Invalidate(infoPtr, NULL);
1762 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1765 return infoPtr->uItemHeight;
1770 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1772 TRACE("%x\n", infoPtr->hFont);
1773 return infoPtr->hFont;
1778 TREEVIEW_ResetTextWidth(LPVOID pItem, DWORD unused)
1782 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1788 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1790 UINT uHeight = infoPtr->uItemHeight;
1792 TRACE("%x %i\n", hFont, bRedraw);
1794 infoPtr->hFont = hFont ? hFont : GetStockObject(SYSTEM_FONT);
1796 DeleteObject(infoPtr->hBoldFont);
1797 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1799 if (!infoPtr->bHeightSet)
1800 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1802 if (uHeight != infoPtr->uItemHeight)
1803 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1805 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1807 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1808 TREEVIEW_UpdateScrollBars(infoPtr);
1811 TREEVIEW_Invalidate(infoPtr, NULL);
1818 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1821 return (LRESULT)infoPtr->clrLine;
1825 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1827 COLORREF prevColor = infoPtr->clrLine;
1830 infoPtr->clrLine = color;
1831 return (LRESULT)prevColor;
1836 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1839 return (LRESULT)infoPtr->clrText;
1843 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1845 COLORREF prevColor = infoPtr->clrText;
1848 infoPtr->clrText = color;
1850 if (infoPtr->clrText != prevColor)
1851 TREEVIEW_Invalidate(infoPtr, NULL);
1853 return (LRESULT)prevColor;
1858 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1861 return (LRESULT)infoPtr->clrBk;
1865 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1867 COLORREF prevColor = infoPtr->clrBk;
1870 infoPtr->clrBk = newColor;
1872 if (newColor != prevColor)
1873 TREEVIEW_Invalidate(infoPtr, NULL);
1875 return (LRESULT)prevColor;
1880 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1883 return (LRESULT)infoPtr->clrInsertMark;
1887 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1889 COLORREF prevColor = infoPtr->clrInsertMark;
1891 TRACE("%lx\n", color);
1892 infoPtr->clrInsertMark = color;
1894 return (LRESULT)prevColor;
1899 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1901 TRACE("%d %p\n", wParam, item);
1903 if (!TREEVIEW_ValidItem(infoPtr, item))
1906 infoPtr->insertBeforeorAfter = wParam;
1907 infoPtr->insertMarkItem = item;
1909 TREEVIEW_Invalidate(infoPtr, NULL);
1915 /************************************************************************
1916 * Some serious braindamage here. lParam is a pointer to both the
1917 * input HTREEITEM and the output RECT.
1920 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1922 TREEVIEW_ITEM *wineItem;
1923 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1927 * validate parameters
1933 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
1937 * If wParam is TRUE return the text size otherwise return
1938 * the whole item size
1942 /* Windows does not send TVN_GETDISPINFO here. */
1944 lpRect->top = wineItem->rect.top;
1945 lpRect->bottom = wineItem->rect.bottom;
1947 lpRect->left = wineItem->textOffset;
1948 lpRect->right = wineItem->textOffset + wineItem->textWidth;
1952 *lpRect = wineItem->rect;
1955 TRACE("%s [L:%d R:%d T:%d B:%d]\n", fTextRect ? "text" : "item",
1956 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
1961 static inline LRESULT
1962 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
1964 /* Suprise! This does not take integral height into account. */
1965 return infoPtr->clientHeight / infoPtr->uItemHeight;
1970 TREEVIEW_GetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1972 TREEVIEW_ITEM *wineItem;
1974 wineItem = tvItem->hItem;
1975 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1978 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1980 if (tvItem->mask & TVIF_CHILDREN)
1981 tvItem->cChildren = wineItem->cChildren;
1983 if (tvItem->mask & TVIF_HANDLE)
1984 tvItem->hItem = wineItem;
1986 if (tvItem->mask & TVIF_IMAGE)
1987 tvItem->iImage = wineItem->iImage;
1989 if (tvItem->mask & TVIF_INTEGRAL)
1990 tvItem->iIntegral = wineItem->iIntegral;
1992 /* undocumented: windows ignores TVIF_PARAM and
1993 * * always sets lParam
1995 tvItem->lParam = wineItem->lParam;
1997 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1998 tvItem->iSelectedImage = wineItem->iSelectedImage;
2000 if (tvItem->mask & TVIF_STATE)
2001 tvItem->state = wineItem->state & tvItem->stateMask;
2003 if (tvItem->mask & TVIF_TEXT)
2004 lstrcpynA(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2006 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2007 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2012 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2013 * which is wrong. */
2015 TREEVIEW_SetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
2017 TREEVIEW_ITEM *wineItem;
2018 TREEVIEW_ITEM originalItem;
2020 wineItem = tvItem->hItem;
2022 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2025 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2028 if (!TREEVIEW_DoSetItem(infoPtr, wineItem, tvItem))
2031 /* store the orignal item values */
2032 originalItem = *wineItem;
2034 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2035 if ((tvItem->mask & TVIF_TEXT
2036 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2037 && ISVISIBLE(wineItem))
2039 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2040 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2043 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2045 /* The refresh updates everything, but we can't wait until then. */
2046 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2048 /* if any of the items values changed, redraw the item */
2049 if(memcmp(&originalItem, wineItem, sizeof(TREEVIEW_ITEM)))
2051 if (tvItem->mask & TVIF_INTEGRAL)
2053 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2054 TREEVIEW_UpdateScrollBars(infoPtr);
2056 TREEVIEW_Invalidate(infoPtr, NULL);
2060 TREEVIEW_UpdateScrollBars(infoPtr);
2061 TREEVIEW_Invalidate(infoPtr, wineItem);
2070 TREEVIEW_GetItemW(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem)
2072 TREEVIEW_ITEM *wineItem;
2074 iItem = (INT)tvItem->hItem;
2076 wineItem = tvItem->hItem;
2077 if(!TREEVIEW_ValidItem (infoPtr, wineItem))
2080 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2082 if (tvItem->mask & TVIF_CHILDREN) {
2083 if (TVIF_CHILDREN==I_CHILDRENCALLBACK)
2084 FIXME("I_CHILDRENCALLBACK not supported\n");
2085 tvItem->cChildren = wineItem->cChildren;
2088 if (tvItem->mask & TVIF_HANDLE) {
2089 tvItem->hItem = wineItem;
2091 if (tvItem->mask & TVIF_IMAGE) {
2092 tvItem->iImage = wineItem->iImage;
2094 if (tvItem->mask & TVIF_INTEGRAL) {
2095 tvItem->iIntegral = wineItem->iIntegral;
2097 /* undocumented: windows ignores TVIF_PARAM and
2098 * always sets lParam */
2099 tvItem->lParam = wineItem->lParam;
2100 if (tvItem->mask & TVIF_SELECTEDIMAGE) {
2101 tvItem->iSelectedImage = wineItem->iSelectedImage;
2103 if (tvItem->mask & TVIF_STATE) {
2104 tvItem->state = wineItem->state & tvItem->stateMask;
2107 if (tvItem->mask & TVIF_TEXT) {
2108 if (wineItem->pszText == LPSTR_TEXTCALLBACKA) {
2109 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2110 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2112 else if (wineItem->pszText) {
2113 TRACE("orig str %s at %p\n",
2114 debugstr_a(wineItem->pszText), wineItem->pszText);
2115 MultiByteToWideChar(CP_ACP, 0, wineItem->pszText,
2116 -1 , tvItem->pszText, tvItem->cchTextMax);
2120 TRACE("item %d<%p>, txt %p<%s>, img %p, action %x\n",
2121 iItem, tvItem, tvItem->pszText, debugstr_w(tvItem->pszText),
2122 &tvItem->iImage, tvItem->mask);
2127 TREEVIEW_SetItemW(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem)
2133 tvItemA.mask = tvItem->mask;
2134 tvItemA.hItem = tvItem->hItem;
2135 tvItemA.state = tvItem->state;
2136 tvItemA.stateMask = tvItem->stateMask;
2137 if (tvItem->mask & TVIF_TEXT) {
2138 len = WideCharToMultiByte(CP_ACP, 0, tvItem->pszText, -1,
2139 NULL ,0 , NULL,NULL);
2142 tvItemA.pszText = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
2143 len = WideCharToMultiByte(CP_ACP, 0, tvItem->pszText, -1,
2144 tvItemA.pszText ,len*sizeof(WCHAR),
2148 tvItemA.pszText = NULL;
2150 tvItemA.cchTextMax = tvItem->cchTextMax;
2151 tvItemA.iImage = tvItem->iImage;
2152 tvItemA.iSelectedImage = tvItem->iSelectedImage;
2153 tvItemA.cChildren = tvItem->cChildren;
2154 tvItemA.lParam = tvItem->lParam;
2155 tvItemA.iIntegral = tvItem->iIntegral;
2157 rc = TREEVIEW_SetItemA(infoPtr,&tvItemA);
2158 HeapFree(GetProcessHeap(),0,tvItemA.pszText);
2163 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2167 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2170 return (wineItem->state & mask);
2174 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2176 TREEVIEW_ITEM *retval;
2180 /* handle all the global data here */
2183 case TVGN_CHILD: /* Special case: child of 0 is root */
2188 retval = infoPtr->root->firstChild;
2192 retval = infoPtr->selectedItem;
2195 case TVGN_FIRSTVISIBLE:
2196 retval = infoPtr->firstVisible;
2199 case TVGN_DROPHILITE:
2200 retval = infoPtr->dropItem;
2203 case TVGN_LASTVISIBLE:
2204 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2210 TRACE("flags:%x, returns %p\n", which, retval);
2211 return (LRESULT)retval;
2214 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2216 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2222 retval = wineItem->nextSibling;
2225 retval = wineItem->prevSibling;
2228 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2231 retval = wineItem->firstChild;
2233 case TVGN_NEXTVISIBLE:
2234 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2236 case TVGN_PREVIOUSVISIBLE:
2237 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2240 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2244 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2245 return (LRESULT)retval;
2250 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2252 TRACE(" %d\n", infoPtr->uNumItems);
2253 return (LRESULT)infoPtr->uNumItems;
2257 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2259 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2261 static const unsigned int state_table[] = { 0, 2, 1 };
2265 state = STATEIMAGEINDEX(item->state);
2266 TRACE("state:%x\n", state);
2267 item->state &= ~TVIS_STATEIMAGEMASK;
2270 state = state_table[state];
2272 item->state |= INDEXTOSTATEIMAGEMASK(state);
2274 TRACE("state:%x\n", state);
2275 TREEVIEW_Invalidate(infoPtr, item);
2280 /* Painting *************************************************************/
2282 /* Draw the lines and expand button for an item. Also draws one section
2283 * of the line from item's parent to item's parent's next sibling. */
2285 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2287 LONG centerx, centery;
2288 BOOL lar = ((infoPtr->dwStyle
2289 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2292 if (!lar && item->iLevel == 0)
2295 centerx = (item->linesOffset + item->stateOffset) / 2;
2296 centery = (item->rect.top + item->rect.bottom) / 2;
2298 if (infoPtr->dwStyle & TVS_HASLINES)
2300 HPEN hOldPen, hNewPen;
2304 * Get a dotted grey pen
2306 hNewPen = CreatePen(PS_ALTERNATE, 0, infoPtr->clrLine);
2307 hOldPen = SelectObject(hdc, hNewPen);
2309 MoveToEx(hdc, item->stateOffset, centery, NULL);
2310 LineTo(hdc, centerx - 1, centery);
2312 if (item->prevSibling || item->parent != infoPtr->root)
2314 MoveToEx(hdc, centerx, item->rect.top, NULL);
2315 LineTo(hdc, centerx, centery);
2318 if (item->nextSibling)
2320 MoveToEx(hdc, centerx, centery, NULL);
2321 LineTo(hdc, centerx, item->rect.bottom + 1);
2324 /* Draw the line from our parent to its next sibling. */
2325 parent = item->parent;
2326 while (parent != infoPtr->root)
2328 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2330 if (parent->nextSibling
2331 /* skip top-levels unless TVS_LINESATROOT */
2332 && parent->stateOffset > parent->linesOffset)
2334 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2335 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2338 parent = parent->parent;
2341 SelectObject(hdc, hOldPen);
2342 DeleteObject(hNewPen);
2346 * Display the (+/-) signs
2349 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2351 if (item->cChildren)
2353 LONG height = item->rect.bottom - item->rect.top;
2354 LONG width = item->stateOffset - item->linesOffset;
2355 LONG rectsize = min(height, width) / 4;
2356 /* plussize = ceil(rectsize * 3/4) */
2357 LONG plussize = (rectsize + 1) * 3 / 4;
2359 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2360 HPEN hOldPen = SelectObject(hdc, hNewPen);
2361 HBRUSH hbr = CreateSolidBrush(infoPtr->clrBk);
2362 HBRUSH hbrOld = SelectObject(hdc, hbr);
2364 Rectangle(hdc, centerx - rectsize, centery - rectsize,
2365 centerx + rectsize + 1, centery + rectsize + 1);
2367 SelectObject(hdc, hbrOld);
2370 SelectObject(hdc, hOldPen);
2371 DeleteObject(hNewPen);
2373 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2374 LineTo(hdc, centerx + plussize, centery);
2376 if (!(item->state & TVIS_EXPANDED))
2378 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2379 LineTo(hdc, centerx, centery + plussize);
2386 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2392 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2394 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2396 /* The custom draw handler can query the text rectangle,
2398 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2402 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2404 cditem = TREEVIEW_SendCustomDrawItemNotify
2405 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT);
2406 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2408 if (cditem & CDRF_SKIPDEFAULT)
2410 SelectObject(hdc, hOldFont);
2415 if (cditem & CDRF_NEWFONT)
2416 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2418 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2420 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2423 * Display the images associated with this item
2428 /* State images are displayed to the left of the Normal image
2429 * image number is in state; zero should be `display no image'.
2431 imageIndex = STATEIMAGEINDEX(wineItem->state);
2433 if (infoPtr->himlState && imageIndex)
2435 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2436 wineItem->stateOffset,
2437 centery - infoPtr->stateImageHeight / 2,
2441 /* Now, draw the normal image; can be either selected or
2442 * non-selected image.
2445 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage))
2447 /* The item is currently selected */
2448 imageIndex = wineItem->iSelectedImage;
2452 /* The item is not selected */
2453 imageIndex = wineItem->iImage;
2456 if (infoPtr->himlNormal)
2458 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2460 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2461 wineItem->imageOffset,
2462 centery - infoPtr->normalImageHeight / 2,
2463 ILD_NORMAL | ovlIdx);
2469 * Display the text associated with this item
2472 /* Don't paint item's text if it's being edited */
2473 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2475 if (wineItem->pszText)
2477 COLORREF oldTextColor = 0;
2480 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2483 oldBkMode = SetBkMode(hdc, TRANSPARENT);
2485 /* - If item is drop target or it is selected and window is in focus -
2486 * use blue background (COLOR_HIGHLIGHT).
2487 * - If item is selected, window is not in focus, but it has style
2488 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2489 * - Otherwise - don't fill background
2491 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2492 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2493 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2495 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2497 hbrBk = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
2499 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2503 hbrBk = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
2505 if (infoPtr->clrText == -1)
2507 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2509 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2514 if (infoPtr->clrText == -1)
2516 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2518 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2521 rcText.top = wineItem->rect.top;
2522 rcText.bottom = wineItem->rect.bottom;
2523 rcText.left = wineItem->textOffset;
2524 rcText.right = rcText.left + wineItem->textWidth + 4;
2528 FillRect(hdc, &rcText, hbrBk);
2529 DeleteObject(hbrBk);
2532 /* Draw the box around the selected item */
2533 if ((wineItem == infoPtr->selectedItem) && inFocus)
2535 DrawFocusRect(hdc,&rcText);
2538 InflateRect(&rcText, -2, -1); /* allow for the focus rect */
2540 TRACE("drawing text %s at (%d,%d)-(%d,%d)\n",
2541 debugstr_a(wineItem->pszText),
2542 rcText.left, rcText.top, rcText.right, rcText.bottom);
2547 lstrlenA(wineItem->pszText),
2549 DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
2551 /* Restore the hdc state */
2552 SetTextColor(hdc, oldTextColor);
2554 if (oldBkMode != TRANSPARENT)
2555 SetBkMode(hdc, oldBkMode);
2559 /* Draw insertion mark if necessary */
2561 if (infoPtr->insertMarkItem)
2562 TRACE("item:%d,mark:%d\n",
2563 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2564 (int)infoPtr->insertMarkItem);
2566 if (wineItem == infoPtr->insertMarkItem)
2568 HPEN hNewPen, hOldPen;
2572 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2573 hOldPen = SelectObject(hdc, hNewPen);
2575 if (infoPtr->insertBeforeorAfter)
2576 offset = wineItem->rect.bottom - 1;
2578 offset = wineItem->rect.top + 1;
2580 left = wineItem->textOffset - 2;
2581 right = wineItem->textOffset + wineItem->textWidth + 2;
2583 MoveToEx(hdc, left, offset - 3, NULL);
2584 LineTo(hdc, left, offset + 4);
2586 MoveToEx(hdc, left, offset, NULL);
2587 LineTo(hdc, right + 1, offset);
2589 MoveToEx(hdc, right, offset + 3, NULL);
2590 LineTo(hdc, right, offset - 4);
2592 SelectObject(hdc, hOldPen);
2593 DeleteObject(hNewPen);
2596 if (cditem & CDRF_NOTIFYPOSTPAINT)
2598 cditem = TREEVIEW_SendCustomDrawItemNotify
2599 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT);
2600 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2603 SelectObject(hdc, hOldFont);
2606 /* Computes treeHeight and treeWidth and updates the scroll bars.
2609 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2611 TREEVIEW_ITEM *wineItem;
2612 HWND hwnd = infoPtr->hwnd;
2616 LONG scrollX = infoPtr->scrollX;
2618 infoPtr->treeWidth = 0;
2619 infoPtr->treeHeight = 0;
2621 /* We iterate through all visible items in order to get the tree height
2623 wineItem = infoPtr->root->firstChild;
2625 while (wineItem != NULL)
2627 if (ISVISIBLE(wineItem))
2629 /* actually we draw text at textOffset + 2 */
2630 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2631 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2633 /* This is scroll-adjusted, but we fix this below. */
2634 infoPtr->treeHeight = wineItem->rect.bottom;
2637 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2640 /* Fix the scroll adjusted treeHeight and treeWidth. */
2641 if (infoPtr->root->firstChild)
2642 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2644 infoPtr->treeWidth += infoPtr->scrollX;
2646 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2648 /* Adding one scroll bar may take up enough space that it forces us
2649 * to add the other as well. */
2650 if (infoPtr->treeHeight > infoPtr->clientHeight)
2654 if (infoPtr->treeWidth
2655 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2658 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2661 if (!vert && horz && infoPtr->treeHeight
2662 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2665 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2667 si.cbSize = sizeof(SCROLLINFO);
2668 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2673 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2674 si.nPos = infoPtr->firstVisible->visibleOrder;
2675 si.nMax = infoPtr->maxVisibleOrder - 1;
2677 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2679 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2680 ShowScrollBar(hwnd, SB_VERT, TRUE);
2681 infoPtr->uInternalStatus |= TV_VSCROLL;
2685 if (infoPtr->uInternalStatus & TV_VSCROLL)
2686 ShowScrollBar(hwnd, SB_VERT, FALSE);
2687 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2692 si.nPage = infoPtr->clientWidth;
2693 si.nPos = infoPtr->scrollX;
2694 si.nMax = infoPtr->treeWidth - 1;
2696 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2698 si.nPos = si.nMax - max( si.nPage-1, 0 );
2702 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2703 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2704 infoPtr->uInternalStatus |= TV_HSCROLL;
2706 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2710 if (infoPtr->uInternalStatus & TV_HSCROLL)
2711 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2712 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2717 if (infoPtr->scrollX != scrollX)
2719 TREEVIEW_HScroll(infoPtr,
2720 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2724 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2727 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2729 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2731 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2734 GetClientRect(infoPtr->hwnd, &rect);
2735 FillRect(hDC, &rect, hBrush);
2736 DeleteObject(hBrush);
2742 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2744 HWND hwnd = infoPtr->hwnd;
2746 TREEVIEW_ITEM *wineItem;
2748 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2750 TRACE("empty window\n");
2754 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2757 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2759 ReleaseDC(hwnd, hdc);
2763 for (wineItem = infoPtr->root->firstChild;
2765 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2767 if (ISVISIBLE(wineItem))
2769 /* Avoid unneeded calculations */
2770 if (wineItem->rect.top > rect.bottom)
2772 if (wineItem->rect.bottom < rect.top)
2775 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2779 TREEVIEW_UpdateScrollBars(infoPtr);
2781 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2783 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2787 TREEVIEW_Invalidate(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2790 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2792 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2796 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2807 if (!GetUpdateRect(infoPtr->hwnd, &rc, TRUE))
2811 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2812 if (!hbitmap) return 0;
2813 GetObjectA(hbitmap, sizeof(BITMAP), &bitmap);
2814 rc.left = 0; rc.top = 0;
2815 rc.right = bitmap.bmWidth;
2816 rc.bottom = bitmap.bmHeight;
2817 TREEVIEW_EraseBackground(infoPtr, wParam);
2822 hdc = BeginPaint(infoPtr->hwnd, &ps);
2826 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2827 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2830 EndPaint(infoPtr->hwnd, &ps);
2836 /* Sorting **************************************************************/
2838 /***************************************************************************
2839 * Forward the DPA local callback to the treeview owner callback
2842 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2844 /* Forward the call to the client-defined callback */
2845 return pCallBackSort->lpfnCompare(first->lParam,
2847 pCallBackSort->lParam);
2850 /***************************************************************************
2851 * Treeview native sort routine: sort on item text.
2854 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2855 TREEVIEW_INFO *infoPtr)
2857 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2858 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2860 return strcasecmp(first->pszText, second->pszText);
2863 /* Returns the number of physical children belonging to item. */
2865 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2870 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2876 /* Returns a DPA containing a pointer to each physical child of item in
2877 * sibling order. If item has no children, an empty DPA is returned. */
2879 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2881 HTREEITEM child = item->firstChild;
2883 HDPA list = DPA_Create(8);
2884 if (list == 0) return NULL;
2886 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2888 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2898 /***************************************************************************
2899 * Setup the treeview structure with regards of the sort method
2900 * and sort the children of the TV item specified in lParam
2901 * fRecurse: currently unused. Should be zero.
2902 * parent: if pSort!=NULL, should equal pSort->hParent.
2903 * otherwise, item which child items are to be sorted.
2904 * pSort: sort method info. if NULL, sort on item text.
2905 * if non-NULL, sort on item's lParam content, and let the
2906 * application decide what that means. See also TVM_SORTCHILDRENCB.
2910 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2914 PFNDPACOMPARE pfnCompare;
2917 /* undocumented feature: TVI_ROOT means `sort the whole tree' */
2918 if (parent == TVI_ROOT)
2919 parent = infoPtr->root;
2921 /* Check for a valid handle to the parent item */
2922 if (!TREEVIEW_ValidItem(infoPtr, parent))
2924 ERR("invalid item hParent=%x\n", (INT)parent);
2930 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2931 lpCompare = (LPARAM)pSort;
2935 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2936 lpCompare = (LPARAM)infoPtr;
2939 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2941 /* Make sure there is something to sort */
2944 /* TREEVIEW_ITEM rechaining */
2947 HTREEITEM nextItem = 0;
2948 HTREEITEM prevItem = 0;
2950 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2952 if (sortList == NULL)
2955 /* let DPA sort the list */
2956 DPA_Sort(sortList, pfnCompare, lpCompare);
2958 /* The order of DPA entries has been changed, so fixup the
2959 * nextSibling and prevSibling pointers. */
2961 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2962 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2964 /* link the two current item toghether */
2965 item->nextSibling = nextItem;
2966 nextItem->prevSibling = item;
2968 if (prevItem == NULL)
2970 /* this is the first item, update the parent */
2971 parent->firstChild = item;
2972 item->prevSibling = NULL;
2976 /* fix the back chaining */
2977 item->prevSibling = prevItem;
2980 /* get ready for the next one */
2985 /* the last item is pointed to by item and never has a sibling */
2986 item->nextSibling = NULL;
2987 parent->lastChild = item;
2989 DPA_Destroy(sortList);
2991 TREEVIEW_VerifyTree(infoPtr);
2993 if (parent->state & TVIS_EXPANDED)
2995 int visOrder = infoPtr->firstVisible->visibleOrder;
2997 if (parent == infoPtr->root)
2998 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3000 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3002 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3004 TREEVIEW_ITEM *item;
3006 for (item = infoPtr->root->firstChild; item != NULL;
3007 item = TREEVIEW_GetNextListItem(infoPtr, item))
3009 if (item->visibleOrder == visOrder)
3013 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3016 TREEVIEW_Invalidate(infoPtr, NULL);
3025 /***************************************************************************
3026 * Setup the treeview structure with regards of the sort method
3027 * and sort the children of the TV item specified in lParam
3030 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3032 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3036 /***************************************************************************
3037 * Sort the children of the TV item specified in lParam.
3040 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3042 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3046 /* Expansion/Collapse ***************************************************/
3049 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3052 return !TREEVIEW_SendTreeviewNotify(infoPtr,
3053 (infoPtr->bNtfUnicode) ? TVN_ITEMEXPANDINGW :
3056 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3057 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3062 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3065 TREEVIEW_SendTreeviewNotify(infoPtr,
3066 (infoPtr->bNtfUnicode) ? TVN_ITEMEXPANDEDW :
3069 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3070 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3075 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3076 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3078 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3079 BOOL bRemoveChildren, BOOL bUser)
3081 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3082 BOOL bSetSelection, bSetFirstVisible;
3084 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3086 if (!(wineItem->state & TVIS_EXPANDED) || wineItem->firstChild == NULL)
3090 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3092 wineItem->state &= ~TVIS_EXPANDED;
3095 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3097 bSetSelection = (infoPtr->selectedItem != NULL
3098 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3100 bSetFirstVisible = (infoPtr->firstVisible != NULL
3101 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3103 if (bRemoveChildren)
3105 TRACE("TVE_COLLAPSERESET\n");
3106 wineItem->state &= ~TVIS_EXPANDEDONCE;
3107 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3110 if (wineItem->firstChild)
3112 TREEVIEW_ITEM *item, *sibling;
3114 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3116 for (item = wineItem->firstChild; item != sibling;
3117 item = TREEVIEW_GetNextListItem(infoPtr, item))
3119 item->visibleOrder = -1;
3123 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3125 TREEVIEW_SetFirstVisible(infoPtr, bSetFirstVisible ? wineItem
3126 : infoPtr->firstVisible, TRUE);
3130 /* Don't call DoSelectItem, it sends notifications. */
3131 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3132 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3133 wineItem->state |= TVIS_SELECTED;
3134 infoPtr->selectedItem = wineItem;
3136 TREEVIEW_EnsureVisible(infoPtr, wineItem, FALSE);
3139 TREEVIEW_UpdateScrollBars(infoPtr);
3140 TREEVIEW_Invalidate(infoPtr, NULL);
3146 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3147 BOOL bExpandPartial, BOOL bUser)
3151 if (!TREEVIEW_HasChildren(infoPtr, wineItem)
3152 || wineItem->state & TVIS_EXPANDED)
3155 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3157 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3159 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3161 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3165 wineItem->state |= TVIS_EXPANDED;
3166 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3167 wineItem->state |= TVIS_EXPANDEDONCE;
3171 /* this item has already been expanded */
3172 wineItem->state |= TVIS_EXPANDED;
3176 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3178 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3179 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3180 TREEVIEW_UpdateScrollBars(infoPtr);
3182 /* Scroll up so that as many children as possible are visible.
3183 * This looses when expanding causes an HScroll bar to appear, but we
3184 * don't know that yet, so the last item is obscured. */
3185 if (wineItem->firstChild != NULL)
3187 int nChildren = wineItem->lastChild->visibleOrder
3188 - wineItem->firstChild->visibleOrder + 1;
3190 int visible_pos = wineItem->visibleOrder
3191 - infoPtr->firstVisible->visibleOrder;
3193 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3195 if (visible_pos > 0 && nChildren > rows_below)
3197 int scroll = nChildren - rows_below;
3199 if (scroll > visible_pos)
3200 scroll = visible_pos;
3204 TREEVIEW_ITEM *newFirstVisible
3205 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3209 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3214 TREEVIEW_Invalidate(infoPtr, NULL);
3220 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3224 if (wineItem->state & TVIS_EXPANDED)
3225 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3227 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3231 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3233 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3235 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3237 if (TREEVIEW_HasChildren(infoPtr, item))
3238 TREEVIEW_ExpandAll(infoPtr, item);
3242 /* Note:If the specified item is the child of a collapsed parent item,
3243 the parent's list of child items is (recursively) expanded to reveal the
3244 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3245 know if it also applies here.
3249 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3251 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3254 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3255 TREEVIEW_ItemName(wineItem), flag,
3256 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3258 switch (flag & TVE_TOGGLE)
3261 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3265 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3269 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3276 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3280 /* Hit-Testing **********************************************************/
3282 static TREEVIEW_ITEM *
3283 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3285 TREEVIEW_ITEM *wineItem;
3288 if (!infoPtr->firstVisible)
3291 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3293 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3294 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3296 if (row >= wineItem->visibleOrder
3297 && row < wineItem->visibleOrder + wineItem->iIntegral)
3305 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3307 TREEVIEW_ITEM *wineItem;
3313 GetClientRect(infoPtr->hwnd, &rect);
3320 status |= TVHT_TOLEFT;
3322 else if (x > rect.right)
3324 status |= TVHT_TORIGHT;
3329 status |= TVHT_ABOVE;
3331 else if (y > rect.bottom)
3333 status |= TVHT_BELOW;
3338 lpht->flags = status;
3339 return (LRESULT)(HTREEITEM)NULL;
3342 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3345 lpht->flags = TVHT_NOWHERE;
3346 return (LRESULT)(HTREEITEM)NULL;
3349 if (x >= wineItem->textOffset + wineItem->textWidth)
3351 lpht->flags = TVHT_ONITEMRIGHT;
3353 else if (x >= wineItem->textOffset)
3355 lpht->flags = TVHT_ONITEMLABEL;
3357 else if (x >= wineItem->imageOffset)
3359 lpht->flags = TVHT_ONITEMICON;
3361 else if (x >= wineItem->stateOffset)
3363 lpht->flags = TVHT_ONITEMSTATEICON;
3365 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3367 lpht->flags = TVHT_ONITEMBUTTON;
3371 lpht->flags = TVHT_ONITEMINDENT;
3374 lpht->hItem = wineItem;
3375 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3377 return (LRESULT)wineItem;
3380 /* Item Label Editing ***************************************************/
3383 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3385 return infoPtr->hwndEdit;
3388 static LRESULT CALLBACK
3389 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3391 TREEVIEW_INFO *infoPtr;
3392 BOOL bCancel = FALSE;
3399 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3401 TRACE("WM_PAINT start\n");
3402 rc = CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3404 TRACE("WM_PAINT done\n");
3410 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3411 if (infoPtr->bIgnoreEditKillFocus)
3418 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3421 if (wParam == (WPARAM)VK_ESCAPE)
3426 else if (wParam == (WPARAM)VK_RETURN)
3434 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3436 return CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3441 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3442 /* eg. Using a messagebox */
3444 infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3445 infoPtr->bIgnoreEditKillFocus = TRUE;
3446 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3447 infoPtr->bIgnoreEditKillFocus = FALSE;
3453 /* should handle edit control messages here */
3456 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3458 TRACE("%x %ld\n", wParam, lParam);
3460 switch (HIWORD(wParam))
3465 * Adjust the edit window size
3468 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3469 HDC hdc = GetDC(infoPtr->hwndEdit);
3472 HFONT hFont, hOldFont = 0;
3474 infoPtr->bLabelChanged = TRUE;
3476 len = GetWindowTextA(infoPtr->hwndEdit, buffer, sizeof(buffer));
3478 /* Select font to get the right dimension of the string */
3479 hFont = SendMessageA(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3482 hOldFont = SelectObject(hdc, hFont);
3485 if (GetTextExtentPoint32A(hdc, buffer, strlen(buffer), &sz))
3487 TEXTMETRICA textMetric;
3489 /* Add Extra spacing for the next character */
3490 GetTextMetricsA(hdc, &textMetric);
3491 sz.cx += (textMetric.tmMaxCharWidth * 2);
3493 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3495 infoPtr->clientWidth - editItem->textOffset + 2);
3497 SetWindowPos(infoPtr->hwndEdit,
3502 editItem->rect.bottom - editItem->rect.top + 3,
3503 SWP_NOMOVE | SWP_DRAWFRAME);
3508 SelectObject(hdc, hOldFont);
3511 ReleaseDC(infoPtr->hwnd, hdc);
3516 return SendMessageA(GetParent(infoPtr->hwnd), WM_COMMAND, wParam, lParam);
3523 TREEVIEW_EditLabelA(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3525 HWND hwnd = infoPtr->hwnd;
3528 TREEVIEW_ITEM *editItem = hItem;
3529 HINSTANCE hinst = GetWindowLongA(hwnd, GWL_HINSTANCE);
3532 TEXTMETRICA textMetric;
3534 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3535 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3538 if (infoPtr->hwndEdit)
3539 return infoPtr->hwndEdit;
3541 infoPtr->bLabelChanged = FALSE;
3543 /* Make sure that edit item is selected */
3544 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3545 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3547 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3550 /* Select the font to get appropriate metric dimensions */
3551 if (infoPtr->hFont != 0)
3553 hOldFont = SelectObject(hdc, infoPtr->hFont);
3556 /* Get string length in pixels */
3557 GetTextExtentPoint32A(hdc, editItem->pszText, strlen(editItem->pszText),
3560 /* Add Extra spacing for the next character */
3561 GetTextMetricsA(hdc, &textMetric);
3562 sz.cx += (textMetric.tmMaxCharWidth * 2);
3564 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3565 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3567 if (infoPtr->hFont != 0)
3569 SelectObject(hdc, hOldFont);
3572 ReleaseDC(hwnd, hdc);
3573 hwndEdit = CreateWindowExA(WS_EX_LEFT,
3576 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3577 WS_CLIPSIBLINGS | ES_WANTRETURN |
3578 ES_LEFT, editItem->textOffset - 2,
3579 editItem->rect.top - 1, sz.cx + 3,
3580 editItem->rect.bottom -
3581 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3582 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3584 infoPtr->hwndEdit = hwndEdit;
3586 /* Get a 2D border. */
3587 SetWindowLongA(hwndEdit, GWL_EXSTYLE,
3588 GetWindowLongA(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3589 SetWindowLongA(hwndEdit, GWL_STYLE,
3590 GetWindowLongA(hwndEdit, GWL_STYLE) | WS_BORDER);
3592 SendMessageA(hwndEdit, WM_SETFONT, TREEVIEW_FontForItem(infoPtr, editItem),
3595 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongA(hwndEdit, GWL_WNDPROC,
3597 TREEVIEW_Edit_SubclassProc);
3599 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3601 DestroyWindow(hwndEdit);
3602 infoPtr->hwndEdit = 0;
3606 infoPtr->selectedItem = hItem;
3607 SetWindowTextA(hwndEdit, editItem->pszText);
3609 SendMessageA(hwndEdit, EM_SETSEL, 0, -1);
3610 ShowWindow(hwndEdit, SW_SHOW);
3617 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3619 HWND hwnd = infoPtr->hwnd;
3620 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3623 char tmpText[1024] = { '\0' };
3626 if (!infoPtr->hwndEdit)
3629 tvdi.hdr.hwndFrom = hwnd;
3630 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
3631 tvdi.hdr.code = TVN_ENDLABELEDITA;
3633 tvdi.item.hItem = editedItem;
3634 tvdi.item.state = editedItem->state;
3635 tvdi.item.lParam = editedItem->lParam;
3639 iLength = GetWindowTextA(infoPtr->hwndEdit, tmpText, 1023);
3641 if (iLength >= 1023)
3643 ERR("Insuficient space to retrieve new item label\n");
3646 tvdi.item.pszText = tmpText;
3647 tvdi.item.cchTextMax = iLength + 1;
3651 tvdi.item.pszText = NULL;
3652 tvdi.item.cchTextMax = 0;
3655 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
3656 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3658 if (!bCancel && bCommit) /* Apply the changes */
3660 if (strcmp(tmpText, editedItem->pszText) != 0)
3662 if (NULL == COMCTL32_ReAlloc(editedItem->pszText, iLength + 1))
3664 ERR("OutOfMemory, cannot allocate space for label\n");
3665 DestroyWindow(infoPtr->hwndEdit);
3666 infoPtr->hwndEdit = 0;
3671 editedItem->cchTextMax = iLength + 1;
3672 lstrcpyA(editedItem->pszText, tmpText);
3677 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3678 DestroyWindow(infoPtr->hwndEdit);
3679 infoPtr->hwndEdit = 0;
3684 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3686 if (wParam != TV_EDIT_TIMER)
3688 ERR("got unknown timer\n");
3692 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3693 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3695 TREEVIEW_EditLabelA(infoPtr, infoPtr->selectedItem);
3701 /* Mouse Tracking/Drag **************************************************/
3703 /***************************************************************************
3704 * This is quite unusual piece of code, but that's how it's implemented in
3708 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3710 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3711 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3715 r.top = pt.y - cyDrag;
3716 r.left = pt.x - cxDrag;
3717 r.bottom = pt.y + cyDrag;
3718 r.right = pt.x + cxDrag;
3720 SetCapture(infoPtr->hwnd);
3724 if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3726 if (msg.message == WM_MOUSEMOVE)
3728 pt.x = SLOWORD(msg.lParam);
3729 pt.y = SHIWORD(msg.lParam);
3730 if (PtInRect(&r, pt))
3738 else if (msg.message >= WM_LBUTTONDOWN &&
3739 msg.message <= WM_RBUTTONDBLCLK)
3741 if (msg.message == WM_RBUTTONUP)
3742 TREEVIEW_RButtonUp(infoPtr, &pt);
3746 DispatchMessageA(&msg);
3749 if (GetCapture() != infoPtr->hwnd)
3759 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3761 TREEVIEW_ITEM *wineItem;
3765 SetFocus(infoPtr->hwnd);
3767 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3769 /* If there is pending 'edit label' event - kill it now */
3770 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3773 hit.pt.x = SLOWORD(lParam);
3774 hit.pt.y = SHIWORD(lParam);
3776 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3779 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3781 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3785 case TVHT_ONITEMRIGHT:
3786 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3789 case TVHT_ONITEMINDENT:
3790 if (!(infoPtr->dwStyle & TVS_HASLINES))
3796 int level = hit.pt.x / infoPtr->uIndent;
3797 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3799 while (wineItem->iLevel > level)
3801 wineItem = wineItem->parent;
3807 case TVHT_ONITEMLABEL:
3808 case TVHT_ONITEMICON:
3809 case TVHT_ONITEMBUTTON:
3810 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3813 case TVHT_ONITEMSTATEICON:
3814 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3815 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3817 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3826 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3828 HWND hwnd = infoPtr->hwnd;
3833 /* If Edit control is active - kill it and return.
3834 * The best way to do it is to set focus to itself.
3835 * Edit control subclassed procedure will automatically call
3838 if (infoPtr->hwndEdit)
3844 ht.pt.x = SLOWORD(lParam);
3845 ht.pt.y = SHIWORD(lParam);
3847 TREEVIEW_HitTest(infoPtr, &ht);
3848 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3850 /* update focusedItem and redraw both items */
3851 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3853 infoPtr->focusedItem = ht.hItem;
3854 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3856 if(infoPtr->selectedItem)
3857 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3860 bTrack = (ht.flags & TVHT_ONITEM)
3861 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3863 /* Send NM_CLICK right away */
3865 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3868 if (ht.flags & TVHT_ONITEMBUTTON)
3870 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3874 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
3875 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3877 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGA, ht.hItem,
3879 infoPtr->dropItem = ht.hItem;
3881 /* clean up focusedItem as we dragged and won't select this item */
3882 if(infoPtr->focusedItem)
3884 /* refresh the item that was focused */
3885 tempItem = infoPtr->focusedItem;
3886 infoPtr->focusedItem = 0;
3887 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
3889 /* refresh the selected item to return the filled background */
3890 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
3897 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3901 * If the style allows editing and the node is already selected
3902 * and the click occurred on the item label...
3904 if ((infoPtr->dwStyle & TVS_EDITLABELS) &&
3905 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem))
3907 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3908 KillTimer(hwnd, TV_EDIT_TIMER);
3910 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
3911 infoPtr->Timer |= TV_EDIT_TIMER_SET;
3913 else if (ht.flags & TVHT_ONITEM) /* select the item if the hit was inside of the icon or text */
3916 * if we are TVS_SINGLEEXPAND then we want this single click to
3917 * do a bunch of things.
3919 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
3920 (infoPtr->hwndEdit == 0))
3922 TREEVIEW_ITEM *SelItem;
3925 * Send the notification
3927 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVIF_HANDLE | TVIF_PARAM,
3931 * Close the previous selection all the way to the root
3932 * as long as the new selection is not a child
3934 if((infoPtr->selectedItem)
3935 && (infoPtr->selectedItem != ht.hItem))
3937 BOOL closeit = TRUE;
3940 /* determine if the hitItem is a child of the currently selected item */
3941 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3943 closeit = (SelItem != infoPtr->selectedItem);
3944 SelItem = SelItem->parent;
3949 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3950 SelItem = infoPtr->selectedItem;
3952 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3954 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3955 SelItem = SelItem->parent;
3961 * Expand the current item
3963 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
3966 /* Select the current item */
3967 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
3969 else if (ht.flags & TVHT_ONITEMSTATEICON)
3971 /* TVS_CHECKBOXES requires us to toggle the current state */
3972 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3973 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
3983 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3987 if (infoPtr->hwndEdit)
3989 SetFocus(infoPtr->hwnd);
3993 ht.pt.x = SLOWORD(lParam);
3994 ht.pt.y = SHIWORD(lParam);
3996 TREEVIEW_HitTest(infoPtr, &ht);
3998 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4002 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGA, ht.hItem,
4004 infoPtr->dropItem = ht.hItem;
4009 SetFocus(infoPtr->hwnd);
4010 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4017 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
4024 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4026 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4030 HBITMAP hbmp, hOldbmp;
4037 if (!(infoPtr->himlNormal))
4040 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4043 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4045 hwtop = GetDesktopWindow();
4046 htopdc = GetDC(hwtop);
4047 hdc = CreateCompatibleDC(htopdc);
4049 hOldFont = SelectObject(hdc, infoPtr->hFont);
4050 GetTextExtentPoint32A(hdc, dragItem->pszText, lstrlenA(dragItem->pszText),
4052 TRACE("%ld %ld %s %d\n", size.cx, size.cy, dragItem->pszText,
4053 lstrlenA(dragItem->pszText));
4054 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4055 hOldbmp = SelectObject(hdc, hbmp);
4057 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4062 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4063 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4067 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4068 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4071 /* draw item text */
4073 SetRect(&rc, cx, 0, size.cx, size.cy);
4074 DrawTextA(hdc, dragItem->pszText, lstrlenA(dragItem->pszText), &rc,
4076 SelectObject(hdc, hOldFont);
4077 SelectObject(hdc, hOldbmp);
4079 ImageList_Add(infoPtr->dragList, hbmp, 0);
4083 ReleaseDC(hwtop, htopdc);
4085 return (LRESULT)infoPtr->dragList;
4088 /* Selection ************************************************************/
4091 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4094 TREEVIEW_ITEM *prevSelect;
4097 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4099 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4100 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4101 newSelect ? newSelect->state : 0);
4103 /* reset and redraw focusedItem if focusedItem was set so we don't */
4104 /* have to worry about the previously focused item when we set a new one */
4105 if(infoPtr->focusedItem)
4107 rcFocused = (infoPtr->focusedItem)->rect;
4108 infoPtr->focusedItem = 0;
4109 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4115 prevSelect = infoPtr->selectedItem;
4117 if (prevSelect == newSelect)
4120 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4123 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4129 prevSelect->state &= ~TVIS_SELECTED;
4131 newSelect->state |= TVIS_SELECTED;
4133 infoPtr->selectedItem = newSelect;
4135 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4137 TREEVIEW_SendTreeviewNotify(infoPtr,
4140 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4143 TREEVIEW_Invalidate(infoPtr, prevSelect);
4144 TREEVIEW_Invalidate(infoPtr, newSelect);
4147 case TVGN_DROPHILITE:
4148 prevSelect = infoPtr->dropItem;
4151 prevSelect->state &= ~TVIS_DROPHILITED;
4153 infoPtr->dropItem = newSelect;
4156 newSelect->state |= TVIS_DROPHILITED;
4158 TREEVIEW_Invalidate(infoPtr, prevSelect);
4159 TREEVIEW_Invalidate(infoPtr, newSelect);
4162 case TVGN_FIRSTVISIBLE:
4163 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4164 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4165 TREEVIEW_Invalidate(infoPtr, NULL);
4169 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4173 /* FIXME: handle NM_KILLFOCUS etc */
4175 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4177 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4180 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4182 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4188 /*************************************************************************
4189 * TREEVIEW_ProcessLetterKeys
4191 * Processes keyboard messages generated by pressing the letter keys
4193 * What this does is perform a case insensitive search from the
4194 * current position with the following quirks:
4195 * - If two chars or more are pressed in quick succession we search
4196 * for the corresponding string (e.g. 'abc').
4197 * - If there is a delay we wipe away the current search string and
4198 * restart with just that char.
4199 * - If the user keeps pressing the same character, whether slowly or
4200 * fast, so that the search string is entirely composed of this
4201 * character ('aaaaa' for instance), then we search for first item
4202 * that starting with that character.
4203 * - If the user types the above character in quick succession, then
4204 * we must also search for the corresponding string ('aaaaa'), and
4205 * go to that string if there is a match.
4213 * - The current implementation has a list of characters it will
4214 * accept and it ignores averything else. In particular it will
4215 * ignore accentuated characters which seems to match what
4216 * Windows does. But I'm not sure it makes sense to follow
4218 * - We don't sound a beep when the search fails.
4219 * - The search should start from the focused item, not from the selected
4220 * item. One reason for this is to allow for multiple selections in trees.
4221 * But currently infoPtr->focusedItem does not seem very usable.
4225 * TREEVIEW_ProcessLetterKeys
4227 static INT TREEVIEW_ProcessLetterKeys(
4228 HWND hwnd, /* handle to the window */
4229 WPARAM charCode, /* the character code, the actual character */
4230 LPARAM keyData /* key data */
4233 TREEVIEW_INFO *infoPtr;
4235 HTREEITEM endidx,idx;
4237 CHAR buffer[MAX_PATH];
4238 DWORD timestamp,elapsed;
4240 /* simple parameter checking */
4241 if (!hwnd || !charCode || !keyData)
4244 infoPtr=(TREEVIEW_INFO*)GetWindowLongA(hwnd, 0);
4248 /* only allow the valid WM_CHARs through */
4249 if (!isalnum(charCode) &&
4250 charCode != '.' && charCode != '`' && charCode != '!' &&
4251 charCode != '@' && charCode != '#' && charCode != '$' &&
4252 charCode != '%' && charCode != '^' && charCode != '&' &&
4253 charCode != '*' && charCode != '(' && charCode != ')' &&
4254 charCode != '-' && charCode != '_' && charCode != '+' &&
4255 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4256 charCode != '}' && charCode != '[' && charCode != '{' &&
4257 charCode != '/' && charCode != '?' && charCode != '>' &&
4258 charCode != '<' && charCode != ',' && charCode != '~')
4261 /* compute how much time elapsed since last keypress */
4262 timestamp = GetTickCount();
4263 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4264 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4266 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4269 /* update the search parameters */
4270 infoPtr->lastKeyPressTimestamp=timestamp;
4271 if (elapsed < KEY_DELAY) {
4272 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam)) {
4273 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4275 if (infoPtr->charCode != charCode) {
4276 infoPtr->charCode=charCode=0;
4279 infoPtr->charCode=charCode;
4280 infoPtr->szSearchParam[0]=charCode;
4281 infoPtr->nSearchParamLength=1;
4282 /* Redundant with the 1 char string */
4286 /* and search from the current position */
4288 if (infoPtr->selectedItem != NULL) {
4289 endidx=infoPtr->selectedItem;
4290 /* if looking for single character match,
4291 * then we must always move forward
4293 if (infoPtr->nSearchParamLength == 1)
4294 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4299 idx=infoPtr->root->firstChild;
4305 idx=infoPtr->root->firstChild;
4309 ZeroMemory(&item, sizeof(item));
4310 item.mask = TVIF_TEXT;
4312 item.pszText = buffer;
4313 item.cchTextMax = sizeof(buffer);
4314 TREEVIEW_GetItemA( infoPtr, &item );
4316 /* check for a match */
4317 if (strncasecmp(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4320 } else if ( (charCode != 0) && (nItem == NULL) &&
4321 (nItem != infoPtr->selectedItem) &&
4322 (strncasecmp(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4323 /* This would work but we must keep looking for a longer match */
4326 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4327 } while (idx != endidx);
4329 if (nItem != NULL) {
4330 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4331 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4338 /* Scrolling ************************************************************/
4341 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4343 HTREEITEM newFirstVisible = NULL;
4346 if (!TREEVIEW_ValidItem(infoPtr, item))
4349 if (!ISVISIBLE(item))
4351 /* Expand parents as necessary. */
4354 /* see if we are trying to ensure that root is vislble */
4355 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4356 parent = item->parent;
4358 parent = item; /* this item is the topmost item */
4360 while (parent != infoPtr->root)
4362 if (!(parent->state & TVIS_EXPANDED))
4363 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4365 parent = parent->parent;
4369 TRACE("%p (%s) %ld - %ld\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4370 infoPtr->firstVisible->visibleOrder);
4372 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4374 if (visible_pos < 0)
4376 /* item is before the start of the list: put it at the top. */
4377 newFirstVisible = item;
4379 else if (visible_pos >= TREEVIEW_GetVisibleCount(infoPtr)
4380 /* Sometimes, before we are displayed, GVC is 0, causing us to
4381 * spuriously scroll up. */
4384 /* item is past the end of the list. */
4385 int scroll = visible_pos - TREEVIEW_GetVisibleCount(infoPtr);
4387 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4393 /* Scroll window so item's text is visible as much as possible */
4394 /* Calculation of amount of extra space is taken from EditLabel code */
4396 TEXTMETRICA textMetric;
4397 HDC hdc = GetWindowDC(infoPtr->hwnd);
4399 x = item->textWidth;
4401 GetTextMetricsA(hdc, &textMetric);
4402 ReleaseDC(infoPtr->hwnd, hdc);
4404 x += (textMetric.tmMaxCharWidth * 2);
4405 x = max(x, textMetric.tmMaxCharWidth * 3);
4407 if (item->textOffset < 0)
4408 pos = item->textOffset;
4409 else if (item->textOffset + x > infoPtr->clientWidth)
4411 if (x > infoPtr->clientWidth)
4412 pos = item->textOffset;
4414 pos = item->textOffset + x - infoPtr->clientWidth;
4419 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4422 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4424 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4433 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4434 TREEVIEW_ITEM *newFirstVisible,
4435 BOOL bUpdateScrollPos)
4439 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4441 if (newFirstVisible != NULL)
4443 /* Prevent an empty gap from appearing at the bottom... */
4444 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4445 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4449 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4452 /* ... unless we just don't have enough items. */
4453 if (newFirstVisible == NULL)
4454 newFirstVisible = infoPtr->root->firstChild;
4458 if (infoPtr->firstVisible != newFirstVisible)
4460 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4462 infoPtr->firstVisible = newFirstVisible;
4463 TREEVIEW_Invalidate(infoPtr, NULL);
4467 TREEVIEW_ITEM *item;
4468 int scroll = infoPtr->uItemHeight *
4469 (infoPtr->firstVisible->visibleOrder
4470 - newFirstVisible->visibleOrder);
4472 infoPtr->firstVisible = newFirstVisible;
4474 for (item = infoPtr->root->firstChild; item != NULL;
4475 item = TREEVIEW_GetNextListItem(infoPtr, item))
4477 item->rect.top += scroll;
4478 item->rect.bottom += scroll;
4481 if (bUpdateScrollPos)
4482 SetScrollPos(infoPtr->hwnd, SB_VERT,
4483 newFirstVisible->visibleOrder, TRUE);
4485 ScrollWindow(infoPtr->hwnd, 0, scroll, NULL, NULL);
4486 UpdateWindow(infoPtr->hwnd);
4491 /************************************************************************
4492 * VScroll is always in units of visible items. i.e. we always have a
4493 * visible item aligned to the top of the control. (Unless we have no
4497 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4499 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4500 TREEVIEW_ITEM *newFirstVisible = NULL;
4502 int nScrollCode = LOWORD(wParam);
4504 TRACE("wp %x\n", wParam);
4506 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4509 if (infoPtr->hwndEdit)
4510 SetFocus(infoPtr->hwnd);
4512 if (!oldFirstVisible)
4514 assert(infoPtr->root->firstChild == NULL);
4518 switch (nScrollCode)
4521 newFirstVisible = infoPtr->root->firstChild;
4525 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4529 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4533 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4537 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4538 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4542 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4543 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4547 case SB_THUMBPOSITION:
4548 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4549 infoPtr->root->firstChild,
4550 (LONG)(SHORT)HIWORD(wParam));
4557 if (newFirstVisible != NULL)
4559 if (newFirstVisible != oldFirstVisible)
4560 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4561 nScrollCode != SB_THUMBTRACK);
4562 else if (nScrollCode == SB_THUMBPOSITION)
4563 SetScrollPos(infoPtr->hwnd, SB_VERT,
4564 newFirstVisible->visibleOrder, TRUE);
4571 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4574 int scrollX = infoPtr->scrollX;
4575 int nScrollCode = LOWORD(wParam);
4577 TRACE("wp %x\n", wParam);
4579 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4582 if (infoPtr->hwndEdit)
4583 SetFocus(infoPtr->hwnd);
4585 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4586 /* shall never occur */
4593 switch (nScrollCode)
4596 scrollX -= infoPtr->uItemHeight;
4599 scrollX += infoPtr->uItemHeight;
4602 scrollX -= infoPtr->clientWidth;
4605 scrollX += infoPtr->clientWidth;
4609 case SB_THUMBPOSITION:
4610 scrollX = (int)(SHORT)HIWORD(wParam);
4617 if (scrollX > maxWidth)
4619 else if (scrollX < 0)
4623 if (scrollX != infoPtr->scrollX)
4625 TREEVIEW_ITEM *item;
4626 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4628 for (item = infoPtr->root->firstChild; item != NULL;
4629 item = TREEVIEW_GetNextListItem(infoPtr, item))
4631 item->linesOffset += scroll_pixels;
4632 item->stateOffset += scroll_pixels;
4633 item->imageOffset += scroll_pixels;
4634 item->textOffset += scroll_pixels;
4637 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4638 infoPtr->scrollX = scrollX;
4639 UpdateWindow(infoPtr->hwnd);
4642 if (nScrollCode != SB_THUMBTRACK)
4643 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4649 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4652 UINT pulScrollLines = 3;
4654 if (infoPtr->firstVisible == NULL)
4657 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4659 gcWheelDelta = -(short)HIWORD(wParam);
4660 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4662 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4664 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4665 int maxDy = infoPtr->maxVisibleOrder;
4673 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4678 /* Create/Destroy *******************************************************/
4681 TREEVIEW_Create(HWND hwnd)
4684 TREEVIEW_INFO *infoPtr;
4686 TRACE("wnd %x, style %lx\n", hwnd, GetWindowLongA(hwnd, GWL_STYLE));
4688 infoPtr = (TREEVIEW_INFO *)COMCTL32_Alloc(sizeof(TREEVIEW_INFO));
4690 if (infoPtr == NULL)
4692 ERR("could not allocate info memory!\n");
4696 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
4698 infoPtr->hwnd = hwnd;
4699 infoPtr->dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
4700 infoPtr->uInternalStatus = 0;
4702 infoPtr->uNumItems = 0;
4703 infoPtr->cdmode = 0;
4704 infoPtr->uScrollTime = 300; /* milliseconds */
4705 infoPtr->bRedraw = TRUE;
4707 GetClientRect(hwnd, &rcClient);
4709 /* No scroll bars yet. */
4710 infoPtr->clientWidth = rcClient.right;
4711 infoPtr->clientHeight = rcClient.bottom;
4713 infoPtr->treeWidth = 0;
4714 infoPtr->treeHeight = 0;
4716 infoPtr->uIndent = 19;
4717 infoPtr->selectedItem = 0;
4718 infoPtr->focusedItem = 0;
4720 infoPtr->firstVisible = 0;
4721 infoPtr->maxVisibleOrder = 0;
4722 infoPtr->dropItem = 0;
4723 infoPtr->insertMarkItem = 0;
4724 infoPtr->insertBeforeorAfter = 0;
4727 infoPtr->scrollX = 0;
4729 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4730 infoPtr->clrText = -1; /* use system color */
4731 infoPtr->clrLine = RGB(128, 128, 128);
4732 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4736 infoPtr->hwndEdit = 0;
4737 infoPtr->wpEditOrig = NULL;
4738 infoPtr->bIgnoreEditKillFocus = FALSE;
4739 infoPtr->bLabelChanged = FALSE;
4741 infoPtr->himlNormal = NULL;
4742 infoPtr->himlState = NULL;
4743 infoPtr->normalImageWidth = 0;
4744 infoPtr->normalImageHeight = 0;
4745 infoPtr->stateImageWidth = 0;
4746 infoPtr->stateImageHeight = 0;
4748 infoPtr->items = DPA_Create(16);
4750 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
4751 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4753 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4755 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4756 infoPtr->root->state = TVIS_EXPANDED;
4757 infoPtr->root->iLevel = -1;
4758 infoPtr->root->visibleOrder = -1;
4760 infoPtr->hwndNotify = GetParent(hwnd);
4762 infoPtr->bTransparent = ( GetWindowLongA( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4765 infoPtr->hwndToolTip = 0;
4767 infoPtr->bUnicode = IsWindowUnicode (hwnd);
4769 /* Determine what type of notify should be issued */
4770 /* sets infoPtr->bNtfUnicode */
4771 TREEVIEW_NotifyFormat(infoPtr, 0, NF_REQUERY);
4773 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4774 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4776 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4779 HBITMAP hbm, hbmOld;
4783 infoPtr->himlState =
4784 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4786 hdc = CreateCompatibleDC(0);
4787 hbm = CreateCompatibleBitmap(hdc, 48, 16);
4788 hbmOld = SelectObject(hdc, hbm);
4790 rc.left = 0; rc.top = 0;
4791 rc.right = 48; rc.bottom = 16;
4792 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4794 rc.left = 18; rc.top = 2;
4795 rc.right = 30; rc.bottom = 14;
4796 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4797 DFCS_BUTTONCHECK|DFCS_FLAT);
4799 rc.left = 34; rc.right = 46;
4800 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4801 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4803 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4804 GetSysColor(COLOR_WINDOW));
4805 TRACE("chckbox index %d\n", nIndex);
4806 SelectObject(hdc, hbmOld);
4810 infoPtr->stateImageWidth = 16;
4811 infoPtr->stateImageHeight = 16;
4818 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4822 TREEVIEW_RemoveTree(infoPtr);
4824 /* tool tip is automatically destroyed: we are its owner */
4826 /* Restore original wndproc */
4827 if (infoPtr->hwndEdit)
4828 SetWindowLongA(infoPtr->hwndEdit, GWL_WNDPROC,
4829 (LONG)infoPtr->wpEditOrig);
4831 /* Deassociate treeview from the window before doing anything drastic. */
4832 SetWindowLongA(infoPtr->hwnd, 0, (LONG)NULL);
4834 DeleteObject(infoPtr->hBoldFont);
4835 COMCTL32_Free(infoPtr);
4840 /* Miscellaneous Messages ***********************************************/
4843 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4851 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4852 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4853 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4854 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4855 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4856 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4857 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4858 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4859 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
4863 if (key >= VK_PRIOR && key <= VK_DOWN)
4865 unsigned char code = scroll[key - VK_PRIOR].code;
4867 (((code & (1 << 7)) == (SB_HORZ << 7))
4869 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
4875 /************************************************************************
4878 * VK_UP Move selection to the previous non-hidden item.
4879 * VK_DOWN Move selection to the next non-hidden item.
4880 * VK_HOME Move selection to the first item.
4881 * VK_END Move selection to the last item.
4882 * VK_LEFT If expanded then collapse, otherwise move to parent.
4883 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4885 * VK_SUBTRACT Collapse.
4886 * VK_MULTIPLY Expand all.
4887 * VK_PRIOR Move up GetVisibleCount items.
4888 * VK_NEXT Move down GetVisibleCount items.
4889 * VK_BACK Move to parent.
4890 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
4893 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4895 /* If it is non-NULL and different, it will be selected and visible. */
4896 TREEVIEW_ITEM *newSelection = NULL;
4898 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
4900 TRACE("%x\n", wParam);
4902 if (prevItem == NULL)
4905 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
4906 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
4911 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
4913 newSelection = infoPtr->root->firstChild;
4917 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
4921 newSelection = infoPtr->root->firstChild;
4925 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4929 if (prevItem->state & TVIS_EXPANDED)
4931 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4933 else if (prevItem->parent != infoPtr->root)
4935 newSelection = prevItem->parent;
4940 if (TREEVIEW_HasChildren(infoPtr, prevItem))
4942 if (!(prevItem->state & TVIS_EXPANDED))
4943 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4946 newSelection = prevItem->firstChild;
4953 TREEVIEW_ExpandAll(infoPtr, prevItem);
4957 if (!(prevItem->state & TVIS_EXPANDED))
4958 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4962 if (prevItem->state & TVIS_EXPANDED)
4963 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4968 = TREEVIEW_GetListItem(infoPtr, prevItem,
4969 -TREEVIEW_GetVisibleCount(infoPtr));
4974 = TREEVIEW_GetListItem(infoPtr, prevItem,
4975 TREEVIEW_GetVisibleCount(infoPtr));
4979 newSelection = prevItem->parent;
4980 if (newSelection == infoPtr->root)
4981 newSelection = NULL;
4985 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4986 TREEVIEW_ToggleItemState(infoPtr, prevItem);
4990 if (newSelection && newSelection != prevItem)
4992 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
4995 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5003 TREEVIEW_Notify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5005 LPNMHDR lpnmh = (LPNMHDR)lParam;
5007 if (lpnmh->code == PGN_CALCSIZE) {
5008 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5010 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5011 lppgc->iWidth = infoPtr->treeWidth;
5012 TRACE("got PGN_CALCSIZE, returning horz size = %ld, client=%ld\n",
5013 infoPtr->treeWidth, infoPtr->clientWidth);
5016 lppgc->iHeight = infoPtr->treeHeight;
5017 TRACE("got PGN_CALCSIZE, returning vert size = %ld, client=%ld\n",
5018 infoPtr->treeHeight, infoPtr->clientHeight);
5022 return DefWindowProcA(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5026 TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5030 if (lParam == NF_REQUERY) {
5031 i = SendMessageA(GetParent (infoPtr->hwnd),
5032 WM_NOTIFYFORMAT, infoPtr->hwnd, NF_QUERY);
5033 if ((i < NFR_ANSI) || (i > NFR_UNICODE)) {
5034 ERR("wrong response to WM_NOTIFYFORMAT (%d), assuming ANSI\n",
5038 infoPtr->bNtfUnicode = (i == NFR_UNICODE) ? 1 : 0;
5041 return (LRESULT)((infoPtr->bNtfUnicode) ? NFR_UNICODE : NFR_ANSI);
5046 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5048 if (wParam == SIZE_RESTORED)
5050 infoPtr->clientWidth = SLOWORD(lParam);
5051 infoPtr->clientHeight = SHIWORD(lParam);
5053 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5054 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5055 TREEVIEW_UpdateScrollBars(infoPtr);
5059 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
5062 TREEVIEW_Invalidate(infoPtr, NULL);
5067 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5069 TRACE("(%x %lx)\n", wParam, lParam);
5071 if (wParam == GWL_STYLE)
5073 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5075 /* we have to take special care about tooltips */
5076 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5078 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5080 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5085 DestroyWindow(infoPtr->hwndToolTip);
5086 infoPtr->hwndToolTip = 0;
5090 infoPtr->dwStyle = dwNewStyle;
5093 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5094 TREEVIEW_UpdateScrollBars(infoPtr);
5095 TREEVIEW_Invalidate(infoPtr, NULL);
5101 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5105 if (!infoPtr->selectedItem)
5107 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5111 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5112 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5117 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
5121 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5122 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5127 static LRESULT WINAPI
5128 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5130 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5131 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5134 if (uMsg == WM_CREATE)
5135 TREEVIEW_Create(hwnd);
5142 case TVM_CREATEDRAGIMAGE:
5143 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5145 case TVM_DELETEITEM:
5146 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5148 case TVM_EDITLABELA:
5149 return TREEVIEW_EditLabelA(infoPtr, (HTREEITEM)lParam);
5151 case TVM_EDITLABELW:
5152 FIXME("Unimplemented msg TVM_EDITLABELW\n");
5155 case TVM_ENDEDITLABELNOW:
5156 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5158 case TVM_ENSUREVISIBLE:
5159 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5162 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5164 case TVM_GETBKCOLOR:
5165 return TREEVIEW_GetBkColor(infoPtr);
5168 return TREEVIEW_GetCount(infoPtr);
5170 case TVM_GETEDITCONTROL:
5171 return TREEVIEW_GetEditControl(infoPtr);
5173 case TVM_GETIMAGELIST:
5174 return TREEVIEW_GetImageList(infoPtr, wParam);
5177 return TREEVIEW_GetIndent(infoPtr);
5179 case TVM_GETINSERTMARKCOLOR:
5180 return TREEVIEW_GetInsertMarkColor(infoPtr);
5182 case TVM_GETISEARCHSTRINGA:
5183 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5186 case TVM_GETISEARCHSTRINGW:
5187 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5191 return TREEVIEW_GetItemA(infoPtr, (LPTVITEMEXA)lParam);
5194 return TREEVIEW_GetItemW(infoPtr, (LPTVITEMEXW)lParam);
5196 case TVM_GETITEMHEIGHT:
5197 return TREEVIEW_GetItemHeight(infoPtr);
5199 case TVM_GETITEMRECT:
5200 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5202 case TVM_GETITEMSTATE:
5203 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5205 case TVM_GETLINECOLOR:
5206 return TREEVIEW_GetLineColor(infoPtr);
5208 case TVM_GETNEXTITEM:
5209 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5211 case TVM_GETSCROLLTIME:
5212 return TREEVIEW_GetScrollTime(infoPtr);
5214 case TVM_GETTEXTCOLOR:
5215 return TREEVIEW_GetTextColor(infoPtr);
5217 case TVM_GETTOOLTIPS:
5218 return TREEVIEW_GetToolTips(infoPtr);
5220 case TVM_GETUNICODEFORMAT:
5221 FIXME("Unimplemented msg TVM_GETUNICODEFORMAT\n");
5224 case TVM_GETVISIBLECOUNT:
5225 return TREEVIEW_GetVisibleCount(infoPtr);
5228 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5230 case TVM_INSERTITEMA:
5231 return TREEVIEW_InsertItemA(infoPtr, lParam);
5233 case TVM_INSERTITEMW:
5234 return TREEVIEW_InsertItemW(infoPtr, lParam);
5236 case TVM_SELECTITEM:
5237 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5239 case TVM_SETBKCOLOR:
5240 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5242 case TVM_SETIMAGELIST:
5243 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5246 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5248 case TVM_SETINSERTMARK:
5249 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5251 case TVM_SETINSERTMARKCOLOR:
5252 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5255 return TREEVIEW_SetItemA(infoPtr, (LPTVITEMEXA)lParam);
5258 return TREEVIEW_SetItemW(infoPtr, (LPTVITEMEXW)lParam);
5261 case TVM_SETLINECOLOR:
5262 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5264 case TVM_SETITEMHEIGHT:
5265 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5267 case TVM_SETSCROLLTIME:
5268 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5270 case TVM_SETTEXTCOLOR:
5271 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5273 case TVM_SETTOOLTIPS:
5274 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5276 case TVM_SETUNICODEFORMAT:
5277 FIXME("Unimplemented msg TVM_SETUNICODEFORMAT\n");
5280 case TVM_SORTCHILDREN:
5281 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5283 case TVM_SORTCHILDRENCB:
5284 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5287 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5290 return TREEVIEW_Command(infoPtr, wParam, lParam);
5293 return TREEVIEW_Destroy(infoPtr);
5298 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5301 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5304 return TREEVIEW_GetFont(infoPtr);
5307 return TREEVIEW_HScroll(infoPtr, wParam);
5310 return TREEVIEW_KeyDown(infoPtr, wParam);
5313 return TREEVIEW_KillFocus(infoPtr);
5315 case WM_LBUTTONDBLCLK:
5316 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5318 case WM_LBUTTONDOWN:
5319 return TREEVIEW_LButtonDown(infoPtr, lParam);
5321 /* WM_MBUTTONDOWN */
5326 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5328 case WM_NOTIFYFORMAT:
5329 return TREEVIEW_NotifyFormat(infoPtr, wParam, lParam);
5332 return TREEVIEW_Paint(infoPtr, wParam);
5334 /* WM_PRINTCLIENT */
5336 case WM_RBUTTONDOWN:
5337 return TREEVIEW_RButtonDown(infoPtr, lParam);
5340 return TREEVIEW_SetFocus(infoPtr);
5343 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5346 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5349 return TREEVIEW_Size(infoPtr, wParam, lParam);
5351 case WM_STYLECHANGED:
5352 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5354 /* WM_SYSCOLORCHANGE */
5359 return TREEVIEW_HandleTimer(infoPtr, wParam);
5362 return TREEVIEW_VScroll(infoPtr, wParam);
5364 /* WM_WININICHANGE */
5367 if (wParam & (MK_SHIFT | MK_CONTROL))
5369 return TREEVIEW_MouseWheel(infoPtr, wParam);
5372 TRACE("drawItem\n");
5376 /* This mostly catches MFC and Delphi messages. :( */
5377 if (uMsg >= WM_USER)
5378 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
5380 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
5386 /* Class Registration ***************************************************/
5389 TREEVIEW_Register(void)
5395 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
5396 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5397 wndClass.lpfnWndProc = (WNDPROC)TREEVIEW_WindowProc;
5398 wndClass.cbClsExtra = 0;
5399 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5401 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
5402 wndClass.hbrBackground = 0;
5403 wndClass.lpszClassName = WC_TREEVIEWA;
5405 RegisterClassA(&wndClass);
5410 TREEVIEW_Unregister(void)
5412 UnregisterClassA(WC_TREEVIEWA, (HINSTANCE) NULL);
5416 /* Tree Verification ****************************************************/
5420 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5422 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5423 TREEVIEW_ITEM *item)
5425 assert(infoPtr != NULL);
5426 assert(item != NULL);
5428 /* both NULL, or both non-null */
5429 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5431 assert(item->firstChild != item);
5432 assert(item->lastChild != item);
5434 if (item->firstChild)
5436 assert(item->firstChild->parent == item);
5437 assert(item->firstChild->prevSibling == NULL);
5440 if (item->lastChild)
5442 assert(item->lastChild->parent == item);
5443 assert(item->lastChild->nextSibling == NULL);
5446 assert(item->nextSibling != item);
5447 if (item->nextSibling)
5449 assert(item->nextSibling->parent == item->parent);
5450 assert(item->nextSibling->prevSibling == item);
5453 assert(item->prevSibling != item);
5454 if (item->prevSibling)
5456 assert(item->prevSibling->parent == item->parent);
5457 assert(item->prevSibling->nextSibling == item);
5462 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5464 assert(item != NULL);
5466 assert(item->parent != NULL);
5467 assert(item->parent != item);
5468 assert(item->iLevel == item->parent->iLevel + 1);
5470 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5472 TREEVIEW_VerifyItemCommon(infoPtr, item);
5474 TREEVIEW_VerifyChildren(infoPtr, item);
5478 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5480 TREEVIEW_ITEM *child;
5481 assert(item != NULL);
5483 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5484 TREEVIEW_VerifyItem(infoPtr, child);
5488 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5490 TREEVIEW_ITEM *root = infoPtr->root;
5492 assert(root != NULL);
5493 assert(root->iLevel == -1);
5494 assert(root->parent == NULL);
5495 assert(root->prevSibling == NULL);
5497 TREEVIEW_VerifyItemCommon(infoPtr, root);
5499 TREEVIEW_VerifyChildren(infoPtr, root);
5503 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5505 assert(infoPtr != NULL);
5507 TREEVIEW_VerifyRoot(infoPtr);