ReactOS 0.4.16-dev-92-g0c2cdca
treeview.c
Go to the documentation of this file.
1/* Treeview control
2 *
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
7 *
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.
12 *
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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 *
22 * NOTES
23 *
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
25 *
26 * Note2: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
29 *
30 * TODO:
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO
33 *
34 * missing styles: TVS_INFOTIP, TVS_RTLREADING,
35 *
36 * missing item styles: TVIS_EXPANDPARTIAL, TVIS_EX_FLAT,
37 * TVIS_EX_DISABLED
38 *
39 * Make the insertion mark look right.
40 * Scroll (instead of repaint) as much as possible.
41 */
42
43#include <assert.h>
44#include <ctype.h>
45#include <stdarg.h>
46#include <string.h>
47#include <limits.h>
48#include <stdlib.h>
49
50#define NONAMELESSUNION
51
52#include "windef.h"
53#include "winbase.h"
54#include "wingdi.h"
55#include "winuser.h"
56#include "winnls.h"
57#include "commctrl.h"
58#include "comctl32.h"
59#include "uxtheme.h"
60#include "vssym32.h"
61#include "wine/debug.h"
62#include "wine/exception.h"
63#include "wine/heap.h"
64
66
67/* internal structures */
68typedef struct tagTREEVIEW_INFO
69{
71 HWND hwndNotify; /* Owner window to send notifications to */
76 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
77 INT cdmode; /* last custom draw setting */
78 UINT uScrollTime; /* max. time for scrolling in milliseconds */
79 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
80
81 UINT uItemHeight; /* item height */
83
84 LONG clientWidth; /* width of control window */
85 LONG clientHeight; /* height of control window */
86
87 LONG treeWidth; /* width of visible tree items */
88 LONG treeHeight; /* height of visible tree items */
89
90 UINT uIndent; /* indentation in pixels */
91 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
92 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
93 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
94 HTREEITEM editItem; /* item being edited with builtin edit box */
95
96 HTREEITEM firstVisible; /* handle to item whose top edge is at y = 0 */
98 HTREEITEM dropItem; /* handle to item selected by drag cursor */
99 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
100 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
101 HIMAGELIST dragList; /* Bitmap of dragged item */
115
117 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
120
121 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
129
135
136typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
137{
138 HTREEITEM parent; /* handle to parent or 0 if at root */
139 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
140 HTREEITEM firstChild; /* handle to first child or 0 if no child */
141
152 int iIntegral; /* item height multiplier (1 is normal) */
153 int iLevel; /* indentation level:0=root level */
155 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
161 LONG textWidth; /* horizontal text extent for pszText */
162 LONG visibleOrder; /* Depth-first numbering of the items whose ancestors are all expanded,
163 corresponding to a top-to-bottom ordering in the tree view.
164 Each item takes up "item.iIntegral" spots in the visible order.
165 0 is the root's first child. */
166 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
168
169/******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
170#define KEY_DELAY 450
171
172/* bitflags for infoPtr->uInternalStatus */
173
174#define TV_HSCROLL 0x01 /* treeview too large to fit in window */
175#define TV_VSCROLL 0x02 /* (horizontal/vertical) */
176#define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
177#define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
178#define TV_RDRAG 0x10 /* ditto Rbutton */
179#define TV_RDRAGGING 0x20
180
181/* bitflags for infoPtr->timer */
182
183#define TV_EDIT_TIMER 2
184#define TV_EDIT_TIMER_SET 2
185
186#define TEXT_CALLBACK_SIZE 260
187
188#define TREEVIEW_LEFT_MARGIN 8
189
190#define MINIMUM_INDENT 19
191
192#define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
193
194#define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
195#define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
196#define ISVISIBLE(x) ((x)->visibleOrder >= 0)
197
198#define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
199#define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
200#define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
201#define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
202
203static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
204
205
207
208
209static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
210
214static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
217
218/* Random Utilities *****************************************************/
219static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
220
221/* Returns the treeview private data if hwnd is a treeview.
222 * Otherwise returns an undefined value. */
223static inline TREEVIEW_INFO *
225{
227}
228
229/* Don't call this. Nothing wants an item index. */
230static inline int
232{
233 return DPA_GetPtrIndex(infoPtr->items, handle);
234}
235
236/* Checks if item has changed and needs to be redrawn */
237static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
238 const TVITEMEXW *tvChange)
239{
240 /* Number of children has changed */
241 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
242 return TRUE;
243
244 /* Image has changed and it's not a callback */
245 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
246 tiNew->iImage != I_IMAGECALLBACK)
247 return TRUE;
248
249 /* Selected image has changed and it's not a callback */
250 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
252 return TRUE;
253
254 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
256 return TRUE;
257
258 /* Text has changed and it's not a callback */
259 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
261 return TRUE;
262
263 /* Indent has changed */
264 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
265 return TRUE;
266
267 /* Item state has changed */
268 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
269 return TRUE;
270
271 return FALSE;
272}
273
274/***************************************************************************
275 * This method checks that handle is an item for this tree.
276 */
277static BOOL
279{
280 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
281 {
282 TRACE("invalid item %p\n", handle);
283 return FALSE;
284 }
285 else
286 return TRUE;
287}
288
289static HFONT
291{
293
294 GetObjectW(hOrigFont, sizeof(font), &font);
295 font.lfWeight = FW_BOLD;
296 return CreateFontIndirectW(&font);
297}
298
299static HFONT
301{
303
304 GetObjectW(hOrigFont, sizeof(font), &font);
305 font.lfUnderline = TRUE;
306 return CreateFontIndirectW(&font);
307}
308
309static HFONT
311{
313
314 GetObjectW(hfont, sizeof(font), &font);
315 font.lfWeight = FW_BOLD;
316 font.lfUnderline = TRUE;
317 return CreateFontIndirectW(&font);
318}
319
320static inline HFONT
322{
323 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
324 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
325 if (item->state & TVIS_BOLD)
326 return infoPtr->hBoldFont;
327 return infoPtr->hFont;
328}
329
330/* for trace/debugging purposes only */
331static const char *
333{
334 if (item == NULL) return "<null item>";
335 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
336 if (item->pszText == NULL) return "<null>";
337 return debugstr_w(item->pszText);
338}
339
340/* An item is not a child of itself. */
341static BOOL
343{
344 do
345 {
346 child = child->parent;
347 if (child == parent) return TRUE;
348 } while (child != NULL);
349
350 return FALSE;
351}
352
353static BOOL
355{
356 return !(infoPtr->dwStyle & TVS_HASLINES) && (infoPtr->dwStyle & TVS_FULLROWSELECT);
357}
358
359static BOOL
361{
362 if (TREEVIEW_IsFullRowSelect(infoPtr))
364 else
365 return ht->flags & TVHT_ONITEM;
366}
367
368/* Tree Traversal *******************************************************/
369
370/***************************************************************************
371 * This method returns the last expanded sibling or child child item
372 * of a tree node
373 */
374static TREEVIEW_ITEM *
376{
377 if (!item) return NULL;
378
379 while (item->lastChild)
380 {
381 if (item->state & TVIS_EXPANDED)
382 item = item->lastChild;
383 else
384 break;
385 }
386
387 if (item == infoPtr->root)
388 return NULL;
389
390 return item;
391}
392
393/***************************************************************************
394 * This method returns the previous non-hidden item in the list not
395 * considering the tree hierarchy.
396 */
397static TREEVIEW_ITEM *
399{
400 if (tvItem->prevSibling)
401 {
402 /* This item has a prevSibling, get the last item in the sibling's tree. */
403 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
404
405 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
406 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
407 else
408 return upItem;
409 }
410 else
411 {
412 /* this item does not have a prevSibling, get the parent */
413 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
414 }
415}
416
417
418/***************************************************************************
419 * This method returns the next physical item in the treeview not
420 * considering the tree hierarchy.
421 */
422static TREEVIEW_ITEM *
424{
425 /*
426 * If this item has children and is expanded, return the first child
427 */
428 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
429 {
430 return tvItem->firstChild;
431 }
432
433
434 /*
435 * try to get the sibling
436 */
437 if (tvItem->nextSibling)
438 return tvItem->nextSibling;
439
440 /*
441 * Otherwise, get the parent's sibling.
442 */
443 while (tvItem->parent)
444 {
445 tvItem = tvItem->parent;
446
447 if (tvItem->nextSibling)
448 return tvItem->nextSibling;
449 }
450
451 return NULL;
452}
453
454/***************************************************************************
455 * This method returns the nth item starting at the given item. It returns
456 * the last item (or first) we we run out of items.
457 *
458 * Will scroll backward if count is <0.
459 * forward if count is >0.
460 */
461static TREEVIEW_ITEM *
463 LONG count)
464{
465 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
466 TREEVIEW_ITEM *previousItem;
467
468 assert(item != NULL);
469
470 if (count > 0)
471 {
472 next_item = TREEVIEW_GetNextListItem;
473 }
474 else if (count < 0)
475 {
476 count = -count;
477 next_item = TREEVIEW_GetPrevListItem;
478 }
479 else
480 return item;
481
482 do
483 {
484 previousItem = item;
485 item = next_item(infoPtr, item);
486
487 } while (--count && item != NULL);
488
489
490 return item ? item : previousItem;
491}
492
493/* Notifications ************************************************************/
494
495static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
496{
497 if (!infoPtr->bNtfUnicode) {
498 switch (code) {
500 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
505 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
506 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
507 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
510 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
511 }
512 }
513 return code;
514}
515
516static inline BOOL
518{
519 TRACE("code=%d, hdr=%p\n", code, hdr);
520
521 hdr->hwndFrom = infoPtr->hwnd;
522 hdr->idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
523 hdr->code = get_notifycode(infoPtr, code);
524
525 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, hdr->idFrom, (LPARAM)hdr);
526}
527
528static BOOL
530{
531 NMHDR hdr;
532 return TREEVIEW_SendRealNotify(infoPtr, code, &hdr);
533}
534
535static VOID
537{
538 tvItem->mask = mask;
539 tvItem->hItem = item;
540 tvItem->state = item->state;
541 tvItem->stateMask = 0;
542 tvItem->iImage = item->iImage;
543 tvItem->iSelectedImage = item->iSelectedImage;
544 tvItem->cChildren = item->cChildren;
545 tvItem->lParam = item->lParam;
546
547 if(mask & TVIF_TEXT)
548 {
549 if (!infoPtr->bNtfUnicode)
550 {
551 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
552 tvItem->pszText = heap_alloc (tvItem->cchTextMax);
553 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
554 }
555 else
556 {
557 tvItem->cchTextMax = item->cchTextMax;
558 tvItem->pszText = item->pszText;
559 }
560 }
561 else
562 {
563 tvItem->cchTextMax = 0;
564 tvItem->pszText = NULL;
565 }
566}
567
568static BOOL
570 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
571{
572 NMTREEVIEWW nmhdr;
573 BOOL ret;
574
575 TRACE("code:%d action:0x%x olditem:%p newitem:%p\n",
576 code, action, oldItem, newItem);
577
578 memset(&nmhdr, 0, sizeof(NMTREEVIEWW));
579 nmhdr.action = action;
580
581 if (oldItem)
582 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
583
584 if (newItem)
585 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
586
587 nmhdr.ptDrag.x = 0;
588 nmhdr.ptDrag.y = 0;
589
590 ret = TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
591 if (!infoPtr->bNtfUnicode)
592 {
595 }
596 return ret;
597}
598
599static BOOL
601 HTREEITEM dragItem, POINT pt)
602{
603 NMTREEVIEWW nmhdr;
604
605 TRACE("code:%d dragitem:%p\n", code, dragItem);
606
607 nmhdr.action = 0;
609 nmhdr.itemNew.hItem = dragItem;
610 nmhdr.itemNew.state = dragItem->state;
611 nmhdr.itemNew.lParam = dragItem->lParam;
612
613 nmhdr.ptDrag.x = pt.x;
614 nmhdr.ptDrag.y = pt.y;
615
616 return TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
617}
618
619
620static BOOL
622 HDC hdc, RECT rc)
623{
624 NMTVCUSTOMDRAW nmcdhdr;
625 NMCUSTOMDRAW *nmcd;
626
627 TRACE("drawstage:0x%x hdc:%p\n", dwDrawStage, hdc);
628
629 nmcd = &nmcdhdr.nmcd;
630 nmcd->dwDrawStage = dwDrawStage;
631 nmcd->hdc = hdc;
632 nmcd->rc = rc;
633 nmcd->dwItemSpec = 0;
634 nmcd->uItemState = 0;
635 nmcd->lItemlParam = 0;
636 nmcdhdr.clrText = infoPtr->clrText;
637 nmcdhdr.clrTextBk = infoPtr->clrBk;
638 nmcdhdr.iLevel = 0;
639
640 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr.nmcd.hdr);
641}
642
643/* FIXME: need to find out when the flags in uItemState need to be set */
644
645static BOOL
647 TREEVIEW_ITEM *item, UINT uItemDrawState,
648 NMTVCUSTOMDRAW *nmcdhdr)
649{
650 NMCUSTOMDRAW *nmcd;
651 DWORD dwDrawStage;
652 DWORD_PTR dwItemSpec;
653 UINT uItemState;
654
655 dwDrawStage = CDDS_ITEM | uItemDrawState;
656 dwItemSpec = (DWORD_PTR)item;
657 uItemState = 0;
658 if (item->state & TVIS_SELECTED)
659 uItemState |= CDIS_SELECTED;
660 if (item == infoPtr->selectedItem)
661 uItemState |= CDIS_FOCUS;
662 if (item == infoPtr->hotItem)
663 uItemState |= CDIS_HOT;
664
665 nmcd = &nmcdhdr->nmcd;
666 nmcd->dwDrawStage = dwDrawStage;
667 nmcd->hdc = hdc;
668 nmcd->rc = item->rect;
669 nmcd->dwItemSpec = dwItemSpec;
670 nmcd->uItemState = uItemState;
671 nmcd->lItemlParam = item->lParam;
672 nmcdhdr->iLevel = item->iLevel;
673
674 TRACE("drawstage:0x%x hdc:%p item:%lx, itemstate:0x%x, lItemlParam:0x%lx\n",
675 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
676 nmcd->uItemState, nmcd->lItemlParam);
677
678 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr->nmcd.hdr);
679}
680
681static BOOL
683{
684 NMTVDISPINFOW tvdi;
685 BOOL ret;
686
688 &tvdi.item, editItem);
689
691
692 if (!infoPtr->bNtfUnicode)
693 heap_free(tvdi.item.pszText);
694
695 return ret;
696}
697
698static void
700 UINT mask)
701{
702 NMTVDISPINFOEXW callback;
703
704 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
705 mask &= item->callbackMask;
706
707 if (mask == 0) return;
708
709 /* 'state' always contains valid value, as well as 'lParam'.
710 * All other parameters are uninitialized.
711 */
712 callback.item.pszText = item->pszText;
713 callback.item.cchTextMax = item->cchTextMax;
714 callback.item.mask = mask;
715 callback.item.hItem = item;
716 callback.item.state = item->state;
717 callback.item.lParam = item->lParam;
718
719 /* If text is changed we need to recalculate textWidth */
720 if (mask & TVIF_TEXT)
721 item->textWidth = 0;
722
724 TRACE("resulting code 0x%08x\n", callback.hdr.code);
725
726 /* It may have changed due to a call to SetItem. */
727 mask &= item->callbackMask;
728
729 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
730 {
731 /* Instead of copying text into our buffer user specified his own */
732 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
733 LPWSTR newText;
734 int buflen;
736 (LPSTR)callback.item.pszText, -1,
737 NULL, 0);
738 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
739 newText = heap_realloc(item->pszText, buflen);
740
741 TRACE("returned str %s, len=%d, buflen=%d\n",
742 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
743
744 if (newText)
745 {
746 item->pszText = newText;
748 (LPSTR)callback.item.pszText, -1,
749 item->pszText, buflen/sizeof(WCHAR));
750 item->cchTextMax = buflen/sizeof(WCHAR);
751 }
752 /* If realloc fails we have nothing to do, but keep original text */
753 }
754 else {
755 int len = max(lstrlenW(callback.item.pszText) + 1,
757 LPWSTR newText = heap_realloc(item->pszText, len*sizeof(WCHAR));
758
759 TRACE("returned wstr %s, len=%d\n",
760 debugstr_w(callback.item.pszText), len);
761
762 if (newText)
763 {
764 item->pszText = newText;
765 lstrcpyW(item->pszText, callback.item.pszText);
766 item->cchTextMax = len;
767 }
768 /* If realloc fails we have nothing to do, but keep original text */
769 }
770 }
771 else if (mask & TVIF_TEXT) {
772 /* User put text into our buffer, that is ok unless A string */
773 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
774 LPWSTR newText;
775 int buflen;
777 (LPSTR)callback.item.pszText, -1,
778 NULL, 0);
779 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
780 newText = heap_alloc(buflen);
781
782 TRACE("same buffer str %s, len=%d, buflen=%d\n",
783 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
784
785 if (newText)
786 {
787 LPWSTR oldText = item->pszText;
788 item->pszText = newText;
790 (LPSTR)callback.item.pszText, -1,
791 item->pszText, buflen/sizeof(WCHAR));
792 item->cchTextMax = buflen/sizeof(WCHAR);
793 heap_free(oldText);
794 }
795 }
796 }
797
798 if (mask & TVIF_IMAGE)
799 item->iImage = callback.item.iImage;
800
802 item->iSelectedImage = callback.item.iSelectedImage;
803
805 item->iExpandedImage = callback.item.iExpandedImage;
806
807 if (mask & TVIF_CHILDREN)
808 item->cChildren = callback.item.cChildren;
809
810 if (callback.item.mask & TVIF_STATE)
811 {
812 item->state &= ~callback.item.stateMask;
813 item->state |= (callback.item.state & callback.item.stateMask);
814 }
815
816 /* These members are now permanently set. */
817 if (callback.item.mask & TVIF_DI_SETITEM)
818 item->callbackMask &= ~callback.item.mask;
819}
820
821/***************************************************************************
822 * This function uses cChildren field to decide whether the item has
823 * children or not.
824 * Note: if this returns TRUE, the child items may not actually exist,
825 * they could be virtual.
826 *
827 * Just use item->firstChild to check for physical children.
828 */
829static BOOL
831{
833 /* Protect for a case when callback field is not changed by a host,
834 otherwise negative values trigger normal notifications. */
835 return item->cChildren != 0 && item->cChildren != I_CHILDRENCALLBACK;
836}
837
838static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
839{
840 INT format;
841
842 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
843
844 if (nCommand != NF_REQUERY) return 0;
845
846 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
847 TRACE("format=%d\n", format);
848
849 /* Invalid format returned by NF_QUERY defaults to ANSI*/
850 if (format != NFR_ANSI && format != NFR_UNICODE)
852
853 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
854
855 return format;
856}
857
858/* Item Position ********************************************************/
859
860/* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
861static VOID
863{
864 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
867
868 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
869 - infoPtr->scrollX;
870 item->stateOffset = item->linesOffset + infoPtr->uIndent;
871 item->imageOffset = item->stateOffset
872 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
873 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
874}
875
876static VOID
878{
879 HDC hdc;
880 HFONT hOldFont=0;
881 SIZE sz;
882
883 /* DRAW's OM docker creates items like this */
884 if (item->pszText == NULL)
885 {
886 item->textWidth = 0;
887 return;
888 }
889
890 if (hDC != 0)
891 {
892 hdc = hDC;
893 }
894 else
895 {
896 hdc = GetDC(infoPtr->hwnd);
897 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
898 }
899
900 GetTextExtentPoint32W(hdc, item->pszText, lstrlenW(item->pszText), &sz);
901 item->textWidth = sz.cx;
902
903 if (hDC == 0)
904 {
905 SelectObject(hdc, hOldFont);
906 ReleaseDC(0, hdc);
907 }
908}
909
910static VOID
912{
913 item->rect.top = infoPtr->uItemHeight *
914 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
915
916 item->rect.bottom = item->rect.top
917 + infoPtr->uItemHeight * item->iIntegral - 1;
918
919 item->rect.left = 0;
920 item->rect.right = infoPtr->clientWidth;
921}
922
923/* We know that only items after start need their order updated. */
924static void
926{
928 int order;
929
930 if (!start)
931 {
932 start = infoPtr->root->firstChild;
933 order = 0;
934 }
935 else
937
938 for (item = start; item != NULL;
940 {
941 if (!ISVISIBLE(item) && order > 0)
943 item->visibleOrder = order;
944 order += item->iIntegral;
945 }
946
947 infoPtr->maxVisibleOrder = order;
948
949 for (item = infoPtr->root->firstChild; item != NULL;
951 {
953 }
954}
955
956
957/* Update metrics of all items in selected subtree.
958 * root must be expanded
959 */
960static VOID
962{
963 TREEVIEW_ITEM *sibling;
964 HDC hdc;
965 HFONT hOldFont;
966
967 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
968 return;
969
970 root->state &= ~TVIS_EXPANDED;
971 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
972 root->state |= TVIS_EXPANDED;
973
974 hdc = GetDC(infoPtr->hwnd);
975 hOldFont = SelectObject(hdc, infoPtr->hFont);
976
977 for (; root != sibling;
979 {
981
982 if (root->callbackMask & TVIF_TEXT)
984
985 if (root->textWidth == 0)
986 {
989 }
990 }
991
992 SelectObject(hdc, hOldFont);
993 ReleaseDC(infoPtr->hwnd, hdc);
994}
995
996/* Item Allocation **********************************************************/
997
998static TREEVIEW_ITEM *
1000{
1001 TREEVIEW_ITEM *newItem = heap_alloc_zero(sizeof(*newItem));
1002
1003 if (!newItem)
1004 return NULL;
1005
1006 /* I_IMAGENONE would make more sense but this is neither what is
1007 * documented (MSDN doesn't specify) nor what Windows actually does
1008 * (it sets it to zero)... and I can so imagine an application using
1009 * inc/dec to toggle the images. */
1010 newItem->iImage = 0;
1011 newItem->iSelectedImage = 0;
1012 newItem->iExpandedImage = (WORD)I_IMAGENONE;
1013 newItem->infoPtr = infoPtr;
1014
1015 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1016 {
1017 heap_free(newItem);
1018 return NULL;
1019 }
1020
1021 return newItem;
1022}
1023
1024/* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1025 * free item->pszText. */
1026static void
1028{
1029 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1030 if (infoPtr->selectedItem == item)
1031 infoPtr->selectedItem = NULL;
1032 if (infoPtr->hotItem == item)
1033 infoPtr->hotItem = NULL;
1034 if (infoPtr->focusedItem == item)
1035 infoPtr->focusedItem = NULL;
1036 if (infoPtr->firstVisible == item)
1037 infoPtr->firstVisible = NULL;
1038 if (infoPtr->dropItem == item)
1039 infoPtr->dropItem = NULL;
1040 if (infoPtr->insertMarkItem == item)
1041 infoPtr->insertMarkItem = NULL;
1042 heap_free(item);
1043}
1044
1045
1046/* Item Insertion *******************************************************/
1047
1048/***************************************************************************
1049 * This method inserts newItem before sibling as a child of parent.
1050 * sibling can be NULL, but only if parent has no children.
1051 */
1052static void
1055{
1056 assert(parent != NULL);
1057
1058 if (sibling != NULL)
1059 {
1060 assert(sibling->parent == parent);
1061
1062 if (sibling->prevSibling != NULL)
1063 sibling->prevSibling->nextSibling = newItem;
1064
1065 newItem->prevSibling = sibling->prevSibling;
1066 sibling->prevSibling = newItem;
1067 }
1068 else
1069 newItem->prevSibling = NULL;
1070
1071 newItem->nextSibling = sibling;
1072
1073 if (parent->firstChild == sibling)
1074 parent->firstChild = newItem;
1075
1076 if (parent->lastChild == NULL)
1077 parent->lastChild = newItem;
1078}
1079
1080/***************************************************************************
1081 * This method inserts newItem after sibling as a child of parent.
1082 * sibling can be NULL, but only if parent has no children.
1083 */
1084static void
1087{
1088 assert(parent != NULL);
1089
1090 if (sibling != NULL)
1091 {
1092 assert(sibling->parent == parent);
1093
1094 if (sibling->nextSibling != NULL)
1095 sibling->nextSibling->prevSibling = newItem;
1096
1097 newItem->nextSibling = sibling->nextSibling;
1098 sibling->nextSibling = newItem;
1099 }
1100 else
1101 newItem->nextSibling = NULL;
1102
1103 newItem->prevSibling = sibling;
1104
1105 if (parent->lastChild == sibling)
1106 parent->lastChild = newItem;
1107
1108 if (parent->firstChild == NULL)
1109 parent->firstChild = newItem;
1110}
1111
1112static BOOL
1114 const TVITEMEXW *tvItem, BOOL isW)
1115{
1116 UINT callbackClear = 0;
1117 UINT callbackSet = 0;
1118
1119 TRACE("item %p\n", item);
1120 /* Do this first in case it fails. */
1121 if (tvItem->mask & TVIF_TEXT)
1122 {
1123 item->textWidth = 0; /* force width recalculation */
1124
1125 /* Covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1126 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL)
1127 {
1128 int len;
1129 LPWSTR newText;
1130 if (isW)
1131 len = lstrlenW(tvItem->pszText) + 1;
1132 else
1133 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1134
1135 /* Allocate new block to make pointer comparison in item_changed() work. */
1136 newText = heap_alloc(len * sizeof(WCHAR));
1137
1138 if (newText == NULL) return FALSE;
1139
1140 callbackClear |= TVIF_TEXT;
1141
1142 heap_free(item->pszText);
1143 item->pszText = newText;
1144 item->cchTextMax = len;
1145 if (isW)
1146 lstrcpynW(item->pszText, tvItem->pszText, len);
1147 else
1148 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1149 item->pszText, len);
1150
1151 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1152 }
1153 else
1154 {
1155 callbackSet |= TVIF_TEXT;
1156 item->pszText = heap_realloc(item->pszText, TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1157 item->cchTextMax = TEXT_CALLBACK_SIZE;
1158 TRACE("setting callback, item %p\n", item);
1159 }
1160 }
1161
1162 if (tvItem->mask & TVIF_CHILDREN)
1163 {
1164 item->cChildren = tvItem->cChildren;
1165
1166 if (item->cChildren == I_CHILDRENCALLBACK)
1167 callbackSet |= TVIF_CHILDREN;
1168 else
1169 callbackClear |= TVIF_CHILDREN;
1170 }
1171
1172 if (tvItem->mask & TVIF_IMAGE)
1173 {
1174 item->iImage = tvItem->iImage;
1175
1176 if (item->iImage == I_IMAGECALLBACK)
1177 callbackSet |= TVIF_IMAGE;
1178 else
1179 callbackClear |= TVIF_IMAGE;
1180 }
1181
1182 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1183 {
1184 item->iSelectedImage = tvItem->iSelectedImage;
1185
1186 if (item->iSelectedImage == I_IMAGECALLBACK)
1187 callbackSet |= TVIF_SELECTEDIMAGE;
1188 else
1189 callbackClear |= TVIF_SELECTEDIMAGE;
1190 }
1191
1192 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1193 {
1194 item->iExpandedImage = tvItem->iExpandedImage;
1195
1196 if (item->iExpandedImage == I_IMAGECALLBACK)
1197 callbackSet |= TVIF_EXPANDEDIMAGE;
1198 else
1199 callbackClear |= TVIF_EXPANDEDIMAGE;
1200 }
1201
1202 if (tvItem->mask & TVIF_PARAM)
1203 item->lParam = tvItem->lParam;
1204
1205 /* If the application sets TVIF_INTEGRAL without
1206 * supplying a TVITEMEX structure, it's toast. */
1207 if (tvItem->mask & TVIF_INTEGRAL)
1208 item->iIntegral = tvItem->iIntegral;
1209
1210 if (tvItem->mask & TVIF_STATE)
1211 {
1212 TRACE("prevstate 0x%x, state 0x%x, mask 0x%x\n", item->state, tvItem->state,
1213 tvItem->stateMask);
1214 item->state &= ~tvItem->stateMask;
1215 item->state |= (tvItem->state & tvItem->stateMask);
1216 }
1217
1218 if (tvItem->mask & TVIF_STATEEX)
1219 {
1220 FIXME("New extended state: 0x%x\n", tvItem->uStateEx);
1221 }
1222
1223 item->callbackMask |= callbackSet;
1224 item->callbackMask &= ~callbackClear;
1225
1226 return TRUE;
1227}
1228
1229/* Note that the new item is pre-zeroed. */
1230static LRESULT
1232{
1233 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1234 HTREEITEM insertAfter;
1235 TREEVIEW_ITEM *newItem, *parentItem;
1236 BOOL bTextUpdated = FALSE;
1237
1238 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1239 {
1240 parentItem = infoPtr->root;
1241 }
1242 else
1243 {
1244 parentItem = ptdi->hParent;
1245
1246 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1247 {
1248 WARN("invalid parent %p\n", parentItem);
1249 return 0;
1250 }
1251 }
1252
1253 insertAfter = ptdi->hInsertAfter;
1254
1255 /* Validate this now for convenience. */
1256 switch ((DWORD_PTR)insertAfter)
1257 {
1258 case (DWORD_PTR)TVI_FIRST:
1259 case (DWORD_PTR)TVI_LAST:
1260 case (DWORD_PTR)TVI_SORT:
1261 break;
1262
1263 default:
1264 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1265 insertAfter->parent != parentItem)
1266 {
1267 WARN("invalid insert after %p\n", insertAfter);
1268 insertAfter = TVI_LAST;
1269 }
1270 }
1271
1272 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1273 (tvItem->mask & TVIF_TEXT)
1274 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1275 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1276 : "<no label>");
1277
1278 newItem = TREEVIEW_AllocateItem(infoPtr);
1279 if (newItem == NULL)
1280 return 0;
1281
1282 newItem->parent = parentItem;
1283 newItem->iIntegral = 1;
1284 newItem->visibleOrder = -1;
1285
1286 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1287 return 0;
1288
1289 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1290
1291 infoPtr->uNumItems++;
1292
1293 switch ((DWORD_PTR)insertAfter)
1294 {
1295 case (DWORD_PTR)TVI_FIRST:
1296 {
1297 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1298 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1299 if (infoPtr->firstVisible == originalFirst)
1300 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1301 }
1302 break;
1303
1304 case (DWORD_PTR)TVI_LAST:
1305 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1306 break;
1307
1308 /* hInsertAfter names a specific item we want to insert after */
1309 default:
1310 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1311 break;
1312
1313 case (DWORD_PTR)TVI_SORT:
1314 {
1315 TREEVIEW_ITEM *aChild;
1316 TREEVIEW_ITEM *previousChild = NULL;
1317 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1318 BOOL bItemInserted = FALSE;
1319
1320 aChild = parentItem->firstChild;
1321
1322 bTextUpdated = TRUE;
1323 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1324
1325 /* Iterate the parent children to see where we fit in */
1326 while (aChild != NULL)
1327 {
1328 INT comp;
1329
1330 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1331 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1332
1333 if (comp < 0) /* we are smaller than the current one */
1334 {
1335 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1336 if (infoPtr->firstVisible == originalFirst &&
1337 aChild == originalFirst)
1338 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1339 bItemInserted = TRUE;
1340 break;
1341 }
1342 else if (comp > 0) /* we are bigger than the current one */
1343 {
1344 previousChild = aChild;
1345
1346 /* This will help us to exit if there is no more sibling */
1347 aChild = (aChild->nextSibling == 0)
1348 ? NULL
1349 : aChild->nextSibling;
1350
1351 /* Look at the next item */
1352 continue;
1353 }
1354 else if (comp == 0)
1355 {
1356 /*
1357 * An item with this name is already existing, therefore,
1358 * we add after the one we found
1359 */
1360 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1361 bItemInserted = TRUE;
1362 break;
1363 }
1364 }
1365
1366 /*
1367 * we reach the end of the child list and the item has not
1368 * yet been inserted, therefore, insert it after the last child.
1369 */
1370 if ((!bItemInserted) && (aChild == NULL))
1371 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1372
1373 break;
1374 }
1375 }
1376
1377
1378 TRACE("new item %p; parent %p, mask 0x%x\n", newItem,
1379 newItem->parent, tvItem->mask);
1380
1381 newItem->iLevel = newItem->parent->iLevel + 1;
1382
1383 if (newItem->parent->cChildren == 0)
1384 newItem->parent->cChildren = 1;
1385
1386 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1387 {
1388 if (STATEIMAGEINDEX(newItem->state) == 0)
1389 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1390 }
1391
1392 if (infoPtr->firstVisible == NULL)
1393 infoPtr->firstVisible = newItem;
1394
1395 TREEVIEW_VerifyTree(infoPtr);
1396
1397 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1398
1399 if (parentItem == infoPtr->root ||
1400 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1401 {
1403 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1404
1405 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1406 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1407
1408 if (!bTextUpdated)
1409 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1410
1411 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1413 /*
1414 * if the item was inserted in a visible part of the tree,
1415 * invalidate it, as well as those after it
1416 */
1417 for (item = newItem;
1418 item != NULL;
1420 TREEVIEW_Invalidate(infoPtr, item);
1421 }
1422 else
1423 {
1424 /* refresh treeview if newItem is the first item inserted under parentItem */
1425 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1426 {
1427 /* parent got '+' - update it */
1428 TREEVIEW_Invalidate(infoPtr, parentItem);
1429 }
1430 }
1431
1432 return (LRESULT)newItem;
1433}
1434
1435/* Item Deletion ************************************************************/
1436static void
1438
1439static void
1441{
1442 TREEVIEW_ITEM *kill = parentItem->firstChild;
1443
1444 while (kill != NULL)
1445 {
1447
1448 TREEVIEW_RemoveItem(infoPtr, kill);
1449
1450 kill = next;
1451 }
1452
1453 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1454 assert(parentItem->firstChild == NULL);
1455 assert(parentItem->lastChild == NULL);
1456}
1457
1458static void
1460{
1461 TREEVIEW_ITEM *parentItem;
1462
1463 assert(item != NULL);
1464 assert(item->parent != NULL); /* i.e. it must not be the root */
1465
1466 parentItem = item->parent;
1467
1468 if (parentItem->firstChild == item)
1469 parentItem->firstChild = item->nextSibling;
1470
1471 if (parentItem->lastChild == item)
1472 parentItem->lastChild = item->prevSibling;
1473
1474 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1475 && parentItem->cChildren > 0)
1476 parentItem->cChildren = 0;
1477
1478 if (item->prevSibling)
1479 item->prevSibling->nextSibling = item->nextSibling;
1480
1481 if (item->nextSibling)
1482 item->nextSibling->prevSibling = item->prevSibling;
1483}
1484
1485static void
1487{
1488 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1489
1490 if (item->firstChild)
1492
1495
1497
1498 infoPtr->uNumItems--;
1499
1500 if (item->pszText != LPSTR_TEXTCALLBACKW)
1501 heap_free(item->pszText);
1502
1503 TREEVIEW_FreeItem(infoPtr, item);
1504}
1505
1506
1507/* Empty out the tree. */
1508static void
1510{
1511 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1512
1513 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1514}
1515
1516static LRESULT
1518{
1519 TREEVIEW_ITEM *newSelection = NULL;
1520 TREEVIEW_ITEM *newFirstVisible = NULL;
1521 TREEVIEW_ITEM *parent, *prev = NULL;
1522 BOOL visible = FALSE;
1523
1524 if (item == TVI_ROOT || !item)
1525 {
1526 TRACE("TVI_ROOT\n");
1527 parent = infoPtr->root;
1528 newSelection = NULL;
1529 visible = TRUE;
1530 TREEVIEW_RemoveTree(infoPtr);
1531 }
1532 else
1533 {
1534 if (!TREEVIEW_ValidItem(infoPtr, item))
1535 return FALSE;
1536
1537 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1538 parent = item->parent;
1539
1540 if (ISVISIBLE(item))
1541 {
1542 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1543 visible = TRUE;
1544 }
1545
1546 if (infoPtr->selectedItem != NULL
1547 && (item == infoPtr->selectedItem
1548 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1549 {
1550 if (item->nextSibling)
1551 newSelection = item->nextSibling;
1552 else if (item->parent != infoPtr->root)
1553 newSelection = item->parent;
1554 else
1555 newSelection = item->prevSibling;
1556 TRACE("newSelection = %p\n", newSelection);
1557 }
1558
1559 if (infoPtr->firstVisible == item)
1560 {
1561 visible = TRUE;
1562 if (item->nextSibling)
1563 newFirstVisible = item->nextSibling;
1564 else if (item->prevSibling)
1565 newFirstVisible = item->prevSibling;
1566 else if (item->parent != infoPtr->root)
1567 newFirstVisible = item->parent;
1569 }
1570 else
1571 newFirstVisible = infoPtr->firstVisible;
1572
1573 TREEVIEW_RemoveItem(infoPtr, item);
1574 }
1575
1576 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1577 if (!infoPtr->selectedItem && newSelection)
1578 {
1579 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1580 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1581 }
1582
1583 /* Validate insertMark dropItem.
1584 * hotItem ??? - used for comparison only.
1585 */
1586 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1587 infoPtr->insertMarkItem = 0;
1588
1589 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1590 infoPtr->dropItem = 0;
1591
1592 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1593 newFirstVisible = infoPtr->root->firstChild;
1594
1595 TREEVIEW_VerifyTree(infoPtr);
1596
1597 if (visible)
1598 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1599
1600 if (!infoPtr->bRedraw) return TRUE;
1601
1602 if (visible)
1603 {
1604 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1606 TREEVIEW_Invalidate(infoPtr, NULL);
1607 }
1608 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1609 {
1610 /* parent lost '+/-' - update it */
1611 TREEVIEW_Invalidate(infoPtr, parent);
1612 }
1613
1614 return TRUE;
1615}
1616
1617
1618/* Get/Set Messages *********************************************************/
1619static LRESULT
1621{
1622 infoPtr->bRedraw = wParam != 0;
1623
1624 if (infoPtr->bRedraw)
1625 {
1626 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1629 TREEVIEW_Invalidate(infoPtr, NULL);
1630 }
1631 return 0;
1632}
1633
1634static LRESULT
1636{
1637 TRACE("\n");
1638 return infoPtr->uIndent;
1639}
1640
1641static LRESULT
1643{
1644 TRACE("\n");
1645
1646 if (newIndent < MINIMUM_INDENT)
1647 newIndent = MINIMUM_INDENT;
1648
1649 if (infoPtr->uIndent != newIndent)
1650 {
1651 infoPtr->uIndent = newIndent;
1652 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1654 TREEVIEW_Invalidate(infoPtr, NULL);
1655 }
1656
1657 return 0;
1658}
1659
1660
1661static LRESULT
1663{
1664 TRACE("\n");
1665 return (LRESULT)infoPtr->hwndToolTip;
1666}
1667
1668static LRESULT
1670{
1671 HWND prevToolTip;
1672
1673 TRACE("\n");
1674 prevToolTip = infoPtr->hwndToolTip;
1675 infoPtr->hwndToolTip = hwndTT;
1676
1677 return (LRESULT)prevToolTip;
1678}
1679
1680static LRESULT
1682{
1683 BOOL rc = infoPtr->bNtfUnicode;
1684 infoPtr->bNtfUnicode = fUnicode;
1685 return rc;
1686}
1687
1688static LRESULT
1690{
1691 return infoPtr->bNtfUnicode;
1692}
1693
1694static LRESULT
1696{
1697 return infoPtr->uScrollTime;
1698}
1699
1700static LRESULT
1702{
1703 UINT uOldScrollTime = infoPtr->uScrollTime;
1704
1705 infoPtr->uScrollTime = min(uScrollTime, 100);
1706
1707 return uOldScrollTime;
1708}
1709
1710
1711static LRESULT
1713{
1714 TRACE("\n");
1715
1716 switch (wParam)
1717 {
1718 case TVSIL_NORMAL:
1719 return (LRESULT)infoPtr->himlNormal;
1720
1721 case TVSIL_STATE:
1722 return (LRESULT)infoPtr->himlState;
1723
1724 default:
1725 return 0;
1726 }
1727}
1728
1729#define TVHEIGHT_MIN 16
1730#define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1731
1732/* Compute the natural height for items. */
1733static UINT
1735{
1737 HDC hdc = GetDC(0);
1738 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1739 UINT height;
1740
1741 /* Height is the maximum of:
1742 * 16 (a hack because our fonts are tiny), and
1743 * The text height + border & margin, and
1744 * The size of the normal image list
1745 */
1747 SelectObject(hdc, hOldFont);
1748 ReleaseDC(0, hdc);
1749
1751 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1752 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1753 if (height < infoPtr->normalImageHeight)
1754 height = infoPtr->normalImageHeight;
1755
1756 /* Round down, unless we support odd ("non even") heights. */
1757 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1758 height &= ~1;
1759
1760 return height;
1761}
1762
1763static LRESULT
1765{
1766 HIMAGELIST himlOld = 0;
1767 int oldWidth = infoPtr->normalImageWidth;
1768 int oldHeight = infoPtr->normalImageHeight;
1769
1770 TRACE("%u,%p\n", type, himlNew);
1771
1772 switch (type)
1773 {
1774 case TVSIL_NORMAL:
1775 himlOld = infoPtr->himlNormal;
1776 infoPtr->himlNormal = himlNew;
1777
1778 if (himlNew)
1779 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1780 &infoPtr->normalImageHeight);
1781 else
1782 {
1783 infoPtr->normalImageWidth = 0;
1784 infoPtr->normalImageHeight = 0;
1785 }
1786
1787 break;
1788
1789 case TVSIL_STATE:
1790 himlOld = infoPtr->himlState;
1791 infoPtr->himlState = himlNew;
1792
1793 if (himlNew)
1794 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1795 &infoPtr->stateImageHeight);
1796 else
1797 {
1798 infoPtr->stateImageWidth = 0;
1799 infoPtr->stateImageHeight = 0;
1800 }
1801
1802 break;
1803
1804 default:
1805 ERR("unknown imagelist type %u\n", type);
1806 }
1807
1808 if (oldWidth != infoPtr->normalImageWidth ||
1809 oldHeight != infoPtr->normalImageHeight)
1810 {
1811 BOOL bRecalcVisible = FALSE;
1812
1813 if (oldHeight != infoPtr->normalImageHeight &&
1814 !infoPtr->bHeightSet)
1815 {
1816 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1817 bRecalcVisible = TRUE;
1818 }
1819
1820 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1821 infoPtr->normalImageWidth != infoPtr->uIndent)
1822 {
1823 infoPtr->uIndent = infoPtr->normalImageWidth;
1824 bRecalcVisible = TRUE;
1825 }
1826
1827 if (bRecalcVisible)
1829
1830 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1832 }
1833
1834 TREEVIEW_Invalidate(infoPtr, NULL);
1835
1836 return (LRESULT)himlOld;
1837}
1838
1839static LRESULT
1841{
1842 INT prevHeight = infoPtr->uItemHeight;
1843
1844 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1845 if (newHeight == -1)
1846 {
1847 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1848 infoPtr->bHeightSet = FALSE;
1849 }
1850 else
1851 {
1852 if (newHeight == 0) newHeight = 1;
1853 infoPtr->uItemHeight = newHeight;
1854 infoPtr->bHeightSet = TRUE;
1855 }
1856
1857 /* Round down, unless we support odd ("non even") heights. */
1858 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1859 {
1860 infoPtr->uItemHeight &= ~1;
1861 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1862 }
1863
1864 if (infoPtr->uItemHeight != prevHeight)
1865 {
1868 TREEVIEW_Invalidate(infoPtr, NULL);
1869 }
1870
1871 return prevHeight;
1872}
1873
1874static LRESULT
1876{
1877 TRACE("\n");
1878 return infoPtr->uItemHeight;
1879}
1880
1881
1882static LRESULT
1884{
1885 TRACE("%p\n", infoPtr->hFont);
1886 return (LRESULT)infoPtr->hFont;
1887}
1888
1889
1890static INT CALLBACK
1892{
1893 (void)unused;
1894
1895 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1896
1897 return 1;
1898}
1899
1900static LRESULT
1902{
1903 UINT uHeight = infoPtr->uItemHeight;
1904
1905 TRACE("%p %i\n", hFont, bRedraw);
1906
1907 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1908
1909 DeleteObject(infoPtr->hBoldFont);
1910 DeleteObject(infoPtr->hUnderlineFont);
1912 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1915
1916 if (!infoPtr->bHeightSet)
1917 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1918
1919 if (uHeight != infoPtr->uItemHeight)
1921
1923
1924 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1926
1927 if (bRedraw)
1928 TREEVIEW_Invalidate(infoPtr, NULL);
1929
1930 return 0;
1931}
1932
1933
1934static LRESULT
1936{
1937 TRACE("\n");
1938 return (LRESULT)infoPtr->clrLine;
1939}
1940
1941static LRESULT
1943{
1944 COLORREF prevColor = infoPtr->clrLine;
1945
1946 TRACE("\n");
1947 infoPtr->clrLine = color;
1948 return (LRESULT)prevColor;
1949}
1950
1951
1952static LRESULT
1954{
1955 TRACE("\n");
1956 return (LRESULT)infoPtr->clrText;
1957}
1958
1959static LRESULT
1961{
1962 COLORREF prevColor = infoPtr->clrText;
1963
1964 TRACE("\n");
1965 infoPtr->clrText = color;
1966
1967 if (infoPtr->clrText != prevColor)
1968 TREEVIEW_Invalidate(infoPtr, NULL);
1969
1970 return (LRESULT)prevColor;
1971}
1972
1973
1974static LRESULT
1976{
1977 TRACE("\n");
1978 return (LRESULT)infoPtr->clrBk;
1979}
1980
1981static LRESULT
1983{
1984 COLORREF prevColor = infoPtr->clrBk;
1985
1986 TRACE("\n");
1987 infoPtr->clrBk = newColor;
1988
1989 if (newColor != prevColor)
1990 TREEVIEW_Invalidate(infoPtr, NULL);
1991
1992 return (LRESULT)prevColor;
1993}
1994
1995
1996static LRESULT
1998{
1999 TRACE("\n");
2000 return (LRESULT)infoPtr->clrInsertMark;
2001}
2002
2003static LRESULT
2005{
2006 COLORREF prevColor = infoPtr->clrInsertMark;
2007
2008 TRACE("0x%08x\n", color);
2009 infoPtr->clrInsertMark = color;
2010
2011 return (LRESULT)prevColor;
2012}
2013
2014
2015static LRESULT
2017{
2018 TRACE("%d %p\n", wParam, item);
2019
2020 if (!TREEVIEW_ValidItem(infoPtr, item))
2021 return 0;
2022
2023 infoPtr->insertBeforeorAfter = wParam;
2024 infoPtr->insertMarkItem = item;
2025
2026 TREEVIEW_Invalidate(infoPtr, NULL);
2027
2028 return 1;
2029}
2030
2031
2032/************************************************************************
2033 * Some serious braindamage here. lParam is a pointer to both the
2034 * input HTREEITEM and the output RECT.
2035 */
2036static LRESULT
2037TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2038{
2040 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2041
2042 TRACE("\n");
2043
2044 if (pItem == NULL)
2045 return FALSE;
2046
2047 item = *pItem;
2048 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2049 return FALSE;
2050
2051 /*
2052 * If wParam is TRUE return the text size otherwise return
2053 * the whole item size
2054 */
2055 if (fTextRect)
2056 {
2057 /* Windows does not send TVN_GETDISPINFO here. */
2058
2059 lpRect->top = item->rect.top;
2060 lpRect->bottom = item->rect.bottom;
2061
2062 lpRect->left = item->textOffset;
2063 if (!item->textWidth)
2064 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2065
2066 lpRect->right = item->textOffset + item->textWidth + 4;
2067 }
2068 else
2069 {
2070 *lpRect = item->rect;
2071 }
2072
2073 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2074
2075 return TRUE;
2076}
2077
2078static inline LRESULT
2080{
2081 /* Surprise! This does not take integral height into account. */
2082 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2083 return infoPtr->clientHeight / infoPtr->uItemHeight;
2084}
2085
2086
2087static LRESULT
2089{
2090 TREEVIEW_ITEM *item = tvItem->hItem;
2091
2092 if (!TREEVIEW_ValidItem(infoPtr, item))
2093 {
2094 BOOL valid_item = FALSE;
2095 if (!item) return FALSE;
2096
2097 __TRY
2098 {
2099 infoPtr = item->infoPtr;
2100 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2101 valid_item = TREEVIEW_ValidItem(infoPtr, item);
2102 }
2104 {
2105 }
2106 __ENDTRY
2107 if (!valid_item) return FALSE;
2108 }
2109
2110 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2111
2112 if (tvItem->mask & TVIF_CHILDREN)
2113 {
2114 if (item->cChildren==I_CHILDRENCALLBACK)
2115 FIXME("I_CHILDRENCALLBACK not supported\n");
2116 tvItem->cChildren = item->cChildren;
2117 }
2118
2119 if (tvItem->mask & TVIF_HANDLE)
2120 tvItem->hItem = item;
2121
2122 if (tvItem->mask & TVIF_IMAGE)
2123 tvItem->iImage = item->iImage;
2124
2125 if (tvItem->mask & TVIF_INTEGRAL)
2126 tvItem->iIntegral = item->iIntegral;
2127
2128 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2129 tvItem->lParam = item->lParam;
2130
2131 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2132 tvItem->iSelectedImage = item->iSelectedImage;
2133
2134 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2135 tvItem->iExpandedImage = item->iExpandedImage;
2136
2137 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2138 tvItem->state = item->state;
2139
2140 if (tvItem->mask & TVIF_TEXT)
2141 {
2142 if (item->pszText == NULL)
2143 {
2144 if (tvItem->cchTextMax > 0)
2145 tvItem->pszText[0] = '\0';
2146 }
2147 else if (isW)
2148 {
2149 if (item->pszText == LPSTR_TEXTCALLBACKW)
2150 {
2151 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2152 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2153 }
2154 else
2155 {
2156 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2157 }
2158 }
2159 else
2160 {
2161 if (item->pszText == LPSTR_TEXTCALLBACKW)
2162 {
2164 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2165 }
2166 else
2167 {
2168 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2169 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2170 }
2171 }
2172 }
2173
2174 if (tvItem->mask & TVIF_STATEEX)
2175 {
2176 FIXME("Extended item state not supported, returning 0.\n");
2177 tvItem->uStateEx = 0;
2178 }
2179
2180 TRACE("item <%p>, txt %p, img %d, mask 0x%x\n",
2181 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2182
2183 return TRUE;
2184}
2185
2186/* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2187 * which is wrong. */
2188static LRESULT
2190{
2192 TREEVIEW_ITEM originalItem;
2193
2194 item = tvItem->hItem;
2195
2196 TRACE("item %d, mask 0x%x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2197 tvItem->mask);
2198
2199 if (!TREEVIEW_ValidItem(infoPtr, item))
2200 return FALSE;
2201
2202 /* Store the original item values. Text buffer will be freed in TREEVIEW_DoSetItemT() below. */
2203 originalItem = *item;
2204
2205 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2206 return FALSE;
2207
2208 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2209 if ((tvItem->mask & TVIF_TEXT
2210 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2211 && ISVISIBLE(item))
2212 {
2214 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2215 }
2216
2217 if (tvItem->mask != 0 && ISVISIBLE(item))
2218 {
2219 /* The refresh updates everything, but we can't wait until then. */
2221
2222 /* if any of the item's values changed and it's not a callback, redraw the item */
2223 if (item_changed(&originalItem, item, tvItem))
2224 {
2225 if (tvItem->mask & TVIF_INTEGRAL)
2226 {
2229
2230 TREEVIEW_Invalidate(infoPtr, NULL);
2231 }
2232 else
2233 {
2235 TREEVIEW_Invalidate(infoPtr, item);
2236 }
2237 }
2238 }
2239
2240 return TRUE;
2241}
2242
2243static LRESULT
2245{
2246 TRACE("\n");
2247
2248 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2249 return 0;
2250
2251 return (item->state & mask);
2252}
2253
2254static LRESULT
2256{
2257 TREEVIEW_ITEM *retval;
2258
2259 retval = 0;
2260
2261 /* handle all the global data here */
2262 switch (which)
2263 {
2264 case TVGN_CHILD: /* Special case: child of 0 is root */
2265 if (item)
2266 break;
2267 /* fall through */
2268 case TVGN_ROOT:
2269 retval = infoPtr->root->firstChild;
2270 break;
2271
2272 case TVGN_CARET:
2273 retval = infoPtr->selectedItem;
2274 break;
2275
2276 case TVGN_FIRSTVISIBLE:
2277 retval = infoPtr->firstVisible;
2278 break;
2279
2280 case TVGN_DROPHILITE:
2281 retval = infoPtr->dropItem;
2282 break;
2283
2284 case TVGN_LASTVISIBLE:
2285 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2286 break;
2287 }
2288
2289 if (retval)
2290 {
2291 TRACE("flags:0x%x, returns %p\n", which, retval);
2292 return (LRESULT)retval;
2293 }
2294
2295 if (item == TVI_ROOT) item = infoPtr->root;
2296
2297 if (!TREEVIEW_ValidItem(infoPtr, item))
2298 return FALSE;
2299
2300 switch (which)
2301 {
2302 case TVGN_NEXT:
2303 retval = item->nextSibling;
2304 break;
2305 case TVGN_PREVIOUS:
2306 retval = item->prevSibling;
2307 break;
2308 case TVGN_PARENT:
2309 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2310 break;
2311 case TVGN_CHILD:
2312 retval = item->firstChild;
2313 break;
2314 case TVGN_NEXTVISIBLE:
2315 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2316 break;
2318 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2319 break;
2320 default:
2321 TRACE("Unknown msg 0x%x, item %p\n", which, item);
2322 break;
2323 }
2324
2325 TRACE("flags: 0x%x, item %p;returns %p\n", which, item, retval);
2326 return (LRESULT)retval;
2327}
2328
2329
2330static LRESULT
2332{
2333 TRACE(" %d\n", infoPtr->uNumItems);
2334 return (LRESULT)infoPtr->uNumItems;
2335}
2336
2337#ifdef __REACTOS__
2338static LRESULT
2340#endif
2341
2342#ifdef __REACTOS__
2343static VOID
2345#else
2346static VOID
2348#endif
2349{
2350 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2351 {
2352 static const unsigned int state_table[] = { 0, 2, 1 };
2353
2354 unsigned int state;
2355
2356 state = STATEIMAGEINDEX(item->state);
2357 TRACE("state: 0x%x\n", state);
2358 item->state &= ~TVIS_STATEIMAGEMASK;
2359
2360 if (state < 3)
2362
2363 item->state |= INDEXTOSTATEIMAGEMASK(state);
2364
2365 TRACE("state: 0x%x\n", state);
2366#ifdef __REACTOS__
2368#endif
2369 TREEVIEW_Invalidate(infoPtr, item);
2370 }
2371}
2372
2373
2374/* Painting *************************************************************/
2375
2376/* Draw the lines and expand button for an item. Also draws one section
2377 * of the line from item's parent to item's parent's next sibling. */
2378static void
2380{
2381 LONG centerx, centery;
2382 BOOL lar = ((infoPtr->dwStyle
2384 > TVS_LINESATROOT);
2385 HBRUSH hbr, hbrOld;
2386 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2387
2388 if (!lar && item->iLevel == 0)
2389 return;
2390
2391 hbr = CreateSolidBrush(clrBk);
2392 hbrOld = SelectObject(hdc, hbr);
2393
2394 centerx = (item->linesOffset + item->stateOffset) / 2;
2395 centery = (item->rect.top + item->rect.bottom) / 2;
2396
2397 if (infoPtr->dwStyle & TVS_HASLINES)
2398 {
2399 HPEN hOldPen, hNewPen;
2401 LOGBRUSH lb;
2402
2403 /* Get a dotted grey pen */
2404 lb.lbStyle = BS_SOLID;
2405 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2406 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2407 hOldPen = SelectObject(hdc, hNewPen);
2408
2409 /* Make sure the center is on a dot (using +2 instead
2410 * of +1 gives us pixel-by-pixel compat with native) */
2411 centery = (centery + 2) & ~1;
2412
2413 MoveToEx(hdc, item->stateOffset, centery, NULL);
2414 LineTo(hdc, centerx - 1, centery);
2415
2416 if (item->prevSibling || item->parent != infoPtr->root)
2417 {
2418 MoveToEx(hdc, centerx, item->rect.top, NULL);
2419 LineTo(hdc, centerx, centery);
2420 }
2421
2422 if (item->nextSibling)
2423 {
2424 MoveToEx(hdc, centerx, centery, NULL);
2425 LineTo(hdc, centerx, item->rect.bottom + 1);
2426 }
2427
2428 /* Draw the line from our parent to its next sibling. */
2429 parent = item->parent;
2430 while (parent != infoPtr->root)
2431 {
2432 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2433
2434 if (parent->nextSibling
2435 /* skip top-levels unless TVS_LINESATROOT */
2436 && parent->stateOffset > parent->linesOffset)
2437 {
2438 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2439 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2440 }
2441
2442 parent = parent->parent;
2443 }
2444
2445 SelectObject(hdc, hOldPen);
2446 DeleteObject(hNewPen);
2447 }
2448
2449 /*
2450 * Display the (+/-) signs
2451 */
2452
2453 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2454 {
2455 if (item->cChildren)
2456 {
2457 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2458 if (theme)
2459 {
2460 RECT glyphRect = item->rect;
2461 glyphRect.left = item->linesOffset;
2462 glyphRect.right = item->stateOffset;
2465 &glyphRect, NULL);
2466 }
2467 else
2468 {
2469 LONG height = item->rect.bottom - item->rect.top;
2470 LONG width = item->stateOffset - item->linesOffset;
2471 LONG rectsize = min(height, width) / 4;
2472 /* plussize = ceil(rectsize * 3/4) */
2473 LONG plussize = (rectsize + 1) * 3 / 4;
2474
2475 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2476 HPEN old_pen = SelectObject(hdc, new_pen);
2477
2478 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2479 centerx + rectsize + 2, centery + rectsize + 2);
2480
2481 SelectObject(hdc, old_pen);
2482 DeleteObject(new_pen);
2483
2484 /* draw +/- signs with current text color */
2485 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2486 old_pen = SelectObject(hdc, new_pen);
2487
2488 if (height < 18 || width < 18)
2489 {
2490 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2491 LineTo(hdc, centerx + plussize, centery);
2492
2493 if (!(item->state & TVIS_EXPANDED) ||
2494 (item->state & TVIS_EXPANDPARTIAL))
2495 {
2496 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2497 LineTo(hdc, centerx, centery + plussize);
2498 }
2499 }
2500 else
2501 {
2502 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2503 centerx + plussize, centery + 2);
2504
2505 if (!(item->state & TVIS_EXPANDED) ||
2506 (item->state & TVIS_EXPANDPARTIAL))
2507 {
2508 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2509 centerx + 2, centery + plussize);
2510 SetPixel(hdc, centerx - 1, centery, clrBk);
2511 SetPixel(hdc, centerx + 1, centery, clrBk);
2512 }
2513 }
2514
2515 SelectObject(hdc, old_pen);
2516 DeleteObject(new_pen);
2517 }
2518 }
2519 }
2520 SelectObject(hdc, hbrOld);
2521 DeleteObject(hbr);
2522}
2523
2524static void
2526{
2527 INT cditem;
2528 HFONT hOldFont;
2529 COLORREF oldTextColor, oldTextBkColor;
2530 int centery;
2531 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2532 NMTVCUSTOMDRAW nmcdhdr;
2533
2535
2536 /* - If item is drop target or it is selected and window is in focus -
2537 * use blue background (COLOR_HIGHLIGHT).
2538 * - If item is selected, window is not in focus, but it has style
2539 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2540 * - Otherwise - use background color
2541 */
2542 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2543 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2544 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2545 {
2546 if ((item->state & TVIS_DROPHILITED) || inFocus)
2547 {
2550 }
2551 else
2552 {
2554 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2555 }
2556 }
2557 else
2558 {
2559 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2560 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2562 else
2563 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2564 }
2565
2566 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2567 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2568 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2569
2570 /* The custom draw handler can query the text rectangle,
2571 * so get ready. */
2572 /* should already be known, set to 0 when changed */
2573 if (!item->textWidth)
2575
2576 cditem = 0;
2577
2578 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2579 {
2581 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2582 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2583
2584 if (cditem & CDRF_SKIPDEFAULT)
2585 {
2586 SelectObject(hdc, hOldFont);
2587 return;
2588 }
2589 }
2590
2591 if (cditem & CDRF_NEWFONT)
2593
2594 if (TREEVIEW_IsFullRowSelect(infoPtr))
2595 {
2596 HBRUSH brush = CreateSolidBrush(nmcdhdr.clrTextBk);
2597 FillRect(hdc, &item->rect, brush);
2598 DeleteObject(brush);
2599 }
2600
2601 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2602
2603 /* reset colors. Custom draw handler can change them */
2604 SetTextColor(hdc, nmcdhdr.clrText);
2605 SetBkColor(hdc, nmcdhdr.clrTextBk);
2606
2607 centery = (item->rect.top + item->rect.bottom) / 2;
2608
2609 /*
2610 * Display the images associated with this item
2611 */
2612 {
2613 INT imageIndex;
2614
2615 /* State images are displayed to the left of the Normal image
2616 * image number is in state; zero should be `display no image'.
2617 */
2618 imageIndex = STATEIMAGEINDEX(item->state);
2619
2620 if (infoPtr->himlState && imageIndex)
2621 {
2622 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2623 item->stateOffset,
2624 centery - infoPtr->stateImageHeight / 2,
2625 ILD_NORMAL);
2626 }
2627
2628 /* Now, draw the normal image; can be either selected,
2629 * non-selected or expanded image.
2630 */
2631
2632 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2633 {
2634 /* The item is currently selected */
2635 imageIndex = item->iSelectedImage;
2636 }
2637 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2638 {
2639 /* The item is currently not selected but expanded */
2640 imageIndex = item->iExpandedImage;
2641 }
2642 else
2643 {
2644 /* The item is not selected and not expanded */
2645 imageIndex = item->iImage;
2646 }
2647
2648 if (infoPtr->himlNormal)
2649 {
2651
2652 style |= item->state & TVIS_OVERLAYMASK;
2653
2654 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2655 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2656 0, 0,
2657 TREEVIEW_IsFullRowSelect(infoPtr) ? nmcdhdr.clrTextBk : infoPtr->clrBk,
2658 item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2659 style);
2660 }
2661 }
2662
2663
2664 /*
2665 * Display the text associated with this item
2666 */
2667
2668 /* Don't paint item's text if it's being edited */
2669 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2670 {
2671 if (item->pszText)
2672 {
2673 RECT rcText;
2674 UINT align;
2675 SIZE sz;
2676
2677 rcText.top = item->rect.top;
2678 rcText.bottom = item->rect.bottom;
2679 rcText.left = item->textOffset;
2680 rcText.right = rcText.left + item->textWidth + 4;
2681
2682 TRACE("drawing text %s at (%s)\n",
2683 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2684
2685 /* Draw it */
2686 GetTextExtentPoint32W(hdc, item->pszText, lstrlenW(item->pszText), &sz);
2687
2689 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2691 &rcText,
2692 item->pszText,
2693 lstrlenW(item->pszText),
2694 NULL);
2696
2697 /* Draw focus box around the selected item */
2698 if ((item == infoPtr->selectedItem) && inFocus)
2699 {
2700 DrawFocusRect(hdc,&rcText);
2701 }
2702 }
2703 }
2704
2705 /* Draw insertion mark if necessary */
2706
2707 if (infoPtr->insertMarkItem)
2708 TRACE("item:%d,mark:%p\n",
2709 TREEVIEW_GetItemIndex(infoPtr, item),
2710 infoPtr->insertMarkItem);
2711
2712 if (item == infoPtr->insertMarkItem)
2713 {
2714 HPEN hNewPen, hOldPen;
2715 int offset;
2716 int left, right;
2717
2718 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2719 hOldPen = SelectObject(hdc, hNewPen);
2720
2721 if (infoPtr->insertBeforeorAfter)
2722 offset = item->rect.bottom - 1;
2723 else
2724 offset = item->rect.top + 1;
2725
2726 left = item->textOffset - 2;
2727 right = item->textOffset + item->textWidth + 2;
2728
2729 MoveToEx(hdc, left, offset - 3, NULL);
2730 LineTo(hdc, left, offset + 4);
2731
2733 LineTo(hdc, right + 1, offset);
2734
2735 MoveToEx(hdc, right, offset + 3, NULL);
2736 LineTo(hdc, right, offset - 4);
2737
2738 SelectObject(hdc, hOldPen);
2739 DeleteObject(hNewPen);
2740 }
2741
2742 /* Restore the hdc state */
2743 SetTextColor(hdc, oldTextColor);
2744 SetBkColor(hdc, oldTextBkColor);
2745 SelectObject(hdc, hOldFont);
2746
2747 if (cditem & CDRF_NOTIFYPOSTPAINT)
2748 {
2750 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2751 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2752 }
2753}
2754
2755/* Computes treeHeight and treeWidth and updates the scroll bars.
2756 */
2757static void
2759{
2761 HWND hwnd = infoPtr->hwnd;
2762 BOOL vert = FALSE;
2763 BOOL horz = FALSE;
2764 SCROLLINFO si;
2765 LONG scrollX = infoPtr->scrollX;
2766
2767 infoPtr->treeWidth = 0;
2768 infoPtr->treeHeight = 0;
2769
2770 /* We iterate through all visible items in order to get the tree height
2771 * and width */
2772 item = infoPtr->root->firstChild;
2773
2774 while (item != NULL)
2775 {
2776 if (ISVISIBLE(item))
2777 {
2778 /* actually we draw text at textOffset + 2 */
2779 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2780 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2781
2782 /* This is scroll-adjusted, but we fix this below. */
2783 infoPtr->treeHeight = item->rect.bottom;
2784 }
2785
2787 }
2788
2789 /* Fix the scroll adjusted treeHeight and treeWidth. */
2790 if (infoPtr->root->firstChild)
2791 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2792
2793 infoPtr->treeWidth += infoPtr->scrollX;
2794
2795 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2796
2797 /* Adding one scroll bar may take up enough space that it forces us
2798 * to add the other as well. */
2799 if (infoPtr->treeHeight > infoPtr->clientHeight)
2800 {
2801 vert = TRUE;
2802
2803 if (infoPtr->treeWidth
2805 horz = TRUE;
2806 }
2807 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2808 horz = TRUE;
2809
2810 if (!vert && horz && infoPtr->treeHeight
2812 vert = TRUE;
2813
2814 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2815
2816 si.cbSize = sizeof(SCROLLINFO);
2818 si.nMin = 0;
2819
2820 if (vert)
2821 {
2822 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2823 if ( si.nPage && NULL != infoPtr->firstVisible)
2824 {
2825 si.nPos = infoPtr->firstVisible->visibleOrder;
2826 si.nMax = infoPtr->maxVisibleOrder - 1;
2827
2829
2830 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2832 infoPtr->uInternalStatus |= TV_VSCROLL;
2833 }
2834 else
2835 {
2836 if (infoPtr->uInternalStatus & TV_VSCROLL)
2838 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2839 }
2840 }
2841 else
2842 {
2843 if (infoPtr->uInternalStatus & TV_VSCROLL)
2845 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2846 }
2847
2848 if (horz)
2849 {
2850 si.nPage = infoPtr->clientWidth;
2851 si.nPos = infoPtr->scrollX;
2852 si.nMax = infoPtr->treeWidth - 1;
2853
2854 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2855 {
2856 si.nPos = si.nMax - max( si.nPage-1, 0 );
2857 scrollX = si.nPos;
2858 }
2859
2860 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2862 infoPtr->uInternalStatus |= TV_HSCROLL;
2863
2865 TREEVIEW_HScroll(infoPtr,
2866 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2867 }
2868 else
2869 {
2870 if (infoPtr->uInternalStatus & TV_HSCROLL)
2872 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2873
2874 scrollX = 0;
2875 if (infoPtr->scrollX != 0)
2876 {
2877 TREEVIEW_HScroll(infoPtr,
2878 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2879 }
2880 }
2881
2882 if (!horz)
2883 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2884}
2885
2886static void
2887TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2888{
2889 HBRUSH hBrush;
2890 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2891
2892 hBrush = CreateSolidBrush(clrBk);
2893 FillRect(hdc, rc, hBrush);
2894 DeleteObject(hBrush);
2895}
2896
2897/* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2898static LRESULT
2900{
2901 RECT rect;
2902
2903 TRACE("%p\n", infoPtr);
2904
2905 GetClientRect(infoPtr->hwnd, &rect);
2906 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2907
2908 return 1;
2909}
2910
2911static void
2913{
2914 HWND hwnd = infoPtr->hwnd;
2915 RECT rect = *rc;
2917
2918 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2919 {
2920 TRACE("empty window\n");
2921 return;
2922 }
2923
2925 hdc, rect);
2926
2927 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2928 {
2929 ReleaseDC(hwnd, hdc);
2930 return;
2931 }
2932
2933 for (item = infoPtr->root->firstChild;
2934 item != NULL;
2936 {
2937 if (ISVISIBLE(item))
2938 {
2939 /* Avoid unneeded calculations */
2940 if (item->rect.top > rect.bottom)
2941 break;
2942 if (item->rect.bottom < rect.top)
2943 continue;
2944
2945 TREEVIEW_DrawItem(infoPtr, hdc, item);
2946 }
2947 }
2948
2949 //
2950 // FIXME: This is correct, but is causes and infinite loop of WM_PAINT
2951 // messages, resulting in continuous painting of the scroll bar in reactos.
2952 // Comment out until the real bug is found. CORE-4912
2953 //
2954#ifndef __REACTOS__
2956#endif
2957
2958 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2959 infoPtr->cdmode =
2961}
2962
2963static inline void
2965{
2966 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2967}
2968
2969static void
2971{
2972 if (item)
2973 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2974 else
2975 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2976}
2977
2978static void
2980{
2981 RECT rc;
2982 HBITMAP hbm, hbmOld;
2983 HDC hdc, hdcScreen;
2984 int nIndex;
2985
2986 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2987
2988 hdcScreen = GetDC(0);
2989
2990 hdc = CreateCompatibleDC(hdcScreen);
2991 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2992 hbmOld = SelectObject(hdc, hbm);
2993
2994 SetRect(&rc, 0, 0, 48, 16);
2995 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2996
2997 SetRect(&rc, 18, 2, 30, 14);
3000
3001 SetRect(&rc, 34, 2, 46, 14);
3004
3005 SelectObject(hdc, hbmOld);
3006 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
3008 TRACE("checkbox index %d\n", nIndex);
3009
3011 DeleteDC(hdc);
3012 ReleaseDC(0, hdcScreen);
3013
3014 infoPtr->stateImageWidth = 16;
3015 infoPtr->stateImageHeight = 16;
3016}
3017
3018static void
3020{
3021 TREEVIEW_ITEM *child = item->firstChild;
3022
3023 item->state &= ~TVIS_STATEIMAGEMASK;
3024 item->state |= INDEXTOSTATEIMAGEMASK(1);
3025
3026 while (child)
3027 {
3028 TREEVIEW_ITEM *next = child->nextSibling;
3030 child = next;
3031 }
3032}
3033
3034static LRESULT
3036{
3037 HDC hdc;
3038 PAINTSTRUCT ps;
3039 RECT rc;
3040
3041 TRACE("(%p %p)\n", infoPtr, hdc_ref);
3042
3043 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
3044 {
3045 TREEVIEW_InitCheckboxes(infoPtr);
3046 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
3047
3049 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
3051 TREEVIEW_Invalidate(infoPtr, NULL);
3052 }
3053
3054 if (hdc_ref)
3055 {
3056 hdc = hdc_ref;
3057 GetClientRect(infoPtr->hwnd, &rc);
3058 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3059 }
3060 else
3061 {
3062 hdc = BeginPaint(infoPtr->hwnd, &ps);
3063 rc = ps.rcPaint;
3064 if(ps.fErase)
3065 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
3066 }
3067
3068 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3069 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3070
3071 if (!hdc_ref)
3072 EndPaint(infoPtr->hwnd, &ps);
3073
3074 return 0;
3075}
3076
3077static LRESULT
3079{
3080 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3081
3082 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3083 return 0;
3084
3085 if (options & PRF_ERASEBKGND)
3086 TREEVIEW_EraseBackground(infoPtr, hdc);
3087
3088 if (options & PRF_CLIENT)
3089 {
3090 RECT rc;
3091 GetClientRect(infoPtr->hwnd, &rc);
3092 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3093 }
3094
3095 return 0;
3096}
3097
3098/* Sorting **************************************************************/
3099
3100/***************************************************************************
3101 * Forward the DPA local callback to the treeview owner callback
3102 */
3103static INT WINAPI
3105 const TVSORTCB *pCallBackSort)
3106{
3107 /* Forward the call to the client-defined callback */
3108 return pCallBackSort->lpfnCompare(first->lParam,
3109 second->lParam,
3110 pCallBackSort->lParam);
3111}
3112
3113/***************************************************************************
3114 * Treeview native sort routine: sort on item text.
3115 */
3116static INT WINAPI
3118 const TREEVIEW_INFO *infoPtr)
3119{
3121 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3122
3123 if(first->pszText && second->pszText)
3124 return lstrcmpiW(first->pszText, second->pszText);
3125 else if(first->pszText)
3126 return -1;
3127 else if(second->pszText)
3128 return 1;
3129 else
3130 return 0;
3131}
3132
3133/* Returns the number of physical children belonging to item. */
3134static INT
3136{
3137 INT cChildren = 0;
3138 HTREEITEM hti;
3139
3140 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3141 cChildren++;
3142
3143 return cChildren;
3144}
3145
3146/* Returns a DPA containing a pointer to each physical child of item in
3147 * sibling order. If item has no children, an empty DPA is returned. */
3148static HDPA
3150{
3152
3153 HDPA list = DPA_Create(8);
3154 if (list == 0) return NULL;
3155
3156 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3157 {
3158 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3159 {
3161 return NULL;
3162 }
3163 }
3164
3165 return list;
3166}
3167
3168/***************************************************************************
3169 * Setup the treeview structure with regards of the sort method
3170 * and sort the children of the TV item specified in lParam
3171 * fRecurse: currently unused. Should be zero.
3172 * parent: if pSort!=NULL, should equal pSort->hParent.
3173 * otherwise, item which child items are to be sorted.
3174 * pSort: sort method info. if NULL, sort on item text.
3175 * if non-NULL, sort on item's lParam content, and let the
3176 * application decide what that means. See also TVM_SORTCHILDRENCB.
3177 */
3178
3179static LRESULT
3181 LPTVSORTCB pSort)
3182{
3183 INT cChildren;
3184 PFNDPACOMPARE pfnCompare;
3185 LPARAM lpCompare;
3186
3187 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3188 if (parent == TVI_ROOT || parent == NULL)
3189 parent = infoPtr->root;
3190
3191 /* Check for a valid handle to the parent item */
3192 if (!TREEVIEW_ValidItem(infoPtr, parent))
3193 {
3194 ERR("invalid item hParent=%p\n", parent);
3195 return FALSE;
3196 }
3197
3198 if (pSort)
3199 {
3201 lpCompare = (LPARAM)pSort;
3202 }
3203 else
3204 {
3205 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3206 lpCompare = (LPARAM)infoPtr;
3207 }
3208
3209 cChildren = TREEVIEW_CountChildren(parent);
3210
3211 /* Make sure there is something to sort */
3212 if (cChildren > 1)
3213 {
3214 /* TREEVIEW_ITEM rechaining */
3215 INT count = 0;
3216 HTREEITEM item = 0;
3217 HTREEITEM nextItem = 0;
3218 HTREEITEM prevItem = 0;
3219
3221
3222 if (sortList == NULL)
3223 return FALSE;
3224
3225 /* let DPA sort the list */
3226 DPA_Sort(sortList, pfnCompare, lpCompare);
3227
3228 /* The order of DPA entries has been changed, so fixup the
3229 * nextSibling and prevSibling pointers. */
3230
3231 item = DPA_GetPtr(sortList, count++);
3232 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3233 {
3234 /* link the two current item together */
3235 item->nextSibling = nextItem;
3236 nextItem->prevSibling = item;
3237
3238 if (prevItem == NULL)
3239 {
3240 /* this is the first item, update the parent */
3241 parent->firstChild = item;
3242 item->prevSibling = NULL;
3243 }
3244 else
3245 {
3246 /* fix the back chaining */
3247 item->prevSibling = prevItem;
3248 }
3249
3250 /* get ready for the next one */
3251 prevItem = item;
3252 item = nextItem;
3253 }
3254
3255 /* the last item is pointed to by item and never has a sibling */
3256 item->nextSibling = NULL;
3257 parent->lastChild = item;
3258
3259 DPA_Destroy(sortList);
3260
3261 TREEVIEW_VerifyTree(infoPtr);
3262
3263 if (parent->state & TVIS_EXPANDED)
3264 {
3265 int visOrder = infoPtr->firstVisible->visibleOrder;
3266
3267 if (parent == infoPtr->root)
3269 else
3271
3272 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3273 {
3275
3276 for (item = infoPtr->root->firstChild; item != NULL;
3278 {
3279 if (item->visibleOrder == visOrder)
3280 break;
3281 }
3282
3283 if (!item) item = parent->firstChild;
3285 }
3286
3287 TREEVIEW_Invalidate(infoPtr, NULL);
3288 }
3289
3290 return TRUE;
3291 }
3292 return FALSE;
3293}
3294
3295
3296/***************************************************************************
3297 * Setup the treeview structure with regards of the sort method
3298 * and sort the children of the TV item specified in lParam
3299 */
3300static LRESULT
3302{
3303 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3304}
3305
3306
3307/***************************************************************************
3308 * Sort the children of the TV item specified in lParam.
3309 */
3310static LRESULT
3312{
3313 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3314}
3315
3316
3317/* Expansion/Collapse ***************************************************/
3318
3319static BOOL
3321 UINT action)
3322{
3326 0, item);
3327}
3328
3329static VOID
3331 UINT action)
3332{
3336 0, item);
3337}
3338
3339
3340/* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3341 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3342static BOOL
3344 BOOL bRemoveChildren, BOOL bUser)
3345{
3346 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3347 BOOL bSetSelection, bSetFirstVisible;
3348 RECT scrollRect;
3349 LONG scrollDist = 0;
3350 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3351 BOOL wasExpanded;
3352
3353 TRACE("TVE_COLLAPSE %p %s\n", item, TREEVIEW_ItemName(item));
3354
3355 if (!TREEVIEW_HasChildren(infoPtr, item))
3356 return FALSE;
3357
3358 if (bUser)
3360
3361 if (item->firstChild == NULL)
3362 return FALSE;
3363
3364 wasExpanded = (item->state & TVIS_EXPANDED) != 0;
3365 item->state &= ~TVIS_EXPANDED;
3366
3367 if (wasExpanded && bUser)
3369
3370 bSetSelection = (infoPtr->selectedItem != NULL
3371 && TREEVIEW_IsChildOf(item, infoPtr->selectedItem));
3372
3373 bSetFirstVisible = (infoPtr->firstVisible != NULL
3374 && TREEVIEW_IsChildOf(item, infoPtr->firstVisible));
3375
3376 tmpItem = item;
3377 while (tmpItem)
3378 {
3379 if (tmpItem->nextSibling)
3380 {
3381 nextItem = tmpItem->nextSibling;
3382 break;
3383 }
3384 tmpItem = tmpItem->parent;
3385 }
3386
3387 if (nextItem)
3388 scrollDist = nextItem->rect.top;
3389
3390 if (bRemoveChildren)
3391 {
3392 INT old_cChildren = item->cChildren;
3393 TRACE("TVE_COLLAPSERESET\n");
3394 item->state &= ~TVIS_EXPANDEDONCE;
3396 item->cChildren = old_cChildren;
3397 }
3398 if (!wasExpanded)
3399 return FALSE;
3400
3401 if (item->firstChild)
3402 {
3403 TREEVIEW_ITEM *i, *sibling;
3404
3405 sibling = TREEVIEW_GetNextListItem(infoPtr, item);
3406
3407 for (i = item->firstChild; i != sibling;
3408 i = TREEVIEW_GetNextListItem(infoPtr, i))
3409 {
3410 i->visibleOrder = -1;
3411 }
3412 }
3413
3415
3416 if (nextItem)
3417 scrollDist = -(scrollDist - nextItem->rect.top);
3418
3419 if (bSetSelection)
3420 {
3421 /* Don't call DoSelectItem, it sends notifications. */
3422 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3423 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3424 item->state |= TVIS_SELECTED;
3425 infoPtr->selectedItem = item;
3426 }
3427
3429
3430 scrollRect.left = 0;
3431 scrollRect.right = infoPtr->clientWidth;
3432 scrollRect.bottom = infoPtr->clientHeight;
3433
3434 if (nextItem)
3435 {
3436 scrollRect.top = nextItem->rect.top;
3437
3438 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3440 TREEVIEW_Invalidate(infoPtr, item);
3441 } else {
3442 scrollRect.top = item->rect.top;
3443 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3444 }
3445
3447 bSetFirstVisible ? item : infoPtr->firstVisible,
3448 TRUE);
3449
3450 return wasExpanded;
3451}
3452
3453static BOOL
3455 BOOL partial, BOOL user)
3456{
3457 LONG scrollDist;
3458 LONG orgNextTop = 0;
3459 RECT scrollRect;
3460 TREEVIEW_ITEM *nextItem, *tmpItem;
3461 BOOL sendsNotifications;
3462
3463 TRACE("(%p, %p, partial=%d, %d)\n", infoPtr, item, partial, user);
3464
3465 if (!TREEVIEW_HasChildren(infoPtr, item))
3466 return FALSE;
3467
3468 tmpItem = item; nextItem = NULL;
3469 while (tmpItem)
3470 {
3471 if (tmpItem->nextSibling)
3472 {
3473 nextItem = tmpItem->nextSibling;
3474 break;
3475 }
3476 tmpItem = tmpItem->parent;
3477 }
3478
3479 if (nextItem)
3480 orgNextTop = nextItem->rect.top;
3481
3482 TRACE("TVE_EXPAND %p %s\n", item, TREEVIEW_ItemName(item));
3483
3484 sendsNotifications = user || ((item->cChildren != 0) &&
3485 !(item->state & TVIS_EXPANDEDONCE));
3486 if (sendsNotifications)
3487 {
3488 if (!TREEVIEW_SendExpanding(infoPtr, item, TVE_EXPAND))
3489 {
3490 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3491 return FALSE;
3492 }
3493 }
3494 if (!item->firstChild)
3495 return FALSE;
3496
3497 item->state |= TVIS_EXPANDED;
3498
3499 if (partial)
3500 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3501
3502 if (ISVISIBLE(item))
3503 {
3505 TREEVIEW_UpdateSubTree(infoPtr, item);
3507
3508 scrollRect.left = 0;
3509 scrollRect.bottom = infoPtr->treeHeight;
3510 scrollRect.right = infoPtr->clientWidth;
3511 if (nextItem)
3512 {
3513 scrollDist = nextItem->rect.top - orgNextTop;
3514 scrollRect.top = orgNextTop;
3515
3516 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3518 TREEVIEW_Invalidate (infoPtr, item);
3519 } else {
3520 scrollRect.top = item->rect.top;
3521 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3522 }
3523
3524 /* Scroll up so that as many children as possible are visible.
3525 * This fails when expanding causes an HScroll bar to appear, but we
3526 * don't know that yet, so the last item is obscured. */
3527 if (item->firstChild != NULL)
3528 {
3529 int nChildren = item->lastChild->visibleOrder
3530 - item->firstChild->visibleOrder + 1;
3531
3532 int visible_pos = item->visibleOrder
3533 - infoPtr->firstVisible->visibleOrder;
3534
3535 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3536
3537 if (visible_pos > 0 && nChildren > rows_below)
3538 {
3539 int scroll = nChildren - rows_below;
3540
3541 if (scroll > visible_pos)
3542 scroll = visible_pos;
3543
3544 if (scroll > 0)
3545 {
3546 TREEVIEW_ITEM *newFirstVisible
3547 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3548 scroll);
3549
3550
3551 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3552 }
3553 }
3554 }
3555 }
3556
3557 if (sendsNotifications) {
3559 item->state |= TVIS_EXPANDEDONCE;
3560 }
3561
3562 return TRUE;
3563}
3564
3565/* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3566 to mouse messages and TVM_SELECTITEM.
3567
3568 selection - previously selected item, used to collapse a part of a tree
3569 item - new selected item
3570*/
3573{
3574 TREEVIEW_ITEM *prev, *curr;
3575
3576 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit || !item) return;
3577
3579
3580 /*
3581 * Close the previous item and its ancestors as long as they are not
3582 * ancestors of the current item
3583 */
3584 for (prev = selection; prev && TREEVIEW_ValidItem(infoPtr, prev); prev = prev->parent)
3585 {
3586 for (curr = item; curr && TREEVIEW_ValidItem(infoPtr, curr); curr = curr->parent)
3587 {
3588 if (curr == prev)
3589 goto finish;
3590 }
3591 TREEVIEW_Collapse(infoPtr, prev, FALSE, TRUE);
3592 }
3593
3594finish:
3595 /*
3596 * Expand the current item
3597 */
3598 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3599}
3600
3601static BOOL
3603{
3604 TRACE("item=%p, user=%d\n", item, user);
3605
3606 if (item->state & TVIS_EXPANDED)
3607 return TREEVIEW_Collapse(infoPtr, item, FALSE, user);
3608 else
3609 return TREEVIEW_Expand(infoPtr, item, FALSE, user);
3610}
3611
3612static VOID
3614{
3615 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3616
3617 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3618 {
3619 if (TREEVIEW_HasChildren(infoPtr, item))
3620 TREEVIEW_ExpandAll(infoPtr, item);
3621 }
3622}
3623
3624/* Note:If the specified item is the child of a collapsed parent item,
3625 the parent's list of child items is (recursively) expanded to reveal the
3626 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3627 know if it also applies here.
3628*/
3629
3630static LRESULT
3632{
3633 if (!TREEVIEW_ValidItem(infoPtr, item))
3634 return 0;
3635
3636 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3638 flag, item->state);
3639
3640 switch (flag & TVE_TOGGLE)
3641 {
3642 case TVE_COLLAPSE:
3643 return TREEVIEW_Collapse(infoPtr, item, flag & TVE_COLLAPSERESET,
3644 FALSE);
3645
3646 case TVE_EXPAND:
3647 return TREEVIEW_Expand(infoPtr, item, flag & TVE_EXPANDPARTIAL,
3648 FALSE);
3649
3650 case TVE_TOGGLE:
3651 return TREEVIEW_Toggle(infoPtr, item, FALSE);
3652
3653 default:
3654 return 0;
3655 }
3656}
3657
3658/* Hit-Testing **********************************************************/
3659
3660static TREEVIEW_ITEM *
3662{
3664 LONG row;
3665
3666 if (!infoPtr->firstVisible)
3667 return NULL;
3668
3669 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3670
3671 for (item = infoPtr->firstVisible; item != NULL;
3673 {
3674 if (row >= item->visibleOrder
3675 && row < item->visibleOrder + item->iIntegral)
3676 break;
3677 }
3678
3679 return item;
3680}
3681
3682static TREEVIEW_ITEM *
3684{
3686 RECT rect;
3687 UINT status;
3688 LONG x, y;
3689
3690 lpht->hItem = 0;
3691 GetClientRect(infoPtr->hwnd, &rect);
3692 status = 0;
3693 x = lpht->pt.x;
3694 y = lpht->pt.y;
3695
3696 if (x < rect.left)
3697 {
3699 }
3700 else if (x > rect.right)
3701 {
3703 }
3704
3705 if (y < rect.top)
3706 {
3707 status |= TVHT_ABOVE;
3708 }
3709 else if (y > rect.bottom)
3710 {
3711 status |= TVHT_BELOW;
3712 }
3713
3714 if (status)
3715 {
3716 lpht->flags = status;
3717 return NULL;
3718 }
3719
3720 item = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3721 if (!item)
3722 {
3723 lpht->flags = TVHT_NOWHERE;
3724 return NULL;
3725 }
3726
3727 if (!item->textWidth)
3728 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
3729
3730 if (x >= item->textOffset + item->textWidth)
3731 {
3732 lpht->flags = TVHT_ONITEMRIGHT;
3733 }
3734 else if (x >= item->textOffset)
3735 {
3736 lpht->flags = TVHT_ONITEMLABEL;
3737 }
3738 else if (x >= item->imageOffset)
3739 {
3740 lpht->flags = TVHT_ONITEMICON;
3741 }
3742 else if (x >= item->stateOffset)
3743 {
3745 }
3746 else if (x >= item->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3747 {
3748 lpht->flags = TVHT_ONITEMBUTTON;
3749 }
3750 else
3751 {
3752 lpht->flags = TVHT_ONITEMINDENT;
3753 }
3754
3755 lpht->hItem = item;
3756 TRACE("(%d,%d):result 0x%x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3757
3758 return item;
3759}
3760
3761/* Item Label Editing ***************************************************/
3762
3763static LRESULT
3765{
3766 return (LRESULT)infoPtr->hwndEdit;
3767}
3768
3769static LRESULT CALLBACK
3771{
3773 BOOL bCancel = FALSE;
3774 LRESULT rc;
3775
3776 switch (uMsg)
3777 {
3778 case WM_PAINT:
3779 TRACE("WM_PAINT start\n");
3780 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3781 lParam);
3782 TRACE("WM_PAINT done\n");
3783 return rc;
3784
3785 case WM_KILLFOCUS:
3786 if (infoPtr->bIgnoreEditKillFocus)
3787 return TRUE;
3788 break;
3789
3790 case WM_DESTROY:
3791 {
3792 WNDPROC editProc = infoPtr->wpEditOrig;
3793 infoPtr->wpEditOrig = 0;
3795 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3796 }
3797
3798 case WM_GETDLGCODE:
3800
3801 case WM_KEYDOWN:
3802 if (wParam == VK_ESCAPE)
3803 {
3804 bCancel = TRUE;
3805 break;
3806 }
3807 else if (wParam == VK_RETURN)
3808 {
3809 break;
3810 }
3811
3812 /* fall through */
3813 default:
3814 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3815 }
3816
3817 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3818 /* eg. Using a messagebox */
3819
3820 infoPtr->bIgnoreEditKillFocus = TRUE;
3821 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3822 infoPtr->bIgnoreEditKillFocus = FALSE;
3823
3824 return 0;
3825}
3826
3827
3828/* should handle edit control messages here */
3829
3830static LRESULT
3832{
3833 TRACE("code=0x%x, id=0x%x, handle=0x%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3834
3835 switch (HIWORD(wParam))
3836 {
3837 case EN_UPDATE:
3838 {
3839 /*
3840 * Adjust the edit window size
3841 */
3842 WCHAR buffer[1024];
3843 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3844 HDC hdc = GetDC(infoPtr->hwndEdit);
3845 SIZE sz;
3846 HFONT hFont, hOldFont = 0;
3847
3848 TRACE("edit=%p\n", infoPtr->hwndEdit);
3849
3850 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3851
3852 infoPtr->bLabelChanged = TRUE;
3853
3855
3856 /* Select font to get the right dimension of the string */
3857 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3858
3859 if (hFont != 0)
3860 {
3861 hOldFont = SelectObject(hdc, hFont);
3862 }
3863
3865 {
3866 TEXTMETRICW textMetric;
3867
3868 /* Add Extra spacing for the next character */
3869 GetTextMetricsW(hdc, &textMetric);
3870 sz.cx += (textMetric.tmMaxCharWidth * 2);
3871
3872 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3873 sz.cx = min(sz.cx,
3874 infoPtr->clientWidth - editItem->textOffset + 2);
3875
3876 SetWindowPos(infoPtr->hwndEdit,
3877 HWND_TOP,
3878 0,
3879 0,
3880 sz.cx,
3881 editItem->rect.bottom - editItem->rect.top + 3,
3883 }
3884
3885 if (hFont != 0)
3886 {
3887 SelectObject(hdc, hOldFont);
3888 }
3889
3890 ReleaseDC(infoPtr->hwnd, hdc);
3891 break;
3892 }
3893 case EN_KILLFOCUS:
3894 /* apparently we should respect passed handle value */
3895 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3896
3898 break;
3899
3900 default:
3901 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3902 }
3903
3904 return 0;
3905}
3906
3907static HWND
3909{
3910 HWND hwnd = infoPtr->hwnd;
3911 HWND hwndEdit;
3912 SIZE sz;
3914 HDC hdc;
3915 HFONT hOldFont=0;
3916 TEXTMETRICW textMetric;
3917
3918 TRACE("%p %p\n", hwnd, hItem);
3919 if (!(infoPtr->dwStyle & TVS_EDITLABELS))
3920 return NULL;
3921
3922 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3923 return NULL;
3924
3925 if (infoPtr->hwndEdit)
3926 return infoPtr->hwndEdit;
3927
3928 infoPtr->bLabelChanged = FALSE;
3929
3930 /* make edit item visible */
3932
3934
3935 hdc = GetDC(hwnd);
3936 /* Select the font to get appropriate metric dimensions */
3937 if (infoPtr->hFont != 0)
3938 {
3939 hOldFont = SelectObject(hdc, infoPtr->hFont);
3940 }
3941
3942 /* Get string length in pixels */
3943 if (hItem->pszText)
3945 &sz);
3946 else
3947 GetTextExtentPoint32A(hdc, "", 0, &sz);
3948
3949 /* Add Extra spacing for the next character */
3950 GetTextMetricsW(hdc, &textMetric);
3951 sz.cx += (textMetric.tmMaxCharWidth * 2);
3952
3953 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3954 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3955
3956 if (infoPtr->hFont != 0)
3957 {
3958 SelectObject(hdc, hOldFont);
3959 }
3960
3961 ReleaseDC(hwnd, hdc);
3962
3963 infoPtr->editItem = hItem;
3964
3966 WC_EDITW,
3967 0,
3970 ES_LEFT, hItem->textOffset - 2,
3971 hItem->rect.top - 1, sz.cx + 3,
3972 hItem->rect.bottom -
3973 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3974/* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3975
3976 infoPtr->hwndEdit = hwndEdit;
3977
3978 /* Get a 2D border. */
3983
3986
3988 (DWORD_PTR)
3991 if (hItem->pszText)
3993
3995 {
3997 infoPtr->hwndEdit = 0;
3998 infoPtr->editItem = NULL;
3999 return NULL;
4000 }
4001
4005
4006 return hwndEdit;
4007}
4008
4009
4010static LRESULT
4012{
4013 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
4014 NMTVDISPINFOW tvdi;
4015 BOOL bCommit;
4016 WCHAR tmpText[MAX_PATH] = { '\0' };
4017 WCHAR *newText;
4018 int iLength = 0;
4019
4020 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
4021
4022 tvdi.item.mask = 0;
4023 tvdi.item.hItem = editedItem;
4024 tvdi.item.state = editedItem->state;
4025 tvdi.item.lParam = editedItem->lParam;
4026
4027 if (!bCancel)
4028 {
4029 if (!infoPtr->bNtfUnicode)
4030 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, ARRAY_SIZE(tmpText));
4031 else
4032 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, ARRAY_SIZE(tmpText));
4033
4034 tvdi.item.mask = TVIF_TEXT;
4035 tvdi.item.pszText = tmpText;
4036 tvdi.item.cchTextMax = ARRAY_SIZE(tmpText);
4037 }
4038 else
4039 {
4040 tvdi.item.pszText = NULL;
4041 tvdi.item.cchTextMax = 0;
4042 }
4043
4044 bCommit = TREEVIEW_SendRealNotify(infoPtr, TVN_ENDLABELEDITW, &tvdi.hdr);
4045
4046 if (!bCancel && bCommit) /* Apply the changes */
4047 {
4048 if (!infoPtr->bNtfUnicode)
4049 {
4050 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tvdi.item.pszText, -1, NULL, 0 );
4051 newText = heap_alloc(len * sizeof(WCHAR));
4052 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tvdi.item.pszText, -1, newText, len );
4053 iLength = len - 1;
4054 }
4055 else
4056 newText = tvdi.item.pszText;
4057
4058 if (lstrcmpW(newText, editedItem->pszText) != 0)
4059 {
4060 WCHAR *ptr = heap_realloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
4061 if (ptr == NULL)
4062 {
4063 ERR("OutOfMemory, cannot allocate space for label\n");
4064 if (newText != tmpText) heap_free(newText);
4065 DestroyWindow(infoPtr->hwndEdit);
4066 infoPtr->hwndEdit = 0;
4067 infoPtr->editItem = NULL;
4068 return FALSE;
4069 }
4070 else
4071 {
4072 editedItem->pszText = ptr;
4073 editedItem->cchTextMax = iLength + 1;
4074 lstrcpyW(editedItem->pszText, newText);
4075 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
4076 }
4077 }
4078 if (newText != tmpText) heap_free(newText);
4079 }
4080
4081 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
4082 DestroyWindow(infoPtr->hwndEdit);
4083 infoPtr->hwndEdit = 0;
4084 infoPtr->editItem = NULL;
4085 return TRUE;
4086}
4087
4088static LRESULT
4090{
4091 if (wParam != TV_EDIT_TIMER)
4092 {
4093 ERR("got unknown timer\n");
4094 return 1;
4095 }
4096
4097 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4098 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
4099
4100 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
4101
4102 return 0;
4103}
4104
4105
4106/* Mouse Tracking/Drag **************************************************/
4107
4108/***************************************************************************
4109 * This is quite unusual piece of code, but that's how it's implemented in
4110 * Windows.
4111 */
4112static LRESULT
4114{
4115 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
4116 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
4117 RECT r;
4118 MSG msg;
4119
4120 r.top = pt.y - cyDrag;
4121 r.left = pt.x - cxDrag;
4122 r.bottom = pt.y + cyDrag;
4123 r.right = pt.x + cxDrag;
4124