3 * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4 * Copyright 1998,1999 Alex Priem <alexp@sci.kun.nl>
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
24 * Note2: All items always! have valid (allocated) pszText field.
25 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
26 * of size TEXT_CALLBACK_SIZE in DoSetItem.
27 * We use callbackMask to keep track of fields to be updated.
30 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
31 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
33 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
38 * Make the insertion mark look right.
39 * Scroll (instead of repaint) as much as possible.
43 #include "wine/port.h"
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
61 #include "wine/unicode.h"
62 #include "wine/debug.h"
64 /* internal structures */
66 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
77 int iIntegral; /* item height multiplier (1 is normal) */
78 int iLevel; /* indentation level:0=root level */
79 HTREEITEM parent; /* handle to parent or 0 if at root */
80 HTREEITEM firstChild; /* handle to first child or 0 if no child */
82 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
83 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
89 LONG textWidth; /* horizontal text extent for pszText */
90 LONG visibleOrder; /* visible ordering, 0 is first visible item */
94 typedef struct tagTREEVIEW_INFO
97 HWND hwndNotify; /* Owner window to send notifications to */
100 UINT uInternalStatus;
102 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
103 INT cdmode; /* last custom draw setting */
104 UINT uScrollTime; /* max. time for scrolling in milliseconds */
105 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
107 UINT uItemHeight; /* item height */
110 LONG clientWidth; /* width of control window */
111 LONG clientHeight; /* height of control window */
113 LONG treeWidth; /* width of visible tree items */
114 LONG treeHeight; /* height of visible tree items */
116 UINT uIndent; /* indentation in pixels */
117 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
118 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
119 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
121 HTREEITEM firstVisible; /* handle to first visible item */
122 LONG maxVisibleOrder;
123 HTREEITEM dropItem; /* handle to item selected by drag cursor */
124 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
125 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
126 HIMAGELIST dragList; /* Bitmap of dragged item */
131 COLORREF clrInsertMark;
137 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
138 BOOL bIgnoreEditKillFocus;
141 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
142 HIMAGELIST himlNormal;
143 int normalImageHeight;
144 int normalImageWidth;
145 HIMAGELIST himlState;
146 int stateImageHeight;
150 DWORD lastKeyPressTimestamp; /* Added */
151 WPARAM charCode; /* Added */
152 INT nSearchParamLength; /* Added */
153 WCHAR szSearchParam[ MAX_PATH ]; /* Added */
157 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
158 #define KEY_DELAY 450
160 /* bitflags for infoPtr->uInternalStatus */
162 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
163 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
164 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
165 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
166 #define TV_RDRAG 0x10 /* dito Rbutton */
167 #define TV_RDRAGGING 0x20
169 /* bitflags for infoPtr->timer */
171 #define TV_EDIT_TIMER 2
172 #define TV_EDIT_TIMER_SET 2
175 VOID TREEVIEW_Register (VOID);
176 VOID TREEVIEW_Unregister (VOID);
179 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
182 #define TEXT_CALLBACK_SIZE 260
184 #define TREEVIEW_LEFT_MARGIN 8
186 #define MINIMUM_INDENT 19
188 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
190 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
191 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
192 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
195 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
198 static VOID TREEVIEW_Invalidate(TREEVIEW_INFO *, TREEVIEW_ITEM *);
200 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
201 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
202 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
203 static LRESULT TREEVIEW_RButtonUp(TREEVIEW_INFO *, LPPOINT);
204 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
205 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
206 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
207 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND wParam, UINT lParam);
210 /* Random Utilities *****************************************************/
214 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
219 /* The definition is at the end of the file. */
220 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
223 /* Returns the treeview private data if hwnd is a treeview.
224 * Otherwise returns an undefined value. */
225 static TREEVIEW_INFO *
226 TREEVIEW_GetInfoPtr(HWND hwnd)
228 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
231 /* Don't call this. Nothing wants an item index. */
233 TREEVIEW_GetItemIndex(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
235 assert(infoPtr != NULL);
237 return DPA_GetPtrIndex(infoPtr->items, handle);
240 /***************************************************************************
241 * This method checks that handle is an item for this tree.
244 TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
246 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
248 TRACE("invalid item %p\n", handle);
256 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
260 GetObjectA(hOrigFont, sizeof(font), &font);
261 font.lfWeight = FW_BOLD;
262 return CreateFontIndirectA(&font);
266 TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
268 return (item->state & TVIS_BOLD) ? infoPtr->hBoldFont : infoPtr->hFont;
271 /* for trace/debugging purposes only */
273 TREEVIEW_ItemName(TREEVIEW_ITEM *item)
275 if (item == NULL) return "<null item>";
276 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
277 if (item->pszText == NULL) return "<null>";
278 return debugstr_w(item->pszText);
281 /* An item is not a child of itself. */
283 TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
287 child = child->parent;
288 if (child == parent) return TRUE;
289 } while (child != NULL);
295 /* Tree Traversal *******************************************************/
297 /***************************************************************************
298 * This method returns the last expanded sibling or child child item
301 static TREEVIEW_ITEM *
302 TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
307 while (wineItem->lastChild)
309 if (wineItem->state & TVIS_EXPANDED)
310 wineItem = wineItem->lastChild;
315 if (wineItem == infoPtr->root)
321 /***************************************************************************
322 * This method returns the previous non-hidden item in the list not
323 * considering the tree hierarchy.
325 static TREEVIEW_ITEM *
326 TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
328 if (tvItem->prevSibling)
330 /* This item has a prevSibling, get the last item in the sibling's tree. */
331 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
333 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
334 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
340 /* this item does not have a prevSibling, get the parent */
341 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
346 /***************************************************************************
347 * This method returns the next physical item in the treeview not
348 * considering the tree hierarchy.
350 static TREEVIEW_ITEM *
351 TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
353 assert(tvItem != NULL);
356 * If this item has children and is expanded, return the first child
358 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
360 return tvItem->firstChild;
365 * try to get the sibling
367 if (tvItem->nextSibling)
368 return tvItem->nextSibling;
371 * Otherwise, get the parent's sibling.
373 while (tvItem->parent)
375 tvItem = tvItem->parent;
377 if (tvItem->nextSibling)
378 return tvItem->nextSibling;
384 /***************************************************************************
385 * This method returns the nth item starting at the given item. It returns
386 * the last item (or first) we we run out of items.
388 * Will scroll backward if count is <0.
389 * forward if count is >0.
391 static TREEVIEW_ITEM *
392 TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
395 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
396 TREEVIEW_ITEM *previousItem;
398 assert(wineItem != NULL);
402 next_item = TREEVIEW_GetNextListItem;
407 next_item = TREEVIEW_GetPrevListItem;
414 previousItem = wineItem;
415 wineItem = next_item(infoPtr, wineItem);
417 } while (--count && wineItem != NULL);
420 return wineItem ? wineItem : previousItem;
423 /* Notifications ************************************************************/
425 static INT get_notifycode(TREEVIEW_INFO *infoPtr, INT code)
427 if (!infoPtr->bNtfUnicode) {
429 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
430 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
431 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
432 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
433 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
434 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
435 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
436 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
437 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
438 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
439 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
440 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
447 TREEVIEW_SendRealNotify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
449 TRACE("wParam=%d, lParam=%ld\n", wParam, lParam);
450 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
454 TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
457 HWND hwnd = infoPtr->hwnd;
460 nmhdr.hwndFrom = hwnd;
461 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
462 nmhdr.code = get_notifycode(infoPtr, code);
464 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
465 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
469 TREEVIEW_TVItemFromItem(TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
472 tvItem->hItem = item;
473 tvItem->state = item->state;
474 tvItem->stateMask = 0;
475 tvItem->iImage = item->iImage;
476 tvItem->iImage = item->iImage;
477 tvItem->iSelectedImage = item->iSelectedImage;
478 tvItem->cChildren = item->cChildren;
479 tvItem->lParam = item->lParam;
483 if (!infoPtr->bNtfUnicode)
485 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
486 tvItem->pszText = Alloc (tvItem->cchTextMax);
487 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
491 tvItem->cchTextMax = item->cchTextMax;
492 tvItem->pszText = item->pszText;
497 tvItem->cchTextMax = 0;
498 tvItem->pszText = NULL;
503 TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
504 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
506 HWND hwnd = infoPtr->hwnd;
510 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
511 code, action, oldItem, newItem);
513 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWA));
515 nmhdr.hdr.hwndFrom = hwnd;
516 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
517 nmhdr.hdr.code = get_notifycode(infoPtr, code);
518 nmhdr.action = action;
521 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
524 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
529 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
530 (WPARAM)nmhdr.hdr.idFrom,
532 if (!infoPtr->bNtfUnicode)
534 Free(nmhdr.itemOld.pszText);
535 Free(nmhdr.itemNew.pszText);
541 TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
542 HTREEITEM dragItem, POINT pt)
544 HWND hwnd = infoPtr->hwnd;
547 TRACE("code:%d dragitem:%p\n", code, dragItem);
549 nmhdr.hdr.hwndFrom = hwnd;
550 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
551 nmhdr.hdr.code = get_notifycode(infoPtr, code);
553 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
554 nmhdr.itemNew.hItem = dragItem;
555 nmhdr.itemNew.state = dragItem->state;
556 nmhdr.itemNew.lParam = dragItem->lParam;
558 nmhdr.ptDrag.x = pt.x;
559 nmhdr.ptDrag.y = pt.y;
561 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
562 (WPARAM)nmhdr.hdr.idFrom,
568 TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
571 HWND hwnd = infoPtr->hwnd;
572 NMTVCUSTOMDRAW nmcdhdr;
575 TRACE("drawstage:%lx hdc:%p\n", dwDrawStage, hdc);
577 nmcd = &nmcdhdr.nmcd;
578 nmcd->hdr.hwndFrom = hwnd;
579 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
580 nmcd->hdr.code = NM_CUSTOMDRAW;
581 nmcd->dwDrawStage = dwDrawStage;
584 nmcd->dwItemSpec = 0;
585 nmcd->uItemState = 0;
586 nmcd->lItemlParam = 0;
587 nmcdhdr.clrText = infoPtr->clrText;
588 nmcdhdr.clrTextBk = infoPtr->clrBk;
591 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
592 (WPARAM)nmcd->hdr.idFrom,
598 /* FIXME: need to find out when the flags in uItemState need to be set */
601 TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
602 TREEVIEW_ITEM *wineItem, UINT uItemDrawState)
604 HWND hwnd = infoPtr->hwnd;
605 NMTVCUSTOMDRAW nmcdhdr;
607 DWORD dwDrawStage, dwItemSpec;
611 dwDrawStage = CDDS_ITEM | uItemDrawState;
612 dwItemSpec = (DWORD)wineItem;
614 if (wineItem->state & TVIS_SELECTED)
615 uItemState |= CDIS_SELECTED;
616 if (wineItem == infoPtr->selectedItem)
617 uItemState |= CDIS_FOCUS;
618 if (wineItem == infoPtr->hotItem)
619 uItemState |= CDIS_HOT;
621 nmcd = &nmcdhdr.nmcd;
622 nmcd->hdr.hwndFrom = hwnd;
623 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
624 nmcd->hdr.code = NM_CUSTOMDRAW;
625 nmcd->dwDrawStage = dwDrawStage;
627 nmcd->rc = wineItem->rect;
628 nmcd->dwItemSpec = dwItemSpec;
629 nmcd->uItemState = uItemState;
630 nmcd->lItemlParam = wineItem->lParam;
631 nmcdhdr.clrText = infoPtr->clrText;
632 nmcdhdr.clrTextBk = infoPtr->clrBk;
633 nmcdhdr.iLevel = wineItem->iLevel;
635 TRACE("drawstage:%lx hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
636 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
637 nmcd->uItemState, nmcd->lItemlParam);
639 retval = TREEVIEW_SendRealNotify(infoPtr,
640 (WPARAM)nmcd->hdr.idFrom,
643 infoPtr->clrText = nmcdhdr.clrText;
644 infoPtr->clrBk = nmcdhdr.clrTextBk;
649 TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
651 HWND hwnd = infoPtr->hwnd;
655 tvdi.hdr.hwndFrom = hwnd;
656 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
657 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
659 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
660 &tvdi.item, editItem);
662 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
664 if (!infoPtr->bNtfUnicode)
665 Free(tvdi.item.pszText);
671 TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
674 NMTVDISPINFOW callback;
675 HWND hwnd = infoPtr->hwnd;
677 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
678 mask &= wineItem->callbackMask;
680 if (mask == 0) return;
682 callback.hdr.hwndFrom = hwnd;
683 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
684 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
686 /* 'state' always contains valid value, as well as 'lParam'.
687 * All other parameters are uninitialized.
689 callback.item.pszText = wineItem->pszText;
690 callback.item.cchTextMax = wineItem->cchTextMax;
691 callback.item.mask = mask;
692 callback.item.hItem = wineItem;
693 callback.item.state = wineItem->state;
694 callback.item.lParam = wineItem->lParam;
696 /* If text is changed we need to recalculate textWidth */
697 if (mask & TVIF_TEXT)
698 wineItem->textWidth = 0;
700 TREEVIEW_SendRealNotify(infoPtr,
701 (WPARAM)callback.hdr.idFrom, (LPARAM)&callback);
703 /* It may have changed due to a call to SetItem. */
704 mask &= wineItem->callbackMask;
706 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
708 /* Instead of copying text into our buffer user specified its own */
709 if (!infoPtr->bNtfUnicode) {
712 int len = MultiByteToWideChar( CP_ACP, 0,
713 (LPSTR)callback.item.pszText, -1,
715 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
716 newText = (LPWSTR)ReAlloc(wineItem->pszText, buflen);
718 TRACE("returned str %s, len=%d, buflen=%d\n",
719 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
723 wineItem->pszText = newText;
724 MultiByteToWideChar( CP_ACP, 0,
725 (LPSTR)callback.item.pszText, -1,
726 wineItem->pszText, buflen);
727 wineItem->cchTextMax = buflen;
729 /* If ReAlloc fails we have nothing to do, but keep original text */
732 int len = max(lstrlenW(callback.item.pszText) + 1,
734 LPWSTR newText = ReAlloc(wineItem->pszText, len);
736 TRACE("returned wstr %s, len=%d\n",
737 debugstr_w(callback.item.pszText), len);
741 wineItem->pszText = newText;
742 strcpyW(wineItem->pszText, callback.item.pszText);
743 wineItem->cchTextMax = len;
745 /* If ReAlloc fails we have nothing to do, but keep original text */
748 else if (mask & TVIF_TEXT) {
749 /* User put text into our buffer, that is ok unless A string */
750 if (!infoPtr->bNtfUnicode) {
752 LPWSTR oldText = NULL;
754 int len = MultiByteToWideChar( CP_ACP, 0,
755 (LPSTR)callback.item.pszText, -1,
757 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
758 newText = (LPWSTR)Alloc(buflen);
760 TRACE("same buffer str %s, len=%d, buflen=%d\n",
761 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
765 oldText = wineItem->pszText;
766 wineItem->pszText = newText;
767 MultiByteToWideChar( CP_ACP, 0,
768 (LPSTR)callback.item.pszText, -1,
769 wineItem->pszText, buflen);
770 wineItem->cchTextMax = buflen;
777 if (mask & TVIF_IMAGE)
778 wineItem->iImage = callback.item.iImage;
780 if (mask & TVIF_SELECTEDIMAGE)
781 wineItem->iSelectedImage = callback.item.iSelectedImage;
783 if (mask & TVIF_CHILDREN)
784 wineItem->cChildren = callback.item.cChildren;
786 /* These members are now permanently set. */
787 if (callback.item.mask & TVIF_DI_SETITEM)
788 wineItem->callbackMask &= ~callback.item.mask;
791 /***************************************************************************
792 * This function uses cChildren field to decide whether the item has
794 * Note: if this returns TRUE, the child items may not actually exist,
795 * they could be virtual.
797 * Just use wineItem->firstChild to check for physical children.
800 TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
802 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
804 return wineItem->cChildren > 0;
808 /* Item Position ********************************************************/
810 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
812 TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
815 /* Same effect, different optimisation. */
817 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
818 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
820 BOOL lar = ((infoPtr->dwStyle
821 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
825 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
827 item->stateOffset = item->linesOffset + infoPtr->uIndent;
828 item->imageOffset = item->stateOffset
829 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
830 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
834 TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
840 /* DRAW's OM docker creates items like this */
841 if (item->pszText == NULL)
853 hdc = GetDC(infoPtr->hwnd);
854 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
857 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
858 item->textWidth = sz.cx;
862 SelectObject(hdc, hOldFont);
868 TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
870 item->rect.top = infoPtr->uItemHeight *
871 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
873 item->rect.bottom = item->rect.top
874 + infoPtr->uItemHeight * item->iIntegral - 1;
877 item->rect.right = infoPtr->clientWidth;
880 /* We know that only items after start need their order updated. */
882 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
889 start = infoPtr->root->firstChild;
893 order = start->visibleOrder;
895 for (item = start; item != NULL;
896 item = TREEVIEW_GetNextListItem(infoPtr, item))
898 item->visibleOrder = order;
899 order += item->iIntegral;
902 infoPtr->maxVisibleOrder = order;
904 for (item = start; item != NULL;
905 item = TREEVIEW_GetNextListItem(infoPtr, item))
907 TREEVIEW_ComputeItemRect(infoPtr, item);
912 /* Update metrics of all items in selected subtree.
913 * root must be expanded
916 TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
918 TREEVIEW_ITEM *sibling;
922 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
925 root->state &= ~TVIS_EXPANDED;
926 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
927 root->state |= TVIS_EXPANDED;
929 hdc = GetDC(infoPtr->hwnd);
930 hOldFont = SelectObject(hdc, infoPtr->hFont);
932 for (; root != sibling;
933 root = TREEVIEW_GetNextListItem(infoPtr, root))
935 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
937 if (root->callbackMask & TVIF_TEXT)
938 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
940 if (root->textWidth == 0)
942 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
943 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
947 SelectObject(hdc, hOldFont);
948 ReleaseDC(infoPtr->hwnd, hdc);
951 /* Item Allocation **********************************************************/
953 static TREEVIEW_ITEM *
954 TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
956 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
961 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
970 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
971 * free item->pszText. */
973 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
975 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
977 if (infoPtr->selectedItem == item)
978 infoPtr->selectedItem = NULL;
979 if (infoPtr->hotItem == item)
980 infoPtr->hotItem = NULL;
981 if (infoPtr->focusedItem == item)
982 infoPtr->focusedItem = NULL;
983 if (infoPtr->firstVisible == item)
984 infoPtr->firstVisible = NULL;
985 if (infoPtr->dropItem == item)
986 infoPtr->dropItem = NULL;
987 if (infoPtr->insertMarkItem == item)
988 infoPtr->insertMarkItem = NULL;
992 /* Item Insertion *******************************************************/
994 /***************************************************************************
995 * This method inserts newItem before sibling as a child of parent.
996 * sibling can be NULL, but only if parent has no children.
999 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1000 TREEVIEW_ITEM *parent)
1002 assert(newItem != NULL);
1003 assert(parent != NULL);
1005 if (sibling != NULL)
1007 assert(sibling->parent == parent);
1009 if (sibling->prevSibling != NULL)
1010 sibling->prevSibling->nextSibling = newItem;
1012 newItem->prevSibling = sibling->prevSibling;
1013 sibling->prevSibling = newItem;
1016 newItem->prevSibling = NULL;
1018 newItem->nextSibling = sibling;
1020 if (parent->firstChild == sibling)
1021 parent->firstChild = newItem;
1023 if (parent->lastChild == NULL)
1024 parent->lastChild = newItem;
1027 /***************************************************************************
1028 * This method inserts newItem after sibling as a child of parent.
1029 * sibling can be NULL, but only if parent has no children.
1032 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1033 TREEVIEW_ITEM *parent)
1035 assert(newItem != NULL);
1036 assert(parent != NULL);
1038 if (sibling != NULL)
1040 assert(sibling->parent == parent);
1042 if (sibling->nextSibling != NULL)
1043 sibling->nextSibling->prevSibling = newItem;
1045 newItem->nextSibling = sibling->nextSibling;
1046 sibling->nextSibling = newItem;
1049 newItem->nextSibling = NULL;
1051 newItem->prevSibling = sibling;
1053 if (parent->lastChild == sibling)
1054 parent->lastChild = newItem;
1056 if (parent->firstChild == NULL)
1057 parent->firstChild = newItem;
1061 TREEVIEW_DoSetItemT(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1062 const TVITEMEXW *tvItem, BOOL isW)
1064 UINT callbackClear = 0;
1065 UINT callbackSet = 0;
1067 TRACE("item %p\n", wineItem);
1068 /* Do this first in case it fails. */
1069 if (tvItem->mask & TVIF_TEXT)
1071 wineItem->textWidth = 0; /* force width recalculation */
1072 if (tvItem->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
1077 len = lstrlenW(tvItem->pszText) + 1;
1079 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1081 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1083 if (newText == NULL) return FALSE;
1085 callbackClear |= TVIF_TEXT;
1087 wineItem->pszText = newText;
1088 wineItem->cchTextMax = len;
1090 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1092 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1093 wineItem->pszText, len);
1095 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1099 callbackSet |= TVIF_TEXT;
1101 wineItem->pszText = ReAlloc(wineItem->pszText,
1102 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1103 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1104 TRACE("setting callback, item %p\n", wineItem);
1108 if (tvItem->mask & TVIF_CHILDREN)
1110 wineItem->cChildren = tvItem->cChildren;
1112 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1113 callbackSet |= TVIF_CHILDREN;
1115 callbackClear |= TVIF_CHILDREN;
1118 if (tvItem->mask & TVIF_IMAGE)
1120 wineItem->iImage = tvItem->iImage;
1122 if (wineItem->iImage == I_IMAGECALLBACK)
1123 callbackSet |= TVIF_IMAGE;
1125 callbackClear |= TVIF_IMAGE;
1128 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1130 wineItem->iSelectedImage = tvItem->iSelectedImage;
1132 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1133 callbackSet |= TVIF_SELECTEDIMAGE;
1135 callbackClear |= TVIF_SELECTEDIMAGE;
1138 if (tvItem->mask & TVIF_PARAM)
1139 wineItem->lParam = tvItem->lParam;
1141 /* If the application sets TVIF_INTEGRAL without
1142 * supplying a TVITEMEX structure, it's toast. */
1143 if (tvItem->mask & TVIF_INTEGRAL)
1144 wineItem->iIntegral = tvItem->iIntegral;
1146 if (tvItem->mask & TVIF_STATE)
1148 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1150 wineItem->state &= ~tvItem->stateMask;
1151 wineItem->state |= (tvItem->state & tvItem->stateMask);
1154 wineItem->callbackMask |= callbackSet;
1155 wineItem->callbackMask &= ~callbackClear;
1160 /* Note that the new item is pre-zeroed. */
1162 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1164 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1165 HTREEITEM insertAfter;
1166 TREEVIEW_ITEM *newItem, *parentItem;
1167 BOOL bTextUpdated = FALSE;
1169 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1171 parentItem = infoPtr->root;
1175 parentItem = ptdi->hParent;
1177 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1179 WARN("invalid parent %p\n", parentItem);
1180 return (LRESULT)(HTREEITEM)NULL;
1184 insertAfter = ptdi->hInsertAfter;
1186 /* Validate this now for convenience. */
1187 switch ((DWORD)insertAfter)
1189 case (DWORD)TVI_FIRST:
1190 case (DWORD)TVI_LAST:
1191 case (DWORD)TVI_SORT:
1195 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1196 insertAfter->parent != parentItem)
1198 WARN("invalid insert after %p\n", insertAfter);
1199 insertAfter = TVI_LAST;
1203 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1204 (tvItem->mask & TVIF_TEXT)
1205 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1206 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1209 newItem = TREEVIEW_AllocateItem(infoPtr);
1210 if (newItem == NULL)
1211 return (LRESULT)(HTREEITEM)NULL;
1213 newItem->parent = parentItem;
1214 newItem->iIntegral = 1;
1216 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1217 return (LRESULT)(HTREEITEM)NULL;
1219 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1221 infoPtr->uNumItems++;
1223 switch ((DWORD)insertAfter)
1225 case (DWORD)TVI_FIRST:
1227 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1228 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1229 if (infoPtr->firstVisible == originalFirst)
1230 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1234 case (DWORD)TVI_LAST:
1235 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1238 /* hInsertAfter names a specific item we want to insert after */
1240 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1243 case (DWORD)TVI_SORT:
1245 TREEVIEW_ITEM *aChild;
1246 TREEVIEW_ITEM *previousChild = NULL;
1247 BOOL bItemInserted = FALSE;
1249 aChild = parentItem->firstChild;
1251 bTextUpdated = TRUE;
1252 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1254 /* Iterate the parent children to see where we fit in */
1255 while (aChild != NULL)
1259 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1260 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1262 if (comp < 0) /* we are smaller than the current one */
1264 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1265 bItemInserted = TRUE;
1268 else if (comp > 0) /* we are bigger than the current one */
1270 previousChild = aChild;
1272 /* This will help us to exit if there is no more sibling */
1273 aChild = (aChild->nextSibling == 0)
1275 : aChild->nextSibling;
1277 /* Look at the next item */
1283 * An item with this name is already existing, therefore,
1284 * we add after the one we found
1286 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1287 bItemInserted = TRUE;
1293 * we reach the end of the child list and the item has not
1294 * yet been inserted, therefore, insert it after the last child.
1296 if ((!bItemInserted) && (aChild == NULL))
1297 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1304 TRACE("new item %p; parent %p, mask %x\n", newItem,
1305 newItem->parent, tvItem->mask);
1307 newItem->iLevel = newItem->parent->iLevel + 1;
1309 if (newItem->parent->cChildren == 0)
1310 newItem->parent->cChildren = 1;
1312 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1314 if (STATEIMAGEINDEX(newItem->state) == 0)
1315 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1318 if (infoPtr->firstVisible == NULL)
1319 infoPtr->firstVisible = newItem;
1321 TREEVIEW_VerifyTree(infoPtr);
1323 if (parentItem == infoPtr->root ||
1324 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1326 TREEVIEW_ITEM *item;
1327 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1329 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1330 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1333 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1335 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1336 TREEVIEW_UpdateScrollBars(infoPtr);
1338 * if the item was inserted in a visible part of the tree,
1339 * invalidate it, as well as those after it
1341 for (item = newItem;
1343 item = TREEVIEW_GetNextListItem(infoPtr, item))
1344 TREEVIEW_Invalidate(infoPtr, item);
1348 newItem->visibleOrder = -1;
1350 /* refresh treeview if newItem is the first item inserted under parentItem */
1351 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1353 /* parent got '+' - update it */
1354 TREEVIEW_Invalidate(infoPtr, parentItem);
1358 return (LRESULT)newItem;
1361 /* Item Deletion ************************************************************/
1363 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1366 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1368 TREEVIEW_ITEM *kill = parentItem->firstChild;
1370 while (kill != NULL)
1372 TREEVIEW_ITEM *next = kill->nextSibling;
1374 TREEVIEW_RemoveItem(infoPtr, kill);
1379 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1380 assert(parentItem->firstChild == NULL);
1381 assert(parentItem->lastChild == NULL);
1385 TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1387 TREEVIEW_ITEM *parentItem = item->parent;
1389 assert(item != NULL);
1390 assert(item->parent != NULL); /* i.e. it must not be the root */
1392 if (parentItem->firstChild == item)
1393 parentItem->firstChild = item->nextSibling;
1395 if (parentItem->lastChild == item)
1396 parentItem->lastChild = item->prevSibling;
1398 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1399 && parentItem->cChildren > 0)
1400 parentItem->cChildren = 0;
1402 if (item->prevSibling)
1403 item->prevSibling->nextSibling = item->nextSibling;
1405 if (item->nextSibling)
1406 item->nextSibling->prevSibling = item->prevSibling;
1410 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1412 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1414 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1415 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1417 if (wineItem->firstChild)
1418 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1420 TREEVIEW_UnlinkItem(wineItem);
1422 infoPtr->uNumItems--;
1424 if (wineItem->pszText && wineItem->pszText != LPSTR_TEXTCALLBACKW)
1425 Free(wineItem->pszText);
1427 TREEVIEW_FreeItem(infoPtr, wineItem);
1431 /* Empty out the tree. */
1433 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1435 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1437 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1441 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1443 TREEVIEW_ITEM *newSelection = NULL;
1444 TREEVIEW_ITEM *newFirstVisible = NULL;
1445 TREEVIEW_ITEM *parent, *prev = NULL;
1446 BOOL visible = FALSE;
1448 if (wineItem == TVI_ROOT)
1450 TRACE("TVI_ROOT\n");
1451 parent = infoPtr->root;
1452 newSelection = NULL;
1454 TREEVIEW_RemoveTree(infoPtr);
1458 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1461 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1462 parent = wineItem->parent;
1464 if (ISVISIBLE(wineItem))
1466 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1470 if (infoPtr->selectedItem != NULL
1471 && (wineItem == infoPtr->selectedItem
1472 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1474 if (wineItem->nextSibling)
1475 newSelection = wineItem->nextSibling;
1476 else if (wineItem->parent != infoPtr->root)
1477 newSelection = wineItem->parent;
1479 newSelection = wineItem->prevSibling;
1480 TRACE("newSelection = %p\n", newSelection);
1483 if (infoPtr->firstVisible == wineItem)
1485 if (wineItem->nextSibling)
1486 newFirstVisible = wineItem->nextSibling;
1487 else if (wineItem->prevSibling)
1488 newFirstVisible = wineItem->prevSibling;
1489 else if (wineItem->parent != infoPtr->root)
1490 newFirstVisible = wineItem->parent;
1491 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1494 newFirstVisible = infoPtr->firstVisible;
1496 TREEVIEW_RemoveItem(infoPtr, wineItem);
1499 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1500 if (!infoPtr->selectedItem && newSelection)
1502 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1503 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1506 /* Validate insertMark dropItem.
1507 * hotItem ??? - used for comparison only.
1509 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1510 infoPtr->insertMarkItem = 0;
1512 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1513 infoPtr->dropItem = 0;
1515 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1516 newFirstVisible = infoPtr->root->firstChild;
1518 TREEVIEW_VerifyTree(infoPtr);
1523 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1524 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1525 TREEVIEW_UpdateScrollBars(infoPtr);
1526 TREEVIEW_Invalidate(infoPtr, NULL);
1528 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1530 /* parent lost '+/-' - update it */
1531 TREEVIEW_Invalidate(infoPtr, parent);
1538 /* Get/Set Messages *********************************************************/
1540 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1543 infoPtr->bRedraw = TRUE;
1545 infoPtr->bRedraw = FALSE;
1551 TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1554 return infoPtr->uIndent;
1558 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1562 if (newIndent < MINIMUM_INDENT)
1563 newIndent = MINIMUM_INDENT;
1565 if (infoPtr->uIndent != newIndent)
1567 infoPtr->uIndent = newIndent;
1568 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1569 TREEVIEW_UpdateScrollBars(infoPtr);
1570 TREEVIEW_Invalidate(infoPtr, NULL);
1578 TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1581 return (LRESULT)infoPtr->hwndToolTip;
1585 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1590 prevToolTip = infoPtr->hwndToolTip;
1591 infoPtr->hwndToolTip = hwndTT;
1593 return (LRESULT)prevToolTip;
1597 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1599 BOOL rc = infoPtr->bNtfUnicode;
1600 infoPtr->bNtfUnicode = fUnicode;
1605 TREEVIEW_GetUnicodeFormat(TREEVIEW_INFO *infoPtr)
1607 return infoPtr->bNtfUnicode;
1611 TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1613 return infoPtr->uScrollTime;
1617 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1619 UINT uOldScrollTime = infoPtr->uScrollTime;
1621 infoPtr->uScrollTime = min(uScrollTime, 100);
1623 return uOldScrollTime;
1628 TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1634 case (WPARAM)TVSIL_NORMAL:
1635 return (LRESULT)infoPtr->himlNormal;
1637 case (WPARAM)TVSIL_STATE:
1638 return (LRESULT)infoPtr->himlState;
1645 #define TVHEIGHT_MIN 16
1646 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1648 /* Compute the natural height for items. */
1650 TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1654 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1657 /* Height is the maximum of:
1658 * 16 (a hack because our fonts are tiny), and
1659 * The text height + border & margin, and
1660 * The size of the normal image list
1662 GetTextMetricsW(hdc, &tm);
1663 SelectObject(hdc, hOldFont);
1666 height = TVHEIGHT_MIN;
1667 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1668 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1669 if (height < infoPtr->normalImageHeight)
1670 height = infoPtr->normalImageHeight;
1675 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1677 HIMAGELIST himlOld = 0;
1678 int oldWidth = infoPtr->normalImageWidth;
1679 int oldHeight = infoPtr->normalImageHeight;
1682 TRACE("%x,%p\n", wParam, himlNew);
1686 case (WPARAM)TVSIL_NORMAL:
1687 himlOld = infoPtr->himlNormal;
1688 infoPtr->himlNormal = himlNew;
1690 if (himlNew != NULL)
1691 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1692 &infoPtr->normalImageHeight);
1695 infoPtr->normalImageWidth = 0;
1696 infoPtr->normalImageHeight = 0;
1701 case (WPARAM)TVSIL_STATE:
1702 himlOld = infoPtr->himlState;
1703 infoPtr->himlState = himlNew;
1705 if (himlNew != NULL)
1706 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1707 &infoPtr->stateImageHeight);
1710 infoPtr->stateImageWidth = 0;
1711 infoPtr->stateImageHeight = 0;
1717 if (oldWidth != infoPtr->normalImageWidth ||
1718 oldHeight != infoPtr->normalImageHeight)
1720 BOOL bRecalcVisible = FALSE;
1722 if (oldHeight != infoPtr->normalImageHeight &&
1723 !infoPtr->bHeightSet)
1725 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1726 bRecalcVisible = TRUE;
1729 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1730 infoPtr->normalImageWidth != infoPtr->uIndent)
1732 infoPtr->uIndent = infoPtr->normalImageWidth;
1733 bRecalcVisible = TRUE;
1737 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1739 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1740 TREEVIEW_UpdateScrollBars(infoPtr);
1743 TREEVIEW_Invalidate(infoPtr, NULL);
1745 return (LRESULT)himlOld;
1749 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1751 INT prevHeight = infoPtr->uItemHeight;
1753 TRACE("%d \n", newHeight);
1754 if (newHeight == -1)
1756 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1757 infoPtr->bHeightSet = FALSE;
1761 infoPtr->uItemHeight = newHeight;
1762 infoPtr->bHeightSet = TRUE;
1765 /* Round down, unless we support odd ("non even") heights. */
1766 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1767 infoPtr->uItemHeight &= ~1;
1769 if (infoPtr->uItemHeight != prevHeight)
1771 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1772 TREEVIEW_UpdateScrollBars(infoPtr);
1773 TREEVIEW_Invalidate(infoPtr, NULL);
1780 TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1783 return infoPtr->uItemHeight;
1788 TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1790 TRACE("%p\n", infoPtr->hFont);
1791 return (LRESULT)infoPtr->hFont;
1796 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1800 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1806 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1808 UINT uHeight = infoPtr->uItemHeight;
1810 TRACE("%p %i\n", hFont, bRedraw);
1812 infoPtr->hFont = hFont ? hFont : GetStockObject(SYSTEM_FONT);
1814 DeleteObject(infoPtr->hBoldFont);
1815 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1817 if (!infoPtr->bHeightSet)
1818 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1820 if (uHeight != infoPtr->uItemHeight)
1821 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1823 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1825 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1826 TREEVIEW_UpdateScrollBars(infoPtr);
1829 TREEVIEW_Invalidate(infoPtr, NULL);
1836 TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1839 return (LRESULT)infoPtr->clrLine;
1843 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1845 COLORREF prevColor = infoPtr->clrLine;
1848 infoPtr->clrLine = color;
1849 return (LRESULT)prevColor;
1854 TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1857 return (LRESULT)infoPtr->clrText;
1861 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1863 COLORREF prevColor = infoPtr->clrText;
1866 infoPtr->clrText = color;
1868 if (infoPtr->clrText != prevColor)
1869 TREEVIEW_Invalidate(infoPtr, NULL);
1871 return (LRESULT)prevColor;
1876 TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1879 return (LRESULT)infoPtr->clrBk;
1883 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1885 COLORREF prevColor = infoPtr->clrBk;
1888 infoPtr->clrBk = newColor;
1890 if (newColor != prevColor)
1891 TREEVIEW_Invalidate(infoPtr, NULL);
1893 return (LRESULT)prevColor;
1898 TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1901 return (LRESULT)infoPtr->clrInsertMark;
1905 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1907 COLORREF prevColor = infoPtr->clrInsertMark;
1909 TRACE("%lx\n", color);
1910 infoPtr->clrInsertMark = color;
1912 return (LRESULT)prevColor;
1917 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1919 TRACE("%d %p\n", wParam, item);
1921 if (!TREEVIEW_ValidItem(infoPtr, item))
1924 infoPtr->insertBeforeorAfter = wParam;
1925 infoPtr->insertMarkItem = item;
1927 TREEVIEW_Invalidate(infoPtr, NULL);
1933 /************************************************************************
1934 * Some serious braindamage here. lParam is a pointer to both the
1935 * input HTREEITEM and the output RECT.
1938 TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1940 TREEVIEW_ITEM *wineItem;
1941 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1945 * validate parameters
1951 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
1955 * If wParam is TRUE return the text size otherwise return
1956 * the whole item size
1960 /* Windows does not send TVN_GETDISPINFO here. */
1962 lpRect->top = wineItem->rect.top;
1963 lpRect->bottom = wineItem->rect.bottom;
1965 lpRect->left = wineItem->textOffset;
1966 lpRect->right = wineItem->textOffset + wineItem->textWidth;
1970 *lpRect = wineItem->rect;
1973 TRACE("%s [L:%ld R:%ld T:%ld B:%ld]\n", fTextRect ? "text" : "item",
1974 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
1979 static inline LRESULT
1980 TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
1982 /* Suprise! This does not take integral height into account. */
1983 return infoPtr->clientHeight / infoPtr->uItemHeight;
1988 TREEVIEW_GetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
1990 TREEVIEW_ITEM *wineItem;
1992 wineItem = tvItem->hItem;
1993 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1996 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1998 if (tvItem->mask & TVIF_CHILDREN)
2000 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2001 FIXME("I_CHILDRENCALLBACK not supported\n");
2002 tvItem->cChildren = wineItem->cChildren;
2005 if (tvItem->mask & TVIF_HANDLE)
2006 tvItem->hItem = wineItem;
2008 if (tvItem->mask & TVIF_IMAGE)
2009 tvItem->iImage = wineItem->iImage;
2011 if (tvItem->mask & TVIF_INTEGRAL)
2012 tvItem->iIntegral = wineItem->iIntegral;
2014 /* undocumented: windows ignores TVIF_PARAM and
2015 * * always sets lParam
2017 tvItem->lParam = wineItem->lParam;
2019 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2020 tvItem->iSelectedImage = wineItem->iSelectedImage;
2022 if (tvItem->mask & TVIF_STATE)
2023 /* Careful here - Windows ignores the stateMask when you get the state
2024 That contradicts the documentation, but makes more common sense, masking
2025 retrieval in this way seems overkill */
2026 tvItem->state = wineItem->state;
2028 if (tvItem->mask & TVIF_TEXT)
2032 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2034 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2035 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2039 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2044 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2046 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2047 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2051 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2052 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2056 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2057 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2062 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2063 * which is wrong. */
2065 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2067 TREEVIEW_ITEM *wineItem;
2068 TREEVIEW_ITEM originalItem;
2070 wineItem = tvItem->hItem;
2072 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2075 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2078 /* store the orignal item values */
2079 originalItem = *wineItem;
2081 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2084 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2085 if ((tvItem->mask & TVIF_TEXT
2086 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2087 && ISVISIBLE(wineItem))
2089 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2090 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2093 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2095 /* The refresh updates everything, but we can't wait until then. */
2096 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2098 /* if any of the items values changed, redraw the item */
2099 if(memcmp(&originalItem, wineItem, sizeof(TREEVIEW_ITEM)) ||
2100 (tvItem->stateMask & TVIS_BOLD))
2102 if (tvItem->mask & TVIF_INTEGRAL)
2104 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2105 TREEVIEW_UpdateScrollBars(infoPtr);
2107 TREEVIEW_Invalidate(infoPtr, NULL);
2111 TREEVIEW_UpdateScrollBars(infoPtr);
2112 TREEVIEW_Invalidate(infoPtr, wineItem);
2121 TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2125 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2128 return (wineItem->state & mask);
2132 TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2134 TREEVIEW_ITEM *retval;
2138 /* handle all the global data here */
2141 case TVGN_CHILD: /* Special case: child of 0 is root */
2146 retval = infoPtr->root->firstChild;
2150 retval = infoPtr->selectedItem;
2153 case TVGN_FIRSTVISIBLE:
2154 retval = infoPtr->firstVisible;
2157 case TVGN_DROPHILITE:
2158 retval = infoPtr->dropItem;
2161 case TVGN_LASTVISIBLE:
2162 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2168 TRACE("flags:%x, returns %p\n", which, retval);
2169 return (LRESULT)retval;
2172 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2174 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2180 retval = wineItem->nextSibling;
2183 retval = wineItem->prevSibling;
2186 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2189 retval = wineItem->firstChild;
2191 case TVGN_NEXTVISIBLE:
2192 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2194 case TVGN_PREVIOUSVISIBLE:
2195 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2198 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2202 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2203 return (LRESULT)retval;
2208 TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2210 TRACE(" %d\n", infoPtr->uNumItems);
2211 return (LRESULT)infoPtr->uNumItems;
2215 TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2217 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2219 static const unsigned int state_table[] = { 0, 2, 1 };
2223 state = STATEIMAGEINDEX(item->state);
2224 TRACE("state:%x\n", state);
2225 item->state &= ~TVIS_STATEIMAGEMASK;
2228 state = state_table[state];
2230 item->state |= INDEXTOSTATEIMAGEMASK(state);
2232 TRACE("state:%x\n", state);
2233 TREEVIEW_Invalidate(infoPtr, item);
2238 /* Painting *************************************************************/
2240 /* Draw the lines and expand button for an item. Also draws one section
2241 * of the line from item's parent to item's parent's next sibling. */
2243 TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2245 LONG centerx, centery;
2246 BOOL lar = ((infoPtr->dwStyle
2247 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2250 if (!lar && item->iLevel == 0)
2253 centerx = (item->linesOffset + item->stateOffset) / 2;
2254 centery = (item->rect.top + item->rect.bottom) / 2;
2256 if (infoPtr->dwStyle & TVS_HASLINES)
2258 HPEN hOldPen, hNewPen;
2262 * Get a dotted grey pen
2264 hNewPen = CreatePen(PS_ALTERNATE, 0, infoPtr->clrLine);
2265 hOldPen = SelectObject(hdc, hNewPen);
2267 MoveToEx(hdc, item->stateOffset, centery, NULL);
2268 LineTo(hdc, centerx - 1, centery);
2270 if (item->prevSibling || item->parent != infoPtr->root)
2272 MoveToEx(hdc, centerx, item->rect.top, NULL);
2273 LineTo(hdc, centerx, centery);
2276 if (item->nextSibling)
2278 MoveToEx(hdc, centerx, centery, NULL);
2279 LineTo(hdc, centerx, item->rect.bottom + 1);
2282 /* Draw the line from our parent to its next sibling. */
2283 parent = item->parent;
2284 while (parent != infoPtr->root)
2286 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2288 if (parent->nextSibling
2289 /* skip top-levels unless TVS_LINESATROOT */
2290 && parent->stateOffset > parent->linesOffset)
2292 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2293 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2296 parent = parent->parent;
2299 SelectObject(hdc, hOldPen);
2300 DeleteObject(hNewPen);
2304 * Display the (+/-) signs
2307 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2309 if (item->cChildren)
2311 LONG height = item->rect.bottom - item->rect.top;
2312 LONG width = item->stateOffset - item->linesOffset;
2313 LONG rectsize = min(height, width) / 4;
2314 /* plussize = ceil(rectsize * 3/4) */
2315 LONG plussize = (rectsize + 1) * 3 / 4;
2317 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2318 HPEN hOldPen = SelectObject(hdc, hNewPen);
2319 HBRUSH hbr = CreateSolidBrush(infoPtr->clrBk);
2320 HBRUSH hbrOld = SelectObject(hdc, hbr);
2322 /* FIXME: Native actually draws:
2332 * This looks a lot better when the icon size is increased.
2334 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2335 centerx + rectsize + 2, centery + rectsize + 2);
2337 SelectObject(hdc, hbrOld);
2340 SelectObject(hdc, hOldPen);
2341 DeleteObject(hNewPen);
2343 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2344 LineTo(hdc, centerx + plussize, centery);
2346 if (!(item->state & TVIS_EXPANDED))
2348 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2349 LineTo(hdc, centerx, centery + plussize);
2356 TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2362 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2364 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2366 /* The custom draw handler can query the text rectangle,
2368 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2372 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2374 cditem = TREEVIEW_SendCustomDrawItemNotify
2375 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT);
2376 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2378 if (cditem & CDRF_SKIPDEFAULT)
2380 SelectObject(hdc, hOldFont);
2385 if (cditem & CDRF_NEWFONT)
2386 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2388 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2390 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2393 * Display the images associated with this item
2398 /* State images are displayed to the left of the Normal image
2399 * image number is in state; zero should be `display no image'.
2401 imageIndex = STATEIMAGEINDEX(wineItem->state);
2403 if (infoPtr->himlState && imageIndex)
2405 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2406 wineItem->stateOffset,
2407 centery - infoPtr->stateImageHeight / 2,
2411 /* Now, draw the normal image; can be either selected or
2412 * non-selected image.
2415 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage))
2417 /* The item is currently selected */
2418 imageIndex = wineItem->iSelectedImage;
2422 /* The item is not selected */
2423 imageIndex = wineItem->iImage;
2426 if (infoPtr->himlNormal)
2428 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2430 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2431 wineItem->imageOffset,
2432 centery - infoPtr->normalImageHeight / 2,
2433 ILD_NORMAL | ovlIdx);
2439 * Display the text associated with this item
2442 /* Don't paint item's text if it's being edited */
2443 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2445 if (wineItem->pszText)
2447 COLORREF oldTextColor = 0;
2450 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2453 oldBkMode = SetBkMode(hdc, TRANSPARENT);
2455 /* - If item is drop target or it is selected and window is in focus -
2456 * use blue background (COLOR_HIGHLIGHT).
2457 * - If item is selected, window is not in focus, but it has style
2458 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2459 * - Otherwise - don't fill background
2461 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2462 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2463 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2465 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2467 hbrBk = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
2469 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2473 hbrBk = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
2475 if (infoPtr->clrText == -1)
2477 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2479 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2484 if (infoPtr->clrText == -1)
2486 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2488 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2491 rcText.top = wineItem->rect.top;
2492 rcText.bottom = wineItem->rect.bottom;
2493 rcText.left = wineItem->textOffset;
2494 rcText.right = rcText.left + wineItem->textWidth + 4;
2498 FillRect(hdc, &rcText, hbrBk);
2499 DeleteObject(hbrBk);
2502 /* Draw the box around the selected item */
2503 if ((wineItem == infoPtr->selectedItem) && inFocus)
2505 DrawFocusRect(hdc,&rcText);
2508 InflateRect(&rcText, -2, -1); /* allow for the focus rect */
2510 TRACE("drawing text %s at (%ld,%ld)-(%ld,%ld)\n",
2511 debugstr_w(wineItem->pszText),
2512 rcText.left, rcText.top, rcText.right, rcText.bottom);
2517 lstrlenW(wineItem->pszText),
2519 DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
2521 /* Restore the hdc state */
2522 SetTextColor(hdc, oldTextColor);
2524 if (oldBkMode != TRANSPARENT)
2525 SetBkMode(hdc, oldBkMode);
2529 /* Draw insertion mark if necessary */
2531 if (infoPtr->insertMarkItem)
2532 TRACE("item:%d,mark:%d\n",
2533 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2534 (int)infoPtr->insertMarkItem);
2536 if (wineItem == infoPtr->insertMarkItem)
2538 HPEN hNewPen, hOldPen;
2542 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2543 hOldPen = SelectObject(hdc, hNewPen);
2545 if (infoPtr->insertBeforeorAfter)
2546 offset = wineItem->rect.bottom - 1;
2548 offset = wineItem->rect.top + 1;
2550 left = wineItem->textOffset - 2;
2551 right = wineItem->textOffset + wineItem->textWidth + 2;
2553 MoveToEx(hdc, left, offset - 3, NULL);
2554 LineTo(hdc, left, offset + 4);
2556 MoveToEx(hdc, left, offset, NULL);
2557 LineTo(hdc, right + 1, offset);
2559 MoveToEx(hdc, right, offset + 3, NULL);
2560 LineTo(hdc, right, offset - 4);
2562 SelectObject(hdc, hOldPen);
2563 DeleteObject(hNewPen);
2566 if (cditem & CDRF_NOTIFYPOSTPAINT)
2568 cditem = TREEVIEW_SendCustomDrawItemNotify
2569 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT);
2570 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2573 SelectObject(hdc, hOldFont);
2576 /* Computes treeHeight and treeWidth and updates the scroll bars.
2579 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2581 TREEVIEW_ITEM *wineItem;
2582 HWND hwnd = infoPtr->hwnd;
2586 LONG scrollX = infoPtr->scrollX;
2588 infoPtr->treeWidth = 0;
2589 infoPtr->treeHeight = 0;
2591 /* We iterate through all visible items in order to get the tree height
2593 wineItem = infoPtr->root->firstChild;
2595 while (wineItem != NULL)
2597 if (ISVISIBLE(wineItem))
2599 /* actually we draw text at textOffset + 2 */
2600 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2601 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2603 /* This is scroll-adjusted, but we fix this below. */
2604 infoPtr->treeHeight = wineItem->rect.bottom;
2607 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2610 /* Fix the scroll adjusted treeHeight and treeWidth. */
2611 if (infoPtr->root->firstChild)
2612 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2614 infoPtr->treeWidth += infoPtr->scrollX;
2616 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2618 /* Adding one scroll bar may take up enough space that it forces us
2619 * to add the other as well. */
2620 if (infoPtr->treeHeight > infoPtr->clientHeight)
2624 if (infoPtr->treeWidth
2625 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2628 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2631 if (!vert && horz && infoPtr->treeHeight
2632 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2635 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2637 si.cbSize = sizeof(SCROLLINFO);
2638 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2643 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2644 if ( si.nPage && NULL != infoPtr->firstVisible)
2646 si.nPos = infoPtr->firstVisible->visibleOrder;
2647 si.nMax = infoPtr->maxVisibleOrder - 1;
2649 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2651 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2652 ShowScrollBar(hwnd, SB_VERT, TRUE);
2653 infoPtr->uInternalStatus |= TV_VSCROLL;
2657 if (infoPtr->uInternalStatus & TV_VSCROLL)
2658 ShowScrollBar(hwnd, SB_VERT, FALSE);
2659 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2664 if (infoPtr->uInternalStatus & TV_VSCROLL)
2665 ShowScrollBar(hwnd, SB_VERT, FALSE);
2666 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2671 si.nPage = infoPtr->clientWidth;
2672 si.nPos = infoPtr->scrollX;
2673 si.nMax = infoPtr->treeWidth - 1;
2675 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2677 si.nPos = si.nMax - max( si.nPage-1, 0 );
2681 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2682 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2683 infoPtr->uInternalStatus |= TV_HSCROLL;
2685 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2689 if (infoPtr->uInternalStatus & TV_HSCROLL)
2690 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2691 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2696 if (infoPtr->scrollX != scrollX)
2698 TREEVIEW_HScroll(infoPtr,
2699 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2703 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2706 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2708 TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2710 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2713 GetClientRect(infoPtr->hwnd, &rect);
2714 FillRect(hDC, &rect, hBrush);
2715 DeleteObject(hBrush);
2721 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2723 HWND hwnd = infoPtr->hwnd;
2725 TREEVIEW_ITEM *wineItem;
2727 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2729 TRACE("empty window\n");
2733 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2736 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2738 ReleaseDC(hwnd, hdc);
2742 for (wineItem = infoPtr->root->firstChild;
2744 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2746 if (ISVISIBLE(wineItem))
2748 /* Avoid unneeded calculations */
2749 if (wineItem->rect.top > rect.bottom)
2751 if (wineItem->rect.bottom < rect.top)
2754 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2758 TREEVIEW_UpdateScrollBars(infoPtr);
2760 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2762 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2766 TREEVIEW_Invalidate(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2769 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2771 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2775 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2786 if (!GetUpdateRect(infoPtr->hwnd, &rc, TRUE))
2790 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2791 if (!hbitmap) return 0;
2792 GetObjectA(hbitmap, sizeof(BITMAP), &bitmap);
2793 rc.left = 0; rc.top = 0;
2794 rc.right = bitmap.bmWidth;
2795 rc.bottom = bitmap.bmHeight;
2796 TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
2801 hdc = BeginPaint(infoPtr->hwnd, &ps);
2805 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2806 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2809 EndPaint(infoPtr->hwnd, &ps);
2815 /* Sorting **************************************************************/
2817 /***************************************************************************
2818 * Forward the DPA local callback to the treeview owner callback
2821 TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2823 /* Forward the call to the client-defined callback */
2824 return pCallBackSort->lpfnCompare(first->lParam,
2826 pCallBackSort->lParam);
2829 /***************************************************************************
2830 * Treeview native sort routine: sort on item text.
2833 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2834 TREEVIEW_INFO *infoPtr)
2836 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2837 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2839 if(first->pszText && second->pszText)
2840 return lstrcmpiW(first->pszText, second->pszText);
2841 else if(first->pszText)
2843 else if(second->pszText)
2849 /* Returns the number of physical children belonging to item. */
2851 TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2856 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2862 /* Returns a DPA containing a pointer to each physical child of item in
2863 * sibling order. If item has no children, an empty DPA is returned. */
2865 TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2867 HTREEITEM child = item->firstChild;
2869 HDPA list = DPA_Create(8);
2870 if (list == 0) return NULL;
2872 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2874 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2884 /***************************************************************************
2885 * Setup the treeview structure with regards of the sort method
2886 * and sort the children of the TV item specified in lParam
2887 * fRecurse: currently unused. Should be zero.
2888 * parent: if pSort!=NULL, should equal pSort->hParent.
2889 * otherwise, item which child items are to be sorted.
2890 * pSort: sort method info. if NULL, sort on item text.
2891 * if non-NULL, sort on item's lParam content, and let the
2892 * application decide what that means. See also TVM_SORTCHILDRENCB.
2896 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2900 PFNDPACOMPARE pfnCompare;
2903 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
2904 if (parent == TVI_ROOT || parent == NULL)
2905 parent = infoPtr->root;
2907 /* Check for a valid handle to the parent item */
2908 if (!TREEVIEW_ValidItem(infoPtr, parent))
2910 ERR("invalid item hParent=%x\n", (INT)parent);
2916 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2917 lpCompare = (LPARAM)pSort;
2921 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2922 lpCompare = (LPARAM)infoPtr;
2925 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2927 /* Make sure there is something to sort */
2930 /* TREEVIEW_ITEM rechaining */
2933 HTREEITEM nextItem = 0;
2934 HTREEITEM prevItem = 0;
2936 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2938 if (sortList == NULL)
2941 /* let DPA sort the list */
2942 DPA_Sort(sortList, pfnCompare, lpCompare);
2944 /* The order of DPA entries has been changed, so fixup the
2945 * nextSibling and prevSibling pointers. */
2947 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2948 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2950 /* link the two current item toghether */
2951 item->nextSibling = nextItem;
2952 nextItem->prevSibling = item;
2954 if (prevItem == NULL)
2956 /* this is the first item, update the parent */
2957 parent->firstChild = item;
2958 item->prevSibling = NULL;
2962 /* fix the back chaining */
2963 item->prevSibling = prevItem;
2966 /* get ready for the next one */
2971 /* the last item is pointed to by item and never has a sibling */
2972 item->nextSibling = NULL;
2973 parent->lastChild = item;
2975 DPA_Destroy(sortList);
2977 TREEVIEW_VerifyTree(infoPtr);
2979 if (parent->state & TVIS_EXPANDED)
2981 int visOrder = infoPtr->firstVisible->visibleOrder;
2983 if (parent == infoPtr->root)
2984 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
2986 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
2988 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
2990 TREEVIEW_ITEM *item;
2992 for (item = infoPtr->root->firstChild; item != NULL;
2993 item = TREEVIEW_GetNextListItem(infoPtr, item))
2995 if (item->visibleOrder == visOrder)
2999 if (!item) item = parent->firstChild;
3000 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3003 TREEVIEW_Invalidate(infoPtr, NULL);
3012 /***************************************************************************
3013 * Setup the treeview structure with regards of the sort method
3014 * and sort the children of the TV item specified in lParam
3017 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3019 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3023 /***************************************************************************
3024 * Sort the children of the TV item specified in lParam.
3027 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3029 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3033 /* Expansion/Collapse ***************************************************/
3036 TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3039 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3040 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3041 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3046 TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3049 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3050 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3051 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3056 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3057 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3059 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3060 BOOL bRemoveChildren, BOOL bUser)
3062 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3063 BOOL bSetSelection, bSetFirstVisible;
3065 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3067 if (!(wineItem->state & TVIS_EXPANDED))
3070 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3071 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3073 if (wineItem->firstChild == NULL)
3076 wineItem->state &= ~TVIS_EXPANDED;
3078 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3079 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3081 bSetSelection = (infoPtr->selectedItem != NULL
3082 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3084 bSetFirstVisible = (infoPtr->firstVisible != NULL
3085 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3087 if (bRemoveChildren)
3089 INT old_cChildren = wineItem->cChildren;
3090 TRACE("TVE_COLLAPSERESET\n");
3091 wineItem->state &= ~TVIS_EXPANDEDONCE;
3092 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3093 wineItem->cChildren = old_cChildren;
3096 if (wineItem->firstChild)
3098 TREEVIEW_ITEM *item, *sibling;
3100 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3102 for (item = wineItem->firstChild; item != sibling;
3103 item = TREEVIEW_GetNextListItem(infoPtr, item))
3105 item->visibleOrder = -1;
3109 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3111 TREEVIEW_SetFirstVisible(infoPtr, bSetFirstVisible ? wineItem
3112 : infoPtr->firstVisible, TRUE);
3116 /* Don't call DoSelectItem, it sends notifications. */
3117 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3118 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3119 wineItem->state |= TVIS_SELECTED;
3120 infoPtr->selectedItem = wineItem;
3122 TREEVIEW_EnsureVisible(infoPtr, wineItem, FALSE);
3125 TREEVIEW_UpdateScrollBars(infoPtr);
3126 TREEVIEW_Invalidate(infoPtr, NULL);
3132 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3133 BOOL bExpandPartial, BOOL bUser)
3137 if (wineItem->state & TVIS_EXPANDED)
3140 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3142 if (bUser || ((wineItem->cChildren != 0) &&
3143 !(wineItem->state & TVIS_EXPANDEDONCE)))
3145 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3147 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3151 if (!wineItem->firstChild)
3154 wineItem->state |= TVIS_EXPANDED;
3155 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3156 wineItem->state |= TVIS_EXPANDEDONCE;
3160 if (!wineItem->firstChild)
3163 /* this item has already been expanded */
3164 wineItem->state |= TVIS_EXPANDED;
3168 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3170 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3171 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3172 TREEVIEW_UpdateScrollBars(infoPtr);
3174 /* Scroll up so that as many children as possible are visible.
3175 * This looses when expanding causes an HScroll bar to appear, but we
3176 * don't know that yet, so the last item is obscured. */
3177 if (wineItem->firstChild != NULL)
3179 int nChildren = wineItem->lastChild->visibleOrder
3180 - wineItem->firstChild->visibleOrder + 1;
3182 int visible_pos = wineItem->visibleOrder
3183 - infoPtr->firstVisible->visibleOrder;
3185 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3187 if (visible_pos > 0 && nChildren > rows_below)
3189 int scroll = nChildren - rows_below;
3191 if (scroll > visible_pos)
3192 scroll = visible_pos;
3196 TREEVIEW_ITEM *newFirstVisible
3197 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3201 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3206 TREEVIEW_Invalidate(infoPtr, NULL);
3212 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3216 if (wineItem->state & TVIS_EXPANDED)
3217 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3219 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3223 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3225 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3227 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3229 if (TREEVIEW_HasChildren(infoPtr, item))
3230 TREEVIEW_ExpandAll(infoPtr, item);
3234 /* Note:If the specified item is the child of a collapsed parent item,
3235 the parent's list of child items is (recursively) expanded to reveal the
3236 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3237 know if it also applies here.
3241 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3243 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3246 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3247 TREEVIEW_ItemName(wineItem), flag,
3248 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3250 switch (flag & TVE_TOGGLE)
3253 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3257 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3261 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3268 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3272 /* Hit-Testing **********************************************************/
3274 static TREEVIEW_ITEM *
3275 TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3277 TREEVIEW_ITEM *wineItem;
3280 if (!infoPtr->firstVisible)
3283 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3285 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3286 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3288 if (row >= wineItem->visibleOrder
3289 && row < wineItem->visibleOrder + wineItem->iIntegral)
3297 TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3299 TREEVIEW_ITEM *wineItem;
3305 GetClientRect(infoPtr->hwnd, &rect);
3312 status |= TVHT_TOLEFT;
3314 else if (x > rect.right)
3316 status |= TVHT_TORIGHT;
3321 status |= TVHT_ABOVE;
3323 else if (y > rect.bottom)
3325 status |= TVHT_BELOW;
3330 lpht->flags = status;
3331 return (LRESULT)(HTREEITEM)NULL;
3334 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3337 lpht->flags = TVHT_NOWHERE;
3338 return (LRESULT)(HTREEITEM)NULL;
3341 if (x >= wineItem->textOffset + wineItem->textWidth)
3343 lpht->flags = TVHT_ONITEMRIGHT;
3345 else if (x >= wineItem->textOffset)
3347 lpht->flags = TVHT_ONITEMLABEL;
3349 else if (x >= wineItem->imageOffset)
3351 lpht->flags = TVHT_ONITEMICON;
3353 else if (x >= wineItem->stateOffset)
3355 lpht->flags = TVHT_ONITEMSTATEICON;
3357 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3359 lpht->flags = TVHT_ONITEMBUTTON;
3363 lpht->flags = TVHT_ONITEMINDENT;
3366 lpht->hItem = wineItem;
3367 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3369 return (LRESULT)wineItem;
3372 /* Item Label Editing ***************************************************/
3375 TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3377 return (LRESULT)infoPtr->hwndEdit;
3380 static LRESULT CALLBACK
3381 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3383 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3384 BOOL bCancel = FALSE;
3390 TRACE("WM_PAINT start\n");
3391 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3393 TRACE("WM_PAINT done\n");
3397 if (infoPtr->bIgnoreEditKillFocus)
3402 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3405 if (wParam == (WPARAM)VK_ESCAPE)
3410 else if (wParam == (WPARAM)VK_RETURN)
3417 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3420 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3421 /* eg. Using a messagebox */
3423 infoPtr->bIgnoreEditKillFocus = TRUE;
3424 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3425 infoPtr->bIgnoreEditKillFocus = FALSE;
3431 /* should handle edit control messages here */
3434 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3436 TRACE("%x %ld\n", wParam, lParam);
3438 switch (HIWORD(wParam))
3443 * Adjust the edit window size
3446 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3447 HDC hdc = GetDC(infoPtr->hwndEdit);
3450 HFONT hFont, hOldFont = 0;
3452 infoPtr->bLabelChanged = TRUE;
3454 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer));
3456 /* Select font to get the right dimension of the string */
3457 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3461 hOldFont = SelectObject(hdc, hFont);
3464 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3466 TEXTMETRICW textMetric;
3468 /* Add Extra spacing for the next character */
3469 GetTextMetricsW(hdc, &textMetric);
3470 sz.cx += (textMetric.tmMaxCharWidth * 2);
3472 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3474 infoPtr->clientWidth - editItem->textOffset + 2);
3476 SetWindowPos(infoPtr->hwndEdit,
3481 editItem->rect.bottom - editItem->rect.top + 3,
3482 SWP_NOMOVE | SWP_DRAWFRAME);
3487 SelectObject(hdc, hOldFont);
3490 ReleaseDC(infoPtr->hwnd, hdc);
3495 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3502 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3504 HWND hwnd = infoPtr->hwnd;
3507 TREEVIEW_ITEM *editItem = hItem;
3508 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3511 TEXTMETRICW textMetric;
3512 static const WCHAR EditW[] = {'E','d','i','t',0};
3514 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3515 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3518 if (infoPtr->hwndEdit)
3519 return infoPtr->hwndEdit;
3521 infoPtr->bLabelChanged = FALSE;
3523 /* Make sure that edit item is selected */
3524 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3525 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3527 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3530 /* Select the font to get appropriate metric dimensions */
3531 if (infoPtr->hFont != 0)
3533 hOldFont = SelectObject(hdc, infoPtr->hFont);
3536 /* Get string length in pixels */
3537 GetTextExtentPoint32W(hdc, editItem->pszText, strlenW(editItem->pszText),
3540 /* Add Extra spacing for the next character */
3541 GetTextMetricsW(hdc, &textMetric);
3542 sz.cx += (textMetric.tmMaxCharWidth * 2);
3544 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3545 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3547 if (infoPtr->hFont != 0)
3549 SelectObject(hdc, hOldFont);
3552 ReleaseDC(hwnd, hdc);
3553 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3556 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3557 WS_CLIPSIBLINGS | ES_WANTRETURN |
3558 ES_LEFT, editItem->textOffset - 2,
3559 editItem->rect.top - 1, sz.cx + 3,
3560 editItem->rect.bottom -
3561 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3562 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3564 infoPtr->hwndEdit = hwndEdit;
3566 /* Get a 2D border. */
3567 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3568 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3569 SetWindowLongW(hwndEdit, GWL_STYLE,
3570 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3572 SendMessageW(hwndEdit, WM_SETFONT,
3573 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3575 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3577 TREEVIEW_Edit_SubclassProc);
3579 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3581 DestroyWindow(hwndEdit);
3582 infoPtr->hwndEdit = 0;
3586 infoPtr->selectedItem = hItem;
3587 SetWindowTextW(hwndEdit, editItem->pszText);
3589 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3590 ShowWindow(hwndEdit, SW_SHOW);
3597 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3599 HWND hwnd = infoPtr->hwnd;
3600 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3603 WCHAR tmpText[1024] = { '\0' };
3604 WCHAR *newText = tmpText;
3607 if (!infoPtr->hwndEdit)
3610 tvdi.hdr.hwndFrom = hwnd;
3611 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3612 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3614 tvdi.item.hItem = editedItem;
3615 tvdi.item.state = editedItem->state;
3616 tvdi.item.lParam = editedItem->lParam;
3620 if (!infoPtr->bNtfUnicode)
3621 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3623 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3625 if (iLength >= 1023)
3627 ERR("Insufficient space to retrieve new item label\n");
3630 tvdi.item.mask = TVIF_TEXT;
3631 tvdi.item.pszText = tmpText;
3632 tvdi.item.cchTextMax = iLength + 1;
3636 tvdi.item.pszText = NULL;
3637 tvdi.item.cchTextMax = 0;
3640 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
3641 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3643 if (!bCancel && bCommit) /* Apply the changes */
3645 if (!infoPtr->bNtfUnicode)
3647 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3648 newText = Alloc(len * sizeof(WCHAR));
3649 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3653 if (strcmpW(newText, editedItem->pszText) != 0)
3655 if (NULL == ReAlloc(editedItem->pszText, iLength + 1))
3657 ERR("OutOfMemory, cannot allocate space for label\n");
3658 DestroyWindow(infoPtr->hwndEdit);
3659 infoPtr->hwndEdit = 0;
3664 editedItem->cchTextMax = iLength + 1;
3665 strcpyW(editedItem->pszText, newText);
3668 if(newText != tmpText) Free(newText);
3671 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3672 DestroyWindow(infoPtr->hwndEdit);
3673 infoPtr->hwndEdit = 0;
3678 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3680 if (wParam != TV_EDIT_TIMER)
3682 ERR("got unknown timer\n");
3686 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3687 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3689 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3695 /* Mouse Tracking/Drag **************************************************/
3697 /***************************************************************************
3698 * This is quite unusual piece of code, but that's how it's implemented in
3702 TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3704 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3705 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3709 r.top = pt.y - cyDrag;
3710 r.left = pt.x - cxDrag;
3711 r.bottom = pt.y + cyDrag;
3712 r.right = pt.x + cxDrag;
3714 SetCapture(infoPtr->hwnd);
3718 if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3720 if (msg.message == WM_MOUSEMOVE)
3722 pt.x = (short)LOWORD(msg.lParam);
3723 pt.y = (short)HIWORD(msg.lParam);
3724 if (PtInRect(&r, pt))
3732 else if (msg.message >= WM_LBUTTONDOWN &&
3733 msg.message <= WM_RBUTTONDBLCLK)
3735 if (msg.message == WM_RBUTTONUP)
3736 TREEVIEW_RButtonUp(infoPtr, &pt);
3740 DispatchMessageA(&msg);
3743 if (GetCapture() != infoPtr->hwnd)
3753 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3755 TREEVIEW_ITEM *wineItem;
3759 SetFocus(infoPtr->hwnd);
3761 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3763 /* If there is pending 'edit label' event - kill it now */
3764 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3767 hit.pt.x = (short)LOWORD(lParam);
3768 hit.pt.y = (short)HIWORD(lParam);
3770 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3773 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3775 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3779 case TVHT_ONITEMRIGHT:
3780 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3783 case TVHT_ONITEMINDENT:
3784 if (!(infoPtr->dwStyle & TVS_HASLINES))
3790 int level = hit.pt.x / infoPtr->uIndent;
3791 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3793 while (wineItem->iLevel > level)
3795 wineItem = wineItem->parent;
3801 case TVHT_ONITEMLABEL:
3802 case TVHT_ONITEMICON:
3803 case TVHT_ONITEMBUTTON:
3804 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3807 case TVHT_ONITEMSTATEICON:
3808 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3809 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3811 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3820 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3822 HWND hwnd = infoPtr->hwnd;
3824 BOOL bTrack, bDoLabelEdit;
3827 /* If Edit control is active - kill it and return.
3828 * The best way to do it is to set focus to itself.
3829 * Edit control subclassed procedure will automatically call
3832 if (infoPtr->hwndEdit)
3838 ht.pt.x = (short)LOWORD(lParam);
3839 ht.pt.y = (short)HIWORD(lParam);
3841 TREEVIEW_HitTest(infoPtr, &ht);
3842 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3844 /* update focusedItem and redraw both items */
3845 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3847 infoPtr->focusedItem = ht.hItem;
3848 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3850 if(infoPtr->selectedItem)
3851 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3854 bTrack = (ht.flags & TVHT_ONITEM)
3855 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3858 * If the style allows editing and the node is already selected
3859 * and the click occurred on the item label...
3861 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
3862 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
3864 /* Send NM_CLICK right away */
3866 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3869 if (ht.flags & TVHT_ONITEMBUTTON)
3871 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3875 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
3876 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3878 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
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 (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3902 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3903 KillTimer(hwnd, TV_EDIT_TIMER);
3905 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
3906 infoPtr->Timer |= TV_EDIT_TIMER_SET;
3908 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
3911 * if we are TVS_SINGLEEXPAND then we want this single click to
3912 * do a bunch of things.
3914 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
3915 (infoPtr->hwndEdit == 0))
3917 TREEVIEW_ITEM *SelItem;
3920 * Send the notification
3922 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
3925 * Close the previous selection all the way to the root
3926 * as long as the new selection is not a child
3928 if((infoPtr->selectedItem)
3929 && (infoPtr->selectedItem != ht.hItem))
3931 BOOL closeit = TRUE;
3934 /* determine if the hitItem is a child of the currently selected item */
3935 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3937 closeit = (SelItem != infoPtr->selectedItem);
3938 SelItem = SelItem->parent;
3943 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3944 SelItem = infoPtr->selectedItem;
3946 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3948 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3949 SelItem = SelItem->parent;
3955 * Expand the current item
3957 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
3960 /* Select the current item */
3961 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
3963 else if (ht.flags & TVHT_ONITEMSTATEICON)
3965 /* TVS_CHECKBOXES requires us to toggle the current state */
3966 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3967 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
3977 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3981 if (infoPtr->hwndEdit)
3983 SetFocus(infoPtr->hwnd);
3987 ht.pt.x = (short)LOWORD(lParam);
3988 ht.pt.y = (short)HIWORD(lParam);
3990 TREEVIEW_HitTest(infoPtr, &ht);
3992 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3996 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
3997 infoPtr->dropItem = ht.hItem;
4002 SetFocus(infoPtr->hwnd);
4003 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4010 TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
4017 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4019 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4023 HBITMAP hbmp, hOldbmp;
4030 if (!(infoPtr->himlNormal))
4033 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4036 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4038 hwtop = GetDesktopWindow();
4039 htopdc = GetDC(hwtop);
4040 hdc = CreateCompatibleDC(htopdc);
4042 hOldFont = SelectObject(hdc, infoPtr->hFont);
4043 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4045 TRACE("%ld %ld %s %d\n", size.cx, size.cy, debugstr_w(dragItem->pszText),
4046 strlenW(dragItem->pszText));
4047 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4048 hOldbmp = SelectObject(hdc, hbmp);
4050 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4055 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4056 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4060 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4061 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4064 /* draw item text */
4066 SetRect(&rc, cx, 0, size.cx, size.cy);
4067 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4069 SelectObject(hdc, hOldFont);
4070 SelectObject(hdc, hOldbmp);
4072 ImageList_Add(infoPtr->dragList, hbmp, 0);
4076 ReleaseDC(hwtop, htopdc);
4078 return (LRESULT)infoPtr->dragList;
4081 /* Selection ************************************************************/
4084 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4087 TREEVIEW_ITEM *prevSelect;
4090 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4092 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4093 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4094 newSelect ? newSelect->state : 0);
4096 /* reset and redraw focusedItem if focusedItem was set so we don't */
4097 /* have to worry about the previously focused item when we set a new one */
4098 if(infoPtr->focusedItem)
4100 rcFocused = (infoPtr->focusedItem)->rect;
4101 infoPtr->focusedItem = 0;
4102 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4108 prevSelect = infoPtr->selectedItem;
4110 if (prevSelect == newSelect)
4113 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4116 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4122 prevSelect->state &= ~TVIS_SELECTED;
4124 newSelect->state |= TVIS_SELECTED;
4126 infoPtr->selectedItem = newSelect;
4128 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4130 TREEVIEW_SendTreeviewNotify(infoPtr,
4133 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4136 TREEVIEW_Invalidate(infoPtr, prevSelect);
4137 TREEVIEW_Invalidate(infoPtr, newSelect);
4140 case TVGN_DROPHILITE:
4141 prevSelect = infoPtr->dropItem;
4144 prevSelect->state &= ~TVIS_DROPHILITED;
4146 infoPtr->dropItem = newSelect;
4149 newSelect->state |= TVIS_DROPHILITED;
4151 TREEVIEW_Invalidate(infoPtr, prevSelect);
4152 TREEVIEW_Invalidate(infoPtr, newSelect);
4155 case TVGN_FIRSTVISIBLE:
4156 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4157 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4158 TREEVIEW_Invalidate(infoPtr, NULL);
4162 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4166 /* FIXME: handle NM_KILLFOCUS etc */
4168 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4170 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4173 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4175 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4181 /*************************************************************************
4182 * TREEVIEW_ProcessLetterKeys
4184 * Processes keyboard messages generated by pressing the letter keys
4186 * What this does is perform a case insensitive search from the
4187 * current position with the following quirks:
4188 * - If two chars or more are pressed in quick succession we search
4189 * for the corresponding string (e.g. 'abc').
4190 * - If there is a delay we wipe away the current search string and
4191 * restart with just that char.
4192 * - If the user keeps pressing the same character, whether slowly or
4193 * fast, so that the search string is entirely composed of this
4194 * character ('aaaaa' for instance), then we search for first item
4195 * that starting with that character.
4196 * - If the user types the above character in quick succession, then
4197 * we must also search for the corresponding string ('aaaaa'), and
4198 * go to that string if there is a match.
4206 * - The current implementation has a list of characters it will
4207 * accept and it ignores averything else. In particular it will
4208 * ignore accentuated characters which seems to match what
4209 * Windows does. But I'm not sure it makes sense to follow
4211 * - We don't sound a beep when the search fails.
4212 * - The search should start from the focused item, not from the selected
4213 * item. One reason for this is to allow for multiple selections in trees.
4214 * But currently infoPtr->focusedItem does not seem very usable.
4218 * TREEVIEW_ProcessLetterKeys
4220 static INT TREEVIEW_ProcessLetterKeys(
4221 HWND hwnd, /* handle to the window */
4222 WPARAM charCode, /* the character code, the actual character */
4223 LPARAM keyData /* key data */
4226 TREEVIEW_INFO *infoPtr;
4228 HTREEITEM endidx,idx;
4230 WCHAR buffer[MAX_PATH];
4231 DWORD timestamp,elapsed;
4233 /* simple parameter checking */
4234 if (!hwnd || !charCode || !keyData)
4237 infoPtr=(TREEVIEW_INFO*)GetWindowLongPtrW(hwnd, 0);
4241 /* only allow the valid WM_CHARs through */
4242 if (!isalnum(charCode) &&
4243 charCode != '.' && charCode != '`' && charCode != '!' &&
4244 charCode != '@' && charCode != '#' && charCode != '$' &&
4245 charCode != '%' && charCode != '^' && charCode != '&' &&
4246 charCode != '*' && charCode != '(' && charCode != ')' &&
4247 charCode != '-' && charCode != '_' && charCode != '+' &&
4248 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4249 charCode != '}' && charCode != '[' && charCode != '{' &&
4250 charCode != '/' && charCode != '?' && charCode != '>' &&
4251 charCode != '<' && charCode != ',' && charCode != '~')
4254 /* compute how much time elapsed since last keypress */
4255 timestamp = GetTickCount();
4256 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4257 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4259 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4262 /* update the search parameters */
4263 infoPtr->lastKeyPressTimestamp=timestamp;
4264 if (elapsed < KEY_DELAY) {
4265 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4266 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4268 if (infoPtr->charCode != charCode) {
4269 infoPtr->charCode=charCode=0;
4272 infoPtr->charCode=charCode;
4273 infoPtr->szSearchParam[0]=charCode;
4274 infoPtr->nSearchParamLength=1;
4275 /* Redundant with the 1 char string */
4279 /* and search from the current position */
4281 if (infoPtr->selectedItem != NULL) {
4282 endidx=infoPtr->selectedItem;
4283 /* if looking for single character match,
4284 * then we must always move forward
4286 if (infoPtr->nSearchParamLength == 1)
4287 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4292 idx=infoPtr->root->firstChild;
4298 idx=infoPtr->root->firstChild;
4302 ZeroMemory(&item, sizeof(item));
4303 item.mask = TVIF_TEXT;
4305 item.pszText = buffer;
4306 item.cchTextMax = sizeof(buffer);
4307 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4309 /* check for a match */
4310 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4313 } else if ( (charCode != 0) && (nItem == NULL) &&
4314 (nItem != infoPtr->selectedItem) &&
4315 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4316 /* This would work but we must keep looking for a longer match */
4319 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4320 } while (idx != endidx);
4322 if (nItem != NULL) {
4323 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4324 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4331 /* Scrolling ************************************************************/
4334 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4337 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4338 HTREEITEM newFirstVisible = NULL;
4339 int visible_pos = -1;
4341 if (!TREEVIEW_ValidItem(infoPtr, item))
4344 if (!ISVISIBLE(item))
4346 /* Expand parents as necessary. */
4349 /* see if we are trying to ensure that root is vislble */
4350 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4351 parent = item->parent;
4353 parent = item; /* this item is the topmost item */
4355 while (parent != infoPtr->root)
4357 if (!(parent->state & TVIS_EXPANDED))
4358 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4360 parent = parent->parent;
4364 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4366 TRACE("%p (%s) %ld - %ld viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4367 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4369 if (hasFirstVisible)
4370 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4372 if (visible_pos < 0)
4374 /* item is before the start of the list: put it at the top. */
4375 newFirstVisible = item;
4377 else if (visible_pos >= viscount
4378 /* Sometimes, before we are displayed, GVC is 0, causing us to
4379 * spuriously scroll up. */
4380 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4382 /* item is past the end of the list. */
4383 int scroll = visible_pos - viscount;
4385 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4391 /* Scroll window so item's text is visible as much as possible */
4392 /* Calculation of amount of extra space is taken from EditLabel code */
4394 TEXTMETRICW textMetric;
4395 HDC hdc = GetWindowDC(infoPtr->hwnd);
4397 x = item->textWidth;
4399 GetTextMetricsW(hdc, &textMetric);
4400 ReleaseDC(infoPtr->hwnd, hdc);
4402 x += (textMetric.tmMaxCharWidth * 2);
4403 x = max(x, textMetric.tmMaxCharWidth * 3);
4405 if (item->textOffset < 0)
4406 pos = item->textOffset;
4407 else if (item->textOffset + x > infoPtr->clientWidth)
4409 if (x > infoPtr->clientWidth)
4410 pos = item->textOffset;
4412 pos = item->textOffset + x - infoPtr->clientWidth;
4417 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4420 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4422 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4431 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4432 TREEVIEW_ITEM *newFirstVisible,
4433 BOOL bUpdateScrollPos)
4437 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4439 if (newFirstVisible != NULL)
4441 /* Prevent an empty gap from appearing at the bottom... */
4442 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4443 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4447 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4450 /* ... unless we just don't have enough items. */
4451 if (newFirstVisible == NULL)
4452 newFirstVisible = infoPtr->root->firstChild;
4456 if (infoPtr->firstVisible != newFirstVisible)
4458 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4460 infoPtr->firstVisible = newFirstVisible;
4461 TREEVIEW_Invalidate(infoPtr, NULL);
4465 TREEVIEW_ITEM *item;
4466 int scroll = infoPtr->uItemHeight *
4467 (infoPtr->firstVisible->visibleOrder
4468 - newFirstVisible->visibleOrder);
4470 infoPtr->firstVisible = newFirstVisible;
4472 for (item = infoPtr->root->firstChild; item != NULL;
4473 item = TREEVIEW_GetNextListItem(infoPtr, item))
4475 item->rect.top += scroll;
4476 item->rect.bottom += scroll;
4479 if (bUpdateScrollPos)
4480 SetScrollPos(infoPtr->hwnd, SB_VERT,
4481 newFirstVisible->visibleOrder, TRUE);
4483 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4488 /************************************************************************
4489 * VScroll is always in units of visible items. i.e. we always have a
4490 * visible item aligned to the top of the control. (Unless we have no
4494 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4496 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4497 TREEVIEW_ITEM *newFirstVisible = NULL;
4499 int nScrollCode = LOWORD(wParam);
4501 TRACE("wp %x\n", wParam);
4503 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4506 if (infoPtr->hwndEdit)
4507 SetFocus(infoPtr->hwnd);
4509 if (!oldFirstVisible)
4511 assert(infoPtr->root->firstChild == NULL);
4515 switch (nScrollCode)
4518 newFirstVisible = infoPtr->root->firstChild;
4522 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4526 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4530 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4534 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4535 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4539 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4540 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4544 case SB_THUMBPOSITION:
4545 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4546 infoPtr->root->firstChild,
4547 (LONG)(SHORT)HIWORD(wParam));
4554 if (newFirstVisible != NULL)
4556 if (newFirstVisible != oldFirstVisible)
4557 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4558 nScrollCode != SB_THUMBTRACK);
4559 else if (nScrollCode == SB_THUMBPOSITION)
4560 SetScrollPos(infoPtr->hwnd, SB_VERT,
4561 newFirstVisible->visibleOrder, TRUE);
4568 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4571 int scrollX = infoPtr->scrollX;
4572 int nScrollCode = LOWORD(wParam);
4574 TRACE("wp %x\n", wParam);
4576 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4579 if (infoPtr->hwndEdit)
4580 SetFocus(infoPtr->hwnd);
4582 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4583 /* shall never occur */
4590 switch (nScrollCode)
4593 scrollX -= infoPtr->uItemHeight;
4596 scrollX += infoPtr->uItemHeight;
4599 scrollX -= infoPtr->clientWidth;
4602 scrollX += infoPtr->clientWidth;
4606 case SB_THUMBPOSITION:
4607 scrollX = (int)(SHORT)HIWORD(wParam);
4614 if (scrollX > maxWidth)
4616 else if (scrollX < 0)
4620 if (scrollX != infoPtr->scrollX)
4622 TREEVIEW_ITEM *item;
4623 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4625 for (item = infoPtr->root->firstChild; item != NULL;
4626 item = TREEVIEW_GetNextListItem(infoPtr, item))
4628 item->linesOffset += scroll_pixels;
4629 item->stateOffset += scroll_pixels;
4630 item->imageOffset += scroll_pixels;
4631 item->textOffset += scroll_pixels;
4634 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4635 infoPtr->scrollX = scrollX;
4636 UpdateWindow(infoPtr->hwnd);
4639 if (nScrollCode != SB_THUMBTRACK)
4640 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4646 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4649 UINT pulScrollLines = 3;
4651 if (infoPtr->firstVisible == NULL)
4654 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4656 gcWheelDelta = -(short)HIWORD(wParam);
4657 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4659 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4661 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4662 int maxDy = infoPtr->maxVisibleOrder;
4670 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4675 /* Create/Destroy *******************************************************/
4678 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4680 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
4682 TREEVIEW_INFO *infoPtr;
4684 TRACE("wnd %p, style %lx\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4686 infoPtr = (TREEVIEW_INFO *)Alloc(sizeof(TREEVIEW_INFO));
4688 if (infoPtr == NULL)
4690 ERR("could not allocate info memory!\n");
4694 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4696 infoPtr->hwnd = hwnd;
4697 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4698 infoPtr->uInternalStatus = 0;
4700 infoPtr->uNumItems = 0;
4701 infoPtr->cdmode = 0;
4702 infoPtr->uScrollTime = 300; /* milliseconds */
4703 infoPtr->bRedraw = TRUE;
4705 GetClientRect(hwnd, &rcClient);
4707 /* No scroll bars yet. */
4708 infoPtr->clientWidth = rcClient.right;
4709 infoPtr->clientHeight = rcClient.bottom;
4711 infoPtr->treeWidth = 0;
4712 infoPtr->treeHeight = 0;
4714 infoPtr->uIndent = MINIMUM_INDENT;
4715 infoPtr->selectedItem = 0;
4716 infoPtr->focusedItem = 0;
4718 infoPtr->firstVisible = 0;
4719 infoPtr->maxVisibleOrder = 0;
4720 infoPtr->dropItem = 0;
4721 infoPtr->insertMarkItem = 0;
4722 infoPtr->insertBeforeorAfter = 0;
4725 infoPtr->scrollX = 0;
4727 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4728 infoPtr->clrText = -1; /* use system color */
4729 infoPtr->clrLine = RGB(128, 128, 128);
4730 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4734 infoPtr->hwndEdit = 0;
4735 infoPtr->wpEditOrig = NULL;
4736 infoPtr->bIgnoreEditKillFocus = FALSE;
4737 infoPtr->bLabelChanged = FALSE;
4739 infoPtr->himlNormal = NULL;
4740 infoPtr->himlState = NULL;
4741 infoPtr->normalImageWidth = 0;
4742 infoPtr->normalImageHeight = 0;
4743 infoPtr->stateImageWidth = 0;
4744 infoPtr->stateImageHeight = 0;
4746 infoPtr->items = DPA_Create(16);
4748 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
4749 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4751 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4753 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4754 infoPtr->root->state = TVIS_EXPANDED;
4755 infoPtr->root->iLevel = -1;
4756 infoPtr->root->visibleOrder = -1;
4758 infoPtr->hwndNotify = lpcs->hwndParent;
4760 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4763 infoPtr->hwndToolTip = 0;
4765 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
4767 /* Determine what type of notify should be issued */
4768 /* sets infoPtr->bNtfUnicode */
4769 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
4771 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4772 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4774 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4777 HBITMAP hbm, hbmOld;
4781 infoPtr->himlState =
4782 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4784 hdcScreen = CreateDCW(szDisplayW, NULL, NULL, NULL);
4786 /* Create a coloured bitmap compatible with the screen depth
4787 because checkboxes are not black&white */
4788 hdc = CreateCompatibleDC(hdcScreen);
4789 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4790 hbmOld = SelectObject(hdc, hbm);
4792 rc.left = 0; rc.top = 0;
4793 rc.right = 48; rc.bottom = 16;
4794 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4796 rc.left = 18; rc.top = 2;
4797 rc.right = 30; rc.bottom = 14;
4798 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4799 DFCS_BUTTONCHECK|DFCS_FLAT);
4801 rc.left = 34; rc.right = 46;
4802 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4803 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4805 SelectObject(hdc, hbmOld);
4806 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4807 GetSysColor(COLOR_WINDOW));
4808 TRACE("checkbox index %d\n", nIndex);
4812 DeleteDC(hdcScreen);
4814 infoPtr->stateImageWidth = 16;
4815 infoPtr->stateImageHeight = 16;
4822 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4826 TREEVIEW_RemoveTree(infoPtr);
4828 /* tool tip is automatically destroyed: we are its owner */
4830 /* Restore original wndproc */
4831 if (infoPtr->hwndEdit)
4832 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
4833 (DWORD_PTR)infoPtr->wpEditOrig);
4835 /* Deassociate treeview from the window before doing anything drastic. */
4836 SetWindowLongPtrW(infoPtr->hwnd, 0, (DWORD_PTR)NULL);
4838 DeleteObject(infoPtr->hBoldFont);
4844 /* Miscellaneous Messages ***********************************************/
4847 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4855 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4856 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4857 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4858 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4859 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4860 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4861 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4862 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4863 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
4867 if (key >= VK_PRIOR && key <= VK_DOWN)
4869 unsigned char code = scroll[key - VK_PRIOR].code;
4871 (((code & (1 << 7)) == (SB_HORZ << 7))
4873 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
4879 /************************************************************************
4882 * VK_UP Move selection to the previous non-hidden item.
4883 * VK_DOWN Move selection to the next non-hidden item.
4884 * VK_HOME Move selection to the first item.
4885 * VK_END Move selection to the last item.
4886 * VK_LEFT If expanded then collapse, otherwise move to parent.
4887 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4889 * VK_SUBTRACT Collapse.
4890 * VK_MULTIPLY Expand all.
4891 * VK_PRIOR Move up GetVisibleCount items.
4892 * VK_NEXT Move down GetVisibleCount items.
4893 * VK_BACK Move to parent.
4894 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
4897 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4899 /* If it is non-NULL and different, it will be selected and visible. */
4900 TREEVIEW_ITEM *newSelection = NULL;
4902 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
4904 TRACE("%x\n", wParam);
4906 if (prevItem == NULL)
4909 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
4910 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
4915 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
4917 newSelection = infoPtr->root->firstChild;
4921 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
4925 newSelection = infoPtr->root->firstChild;
4929 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4933 if (prevItem->state & TVIS_EXPANDED)
4935 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4937 else if (prevItem->parent != infoPtr->root)
4939 newSelection = prevItem->parent;
4944 if (TREEVIEW_HasChildren(infoPtr, prevItem))
4946 if (!(prevItem->state & TVIS_EXPANDED))
4947 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4950 newSelection = prevItem->firstChild;
4957 TREEVIEW_ExpandAll(infoPtr, prevItem);
4961 if (!(prevItem->state & TVIS_EXPANDED))
4962 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4966 if (prevItem->state & TVIS_EXPANDED)
4967 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4972 = TREEVIEW_GetListItem(infoPtr, prevItem,
4973 -TREEVIEW_GetVisibleCount(infoPtr));
4978 = TREEVIEW_GetListItem(infoPtr, prevItem,
4979 TREEVIEW_GetVisibleCount(infoPtr));
4983 newSelection = prevItem->parent;
4984 if (newSelection == infoPtr->root)
4985 newSelection = NULL;
4989 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4990 TREEVIEW_ToggleItemState(infoPtr, prevItem);
4994 if (newSelection && newSelection != prevItem)
4996 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
4999 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5007 TREEVIEW_Notify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5009 LPNMHDR lpnmh = (LPNMHDR)lParam;
5011 if (lpnmh->code == PGN_CALCSIZE) {
5012 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5014 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5015 lppgc->iWidth = infoPtr->treeWidth;
5016 TRACE("got PGN_CALCSIZE, returning horz size = %ld, client=%ld\n",
5017 infoPtr->treeWidth, infoPtr->clientWidth);
5020 lppgc->iHeight = infoPtr->treeHeight;
5021 TRACE("got PGN_CALCSIZE, returning vert size = %ld, client=%ld\n",
5022 infoPtr->treeHeight, infoPtr->clientHeight);
5026 return DefWindowProcA(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5029 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
5033 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
5035 if (nCommand != NF_REQUERY) return 0;
5037 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5038 TRACE("format=%d\n", format);
5040 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
5042 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
5048 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5050 if (wParam == SIZE_RESTORED)
5052 infoPtr->clientWidth = (short)LOWORD(lParam);
5053 infoPtr->clientHeight = (short)HIWORD(lParam);
5055 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5056 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5057 TREEVIEW_UpdateScrollBars(infoPtr);
5061 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
5064 TREEVIEW_Invalidate(infoPtr, NULL);
5069 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5071 TRACE("(%x %lx)\n", wParam, lParam);
5073 if (wParam == GWL_STYLE)
5075 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5077 /* we have to take special care about tooltips */
5078 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5080 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5082 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5087 DestroyWindow(infoPtr->hwndToolTip);
5088 infoPtr->hwndToolTip = 0;
5092 infoPtr->dwStyle = dwNewStyle;
5095 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5096 TREEVIEW_UpdateScrollBars(infoPtr);
5097 TREEVIEW_Invalidate(infoPtr, NULL);
5103 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5107 if (!infoPtr->selectedItem)
5109 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5113 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5114 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5119 TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
5123 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5124 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5129 static LRESULT WINAPI
5130 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5132 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5134 TRACE("hwnd %p msg %04x wp=%08x lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5136 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5139 if (uMsg == WM_CREATE)
5140 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5147 case TVM_CREATEDRAGIMAGE:
5148 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5150 case TVM_DELETEITEM:
5151 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5153 case TVM_EDITLABELA:
5154 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5156 case TVM_EDITLABELW:
5157 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5159 case TVM_ENDEDITLABELNOW:
5160 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5162 case TVM_ENSUREVISIBLE:
5163 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5166 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5168 case TVM_GETBKCOLOR:
5169 return TREEVIEW_GetBkColor(infoPtr);
5172 return TREEVIEW_GetCount(infoPtr);
5174 case TVM_GETEDITCONTROL:
5175 return TREEVIEW_GetEditControl(infoPtr);
5177 case TVM_GETIMAGELIST:
5178 return TREEVIEW_GetImageList(infoPtr, wParam);
5181 return TREEVIEW_GetIndent(infoPtr);
5183 case TVM_GETINSERTMARKCOLOR:
5184 return TREEVIEW_GetInsertMarkColor(infoPtr);
5186 case TVM_GETISEARCHSTRINGA:
5187 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5190 case TVM_GETISEARCHSTRINGW:
5191 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5195 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5198 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5200 case TVM_GETITEMHEIGHT:
5201 return TREEVIEW_GetItemHeight(infoPtr);
5203 case TVM_GETITEMRECT:
5204 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5206 case TVM_GETITEMSTATE:
5207 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5209 case TVM_GETLINECOLOR:
5210 return TREEVIEW_GetLineColor(infoPtr);
5212 case TVM_GETNEXTITEM:
5213 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5215 case TVM_GETSCROLLTIME:
5216 return TREEVIEW_GetScrollTime(infoPtr);
5218 case TVM_GETTEXTCOLOR:
5219 return TREEVIEW_GetTextColor(infoPtr);
5221 case TVM_GETTOOLTIPS:
5222 return TREEVIEW_GetToolTips(infoPtr);
5224 case TVM_GETUNICODEFORMAT:
5225 return TREEVIEW_GetUnicodeFormat(infoPtr);
5227 case TVM_GETVISIBLECOUNT:
5228 return TREEVIEW_GetVisibleCount(infoPtr);
5231 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5233 case TVM_INSERTITEMA:
5234 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, FALSE);
5236 case TVM_INSERTITEMW:
5237 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam, TRUE);
5239 case TVM_SELECTITEM:
5240 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5242 case TVM_SETBKCOLOR:
5243 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5245 case TVM_SETIMAGELIST:
5246 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5249 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5251 case TVM_SETINSERTMARK:
5252 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5254 case TVM_SETINSERTMARKCOLOR:
5255 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5258 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, FALSE);
5261 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam, TRUE);
5263 case TVM_SETLINECOLOR:
5264 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5266 case TVM_SETITEMHEIGHT:
5267 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5269 case TVM_SETSCROLLTIME:
5270 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5272 case TVM_SETTEXTCOLOR:
5273 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5275 case TVM_SETTOOLTIPS:
5276 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5278 case TVM_SETUNICODEFORMAT:
5279 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5281 case TVM_SORTCHILDREN:
5282 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5284 case TVM_SORTCHILDRENCB:
5285 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5288 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5291 return TREEVIEW_Command(infoPtr, wParam, lParam);
5294 return TREEVIEW_Destroy(infoPtr);
5299 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5302 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5305 return TREEVIEW_GetFont(infoPtr);
5308 return TREEVIEW_HScroll(infoPtr, wParam);
5311 return TREEVIEW_KeyDown(infoPtr, wParam);
5314 return TREEVIEW_KillFocus(infoPtr);
5316 case WM_LBUTTONDBLCLK:
5317 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5319 case WM_LBUTTONDOWN:
5320 return TREEVIEW_LButtonDown(infoPtr, lParam);
5322 /* WM_MBUTTONDOWN */
5327 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5329 case WM_NOTIFYFORMAT:
5330 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5333 return TREEVIEW_Paint(infoPtr, wParam);
5335 /* WM_PRINTCLIENT */
5337 case WM_RBUTTONDOWN:
5338 return TREEVIEW_RButtonDown(infoPtr, lParam);
5341 return TREEVIEW_SetFocus(infoPtr);
5344 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5347 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5350 return TREEVIEW_Size(infoPtr, wParam, lParam);
5352 case WM_STYLECHANGED:
5353 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5355 /* WM_SYSCOLORCHANGE */
5360 return TREEVIEW_HandleTimer(infoPtr, wParam);
5363 return TREEVIEW_VScroll(infoPtr, wParam);
5365 /* WM_WININICHANGE */
5368 if (wParam & (MK_SHIFT | MK_CONTROL))
5370 return TREEVIEW_MouseWheel(infoPtr, wParam);
5373 TRACE("drawItem\n");
5377 /* This mostly catches MFC and Delphi messages. :( */
5378 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5379 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
5381 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
5387 /* Class Registration ***************************************************/
5390 TREEVIEW_Register(void)
5396 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
5397 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5398 wndClass.lpfnWndProc = (WNDPROC)TREEVIEW_WindowProc;
5399 wndClass.cbClsExtra = 0;
5400 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5402 wndClass.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
5403 wndClass.hbrBackground = 0;
5404 wndClass.lpszClassName = WC_TREEVIEWA;
5406 RegisterClassA(&wndClass);
5411 TREEVIEW_Unregister(void)
5413 UnregisterClassA(WC_TREEVIEWA, NULL);
5417 /* Tree Verification ****************************************************/
5421 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5423 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5424 TREEVIEW_ITEM *item)
5426 assert(infoPtr != NULL);
5427 assert(item != NULL);
5429 /* both NULL, or both non-null */
5430 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5432 assert(item->firstChild != item);
5433 assert(item->lastChild != item);
5435 if (item->firstChild)
5437 assert(item->firstChild->parent == item);
5438 assert(item->firstChild->prevSibling == NULL);
5441 if (item->lastChild)
5443 assert(item->lastChild->parent == item);
5444 assert(item->lastChild->nextSibling == NULL);
5447 assert(item->nextSibling != item);
5448 if (item->nextSibling)
5450 assert(item->nextSibling->parent == item->parent);
5451 assert(item->nextSibling->prevSibling == item);
5454 assert(item->prevSibling != item);
5455 if (item->prevSibling)
5457 assert(item->prevSibling->parent == item->parent);
5458 assert(item->prevSibling->nextSibling == item);
5463 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5465 assert(item != NULL);
5467 assert(item->parent != NULL);
5468 assert(item->parent != item);
5469 assert(item->iLevel == item->parent->iLevel + 1);
5471 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5473 TREEVIEW_VerifyItemCommon(infoPtr, item);
5475 TREEVIEW_VerifyChildren(infoPtr, item);
5479 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5481 TREEVIEW_ITEM *child;
5482 assert(item != NULL);
5484 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5485 TREEVIEW_VerifyItem(infoPtr, child);
5489 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5491 TREEVIEW_ITEM *root = infoPtr->root;
5493 assert(root != NULL);
5494 assert(root->iLevel == -1);
5495 assert(root->parent == NULL);
5496 assert(root->prevSibling == NULL);
5498 TREEVIEW_VerifyItemCommon(infoPtr, root);
5500 TREEVIEW_VerifyChildren(infoPtr, root);
5504 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5506 assert(infoPtr != NULL);
5508 TREEVIEW_VerifyRoot(infoPtr);