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