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

Last change on this file since 10098 was 10098, checked in by sandervl, 22 years ago

Wine resync

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