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