source: trunk/src/comctl32/treeview.c@ 9370

Last change on this file since 9370 was 9370, checked in by sandervl, 23 years ago

Wine resync

File size: 140.3 KB
Line 
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 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
22 *
23 * Note2: All items always! have valid (allocated) pszText field.
24 * If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
25 * of size TEXT_CALLBACK_SIZE in DoSetItem.
26 * We use callbackMask to keep track of fields to be updated.
27 *
28 * TODO:
29 * missing notifications: NM_SETCURSOR, TVN_GETINFOTIP, TVN_KEYDOWN,
30 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
31 *
32 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_NOSCROLL,
33 * TVS_RTLREADING, TVS_TRACKSELECT
34 *
35 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
36 *
37 * Make the insertion mark look right.
38 * Scroll (instead of repaint) as much as possible.
39 */
40
41#include <assert.h>
42#include <ctype.h>
43#include <string.h>
44#include <limits.h>
45#include <stdlib.h>
46
47#include "winbase.h"
48#include "wingdi.h"
49#include "commctrl.h"
50#include "comctl32.h"
51#include "wine/debug.h"
52
53/* internal structures */
54
55typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
56{
57 UINT callbackMask;
58 UINT state;
59 UINT stateMask;
60 LPSTR pszText;
61 int cchTextMax;
62 int iImage;
63 int iSelectedImage;
64 int cChildren;
65 LPARAM lParam;
66 int iIntegral; /* item height multiplier (1 is normal) */
67 int iLevel; /* indentation level:0=root level */
68 HTREEITEM parent; /* handle to parent or 0 if at root */
69 HTREEITEM firstChild; /* handle to first child or 0 if no child */
70 HTREEITEM lastChild;
71 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
72 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
73 RECT rect;
74 LONG linesOffset;
75 LONG stateOffset;
76 LONG imageOffset;
77 LONG textOffset;
78 LONG textWidth; /* horizontal text extent for pszText */
79 LONG visibleOrder; /* visible ordering, 0 is first visible item */
80} TREEVIEW_ITEM;
81
82
83typedef struct tagTREEVIEW_INFO
84{
85 HWND hwnd;
86 HWND hwndNotify; /* Owner window to send notifications to */
87 DWORD dwStyle;
88 HTREEITEM root;
89 UINT uInternalStatus;
90 INT Timer;
91 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
92 INT cdmode; /* last custom draw setting */
93 UINT uScrollTime; /* max. time for scrolling in milliseconds */
94 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
95
96 UINT uItemHeight; /* item height */
97 BOOL bHeightSet;
98
99 LONG clientWidth; /* width of control window */
100 LONG clientHeight; /* height of control window */
101
102 LONG treeWidth; /* width of visible tree items */
103 LONG treeHeight; /* height of visible tree items */
104
105 UINT uIndent; /* indentation in pixels */
106 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
107 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
108 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
109
110 HTREEITEM firstVisible; /* handle to first visible item */
111 LONG maxVisibleOrder;
112 HTREEITEM dropItem; /* handle to item selected by drag cursor */
113 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
114 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
115 HIMAGELIST dragList; /* Bitmap of dragged item */
116 LONG scrollX;
117 COLORREF clrBk;
118 COLORREF clrText;
119 COLORREF clrLine;
120 COLORREF clrInsertMark;
121 HFONT hFont;
122 HFONT hBoldFont;
123 HWND hwndToolTip;
124
125 HWND hwndEdit;
126 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
127 BOOL bIgnoreEditKillFocus;
128 BOOL bLabelChanged;
129
130 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
131 BOOL bUnicode; /* set by CCM_SETUNICODEFORMAT */
132 HIMAGELIST himlNormal;
133 int normalImageHeight;
134 int normalImageWidth;
135 HIMAGELIST himlState;
136 int stateImageHeight;
137 int stateImageWidth;
138 HDPA items;
139
140 DWORD lastKeyPressTimestamp; /* Added */
141 WPARAM charCode; /* Added */
142 INT nSearchParamLength; /* Added */
143 CHAR szSearchParam[ MAX_PATH ]; /* Added */
144} TREEVIEW_INFO;
145
146
147/******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
148#define KEY_DELAY 450
149
150/* bitflags for infoPtr->uInternalStatus */
151
152#define TV_HSCROLL 0x01 /* treeview too large to fit in window */
153#define TV_VSCROLL 0x02 /* (horizontal/vertical) */
154#define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
155#define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
156#define TV_RDRAG 0x10 /* dito Rbutton */
157#define TV_RDRAGGING 0x20
158
159/* bitflags for infoPtr->timer */
160
161#define TV_EDIT_TIMER 2
162#define TV_EDIT_TIMER_SET 2
163
164
165VOID TREEVIEW_Register (VOID);
166VOID TREEVIEW_Unregister (VOID);
167
168
169WINE_DEFAULT_DEBUG_CHANNEL(treeview);
170
171
172#define TEXT_CALLBACK_SIZE 260
173
174#define TREEVIEW_LEFT_MARGIN 8
175
176#define MINIMUM_INDENT 19
177
178#define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
179
180#define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
181#define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
182#define ISVISIBLE(x) ((x)->visibleOrder >= 0)
183
184
185typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
186
187
188static VOID TREEVIEW_Invalidate(TREEVIEW_INFO *, TREEVIEW_ITEM *);
189
190static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
191static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
192static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
193static LRESULT TREEVIEW_RButtonUp(TREEVIEW_INFO *, LPPOINT);
194static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
195static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
196static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
197static LRESULT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
198
199
200/* Random Utilities *****************************************************/
201
202#ifndef NDEBUG
203static inline void
204TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
205{
206 (void)infoPtr;
207}
208#else
209/* The definition is at the end of the file. */
210static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
211#endif
212
213/* Returns the treeview private data if hwnd is a treeview.
214 * Otherwise returns an undefined value. */
215static TREEVIEW_INFO *
216TREEVIEW_GetInfoPtr(HWND hwnd)
217{
218 return (TREEVIEW_INFO *)GetWindowLongA(hwnd, 0);
219}
220
221/* Don't call this. Nothing wants an item index. */
222static inline int
223TREEVIEW_GetItemIndex(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
224{
225 assert(infoPtr != NULL);
226
227 return DPA_GetPtrIndex(infoPtr->items, handle);
228}
229
230/***************************************************************************
231 * This method checks that handle is an item for this tree.
232 */
233static BOOL
234TREEVIEW_ValidItem(TREEVIEW_INFO *infoPtr, HTREEITEM handle)
235{
236 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
237 {
238 TRACE("invalid item %p\n", handle);
239 return FALSE;
240 }
241 else
242 return TRUE;
243}
244
245static HFONT
246TREEVIEW_CreateBoldFont(HFONT hOrigFont)
247{
248 LOGFONTA font;
249
250 GetObjectA(hOrigFont, sizeof(font), &font);
251 font.lfWeight = FW_BOLD;
252 return CreateFontIndirectA(&font);
253}
254
255static inline HFONT
256TREEVIEW_FontForItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
257{
258 return (item->state & TVIS_BOLD) ? infoPtr->hBoldFont : infoPtr->hFont;
259}
260
261/* for trace/debugging purposes only */
262static const char *
263TREEVIEW_ItemName(TREEVIEW_ITEM *item)
264{
265 if (item == NULL) return "<null item>";
266 if (item->pszText == LPSTR_TEXTCALLBACKA) return "<callback>";
267 if (item->pszText == NULL) return "<null>";
268 return item->pszText;
269}
270
271/* An item is not a child of itself. */
272static BOOL
273TREEVIEW_IsChildOf(TREEVIEW_ITEM *parent, TREEVIEW_ITEM *child)
274{
275 do
276 {
277 child = child->parent;
278 if (child == parent) return TRUE;
279 } while (child != NULL);
280
281 return FALSE;
282}
283
284
285/* Tree Traversal *******************************************************/
286
287/***************************************************************************
288 * This method returns the last expanded sibling or child child item
289 * of a tree node
290 */
291static TREEVIEW_ITEM *
292TREEVIEW_GetLastListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
293{
294 if (!wineItem)
295 return NULL;
296
297 while (wineItem->lastChild)
298 {
299 if (wineItem->state & TVIS_EXPANDED)
300 wineItem = wineItem->lastChild;
301 else
302 break;
303 }
304
305 if (wineItem == infoPtr->root)
306 return NULL;
307
308 return wineItem;
309}
310
311/***************************************************************************
312 * This method returns the previous non-hidden item in the list not
313 * considering the tree hierarchy.
314 */
315static TREEVIEW_ITEM *
316TREEVIEW_GetPrevListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
317{
318 if (tvItem->prevSibling)
319 {
320 /* This item has a prevSibling, get the last item in the sibling's tree. */
321 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
322
323 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
324 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
325 else
326 return upItem;
327 }
328 else
329 {
330 /* this item does not have a prevSibling, get the parent */
331 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
332 }
333}
334
335
336/***************************************************************************
337 * This method returns the next physical item in the treeview not
338 * considering the tree hierarchy.
339 */
340static TREEVIEW_ITEM *
341TREEVIEW_GetNextListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *tvItem)
342{
343 assert(tvItem != NULL);
344
345 /*
346 * If this item has children and is expanded, return the first child
347 */
348 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
349 {
350 return tvItem->firstChild;
351 }
352
353
354 /*
355 * try to get the sibling
356 */
357 if (tvItem->nextSibling)
358 return tvItem->nextSibling;
359
360 /*
361 * Otherwise, get the parent's sibling.
362 */
363 while (tvItem->parent)
364 {
365 tvItem = tvItem->parent;
366
367 if (tvItem->nextSibling)
368 return tvItem->nextSibling;
369 }
370
371 return NULL;
372}
373
374/***************************************************************************
375 * This method returns the nth item starting at the given item. It returns
376 * the last item (or first) we we run out of items.
377 *
378 * Will scroll backward if count is <0.
379 * forward if count is >0.
380 */
381static TREEVIEW_ITEM *
382TREEVIEW_GetListItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
383 LONG count)
384{
385 TREEVIEW_ITEM *(*next_item)(TREEVIEW_INFO *, TREEVIEW_ITEM *);
386 TREEVIEW_ITEM *previousItem;
387
388 assert(wineItem != NULL);
389
390 if (count > 0)
391 {
392 next_item = TREEVIEW_GetNextListItem;
393 }
394 else if (count < 0)
395 {
396 count = -count;
397 next_item = TREEVIEW_GetPrevListItem;
398 }
399 else
400 return wineItem;
401
402 do
403 {
404 previousItem = wineItem;
405 wineItem = next_item(infoPtr, wineItem);
406
407 } while (--count && wineItem != NULL);
408
409
410 return wineItem ? wineItem : previousItem;
411}
412
413/* Notifications ************************************************************/
414
415static LRESULT
416TREEVIEW_SendRealNotify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
417{
418 if (infoPtr->bNtfUnicode)
419 return (BOOL)SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
420 wParam, lParam);
421 else
422 return (BOOL)SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
423 wParam, lParam);
424}
425
426static BOOL
427TREEVIEW_SendSimpleNotify(TREEVIEW_INFO *infoPtr, UINT code)
428{
429 NMHDR nmhdr;
430 HWND hwnd = infoPtr->hwnd;
431
432 TRACE("%x\n", code);
433 nmhdr.hwndFrom = hwnd;
434 nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
435 nmhdr.code = code;
436
437 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
438 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
439}
440
441static VOID
442TREEVIEW_TVItemFromItem(TREEVIEW_INFO *infoPtr, UINT mask, TVITEMA *tvItem, TREEVIEW_ITEM *item)
443{
444 tvItem->mask = mask;
445 tvItem->hItem = item;
446 tvItem->state = item->state;
447 tvItem->stateMask = 0;
448 tvItem->iImage = item->iImage;
449 tvItem->cchTextMax = item->cchTextMax;
450 tvItem->iImage = item->iImage;
451 tvItem->iSelectedImage = item->iSelectedImage;
452 tvItem->cChildren = item->cChildren;
453 tvItem->lParam = item->lParam;
454
455 /* **** **** **** **** WARNING **** **** **** **** */
456 /* This control stores all the data in A format */
457 /* we will convert it to W if the notify format */
458 /* is Unicode. */
459 /* **** **** **** **** WARNING **** **** **** **** */
460 if (infoPtr->bNtfUnicode) {
461 INT len = MultiByteToWideChar( CP_ACP, 0, item->pszText, -1, NULL, 0 );
462 if (len > 1) {
463 tvItem->pszText = (LPSTR)COMCTL32_Alloc (len*sizeof(WCHAR));
464 MultiByteToWideChar( CP_ACP, 0, item->pszText, -1, (LPWSTR)tvItem->pszText, len*sizeof(WCHAR) );
465 }
466 }
467 else
468 tvItem->pszText = item->pszText;
469}
470
471static BOOL
472TREEVIEW_SendTreeviewNotify(TREEVIEW_INFO *infoPtr, UINT code, UINT action,
473 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
474{
475 HWND hwnd = infoPtr->hwnd;
476 NMTREEVIEWA nmhdr;
477 BOOL ret;
478
479 TRACE("code:%x action:%x olditem:%p newitem:%p\n",
480 code, action, oldItem, newItem);
481
482 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWA));
483
484 nmhdr.hdr.hwndFrom = hwnd;
485 nmhdr.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
486 nmhdr.hdr.code = code;
487 nmhdr.action = action;
488
489 if (oldItem)
490 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
491
492 if (newItem)
493 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
494
495 nmhdr.ptDrag.x = 0;
496 nmhdr.ptDrag.y = 0;
497
498 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
499 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
500 (LPARAM)&nmhdr);
501 if (infoPtr->bNtfUnicode) {
502 COMCTL32_Free(nmhdr.itemOld.pszText);
503 COMCTL32_Free(nmhdr.itemNew.pszText);
504 }
505 return ret;
506}
507
508static BOOL
509TREEVIEW_SendTreeviewDnDNotify(TREEVIEW_INFO *infoPtr, UINT code,
510 HTREEITEM dragItem, POINT pt)
511{
512 HWND hwnd = infoPtr->hwnd;
513 NMTREEVIEWA nmhdr;
514
515 TRACE("code:%x dragitem:%p\n", code, dragItem);
516
517 nmhdr.hdr.hwndFrom = hwnd;
518 nmhdr.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
519 nmhdr.hdr.code = code;
520 nmhdr.action = 0;
521 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
522 nmhdr.itemNew.hItem = dragItem;
523 nmhdr.itemNew.state = dragItem->state;
524 nmhdr.itemNew.lParam = dragItem->lParam;
525
526 nmhdr.ptDrag.x = pt.x;
527 nmhdr.ptDrag.y = pt.y;
528
529 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
530 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
531 (LPARAM)&nmhdr);
532}
533
534
535static BOOL
536TREEVIEW_SendCustomDrawNotify(TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
537 HDC hdc, RECT rc)
538{
539 HWND hwnd = infoPtr->hwnd;
540 NMTVCUSTOMDRAW nmcdhdr;
541 LPNMCUSTOMDRAW nmcd;
542
543 TRACE("drawstage:%lx hdc:%p\n", dwDrawStage, hdc);
544
545 nmcd = &nmcdhdr.nmcd;
546 nmcd->hdr.hwndFrom = hwnd;
547 nmcd->hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
548 nmcd->hdr.code = NM_CUSTOMDRAW;
549 nmcd->dwDrawStage = dwDrawStage;
550 nmcd->hdc = hdc;
551 nmcd->rc = rc;
552 nmcd->dwItemSpec = 0;
553 nmcd->uItemState = 0;
554 nmcd->lItemlParam = 0;
555 nmcdhdr.clrText = infoPtr->clrText;
556 nmcdhdr.clrTextBk = infoPtr->clrBk;
557 nmcdhdr.iLevel = 0;
558
559 return (BOOL)TREEVIEW_SendRealNotify(infoPtr,
560 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
561 (LPARAM)&nmcdhdr);
562}
563
564
565
566/* FIXME: need to find out when the flags in uItemState need to be set */
567
568static BOOL
569TREEVIEW_SendCustomDrawItemNotify(TREEVIEW_INFO *infoPtr, HDC hdc,
570 TREEVIEW_ITEM *wineItem, UINT uItemDrawState)
571{
572 HWND hwnd = infoPtr->hwnd;
573 NMTVCUSTOMDRAW nmcdhdr;
574 LPNMCUSTOMDRAW nmcd;
575 DWORD dwDrawStage, dwItemSpec;
576 UINT uItemState;
577 INT retval;
578
579 dwDrawStage = CDDS_ITEM | uItemDrawState;
580 dwItemSpec = (DWORD)wineItem;
581 uItemState = 0;
582 if (wineItem->state & TVIS_SELECTED)
583 uItemState |= CDIS_SELECTED;
584 if (wineItem == infoPtr->selectedItem)
585 uItemState |= CDIS_FOCUS;
586 if (wineItem == infoPtr->hotItem)
587 uItemState |= CDIS_HOT;
588
589 nmcd = &nmcdhdr.nmcd;
590 nmcd->hdr.hwndFrom = hwnd;
591 nmcd->hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
592 nmcd->hdr.code = NM_CUSTOMDRAW;
593 nmcd->dwDrawStage = dwDrawStage;
594 nmcd->hdc = hdc;
595 nmcd->rc = wineItem->rect;
596 nmcd->dwItemSpec = dwItemSpec;
597 nmcd->uItemState = uItemState;
598 nmcd->lItemlParam = wineItem->lParam;
599 nmcdhdr.clrText = infoPtr->clrText;
600 nmcdhdr.clrTextBk = infoPtr->clrBk;
601 nmcdhdr.iLevel = wineItem->iLevel;
602
603 TRACE("drawstage:%lx hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
604 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
605 nmcd->uItemState, nmcd->lItemlParam);
606
607 retval = TREEVIEW_SendRealNotify(infoPtr,
608 (WPARAM)GetWindowLongA(hwnd, GWL_ID),
609 (LPARAM)&nmcdhdr);
610
611 infoPtr->clrText = nmcdhdr.clrText;
612 infoPtr->clrBk = nmcdhdr.clrTextBk;
613 return (BOOL)retval;
614}
615
616static BOOL
617TREEVIEW_BeginLabelEditNotify(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
618{
619 HWND hwnd = infoPtr->hwnd;
620 LPSTR allocated = NULL;
621 NMTVDISPINFOA tvdi;
622 BOOL ret;
623
624 tvdi.hdr.hwndFrom = hwnd;
625 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
626 tvdi.hdr.code = TVN_BEGINLABELEDITA;
627
628 tvdi.item.mask = TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT;
629 tvdi.item.hItem = editItem;
630 tvdi.item.state = editItem->state;
631 tvdi.item.lParam = editItem->lParam;
632 if (infoPtr->bNtfUnicode) {
633 INT len = MultiByteToWideChar( CP_ACP, 0, editItem->pszText, -1, NULL, 0 );
634 if (len > 1) {
635 tvdi.item.pszText = allocated = (LPSTR)COMCTL32_Alloc (len*sizeof(WCHAR));
636 MultiByteToWideChar( CP_ACP, 0, editItem->pszText, -1, (LPWSTR)tvdi.item.pszText, len*sizeof(WCHAR) );
637 tvdi.item.cchTextMax = len*sizeof(WCHAR);
638 }
639 else {
640 tvdi.item.pszText = editItem->pszText; /* ??? */
641 tvdi.item.cchTextMax = editItem->cchTextMax; /* ??? */
642 }
643 }
644 else {
645 tvdi.item.pszText = editItem->pszText;
646 tvdi.item.cchTextMax = editItem->cchTextMax;
647 }
648
649 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
650 tvdi.hdr.idFrom,
651 (LPARAM)&tvdi);
652 if (allocated)
653 COMCTL32_Free(allocated);
654 return ret;
655}
656
657static void
658TREEVIEW_UpdateDispInfo(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
659 UINT mask)
660{
661 NMTVDISPINFOA callback;
662 HWND hwnd = infoPtr->hwnd;
663
664 mask &= wineItem->callbackMask;
665
666 if (mask == 0) return;
667
668 callback.hdr.hwndFrom = hwnd;
669 callback.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
670 callback.hdr.code = (infoPtr->bNtfUnicode) ? TVN_GETDISPINFOW :
671 TVN_GETDISPINFOA;
672
673 /* 'state' always contains valid value, as well as 'lParam'.
674 * All other parameters are uninitialized.
675 */
676 callback.item.pszText = wineItem->pszText;
677 callback.item.cchTextMax = wineItem->cchTextMax;
678 callback.item.mask = mask;
679 callback.item.hItem = wineItem;
680 callback.item.state = wineItem->state;
681 callback.item.lParam = wineItem->lParam;
682
683 /* If text is changed we need to recalculate textWidth */
684 if (mask & TVIF_TEXT)
685 wineItem->textWidth = 0;
686
687 TREEVIEW_SendRealNotify(infoPtr,
688 (WPARAM)callback.hdr.idFrom, (LPARAM)&callback);
689
690 /* It may have changed due to a call to SetItem. */
691 mask &= wineItem->callbackMask;
692
693 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
694 {
695 /* Instead of copying text into our buffer user specified its own */
696 if (infoPtr->bNtfUnicode) {
697 LPWSTR newText;
698 int buflen;
699 int len = WideCharToMultiByte( CP_ACP, 0,
700 (LPWSTR)callback.item.pszText, -1,
701 NULL, 0, NULL, NULL );
702 buflen = max((len+1)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
703 newText = (LPWSTR)COMCTL32_ReAlloc(wineItem->pszText, buflen);
704
705 TRACE("returned wstr %s, len=%d, buflen=%d\n",
706 debugstr_w((LPWSTR)callback.item.pszText), len, buflen);
707
708 if (newText)
709 {
710 wineItem->pszText = (LPSTR)newText;
711 WideCharToMultiByte( CP_ACP, 0,
712 (LPWSTR)callback.item.pszText, -1,
713 wineItem->pszText, buflen,
714 NULL, NULL );
715 wineItem->cchTextMax = buflen;
716 }
717 /* If ReAlloc fails we have nothing to do, but keep original text */
718 }
719 else {
720 int len = max(lstrlenA(callback.item.pszText) + 1,
721 TEXT_CALLBACK_SIZE);
722 LPSTR newText = COMCTL32_ReAlloc(wineItem->pszText, len);
723
724 TRACE("returned str %s, len=%d\n",
725 debugstr_a(callback.item.pszText), len);
726
727 if (newText)
728 {
729 wineItem->pszText = newText;
730 strcpy(wineItem->pszText, callback.item.pszText);
731 wineItem->cchTextMax = len;
732 }
733 /* If ReAlloc fails we have nothing to do, but keep original text */
734 }
735 }
736 else if (mask & TVIF_TEXT) {
737 /* User put text into our buffer, that is ok unless W string */
738 if (infoPtr->bNtfUnicode) {
739 LPWSTR newText;
740 LPSTR oldText = NULL;
741 int buflen;
742 int len = WideCharToMultiByte( CP_ACP, 0,
743 (LPWSTR)callback.item.pszText, -1,
744 NULL, 0, NULL, NULL );
745 buflen = max((len+1)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
746 newText = (LPWSTR)COMCTL32_Alloc(buflen);
747
748 TRACE("same buffer wstr %s, len=%d, buflen=%d\n",
749 debugstr_w((LPWSTR)callback.item.pszText), len, buflen);
750
751 if (newText)
752 {
753 oldText = wineItem->pszText;
754 wineItem->pszText = (LPSTR)newText;
755 WideCharToMultiByte( CP_ACP, 0,
756 (LPWSTR)callback.item.pszText, -1,
757 wineItem->pszText, buflen, NULL, NULL );
758 wineItem->cchTextMax = buflen;
759 if (oldText)
760 COMCTL32_Free(oldText);
761 }
762 }
763 }
764
765 if (mask & TVIF_IMAGE)
766 wineItem->iImage = callback.item.iImage;
767
768 if (mask & TVIF_SELECTEDIMAGE)
769 wineItem->iSelectedImage = callback.item.iSelectedImage;
770
771 if (mask & TVIF_CHILDREN)
772 wineItem->cChildren = callback.item.cChildren;
773
774 /* These members are now permanently set. */
775 if (callback.item.mask & TVIF_DI_SETITEM)
776 wineItem->callbackMask &= ~callback.item.mask;
777}
778
779/***************************************************************************
780 * This function uses cChildren field to decide whether the item has
781 * children or not.
782 * Note: if this returns TRUE, the child items may not actually exist,
783 * they could be virtual.
784 *
785 * Just use wineItem->firstChild to check for physical children.
786 */
787static BOOL
788TREEVIEW_HasChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
789{
790 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
791
792 return wineItem->cChildren > 0;
793}
794
795
796/* Item Position ********************************************************/
797
798/* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
799static VOID
800TREEVIEW_ComputeItemInternalMetrics(TREEVIEW_INFO *infoPtr,
801 TREEVIEW_ITEM *item)
802{
803 /* Same effect, different optimisation. */
804#if 0
805 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
806 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
807#else
808 BOOL lar = ((infoPtr->dwStyle
809 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
810 > TVS_LINESATROOT);
811#endif
812
813 item->linesOffset = infoPtr->uIndent * (item->iLevel + lar - 1)
814 - infoPtr->scrollX;
815 item->stateOffset = item->linesOffset + infoPtr->uIndent;
816 item->imageOffset = item->stateOffset
817 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
818 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
819}
820
821static VOID
822TREEVIEW_ComputeTextWidth(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
823{
824 HDC hdc;
825 HFONT hOldFont=0;
826 SIZE sz;
827
828 /* DRAW's OM docker creates items like this */
829 if (item->pszText == NULL)
830 {
831 item->textWidth = 0;
832 return;
833 }
834
835 if (item->textWidth != 0 && !(item->callbackMask & TVIF_TEXT))
836 return;
837
838 if (hDC != 0)
839 {
840 hdc = hDC;
841 }
842 else
843 {
844 hdc = GetDC(infoPtr->hwnd);
845 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
846 }
847
848 GetTextExtentPoint32A(hdc, item->pszText, strlen(item->pszText), &sz);
849 item->textWidth = sz.cx;
850
851 if (hDC == 0)
852 {
853 SelectObject(hdc, hOldFont);
854 ReleaseDC(0, hdc);
855 }
856}
857
858static VOID
859TREEVIEW_ComputeItemRect(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
860{
861 item->rect.top = infoPtr->uItemHeight *
862 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
863
864 item->rect.bottom = item->rect.top
865 + infoPtr->uItemHeight * item->iIntegral - 1;
866
867 item->rect.left = 0;
868 item->rect.right = infoPtr->clientWidth;
869}
870
871/* We know that only items after start need their order updated. */
872static void
873TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
874{
875 TREEVIEW_ITEM *item;
876 int order;
877
878 if (!start)
879 {
880 start = infoPtr->root->firstChild;
881 order = 0;
882 }
883 else
884 order = start->visibleOrder;
885
886 for (item = start; item != NULL;
887 item = TREEVIEW_GetNextListItem(infoPtr, item))
888 {
889 item->visibleOrder = order;
890 order += item->iIntegral;
891 }
892
893 infoPtr->maxVisibleOrder = order;
894
895 for (item = start; item != NULL;
896 item = TREEVIEW_GetNextListItem(infoPtr, item))
897 {
898 TREEVIEW_ComputeItemRect(infoPtr, item);
899 }
900}
901
902
903/* Update metrics of all items in selected subtree.
904 * root must be expanded
905 */
906static VOID
907TREEVIEW_UpdateSubTree(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
908{
909 TREEVIEW_ITEM *sibling;
910 HDC hdc;
911 HFONT hOldFont;
912
913 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
914 return;
915
916 root->state &= ~TVIS_EXPANDED;
917 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
918 root->state |= TVIS_EXPANDED;
919
920 hdc = GetDC(infoPtr->hwnd);
921 hOldFont = SelectObject(hdc, infoPtr->hFont);
922
923 for (; root != sibling;
924 root = TREEVIEW_GetNextListItem(infoPtr, root))
925 {
926 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
927
928 if (root->callbackMask & TVIF_TEXT)
929 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
930
931 if (root->textWidth == 0)
932 {
933 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
934 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
935 }
936 }
937
938 SelectObject(hdc, hOldFont);
939 ReleaseDC(infoPtr->hwnd, hdc);
940}
941
942/* Item Allocation **********************************************************/
943
944static TREEVIEW_ITEM *
945TREEVIEW_AllocateItem(TREEVIEW_INFO *infoPtr)
946{
947 TREEVIEW_ITEM *newItem = COMCTL32_Alloc(sizeof(TREEVIEW_ITEM));
948
949 if (!newItem)
950 return NULL;
951
952 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
953 {
954 COMCTL32_Free(newItem);
955 return NULL;
956 }
957
958 return newItem;
959}
960
961/* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
962 * free item->pszText. */
963static void
964TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
965{
966 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
967 COMCTL32_Free(item);
968 if (infoPtr->selectedItem == item)
969 infoPtr->selectedItem = NULL;
970}
971
972
973/* Item Insertion *******************************************************/
974
975/***************************************************************************
976 * This method inserts newItem before sibling as a child of parent.
977 * sibling can be NULL, but only if parent has no children.
978 */
979static void
980TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
981 TREEVIEW_ITEM *parent)
982{
983 assert(newItem != NULL);
984 assert(parent != NULL);
985
986 if (sibling != NULL)
987 {
988 assert(sibling->parent == parent);
989
990 if (sibling->prevSibling != NULL)
991 sibling->prevSibling->nextSibling = newItem;
992
993 newItem->prevSibling = sibling->prevSibling;
994 sibling->prevSibling = newItem;
995 }
996 else
997 newItem->prevSibling = NULL;
998
999 newItem->nextSibling = sibling;
1000
1001 if (parent->firstChild == sibling)
1002 parent->firstChild = newItem;
1003
1004 if (parent->lastChild == NULL)
1005 parent->lastChild = newItem;
1006}
1007
1008/***************************************************************************
1009 * This method inserts newItem after sibling as a child of parent.
1010 * sibling can be NULL, but only if parent has no children.
1011 */
1012static void
1013TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1014 TREEVIEW_ITEM *parent)
1015{
1016 assert(newItem != NULL);
1017 assert(parent != NULL);
1018
1019 if (sibling != NULL)
1020 {
1021 assert(sibling->parent == parent);
1022
1023 if (sibling->nextSibling != NULL)
1024 sibling->nextSibling->prevSibling = newItem;
1025
1026 newItem->nextSibling = sibling->nextSibling;
1027 sibling->nextSibling = newItem;
1028 }
1029 else
1030 newItem->nextSibling = NULL;
1031
1032 newItem->prevSibling = sibling;
1033
1034 if (parent->lastChild == sibling)
1035 parent->lastChild = newItem;
1036
1037 if (parent->firstChild == NULL)
1038 parent->firstChild = newItem;
1039}
1040
1041static BOOL
1042TREEVIEW_DoSetItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1043 const TVITEMEXA *tvItem)
1044{
1045 UINT callbackClear = 0;
1046 UINT callbackSet = 0;
1047
1048 /* Do this first in case it fails. */
1049 if (tvItem->mask & TVIF_TEXT)
1050 {
1051 wineItem->textWidth = 0; /* force width recalculation */
1052 if (tvItem->pszText != LPSTR_TEXTCALLBACKA)
1053 {
1054 int len = lstrlenA(tvItem->pszText) + 1;
1055 LPSTR newText = COMCTL32_ReAlloc(wineItem->pszText, len);
1056
1057 if (newText == NULL) return FALSE;
1058
1059 callbackClear |= TVIF_TEXT;
1060
1061 wineItem->pszText = newText;
1062 wineItem->cchTextMax = len;
1063 lstrcpynA(wineItem->pszText, tvItem->pszText, len);
1064 TRACE("setting text %s, item %p\n",
1065 debugstr_a(wineItem->pszText), wineItem);
1066 }
1067 else
1068 {
1069 callbackSet |= TVIF_TEXT;
1070
1071 wineItem->pszText = COMCTL32_ReAlloc(wineItem->pszText,
1072 TEXT_CALLBACK_SIZE);
1073 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1074 TRACE("setting callback, item %p\n",
1075 wineItem);
1076 }
1077 }
1078
1079 if (tvItem->mask & TVIF_CHILDREN)
1080 {
1081 wineItem->cChildren = tvItem->cChildren;
1082
1083 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1084 callbackSet |= TVIF_CHILDREN;
1085 else
1086 callbackClear |= TVIF_CHILDREN;
1087 }
1088
1089 if (tvItem->mask & TVIF_IMAGE)
1090 {
1091 wineItem->iImage = tvItem->iImage;
1092
1093 if (wineItem->iImage == I_IMAGECALLBACK)
1094 callbackSet |= TVIF_IMAGE;
1095 else
1096 callbackClear |= TVIF_IMAGE;
1097 }
1098
1099 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1100 {
1101 wineItem->iSelectedImage = tvItem->iSelectedImage;
1102
1103 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1104 callbackSet |= TVIF_SELECTEDIMAGE;
1105 else
1106 callbackClear |= TVIF_SELECTEDIMAGE;
1107 }
1108
1109 if (tvItem->mask & TVIF_PARAM)
1110 wineItem->lParam = tvItem->lParam;
1111
1112 /* If the application sets TVIF_INTEGRAL without
1113 * supplying a TVITEMEX structure, it's toast. */
1114 if (tvItem->mask & TVIF_INTEGRAL)
1115 wineItem->iIntegral = tvItem->iIntegral;
1116
1117 if (tvItem->mask & TVIF_STATE)
1118 {
1119 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1120 tvItem->stateMask);
1121 wineItem->state &= ~tvItem->stateMask;
1122 wineItem->state |= (tvItem->state & tvItem->stateMask);
1123 }
1124
1125 wineItem->callbackMask |= callbackSet;
1126 wineItem->callbackMask &= ~callbackClear;
1127
1128 return TRUE;
1129}
1130
1131/* Note that the new item is pre-zeroed. */
1132static LRESULT
1133TREEVIEW_InsertItemA(TREEVIEW_INFO *infoPtr, LPARAM lParam)
1134{
1135 const TVINSERTSTRUCTA *ptdi = (LPTVINSERTSTRUCTA) lParam;
1136 const TVITEMEXA *tvItem = &ptdi->DUMMYUNIONNAME.itemex;
1137 HTREEITEM insertAfter;
1138 TREEVIEW_ITEM *newItem, *parentItem;
1139 BOOL bTextUpdated = FALSE;
1140
1141 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1142 {
1143 parentItem = infoPtr->root;
1144 }
1145 else
1146 {
1147 parentItem = ptdi->hParent;
1148
1149 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1150 {
1151 WARN("invalid parent %p\n", parentItem);
1152 return (LRESULT)(HTREEITEM)NULL;
1153 }
1154 }
1155
1156 insertAfter = ptdi->hInsertAfter;
1157
1158 /* Validate this now for convenience. */
1159 switch ((DWORD)insertAfter)
1160 {
1161 case (DWORD)TVI_FIRST:
1162 case (DWORD)TVI_LAST:
1163 case (DWORD)TVI_SORT:
1164 break;
1165
1166 default:
1167 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1168 insertAfter->parent != parentItem)
1169 {
1170 WARN("invalid insert after %p\n", insertAfter);
1171 insertAfter = TVI_LAST;
1172 }
1173 }
1174
1175 TRACE("parent %p position %p: '%s'\n", parentItem, insertAfter,
1176 (tvItem->mask & TVIF_TEXT)
1177 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKA) ? "<callback>"
1178 : tvItem->pszText)
1179 : "<no label>");
1180
1181 newItem = TREEVIEW_AllocateItem(infoPtr);
1182 if (newItem == NULL)
1183 return (LRESULT)(HTREEITEM)NULL;
1184
1185 newItem->parent = parentItem;
1186 newItem->iIntegral = 1;
1187
1188 if (!TREEVIEW_DoSetItem(infoPtr, newItem, tvItem))
1189 return (LRESULT)(HTREEITEM)NULL;
1190
1191 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1192
1193 infoPtr->uNumItems++;
1194
1195 switch ((DWORD)insertAfter)
1196 {
1197 case (DWORD)TVI_FIRST:
1198 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1199 if (infoPtr->firstVisible == parentItem->firstChild)
1200 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1201 break;
1202
1203 case (DWORD)TVI_LAST:
1204 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1205 break;
1206
1207 /* hInsertAfter names a specific item we want to insert after */
1208 default:
1209 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1210 break;
1211
1212 case (DWORD)TVI_SORT:
1213 {
1214 TREEVIEW_ITEM *aChild;
1215 TREEVIEW_ITEM *previousChild = NULL;
1216 BOOL bItemInserted = FALSE;
1217
1218 aChild = parentItem->firstChild;
1219
1220 bTextUpdated = TRUE;
1221 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1222
1223 /* Iterate the parent children to see where we fit in */
1224 while (aChild != NULL)
1225 {
1226 INT comp;
1227
1228 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1229 comp = lstrcmpA(newItem->pszText, aChild->pszText);
1230
1231 if (comp < 0) /* we are smaller than the current one */
1232 {
1233 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1234 bItemInserted = TRUE;
1235 break;
1236 }
1237 else if (comp > 0) /* we are bigger than the current one */
1238 {
1239 previousChild = aChild;
1240
1241 /* This will help us to exit if there is no more sibling */
1242 aChild = (aChild->nextSibling == 0)
1243 ? NULL
1244 : aChild->nextSibling;
1245
1246 /* Look at the next item */
1247 continue;
1248 }
1249 else if (comp == 0)
1250 {
1251 /*
1252 * An item with this name is already existing, therefore,
1253 * we add after the one we found
1254 */
1255 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1256 bItemInserted = TRUE;
1257 break;
1258 }
1259 }
1260
1261 /*
1262 * we reach the end of the child list and the item has not
1263 * yet been inserted, therefore, insert it after the last child.
1264 */
1265 if ((!bItemInserted) && (aChild == NULL))
1266 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1267
1268 break;
1269 }
1270 }
1271
1272
1273 TRACE("new item %p; parent %p, mask %x\n", newItem,
1274 newItem->parent, tvItem->mask);
1275
1276 newItem->iLevel = newItem->parent->iLevel + 1;
1277
1278 if (newItem->parent->cChildren == 0)
1279 newItem->parent->cChildren = 1;
1280
1281 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1282 {
1283 if (STATEIMAGEINDEX(newItem->state) == 0)
1284 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1285 }
1286
1287 if (infoPtr->firstVisible == NULL)
1288 infoPtr->firstVisible = newItem;
1289
1290 TREEVIEW_VerifyTree(infoPtr);
1291
1292 if (parentItem == infoPtr->root ||
1293 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1294 {
1295 TREEVIEW_ITEM *item;
1296 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1297
1298 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1299 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1300
1301 if (!bTextUpdated)
1302 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1303
1304 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1305 TREEVIEW_UpdateScrollBars(infoPtr);
1306 /*
1307 * if the item was inserted in a visible part of the tree,
1308 * invalidate it, as well as those after it
1309 */
1310 for (item = newItem;
1311 item != NULL;
1312 item = TREEVIEW_GetNextListItem(infoPtr, item))
1313 TREEVIEW_Invalidate(infoPtr, item);
1314 }
1315 else
1316 {
1317 newItem->visibleOrder = -1;
1318
1319 /* refresh treeview if newItem is the first item inserted under parentItem */
1320 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1321 {
1322 /* parent got '+' - update it */
1323 TREEVIEW_Invalidate(infoPtr, parentItem);
1324 }
1325 }
1326
1327 return (LRESULT)newItem;
1328}
1329
1330
1331static LRESULT
1332TREEVIEW_InsertItemW(TREEVIEW_INFO *infoPtr, LPARAM lParam)
1333{
1334 TVINSERTSTRUCTW *tvisW;
1335 TVINSERTSTRUCTA tvisA;
1336 LRESULT lRes;
1337
1338 tvisW = (LPTVINSERTSTRUCTW) lParam;
1339
1340 tvisA.hParent = tvisW->hParent;
1341 tvisA.hInsertAfter = tvisW->hInsertAfter;
1342
1343 tvisA.DUMMYUNIONNAME.item.mask = tvisW->DUMMYUNIONNAME.item.mask;
1344 tvisA.DUMMYUNIONNAME.item.hItem = tvisW->DUMMYUNIONNAME.item.hItem;
1345 tvisA.DUMMYUNIONNAME.item.state = tvisW->DUMMYUNIONNAME.item.state;
1346 tvisA.DUMMYUNIONNAME.item.stateMask = tvisW->DUMMYUNIONNAME.item.stateMask;
1347 tvisA.DUMMYUNIONNAME.item.cchTextMax =
1348 tvisW->DUMMYUNIONNAME.item.cchTextMax;
1349
1350 if (tvisW->DUMMYUNIONNAME.item.pszText)
1351 {
1352 if (tvisW->DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKW)
1353 {
1354 int len = WideCharToMultiByte( CP_ACP, 0, tvisW->DUMMYUNIONNAME.item.pszText, -1,
1355 NULL, 0, NULL, NULL );
1356 tvisA.DUMMYUNIONNAME.item.pszText = COMCTL32_Alloc(len);
1357 WideCharToMultiByte( CP_ACP, 0, tvisW->DUMMYUNIONNAME.item.pszText, -1,
1358 tvisA.DUMMYUNIONNAME.item.pszText, len, NULL, NULL );
1359 }
1360 else
1361 {
1362 tvisA.DUMMYUNIONNAME.item.pszText = LPSTR_TEXTCALLBACKA;
1363 tvisA.DUMMYUNIONNAME.item.cchTextMax = 0;
1364 }
1365 }
1366
1367 tvisA.DUMMYUNIONNAME.item.iImage = tvisW->DUMMYUNIONNAME.item.iImage;
1368 tvisA.DUMMYUNIONNAME.item.iSelectedImage =
1369 tvisW->DUMMYUNIONNAME.item.iSelectedImage;
1370 tvisA.DUMMYUNIONNAME.item.cChildren = tvisW->DUMMYUNIONNAME.item.cChildren;
1371 tvisA.DUMMYUNIONNAME.item.lParam = tvisW->DUMMYUNIONNAME.item.lParam;
1372
1373 lRes = TREEVIEW_InsertItemA(infoPtr, (LPARAM)&tvisA);
1374
1375 if (tvisA.DUMMYUNIONNAME.item.pszText != LPSTR_TEXTCALLBACKA)
1376 {
1377 COMCTL32_Free(tvisA.DUMMYUNIONNAME.item.pszText);
1378 }
1379
1380 return lRes;
1381
1382}
1383
1384
1385/* Item Deletion ************************************************************/
1386static void
1387TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1388
1389static void
1390TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *parentItem)
1391{
1392 TREEVIEW_ITEM *kill = parentItem->firstChild;
1393
1394 while (kill != NULL)
1395 {
1396 TREEVIEW_ITEM *next = kill->nextSibling;
1397
1398 TREEVIEW_RemoveItem(infoPtr, kill);
1399
1400 kill = next;
1401 }
1402
1403 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1404 assert(parentItem->firstChild == NULL);
1405 assert(parentItem->lastChild == NULL);
1406}
1407
1408static void
1409TREEVIEW_UnlinkItem(TREEVIEW_ITEM *item)
1410{
1411 TREEVIEW_ITEM *parentItem = item->parent;
1412
1413 assert(item != NULL);
1414 assert(item->parent != NULL); /* i.e. it must not be the root */
1415
1416 if (parentItem->firstChild == item)
1417 parentItem->firstChild = item->nextSibling;
1418
1419 if (parentItem->lastChild == item)
1420 parentItem->lastChild = item->prevSibling;
1421
1422 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1423 && parentItem->cChildren > 0)
1424 parentItem->cChildren = 0;
1425
1426 if (item->prevSibling)
1427 item->prevSibling->nextSibling = item->nextSibling;
1428
1429 if (item->nextSibling)
1430 item->nextSibling->prevSibling = item->prevSibling;
1431}
1432
1433static void
1434TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1435{
1436 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1437
1438 TREEVIEW_SendTreeviewNotify(infoPtr,
1439 (infoPtr->bNtfUnicode) ? TVN_DELETEITEMW :
1440 TVN_DELETEITEMA,
1441 TVIF_HANDLE | TVIF_PARAM, 0, wineItem, 0);
1442
1443 if (wineItem->firstChild)
1444 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1445
1446 TREEVIEW_UnlinkItem(wineItem);
1447
1448 infoPtr->uNumItems--;
1449
1450 if (wineItem->pszText != LPSTR_TEXTCALLBACKA)
1451 COMCTL32_Free(wineItem->pszText);
1452
1453 TREEVIEW_FreeItem(infoPtr, wineItem);
1454}
1455
1456
1457/* Empty out the tree. */
1458static void
1459TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1460{
1461 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1462
1463 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1464}
1465
1466static LRESULT
1467TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1468{
1469 TREEVIEW_ITEM *oldSelection = infoPtr->selectedItem;
1470 TREEVIEW_ITEM *newSelection = oldSelection;
1471 TREEVIEW_ITEM *newFirstVisible = NULL;
1472 TREEVIEW_ITEM *parent, *prev = NULL;
1473 BOOL visible = FALSE;
1474
1475 if (wineItem == TVI_ROOT)
1476 {
1477 TRACE("TVI_ROOT\n");
1478 parent = infoPtr->root;
1479 newSelection = NULL;
1480 visible = TRUE;
1481 TREEVIEW_RemoveTree(infoPtr);
1482 }
1483 else
1484 {
1485 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1486 return FALSE;
1487
1488 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1489 parent = wineItem->parent;
1490
1491 if (ISVISIBLE(wineItem))
1492 {
1493 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1494 visible = TRUE;
1495 }
1496
1497 if (infoPtr->selectedItem != NULL
1498 && (wineItem == infoPtr->selectedItem
1499 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1500 {
1501 if (wineItem->nextSibling)
1502 newSelection = wineItem->nextSibling;
1503 else if (wineItem->parent != infoPtr->root)
1504 newSelection = wineItem->parent;
1505 }
1506
1507 if (infoPtr->firstVisible == wineItem)
1508 {
1509 if (wineItem->nextSibling)
1510 newFirstVisible = wineItem->nextSibling;
1511 else if (wineItem->prevSibling)
1512 newFirstVisible = wineItem->prevSibling;
1513 else if (wineItem->parent != infoPtr->root)
1514 newFirstVisible = wineItem->parent;
1515 }
1516 else
1517 newFirstVisible = infoPtr->firstVisible;
1518
1519 TREEVIEW_RemoveItem(infoPtr, wineItem);
1520 }
1521
1522 /* Don't change if somebody else already has. */
1523 if (oldSelection == infoPtr->selectedItem)
1524 {
1525#ifdef __WIN32OS2__
1526 //Crash in UltraEdit when opening file, because the old selected
1527 //item is already freed
1528 if (oldSelection == wineItem) {
1529 infoPtr->selectedItem = 0;
1530 }
1531#endif
1532 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1533 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1534 else
1535 infoPtr->selectedItem = 0;
1536 }
1537
1538 /* Validate insertMark dropItem.
1539 * hotItem ??? - used for comparison only.
1540 */
1541 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1542 infoPtr->insertMarkItem = 0;
1543
1544 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1545 infoPtr->dropItem = 0;
1546
1547 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1548 newFirstVisible = infoPtr->root->firstChild;
1549
1550 TREEVIEW_VerifyTree(infoPtr);
1551
1552
1553 if (visible)
1554 {
1555 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1556 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1557 TREEVIEW_UpdateScrollBars(infoPtr);
1558 TREEVIEW_Invalidate(infoPtr, NULL);
1559 }
1560 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1561 {
1562 /* parent lost '+/-' - update it */
1563 TREEVIEW_Invalidate(infoPtr, parent);
1564 }
1565
1566 return TRUE;
1567}
1568
1569
1570/* Get/Set Messages *********************************************************/
1571static LRESULT
1572TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam, LPARAM lParam)
1573{
1574 if(wParam)
1575 infoPtr->bRedraw = TRUE;
1576 else
1577 infoPtr->bRedraw = FALSE;
1578
1579 return 0;
1580}
1581
1582static LRESULT
1583TREEVIEW_GetIndent(TREEVIEW_INFO *infoPtr)
1584{
1585 TRACE("\n");
1586 return infoPtr->uIndent;
1587}
1588
1589static LRESULT
1590TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1591{
1592 TRACE("\n");
1593
1594 if (newIndent < MINIMUM_INDENT)
1595 newIndent = MINIMUM_INDENT;
1596
1597 if (infoPtr->uIndent != newIndent)
1598 {
1599 infoPtr->uIndent = newIndent;
1600 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1601 TREEVIEW_UpdateScrollBars(infoPtr);
1602 TREEVIEW_Invalidate(infoPtr, NULL);
1603 }
1604
1605 return 0;
1606}
1607
1608
1609static LRESULT
1610TREEVIEW_GetToolTips(TREEVIEW_INFO *infoPtr)
1611{
1612 TRACE("\n");
1613 return (LRESULT)infoPtr->hwndToolTip;
1614}
1615
1616static LRESULT
1617TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1618{
1619 HWND prevToolTip;
1620
1621 TRACE("\n");
1622 prevToolTip = infoPtr->hwndToolTip;
1623 infoPtr->hwndToolTip = hwndTT;
1624
1625 return (LRESULT)prevToolTip;
1626}
1627
1628
1629static LRESULT
1630TREEVIEW_GetScrollTime(TREEVIEW_INFO *infoPtr)
1631{
1632 return infoPtr->uScrollTime;
1633}
1634
1635static LRESULT
1636TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1637{
1638 UINT uOldScrollTime = infoPtr->uScrollTime;
1639
1640 infoPtr->uScrollTime = min(uScrollTime, 100);
1641
1642 return uOldScrollTime;
1643}
1644
1645
1646static LRESULT
1647TREEVIEW_GetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam)
1648{
1649 TRACE("\n");
1650
1651 switch (wParam)
1652 {
1653 case (WPARAM)TVSIL_NORMAL:
1654 return (LRESULT)infoPtr->himlNormal;
1655
1656 case (WPARAM)TVSIL_STATE:
1657 return (LRESULT)infoPtr->himlState;
1658
1659 default:
1660 return 0;
1661 }
1662}
1663
1664static LRESULT
1665TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1666{
1667 HIMAGELIST himlOld = 0;
1668 int oldWidth = infoPtr->normalImageWidth;
1669 int oldHeight = infoPtr->normalImageHeight;
1670
1671
1672 TRACE("%x,%p\n", wParam, himlNew);
1673
1674 switch (wParam)
1675 {
1676 case (WPARAM)TVSIL_NORMAL:
1677 himlOld = infoPtr->himlNormal;
1678 infoPtr->himlNormal = himlNew;
1679
1680 if (himlNew != NULL)
1681 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1682 &infoPtr->normalImageHeight);
1683 else
1684 {
1685 infoPtr->normalImageWidth = 0;
1686 infoPtr->normalImageHeight = 0;
1687 }
1688
1689 break;
1690
1691 case (WPARAM)TVSIL_STATE:
1692 himlOld = infoPtr->himlState;
1693 infoPtr->himlState = himlNew;
1694
1695 if (himlNew != NULL)
1696 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1697 &infoPtr->stateImageHeight);
1698 else
1699 {
1700 infoPtr->stateImageWidth = 0;
1701 infoPtr->stateImageHeight = 0;
1702 }
1703
1704 break;
1705 }
1706
1707 if (oldWidth != infoPtr->normalImageWidth ||
1708 oldHeight != infoPtr->normalImageHeight)
1709 {
1710 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1711 TREEVIEW_UpdateScrollBars(infoPtr);
1712 }
1713
1714 TREEVIEW_Invalidate(infoPtr, NULL);
1715
1716 return (LRESULT)himlOld;
1717}
1718
1719/* Compute the natural height (based on the font size) for items. */
1720static UINT
1721TREEVIEW_NaturalHeight(TREEVIEW_INFO *infoPtr)
1722{
1723 TEXTMETRICA tm;
1724 HDC hdc = GetDC(0);
1725 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1726
1727 GetTextMetricsA(hdc, &tm);
1728
1729 SelectObject(hdc, hOldFont);
1730 ReleaseDC(0, hdc);
1731
1732 /* The 16 is a hack because our fonts are tiny. */
1733 /* add 2 for the focus border and 1 more for margin some apps assume */
1734 return max(16, tm.tmHeight + tm.tmExternalLeading + 3);
1735}
1736
1737static LRESULT
1738TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1739{
1740 INT prevHeight = infoPtr->uItemHeight;
1741
1742 TRACE("%d \n", newHeight);
1743 if (newHeight == -1)
1744 {
1745 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1746 infoPtr->bHeightSet = FALSE;
1747 }
1748 else
1749 {
1750 infoPtr->uItemHeight = newHeight;
1751 infoPtr->bHeightSet = TRUE;
1752 }
1753
1754 /* Round down, unless we support odd ("non even") heights. */
1755 if (!(infoPtr->dwStyle) & TVS_NONEVENHEIGHT)
1756 infoPtr->uItemHeight &= ~1;
1757
1758 if (infoPtr->uItemHeight != prevHeight)
1759 {
1760 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1761 TREEVIEW_UpdateScrollBars(infoPtr);
1762 TREEVIEW_Invalidate(infoPtr, NULL);
1763 }
1764
1765 return prevHeight;
1766}
1767
1768static LRESULT
1769TREEVIEW_GetItemHeight(TREEVIEW_INFO *infoPtr)
1770{
1771 TRACE("\n");
1772 return infoPtr->uItemHeight;
1773}
1774
1775
1776static LRESULT
1777TREEVIEW_GetFont(TREEVIEW_INFO *infoPtr)
1778{
1779 TRACE("%p\n", infoPtr->hFont);
1780 return (LRESULT)infoPtr->hFont;
1781}
1782
1783
1784static INT CALLBACK
1785TREEVIEW_ResetTextWidth(LPVOID pItem, DWORD unused)
1786{
1787 (void)unused;
1788
1789 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1790
1791 return 1;
1792}
1793
1794static LRESULT
1795TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1796{
1797 UINT uHeight = infoPtr->uItemHeight;
1798
1799 TRACE("%p %i\n", hFont, bRedraw);
1800
1801 infoPtr->hFont = hFont ? hFont : GetStockObject(SYSTEM_FONT);
1802
1803 DeleteObject(infoPtr->hBoldFont);
1804 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1805
1806 if (!infoPtr->bHeightSet)
1807 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1808
1809 if (uHeight != infoPtr->uItemHeight)
1810 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1811
1812 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1813
1814 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1815 TREEVIEW_UpdateScrollBars(infoPtr);
1816
1817 if (bRedraw)
1818 TREEVIEW_Invalidate(infoPtr, NULL);
1819
1820 return 0;
1821}
1822
1823
1824static LRESULT
1825TREEVIEW_GetLineColor(TREEVIEW_INFO *infoPtr)
1826{
1827 TRACE("\n");
1828 return (LRESULT)infoPtr->clrLine;
1829}
1830
1831static LRESULT
1832TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1833{
1834 COLORREF prevColor = infoPtr->clrLine;
1835
1836 TRACE("\n");
1837 infoPtr->clrLine = color;
1838 return (LRESULT)prevColor;
1839}
1840
1841
1842static LRESULT
1843TREEVIEW_GetTextColor(TREEVIEW_INFO *infoPtr)
1844{
1845 TRACE("\n");
1846 return (LRESULT)infoPtr->clrText;
1847}
1848
1849static LRESULT
1850TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1851{
1852 COLORREF prevColor = infoPtr->clrText;
1853
1854 TRACE("\n");
1855 infoPtr->clrText = color;
1856
1857 if (infoPtr->clrText != prevColor)
1858 TREEVIEW_Invalidate(infoPtr, NULL);
1859
1860 return (LRESULT)prevColor;
1861}
1862
1863
1864static LRESULT
1865TREEVIEW_GetBkColor(TREEVIEW_INFO *infoPtr)
1866{
1867 TRACE("\n");
1868 return (LRESULT)infoPtr->clrBk;
1869}
1870
1871static LRESULT
1872TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1873{
1874 COLORREF prevColor = infoPtr->clrBk;
1875
1876 TRACE("\n");
1877 infoPtr->clrBk = newColor;
1878
1879 if (newColor != prevColor)
1880 TREEVIEW_Invalidate(infoPtr, NULL);
1881
1882 return (LRESULT)prevColor;
1883}
1884
1885
1886static LRESULT
1887TREEVIEW_GetInsertMarkColor(TREEVIEW_INFO *infoPtr)
1888{
1889 TRACE("\n");
1890 return (LRESULT)infoPtr->clrInsertMark;
1891}
1892
1893static LRESULT
1894TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1895{
1896 COLORREF prevColor = infoPtr->clrInsertMark;
1897
1898 TRACE("%lx\n", color);
1899 infoPtr->clrInsertMark = color;
1900
1901 return (LRESULT)prevColor;
1902}
1903
1904
1905static LRESULT
1906TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1907{
1908 TRACE("%d %p\n", wParam, item);
1909
1910 if (!TREEVIEW_ValidItem(infoPtr, item))
1911 return 0;
1912
1913 infoPtr->insertBeforeorAfter = wParam;
1914 infoPtr->insertMarkItem = item;
1915
1916 TREEVIEW_Invalidate(infoPtr, NULL);
1917
1918 return 1;
1919}
1920
1921
1922/************************************************************************
1923 * Some serious braindamage here. lParam is a pointer to both the
1924 * input HTREEITEM and the output RECT.
1925 */
1926static LRESULT
1927TREEVIEW_GetItemRect(TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1928{
1929 TREEVIEW_ITEM *wineItem;
1930 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
1931
1932 TRACE("\n");
1933 /*
1934 * validate parameters
1935 */
1936 if (pItem == NULL)
1937 return FALSE;
1938
1939 wineItem = *pItem;
1940 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
1941 return FALSE;
1942
1943 /*
1944 * If wParam is TRUE return the text size otherwise return
1945 * the whole item size
1946 */
1947 if (fTextRect)
1948 {
1949 /* Windows does not send TVN_GETDISPINFO here. */
1950
1951 lpRect->top = wineItem->rect.top;
1952 lpRect->bottom = wineItem->rect.bottom;
1953
1954 lpRect->left = wineItem->textOffset;
1955 lpRect->right = wineItem->textOffset + wineItem->textWidth;
1956 }
1957 else
1958 {
1959 *lpRect = wineItem->rect;
1960 }
1961
1962 TRACE("%s [L:%d R:%d T:%d B:%d]\n", fTextRect ? "text" : "item",
1963 lpRect->left, lpRect->right, lpRect->top, lpRect->bottom);
1964
1965 return TRUE;
1966}
1967
1968static inline LRESULT
1969TREEVIEW_GetVisibleCount(TREEVIEW_INFO *infoPtr)
1970{
1971 /* Suprise! This does not take integral height into account. */
1972 return infoPtr->clientHeight / infoPtr->uItemHeight;
1973}
1974
1975
1976static LRESULT
1977TREEVIEW_GetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
1978{
1979 TREEVIEW_ITEM *wineItem;
1980
1981 wineItem = tvItem->hItem;
1982 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1983 return FALSE;
1984
1985 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
1986
1987 if (tvItem->mask & TVIF_CHILDREN)
1988 tvItem->cChildren = wineItem->cChildren;
1989
1990 if (tvItem->mask & TVIF_HANDLE)
1991 tvItem->hItem = wineItem;
1992
1993 if (tvItem->mask & TVIF_IMAGE)
1994 tvItem->iImage = wineItem->iImage;
1995
1996 if (tvItem->mask & TVIF_INTEGRAL)
1997 tvItem->iIntegral = wineItem->iIntegral;
1998
1999 /* undocumented: windows ignores TVIF_PARAM and
2000 * * always sets lParam
2001 */
2002 tvItem->lParam = wineItem->lParam;
2003
2004 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2005 tvItem->iSelectedImage = wineItem->iSelectedImage;
2006
2007 if (tvItem->mask & TVIF_STATE)
2008 tvItem->state = wineItem->state & tvItem->stateMask;
2009
2010 if (tvItem->mask & TVIF_TEXT)
2011 lstrcpynA(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2012
2013 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2014 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2015
2016 return TRUE;
2017}
2018
2019/* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2020 * which is wrong. */
2021static LRESULT
2022TREEVIEW_SetItemA(TREEVIEW_INFO *infoPtr, LPTVITEMEXA tvItem)
2023{
2024 TREEVIEW_ITEM *wineItem;
2025 TREEVIEW_ITEM originalItem;
2026
2027 wineItem = tvItem->hItem;
2028
2029 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2030 tvItem->mask);
2031
2032 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2033 return FALSE;
2034
2035 if (!TREEVIEW_DoSetItem(infoPtr, wineItem, tvItem))
2036 return FALSE;
2037
2038 /* store the orignal item values */
2039 originalItem = *wineItem;
2040
2041 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2042 if ((tvItem->mask & TVIF_TEXT
2043 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2044 && ISVISIBLE(wineItem))
2045 {
2046 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2047 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2048 }
2049
2050 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2051 {
2052 /* The refresh updates everything, but we can't wait until then. */
2053 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2054
2055 /* if any of the items values changed, redraw the item */
2056 if(memcmp(&originalItem, wineItem, sizeof(TREEVIEW_ITEM)))
2057 {
2058 if (tvItem->mask & TVIF_INTEGRAL)
2059 {
2060 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2061 TREEVIEW_UpdateScrollBars(infoPtr);
2062
2063 TREEVIEW_Invalidate(infoPtr, NULL);
2064 }
2065 else
2066 {
2067 TREEVIEW_UpdateScrollBars(infoPtr);
2068 TREEVIEW_Invalidate(infoPtr, wineItem);
2069 }
2070 }
2071 }
2072
2073 return TRUE;
2074}
2075
2076static LRESULT
2077TREEVIEW_GetItemW(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem)
2078{
2079 TREEVIEW_ITEM *wineItem;
2080 INT iItem;
2081 iItem = (INT)tvItem->hItem;
2082
2083 wineItem = tvItem->hItem;
2084 if(!TREEVIEW_ValidItem (infoPtr, wineItem))
2085 return FALSE;
2086
2087 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2088
2089 if (tvItem->mask & TVIF_CHILDREN) {
2090 if (TVIF_CHILDREN==I_CHILDRENCALLBACK)
2091 FIXME("I_CHILDRENCALLBACK not supported\n");
2092 tvItem->cChildren = wineItem->cChildren;
2093 }
2094
2095 if (tvItem->mask & TVIF_HANDLE) {
2096 tvItem->hItem = wineItem;
2097 }
2098 if (tvItem->mask & TVIF_IMAGE) {
2099 tvItem->iImage = wineItem->iImage;
2100 }
2101 if (tvItem->mask & TVIF_INTEGRAL) {
2102 tvItem->iIntegral = wineItem->iIntegral;
2103 }
2104 /* undocumented: windows ignores TVIF_PARAM and
2105 * always sets lParam */
2106 tvItem->lParam = wineItem->lParam;
2107 if (tvItem->mask & TVIF_SELECTEDIMAGE) {
2108 tvItem->iSelectedImage = wineItem->iSelectedImage;
2109 }
2110 if (tvItem->mask & TVIF_STATE) {
2111 tvItem->state = wineItem->state & tvItem->stateMask;
2112 }
2113
2114 if (tvItem->mask & TVIF_TEXT) {
2115 if (wineItem->pszText == LPSTR_TEXTCALLBACKA) {
2116 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2117 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2118 }
2119 else if (wineItem->pszText) {
2120 TRACE("orig str %s at %p\n",
2121 debugstr_a(wineItem->pszText), wineItem->pszText);
2122 MultiByteToWideChar(CP_ACP, 0, wineItem->pszText,
2123 -1 , tvItem->pszText, tvItem->cchTextMax);
2124 }
2125 }
2126
2127 TRACE("item %d<%p>, txt %p<%s>, img %p, action %x\n",
2128 iItem, tvItem, tvItem->pszText, debugstr_w(tvItem->pszText),
2129 &tvItem->iImage, tvItem->mask);
2130 return TRUE;
2131}
2132
2133static LRESULT
2134TREEVIEW_SetItemW(TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem)
2135{
2136 TVITEMEXA tvItemA;
2137 INT len;
2138 LRESULT rc;
2139
2140 tvItemA.mask = tvItem->mask;
2141 tvItemA.hItem = tvItem->hItem;
2142 tvItemA.state = tvItem->state;
2143 tvItemA.stateMask = tvItem->stateMask;
2144 if (tvItem->mask & TVIF_TEXT) {
2145 len = WideCharToMultiByte(CP_ACP, 0, tvItem->pszText, -1,
2146 NULL ,0 , NULL,NULL);
2147 if (len) {
2148 len ++;
2149 tvItemA.pszText = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
2150 len = WideCharToMultiByte(CP_ACP, 0, tvItem->pszText, -1,
2151 tvItemA.pszText ,len*sizeof(WCHAR),
2152 NULL,NULL);
2153 }
2154 else
2155 tvItemA.pszText = NULL;
2156 }
2157 tvItemA.cchTextMax = tvItem->cchTextMax;
2158 tvItemA.iImage = tvItem->iImage;
2159 tvItemA.iSelectedImage = tvItem->iSelectedImage;
2160 tvItemA.cChildren = tvItem->cChildren;
2161 tvItemA.lParam = tvItem->lParam;
2162 tvItemA.iIntegral = tvItem->iIntegral;
2163
2164 rc = TREEVIEW_SetItemA(infoPtr,&tvItemA);
2165 HeapFree(GetProcessHeap(),0,tvItemA.pszText);
2166 return rc;
2167}
2168
2169static LRESULT
2170TREEVIEW_GetItemState(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2171{
2172 TRACE("\n");
2173
2174 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2175 return 0;
2176
2177 return (wineItem->state & mask);
2178}
2179
2180static LRESULT
2181TREEVIEW_GetNextItem(TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2182{
2183 TREEVIEW_ITEM *retval;
2184
2185 retval = 0;
2186
2187 /* handle all the global data here */
2188 switch (which)
2189 {
2190 case TVGN_CHILD: /* Special case: child of 0 is root */
2191 if (wineItem)
2192 break;
2193 /* fall through */
2194 case TVGN_ROOT:
2195 retval = infoPtr->root->firstChild;
2196 break;
2197
2198 case TVGN_CARET:
2199 retval = infoPtr->selectedItem;
2200 break;
2201
2202 case TVGN_FIRSTVISIBLE:
2203 retval = infoPtr->firstVisible;
2204 break;
2205
2206 case TVGN_DROPHILITE:
2207 retval = infoPtr->dropItem;
2208 break;
2209
2210 case TVGN_LASTVISIBLE:
2211 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2212 break;
2213 }
2214
2215 if (retval)
2216 {
2217 TRACE("flags:%x, returns %p\n", which, retval);
2218 return (LRESULT)retval;
2219 }
2220
2221 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2222
2223 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2224 return FALSE;
2225
2226 switch (which)
2227 {
2228 case TVGN_NEXT:
2229 retval = wineItem->nextSibling;
2230 break;
2231 case TVGN_PREVIOUS:
2232 retval = wineItem->prevSibling;
2233 break;
2234 case TVGN_PARENT:
2235 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2236 break;
2237 case TVGN_CHILD:
2238 retval = wineItem->firstChild;
2239 break;
2240 case TVGN_NEXTVISIBLE:
2241 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2242 break;
2243 case TVGN_PREVIOUSVISIBLE:
2244 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2245 break;
2246 default:
2247 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2248 break;
2249 }
2250
2251 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2252 return (LRESULT)retval;
2253}
2254
2255
2256static LRESULT
2257TREEVIEW_GetCount(TREEVIEW_INFO *infoPtr)
2258{
2259 TRACE(" %d\n", infoPtr->uNumItems);
2260 return (LRESULT)infoPtr->uNumItems;
2261}
2262
2263static VOID
2264TREEVIEW_ToggleItemState(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2265{
2266 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2267 {
2268 static const unsigned int state_table[] = { 0, 2, 1 };
2269
2270 unsigned int state;
2271
2272 state = STATEIMAGEINDEX(item->state);
2273 TRACE("state:%x\n", state);
2274 item->state &= ~TVIS_STATEIMAGEMASK;
2275
2276 if (state < 3)
2277 state = state_table[state];
2278
2279 item->state |= INDEXTOSTATEIMAGEMASK(state);
2280
2281 TRACE("state:%x\n", state);
2282 TREEVIEW_Invalidate(infoPtr, item);
2283 }
2284}
2285
2286
2287/* Painting *************************************************************/
2288
2289/* Draw the lines and expand button for an item. Also draws one section
2290 * of the line from item's parent to item's parent's next sibling. */
2291static void
2292TREEVIEW_DrawItemLines(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *item)
2293{
2294 LONG centerx, centery;
2295 BOOL lar = ((infoPtr->dwStyle
2296 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2297 > TVS_LINESATROOT);
2298
2299 if (!lar && item->iLevel == 0)
2300 return;
2301
2302 centerx = (item->linesOffset + item->stateOffset) / 2;
2303 centery = (item->rect.top + item->rect.bottom) / 2;
2304
2305 if (infoPtr->dwStyle & TVS_HASLINES)
2306 {
2307 HPEN hOldPen, hNewPen;
2308 HTREEITEM parent;
2309
2310 /*
2311 * Get a dotted grey pen
2312 */
2313 hNewPen = CreatePen(PS_ALTERNATE, 0, infoPtr->clrLine);
2314 hOldPen = SelectObject(hdc, hNewPen);
2315
2316 MoveToEx(hdc, item->stateOffset, centery, NULL);
2317 LineTo(hdc, centerx - 1, centery);
2318
2319 if (item->prevSibling || item->parent != infoPtr->root)
2320 {
2321 MoveToEx(hdc, centerx, item->rect.top, NULL);
2322 LineTo(hdc, centerx, centery);
2323 }
2324
2325 if (item->nextSibling)
2326 {
2327 MoveToEx(hdc, centerx, centery, NULL);
2328 LineTo(hdc, centerx, item->rect.bottom + 1);
2329 }
2330
2331 /* Draw the line from our parent to its next sibling. */
2332 parent = item->parent;
2333 while (parent != infoPtr->root)
2334 {
2335 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2336
2337 if (parent->nextSibling
2338 /* skip top-levels unless TVS_LINESATROOT */
2339 && parent->stateOffset > parent->linesOffset)
2340 {
2341 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2342 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2343 }
2344
2345 parent = parent->parent;
2346 }
2347
2348 SelectObject(hdc, hOldPen);
2349 DeleteObject(hNewPen);
2350 }
2351
2352 /*
2353 * Display the (+/-) signs
2354 */
2355
2356 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2357 {
2358 if (item->cChildren)
2359 {
2360 LONG height = item->rect.bottom - item->rect.top;
2361 LONG width = item->stateOffset - item->linesOffset;
2362 LONG rectsize = min(height, width) / 4;
2363 /* plussize = ceil(rectsize * 3/4) */
2364 LONG plussize = (rectsize + 1) * 3 / 4;
2365
2366 HPEN hNewPen = CreatePen(PS_SOLID, 0, infoPtr->clrLine);
2367 HPEN hOldPen = SelectObject(hdc, hNewPen);
2368 HBRUSH hbr = CreateSolidBrush(infoPtr->clrBk);
2369 HBRUSH hbrOld = SelectObject(hdc, hbr);
2370
2371 Rectangle(hdc, centerx - rectsize, centery - rectsize,
2372 centerx + rectsize + 1, centery + rectsize + 1);
2373
2374 SelectObject(hdc, hbrOld);
2375 DeleteObject(hbr);
2376
2377 SelectObject(hdc, hOldPen);
2378 DeleteObject(hNewPen);
2379
2380 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2381 LineTo(hdc, centerx + plussize, centery);
2382
2383 if (!(item->state & TVIS_EXPANDED))
2384 {
2385 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2386 LineTo(hdc, centerx, centery + plussize);
2387 }
2388 }
2389 }
2390}
2391
2392static void
2393TREEVIEW_DrawItem(TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2394{
2395 INT cditem;
2396 HFONT hOldFont;
2397 int centery;
2398
2399 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2400
2401 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2402
2403 /* The custom draw handler can query the text rectangle,
2404 * so get ready. */
2405 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2406
2407 cditem = 0;
2408
2409 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2410 {
2411 cditem = TREEVIEW_SendCustomDrawItemNotify
2412 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT);
2413 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2414
2415 if (cditem & CDRF_SKIPDEFAULT)
2416 {
2417 SelectObject(hdc, hOldFont);
2418 return;
2419 }
2420 }
2421
2422 if (cditem & CDRF_NEWFONT)
2423 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2424
2425 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2426
2427 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2428
2429 /*
2430 * Display the images associated with this item
2431 */
2432 {
2433 INT imageIndex;
2434
2435 /* State images are displayed to the left of the Normal image
2436 * image number is in state; zero should be `display no image'.
2437 */
2438 imageIndex = STATEIMAGEINDEX(wineItem->state);
2439
2440 if (infoPtr->himlState && imageIndex)
2441 {
2442 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2443 wineItem->stateOffset,
2444 centery - infoPtr->stateImageHeight / 2,
2445 ILD_NORMAL);
2446 }
2447
2448 /* Now, draw the normal image; can be either selected or
2449 * non-selected image.
2450 */
2451
2452 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage))
2453 {
2454 /* The item is currently selected */
2455 imageIndex = wineItem->iSelectedImage;
2456 }
2457 else
2458 {
2459 /* The item is not selected */
2460 imageIndex = wineItem->iImage;
2461 }
2462
2463 if (infoPtr->himlNormal)
2464 {
2465 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2466
2467 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2468 wineItem->imageOffset,
2469 centery - infoPtr->normalImageHeight / 2,
2470 ILD_NORMAL | ovlIdx);
2471 }
2472 }
2473
2474
2475 /*
2476 * Display the text associated with this item
2477 */
2478
2479 /* Don't paint item's text if it's being edited */
2480 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2481 {
2482 if (wineItem->pszText)
2483 {
2484 COLORREF oldTextColor = 0;
2485 INT oldBkMode;
2486 HBRUSH hbrBk = 0;
2487 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2488 RECT rcText;
2489
2490 oldBkMode = SetBkMode(hdc, TRANSPARENT);
2491
2492 /* - If item is drop target or it is selected and window is in focus -
2493 * use blue background (COLOR_HIGHLIGHT).
2494 * - If item is selected, window is not in focus, but it has style
2495 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2496 * - Otherwise - don't fill background
2497 */
2498 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2499 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2500 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2501 {
2502 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2503 {
2504 hbrBk = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
2505 oldTextColor =
2506 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2507 }
2508 else
2509 {
2510 hbrBk = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
2511
2512 if (infoPtr->clrText == -1)
2513 oldTextColor =
2514 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2515 else
2516 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2517 }
2518 }
2519 else
2520 {
2521 if (infoPtr->clrText == -1)
2522 oldTextColor =
2523 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
2524 else
2525 oldTextColor = SetTextColor(hdc, infoPtr->clrText);
2526 }
2527
2528 rcText.top = wineItem->rect.top;
2529 rcText.bottom = wineItem->rect.bottom;
2530 rcText.left = wineItem->textOffset;
2531 rcText.right = rcText.left + wineItem->textWidth + 4;
2532
2533 if (hbrBk)
2534 {
2535 FillRect(hdc, &rcText, hbrBk);
2536 DeleteObject(hbrBk);
2537 }
2538
2539 /* Draw the box around the selected item */
2540 if ((wineItem == infoPtr->selectedItem) && inFocus)
2541 {
2542 DrawFocusRect(hdc,&rcText);
2543 }
2544
2545 InflateRect(&rcText, -2, -1); /* allow for the focus rect */
2546
2547 TRACE("drawing text %s at (%d,%d)-(%d,%d)\n",
2548 debugstr_a(wineItem->pszText),
2549 rcText.left, rcText.top, rcText.right, rcText.bottom);
2550
2551 /* Draw it */
2552 DrawTextA(hdc,
2553 wineItem->pszText,
2554 lstrlenA(wineItem->pszText),
2555 &rcText,
2556 DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
2557
2558 /* Restore the hdc state */
2559 SetTextColor(hdc, oldTextColor);
2560
2561 if (oldBkMode != TRANSPARENT)
2562 SetBkMode(hdc, oldBkMode);
2563 }
2564 }
2565
2566 /* Draw insertion mark if necessary */
2567
2568 if (infoPtr->insertMarkItem)
2569 TRACE("item:%d,mark:%d\n",
2570 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2571 (int)infoPtr->insertMarkItem);
2572
2573 if (wineItem == infoPtr->insertMarkItem)
2574 {
2575 HPEN hNewPen, hOldPen;
2576 int offset;
2577 int left, right;
2578
2579 hNewPen = CreatePen(PS_SOLID, 2, infoPtr->clrInsertMark);
2580 hOldPen = SelectObject(hdc, hNewPen);
2581
2582 if (infoPtr->insertBeforeorAfter)
2583 offset = wineItem->rect.bottom - 1;
2584 else
2585 offset = wineItem->rect.top + 1;
2586
2587 left = wineItem->textOffset - 2;
2588 right = wineItem->textOffset + wineItem->textWidth + 2;
2589
2590 MoveToEx(hdc, left, offset - 3, NULL);
2591 LineTo(hdc, left, offset + 4);
2592
2593 MoveToEx(hdc, left, offset, NULL);
2594 LineTo(hdc, right + 1, offset);
2595
2596 MoveToEx(hdc, right, offset + 3, NULL);
2597 LineTo(hdc, right, offset - 4);
2598
2599 SelectObject(hdc, hOldPen);
2600 DeleteObject(hNewPen);
2601 }
2602
2603 if (cditem & CDRF_NOTIFYPOSTPAINT)
2604 {
2605 cditem = TREEVIEW_SendCustomDrawItemNotify
2606 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT);
2607 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2608 }
2609
2610 SelectObject(hdc, hOldFont);
2611}
2612
2613/* Computes treeHeight and treeWidth and updates the scroll bars.
2614 */
2615static void
2616TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2617{
2618 TREEVIEW_ITEM *wineItem;
2619 HWND hwnd = infoPtr->hwnd;
2620 BOOL vert = FALSE;
2621 BOOL horz = FALSE;
2622 SCROLLINFO si;
2623 LONG scrollX = infoPtr->scrollX;
2624
2625 infoPtr->treeWidth = 0;
2626 infoPtr->treeHeight = 0;
2627
2628 /* We iterate through all visible items in order to get the tree height
2629 * and width */
2630 wineItem = infoPtr->root->firstChild;
2631
2632 while (wineItem != NULL)
2633 {
2634 if (ISVISIBLE(wineItem))
2635 {
2636 /* actually we draw text at textOffset + 2 */
2637 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2638 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2639
2640 /* This is scroll-adjusted, but we fix this below. */
2641 infoPtr->treeHeight = wineItem->rect.bottom;
2642 }
2643
2644 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2645 }
2646
2647 /* Fix the scroll adjusted treeHeight and treeWidth. */
2648 if (infoPtr->root->firstChild)
2649 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2650
2651 infoPtr->treeWidth += infoPtr->scrollX;
2652
2653 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2654
2655 /* Adding one scroll bar may take up enough space that it forces us
2656 * to add the other as well. */
2657 if (infoPtr->treeHeight > infoPtr->clientHeight)
2658 {
2659 vert = TRUE;
2660
2661 if (infoPtr->treeWidth
2662 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2663 horz = TRUE;
2664 }
2665 else if (infoPtr->treeWidth > infoPtr->clientWidth)
2666 horz = TRUE;
2667
2668 if (!vert && horz && infoPtr->treeHeight
2669 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2670 vert = TRUE;
2671
2672 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2673
2674 si.cbSize = sizeof(SCROLLINFO);
2675 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2676 si.nMin = 0;
2677
2678 if (vert)
2679 {
2680 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2681 si.nPos = infoPtr->firstVisible->visibleOrder;
2682 si.nMax = infoPtr->maxVisibleOrder - 1;
2683
2684 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2685
2686 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2687 ShowScrollBar(hwnd, SB_VERT, TRUE);
2688 infoPtr->uInternalStatus |= TV_VSCROLL;
2689 }
2690 else
2691 {
2692 if (infoPtr->uInternalStatus & TV_VSCROLL)
2693 ShowScrollBar(hwnd, SB_VERT, FALSE);
2694 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2695 }
2696
2697 if (horz)
2698 {
2699 si.nPage = infoPtr->clientWidth;
2700 si.nPos = infoPtr->scrollX;
2701 si.nMax = infoPtr->treeWidth - 1;
2702
2703 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2704 {
2705 si.nPos = si.nMax - max( si.nPage-1, 0 );
2706 scrollX = si.nPos;
2707 }
2708
2709 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2710 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2711 infoPtr->uInternalStatus |= TV_HSCROLL;
2712
2713 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2714 }
2715 else
2716 {
2717 if (infoPtr->uInternalStatus & TV_HSCROLL)
2718 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2719 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2720
2721 scrollX = 0;
2722 }
2723
2724 if (infoPtr->scrollX != scrollX)
2725 {
2726 TREEVIEW_HScroll(infoPtr,
2727 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2728 }
2729
2730 if (!horz)
2731 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2732}
2733
2734/* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2735static LRESULT
2736TREEVIEW_EraseBackground(TREEVIEW_INFO *infoPtr, HDC hDC)
2737{
2738 HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
2739 RECT rect;
2740
2741 GetClientRect(infoPtr->hwnd, &rect);
2742 FillRect(hDC, &rect, hBrush);
2743 DeleteObject(hBrush);
2744
2745 return 1;
2746}
2747
2748static void
2749TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, RECT *rc)
2750{
2751 HWND hwnd = infoPtr->hwnd;
2752 RECT rect = *rc;
2753 TREEVIEW_ITEM *wineItem;
2754
2755 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2756 {
2757 TRACE("empty window\n");
2758 return;
2759 }
2760
2761 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2762 hdc, rect);
2763
2764 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2765 {
2766 ReleaseDC(hwnd, hdc);
2767 return;
2768 }
2769
2770 for (wineItem = infoPtr->root->firstChild;
2771 wineItem != NULL;
2772 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2773 {
2774 if (ISVISIBLE(wineItem))
2775 {
2776 /* Avoid unneeded calculations */
2777 if (wineItem->rect.top > rect.bottom)
2778 break;
2779 if (wineItem->rect.bottom < rect.top)
2780 continue;
2781
2782 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2783 }
2784 }
2785
2786 TREEVIEW_UpdateScrollBars(infoPtr);
2787
2788 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2789 infoPtr->cdmode =
2790 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2791}
2792
2793static void
2794TREEVIEW_Invalidate(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2795{
2796 if (item != NULL)
2797 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2798 else
2799 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2800}
2801
2802static LRESULT
2803TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, WPARAM wParam)
2804{
2805 HDC hdc;
2806 PAINTSTRUCT ps;
2807 RECT rc;
2808
2809 TRACE("\n");
2810
2811 if (wParam)
2812 {
2813 hdc = (HDC)wParam;
2814 if (!GetUpdateRect(infoPtr->hwnd, &rc, TRUE))
2815 {
2816 HBITMAP hbitmap;
2817 BITMAP bitmap;
2818 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2819 if (!hbitmap) return 0;
2820 GetObjectA(hbitmap, sizeof(BITMAP), &bitmap);
2821 rc.left = 0; rc.top = 0;
2822 rc.right = bitmap.bmWidth;
2823 rc.bottom = bitmap.bmHeight;
2824 TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
2825 }
2826 }
2827 else
2828 {
2829 hdc = BeginPaint(infoPtr->hwnd, &ps);
2830 rc = ps.rcPaint;
2831 }
2832
2833 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2834 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2835
2836 if (!wParam)
2837 EndPaint(infoPtr->hwnd, &ps);
2838
2839 return 0;
2840}
2841
2842
2843/* Sorting **************************************************************/
2844
2845/***************************************************************************
2846 * Forward the DPA local callback to the treeview owner callback
2847 */
2848static INT WINAPI
2849TREEVIEW_CallBackCompare(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second, LPTVSORTCB pCallBackSort)
2850{
2851 /* Forward the call to the client-defined callback */
2852 return pCallBackSort->lpfnCompare(first->lParam,
2853 second->lParam,
2854 pCallBackSort->lParam);
2855}
2856
2857/***************************************************************************
2858 * Treeview native sort routine: sort on item text.
2859 */
2860static INT WINAPI
2861TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2862 TREEVIEW_INFO *infoPtr)
2863{
2864 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2865 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2866
2867 return strcasecmp(first->pszText, second->pszText);
2868}
2869
2870/* Returns the number of physical children belonging to item. */
2871static INT
2872TREEVIEW_CountChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2873{
2874 INT cChildren = 0;
2875 HTREEITEM hti;
2876
2877 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2878 cChildren++;
2879
2880 return cChildren;
2881}
2882
2883/* Returns a DPA containing a pointer to each physical child of item in
2884 * sibling order. If item has no children, an empty DPA is returned. */
2885static HDPA
2886TREEVIEW_BuildChildDPA(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2887{
2888 HTREEITEM child = item->firstChild;
2889
2890 HDPA list = DPA_Create(8);
2891 if (list == 0) return NULL;
2892
2893 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2894 {
2895 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2896 {
2897 DPA_Destroy(list);
2898 return NULL;
2899 }
2900 }
2901
2902 return list;
2903}
2904
2905/***************************************************************************
2906 * Setup the treeview structure with regards of the sort method
2907 * and sort the children of the TV item specified in lParam
2908 * fRecurse: currently unused. Should be zero.
2909 * parent: if pSort!=NULL, should equal pSort->hParent.
2910 * otherwise, item which child items are to be sorted.
2911 * pSort: sort method info. if NULL, sort on item text.
2912 * if non-NULL, sort on item's lParam content, and let the
2913 * application decide what that means. See also TVM_SORTCHILDRENCB.
2914 */
2915
2916static LRESULT
2917TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, BOOL fRecurse, HTREEITEM parent,
2918 LPTVSORTCB pSort)
2919{
2920 INT cChildren;
2921 PFNDPACOMPARE pfnCompare;
2922 LPARAM lpCompare;
2923
2924 /* undocumented feature: TVI_ROOT means `sort the whole tree' */
2925 if (parent == TVI_ROOT)
2926 parent = infoPtr->root;
2927
2928 /* Check for a valid handle to the parent item */
2929 if (!TREEVIEW_ValidItem(infoPtr, parent))
2930 {
2931 ERR("invalid item hParent=%x\n", (INT)parent);
2932 return FALSE;
2933 }
2934
2935 if (pSort)
2936 {
2937 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
2938 lpCompare = (LPARAM)pSort;
2939 }
2940 else
2941 {
2942 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
2943 lpCompare = (LPARAM)infoPtr;
2944 }
2945
2946 cChildren = TREEVIEW_CountChildren(infoPtr, parent);
2947
2948 /* Make sure there is something to sort */
2949 if (cChildren > 1)
2950 {
2951 /* TREEVIEW_ITEM rechaining */
2952 INT count = 0;
2953 HTREEITEM item = 0;
2954 HTREEITEM nextItem = 0;
2955 HTREEITEM prevItem = 0;
2956
2957 HDPA sortList = TREEVIEW_BuildChildDPA(infoPtr, parent);
2958
2959 if (sortList == NULL)
2960 return FALSE;
2961
2962 /* let DPA sort the list */
2963 DPA_Sort(sortList, pfnCompare, lpCompare);
2964
2965 /* The order of DPA entries has been changed, so fixup the
2966 * nextSibling and prevSibling pointers. */
2967
2968 item = (HTREEITEM)DPA_GetPtr(sortList, count++);
2969 while ((nextItem = (HTREEITEM)DPA_GetPtr(sortList, count++)) != NULL)
2970 {
2971 /* link the two current item toghether */
2972 item->nextSibling = nextItem;
2973 nextItem->prevSibling = item;
2974
2975 if (prevItem == NULL)
2976 {
2977 /* this is the first item, update the parent */
2978 parent->firstChild = item;
2979 item->prevSibling = NULL;
2980 }
2981 else
2982 {
2983 /* fix the back chaining */
2984 item->prevSibling = prevItem;
2985 }
2986
2987 /* get ready for the next one */
2988 prevItem = item;
2989 item = nextItem;
2990 }
2991
2992 /* the last item is pointed to by item and never has a sibling */
2993 item->nextSibling = NULL;
2994 parent->lastChild = item;
2995
2996 DPA_Destroy(sortList);
2997
2998 TREEVIEW_VerifyTree(infoPtr);
2999
3000 if (parent->state & TVIS_EXPANDED)
3001 {
3002 int visOrder = infoPtr->firstVisible->visibleOrder;
3003
3004 if (parent == infoPtr->root)
3005 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3006 else
3007 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3008
3009 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3010 {
3011 TREEVIEW_ITEM *item;
3012
3013 for (item = infoPtr->root->firstChild; item != NULL;
3014 item = TREEVIEW_GetNextListItem(infoPtr, item))
3015 {
3016 if (item->visibleOrder == visOrder)
3017 break;
3018 }
3019
3020 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3021 }
3022
3023 TREEVIEW_Invalidate(infoPtr, NULL);
3024 }
3025
3026 return TRUE;
3027 }
3028 return FALSE;
3029}
3030
3031
3032/***************************************************************************
3033 * Setup the treeview structure with regards of the sort method
3034 * and sort the children of the TV item specified in lParam
3035 */
3036static LRESULT
3037TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPTVSORTCB pSort)
3038{
3039 return TREEVIEW_Sort(infoPtr, wParam, pSort->hParent, pSort);
3040}
3041
3042
3043/***************************************************************************
3044 * Sort the children of the TV item specified in lParam.
3045 */
3046static LRESULT
3047TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3048{
3049 return TREEVIEW_Sort(infoPtr, (BOOL)wParam, (HTREEITEM)lParam, NULL);
3050}
3051
3052
3053/* Expansion/Collapse ***************************************************/
3054
3055static BOOL
3056TREEVIEW_SendExpanding(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3057 UINT action)
3058{
3059 return !TREEVIEW_SendTreeviewNotify(infoPtr,
3060 (infoPtr->bNtfUnicode) ? TVN_ITEMEXPANDINGW :
3061 TVN_ITEMEXPANDINGA,
3062 action,
3063 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3064 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3065 0, wineItem);
3066}
3067
3068static VOID
3069TREEVIEW_SendExpanded(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3070 UINT action)
3071{
3072 TREEVIEW_SendTreeviewNotify(infoPtr,
3073 (infoPtr->bNtfUnicode) ? TVN_ITEMEXPANDEDW :
3074 TVN_ITEMEXPANDEDA,
3075 action,
3076 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3077 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3078 0, wineItem);
3079}
3080
3081
3082/* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3083 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3084static BOOL
3085TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3086 BOOL bRemoveChildren, BOOL bUser)
3087{
3088 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3089 BOOL bSetSelection, bSetFirstVisible;
3090
3091 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3092
3093 if (!(wineItem->state & TVIS_EXPANDED) || wineItem->firstChild == NULL)
3094 return FALSE;
3095
3096 if (bUser)
3097 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3098
3099 wineItem->state &= ~TVIS_EXPANDED;
3100
3101 if (bUser)
3102 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3103
3104 bSetSelection = (infoPtr->selectedItem != NULL
3105 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3106
3107 bSetFirstVisible = (infoPtr->firstVisible != NULL
3108 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3109
3110 if (bRemoveChildren)
3111 {
3112 TRACE("TVE_COLLAPSERESET\n");
3113 wineItem->state &= ~TVIS_EXPANDEDONCE;
3114 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3115 }
3116
3117 if (wineItem->firstChild)
3118 {
3119 TREEVIEW_ITEM *item, *sibling;
3120
3121 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3122
3123 for (item = wineItem->firstChild; item != sibling;
3124 item = TREEVIEW_GetNextListItem(infoPtr, item))
3125 {
3126 item->visibleOrder = -1;
3127 }
3128 }
3129
3130 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3131
3132 TREEVIEW_SetFirstVisible(infoPtr, bSetFirstVisible ? wineItem
3133 : infoPtr->firstVisible, TRUE);
3134
3135 if (bSetSelection)
3136 {
3137 /* Don't call DoSelectItem, it sends notifications. */
3138 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3139 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3140 wineItem->state |= TVIS_SELECTED;
3141 infoPtr->selectedItem = wineItem;
3142
3143 TREEVIEW_EnsureVisible(infoPtr, wineItem, FALSE);
3144 }
3145
3146 TREEVIEW_UpdateScrollBars(infoPtr);
3147 TREEVIEW_Invalidate(infoPtr, NULL);
3148
3149 return TRUE;
3150}
3151
3152static BOOL
3153TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3154 BOOL bExpandPartial, BOOL bUser)
3155{
3156 TRACE("\n");
3157
3158 if (!TREEVIEW_HasChildren(infoPtr, wineItem)
3159 || wineItem->state & TVIS_EXPANDED)
3160 return FALSE;
3161
3162 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3163
3164 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3165 {
3166 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3167 {
3168 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3169 return FALSE;
3170 }
3171
3172 wineItem->state |= TVIS_EXPANDED;
3173 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3174 wineItem->state |= TVIS_EXPANDEDONCE;
3175 }
3176 else
3177 {
3178 /* this item has already been expanded */
3179 wineItem->state |= TVIS_EXPANDED;
3180 }
3181
3182 if (bExpandPartial)
3183 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3184
3185 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3186 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3187 TREEVIEW_UpdateScrollBars(infoPtr);
3188
3189 /* Scroll up so that as many children as possible are visible.
3190 * This looses when expanding causes an HScroll bar to appear, but we
3191 * don't know that yet, so the last item is obscured. */
3192 if (wineItem->firstChild != NULL)
3193 {
3194 int nChildren = wineItem->lastChild->visibleOrder
3195 - wineItem->firstChild->visibleOrder + 1;
3196
3197 int visible_pos = wineItem->visibleOrder
3198 - infoPtr->firstVisible->visibleOrder;
3199
3200 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3201
3202 if (visible_pos > 0 && nChildren > rows_below)
3203 {
3204 int scroll = nChildren - rows_below;
3205
3206 if (scroll > visible_pos)
3207 scroll = visible_pos;
3208
3209 if (scroll > 0)
3210 {
3211 TREEVIEW_ITEM *newFirstVisible
3212 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3213 scroll);
3214
3215
3216 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3217 }
3218 }
3219 }
3220
3221 TREEVIEW_Invalidate(infoPtr, NULL);
3222
3223 return TRUE;
3224}
3225
3226static BOOL
3227TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3228{
3229 TRACE("\n");
3230
3231 if (wineItem->state & TVIS_EXPANDED)
3232 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3233 else
3234 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3235}
3236
3237static VOID
3238TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3239{
3240 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3241
3242 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3243 {
3244 if (TREEVIEW_HasChildren(infoPtr, item))
3245 TREEVIEW_ExpandAll(infoPtr, item);
3246 }
3247}
3248
3249/* Note:If the specified item is the child of a collapsed parent item,
3250 the parent's list of child items is (recursively) expanded to reveal the
3251 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3252 know if it also applies here.
3253*/
3254
3255static LRESULT
3256TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3257{
3258 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3259 return 0;
3260
3261 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3262 TREEVIEW_ItemName(wineItem), flag,
3263 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3264
3265 switch (flag & TVE_TOGGLE)
3266 {
3267 case TVE_COLLAPSE:
3268 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3269 FALSE);
3270
3271 case TVE_EXPAND:
3272 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3273 FALSE);
3274
3275 case TVE_TOGGLE:
3276 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3277
3278 default:
3279 return 0;
3280 }
3281
3282#if 0
3283 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3284#endif
3285}
3286
3287/* Hit-Testing **********************************************************/
3288
3289static TREEVIEW_ITEM *
3290TREEVIEW_HitTestPoint(TREEVIEW_INFO *infoPtr, POINT pt)
3291{
3292 TREEVIEW_ITEM *wineItem;
3293 LONG row;
3294
3295 if (!infoPtr->firstVisible)
3296 return NULL;
3297
3298 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3299
3300 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3301 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3302 {
3303 if (row >= wineItem->visibleOrder
3304 && row < wineItem->visibleOrder + wineItem->iIntegral)
3305 break;
3306 }
3307
3308 return wineItem;
3309}
3310
3311static LRESULT
3312TREEVIEW_HitTest(TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3313{
3314 TREEVIEW_ITEM *wineItem;
3315 RECT rect;
3316 UINT status;
3317 LONG x, y;
3318
3319 lpht->hItem = 0;
3320 GetClientRect(infoPtr->hwnd, &rect);
3321 status = 0;
3322 x = lpht->pt.x;
3323 y = lpht->pt.y;
3324
3325 if (x < rect.left)
3326 {
3327 status |= TVHT_TOLEFT;
3328 }
3329 else if (x > rect.right)
3330 {
3331 status |= TVHT_TORIGHT;
3332 }
3333
3334 if (y < rect.top)
3335 {
3336 status |= TVHT_ABOVE;
3337 }
3338 else if (y > rect.bottom)
3339 {
3340 status |= TVHT_BELOW;
3341 }
3342
3343 if (status)
3344 {
3345 lpht->flags = status;
3346 return (LRESULT)(HTREEITEM)NULL;
3347 }
3348
3349 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3350 if (!wineItem)
3351 {
3352 lpht->flags = TVHT_NOWHERE;
3353 return (LRESULT)(HTREEITEM)NULL;
3354 }
3355
3356 if (x >= wineItem->textOffset + wineItem->textWidth)
3357 {
3358 lpht->flags = TVHT_ONITEMRIGHT;
3359 }
3360 else if (x >= wineItem->textOffset)
3361 {
3362 lpht->flags = TVHT_ONITEMLABEL;
3363 }
3364 else if (x >= wineItem->imageOffset)
3365 {
3366 lpht->flags = TVHT_ONITEMICON;
3367 }
3368 else if (x >= wineItem->stateOffset)
3369 {
3370 lpht->flags = TVHT_ONITEMSTATEICON;
3371 }
3372 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3373 {
3374 lpht->flags = TVHT_ONITEMBUTTON;
3375 }
3376 else
3377 {
3378 lpht->flags = TVHT_ONITEMINDENT;
3379 }
3380
3381 lpht->hItem = wineItem;
3382 TRACE("(%ld,%ld):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3383
3384 return (LRESULT)wineItem;
3385}
3386
3387/* Item Label Editing ***************************************************/
3388
3389static LRESULT
3390TREEVIEW_GetEditControl(TREEVIEW_INFO *infoPtr)
3391{
3392 return (LRESULT)infoPtr->hwndEdit;
3393}
3394
3395static LRESULT CALLBACK
3396TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3397{
3398 TREEVIEW_INFO *infoPtr;
3399 BOOL bCancel = FALSE;
3400
3401 switch (uMsg)
3402 {
3403 case WM_PAINT:
3404 {
3405 LRESULT rc;
3406 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3407
3408 TRACE("WM_PAINT start\n");
3409 rc = CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3410 lParam);
3411 TRACE("WM_PAINT done\n");
3412 return rc;
3413 }
3414
3415 case WM_KILLFOCUS:
3416 {
3417 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3418 if (infoPtr->bIgnoreEditKillFocus)
3419 return TRUE;
3420
3421 break;
3422 }
3423
3424 case WM_GETDLGCODE:
3425 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3426
3427 case WM_KEYDOWN:
3428 if (wParam == (WPARAM)VK_ESCAPE)
3429 {
3430 bCancel = TRUE;
3431 break;
3432 }
3433 else if (wParam == (WPARAM)VK_RETURN)
3434 {
3435 break;
3436 }
3437
3438 /* fall through */
3439 default:
3440 {
3441 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3442
3443 return CallWindowProcA(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3444 lParam);
3445 }
3446 }
3447
3448 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3449 /* eg. Using a messagebox */
3450
3451 infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3452 infoPtr->bIgnoreEditKillFocus = TRUE;
3453 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3454 infoPtr->bIgnoreEditKillFocus = FALSE;
3455
3456 return 0;
3457}
3458
3459
3460/* should handle edit control messages here */
3461
3462static LRESULT
3463TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3464{
3465 TRACE("%x %ld\n", wParam, lParam);
3466
3467 switch (HIWORD(wParam))
3468 {
3469 case EN_UPDATE:
3470 {
3471 /*
3472 * Adjust the edit window size
3473 */
3474 char buffer[1024];
3475 TREEVIEW_ITEM *editItem = infoPtr->selectedItem;
3476 HDC hdc = GetDC(infoPtr->hwndEdit);
3477 SIZE sz;
3478 int len;
3479 HFONT hFont, hOldFont = 0;
3480
3481 infoPtr->bLabelChanged = TRUE;
3482
3483 len = GetWindowTextA(infoPtr->hwndEdit, buffer, sizeof(buffer));
3484
3485 /* Select font to get the right dimension of the string */
3486 hFont = (HFONT)SendMessageA(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3487 if (hFont != 0)
3488 {
3489 hOldFont = SelectObject(hdc, hFont);
3490 }
3491
3492 if (GetTextExtentPoint32A(hdc, buffer, strlen(buffer), &sz))
3493 {
3494 TEXTMETRICA textMetric;
3495
3496 /* Add Extra spacing for the next character */
3497 GetTextMetricsA(hdc, &textMetric);
3498 sz.cx += (textMetric.tmMaxCharWidth * 2);
3499
3500 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3501 sz.cx = min(sz.cx,
3502 infoPtr->clientWidth - editItem->textOffset + 2);
3503
3504 SetWindowPos(infoPtr->hwndEdit,
3505 HWND_TOP,
3506 0,
3507 0,
3508 sz.cx,
3509 editItem->rect.bottom - editItem->rect.top + 3,
3510 SWP_NOMOVE | SWP_DRAWFRAME);
3511 }
3512
3513 if (hFont != 0)
3514 {
3515 SelectObject(hdc, hOldFont);
3516 }
3517
3518 ReleaseDC(infoPtr->hwnd, hdc);
3519 break;
3520 }
3521
3522 default:
3523 return SendMessageA(GetParent(infoPtr->hwnd), WM_COMMAND, wParam, lParam);
3524 }
3525
3526 return 0;
3527}
3528
3529static HWND
3530TREEVIEW_EditLabelA(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3531{
3532 HWND hwnd = infoPtr->hwnd;
3533 HWND hwndEdit;
3534 SIZE sz;
3535 TREEVIEW_ITEM *editItem = hItem;
3536 HINSTANCE hinst = (HINSTANCE)GetWindowLongA(hwnd, GWL_HINSTANCE);
3537 HDC hdc;
3538 HFONT hOldFont=0;
3539 TEXTMETRICA textMetric;
3540
3541 TRACE("%x %p\n", (unsigned)hwnd, hItem);
3542 if (!TREEVIEW_ValidItem(infoPtr, editItem))
3543 return (HWND)NULL;
3544
3545 if (infoPtr->hwndEdit)
3546 return infoPtr->hwndEdit;
3547
3548 infoPtr->bLabelChanged = FALSE;
3549
3550 /* Make sure that edit item is selected */
3551 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, hItem, TVC_UNKNOWN);
3552 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3553
3554 TREEVIEW_UpdateDispInfo(infoPtr, editItem, TVIF_TEXT);
3555
3556 hdc = GetDC(hwnd);
3557 /* Select the font to get appropriate metric dimensions */
3558 if (infoPtr->hFont != 0)
3559 {
3560 hOldFont = SelectObject(hdc, infoPtr->hFont);
3561 }
3562
3563 /* Get string length in pixels */
3564 GetTextExtentPoint32A(hdc, editItem->pszText, strlen(editItem->pszText),
3565 &sz);
3566
3567 /* Add Extra spacing for the next character */
3568 GetTextMetricsA(hdc, &textMetric);
3569 sz.cx += (textMetric.tmMaxCharWidth * 2);
3570
3571 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3572 sz.cx = min(sz.cx, infoPtr->clientWidth - editItem->textOffset + 2);
3573
3574 if (infoPtr->hFont != 0)
3575 {
3576 SelectObject(hdc, hOldFont);
3577 }
3578
3579 ReleaseDC(hwnd, hdc);
3580 hwndEdit = CreateWindowExA(WS_EX_LEFT,
3581 "EDIT",
3582 0,
3583 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3584 WS_CLIPSIBLINGS | ES_WANTRETURN |
3585 ES_LEFT, editItem->textOffset - 2,
3586 editItem->rect.top - 1, sz.cx + 3,
3587 editItem->rect.bottom -
3588 editItem->rect.top + 3, hwnd, 0, hinst, 0);
3589/* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3590
3591 infoPtr->hwndEdit = hwndEdit;
3592
3593 /* Get a 2D border. */
3594 SetWindowLongA(hwndEdit, GWL_EXSTYLE,
3595 GetWindowLongA(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3596 SetWindowLongA(hwndEdit, GWL_STYLE,
3597 GetWindowLongA(hwndEdit, GWL_STYLE) | WS_BORDER);
3598
3599 SendMessageA(hwndEdit, WM_SETFONT,
3600 (WPARAM)TREEVIEW_FontForItem(infoPtr, editItem), FALSE);
3601
3602 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongA(hwndEdit, GWL_WNDPROC,
3603 (DWORD)
3604 TREEVIEW_Edit_SubclassProc);
3605
3606 if (TREEVIEW_BeginLabelEditNotify(infoPtr, editItem))
3607 {
3608 DestroyWindow(hwndEdit);
3609 infoPtr->hwndEdit = 0;
3610 return (HWND)NULL;
3611 }
3612
3613 infoPtr->selectedItem = hItem;
3614 SetWindowTextA(hwndEdit, editItem->pszText);
3615 SetFocus(hwndEdit);
3616 SendMessageA(hwndEdit, EM_SETSEL, 0, -1);
3617 ShowWindow(hwndEdit, SW_SHOW);
3618
3619 return hwndEdit;
3620}
3621
3622
3623static LRESULT
3624TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3625{
3626 HWND hwnd = infoPtr->hwnd;
3627 TREEVIEW_ITEM *editedItem = infoPtr->selectedItem;
3628 NMTVDISPINFOA tvdi;
3629 BOOL bCommit;
3630 char tmpText[1024] = { '\0' };
3631 int iLength = 0;
3632
3633 if (!infoPtr->hwndEdit)
3634 return FALSE;
3635
3636 tvdi.hdr.hwndFrom = hwnd;
3637 tvdi.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
3638 tvdi.hdr.code = TVN_ENDLABELEDITA;
3639 tvdi.item.mask = 0;
3640 tvdi.item.hItem = editedItem;
3641 tvdi.item.state = editedItem->state;
3642 tvdi.item.lParam = editedItem->lParam;
3643
3644 if (!bCancel)
3645 {
3646 iLength = GetWindowTextA(infoPtr->hwndEdit, tmpText, 1023);
3647
3648 if (iLength >= 1023)
3649 {
3650 ERR("Insuficient space to retrieve new item label\n");
3651 }
3652
3653 tvdi.item.pszText = tmpText;
3654 tvdi.item.cchTextMax = iLength + 1;
3655 }
3656 else
3657 {
3658 tvdi.item.pszText = NULL;
3659 tvdi.item.cchTextMax = 0;
3660 }
3661
3662 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr,
3663 (WPARAM)tvdi.hdr.idFrom, (LPARAM)&tvdi);
3664
3665 if (!bCancel && bCommit) /* Apply the changes */
3666 {
3667 if (strcmp(tmpText, editedItem->pszText) != 0)
3668 {
3669 if (NULL == COMCTL32_ReAlloc(editedItem->pszText, iLength + 1))
3670 {
3671 ERR("OutOfMemory, cannot allocate space for label\n");
3672 DestroyWindow(infoPtr->hwndEdit);
3673 infoPtr->hwndEdit = 0;
3674 return FALSE;
3675 }
3676 else
3677 {
3678 editedItem->cchTextMax = iLength + 1;
3679 lstrcpyA(editedItem->pszText, tmpText);
3680 }
3681 }
3682 }
3683
3684 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3685 DestroyWindow(infoPtr->hwndEdit);
3686 infoPtr->hwndEdit = 0;
3687 return TRUE;
3688}
3689
3690static LRESULT
3691TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3692{
3693 if (wParam != TV_EDIT_TIMER)
3694 {
3695 ERR("got unknown timer\n");
3696 return 1;
3697 }
3698
3699 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3700 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3701
3702 TREEVIEW_EditLabelA(infoPtr, infoPtr->selectedItem);
3703
3704 return 0;
3705}
3706
3707
3708/* Mouse Tracking/Drag **************************************************/
3709
3710/***************************************************************************
3711 * This is quite unusual piece of code, but that's how it's implemented in
3712 * Windows.
3713 */
3714static LRESULT
3715TREEVIEW_TrackMouse(TREEVIEW_INFO *infoPtr, POINT pt)
3716{
3717 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3718 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3719 RECT r;
3720 MSG msg;
3721
3722 r.top = pt.y - cyDrag;
3723 r.left = pt.x - cxDrag;
3724 r.bottom = pt.y + cyDrag;
3725 r.right = pt.x + cxDrag;
3726
3727 SetCapture(infoPtr->hwnd);
3728
3729 while (1)
3730 {
3731 if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3732 {
3733 if (msg.message == WM_MOUSEMOVE)
3734 {
3735 pt.x = SLOWORD(msg.lParam);
3736 pt.y = SHIWORD(msg.lParam);
3737 if (PtInRect(&r, pt))
3738 continue;
3739 else
3740 {
3741 ReleaseCapture();
3742 return 1;
3743 }
3744 }
3745 else if (msg.message >= WM_LBUTTONDOWN &&
3746 msg.message <= WM_RBUTTONDBLCLK)
3747 {
3748 if (msg.message == WM_RBUTTONUP)
3749 TREEVIEW_RButtonUp(infoPtr, &pt);
3750 break;
3751 }
3752
3753 DispatchMessageA(&msg);
3754 }
3755
3756 if (GetCapture() != infoPtr->hwnd)
3757 return 0;
3758 }
3759
3760 ReleaseCapture();
3761 return 0;
3762}
3763
3764
3765static LRESULT
3766TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3767{
3768 TREEVIEW_ITEM *wineItem;
3769 TVHITTESTINFO hit;
3770
3771 TRACE("\n");
3772 SetFocus(infoPtr->hwnd);
3773
3774 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3775 {
3776 /* If there is pending 'edit label' event - kill it now */
3777 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3778 }
3779
3780 hit.pt.x = SLOWORD(lParam);
3781 hit.pt.y = SHIWORD(lParam);
3782
3783 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3784 if (!wineItem)
3785 return 0;
3786 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3787
3788 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3789 { /* FIXME! */
3790 switch (hit.flags)
3791 {
3792 case TVHT_ONITEMRIGHT:
3793 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3794 break;
3795
3796 case TVHT_ONITEMINDENT:
3797 if (!(infoPtr->dwStyle & TVS_HASLINES))
3798 {
3799 break;
3800 }
3801 else
3802 {
3803 int level = hit.pt.x / infoPtr->uIndent;
3804 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3805
3806 while (wineItem->iLevel > level)
3807 {
3808 wineItem = wineItem->parent;
3809 }
3810
3811 /* fall through */
3812 }
3813
3814 case TVHT_ONITEMLABEL:
3815 case TVHT_ONITEMICON:
3816 case TVHT_ONITEMBUTTON:
3817 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3818 break;
3819
3820 case TVHT_ONITEMSTATEICON:
3821 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3822 TREEVIEW_ToggleItemState(infoPtr, wineItem);
3823 else
3824 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3825 break;
3826 }
3827 }
3828 return TRUE;
3829}
3830
3831
3832static LRESULT
3833TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3834{
3835 HWND hwnd = infoPtr->hwnd;
3836 TVHITTESTINFO ht;
3837 BOOL bTrack;
3838 HTREEITEM tempItem;
3839
3840 /* If Edit control is active - kill it and return.
3841 * The best way to do it is to set focus to itself.
3842 * Edit control subclassed procedure will automatically call
3843 * EndEditLabelNow.
3844 */
3845 if (infoPtr->hwndEdit)
3846 {
3847 SetFocus(hwnd);
3848 return 0;
3849 }
3850
3851 ht.pt.x = SLOWORD(lParam);
3852 ht.pt.y = SHIWORD(lParam);
3853
3854 TREEVIEW_HitTest(infoPtr, &ht);
3855 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
3856
3857 /* update focusedItem and redraw both items */
3858 if(ht.hItem && (ht.flags & TVHT_ONITEM))
3859 {
3860 infoPtr->focusedItem = ht.hItem;
3861 InvalidateRect(hwnd, &(((HTREEITEM)(ht.hItem))->rect), TRUE);
3862
3863 if(infoPtr->selectedItem)
3864 InvalidateRect(hwnd, &(infoPtr->selectedItem->rect), TRUE);
3865 }
3866
3867 bTrack = (ht.flags & TVHT_ONITEM)
3868 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
3869
3870 /* Send NM_CLICK right away */
3871 if (!bTrack)
3872 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3873 goto setfocus;
3874
3875 if (ht.flags & TVHT_ONITEMBUTTON)
3876 {
3877 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
3878 goto setfocus;
3879 }
3880 else if (bTrack)
3881 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
3882 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
3883 {
3884 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGA, ht.hItem,
3885 ht.pt);
3886 infoPtr->dropItem = ht.hItem;
3887
3888 /* clean up focusedItem as we dragged and won't select this item */
3889 if(infoPtr->focusedItem)
3890 {
3891 /* refresh the item that was focused */
3892 tempItem = infoPtr->focusedItem;
3893 infoPtr->focusedItem = 0;
3894 InvalidateRect(infoPtr->hwnd, &tempItem->rect, TRUE);
3895
3896 /* refresh the selected item to return the filled background */
3897 InvalidateRect(infoPtr->hwnd, &(infoPtr->selectedItem->rect), TRUE);
3898 }
3899
3900 return 0;
3901 }
3902 }
3903
3904 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
3905 goto setfocus;
3906
3907 /*
3908 * If the style allows editing and the node is already selected
3909 * and the click occurred on the item label...
3910 */
3911 if ((infoPtr->dwStyle & TVS_EDITLABELS) &&
3912 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem))
3913 {
3914 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3915 KillTimer(hwnd, TV_EDIT_TIMER);
3916
3917 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
3918 infoPtr->Timer |= TV_EDIT_TIMER_SET;
3919 }
3920 else if (ht.flags & TVHT_ONITEM) /* select the item if the hit was inside of the icon or text */
3921 {
3922 /*
3923 * if we are TVS_SINGLEEXPAND then we want this single click to
3924 * do a bunch of things.
3925 */
3926 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
3927 (infoPtr->hwndEdit == 0))
3928 {
3929 TREEVIEW_ITEM *SelItem;
3930
3931 /*
3932 * Send the notification
3933 */
3934 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVIF_HANDLE | TVIF_PARAM,
3935 0, ht.hItem, 0);
3936
3937 /*
3938 * Close the previous selection all the way to the root
3939 * as long as the new selection is not a child
3940 */
3941 if((infoPtr->selectedItem)
3942 && (infoPtr->selectedItem != ht.hItem))
3943 {
3944 BOOL closeit = TRUE;
3945 SelItem = ht.hItem;
3946
3947 /* determine if the hitItem is a child of the currently selected item */
3948 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3949 {
3950 closeit = (SelItem != infoPtr->selectedItem);
3951 SelItem = SelItem->parent;
3952 }
3953
3954 if(closeit)
3955 {
3956 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3957 SelItem = infoPtr->selectedItem;
3958
3959 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
3960 {
3961 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3962 SelItem = SelItem->parent;
3963 }
3964 }
3965 }
3966
3967 /*
3968 * Expand the current item
3969 */
3970 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
3971 }
3972
3973 /* Select the current item */
3974 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
3975 }
3976 else if (ht.flags & TVHT_ONITEMSTATEICON)
3977 {
3978 /* TVS_CHECKBOXES requires us to toggle the current state */
3979 if (infoPtr->dwStyle & TVS_CHECKBOXES)
3980 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
3981 }
3982
3983 setfocus:
3984 SetFocus(hwnd);
3985 return 0;
3986}
3987
3988
3989static LRESULT
3990TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3991{
3992 TVHITTESTINFO ht;
3993
3994 if (infoPtr->hwndEdit)
3995 {
3996 SetFocus(infoPtr->hwnd);
3997 return 0;
3998 }
3999
4000 ht.pt.x = SLOWORD(lParam);
4001 ht.pt.y = SHIWORD(lParam);
4002
4003 TREEVIEW_HitTest(infoPtr, &ht);
4004
4005 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4006 {
4007 if (ht.hItem)
4008 {
4009 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGA, ht.hItem,
4010 ht.pt);
4011 infoPtr->dropItem = ht.hItem;
4012 }
4013 }
4014 else
4015 {
4016 SetFocus(infoPtr->hwnd);
4017 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4018 }
4019
4020 return 0;
4021}
4022
4023static LRESULT
4024TREEVIEW_RButtonUp(TREEVIEW_INFO *infoPtr, LPPOINT pPt)
4025{
4026 return 0;
4027}
4028
4029
4030static LRESULT
4031TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4032{
4033 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4034 INT cx, cy;
4035 HDC hdc, htopdc;
4036 HWND hwtop;
4037 HBITMAP hbmp, hOldbmp;
4038 SIZE size;
4039 RECT rc;
4040 HFONT hOldFont;
4041
4042 TRACE("\n");
4043
4044 if (!(infoPtr->himlNormal))
4045 return 0;
4046
4047 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4048 return 0;
4049
4050 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4051
4052 hwtop = GetDesktopWindow();
4053 htopdc = GetDC(hwtop);
4054 hdc = CreateCompatibleDC(htopdc);
4055
4056 hOldFont = SelectObject(hdc, infoPtr->hFont);
4057 GetTextExtentPoint32A(hdc, dragItem->pszText, lstrlenA(dragItem->pszText),
4058 &size);
4059 TRACE("%ld %ld %s %d\n", size.cx, size.cy, dragItem->pszText,
4060 lstrlenA(dragItem->pszText));
4061 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4062 hOldbmp = SelectObject(hdc, hbmp);
4063
4064 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4065 size.cx += cx;
4066 if (cy > size.cy)
4067 size.cy = cy;
4068
4069 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4070 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4071 ILD_NORMAL);
4072
4073/*
4074 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4075 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4076*/
4077
4078/* draw item text */
4079
4080 SetRect(&rc, cx, 0, size.cx, size.cy);
4081 DrawTextA(hdc, dragItem->pszText, lstrlenA(dragItem->pszText), &rc,
4082 DT_LEFT);
4083 SelectObject(hdc, hOldFont);
4084 SelectObject(hdc, hOldbmp);
4085
4086 ImageList_Add(infoPtr->dragList, hbmp, 0);
4087
4088 DeleteDC(hdc);
4089 DeleteObject(hbmp);
4090 ReleaseDC(hwtop, htopdc);
4091
4092 return (LRESULT)infoPtr->dragList;
4093}
4094
4095/* Selection ************************************************************/
4096
4097static LRESULT
4098TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4099 INT cause)
4100{
4101 TREEVIEW_ITEM *prevSelect;
4102 RECT rcFocused;
4103
4104 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4105
4106 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4107 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4108 newSelect ? newSelect->state : 0);
4109
4110 /* reset and redraw focusedItem if focusedItem was set so we don't */
4111 /* have to worry about the previously focused item when we set a new one */
4112 if(infoPtr->focusedItem)
4113 {
4114 rcFocused = (infoPtr->focusedItem)->rect;
4115 infoPtr->focusedItem = 0;
4116 InvalidateRect(infoPtr->hwnd, &rcFocused, TRUE);
4117 }
4118
4119 switch (action)
4120 {
4121 case TVGN_CARET:
4122 prevSelect = infoPtr->selectedItem;
4123
4124 if (prevSelect == newSelect)
4125 return FALSE;
4126
4127 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4128 TVN_SELCHANGINGA,
4129 cause,
4130 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4131 prevSelect,
4132 newSelect))
4133 return FALSE;
4134
4135 if (prevSelect)
4136 prevSelect->state &= ~TVIS_SELECTED;
4137 if (newSelect)
4138 newSelect->state |= TVIS_SELECTED;
4139
4140 infoPtr->selectedItem = newSelect;
4141
4142 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4143
4144 TREEVIEW_SendTreeviewNotify(infoPtr,
4145 TVN_SELCHANGEDA,
4146 cause,
4147 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4148 prevSelect,
4149 newSelect);
4150 TREEVIEW_Invalidate(infoPtr, prevSelect);
4151 TREEVIEW_Invalidate(infoPtr, newSelect);
4152 break;
4153
4154 case TVGN_DROPHILITE:
4155 prevSelect = infoPtr->dropItem;
4156
4157 if (prevSelect)
4158 prevSelect->state &= ~TVIS_DROPHILITED;
4159
4160 infoPtr->dropItem = newSelect;
4161
4162 if (newSelect)
4163 newSelect->state |= TVIS_DROPHILITED;
4164
4165 TREEVIEW_Invalidate(infoPtr, prevSelect);
4166 TREEVIEW_Invalidate(infoPtr, newSelect);
4167 break;
4168
4169 case TVGN_FIRSTVISIBLE:
4170 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4171 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4172 TREEVIEW_Invalidate(infoPtr, NULL);
4173 break;
4174 }
4175
4176 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4177 return TRUE;
4178}
4179
4180/* FIXME: handle NM_KILLFOCUS etc */
4181static LRESULT
4182TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4183{
4184 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4185 return FALSE;
4186
4187 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4188
4189 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4190 return FALSE;
4191
4192 return TRUE;
4193}
4194
4195/*************************************************************************
4196 * TREEVIEW_ProcessLetterKeys
4197 *
4198 * Processes keyboard messages generated by pressing the letter keys
4199 * on the keyboard.
4200 * What this does is perform a case insensitive search from the
4201 * current position with the following quirks:
4202 * - If two chars or more are pressed in quick succession we search
4203 * for the corresponding string (e.g. 'abc').
4204 * - If there is a delay we wipe away the current search string and
4205 * restart with just that char.
4206 * - If the user keeps pressing the same character, whether slowly or
4207 * fast, so that the search string is entirely composed of this
4208 * character ('aaaaa' for instance), then we search for first item
4209 * that starting with that character.
4210 * - If the user types the above character in quick succession, then
4211 * we must also search for the corresponding string ('aaaaa'), and
4212 * go to that string if there is a match.
4213 *
4214 * RETURNS
4215 *
4216 * Zero.
4217 *
4218 * BUGS
4219 *
4220 * - The current implementation has a list of characters it will
4221 * accept and it ignores averything else. In particular it will
4222 * ignore accentuated characters which seems to match what
4223 * Windows does. But I'm not sure it makes sense to follow
4224 * Windows there.
4225 * - We don't sound a beep when the search fails.
4226 * - The search should start from the focused item, not from the selected
4227 * item. One reason for this is to allow for multiple selections in trees.
4228 * But currently infoPtr->focusedItem does not seem very usable.
4229 *
4230 * SEE ALSO
4231 *
4232 * TREEVIEW_ProcessLetterKeys
4233 */
4234static INT TREEVIEW_ProcessLetterKeys(
4235 HWND hwnd, /* handle to the window */
4236 WPARAM charCode, /* the character code, the actual character */
4237 LPARAM keyData /* key data */
4238 )
4239{
4240 TREEVIEW_INFO *infoPtr;
4241 HTREEITEM nItem;
4242 HTREEITEM endidx,idx;
4243 TVITEMEXA item;
4244 CHAR buffer[MAX_PATH];
4245 DWORD timestamp,elapsed;
4246
4247 /* simple parameter checking */
4248 if (!hwnd || !charCode || !keyData)
4249 return 0;
4250
4251 infoPtr=(TREEVIEW_INFO*)GetWindowLongA(hwnd, 0);
4252 if (!infoPtr)
4253 return 0;
4254
4255 /* only allow the valid WM_CHARs through */
4256 if (!isalnum(charCode) &&
4257 charCode != '.' && charCode != '`' && charCode != '!' &&
4258 charCode != '@' && charCode != '#' && charCode != '$' &&
4259 charCode != '%' && charCode != '^' && charCode != '&' &&
4260 charCode != '*' && charCode != '(' && charCode != ')' &&
4261 charCode != '-' && charCode != '_' && charCode != '+' &&
4262 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4263 charCode != '}' && charCode != '[' && charCode != '{' &&
4264 charCode != '/' && charCode != '?' && charCode != '>' &&
4265 charCode != '<' && charCode != ',' && charCode != '~')
4266 return 0;
4267
4268 /* compute how much time elapsed since last keypress */
4269 timestamp = GetTickCount();
4270 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4271 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4272 } else {
4273 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4274 }
4275
4276 /* update the search parameters */
4277 infoPtr->lastKeyPressTimestamp=timestamp;
4278 if (elapsed < KEY_DELAY) {
4279 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam)) {
4280 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4281 }
4282 if (infoPtr->charCode != charCode) {
4283 infoPtr->charCode=charCode=0;
4284 }
4285 } else {
4286 infoPtr->charCode=charCode;
4287 infoPtr->szSearchParam[0]=charCode;
4288 infoPtr->nSearchParamLength=1;
4289 /* Redundant with the 1 char string */
4290 charCode=0;
4291 }
4292
4293 /* and search from the current position */
4294 nItem=NULL;
4295 if (infoPtr->selectedItem != NULL) {
4296 endidx=infoPtr->selectedItem;
4297 /* if looking for single character match,
4298 * then we must always move forward
4299 */
4300 if (infoPtr->nSearchParamLength == 1)
4301 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4302 else
4303 idx=endidx;
4304 } else {
4305 endidx=NULL;
4306 idx=infoPtr->root->firstChild;
4307 }
4308 do {
4309 if (idx == NULL) {
4310 if (endidx == NULL)
4311 break;
4312 idx=infoPtr->root->firstChild;
4313 }
4314
4315 /* get item */
4316 ZeroMemory(&item, sizeof(item));
4317 item.mask = TVIF_TEXT;
4318 item.hItem = idx;
4319 item.pszText = buffer;
4320 item.cchTextMax = sizeof(buffer);
4321 TREEVIEW_GetItemA( infoPtr, &item );
4322
4323 /* check for a match */
4324 if (strncasecmp(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4325 nItem=idx;
4326 break;
4327 } else if ( (charCode != 0) && (nItem == NULL) &&
4328 (nItem != infoPtr->selectedItem) &&
4329 (strncasecmp(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4330 /* This would work but we must keep looking for a longer match */
4331 nItem=idx;
4332 }
4333 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4334 } while (idx != endidx);
4335
4336 if (nItem != NULL) {
4337 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4338 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4339 }
4340 }
4341
4342 return 0;
4343}
4344
4345/* Scrolling ************************************************************/
4346
4347static LRESULT
4348TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4349{
4350 HTREEITEM newFirstVisible = NULL;
4351 int visible_pos;
4352
4353 if (!TREEVIEW_ValidItem(infoPtr, item))
4354 return FALSE;
4355
4356 if (!ISVISIBLE(item))
4357 {
4358 /* Expand parents as necessary. */
4359 HTREEITEM parent;
4360
4361 /* see if we are trying to ensure that root is vislble */
4362 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4363 parent = item->parent;
4364 else
4365 parent = item; /* this item is the topmost item */
4366
4367 while (parent != infoPtr->root)
4368 {
4369 if (!(parent->state & TVIS_EXPANDED))
4370 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4371
4372 parent = parent->parent;
4373 }
4374 }
4375
4376 TRACE("%p (%s) %ld - %ld\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4377 infoPtr->firstVisible->visibleOrder);
4378
4379 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4380
4381 if (visible_pos < 0)
4382 {
4383 /* item is before the start of the list: put it at the top. */
4384 newFirstVisible = item;
4385 }
4386 else if (visible_pos >= TREEVIEW_GetVisibleCount(infoPtr)
4387 /* Sometimes, before we are displayed, GVC is 0, causing us to
4388 * spuriously scroll up. */
4389 && visible_pos > 0)
4390 {
4391 /* item is past the end of the list. */
4392 int scroll = visible_pos - TREEVIEW_GetVisibleCount(infoPtr);
4393
4394 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4395 scroll + 1);
4396 }
4397
4398 if (bHScroll)
4399 {
4400 /* Scroll window so item's text is visible as much as possible */
4401 /* Calculation of amount of extra space is taken from EditLabel code */
4402 INT pos, x;
4403 TEXTMETRICA textMetric;
4404 HDC hdc = GetWindowDC(infoPtr->hwnd);
4405
4406 x = item->textWidth;
4407
4408 GetTextMetricsA(hdc, &textMetric);
4409 ReleaseDC(infoPtr->hwnd, hdc);
4410
4411 x += (textMetric.tmMaxCharWidth * 2);
4412 x = max(x, textMetric.tmMaxCharWidth * 3);
4413
4414 if (item->textOffset < 0)
4415 pos = item->textOffset;
4416 else if (item->textOffset + x > infoPtr->clientWidth)
4417 {
4418 if (x > infoPtr->clientWidth)
4419 pos = item->textOffset;
4420 else
4421 pos = item->textOffset + x - infoPtr->clientWidth;
4422 }
4423 else
4424 pos = 0;
4425
4426 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4427 }
4428
4429 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4430 {
4431 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4432
4433 return TRUE;
4434 }
4435
4436 return FALSE;
4437}
4438
4439static VOID
4440TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4441 TREEVIEW_ITEM *newFirstVisible,
4442 BOOL bUpdateScrollPos)
4443{
4444 int gap_size;
4445
4446 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4447
4448 if (newFirstVisible != NULL)
4449 {
4450 /* Prevent an empty gap from appearing at the bottom... */
4451 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4452 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4453
4454 if (gap_size > 0)
4455 {
4456 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4457 -gap_size);
4458
4459 /* ... unless we just don't have enough items. */
4460 if (newFirstVisible == NULL)
4461 newFirstVisible = infoPtr->root->firstChild;
4462 }
4463 }
4464
4465 if (infoPtr->firstVisible != newFirstVisible)
4466 {
4467 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4468 {
4469 infoPtr->firstVisible = newFirstVisible;
4470 TREEVIEW_Invalidate(infoPtr, NULL);
4471 }
4472 else
4473 {
4474 TREEVIEW_ITEM *item;
4475 int scroll = infoPtr->uItemHeight *
4476 (infoPtr->firstVisible->visibleOrder
4477 - newFirstVisible->visibleOrder);
4478
4479 infoPtr->firstVisible = newFirstVisible;
4480
4481 for (item = infoPtr->root->firstChild; item != NULL;
4482 item = TREEVIEW_GetNextListItem(infoPtr, item))
4483 {
4484 item->rect.top += scroll;
4485 item->rect.bottom += scroll;
4486 }
4487
4488 if (bUpdateScrollPos)
4489 SetScrollPos(infoPtr->hwnd, SB_VERT,
4490 newFirstVisible->visibleOrder, TRUE);
4491
4492 ScrollWindow(infoPtr->hwnd, 0, scroll, NULL, NULL);
4493 UpdateWindow(infoPtr->hwnd);
4494 }
4495 }
4496}
4497
4498/************************************************************************
4499 * VScroll is always in units of visible items. i.e. we always have a
4500 * visible item aligned to the top of the control. (Unless we have no
4501 * items at all.)
4502 */
4503static LRESULT
4504TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4505{
4506 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4507 TREEVIEW_ITEM *newFirstVisible = NULL;
4508
4509 int nScrollCode = LOWORD(wParam);
4510
4511 TRACE("wp %x\n", wParam);
4512
4513 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4514 return 0;
4515
4516 if (infoPtr->hwndEdit)
4517 SetFocus(infoPtr->hwnd);
4518
4519 if (!oldFirstVisible)
4520 {
4521 assert(infoPtr->root->firstChild == NULL);
4522 return 0;
4523 }
4524
4525 switch (nScrollCode)
4526 {
4527 case SB_TOP:
4528 newFirstVisible = infoPtr->root->firstChild;
4529 break;
4530
4531 case SB_BOTTOM:
4532 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4533 break;
4534
4535 case SB_LINEUP:
4536 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4537 break;
4538
4539 case SB_LINEDOWN:
4540 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4541 break;
4542
4543 case SB_PAGEUP:
4544 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4545 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4546 break;
4547
4548 case SB_PAGEDOWN:
4549 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4550 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4551 break;
4552
4553 case SB_THUMBTRACK:
4554 case SB_THUMBPOSITION:
4555 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4556 infoPtr->root->firstChild,
4557 (LONG)(SHORT)HIWORD(wParam));
4558 break;
4559
4560 case SB_ENDSCROLL:
4561 return 0;
4562 }
4563
4564 if (newFirstVisible != NULL)
4565 {
4566 if (newFirstVisible != oldFirstVisible)
4567 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4568 nScrollCode != SB_THUMBTRACK);
4569 else if (nScrollCode == SB_THUMBPOSITION)
4570 SetScrollPos(infoPtr->hwnd, SB_VERT,
4571 newFirstVisible->visibleOrder, TRUE);
4572 }
4573
4574 return 0;
4575}
4576
4577static LRESULT
4578TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4579{
4580 int maxWidth;
4581 int scrollX = infoPtr->scrollX;
4582 int nScrollCode = LOWORD(wParam);
4583
4584 TRACE("wp %x\n", wParam);
4585
4586 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4587 return FALSE;
4588
4589 if (infoPtr->hwndEdit)
4590 SetFocus(infoPtr->hwnd);
4591
4592 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4593 /* shall never occur */
4594 if (maxWidth <= 0)
4595 {
4596 scrollX = 0;
4597 goto scroll;
4598 }
4599
4600 switch (nScrollCode)
4601 {
4602 case SB_LINELEFT:
4603 scrollX -= infoPtr->uItemHeight;
4604 break;
4605 case SB_LINERIGHT:
4606 scrollX += infoPtr->uItemHeight;
4607 break;
4608 case SB_PAGELEFT:
4609 scrollX -= infoPtr->clientWidth;
4610 break;
4611 case SB_PAGERIGHT:
4612 scrollX += infoPtr->clientWidth;
4613 break;
4614
4615 case SB_THUMBTRACK:
4616 case SB_THUMBPOSITION:
4617 scrollX = (int)(SHORT)HIWORD(wParam);
4618 break;
4619
4620 case SB_ENDSCROLL:
4621 return 0;
4622 }
4623
4624 if (scrollX > maxWidth)
4625 scrollX = maxWidth;
4626 else if (scrollX < 0)
4627 scrollX = 0;
4628
4629scroll:
4630 if (scrollX != infoPtr->scrollX)
4631 {
4632 TREEVIEW_ITEM *item;
4633 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4634
4635 for (item = infoPtr->root->firstChild; item != NULL;
4636 item = TREEVIEW_GetNextListItem(infoPtr, item))
4637 {
4638 item->linesOffset += scroll_pixels;
4639 item->stateOffset += scroll_pixels;
4640 item->imageOffset += scroll_pixels;
4641 item->textOffset += scroll_pixels;
4642 }
4643
4644 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4645 infoPtr->scrollX = scrollX;
4646 UpdateWindow(infoPtr->hwnd);
4647 }
4648
4649 if (nScrollCode != SB_THUMBTRACK)
4650 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4651
4652 return 0;
4653}
4654
4655static LRESULT
4656TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4657{
4658 short gcWheelDelta;
4659 UINT pulScrollLines = 3;
4660
4661 if (infoPtr->firstVisible == NULL)
4662 return TRUE;
4663
4664 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4665
4666 gcWheelDelta = -(short)HIWORD(wParam);
4667 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4668
4669 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4670 {
4671 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4672 int maxDy = infoPtr->maxVisibleOrder;
4673
4674 if (newDy > maxDy)
4675 newDy = maxDy;
4676
4677 if (newDy < 0)
4678 newDy = 0;
4679
4680 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4681 }
4682 return TRUE;
4683}
4684
4685/* Create/Destroy *******************************************************/
4686
4687static LRESULT
4688TREEVIEW_Create(HWND hwnd)
4689{
4690 RECT rcClient;
4691 TREEVIEW_INFO *infoPtr;
4692
4693 TRACE("wnd %p, style %lx\n", hwnd, GetWindowLongA(hwnd, GWL_STYLE));
4694
4695 infoPtr = (TREEVIEW_INFO *)COMCTL32_Alloc(sizeof(TREEVIEW_INFO));
4696
4697 if (infoPtr == NULL)
4698 {
4699 ERR("could not allocate info memory!\n");
4700 return 0;
4701 }
4702
4703 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
4704
4705 infoPtr->hwnd = hwnd;
4706 infoPtr->dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
4707 infoPtr->uInternalStatus = 0;
4708 infoPtr->Timer = 0;
4709 infoPtr->uNumItems = 0;
4710 infoPtr->cdmode = 0;
4711 infoPtr->uScrollTime = 300; /* milliseconds */
4712 infoPtr->bRedraw = TRUE;
4713
4714 GetClientRect(hwnd, &rcClient);
4715
4716 /* No scroll bars yet. */
4717 infoPtr->clientWidth = rcClient.right;
4718 infoPtr->clientHeight = rcClient.bottom;
4719
4720 infoPtr->treeWidth = 0;
4721 infoPtr->treeHeight = 0;
4722
4723 infoPtr->uIndent = 19;
4724 infoPtr->selectedItem = 0;
4725 infoPtr->focusedItem = 0;
4726 /* hotItem? */
4727 infoPtr->firstVisible = 0;
4728 infoPtr->maxVisibleOrder = 0;
4729 infoPtr->dropItem = 0;
4730 infoPtr->insertMarkItem = 0;
4731 infoPtr->insertBeforeorAfter = 0;
4732 /* dragList */
4733
4734 infoPtr->scrollX = 0;
4735
4736 infoPtr->clrBk = GetSysColor(COLOR_WINDOW);
4737 infoPtr->clrText = -1; /* use system color */
4738 infoPtr->clrLine = RGB(128, 128, 128);
4739 infoPtr->clrInsertMark = GetSysColor(COLOR_BTNTEXT);
4740
4741 /* hwndToolTip */
4742
4743 infoPtr->hwndEdit = 0;
4744 infoPtr->wpEditOrig = NULL;
4745 infoPtr->bIgnoreEditKillFocus = FALSE;
4746 infoPtr->bLabelChanged = FALSE;
4747
4748 infoPtr->himlNormal = NULL;
4749 infoPtr->himlState = NULL;
4750 infoPtr->normalImageWidth = 0;
4751 infoPtr->normalImageHeight = 0;
4752 infoPtr->stateImageWidth = 0;
4753 infoPtr->stateImageHeight = 0;
4754
4755 infoPtr->items = DPA_Create(16);
4756
4757 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
4758 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
4759
4760 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
4761
4762 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
4763 infoPtr->root->state = TVIS_EXPANDED;
4764 infoPtr->root->iLevel = -1;
4765 infoPtr->root->visibleOrder = -1;
4766
4767 infoPtr->hwndNotify = GetParent(hwnd);
4768#if 0
4769 infoPtr->bTransparent = ( GetWindowLongA( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
4770#endif
4771
4772 infoPtr->hwndToolTip = 0;
4773
4774 infoPtr->bUnicode = IsWindowUnicode (hwnd);
4775
4776 /* Determine what type of notify should be issued */
4777 /* sets infoPtr->bNtfUnicode */
4778 TREEVIEW_NotifyFormat(infoPtr, 0, NF_REQUERY);
4779
4780 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
4781 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(hwnd);
4782
4783 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4784 {
4785 RECT rc;
4786 HBITMAP hbm, hbmOld;
4787 HDC hdc;
4788 int nIndex;
4789
4790 infoPtr->himlState =
4791 ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4792
4793 hdc = CreateCompatibleDC(0);
4794 hbm = CreateCompatibleBitmap(hdc, 48, 16);
4795 hbmOld = SelectObject(hdc, hbm);
4796
4797 rc.left = 0; rc.top = 0;
4798 rc.right = 48; rc.bottom = 16;
4799 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4800
4801 rc.left = 18; rc.top = 2;
4802 rc.right = 30; rc.bottom = 14;
4803 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4804 DFCS_BUTTONCHECK|DFCS_FLAT);
4805
4806 rc.left = 34; rc.right = 46;
4807 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4808 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4809
4810 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4811 GetSysColor(COLOR_WINDOW));
4812 TRACE("chckbox index %d\n", nIndex);
4813 SelectObject(hdc, hbmOld);
4814 DeleteObject(hbm);
4815 DeleteDC(hdc);
4816
4817 infoPtr->stateImageWidth = 16;
4818 infoPtr->stateImageHeight = 16;
4819 }
4820 return 0;
4821}
4822
4823
4824static LRESULT
4825TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
4826{
4827 TRACE("\n");
4828
4829 TREEVIEW_RemoveTree(infoPtr);
4830
4831 /* tool tip is automatically destroyed: we are its owner */
4832
4833 /* Restore original wndproc */
4834 if (infoPtr->hwndEdit)
4835 SetWindowLongA(infoPtr->hwndEdit, GWL_WNDPROC,
4836 (LONG)infoPtr->wpEditOrig);
4837
4838 /* Deassociate treeview from the window before doing anything drastic. */
4839 SetWindowLongA(infoPtr->hwnd, 0, (LONG)NULL);
4840
4841 DeleteObject(infoPtr->hBoldFont);
4842 COMCTL32_Free(infoPtr);
4843
4844 return 0;
4845}
4846
4847/* Miscellaneous Messages ***********************************************/
4848
4849static LRESULT
4850TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
4851{
4852 static const struct
4853 {
4854 unsigned char code;
4855 }
4856 scroll[] =
4857 {
4858#define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
4859 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
4860 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
4861 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
4862 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
4863 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
4864 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
4865 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
4866 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
4867#undef SCROLL_ENTRY
4868 };
4869
4870 if (key >= VK_PRIOR && key <= VK_DOWN)
4871 {
4872 unsigned char code = scroll[key - VK_PRIOR].code;
4873
4874 (((code & (1 << 7)) == (SB_HORZ << 7))
4875 ? TREEVIEW_HScroll
4876 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
4877 }
4878
4879 return 0;
4880}
4881
4882/************************************************************************
4883 * TREEVIEW_KeyDown
4884 *
4885 * VK_UP Move selection to the previous non-hidden item.
4886 * VK_DOWN Move selection to the next non-hidden item.
4887 * VK_HOME Move selection to the first item.
4888 * VK_END Move selection to the last item.
4889 * VK_LEFT If expanded then collapse, otherwise move to parent.
4890 * VK_RIGHT If collapsed then expand, otherwise move to first child.
4891 * VK_ADD Expand.
4892 * VK_SUBTRACT Collapse.
4893 * VK_MULTIPLY Expand all.
4894 * VK_PRIOR Move up GetVisibleCount items.
4895 * VK_NEXT Move down GetVisibleCount items.
4896 * VK_BACK Move to parent.
4897 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
4898 */
4899static LRESULT
4900TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4901{
4902 /* If it is non-NULL and different, it will be selected and visible. */
4903 TREEVIEW_ITEM *newSelection = NULL;
4904
4905 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
4906
4907 TRACE("%x\n", wParam);
4908
4909 if (prevItem == NULL)
4910 return FALSE;
4911
4912 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
4913 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
4914
4915 switch (wParam)
4916 {
4917 case VK_UP:
4918 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
4919 if (!newSelection)
4920 newSelection = infoPtr->root->firstChild;
4921 break;
4922
4923 case VK_DOWN:
4924 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
4925 break;
4926
4927 case VK_HOME:
4928 newSelection = infoPtr->root->firstChild;
4929 break;
4930
4931 case VK_END:
4932 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4933 break;
4934
4935 case VK_LEFT:
4936 if (prevItem->state & TVIS_EXPANDED)
4937 {
4938 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4939 }
4940 else if (prevItem->parent != infoPtr->root)
4941 {
4942 newSelection = prevItem->parent;
4943 }
4944 break;
4945
4946 case VK_RIGHT:
4947 if (TREEVIEW_HasChildren(infoPtr, prevItem))
4948 {
4949 if (!(prevItem->state & TVIS_EXPANDED))
4950 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4951 else
4952 {
4953 newSelection = prevItem->firstChild;
4954 }
4955 }
4956
4957 break;
4958
4959 case VK_MULTIPLY:
4960 TREEVIEW_ExpandAll(infoPtr, prevItem);
4961 break;
4962
4963 case VK_ADD:
4964 if (!(prevItem->state & TVIS_EXPANDED))
4965 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
4966 break;
4967
4968 case VK_SUBTRACT:
4969 if (prevItem->state & TVIS_EXPANDED)
4970 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
4971 break;
4972
4973 case VK_PRIOR:
4974 newSelection
4975 = TREEVIEW_GetListItem(infoPtr, prevItem,
4976 -TREEVIEW_GetVisibleCount(infoPtr));
4977 break;
4978
4979 case VK_NEXT:
4980 newSelection
4981 = TREEVIEW_GetListItem(infoPtr, prevItem,
4982 TREEVIEW_GetVisibleCount(infoPtr));
4983 break;
4984
4985 case VK_BACK:
4986 newSelection = prevItem->parent;
4987 if (newSelection == infoPtr->root)
4988 newSelection = NULL;
4989 break;
4990
4991 case VK_SPACE:
4992 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4993 TREEVIEW_ToggleItemState(infoPtr, prevItem);
4994 break;
4995 }
4996
4997 if (newSelection && newSelection != prevItem)
4998 {
4999 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5000 TVC_BYKEYBOARD))
5001 {
5002 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5003 }
5004 }
5005
5006 return FALSE;
5007}
5008
5009static LRESULT
5010TREEVIEW_Notify(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5011{
5012 LPNMHDR lpnmh = (LPNMHDR)lParam;
5013
5014 if (lpnmh->code == PGN_CALCSIZE) {
5015 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5016
5017 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5018 lppgc->iWidth = infoPtr->treeWidth;
5019 TRACE("got PGN_CALCSIZE, returning horz size = %ld, client=%ld\n",
5020 infoPtr->treeWidth, infoPtr->clientWidth);
5021 }
5022 else {
5023 lppgc->iHeight = infoPtr->treeHeight;
5024 TRACE("got PGN_CALCSIZE, returning vert size = %ld, client=%ld\n",
5025 infoPtr->treeHeight, infoPtr->clientHeight);
5026 }
5027 return 0;
5028 }
5029 return DefWindowProcA(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5030}
5031
5032static LRESULT
5033TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5034{
5035 INT i;
5036
5037 if (lParam == NF_REQUERY) {
5038 i = SendMessageA(GetParent (infoPtr->hwnd),
5039 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
5040 if ((i < NFR_ANSI) || (i > NFR_UNICODE)) {
5041 ERR("wrong response to WM_NOTIFYFORMAT (%d), assuming ANSI\n",
5042 i);
5043 i = NFR_ANSI;
5044 }
5045 infoPtr->bNtfUnicode = (i == NFR_UNICODE) ? 1 : 0;
5046 return (LRESULT)i;
5047 }
5048 return (LRESULT)((infoPtr->bNtfUnicode) ? NFR_UNICODE : NFR_ANSI);
5049}
5050
5051
5052static LRESULT
5053TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5054{
5055 if (wParam == SIZE_RESTORED)
5056 {
5057 infoPtr->clientWidth = SLOWORD(lParam);
5058 infoPtr->clientHeight = SHIWORD(lParam);
5059
5060 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5061 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5062 TREEVIEW_UpdateScrollBars(infoPtr);
5063 }
5064 else
5065 {
5066 FIXME("WM_SIZE flag %x %lx not handled\n", wParam, lParam);
5067 }
5068
5069 TREEVIEW_Invalidate(infoPtr, NULL);
5070 return 0;
5071}
5072
5073static LRESULT
5074TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5075{
5076 TRACE("(%x %lx)\n", wParam, lParam);
5077
5078 if (wParam == GWL_STYLE)
5079 {
5080 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5081
5082 /* we have to take special care about tooltips */
5083 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5084 {
5085 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5086 {
5087 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5088 TRACE("\n");
5089 }
5090 else
5091 {
5092 DestroyWindow(infoPtr->hwndToolTip);
5093 infoPtr->hwndToolTip = 0;
5094 }
5095 }
5096
5097 infoPtr->dwStyle = dwNewStyle;
5098 }
5099
5100 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5101 TREEVIEW_UpdateScrollBars(infoPtr);
5102 TREEVIEW_Invalidate(infoPtr, NULL);
5103
5104 return 0;
5105}
5106
5107static LRESULT
5108TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5109{
5110 TRACE("\n");
5111
5112 if (!infoPtr->selectedItem)
5113 {
5114 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5115 TVC_UNKNOWN);
5116 }
5117
5118 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5119 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5120 return 0;
5121}
5122
5123static LRESULT
5124TREEVIEW_KillFocus(TREEVIEW_INFO *infoPtr)
5125{
5126 TRACE("\n");
5127
5128 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5129 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5130 return 0;
5131}
5132
5133
5134static LRESULT WINAPI
5135TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5136{
5137 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5138 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5139 else
5140 {
5141 if (uMsg == WM_CREATE)
5142 TREEVIEW_Create(hwnd);
5143 else
5144 goto def;
5145 }
5146
5147 switch (uMsg)
5148 {
5149 case TVM_CREATEDRAGIMAGE:
5150 return TREEVIEW_CreateDragImage(infoPtr, wParam, lParam);
5151
5152 case TVM_DELETEITEM:
5153 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5154
5155 case TVM_EDITLABELA:
5156 return (LRESULT)TREEVIEW_EditLabelA(infoPtr, (HTREEITEM)lParam);
5157
5158 case TVM_EDITLABELW:
5159 FIXME("Unimplemented msg TVM_EDITLABELW\n");
5160 return 0;
5161
5162 case TVM_ENDEDITLABELNOW:
5163 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5164
5165 case TVM_ENSUREVISIBLE:
5166 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5167
5168 case TVM_EXPAND:
5169 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5170
5171 case TVM_GETBKCOLOR:
5172 return TREEVIEW_GetBkColor(infoPtr);
5173
5174 case TVM_GETCOUNT:
5175 return TREEVIEW_GetCount(infoPtr);
5176
5177 case TVM_GETEDITCONTROL:
5178 return TREEVIEW_GetEditControl(infoPtr);
5179
5180 case TVM_GETIMAGELIST:
5181 return TREEVIEW_GetImageList(infoPtr, wParam);
5182
5183 case TVM_GETINDENT:
5184 return TREEVIEW_GetIndent(infoPtr);
5185
5186 case TVM_GETINSERTMARKCOLOR:
5187 return TREEVIEW_GetInsertMarkColor(infoPtr);
5188
5189 case TVM_GETISEARCHSTRINGA:
5190 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5191 return 0;
5192
5193 case TVM_GETISEARCHSTRINGW:
5194 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5195 return 0;
5196
5197 case TVM_GETITEMA:
5198 return TREEVIEW_GetItemA(infoPtr, (LPTVITEMEXA)lParam);
5199
5200 case TVM_GETITEMW:
5201 return TREEVIEW_GetItemW(infoPtr, (LPTVITEMEXW)lParam);
5202
5203 case TVM_GETITEMHEIGHT:
5204 return TREEVIEW_GetItemHeight(infoPtr);
5205
5206 case TVM_GETITEMRECT:
5207 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5208
5209 case TVM_GETITEMSTATE:
5210 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5211
5212 case TVM_GETLINECOLOR:
5213 return TREEVIEW_GetLineColor(infoPtr);
5214
5215 case TVM_GETNEXTITEM:
5216 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5217
5218 case TVM_GETSCROLLTIME:
5219 return TREEVIEW_GetScrollTime(infoPtr);
5220
5221 case TVM_GETTEXTCOLOR:
5222 return TREEVIEW_GetTextColor(infoPtr);
5223
5224 case TVM_GETTOOLTIPS:
5225 return TREEVIEW_GetToolTips(infoPtr);
5226
5227 case TVM_GETUNICODEFORMAT:
5228 FIXME("Unimplemented msg TVM_GETUNICODEFORMAT\n");
5229 return 0;
5230
5231 case TVM_GETVISIBLECOUNT:
5232 return TREEVIEW_GetVisibleCount(infoPtr);
5233
5234 case TVM_HITTEST:
5235 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5236
5237 case TVM_INSERTITEMA:
5238 return TREEVIEW_InsertItemA(infoPtr, lParam);
5239
5240 case TVM_INSERTITEMW:
5241 return TREEVIEW_InsertItemW(infoPtr, lParam);
5242
5243 case TVM_SELECTITEM:
5244 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5245
5246 case TVM_SETBKCOLOR:
5247 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5248
5249 case TVM_SETIMAGELIST:
5250 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5251
5252 case TVM_SETINDENT:
5253 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5254
5255 case TVM_SETINSERTMARK:
5256 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5257
5258 case TVM_SETINSERTMARKCOLOR:
5259 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5260
5261 case TVM_SETITEMA:
5262 return TREEVIEW_SetItemA(infoPtr, (LPTVITEMEXA)lParam);
5263
5264 case TVM_SETITEMW:
5265 return TREEVIEW_SetItemW(infoPtr, (LPTVITEMEXW)lParam);
5266 return 0;
5267
5268 case TVM_SETLINECOLOR:
5269 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5270
5271 case TVM_SETITEMHEIGHT:
5272 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5273
5274 case TVM_SETSCROLLTIME:
5275 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5276
5277 case TVM_SETTEXTCOLOR:
5278 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5279
5280 case TVM_SETTOOLTIPS:
5281 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5282
5283 case TVM_SETUNICODEFORMAT:
5284 FIXME("Unimplemented msg TVM_SETUNICODEFORMAT\n");
5285 return 0;
5286
5287 case TVM_SORTCHILDREN:
5288 return TREEVIEW_SortChildren(infoPtr, wParam, lParam);
5289
5290 case TVM_SORTCHILDRENCB:
5291 return TREEVIEW_SortChildrenCB(infoPtr, wParam, (LPTVSORTCB)lParam);
5292
5293 case WM_CHAR:
5294 return TREEVIEW_ProcessLetterKeys( hwnd, wParam, lParam );
5295
5296 case WM_COMMAND:
5297 return TREEVIEW_Command(infoPtr, wParam, lParam);
5298
5299 case WM_DESTROY:
5300 return TREEVIEW_Destroy(infoPtr);
5301
5302 /* WM_ENABLE */
5303
5304 case WM_ERASEBKGND:
5305 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5306
5307 case WM_GETDLGCODE:
5308 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5309
5310 case WM_GETFONT:
5311 return TREEVIEW_GetFont(infoPtr);
5312
5313 case WM_HSCROLL:
5314 return TREEVIEW_HScroll(infoPtr, wParam);
5315
5316 case WM_KEYDOWN:
5317 return TREEVIEW_KeyDown(infoPtr, wParam);
5318
5319 case WM_KILLFOCUS:
5320 return TREEVIEW_KillFocus(infoPtr);
5321
5322 case WM_LBUTTONDBLCLK:
5323 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5324
5325 case WM_LBUTTONDOWN:
5326 return TREEVIEW_LButtonDown(infoPtr, lParam);
5327
5328 /* WM_MBUTTONDOWN */
5329
5330 /* WM_MOUSEMOVE */
5331
5332 case WM_NOTIFY:
5333 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5334
5335 case WM_NOTIFYFORMAT:
5336 return TREEVIEW_NotifyFormat(infoPtr, wParam, lParam);
5337
5338 case WM_PAINT:
5339 return TREEVIEW_Paint(infoPtr, wParam);
5340
5341 /* WM_PRINTCLIENT */
5342
5343 case WM_RBUTTONDOWN:
5344 return TREEVIEW_RButtonDown(infoPtr, lParam);
5345
5346 case WM_SETFOCUS:
5347 return TREEVIEW_SetFocus(infoPtr);
5348
5349 case WM_SETFONT:
5350 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5351
5352 case WM_SETREDRAW:
5353 return TREEVIEW_SetRedraw(infoPtr, wParam, lParam);
5354
5355 case WM_SIZE:
5356 return TREEVIEW_Size(infoPtr, wParam, lParam);
5357
5358 case WM_STYLECHANGED:
5359 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5360
5361 /* WM_SYSCOLORCHANGE */
5362
5363 /* WM_SYSKEYDOWN */
5364
5365 case WM_TIMER:
5366 return TREEVIEW_HandleTimer(infoPtr, wParam);
5367
5368 case WM_VSCROLL:
5369 return TREEVIEW_VScroll(infoPtr, wParam);
5370
5371 /* WM_WININICHANGE */
5372
5373 case WM_MOUSEWHEEL:
5374 if (wParam & (MK_SHIFT | MK_CONTROL))
5375 goto def;
5376 return TREEVIEW_MouseWheel(infoPtr, wParam);
5377
5378 case WM_DRAWITEM:
5379 TRACE("drawItem\n");
5380 goto def;
5381
5382 default:
5383 /* This mostly catches MFC and Delphi messages. :( */
5384 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5385 TRACE("Unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
5386def:
5387 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
5388 }
5389 return 0;
5390}
5391
5392
5393/* Class Registration ***************************************************/
5394
5395VOID
5396TREEVIEW_Register(void)
5397{
5398 WNDCLASSA wndClass;
5399
5400 TRACE("\n");
5401
5402 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
5403 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5404 wndClass.lpfnWndProc = (WNDPROC)TREEVIEW_WindowProc;
5405 wndClass.cbClsExtra = 0;
5406 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5407
5408 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
5409 wndClass.hbrBackground = 0;
5410 wndClass.lpszClassName = WC_TREEVIEWA;
5411
5412 RegisterClassA(&wndClass);
5413}
5414
5415
5416VOID
5417TREEVIEW_Unregister(void)
5418{
5419 UnregisterClassA(WC_TREEVIEWA, (HINSTANCE) NULL);
5420}
5421
5422
5423/* Tree Verification ****************************************************/
5424
5425#ifdef NDEBUG
5426static inline void
5427TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5428
5429static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5430 TREEVIEW_ITEM *item)
5431{
5432 assert(infoPtr != NULL);
5433 assert(item != NULL);
5434
5435 /* both NULL, or both non-null */
5436 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5437
5438 assert(item->firstChild != item);
5439 assert(item->lastChild != item);
5440
5441 if (item->firstChild)
5442 {
5443 assert(item->firstChild->parent == item);
5444 assert(item->firstChild->prevSibling == NULL);
5445 }
5446
5447 if (item->lastChild)
5448 {
5449 assert(item->lastChild->parent == item);
5450 assert(item->lastChild->nextSibling == NULL);
5451 }
5452
5453 assert(item->nextSibling != item);
5454 if (item->nextSibling)
5455 {
5456 assert(item->nextSibling->parent == item->parent);
5457 assert(item->nextSibling->prevSibling == item);
5458 }
5459
5460 assert(item->prevSibling != item);
5461 if (item->prevSibling)
5462 {
5463 assert(item->prevSibling->parent == item->parent);
5464 assert(item->prevSibling->nextSibling == item);
5465 }
5466}
5467
5468static inline void
5469TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5470{
5471 assert(item != NULL);
5472
5473 assert(item->parent != NULL);
5474 assert(item->parent != item);
5475 assert(item->iLevel == item->parent->iLevel + 1);
5476
5477 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5478
5479 TREEVIEW_VerifyItemCommon(infoPtr, item);
5480
5481 TREEVIEW_VerifyChildren(infoPtr, item);
5482}
5483
5484static inline void
5485TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5486{
5487 TREEVIEW_ITEM *child;
5488 assert(item != NULL);
5489
5490 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5491 TREEVIEW_VerifyItem(infoPtr, child);
5492}
5493
5494static inline void
5495TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5496{
5497 TREEVIEW_ITEM *root = infoPtr->root;
5498
5499 assert(root != NULL);
5500 assert(root->iLevel == -1);
5501 assert(root->parent == NULL);
5502 assert(root->prevSibling == NULL);
5503
5504 TREEVIEW_VerifyItemCommon(infoPtr, root);
5505
5506 TREEVIEW_VerifyChildren(infoPtr, root);
5507}
5508
5509static void
5510TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5511{
5512 assert(infoPtr != NULL);
5513
5514 TREEVIEW_VerifyRoot(infoPtr);
5515}
5516#endif
Note: See TracBrowser for help on using the repository browser.