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