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