[COMCTL32] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / dll / win32 / comctl32 / treeview.c
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_FULLROWSELECT, 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 "comctl32.h"
44
45 #include <wine/exception.h>
46
47 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
48
49 /* internal structures */
50 typedef struct tagTREEVIEW_INFO
51 {
52 HWND hwnd;
53 HWND hwndNotify; /* Owner window to send notifications to */
54 DWORD dwStyle;
55 HTREEITEM root;
56 UINT uInternalStatus;
57 INT Timer;
58 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
59 INT cdmode; /* last custom draw setting */
60 UINT uScrollTime; /* max. time for scrolling in milliseconds */
61 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
62
63 UINT uItemHeight; /* item height */
64 BOOL bHeightSet;
65
66 LONG clientWidth; /* width of control window */
67 LONG clientHeight; /* height of control window */
68
69 LONG treeWidth; /* width of visible tree items */
70 LONG treeHeight; /* height of visible tree items */
71
72 UINT uIndent; /* indentation in pixels */
73 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
74 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
75 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
76 HTREEITEM editItem; /* item being edited with builtin edit box */
77
78 HTREEITEM firstVisible; /* handle to first visible item */
79 LONG maxVisibleOrder;
80 HTREEITEM dropItem; /* handle to item selected by drag cursor */
81 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
82 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
83 HIMAGELIST dragList; /* Bitmap of dragged item */
84 LONG scrollX;
85 INT wheelRemainder;
86 COLORREF clrBk;
87 COLORREF clrText;
88 COLORREF clrLine;
89 COLORREF clrInsertMark;
90 HFONT hFont;
91 HFONT hDefaultFont;
92 HFONT hBoldFont;
93 HFONT hUnderlineFont;
94 HFONT hBoldUnderlineFont;
95 HCURSOR hcurHand;
96 HWND hwndToolTip;
97
98 HWND hwndEdit;
99 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
100 BOOL bIgnoreEditKillFocus;
101 BOOL bLabelChanged;
102
103 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
104 HIMAGELIST himlNormal;
105 int normalImageHeight;
106 int normalImageWidth;
107 HIMAGELIST himlState;
108 int stateImageHeight;
109 int stateImageWidth;
110 HDPA items;
111
112 DWORD lastKeyPressTimestamp;
113 WPARAM charCode;
114 INT nSearchParamLength;
115 WCHAR szSearchParam[ MAX_PATH ];
116 } TREEVIEW_INFO;
117
118 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
119 {
120 HTREEITEM parent; /* handle to parent or 0 if at root */
121 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
122 HTREEITEM firstChild; /* handle to first child or 0 if no child */
123
124 UINT callbackMask;
125 UINT state;
126 UINT stateMask;
127 LPWSTR pszText;
128 int cchTextMax;
129 int iImage;
130 int iSelectedImage;
131 int iExpandedImage;
132 int cChildren;
133 LPARAM lParam;
134 int iIntegral; /* item height multiplier (1 is normal) */
135 int iLevel; /* indentation level:0=root level */
136 HTREEITEM lastChild;
137 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
138 RECT rect;
139 LONG linesOffset;
140 LONG stateOffset;
141 LONG imageOffset;
142 LONG textOffset;
143 LONG textWidth; /* horizontal text extent for pszText */
144 LONG visibleOrder; /* visible ordering, 0 is first visible item */
145 const TREEVIEW_INFO *infoPtr; /* tree data this item belongs to */
146 } TREEVIEW_ITEM;
147
148 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
149 #define KEY_DELAY 450
150
151 /* bitflags for infoPtr->uInternalStatus */
152
153 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
154 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
155 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
156 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
157 #define TV_RDRAG 0x10 /* ditto Rbutton */
158 #define TV_RDRAGGING 0x20
159
160 /* bitflags for infoPtr->timer */
161
162 #define TV_EDIT_TIMER 2
163 #define TV_EDIT_TIMER_SET 2
164
165 #define TEXT_CALLBACK_SIZE 260
166
167 #define TREEVIEW_LEFT_MARGIN 8
168
169 #define MINIMUM_INDENT 19
170
171 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
172
173 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
174 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
175 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
176
177 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
178 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
179 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
180 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
181
182 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
183
184
185 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
186
187
188 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
189
190 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
191 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
192 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
193 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
194 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
195 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
196
197 /* Random Utilities *****************************************************/
198 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
199
200 /* Returns the treeview private data if hwnd is a treeview.
201 * Otherwise returns an undefined value. */
202 static inline TREEVIEW_INFO *
203 TREEVIEW_GetInfoPtr(HWND hwnd)
204 {
205 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
206 }
207
208 /* Don't call this. Nothing wants an item index. */
209 static inline int
210 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
211 {
212 return DPA_GetPtrIndex(infoPtr->items, handle);
213 }
214
215 /* Checks if item has changed and needs to be redrawn */
216 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
217 const TVITEMEXW *tvChange)
218 {
219 /* Number of children has changed */
220 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
221 return TRUE;
222
223 /* Image has changed and it's not a callback */
224 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
225 tiNew->iImage != I_IMAGECALLBACK)
226 return TRUE;
227
228 /* Selected image has changed and it's not a callback */
229 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
230 tiNew->iSelectedImage != I_IMAGECALLBACK)
231 return TRUE;
232
233 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
234 tiNew->iExpandedImage != I_IMAGECALLBACK)
235 return TRUE;
236
237 /* Text has changed and it's not a callback */
238 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
239 tiNew->pszText != LPSTR_TEXTCALLBACKW)
240 return TRUE;
241
242 /* Indent has changed */
243 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
244 return TRUE;
245
246 /* Item state has changed */
247 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
248 return TRUE;
249
250 return FALSE;
251 }
252
253 /***************************************************************************
254 * This method checks that handle is an item for this tree.
255 */
256 static BOOL
257 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
258 {
259 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
260 {
261 TRACE("invalid item %p\n", handle);
262 return FALSE;
263 }
264 else
265 return TRUE;
266 }
267
268 static HFONT
269 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
270 {
271 LOGFONTW font;
272
273 GetObjectW(hOrigFont, sizeof(font), &font);
274 font.lfWeight = FW_BOLD;
275 return CreateFontIndirectW(&font);
276 }
277
278 static HFONT
279 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
280 {
281 LOGFONTW font;
282
283 GetObjectW(hOrigFont, sizeof(font), &font);
284 font.lfUnderline = TRUE;
285 return CreateFontIndirectW(&font);
286 }
287
288 static HFONT
289 TREEVIEW_CreateBoldUnderlineFont(HFONT hfont)
290 {
291 LOGFONTW font;
292
293 GetObjectW(hfont, sizeof(font), &font);
294 font.lfWeight = FW_BOLD;
295 font.lfUnderline = TRUE;
296 return CreateFontIndirectW(&font);
297 }
298
299 static inline HFONT
300 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
301 {
302 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
303 return item->state & TVIS_BOLD ? infoPtr->hBoldUnderlineFont : infoPtr->hUnderlineFont;
304 if (item->state & TVIS_BOLD)
305 return infoPtr->hBoldFont;
306 return infoPtr->hFont;
307 }
308
309 /* for trace/debugging purposes only */
310 static const char *
311 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
312 {
313 if (item == NULL) return "<null item>";
314 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
315 if (item->pszText == NULL) return "<null>";
316 return debugstr_w(item->pszText);
317 }
318
319 /* An item is not a child of itself. */
320 static BOOL
321 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
322 {
323 do
324 {
325 child = child->parent;
326 if (child == parent) return TRUE;
327 } while (child != NULL);
328
329 return FALSE;
330 }
331
332
333 /* Tree Traversal *******************************************************/
334
335 /***************************************************************************
336 * This method returns the last expanded sibling or child child item
337 * of a tree node
338 */
339 static TREEVIEW_ITEM *
340 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
341 {
342 if (!item) return NULL;
343
344 while (item->lastChild)
345 {
346 if (item->state & TVIS_EXPANDED)
347 item = item->lastChild;
348 else
349 break;
350 }
351
352 if (item == infoPtr->root)
353 return NULL;
354
355 return item;
356 }
357
358 /***************************************************************************
359 * This method returns the previous non-hidden item in the list not
360 * considering the tree hierarchy.
361 */
362 static TREEVIEW_ITEM *
363 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
364 {
365 if (tvItem->prevSibling)
366 {
367 /* This item has a prevSibling, get the last item in the sibling's tree. */
368 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
369
370 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
371 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
372 else
373 return upItem;
374 }
375 else
376 {
377 /* this item does not have a prevSibling, get the parent */
378 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
379 }
380 }
381
382
383 /***************************************************************************
384 * This method returns the next physical item in the treeview not
385 * considering the tree hierarchy.
386 */
387 static TREEVIEW_ITEM *
388 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
389 {
390 /*
391 * If this item has children and is expanded, return the first child
392 */
393 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
394 {
395 return tvItem->firstChild;
396 }
397
398
399 /*
400 * try to get the sibling
401 */
402 if (tvItem->nextSibling)
403 return tvItem->nextSibling;
404
405 /*
406 * Otherwise, get the parent's sibling.
407 */
408 while (tvItem->parent)
409 {
410 tvItem = tvItem->parent;
411
412 if (tvItem->nextSibling)
413 return tvItem->nextSibling;
414 }
415
416 return NULL;
417 }
418
419 /***************************************************************************
420 * This method returns the nth item starting at the given item. It returns
421 * the last item (or first) we we run out of items.
422 *
423 * Will scroll backward if count is <0.
424 * forward if count is >0.
425 */
426 static TREEVIEW_ITEM *
427 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
428 LONG count)
429 {
430 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
431 TREEVIEW_ITEM *previousItem;
432
433 assert(item != NULL);
434
435 if (count > 0)
436 {
437 next_item = TREEVIEW_GetNextListItem;
438 }
439 else if (count < 0)
440 {
441 count = -count;
442 next_item = TREEVIEW_GetPrevListItem;
443 }
444 else
445 return item;
446
447 do
448 {
449 previousItem = item;
450 item = next_item(infoPtr, item);
451
452 } while (--count && item != NULL);
453
454
455 return item ? item : previousItem;
456 }
457
458 /* Notifications ************************************************************/
459
460 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
461 {
462 if (!infoPtr->bNtfUnicode) {
463 switch (code) {
464 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
465 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
466 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
467 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
468 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
469 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
470 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
471 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
472 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
473 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
474 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
475 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
476 }
477 }
478 return code;
479 }
480
481 static inline BOOL
482 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, UINT code, NMHDR *hdr)
483 {
484 TRACE("code=%d, hdr=%p\n", code, hdr);
485
486 hdr->hwndFrom = infoPtr->hwnd;
487 hdr->idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
488 hdr->code = get_notifycode(infoPtr, code);
489
490 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, hdr->idFrom, (LPARAM)hdr);
491 }
492
493 static BOOL
494 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
495 {
496 NMHDR hdr;
497 return TREEVIEW_SendRealNotify(infoPtr, code, &hdr);
498 }
499
500 static VOID
501 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
502 {
503 tvItem->mask = mask;
504 tvItem->hItem = item;
505 tvItem->state = item->state;
506 tvItem->stateMask = 0;
507 tvItem->iImage = item->iImage;
508 tvItem->iSelectedImage = item->iSelectedImage;
509 tvItem->cChildren = item->cChildren;
510 tvItem->lParam = item->lParam;
511
512 if(mask & TVIF_TEXT)
513 {
514 if (!infoPtr->bNtfUnicode)
515 {
516 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
517 tvItem->pszText = Alloc (tvItem->cchTextMax);
518 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
519 }
520 else
521 {
522 tvItem->cchTextMax = item->cchTextMax;
523 tvItem->pszText = item->pszText;
524 }
525 }
526 else
527 {
528 tvItem->cchTextMax = 0;
529 tvItem->pszText = NULL;
530 }
531 }
532
533 static BOOL
534 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
535 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
536 {
537 NMTREEVIEWW nmhdr;
538 BOOL ret;
539
540 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
541 code, action, oldItem, newItem);
542
543 memset(&nmhdr, 0, sizeof(NMTREEVIEWW));
544 nmhdr.action = action;
545
546 if (oldItem)
547 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
548
549 if (newItem)
550 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
551
552 nmhdr.ptDrag.x = 0;
553 nmhdr.ptDrag.y = 0;
554
555 ret = TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
556 if (!infoPtr->bNtfUnicode)
557 {
558 Free(nmhdr.itemOld.pszText);
559 Free(nmhdr.itemNew.pszText);
560 }
561 return ret;
562 }
563
564 static BOOL
565 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
566 HTREEITEM dragItem, POINT pt)
567 {
568 NMTREEVIEWW nmhdr;
569
570 TRACE("code:%d dragitem:%p\n", code, dragItem);
571
572 nmhdr.action = 0;
573 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
574 nmhdr.itemNew.hItem = dragItem;
575 nmhdr.itemNew.state = dragItem->state;
576 nmhdr.itemNew.lParam = dragItem->lParam;
577
578 nmhdr.ptDrag.x = pt.x;
579 nmhdr.ptDrag.y = pt.y;
580
581 return TREEVIEW_SendRealNotify(infoPtr, code, &nmhdr.hdr);
582 }
583
584
585 static BOOL
586 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
587 HDC hdc, RECT rc)
588 {
589 NMTVCUSTOMDRAW nmcdhdr;
590 NMCUSTOMDRAW *nmcd;
591
592 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
593
594 nmcd = &nmcdhdr.nmcd;
595 nmcd->dwDrawStage = dwDrawStage;
596 nmcd->hdc = hdc;
597 nmcd->rc = rc;
598 nmcd->dwItemSpec = 0;
599 nmcd->uItemState = 0;
600 nmcd->lItemlParam = 0;
601 nmcdhdr.clrText = infoPtr->clrText;
602 nmcdhdr.clrTextBk = infoPtr->clrBk;
603 nmcdhdr.iLevel = 0;
604
605 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr.nmcd.hdr);
606 }
607
608 /* FIXME: need to find out when the flags in uItemState need to be set */
609
610 static BOOL
611 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
612 TREEVIEW_ITEM *item, UINT uItemDrawState,
613 NMTVCUSTOMDRAW *nmcdhdr)
614 {
615 NMCUSTOMDRAW *nmcd;
616 DWORD dwDrawStage;
617 DWORD_PTR dwItemSpec;
618 UINT uItemState;
619
620 dwDrawStage = CDDS_ITEM | uItemDrawState;
621 dwItemSpec = (DWORD_PTR)item;
622 uItemState = 0;
623 if (item->state & TVIS_SELECTED)
624 uItemState |= CDIS_SELECTED;
625 if (item == infoPtr->selectedItem)
626 uItemState |= CDIS_FOCUS;
627 if (item == infoPtr->hotItem)
628 uItemState |= CDIS_HOT;
629
630 nmcd = &nmcdhdr->nmcd;
631 nmcd->dwDrawStage = dwDrawStage;
632 nmcd->hdc = hdc;
633 nmcd->rc = item->rect;
634 nmcd->dwItemSpec = dwItemSpec;
635 nmcd->uItemState = uItemState;
636 nmcd->lItemlParam = item->lParam;
637 nmcdhdr->iLevel = item->iLevel;
638
639 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
640 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
641 nmcd->uItemState, nmcd->lItemlParam);
642
643 return TREEVIEW_SendRealNotify(infoPtr, NM_CUSTOMDRAW, &nmcdhdr->nmcd.hdr);
644 }
645
646 static BOOL
647 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
648 {
649 NMTVDISPINFOW tvdi;
650 BOOL ret;
651
652 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
653 &tvdi.item, editItem);
654
655 ret = TREEVIEW_SendRealNotify(infoPtr, TVN_BEGINLABELEDITW, &tvdi.hdr);
656
657 if (!infoPtr->bNtfUnicode)
658 Free(tvdi.item.pszText);
659
660 return ret;
661 }
662
663 static void
664 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
665 UINT mask)
666 {
667 NMTVDISPINFOEXW callback;
668
669 TRACE("mask=0x%x, callbackmask=0x%x\n", mask, item->callbackMask);
670 mask &= item->callbackMask;
671
672 if (mask == 0) return;
673
674 /* 'state' always contains valid value, as well as 'lParam'.
675 * All other parameters are uninitialized.
676 */
677 callback.item.pszText = item->pszText;
678 callback.item.cchTextMax = item->cchTextMax;
679 callback.item.mask = mask;
680 callback.item.hItem = item;
681 callback.item.state = item->state;
682 callback.item.lParam = item->lParam;
683
684 /* If text is changed we need to recalculate textWidth */
685 if (mask & TVIF_TEXT)
686 item->textWidth = 0;
687
688 TREEVIEW_SendRealNotify(infoPtr, TVN_GETDISPINFOW, &callback.hdr);
689 TRACE("resulting code 0x%08x\n", callback.hdr.code);
690
691 /* It may have changed due to a call to SetItem. */
692 mask &= item->callbackMask;
693
694 if ((mask & TVIF_TEXT) && callback.item.pszText != item->pszText)
695 {
696 /* Instead of copying text into our buffer user specified his own */
697 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
698 LPWSTR newText;
699 int buflen;
700 int len = MultiByteToWideChar( CP_ACP, 0,
701 (LPSTR)callback.item.pszText, -1,
702 NULL, 0);
703 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
704 newText = ReAlloc(item->pszText, buflen);
705
706 TRACE("returned str %s, len=%d, buflen=%d\n",
707 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
708
709 if (newText)
710 {
711 item->pszText = newText;
712 MultiByteToWideChar( CP_ACP, 0,
713 (LPSTR)callback.item.pszText, -1,
714 item->pszText, buflen/sizeof(WCHAR));
715 item->cchTextMax = buflen/sizeof(WCHAR);
716 }
717 /* If ReAlloc fails we have nothing to do, but keep original text */
718 }
719 else {
720 int len = max(lstrlenW(callback.item.pszText) + 1,
721 TEXT_CALLBACK_SIZE);
722 LPWSTR newText = ReAlloc(item->pszText, len);
723
724 TRACE("returned wstr %s, len=%d\n",
725 debugstr_w(callback.item.pszText), len);
726
727 if (newText)
728 {
729 item->pszText = newText;
730 strcpyW(item->pszText, callback.item.pszText);
731 item->cchTextMax = len;
732 }
733 /* If ReAlloc fails we have nothing to do, but keep original text */
734 }
735 }
736 else if (mask & TVIF_TEXT) {
737 /* User put text into our buffer, that is ok unless A string */
738 if (!infoPtr->bNtfUnicode && (callback.hdr.code == TVN_GETDISPINFOA)) {
739 LPWSTR newText;
740 int buflen;
741 int len = MultiByteToWideChar( CP_ACP, 0,
742 (LPSTR)callback.item.pszText, -1,
743 NULL, 0);
744 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
745 newText = Alloc(buflen);
746
747 TRACE("same buffer str %s, len=%d, buflen=%d\n",
748 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
749
750 if (newText)
751 {
752 LPWSTR oldText = item->pszText;
753 item->pszText = newText;
754 MultiByteToWideChar( CP_ACP, 0,
755 (LPSTR)callback.item.pszText, -1,
756 item->pszText, buflen/sizeof(WCHAR));
757 item->cchTextMax = buflen/sizeof(WCHAR);
758 Free(oldText);
759 }
760 }
761 }
762
763 if (mask & TVIF_IMAGE)
764 item->iImage = callback.item.iImage;
765
766 if (mask & TVIF_SELECTEDIMAGE)
767 item->iSelectedImage = callback.item.iSelectedImage;
768
769 if (mask & TVIF_EXPANDEDIMAGE)
770 item->iExpandedImage = callback.item.iExpandedImage;
771
772 if (mask & TVIF_CHILDREN)
773 item->cChildren = callback.item.cChildren;
774
775 if (callback.item.mask & TVIF_STATE)
776 {
777 item->state &= ~callback.item.stateMask;
778 item->state |= (callback.item.state & callback.item.stateMask);
779 }
780
781 /* These members are now permanently set. */
782 if (callback.item.mask & TVIF_DI_SETITEM)
783 item->callbackMask &= ~callback.item.mask;
784 }
785
786 /***************************************************************************
787 * This function uses cChildren field to decide whether the item has
788 * children or not.
789 * Note: if this returns TRUE, the child items may not actually exist,
790 * they could be virtual.
791 *
792 * Just use item->firstChild to check for physical children.
793 */
794 static BOOL
795 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
796 {
797 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_CHILDREN);
798
799 return item->cChildren > 0;
800 }
801
802 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
803 {
804 INT format;
805
806 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
807
808 if (nCommand != NF_REQUERY) return 0;
809
810 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
811 TRACE("format=%d\n", format);
812
813 /* Invalid format returned by NF_QUERY defaults to ANSI*/
814 if (format != NFR_ANSI && format != NFR_UNICODE)
815 format = NFR_ANSI;
816
817 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
818
819 return format;
820 }
821
822 /* Item Position ********************************************************/
823
824 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
825 static VOID
826 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
827 {
828 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
829 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
830 > TVS_LINESATROOT);
831
832 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
833 - infoPtr->scrollX;
834 item->stateOffset = item->linesOffset + infoPtr->uIndent;
835 item->imageOffset = item->stateOffset
836 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
837 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
838 }
839
840 static VOID
841 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
842 {
843 HDC hdc;
844 HFONT hOldFont=0;
845 SIZE sz;
846
847 /* DRAW's OM docker creates items like this */
848 if (item->pszText == NULL)
849 {
850 item->textWidth = 0;
851 return;
852 }
853
854 if (hDC != 0)
855 {
856 hdc = hDC;
857 }
858 else
859 {
860 hdc = GetDC(infoPtr->hwnd);
861 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
862 }
863
864 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
865 item->textWidth = sz.cx;
866
867 if (hDC == 0)
868 {
869 SelectObject(hdc, hOldFont);
870 ReleaseDC(0, hdc);
871 }
872 }
873
874 static VOID
875 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
876 {
877 item->rect.top = infoPtr->uItemHeight *
878 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
879
880 item->rect.bottom = item->rect.top
881 + infoPtr->uItemHeight * item->iIntegral - 1;
882
883 item->rect.left = 0;
884 item->rect.right = infoPtr->clientWidth;
885 }
886
887 /* We know that only items after start need their order updated. */
888 static void
889 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
890 {
891 TREEVIEW_ITEM *item;
892 int order;
893
894 if (!start)
895 {
896 start = infoPtr->root->firstChild;
897 order = 0;
898 }
899 else
900 order = start->visibleOrder;
901
902 for (item = start; item != NULL;
903 item = TREEVIEW_GetNextListItem(infoPtr, item))
904 {
905 if (!ISVISIBLE(item) && order > 0)
906 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
907 item->visibleOrder = order;
908 order += item->iIntegral;
909 }
910
911 infoPtr->maxVisibleOrder = order;
912
913 for (item = start; item != NULL;
914 item = TREEVIEW_GetNextListItem(infoPtr, item))
915 {
916 TREEVIEW_ComputeItemRect(infoPtr, item);
917 }
918 }
919
920
921 /* Update metrics of all items in selected subtree.
922 * root must be expanded
923 */
924 static VOID
925 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
926 {
927 TREEVIEW_ITEM *sibling;
928 HDC hdc;
929 HFONT hOldFont;
930
931 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
932 return;
933
934 root->state &= ~TVIS_EXPANDED;
935 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
936 root->state |= TVIS_EXPANDED;
937
938 hdc = GetDC(infoPtr->hwnd);
939 hOldFont = SelectObject(hdc, infoPtr->hFont);
940
941 for (; root != sibling;
942 root = TREEVIEW_GetNextListItem(infoPtr, root))
943 {
944 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
945
946 if (root->callbackMask & TVIF_TEXT)
947 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
948
949 if (root->textWidth == 0)
950 {
951 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
952 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
953 }
954 }
955
956 SelectObject(hdc, hOldFont);
957 ReleaseDC(infoPtr->hwnd, hdc);
958 }
959
960 /* Item Allocation **********************************************************/
961
962 static TREEVIEW_ITEM *
963 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
964 {
965 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
966
967 if (!newItem)
968 return NULL;
969
970 /* I_IMAGENONE would make more sense but this is neither what is
971 * documented (MSDN doesn't specify) nor what Windows actually does
972 * (it sets it to zero)... and I can so imagine an application using
973 * inc/dec to toggle the images. */
974 newItem->iImage = 0;
975 newItem->iSelectedImage = 0;
976 newItem->iExpandedImage = (WORD)I_IMAGENONE;
977 newItem->infoPtr = infoPtr;
978
979 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
980 {
981 Free(newItem);
982 return NULL;
983 }
984
985 return newItem;
986 }
987
988 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
989 * free item->pszText. */
990 static void
991 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
992 {
993 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
994 if (infoPtr->selectedItem == item)
995 infoPtr->selectedItem = NULL;
996 if (infoPtr->hotItem == item)
997 infoPtr->hotItem = NULL;
998 if (infoPtr->focusedItem == item)
999 infoPtr->focusedItem = NULL;
1000 if (infoPtr->firstVisible == item)
1001 infoPtr->firstVisible = NULL;
1002 if (infoPtr->dropItem == item)
1003 infoPtr->dropItem = NULL;
1004 if (infoPtr->insertMarkItem == item)
1005 infoPtr->insertMarkItem = NULL;
1006 Free(item);
1007 }
1008
1009
1010 /* Item Insertion *******************************************************/
1011
1012 /***************************************************************************
1013 * This method inserts newItem before sibling as a child of parent.
1014 * sibling can be NULL, but only if parent has no children.
1015 */
1016 static void
1017 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1018 TREEVIEW_ITEM *parent)
1019 {
1020 assert(parent != NULL);
1021
1022 if (sibling != NULL)
1023 {
1024 assert(sibling->parent == parent);
1025
1026 if (sibling->prevSibling != NULL)
1027 sibling->prevSibling->nextSibling = newItem;
1028
1029 newItem->prevSibling = sibling->prevSibling;
1030 sibling->prevSibling = newItem;
1031 }
1032 else
1033 newItem->prevSibling = NULL;
1034
1035 newItem->nextSibling = sibling;
1036
1037 if (parent->firstChild == sibling)
1038 parent->firstChild = newItem;
1039
1040 if (parent->lastChild == NULL)
1041 parent->lastChild = newItem;
1042 }
1043
1044 /***************************************************************************
1045 * This method inserts newItem after sibling as a child of parent.
1046 * sibling can be NULL, but only if parent has no children.
1047 */
1048 static void
1049 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1050 TREEVIEW_ITEM *parent)
1051 {
1052 assert(parent != NULL);
1053
1054 if (sibling != NULL)
1055 {
1056 assert(sibling->parent == parent);
1057
1058 if (sibling->nextSibling != NULL)
1059 sibling->nextSibling->prevSibling = newItem;
1060
1061 newItem->nextSibling = sibling->nextSibling;
1062 sibling->nextSibling = newItem;
1063 }
1064 else
1065 newItem->nextSibling = NULL;
1066
1067 newItem->prevSibling = sibling;
1068
1069 if (parent->lastChild == sibling)
1070 parent->lastChild = newItem;
1071
1072 if (parent->firstChild == NULL)
1073 parent->firstChild = newItem;
1074 }
1075
1076 static BOOL
1077 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item,
1078 const TVITEMEXW *tvItem, BOOL isW)
1079 {
1080 UINT callbackClear = 0;
1081 UINT callbackSet = 0;
1082
1083 TRACE("item %p\n", item);
1084 /* Do this first in case it fails. */
1085 if (tvItem->mask & TVIF_TEXT)
1086 {
1087 item->textWidth = 0; /* force width recalculation */
1088 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1089 {
1090 int len;
1091 LPWSTR newText;
1092 if (isW)
1093 len = lstrlenW(tvItem->pszText) + 1;
1094 else
1095 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1096
1097 newText = ReAlloc(item->pszText, len * sizeof(WCHAR));
1098
1099 if (newText == NULL) return FALSE;
1100
1101 callbackClear |= TVIF_TEXT;
1102
1103 item->pszText = newText;
1104 item->cchTextMax = len;
1105 if (isW)
1106 lstrcpynW(item->pszText, tvItem->pszText, len);
1107 else
1108 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1109 item->pszText, len);
1110
1111 TRACE("setting text %s, item %p\n", debugstr_w(item->pszText), item);
1112 }
1113 else
1114 {
1115 callbackSet |= TVIF_TEXT;
1116
1117 item->pszText = ReAlloc(item->pszText,
1118 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1119 item->cchTextMax = TEXT_CALLBACK_SIZE;
1120 TRACE("setting callback, item %p\n", item);
1121 }
1122 }
1123
1124 if (tvItem->mask & TVIF_CHILDREN)
1125 {
1126 item->cChildren = tvItem->cChildren;
1127
1128 if (item->cChildren == I_CHILDRENCALLBACK)
1129 callbackSet |= TVIF_CHILDREN;
1130 else
1131 callbackClear |= TVIF_CHILDREN;
1132 }
1133
1134 if (tvItem->mask & TVIF_IMAGE)
1135 {
1136 item->iImage = tvItem->iImage;
1137
1138 if (item->iImage == I_IMAGECALLBACK)
1139 callbackSet |= TVIF_IMAGE;
1140 else
1141 callbackClear |= TVIF_IMAGE;
1142 }
1143
1144 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1145 {
1146 item->iSelectedImage = tvItem->iSelectedImage;
1147
1148 if (item->iSelectedImage == I_IMAGECALLBACK)
1149 callbackSet |= TVIF_SELECTEDIMAGE;
1150 else
1151 callbackClear |= TVIF_SELECTEDIMAGE;
1152 }
1153
1154 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
1155 {
1156 item->iExpandedImage = tvItem->iExpandedImage;
1157
1158 if (item->iExpandedImage == I_IMAGECALLBACK)
1159 callbackSet |= TVIF_EXPANDEDIMAGE;
1160 else
1161 callbackClear |= TVIF_EXPANDEDIMAGE;
1162 }
1163
1164 if (tvItem->mask & TVIF_PARAM)
1165 item->lParam = tvItem->lParam;
1166
1167 /* If the application sets TVIF_INTEGRAL without
1168 * supplying a TVITEMEX structure, it's toast. */
1169 if (tvItem->mask & TVIF_INTEGRAL)
1170 item->iIntegral = tvItem->iIntegral;
1171
1172 if (tvItem->mask & TVIF_STATE)
1173 {
1174 TRACE("prevstate,state,mask:%x,%x,%x\n", item->state, tvItem->state,
1175 tvItem->stateMask);
1176 item->state &= ~tvItem->stateMask;
1177 item->state |= (tvItem->state & tvItem->stateMask);
1178 }
1179
1180 if (tvItem->mask & TVIF_STATEEX)
1181 {
1182 FIXME("New extended state: %x\n", tvItem->uStateEx);
1183 }
1184
1185 item->callbackMask |= callbackSet;
1186 item->callbackMask &= ~callbackClear;
1187
1188 return TRUE;
1189 }
1190
1191 /* Note that the new item is pre-zeroed. */
1192 static LRESULT
1193 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1194 {
1195 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1196 HTREEITEM insertAfter;
1197 TREEVIEW_ITEM *newItem, *parentItem;
1198 BOOL bTextUpdated = FALSE;
1199
1200 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1201 {
1202 parentItem = infoPtr->root;
1203 }
1204 else
1205 {
1206 parentItem = ptdi->hParent;
1207
1208 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1209 {
1210 WARN("invalid parent %p\n", parentItem);
1211 return 0;
1212 }
1213 }
1214
1215 insertAfter = ptdi->hInsertAfter;
1216
1217 /* Validate this now for convenience. */
1218 switch ((DWORD_PTR)insertAfter)
1219 {
1220 case (DWORD_PTR)TVI_FIRST:
1221 case (DWORD_PTR)TVI_LAST:
1222 case (DWORD_PTR)TVI_SORT:
1223 break;
1224
1225 default:
1226 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1227 insertAfter->parent != parentItem)
1228 {
1229 WARN("invalid insert after %p\n", insertAfter);
1230 insertAfter = TVI_LAST;
1231 }
1232 }
1233
1234 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1235 (tvItem->mask & TVIF_TEXT)
1236 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1237 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1238 : "<no label>");
1239
1240 newItem = TREEVIEW_AllocateItem(infoPtr);
1241 if (newItem == NULL)
1242 return 0;
1243
1244 newItem->parent = parentItem;
1245 newItem->iIntegral = 1;
1246 newItem->visibleOrder = -1;
1247
1248 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1249 return 0;
1250
1251 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1252
1253 infoPtr->uNumItems++;
1254
1255 switch ((DWORD_PTR)insertAfter)
1256 {
1257 case (DWORD_PTR)TVI_FIRST:
1258 {
1259 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1260 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1261 if (infoPtr->firstVisible == originalFirst)
1262 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1263 }
1264 break;
1265
1266 case (DWORD_PTR)TVI_LAST:
1267 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1268 break;
1269
1270 /* hInsertAfter names a specific item we want to insert after */
1271 default:
1272 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1273 break;
1274
1275 case (DWORD_PTR)TVI_SORT:
1276 {
1277 TREEVIEW_ITEM *aChild;
1278 TREEVIEW_ITEM *previousChild = NULL;
1279 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1280 BOOL bItemInserted = FALSE;
1281
1282 aChild = parentItem->firstChild;
1283
1284 bTextUpdated = TRUE;
1285 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1286
1287 /* Iterate the parent children to see where we fit in */
1288 while (aChild != NULL)
1289 {
1290 INT comp;
1291
1292 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1293 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1294
1295 if (comp < 0) /* we are smaller than the current one */
1296 {
1297 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1298 if (infoPtr->firstVisible == originalFirst &&
1299 aChild == originalFirst)
1300 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1301 bItemInserted = TRUE;
1302 break;
1303 }
1304 else if (comp > 0) /* we are bigger than the current one */
1305 {
1306 previousChild = aChild;
1307
1308 /* This will help us to exit if there is no more sibling */
1309 aChild = (aChild->nextSibling == 0)
1310 ? NULL
1311 : aChild->nextSibling;
1312
1313 /* Look at the next item */
1314 continue;
1315 }
1316 else if (comp == 0)
1317 {
1318 /*
1319 * An item with this name is already existing, therefore,
1320 * we add after the one we found
1321 */
1322 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1323 bItemInserted = TRUE;
1324 break;
1325 }
1326 }
1327
1328 /*
1329 * we reach the end of the child list and the item has not
1330 * yet been inserted, therefore, insert it after the last child.
1331 */
1332 if ((!bItemInserted) && (aChild == NULL))
1333 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1334
1335 break;
1336 }
1337 }
1338
1339
1340 TRACE("new item %p; parent %p, mask %x\n", newItem,
1341 newItem->parent, tvItem->mask);
1342
1343 newItem->iLevel = newItem->parent->iLevel + 1;
1344
1345 if (newItem->parent->cChildren == 0)
1346 newItem->parent->cChildren = 1;
1347
1348 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1349 {
1350 if (STATEIMAGEINDEX(newItem->state) == 0)
1351 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1352 }
1353
1354 if (infoPtr->firstVisible == NULL)
1355 infoPtr->firstVisible = newItem;
1356
1357 TREEVIEW_VerifyTree(infoPtr);
1358
1359 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1360
1361 if (parentItem == infoPtr->root ||
1362 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1363 {
1364 TREEVIEW_ITEM *item;
1365 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1366
1367 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1368 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1369
1370 if (!bTextUpdated)
1371 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1372
1373 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1374 TREEVIEW_UpdateScrollBars(infoPtr);
1375 /*
1376 * if the item was inserted in a visible part of the tree,
1377 * invalidate it, as well as those after it
1378 */
1379 for (item = newItem;
1380 item != NULL;
1381 item = TREEVIEW_GetNextListItem(infoPtr, item))
1382 TREEVIEW_Invalidate(infoPtr, item);
1383 }
1384 else
1385 {
1386 /* refresh treeview if newItem is the first item inserted under parentItem */
1387 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1388 {
1389 /* parent got '+' - update it */
1390 TREEVIEW_Invalidate(infoPtr, parentItem);
1391 }
1392 }
1393
1394 return (LRESULT)newItem;
1395 }
1396
1397 /* Item Deletion ************************************************************/
1398 static void
1399 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
1400
1401 static void
1402 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1403 {
1404 TREEVIEW_ITEM *kill = parentItem->firstChild;
1405
1406 while (kill != NULL)
1407 {
1408 TREEVIEW_ITEM *next = kill->nextSibling;
1409
1410 TREEVIEW_RemoveItem(infoPtr, kill);
1411
1412 kill = next;
1413 }
1414
1415 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1416 assert(parentItem->firstChild == NULL);
1417 assert(parentItem->lastChild == NULL);
1418 }
1419
1420 static void
1421 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1422 {
1423 TREEVIEW_ITEM *parentItem = item->parent;
1424
1425 assert(item != NULL);
1426 assert(item->parent != NULL); /* i.e. it must not be the root */
1427
1428 if (parentItem->firstChild == item)
1429 parentItem->firstChild = item->nextSibling;
1430
1431 if (parentItem->lastChild == item)
1432 parentItem->lastChild = item->prevSibling;
1433
1434 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1435 && parentItem->cChildren > 0)
1436 parentItem->cChildren = 0;
1437
1438 if (item->prevSibling)
1439 item->prevSibling->nextSibling = item->nextSibling;
1440
1441 if (item->nextSibling)
1442 item->nextSibling->prevSibling = item->prevSibling;
1443 }
1444
1445 static void
1446 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1447 {
1448 TRACE("%p, (%s)\n", item, TREEVIEW_ItemName(item));
1449
1450 if (item->firstChild)
1451 TREEVIEW_RemoveAllChildren(infoPtr, item);
1452
1453 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1454 TVIF_HANDLE | TVIF_PARAM, item, 0);
1455
1456 TREEVIEW_UnlinkItem(item);
1457
1458 infoPtr->uNumItems--;
1459
1460 if (item->pszText != LPSTR_TEXTCALLBACKW)
1461 Free(item->pszText);
1462
1463 TREEVIEW_FreeItem(infoPtr, item);
1464 }
1465
1466
1467 /* Empty out the tree. */
1468 static void
1469 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1470 {
1471 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1472
1473 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1474 }
1475
1476 static LRESULT
1477 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM item)
1478 {
1479 TREEVIEW_ITEM *newSelection = NULL;
1480 TREEVIEW_ITEM *newFirstVisible = NULL;
1481 TREEVIEW_ITEM *parent, *prev = NULL;
1482 BOOL visible = FALSE;
1483
1484 if (item == TVI_ROOT || !item)
1485 {
1486 TRACE("TVI_ROOT\n");
1487 parent = infoPtr->root;
1488 newSelection = NULL;
1489 visible = TRUE;
1490 TREEVIEW_RemoveTree(infoPtr);
1491 }
1492 else
1493 {
1494 if (!TREEVIEW_ValidItem(infoPtr, item))
1495 return FALSE;
1496
1497 TRACE("%p (%s)\n", item, TREEVIEW_ItemName(item));
1498 parent = item->parent;
1499
1500 if (ISVISIBLE(item))
1501 {
1502 prev = TREEVIEW_GetPrevListItem(infoPtr, item);
1503 visible = TRUE;
1504 }
1505
1506 if (infoPtr->selectedItem != NULL
1507 && (item == infoPtr->selectedItem
1508 || TREEVIEW_IsChildOf(item, infoPtr->selectedItem)))
1509 {
1510 if (item->nextSibling)
1511 newSelection = item->nextSibling;
1512 else if (item->parent != infoPtr->root)
1513 newSelection = item->parent;
1514 else
1515 newSelection = item->prevSibling;
1516 TRACE("newSelection = %p\n", newSelection);
1517 }
1518
1519 if (infoPtr->firstVisible == item)
1520 {
1521 if (item->nextSibling)
1522 newFirstVisible = item->nextSibling;
1523 else if (item->prevSibling)
1524 newFirstVisible = item->prevSibling;
1525 else if (item->parent != infoPtr->root)
1526 newFirstVisible = item->parent;
1527 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1528 }
1529 else
1530 newFirstVisible = infoPtr->firstVisible;
1531
1532 TREEVIEW_RemoveItem(infoPtr, item);
1533 }
1534
1535 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1536 if (!infoPtr->selectedItem && newSelection)
1537 {
1538 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1539 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1540 }
1541
1542 /* Validate insertMark dropItem.
1543 * hotItem ??? - used for comparison only.
1544 */
1545 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1546 infoPtr->insertMarkItem = 0;
1547
1548 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1549 infoPtr->dropItem = 0;
1550
1551 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1552 newFirstVisible = infoPtr->root->firstChild;
1553
1554 TREEVIEW_VerifyTree(infoPtr);
1555
1556 if (!infoPtr->bRedraw) return TRUE;
1557
1558 if (visible)
1559 {
1560 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1561 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1562 TREEVIEW_UpdateScrollBars(infoPtr);
1563 TREEVIEW_Invalidate(infoPtr, NULL);
1564 }
1565 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1566 {
1567 /* parent lost '+/-' - update it */
1568 TREEVIEW_Invalidate(infoPtr, parent);
1569 }
1570
1571 return TRUE;
1572 }
1573
1574
1575 /* Get/Set Messages *********************************************************/
1576 static LRESULT
1577 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1578 {
1579 infoPtr->bRedraw = wParam != 0;
1580
1581 if (infoPtr->bRedraw)
1582 {
1583 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1584 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1585 TREEVIEW_UpdateScrollBars(infoPtr);
1586 TREEVIEW_Invalidate(infoPtr, NULL);
1587 }
1588 return 0;
1589 }
1590
1591 static LRESULT
1592 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1593 {
1594 TRACE("\n");
1595 return infoPtr->uIndent;
1596 }
1597
1598 static LRESULT
1599 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1600 {
1601 TRACE("\n");
1602
1603 if (newIndent < MINIMUM_INDENT)
1604 newIndent = MINIMUM_INDENT;
1605
1606 if (infoPtr->uIndent != newIndent)
1607 {
1608 infoPtr->uIndent = newIndent;
1609 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1610 TREEVIEW_UpdateScrollBars(infoPtr);
1611 TREEVIEW_Invalidate(infoPtr, NULL);
1612 }
1613
1614 return 0;
1615 }
1616
1617
1618 static LRESULT
1619 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1620 {
1621 TRACE("\n");
1622 return (LRESULT)infoPtr->hwndToolTip;
1623 }
1624
1625 static LRESULT
1626 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1627 {
1628 HWND prevToolTip;
1629
1630 TRACE("\n");
1631 prevToolTip = infoPtr->hwndToolTip;
1632 infoPtr->hwndToolTip = hwndTT;
1633
1634 return (LRESULT)prevToolTip;
1635 }
1636
1637 static LRESULT
1638 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1639 {
1640 BOOL rc = infoPtr->bNtfUnicode;
1641 infoPtr->bNtfUnicode = fUnicode;
1642 return rc;
1643 }
1644
1645 static LRESULT
1646 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1647 {
1648 return infoPtr->bNtfUnicode;
1649 }
1650
1651 static LRESULT
1652 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1653 {
1654 return infoPtr->uScrollTime;
1655 }
1656
1657 static LRESULT
1658 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1659 {
1660 UINT uOldScrollTime = infoPtr->uScrollTime;
1661
1662 infoPtr->uScrollTime = min(uScrollTime, 100);
1663
1664 return uOldScrollTime;
1665 }
1666
1667
1668 static LRESULT
1669 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1670 {
1671 TRACE("\n");
1672
1673 switch (wParam)
1674 {
1675 case TVSIL_NORMAL:
1676 return (LRESULT)infoPtr->himlNormal;
1677
1678 case TVSIL_STATE:
1679 return (LRESULT)infoPtr->himlState;
1680
1681 default:
1682 return 0;
1683 }
1684 }
1685
1686 #define TVHEIGHT_MIN 16
1687 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1688
1689 /* Compute the natural height for items. */
1690 static UINT
1691 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1692 {
1693 TEXTMETRICW tm;
1694 HDC hdc = GetDC(0);
1695 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1696 UINT height;
1697
1698 /* Height is the maximum of:
1699 * 16 (a hack because our fonts are tiny), and
1700 * The text height + border & margin, and
1701 * The size of the normal image list
1702 */
1703 GetTextMetricsW(hdc, &tm);
1704 SelectObject(hdc, hOldFont);
1705 ReleaseDC(0, hdc);
1706
1707 height = TVHEIGHT_MIN;
1708 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1709 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1710 if (height < infoPtr->normalImageHeight)
1711 height = infoPtr->normalImageHeight;
1712
1713 /* Round down, unless we support odd ("non even") heights. */
1714 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1715 height &= ~1;
1716
1717 return height;
1718 }
1719
1720 static LRESULT
1721 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, UINT type, HIMAGELIST himlNew)
1722 {
1723 HIMAGELIST himlOld = 0;
1724 int oldWidth = infoPtr->normalImageWidth;
1725 int oldHeight = infoPtr->normalImageHeight;
1726
1727 TRACE("%u,%p\n", type, himlNew);
1728
1729 switch (type)
1730 {
1731 case TVSIL_NORMAL:
1732 himlOld = infoPtr->himlNormal;
1733 infoPtr->himlNormal = himlNew;
1734
1735 if (himlNew)
1736 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1737 &infoPtr->normalImageHeight);
1738 else
1739 {
1740 infoPtr->normalImageWidth = 0;
1741 infoPtr->normalImageHeight = 0;
1742 }
1743
1744 break;
1745
1746 case TVSIL_STATE:
1747 himlOld = infoPtr->himlState;
1748 infoPtr->himlState = himlNew;
1749
1750 if (himlNew)
1751 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1752 &infoPtr->stateImageHeight);
1753 else
1754 {
1755 infoPtr->stateImageWidth = 0;
1756 infoPtr->stateImageHeight = 0;
1757 }
1758
1759 break;
1760
1761 default:
1762 ERR("unknown imagelist type %u\n", type);
1763 }
1764
1765 if (oldWidth != infoPtr->normalImageWidth ||
1766 oldHeight != infoPtr->normalImageHeight)
1767 {
1768 BOOL bRecalcVisible = FALSE;
1769
1770 if (oldHeight != infoPtr->normalImageHeight &&
1771 !infoPtr->bHeightSet)
1772 {
1773 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1774 bRecalcVisible = TRUE;
1775 }
1776
1777 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1778 infoPtr->normalImageWidth != infoPtr->uIndent)
1779 {
1780 infoPtr->uIndent = infoPtr->normalImageWidth;
1781 bRecalcVisible = TRUE;
1782 }
1783
1784 if (bRecalcVisible)
1785 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1786
1787 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1788 TREEVIEW_UpdateScrollBars(infoPtr);
1789 }
1790
1791 TREEVIEW_Invalidate(infoPtr, NULL);
1792
1793 return (LRESULT)himlOld;
1794 }
1795
1796 static LRESULT
1797 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1798 {
1799 INT prevHeight = infoPtr->uItemHeight;
1800
1801 TRACE("new=%d, old=%d\n", newHeight, prevHeight);
1802 if (newHeight == -1)
1803 {
1804 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1805 infoPtr->bHeightSet = FALSE;
1806 }
1807 else
1808 {
1809 if (newHeight == 0) newHeight = 1;
1810 infoPtr->uItemHeight = newHeight;
1811 infoPtr->bHeightSet = TRUE;
1812 }
1813
1814 /* Round down, unless we support odd ("non even") heights. */
1815 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT) && infoPtr->uItemHeight != 1)
1816 {
1817 infoPtr->uItemHeight &= ~1;
1818 TRACE("after rounding=%d\n", infoPtr->uItemHeight);
1819 }
1820
1821 if (infoPtr->uItemHeight != prevHeight)
1822 {
1823 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1824 TREEVIEW_UpdateScrollBars(infoPtr);
1825 TREEVIEW_Invalidate(infoPtr, NULL);
1826 }
1827
1828 return prevHeight;
1829 }
1830
1831 static LRESULT
1832 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1833 {
1834 TRACE("\n");
1835 return infoPtr->uItemHeight;
1836 }
1837
1838
1839 static LRESULT
1840 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1841 {
1842 TRACE("%p\n", infoPtr->hFont);
1843 return (LRESULT)infoPtr->hFont;
1844 }
1845
1846
1847 static INT CALLBACK
1848 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1849 {
1850 (void)unused;
1851
1852 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1853
1854 return 1;
1855 }
1856
1857 static LRESULT
1858 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1859 {
1860 UINT uHeight = infoPtr->uItemHeight;
1861
1862 TRACE("%p %i\n", hFont, bRedraw);
1863
1864 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1865
1866 DeleteObject(infoPtr->hBoldFont);
1867 DeleteObject(infoPtr->hUnderlineFont);
1868 DeleteObject(infoPtr->hBoldUnderlineFont);
1869 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1870 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1871 infoPtr->hBoldUnderlineFont = TREEVIEW_CreateBoldUnderlineFont(infoPtr->hFont);
1872
1873 if (!infoPtr->bHeightSet)
1874 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1875
1876 if (uHeight != infoPtr->uItemHeight)
1877 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1878
1879 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1880
1881 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1882 TREEVIEW_UpdateScrollBars(infoPtr);
1883
1884 if (bRedraw)
1885 TREEVIEW_Invalidate(infoPtr, NULL);
1886
1887 return 0;
1888 }
1889
1890
1891 static LRESULT
1892 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1893 {
1894 TRACE("\n");
1895 return (LRESULT)infoPtr->clrLine;
1896 }
1897
1898 static LRESULT
1899 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1900 {
1901 COLORREF prevColor = infoPtr->clrLine;
1902
1903 TRACE("\n");
1904 infoPtr->clrLine = color;
1905 return (LRESULT)prevColor;
1906 }
1907
1908
1909 static LRESULT
1910 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1911 {
1912 TRACE("\n");
1913 return (LRESULT)infoPtr->clrText;
1914 }
1915
1916 static LRESULT
1917 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1918 {
1919 COLORREF prevColor = infoPtr->clrText;
1920
1921 TRACE("\n");
1922 infoPtr->clrText = color;
1923
1924 if (infoPtr->clrText != prevColor)
1925 TREEVIEW_Invalidate(infoPtr, NULL);
1926
1927 return (LRESULT)prevColor;
1928 }
1929
1930
1931 static LRESULT
1932 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1933 {
1934 TRACE("\n");
1935 return (LRESULT)infoPtr->clrBk;
1936 }
1937
1938 static LRESULT
1939 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1940 {
1941 COLORREF prevColor = infoPtr->clrBk;
1942
1943 TRACE("\n");
1944 infoPtr->clrBk = newColor;
1945
1946 if (newColor != prevColor)
1947 TREEVIEW_Invalidate(infoPtr, NULL);
1948
1949 return (LRESULT)prevColor;
1950 }
1951
1952
1953 static LRESULT
1954 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1955 {
1956 TRACE("\n");
1957 return (LRESULT)infoPtr->clrInsertMark;
1958 }
1959
1960 static LRESULT
1961 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1962 {
1963 COLORREF prevColor = infoPtr->clrInsertMark;
1964
1965 TRACE("%x\n", color);
1966 infoPtr->clrInsertMark = color;
1967
1968 return (LRESULT)prevColor;
1969 }
1970
1971
1972 static LRESULT
1973 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1974 {
1975 TRACE("%d %p\n", wParam, item);
1976
1977 if (!TREEVIEW_ValidItem(infoPtr, item))
1978 return 0;
1979
1980 infoPtr->insertBeforeorAfter = wParam;
1981 infoPtr->insertMarkItem = item;
1982
1983 TREEVIEW_Invalidate(infoPtr, NULL);
1984
1985 return 1;
1986 }
1987
1988
1989 /************************************************************************
1990 * Some serious braindamage here. lParam is a pointer to both the
1991 * input HTREEITEM and the output RECT.
1992 */
1993 static LRESULT
1994 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1995 {
1996 TREEVIEW_ITEM *item;
1997 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1998
1999 TRACE("\n");
2000
2001 if (pItem == NULL)
2002 return FALSE;
2003
2004 item = *pItem;
2005 if (!TREEVIEW_ValidItem(infoPtr, item) || !ISVISIBLE(item))
2006 return FALSE;
2007
2008 /*
2009 * If wParam is TRUE return the text size otherwise return
2010 * the whole item size
2011 */
2012 if (fTextRect)
2013 {
2014 /* Windows does not send TVN_GETDISPINFO here. */
2015
2016 lpRect->top = item->rect.top;
2017 lpRect->bottom = item->rect.bottom;
2018
2019 lpRect->left = item->textOffset;
2020 if (!item->textWidth)
2021 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2022
2023 lpRect->right = item->textOffset + item->textWidth + 4;
2024 }
2025 else
2026 {
2027 *lpRect = item->rect;
2028 }
2029
2030 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2031
2032 return TRUE;
2033 }
2034
2035 static inline LRESULT
2036 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2037 {
2038 /* Surprise! This does not take integral height into account. */
2039 TRACE("client=%d, item=%d\n", infoPtr->clientHeight, infoPtr->uItemHeight);
2040 return infoPtr->clientHeight / infoPtr->uItemHeight;
2041 }
2042
2043
2044 static LRESULT
2045 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2046 {
2047 TREEVIEW_ITEM *item = tvItem->hItem;
2048
2049 if (!TREEVIEW_ValidItem(infoPtr, item))
2050 {
2051 BOOL valid_item = FALSE;
2052 if (!item) return FALSE;
2053
2054 __TRY
2055 {
2056 infoPtr = item->infoPtr;
2057 TRACE("got item from different tree %p, called from %p\n", item->infoPtr, infoPtr);
2058 valid_item = TREEVIEW_ValidItem(infoPtr, item);
2059 }
2060 __EXCEPT_PAGE_FAULT
2061 {
2062 }
2063 __ENDTRY
2064 if (!valid_item) return FALSE;
2065 }
2066
2067 TREEVIEW_UpdateDispInfo(infoPtr, item, tvItem->mask);
2068
2069 if (tvItem->mask & TVIF_CHILDREN)
2070 {
2071 if (item->cChildren==I_CHILDRENCALLBACK)
2072 FIXME("I_CHILDRENCALLBACK not supported\n");
2073 tvItem->cChildren = item->cChildren;
2074 }
2075
2076 if (tvItem->mask & TVIF_HANDLE)
2077 tvItem->hItem = item;
2078
2079 if (tvItem->mask & TVIF_IMAGE)
2080 tvItem->iImage = item->iImage;
2081
2082 if (tvItem->mask & TVIF_INTEGRAL)
2083 tvItem->iIntegral = item->iIntegral;
2084
2085 /* undocumented: (mask & TVIF_PARAM) ignored and lParam is always set */
2086 tvItem->lParam = item->lParam;
2087
2088 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2089 tvItem->iSelectedImage = item->iSelectedImage;
2090
2091 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2092 tvItem->iExpandedImage = item->iExpandedImage;
2093
2094 /* undocumented: stateMask and (state & TVIF_STATE) ignored, so state is always set */
2095 tvItem->state = item->state;
2096
2097 if (tvItem->mask & TVIF_TEXT)
2098 {
2099 if (item->pszText == NULL)
2100 {
2101 if (tvItem->cchTextMax > 0)
2102 tvItem->pszText[0] = '\0';
2103 }
2104 else if (isW)
2105 {
2106 if (item->pszText == LPSTR_TEXTCALLBACKW)
2107 {
2108 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2109 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2110 }
2111 else
2112 {
2113 lstrcpynW(tvItem->pszText, item->pszText, tvItem->cchTextMax);
2114 }
2115 }
2116 else
2117 {
2118 if (item->pszText == LPSTR_TEXTCALLBACKW)
2119 {
2120 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2121 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2122 }
2123 else
2124 {
2125 WideCharToMultiByte(CP_ACP, 0, item->pszText, -1,
2126 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2127 }
2128 }
2129 }
2130
2131 if (tvItem->mask & TVIF_STATEEX)
2132 {
2133 FIXME("Extended item state not supported, returning 0.\n");
2134 tvItem->uStateEx = 0;
2135 }
2136
2137 TRACE("item <%p>, txt %p, img %d, mask %x\n",
2138 item, tvItem->pszText, tvItem->iImage, tvItem->mask);
2139
2140 return TRUE;
2141 }
2142
2143 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2144 * which is wrong. */
2145 static LRESULT
2146 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2147 {
2148 TREEVIEW_ITEM *item;
2149 TREEVIEW_ITEM originalItem;
2150
2151 item = tvItem->hItem;
2152
2153 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, item),
2154 tvItem->mask);
2155
2156 if (!TREEVIEW_ValidItem(infoPtr, item))
2157 return FALSE;
2158
2159 /* store the original item values */
2160 originalItem = *item;
2161
2162 if (!TREEVIEW_DoSetItemT(infoPtr, item, tvItem, isW))
2163 return FALSE;
2164
2165 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2166 if ((tvItem->mask & TVIF_TEXT
2167 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2168 && ISVISIBLE(item))
2169 {
2170 TREEVIEW_UpdateDispInfo(infoPtr, item, TVIF_TEXT);
2171 TREEVIEW_ComputeTextWidth(infoPtr, item, 0);
2172 }
2173
2174 if (tvItem->mask != 0 && ISVISIBLE(item))
2175 {
2176 /* The refresh updates everything, but we can't wait until then. */
2177 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
2178
2179 /* if any of the item's values changed and it's not a callback, redraw the item */
2180 if (item_changed(&originalItem, item, tvItem))
2181 {
2182 if (tvItem->mask & TVIF_INTEGRAL)
2183 {
2184 TREEVIEW_RecalculateVisibleOrder(infoPtr, item);
2185 TREEVIEW_UpdateScrollBars(infoPtr);
2186
2187 TREEVIEW_Invalidate(infoPtr, NULL);
2188 }
2189 else
2190 {
2191 TREEVIEW_UpdateScrollBars(infoPtr);
2192 TREEVIEW_Invalidate(infoPtr, item);
2193 }
2194 }
2195 }
2196
2197 return TRUE;
2198 }
2199
2200 static LRESULT
2201 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM item, UINT mask)
2202 {
2203 TRACE("\n");
2204
2205 if (!item || !TREEVIEW_ValidItem(infoPtr, item))
2206 return 0;
2207
2208 return (item->state & mask);
2209 }
2210
2211 static LRESULT
2212 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM item)
2213 {
2214 TREEVIEW_ITEM *retval;
2215
2216 retval = 0;
2217
2218 /* handle all the global data here */
2219 switch (which)
2220 {
2221 case TVGN_CHILD: /* Special case: child of 0 is root */
2222 if (item)
2223 break;
2224 /* fall through */
2225 case TVGN_ROOT:
2226 retval = infoPtr->root->firstChild;
2227 break;
2228
2229 case TVGN_CARET:
2230 retval = infoPtr->selectedItem;
2231 break;
2232
2233 case TVGN_FIRSTVISIBLE:
2234 retval = infoPtr->firstVisible;
2235 break;
2236
2237 case TVGN_DROPHILITE:
2238 retval = infoPtr->dropItem;
2239 break;
2240
2241 case TVGN_LASTVISIBLE:
2242 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2243 break;
2244 }
2245
2246 if (retval)
2247 {
2248 TRACE("flags:%x, returns %p\n", which, retval);
2249 return (LRESULT)retval;
2250 }
2251
2252 if (item == TVI_ROOT) item = infoPtr->root;
2253
2254 if (!TREEVIEW_ValidItem(infoPtr, item))
2255 return FALSE;
2256
2257 switch (which)
2258 {
2259 case TVGN_NEXT:
2260 retval = item->nextSibling;
2261 break;
2262 case TVGN_PREVIOUS:
2263 retval = item->prevSibling;
2264 break;
2265 case TVGN_PARENT:
2266 retval = (item->parent != infoPtr->root) ? item->parent : NULL;
2267 break;
2268 case TVGN_CHILD:
2269 retval = item->firstChild;
2270 break;
2271 case TVGN_NEXTVISIBLE:
2272 retval = TREEVIEW_GetNextListItem(infoPtr, item);
2273 break;
2274 case TVGN_PREVIOUSVISIBLE:
2275 retval = TREEVIEW_GetPrevListItem(infoPtr, item);
2276 break;
2277 default:
2278 TRACE("Unknown msg %x,item %p\n", which, item);
2279 break;
2280 }
2281
2282 TRACE("flags:%x, item %p;returns %p\n", which, item, retval);
2283 return (LRESULT)retval;
2284 }
2285
2286
2287 static LRESULT
2288 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2289 {
2290 TRACE(" %d\n", infoPtr->uNumItems);
2291 return (LRESULT)infoPtr->uNumItems;
2292 }
2293
2294 static VOID
2295 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2296 {
2297 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2298 {
2299 static const unsigned int state_table[] = { 0, 2, 1 };
2300
2301 unsigned int state;
2302
2303 state = STATEIMAGEINDEX(item->state);
2304 TRACE("state:%x\n", state);
2305 item->state &= ~TVIS_STATEIMAGEMASK;
2306
2307 if (state < 3)
2308 state = state_table[state];
2309
2310 item->state |= INDEXTOSTATEIMAGEMASK(state);
2311
2312 TRACE("state:%x\n", state);
2313 TREEVIEW_Invalidate(infoPtr, item);
2314 }
2315 }
2316
2317
2318 /* Painting *************************************************************/
2319
2320 /* Draw the lines and expand button for an item. Also draws one section
2321 * of the line from item's parent to item's parent's next sibling. */
2322 static void
2323 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2324 {
2325 LONG centerx, centery;
2326 BOOL lar = ((infoPtr->dwStyle
2327 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2328 > TVS_LINESATROOT);
2329 HBRUSH hbr, hbrOld;
2330 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2331
2332 if (!lar && item->iLevel == 0)
2333 return;
2334
2335 hbr = CreateSolidBrush(clrBk);
2336 hbrOld = SelectObject(hdc, hbr);
2337
2338 centerx = (item->linesOffset + item->stateOffset) / 2;
2339 centery = (item->rect.top + item->rect.bottom) / 2;
2340
2341 if (infoPtr->dwStyle & TVS_HASLINES)
2342 {
2343 HPEN hOldPen, hNewPen;
2344 HTREEITEM parent;
2345 LOGBRUSH lb;
2346
2347 /* Get a dotted grey pen */
2348 lb.lbStyle = BS_SOLID;
2349 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2350 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2351 hOldPen = SelectObject(hdc, hNewPen);
2352
2353 /* Make sure the center is on a dot (using +2 instead
2354 * of +1 gives us pixel-by-pixel compat with native) */
2355 centery = (centery + 2) & ~1;
2356
2357 MoveToEx(hdc, item->stateOffset, centery, NULL);
2358 LineTo(hdc, centerx - 1, centery);
2359
2360 if (item->prevSibling || item->parent != infoPtr->root)
2361 {
2362 MoveToEx(hdc, centerx, item->rect.top, NULL);
2363 LineTo(hdc, centerx, centery);
2364 }
2365
2366 if (item->nextSibling)
2367 {
2368 MoveToEx(hdc, centerx, centery, NULL);
2369 LineTo(hdc, centerx, item->rect.bottom + 1);
2370 }
2371
2372 /* Draw the line from our parent to its next sibling. */
2373 parent = item->parent;
2374 while (parent != infoPtr->root)
2375 {
2376 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2377
2378 if (parent->nextSibling
2379 /* skip top-levels unless TVS_LINESATROOT */
2380 && parent->stateOffset > parent->linesOffset)
2381 {
2382 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2383 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2384 }
2385
2386 parent = parent->parent;
2387 }
2388
2389 SelectObject(hdc, hOldPen);
2390 DeleteObject(hNewPen);
2391 }
2392
2393 /*
2394 * Display the (+/-) signs
2395 */
2396
2397 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2398 {
2399 if (item->cChildren)
2400 {
2401 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2402 if (theme)
2403 {
2404 RECT glyphRect = item->rect;
2405 glyphRect.left = item->linesOffset;
2406 glyphRect.right = item->stateOffset;
2407 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2408 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2409 &glyphRect, NULL);
2410 }
2411 else
2412 {
2413 LONG height = item->rect.bottom - item->rect.top;
2414 LONG width = item->stateOffset - item->linesOffset;
2415 LONG rectsize = min(height, width) / 4;
2416 /* plussize = ceil(rectsize * 3/4) */
2417 LONG plussize = (rectsize + 1) * 3 / 4;
2418
2419 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2420 HPEN old_pen = SelectObject(hdc, new_pen);
2421
2422 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2423 centerx + rectsize + 2, centery + rectsize + 2);
2424
2425 SelectObject(hdc, old_pen);
2426 DeleteObject(new_pen);
2427
2428 /* draw +/- signs with current text color */
2429 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2430 old_pen = SelectObject(hdc, new_pen);
2431
2432 if (height < 18 || width < 18)
2433 {
2434 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2435 LineTo(hdc, centerx + plussize, centery);
2436
2437 if (!(item->state & TVIS_EXPANDED) ||
2438 (item->state & TVIS_EXPANDPARTIAL))
2439 {
2440 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2441 LineTo(hdc, centerx, centery + plussize);
2442 }
2443 }
2444 else
2445 {
2446 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2447 centerx + plussize, centery + 2);
2448
2449 if (!(item->state & TVIS_EXPANDED) ||
2450 (item->state & TVIS_EXPANDPARTIAL))
2451 {
2452 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2453 centerx + 2, centery + plussize);
2454 SetPixel(hdc, centerx - 1, centery, clrBk);
2455 SetPixel(hdc, centerx + 1, centery, clrBk);
2456 }
2457 }
2458
2459 SelectObject(hdc, old_pen);
2460 DeleteObject(new_pen);
2461 }
2462 }
2463 }
2464 SelectObject(hdc, hbrOld);
2465 DeleteObject(hbr);
2466 }
2467
2468 static void
2469 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2470 {
2471 INT cditem;
2472 HFONT hOldFont;
2473 COLORREF oldTextColor, oldTextBkColor;
2474 int centery;
2475 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2476 NMTVCUSTOMDRAW nmcdhdr;
2477
2478 TREEVIEW_UpdateDispInfo(infoPtr, item, CALLBACK_MASK_ALL);
2479
2480 /* - If item is drop target or it is selected and window is in focus -
2481 * use blue background (COLOR_HIGHLIGHT).
2482 * - If item is selected, window is not in focus, but it has style
2483 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2484 * - Otherwise - use background color
2485 */
2486 if ((item->state & TVIS_DROPHILITED) || ((item == infoPtr->focusedItem) && !(item->state & TVIS_SELECTED)) ||
2487 ((item->state & TVIS_SELECTED) && (!infoPtr->focusedItem || item == infoPtr->focusedItem) &&
2488 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2489 {
2490 if ((item->state & TVIS_DROPHILITED) || inFocus)
2491 {
2492 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2493 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2494 }
2495 else
2496 {
2497 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2498 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2499 }
2500 }
2501 else
2502 {
2503 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2504 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
2505 nmcdhdr.clrText = comctl32_color.clrHighlight;
2506 else
2507 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2508 }
2509
2510 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
2511
2512 /* The custom draw handler can query the text rectangle,
2513 * so get ready. */
2514 /* should already be known, set to 0 when changed */
2515 if (!item->textWidth)
2516 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2517
2518 cditem = 0;
2519
2520 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2521 {
2522 cditem = TREEVIEW_SendCustomDrawItemNotify
2523 (infoPtr, hdc, item, CDDS_ITEMPREPAINT, &nmcdhdr);
2524 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2525
2526 if (cditem & CDRF_SKIPDEFAULT)
2527 {
2528 SelectObject(hdc, hOldFont);
2529 return;
2530 }
2531 }
2532
2533 if (cditem & CDRF_NEWFONT)
2534 TREEVIEW_ComputeTextWidth(infoPtr, item, hdc);
2535
2536 TREEVIEW_DrawItemLines(infoPtr, hdc, item);
2537
2538 /* Set colors. Custom draw handler can change these so we do this after it. */
2539 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2540 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2541
2542 centery = (item->rect.top + item->rect.bottom) / 2;
2543
2544 /*
2545 * Display the images associated with this item
2546 */
2547 {
2548 INT imageIndex;
2549
2550 /* State images are displayed to the left of the Normal image
2551 * image number is in state; zero should be `display no image'.
2552 */
2553 imageIndex = STATEIMAGEINDEX(item->state);
2554
2555 if (infoPtr->himlState && imageIndex)
2556 {
2557 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2558 item->stateOffset,
2559 centery - infoPtr->stateImageHeight / 2,
2560 ILD_NORMAL);
2561 }
2562
2563 /* Now, draw the normal image; can be either selected,
2564 * non-selected or expanded image.
2565 */
2566
2567 if ((item->state & TVIS_SELECTED) && (item->iSelectedImage >= 0))
2568 {
2569 /* The item is currently selected */
2570 imageIndex = item->iSelectedImage;
2571 }
2572 else if ((item->state & TVIS_EXPANDED) && (item->iExpandedImage != (WORD)I_IMAGENONE))
2573 {
2574 /* The item is currently not selected but expanded */
2575 imageIndex = item->iExpandedImage;
2576 }
2577 else
2578 {
2579 /* The item is not selected and not expanded */
2580 imageIndex = item->iImage;
2581 }
2582
2583 if (infoPtr->himlNormal)
2584 {
2585 UINT style = item->state & TVIS_CUT ? ILD_SELECTED : ILD_NORMAL;
2586
2587 style |= item->state & TVIS_OVERLAYMASK;
2588
2589 ImageList_DrawEx(infoPtr->himlNormal, imageIndex, hdc,
2590 item->imageOffset, centery - infoPtr->normalImageHeight / 2,
2591 0, 0, infoPtr->clrBk, item->state & TVIS_CUT ? GETBKCOLOR(infoPtr->clrBk) : CLR_DEFAULT,
2592 style);
2593 }
2594 }
2595
2596
2597 /*
2598 * Display the text associated with this item
2599 */
2600
2601 /* Don't paint item's text if it's being edited */
2602 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != item))
2603 {
2604 if (item->pszText)
2605 {
2606 RECT rcText;
2607 UINT align;
2608 SIZE sz;
2609
2610 rcText.top = item->rect.top;
2611 rcText.bottom = item->rect.bottom;
2612 rcText.left = item->textOffset;
2613 rcText.right = rcText.left + item->textWidth + 4;
2614
2615 TRACE("drawing text %s at (%s)\n",
2616 debugstr_w(item->pszText), wine_dbgstr_rect(&rcText));
2617
2618 /* Draw it */
2619 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
2620
2621 align = SetTextAlign(hdc, TA_LEFT | TA_TOP);
2622 ExtTextOutW(hdc, rcText.left + 2, (rcText.top + rcText.bottom - sz.cy) / 2,
2623 ETO_CLIPPED | ETO_OPAQUE,
2624 &rcText,
2625 item->pszText,
2626 lstrlenW(item->pszText),
2627 NULL);
2628 SetTextAlign(hdc, align);
2629
2630 /* Draw focus box around the selected item */
2631 if ((item == infoPtr->selectedItem) && inFocus)
2632 {
2633 DrawFocusRect(hdc,&rcText);
2634 }
2635 }
2636 }
2637
2638 /* Draw insertion mark if necessary */
2639
2640 if (infoPtr->insertMarkItem)
2641 TRACE("item:%d,mark:%p\n",
2642 TREEVIEW_GetItemIndex(infoPtr, item),
2643 infoPtr->insertMarkItem);
2644
2645 if (item == infoPtr->insertMarkItem)
2646 {
2647 HPEN hNewPen, hOldPen;
2648 int offset;
2649 int left, right;
2650
2651 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2652 hOldPen = SelectObject(hdc, hNewPen);
2653
2654 if (infoPtr->insertBeforeorAfter)
2655 offset = item->rect.bottom - 1;
2656 else
2657 offset = item->rect.top + 1;
2658
2659 left = item->textOffset - 2;
2660 right = item->textOffset + item->textWidth + 2;
2661
2662 MoveToEx(hdc, left, offset - 3, NULL);
2663 LineTo(hdc, left, offset + 4);
2664
2665 MoveToEx(hdc, left, offset, NULL);
2666 LineTo(hdc, right + 1, offset);
2667
2668 MoveToEx(hdc, right, offset + 3, NULL);
2669 LineTo(hdc, right, offset - 4);
2670
2671 SelectObject(hdc, hOldPen);
2672 DeleteObject(hNewPen);
2673 }
2674
2675 /* Restore the hdc state */
2676 SetTextColor(hdc, oldTextColor);
2677 SetBkColor(hdc, oldTextBkColor);
2678 SelectObject(hdc, hOldFont);
2679
2680 if (cditem & CDRF_NOTIFYPOSTPAINT)
2681 {
2682 cditem = TREEVIEW_SendCustomDrawItemNotify
2683 (infoPtr, hdc, item, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2684 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2685 }
2686 }
2687
2688 /* Computes treeHeight and treeWidth and updates the scroll bars.
2689 */
2690 static void
2691 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2692 {
2693 TREEVIEW_ITEM *item;
2694 HWND hwnd = infoPtr->hwnd;
2695 BOOL vert = FALSE;
2696 BOOL horz = FALSE;
2697 SCROLLINFO si;
2698 LONG scrollX = infoPtr->scrollX;
2699
2700 infoPtr->treeWidth = 0;
2701 infoPtr->treeHeight = 0;
2702
2703 /* We iterate through all visible items in order to get the tree height
2704 * and width */
2705 item = infoPtr->root->firstChild;
2706
2707 while (item != NULL)
2708 {
2709 if (ISVISIBLE(item))
2710 {
2711 /* actually we draw text at textOffset + 2 */
2712 if (2+item->textOffset+item->textWidth > infoPtr->treeWidth)
2713 infoPtr->treeWidth = item->textOffset+item->textWidth+2;
2714
2715 /* This is scroll-adjusted, but we fix this below. */
2716 infoPtr->treeHeight = item->rect.bottom;
2717 }
2718
2719 item = TREEVIEW_GetNextListItem(infoPtr, item);
2720 }
2721
2722 /* Fix the scroll adjusted treeHeight and treeWidth. */
2723 if (infoPtr->root->firstChild)
2724 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2725
2726 infoPtr->treeWidth += infoPtr->scrollX;
2727
2728 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2729
2730 /* Adding one scroll bar may take up enough space that it forces us
2731 * to add the other as well. */
2732 if (infoPtr->treeHeight > infoPtr->clientHeight)
2733 {
2734 vert = TRUE;
2735
2736 if (infoPtr->treeWidth
2737 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2738 horz = TRUE;
2739 }
2740 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2741 horz = TRUE;
2742
2743 if (!vert && horz && infoPtr->treeHeight
2744 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2745 vert = TRUE;
2746
2747 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2748
2749 si.cbSize = sizeof(SCROLLINFO);
2750 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2751 si.nMin = 0;
2752
2753 if (vert)
2754 {
2755 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2756 if ( si.nPage && NULL != infoPtr->firstVisible)
2757 {
2758 si.nPos = infoPtr->firstVisible->visibleOrder;
2759 si.nMax = infoPtr->maxVisibleOrder - 1;
2760
2761 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2762
2763 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2764 ShowScrollBar(hwnd, SB_VERT, TRUE);
2765 infoPtr->uInternalStatus |= TV_VSCROLL;
2766 }
2767 else
2768 {
2769 if (infoPtr->uInternalStatus & TV_VSCROLL)
2770 ShowScrollBar(hwnd, SB_VERT, FALSE);
2771 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2772 }
2773 }
2774 else
2775 {
2776 if (infoPtr->uInternalStatus & TV_VSCROLL)
2777 ShowScrollBar(hwnd, SB_VERT, FALSE);
2778 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2779 }
2780
2781 if (horz)
2782 {
2783 si.nPage = infoPtr->clientWidth;
2784 si.nPos = infoPtr->scrollX;
2785 si.nMax = infoPtr->treeWidth - 1;
2786
2787 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2788 {
2789 si.nPos = si.nMax - max( si.nPage-1, 0 );
2790 scrollX = si.nPos;
2791 }
2792
2793 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2794 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2795 infoPtr->uInternalStatus |= TV_HSCROLL;
2796
2797 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2798 TREEVIEW_HScroll(infoPtr,
2799 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2800 }
2801 else
2802 {
2803 if (infoPtr->uInternalStatus & TV_HSCROLL)
2804 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2805 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2806
2807 scrollX = 0;
2808 if (infoPtr->scrollX != 0)
2809 {
2810 TREEVIEW_HScroll(infoPtr,
2811 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2812 }
2813 }
2814
2815 if (!horz)
2816 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2817 }
2818
2819 static void
2820 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2821 {
2822 HBRUSH hBrush;
2823 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2824
2825 hBrush = CreateSolidBrush(clrBk);
2826 FillRect(hdc, rc, hBrush);
2827 DeleteObject(hBrush);
2828 }
2829
2830 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2831 static LRESULT
2832 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2833 {
2834 RECT rect;
2835
2836 TRACE("%p\n", infoPtr);
2837
2838 GetClientRect(infoPtr->hwnd, &rect);
2839 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2840
2841 return 1;
2842 }
2843
2844 static void
2845 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2846 {
2847 HWND hwnd = infoPtr->hwnd;
2848 RECT rect = *rc;
2849 TREEVIEW_ITEM *item;
2850
2851 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2852 {
2853 TRACE("empty window\n");
2854 return;
2855 }
2856
2857 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2858 hdc, rect);
2859
2860 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2861 {
2862 ReleaseDC(hwnd, hdc);
2863 return;
2864 }
2865
2866 for (item = infoPtr->root->firstChild;
2867 item != NULL;
2868 item = TREEVIEW_GetNextListItem(infoPtr, item))
2869 {
2870 if (ISVISIBLE(item))
2871 {
2872 /* Avoid unneeded calculations */
2873 if (item->rect.top > rect.bottom)
2874 break;
2875 if (item->rect.bottom < rect.top)
2876 continue;
2877
2878 TREEVIEW_DrawItem(infoPtr, hdc, item);
2879 }
2880 }
2881
2882 //
2883 // FIXME: This is correct, but is causes and infinite loop of WM_PAINT
2884 // messages, resulting in continuous painting of the scroll bar in reactos.
2885 // Comment out until the real bug is found. CORE-4912
2886 //
2887 #ifndef __REACTOS__
2888 TREEVIEW_UpdateScrollBars(infoPtr);
2889 #endif
2890
2891 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2892 infoPtr->cdmode =
2893 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2894 }
2895
2896 static inline void
2897 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2898 {
2899 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2900 }
2901
2902 static void
2903 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2904 {
2905 if (item)
2906 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2907 else
2908 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2909 }
2910
2911 static void
2912 TREEVIEW_InitCheckboxes(TREEVIEW_INFO *infoPtr)
2913 {
2914 RECT rc;
2915 HBITMAP hbm, hbmOld;
2916 HDC hdc, hdcScreen;
2917 int nIndex;
2918
2919 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
2920
2921 hdcScreen = GetDC(0);
2922
2923 hdc = CreateCompatibleDC(hdcScreen);
2924 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
2925 hbmOld = SelectObject(hdc, hbm);
2926
2927 SetRect(&rc, 0, 0, 48, 16);
2928 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
2929
2930 SetRect(&rc, 18, 2, 30, 14);
2931 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2932 DFCS_BUTTONCHECK|DFCS_FLAT);
2933
2934 SetRect(&rc, 34, 2, 46, 14);
2935 DrawFrameControl(hdc, &rc, DFC_BUTTON,
2936 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
2937
2938 SelectObject(hdc, hbmOld);
2939 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
2940 comctl32_color.clrWindow);
2941 TRACE("checkbox index %d\n", nIndex);
2942
2943 DeleteObject(hbm);
2944 DeleteDC(hdc);
2945 ReleaseDC(0, hdcScreen);
2946
2947 infoPtr->stateImageWidth = 16;
2948 infoPtr->stateImageHeight = 16;
2949 }
2950
2951 static void
2952 TREEVIEW_ResetImageStateIndex(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2953 {
2954 TREEVIEW_ITEM *child = item->firstChild;
2955
2956 item->state &= ~TVIS_STATEIMAGEMASK;
2957 item->state |= INDEXTOSTATEIMAGEMASK(1);
2958
2959 while (child)
2960 {
2961 TREEVIEW_ITEM *next = child->nextSibling;
2962 TREEVIEW_ResetImageStateIndex(infoPtr, child);
2963 child = next;
2964 }
2965 }
2966
2967 static LRESULT
2968 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2969 {
2970 HDC hdc;
2971 PAINTSTRUCT ps;
2972 RECT rc;
2973
2974 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2975
2976 if ((infoPtr->dwStyle & TVS_CHECKBOXES) && !infoPtr->himlState)
2977 {
2978 TREEVIEW_InitCheckboxes(infoPtr);
2979 TREEVIEW_ResetImageStateIndex(infoPtr, infoPtr->root);
2980
2981 TREEVIEW_EndEditLabelNow(infoPtr, TRUE);
2982 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
2983 TREEVIEW_UpdateScrollBars(infoPtr);
2984 TREEVIEW_Invalidate(infoPtr, NULL);
2985 }
2986
2987 if (hdc_ref)
2988 {
2989 hdc = hdc_ref;
2990 GetClientRect(infoPtr->hwnd, &rc);
2991 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2992 }
2993 else
2994 {
2995 hdc = BeginPaint(infoPtr->hwnd, &ps);
2996 rc = ps.rcPaint;
2997 if(ps.fErase)
2998 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2999 }
3000
3001 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
3002 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3003
3004 if (!hdc_ref)
3005 EndPaint(infoPtr->hwnd, &ps);
3006
3007 return 0;
3008 }
3009
3010 static LRESULT
3011 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
3012 {
3013 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
3014
3015 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
3016 return 0;
3017
3018 if (options & PRF_ERASEBKGND)
3019 TREEVIEW_EraseBackground(infoPtr, hdc);
3020
3021 if (options & PRF_CLIENT)
3022 {
3023 RECT rc;
3024 GetClientRect(infoPtr->hwnd, &rc);
3025 TREEVIEW_Refresh(infoPtr, hdc, &rc);
3026 }
3027
3028 return 0;
3029 }
3030
3031 /* Sorting **************************************************************/
3032
3033 /***************************************************************************
3034 * Forward the DPA local callback to the treeview owner callback
3035 */
3036 static INT WINAPI
3037 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
3038 const TVSORTCB *pCallBackSort)
3039 {
3040 /* Forward the call to the client-defined callback */
3041 return pCallBackSort->lpfnCompare(first->lParam,
3042 second->lParam,
3043 pCallBackSort->lParam);
3044 }
3045
3046 /***************************************************************************
3047 * Treeview native sort routine: sort on item text.
3048 */
3049 static INT WINAPI
3050 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
3051 const TREEVIEW_INFO *infoPtr)
3052 {
3053 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
3054 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
3055
3056 if(first->pszText && second->pszText)
3057 return lstrcmpiW(first->pszText, second->pszText);
3058 else if(first->pszText)
3059 return -1;
3060 else if(second->pszText)
3061 return 1;
3062 else
3063 return 0;
3064 }
3065
3066 /* Returns the number of physical children belonging to item. */
3067 static INT
3068 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
3069 {
3070 INT cChildren = 0;
3071 HTREEITEM hti;
3072
3073 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3074 cChildren++;
3075
3076 return cChildren;
3077 }
3078
3079 /* Returns a DPA containing a pointer to each physical child of item in
3080 * sibling order. If item has no children, an empty DPA is returned. */
3081 static HDPA
3082 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3083 {
3084 HTREEITEM child;
3085
3086 HDPA list = DPA_Create(8);
3087 if (list == 0) return NULL;
3088
3089 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3090 {
3091 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3092 {
3093 DPA_Destroy(list);
3094 return NULL;
3095 }
3096 }
3097
3098 return list;
3099 }
3100
3101 /***************************************************************************
3102 * Setup the treeview structure with regards of the sort method
3103 * and sort the children of the TV item specified in lParam
3104 * fRecurse: currently unused. Should be zero.
3105 * parent: if pSort!=NULL, should equal pSort->hParent.
3106 * otherwise, item which child items are to be sorted.
3107 * pSort: sort method info. if NULL, sort on item text.
3108 * if non-NULL, sort on item's lParam content, and let the
3109 * application decide what that means. See also TVM_SORTCHILDRENCB.
3110 */
3111
3112 static LRESULT
3113 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3114 LPTVSORTCB pSort)
3115 {
3116 INT cChildren;
3117 PFNDPACOMPARE pfnCompare;
3118 LPARAM lpCompare;
3119
3120 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3121 if (parent == TVI_ROOT || parent == NULL)
3122 parent = infoPtr->root;
3123
3124 /* Check for a valid handle to the parent item */
3125 if (!TREEVIEW_ValidItem(infoPtr, parent))
3126 {
3127 ERR("invalid item hParent=%p\n", parent);
3128 return FALSE;
3129 }
3130
3131 if (pSort)
3132 {
3133 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3134 lpCompare = (LPARAM)pSort;
3135 }
3136 else
3137 {
3138 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3139 lpCompare = (LPARAM)infoPtr;
3140 }
3141
3142 cChildren = TREEVIEW_CountChildren(parent);
3143
3144 /* Make sure there is something to sort */
3145 if (cChildren > 1)
3146 {
3147 /* TREEVIEW_ITEM rechaining */
3148 INT count = 0;
3149 HTREEITEM item = 0;
3150 HTREEITEM nextItem = 0;
3151 HTREEITEM prevItem = 0;
3152
3153 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3154
3155 if (sortList == NULL)
3156 return FALSE;
3157
3158 /* let DPA sort the list */
3159 DPA_Sort(sortList, pfnCompare, lpCompare);
3160
3161 /* The order of DPA entries has been changed, so fixup the
3162 * nextSibling and prevSibling pointers. */
3163
3164 item = DPA_GetPtr(sortList, count++);
3165 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3166 {
3167 /* link the two current item together */
3168 item->nextSibling = nextItem;
3169 nextItem->prevSibling = item;
3170
3171 if (prevItem == NULL)
3172 {
3173 /* this is the first item, update the parent */
3174 parent->firstChild = item;
3175 item->prevSibling = NULL;
3176 }
3177 else
3178 {
3179 /* fix the back chaining */
3180 item->prevSibling = prevItem;
3181 }
3182
3183 /* get ready for the next one */
3184 prevItem = item;
3185 item = nextItem;
3186 }
3187
3188 /* the last item is pointed to by item and never has a sibling */
3189 item->nextSibling = NULL;
3190 parent->lastChild = item;
3191
3192 DPA_Destroy(sortList);
3193
3194 TREEVIEW_VerifyTree(infoPtr);
3195
3196 if (parent->state & TVIS_EXPANDED)
3197 {
3198 int visOrder = infoPtr->firstVisible->visibleOrder;
3199
3200 if (parent == infoPtr->root)
3201 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3202 else
3203 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3204
3205 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3206 {
3207 TREEVIEW_ITEM *item;
3208