]> git.saurik.com Git - wxWidgets.git/blame - src/msw/treectrl.cpp
Applied patch [ 774837 ] OGL wxLineShape::HitTest: smaller region
[wxWidgets.git] / src / msw / treectrl.cpp
CommitLineData
b823f5a1 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/msw/treectrl.cpp
b823f5a1
JS
3// Purpose: wxTreeCtrl
4// Author: Julian Smart
08b7c251 5// Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
b823f5a1
JS
6// Created: 1997
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
08b7c251 9// Licence: wxWindows licence
b823f5a1 10/////////////////////////////////////////////////////////////////////////////
2bda0e17 11
08b7c251
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
1e6feb95 19
2bda0e17 20#ifdef __GNUG__
08b7c251 21 #pragma implementation "treectrl.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
08b7c251 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_TREECTRL
32
0c589ad0
BM
33#include "wx/msw/private.h"
34
1e6feb95
VZ
35// Set this to 1 to be _absolutely_ sure that repainting will work for all
36// comctl32.dll versions
f6bcfd97
BP
37#define wxUSE_COMCTL32_SAFELY 0
38
c6f4913a 39#include "wx/app.h"
08b7c251 40#include "wx/log.h"
ce3ed50d 41#include "wx/dynarray.h"
08b7c251 42#include "wx/imaglist.h"
77cff606 43#include "wx/settings.h"
484523cf 44#include "wx/msw/treectrl.h"
23f681ec
VZ
45#include "wx/msw/dragimag.h"
46
c42404a5
VZ
47#ifdef __GNUWIN32_OLD__
48 #include "wx/msw/gnuwin32/extra.h"
65fd5cb0 49#endif
9a05fd8d 50
b39dbf34 51#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
08b7c251 52 #include <commctrl.h>
2bda0e17
KB
53#endif
54
55// Bug in headers, sometimes
56#ifndef TVIS_FOCUSED
08b7c251 57 #define TVIS_FOCUSED 0x0001
2bda0e17
KB
58#endif
59
bb448552
VZ
60#ifndef TV_FIRST
61 #define TV_FIRST 0x1100
62#endif
63
b94ae1ea
VZ
64#ifndef TVS_CHECKBOXES
65 #define TVS_CHECKBOXES 0x0100
66#endif
67
5ece068d
VZ
68#ifndef TVS_FULLROWSELECT
69 #define TVS_FULLROWSELECT 0x1000
70#endif
71
bb448552
VZ
72// old headers might miss these messages (comctl32.dll 4.71+ only)
73#ifndef TVM_SETBKCOLOR
74 #define TVM_SETBKCOLOR (TV_FIRST + 29)
75 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
76#endif
77
f888d614
VZ
78// macros to hide the cast ugliness
79// --------------------------------
80
81// ptr is the real item id, i.e. wxTreeItemId::m_pItem
82#define HITEM_PTR(ptr) (HTREEITEM)(ptr)
83
84// item here is a wxTreeItemId
85#define HITEM(item) HITEM_PTR((item).m_pItem)
3f7bc32b
VZ
86
87// the native control doesn't support multiple selections under MSW and we
88// have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
89// checkboxes be the selection status (checked == selected) or by really
90// emulating everything, i.e. intercepting mouse and key events &c. The first
91// approach is much easier but doesn't work with comctl32.dll < 4.71 and also
92// looks quite ugly.
93#define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
94
3f7bc32b
VZ
95// ----------------------------------------------------------------------------
96// private functions
97// ----------------------------------------------------------------------------
98
99// wrapper for TreeView_HitTest
100static HTREEITEM GetItemFromPoint(HWND hwndTV, int x, int y)
101{
102 TV_HITTESTINFO tvht;
103 tvht.pt.x = x;
104 tvht.pt.y = y;
105
3f7bc32b
VZ
106 return (HTREEITEM)TreeView_HitTest(hwndTV, &tvht);
107}
108
109#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
110
111// wrappers for TreeView_GetItem/TreeView_SetItem
112static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem)
113{
a9c1265f 114
3f7bc32b
VZ
115 TV_ITEM tvi;
116 tvi.mask = TVIF_STATE | TVIF_HANDLE;
117 tvi.stateMask = TVIS_SELECTED;
118 tvi.hItem = hItem;
119
120 if ( !TreeView_GetItem(hwndTV, &tvi) )
121 {
f6bcfd97 122 wxLogLastError(wxT("TreeView_GetItem"));
3f7bc32b
VZ
123 }
124
125 return (tvi.state & TVIS_SELECTED) != 0;
126}
127
04cd30de 128static void SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = true)
3f7bc32b
VZ
129{
130 TV_ITEM tvi;
131 tvi.mask = TVIF_STATE | TVIF_HANDLE;
132 tvi.stateMask = TVIS_SELECTED;
133 tvi.state = select ? TVIS_SELECTED : 0;
134 tvi.hItem = hItem;
135
136 if ( TreeView_SetItem(hwndTV, &tvi) == -1 )
137 {
f6bcfd97 138 wxLogLastError(wxT("TreeView_SetItem"));
3f7bc32b
VZ
139 }
140}
141
142static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem)
143{
04cd30de 144 SelectItem(hwndTV, htItem, false);
3f7bc32b
VZ
145}
146
147static inline void ToggleItemSelection(HWND hwndTV, HTREEITEM htItem)
148{
149 SelectItem(hwndTV, htItem, !IsItemSelected(hwndTV, htItem));
150}
151
152// helper function which selects all items in a range and, optionally,
153// unselects all others
154static void SelectRange(HWND hwndTV,
155 HTREEITEM htFirst,
156 HTREEITEM htLast,
04cd30de 157 bool unselectOthers = true)
3f7bc32b
VZ
158{
159 // find the first (or last) item and select it
04cd30de 160 bool cont = true;
2c8e4738 161 HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV);
3f7bc32b
VZ
162 while ( htItem && cont )
163 {
164 if ( (htItem == htFirst) || (htItem == htLast) )
165 {
166 if ( !IsItemSelected(hwndTV, htItem) )
167 {
168 SelectItem(hwndTV, htItem);
169 }
170
04cd30de 171 cont = false;
3f7bc32b
VZ
172 }
173 else
174 {
175 if ( unselectOthers && IsItemSelected(hwndTV, htItem) )
176 {
177 UnselectItem(hwndTV, htItem);
178 }
179 }
180
2c8e4738 181 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
3f7bc32b
VZ
182 }
183
184 // select the items in range
185 cont = htFirst != htLast;
186 while ( htItem && cont )
187 {
188 if ( !IsItemSelected(hwndTV, htItem) )
189 {
190 SelectItem(hwndTV, htItem);
191 }
192
193 cont = (htItem != htFirst) && (htItem != htLast);
194
2c8e4738 195 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
3f7bc32b
VZ
196 }
197
198 // unselect the rest
199 if ( unselectOthers )
200 {
201 while ( htItem )
202 {
203 if ( IsItemSelected(hwndTV, htItem) )
204 {
205 UnselectItem(hwndTV, htItem);
206 }
207
2c8e4738 208 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
3f7bc32b
VZ
209 }
210 }
211
212 // seems to be necessary - otherwise the just selected items don't always
213 // appear as selected
214 UpdateWindow(hwndTV);
215}
216
217// helper function which tricks the standard control into changing the focused
218// item without changing anything else (if someone knows why Microsoft doesn't
219// allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
220static void SetFocus(HWND hwndTV, HTREEITEM htItem)
221{
222 // the current focus
2c8e4738 223 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV);
3f7bc32b
VZ
224
225 if ( htItem )
226 {
227 // set the focus
228 if ( htItem != htFocus )
229 {
230 // remember the selection state of the item
231 bool wasSelected = IsItemSelected(hwndTV, htItem);
232
233 if ( htFocus && IsItemSelected(hwndTV, htFocus) )
234 {
235 // prevent the tree from unselecting the old focus which it
236 // would do by default (TreeView_SelectItem unselects the
237 // focused item)
238 TreeView_SelectItem(hwndTV, 0);
239 SelectItem(hwndTV, htFocus);
240 }
241
242 TreeView_SelectItem(hwndTV, htItem);
243
244 if ( !wasSelected )
245 {
246 // need to clear the selection which TreeView_SelectItem() gave
247 // us
248 UnselectItem(hwndTV, htItem);
249 }
250 //else: was selected, still selected - ok
251 }
252 //else: nothing to do, focus already there
253 }
254 else
255 {
256 if ( htFocus )
257 {
258 bool wasFocusSelected = IsItemSelected(hwndTV, htFocus);
259
260 // just clear the focus
261 TreeView_SelectItem(hwndTV, 0);
262
263 if ( wasFocusSelected )
264 {
265 // restore the selection state
266 SelectItem(hwndTV, htFocus);
267 }
268 }
269 //else: nothing to do, no focus already
270 }
271}
272
273#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
274
08b7c251
VZ
275// ----------------------------------------------------------------------------
276// private classes
277// ----------------------------------------------------------------------------
2bda0e17 278
08b7c251 279// a convenient wrapper around TV_ITEM struct which adds a ctor
f3ef286f 280#ifdef __VISUALC__
3f7bc32b 281#pragma warning( disable : 4097 ) // inheriting from typedef
f3ef286f
JS
282#endif
283
08b7c251
VZ
284struct wxTreeViewItem : public TV_ITEM
285{
9dfbf520
VZ
286 wxTreeViewItem(const wxTreeItemId& item, // the item handle
287 UINT mask_, // fields which are valid
288 UINT stateMask_ = 0) // for TVIF_STATE only
08b7c251 289 {
a9c1265f
VZ
290 wxZeroMemory(*this);
291
9dfbf520
VZ
292 // hItem member is always valid
293 mask = mask_ | TVIF_HANDLE;
08b7c251 294 stateMask = stateMask_;
3f7bc32b 295 hItem = HITEM(item);
08b7c251
VZ
296 }
297};
f3ef286f 298
a9c1265f
VZ
299// wxVirutalNode is used in place of a single root when 'hidden' root is
300// specified.
efa38350 301class wxVirtualNode : public wxTreeViewItem
a9c1265f
VZ
302{
303public:
304 wxVirtualNode(wxTreeItemData *data)
efa38350 305 : wxTreeViewItem(TVI_ROOT, 0)
a9c1265f
VZ
306 {
307 m_data = data;
a9c1265f
VZ
308 }
309
310 ~wxVirtualNode()
311 {
312 delete m_data;
a9c1265f
VZ
313 }
314
315 wxTreeItemData *GetData() const { return m_data; }
316 void SetData(wxTreeItemData *data) { delete m_data; m_data = data; }
317
318private:
a9c1265f 319 wxTreeItemData *m_data;
22f3361e
VZ
320
321 DECLARE_NO_COPY_CLASS(wxVirtualNode)
a9c1265f
VZ
322};
323
f3ef286f 324#ifdef __VISUALC__
197dd9af 325#pragma warning( default : 4097 )
f3ef286f 326#endif
2bda0e17 327
a9c1265f
VZ
328// a macro to get the virtual root, returns NULL if none
329#define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
330
04cd30de 331// returns true if the item is the virtual root
a9c1265f
VZ
332#define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
333
9dfbf520 334// a class which encapsulates the tree traversal logic: it vists all (unless
04cd30de 335// OnVisit() returns false) items under the given one
9dfbf520
VZ
336class wxTreeTraversal
337{
338public:
339 wxTreeTraversal(const wxTreeCtrl *tree)
340 {
341 m_tree = tree;
342 }
343
344 // do traverse the tree: visit all items (recursively by default) under the
04cd30de
RL
345 // given one; return true if all items were traversed or false if the
346 // traversal was aborted because OnVisit returned false
347 bool DoTraverse(const wxTreeItemId& root, bool recursively = true);
9dfbf520
VZ
348
349 // override this function to do whatever is needed for each item, return
04cd30de 350 // false to stop traversing
9dfbf520
VZ
351 virtual bool OnVisit(const wxTreeItemId& item) = 0;
352
353protected:
354 const wxTreeCtrl *GetTree() const { return m_tree; }
355
356private:
357 bool Traverse(const wxTreeItemId& root, bool recursively);
358
359 const wxTreeCtrl *m_tree;
22f3361e
VZ
360
361 DECLARE_NO_COPY_CLASS(wxTreeTraversal)
9dfbf520
VZ
362};
363
74b31181
VZ
364// internal class for getting the selected items
365class TraverseSelections : public wxTreeTraversal
366{
367public:
368 TraverseSelections(const wxTreeCtrl *tree,
369 wxArrayTreeItemIds& selections)
370 : wxTreeTraversal(tree), m_selections(selections)
371 {
372 m_selections.Empty();
373
374 DoTraverse(tree->GetRootItem());
375 }
376
377 virtual bool OnVisit(const wxTreeItemId& item)
378 {
a9c1265f
VZ
379 // can't visit a virtual node.
380 if ( (GetTree()->GetRootItem() == item) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT))
381 {
04cd30de 382 return true;
a9c1265f
VZ
383 }
384
3f7bc32b 385#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
74b31181 386 if ( GetTree()->IsItemChecked(item) )
3f7bc32b
VZ
387#else
388 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item)) )
389#endif
74b31181
VZ
390 {
391 m_selections.Add(item);
392 }
393
04cd30de 394 return true;
74b31181
VZ
395 }
396
e47c4d48
VZ
397 size_t GetCount() const { return m_selections.GetCount(); }
398
74b31181
VZ
399private:
400 wxArrayTreeItemIds& m_selections;
fc7a2a60
VZ
401
402 DECLARE_NO_COPY_CLASS(TraverseSelections)
74b31181
VZ
403};
404
405// internal class for counting tree items
406class TraverseCounter : public wxTreeTraversal
407{
408public:
409 TraverseCounter(const wxTreeCtrl *tree,
410 const wxTreeItemId& root,
411 bool recursively)
412 : wxTreeTraversal(tree)
413 {
414 m_count = 0;
415
416 DoTraverse(root, recursively);
417 }
418
33ac7e6f 419 virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item))
74b31181
VZ
420 {
421 m_count++;
422
04cd30de 423 return true;
74b31181
VZ
424 }
425
426 size_t GetCount() const { return m_count; }
427
428private:
429 size_t m_count;
fc7a2a60
VZ
430
431 DECLARE_NO_COPY_CLASS(TraverseCounter)
74b31181
VZ
432};
433
434// ----------------------------------------------------------------------------
435// This class is needed for support of different images: the Win32 common
436// control natively supports only 2 images (the normal one and another for the
437// selected state). We wish to provide support for 2 more of them for folder
438// items (i.e. those which have children): for expanded state and for expanded
439// selected state. For this we use this structure to store the additional items
440// images.
441//
442// There is only one problem with this: when we retrieve the item's data, we
443// don't know whether we get a pointer to wxTreeItemData or
502a2b18
VZ
444// wxTreeItemIndirectData. So we always set the item id to an invalid value
445// in this class and the code using the client data checks for it and retrieves
446// the real client data in this case.
74b31181 447// ----------------------------------------------------------------------------
696e1ea0 448
502a2b18 449class wxTreeItemIndirectData : public wxTreeItemData
74b31181
VZ
450{
451public:
452 // ctor associates this data with the item and the real item data becomes
453 // available through our GetData() method
454 wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item)
455 {
456 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
457 {
458 m_images[n] = -1;
459 }
460
461 // save the old data
462 m_data = tree->GetItemData(item);
463
464 // and set ourselves as the new one
465 tree->SetIndirectItemData(item, this);
cf48ccc0
VZ
466
467 // we must have the invalid value for the item
468 m_pItem = 0l;
74b31181
VZ
469 }
470
471 // dtor deletes the associated data as well
502a2b18 472 virtual ~wxTreeItemIndirectData() { delete m_data; }
74b31181
VZ
473
474 // accessors
475 // get the real data associated with the item
476 wxTreeItemData *GetData() const { return m_data; }
477 // change it
478 void SetData(wxTreeItemData *data) { m_data = data; }
479
480 // do we have such image?
481 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
482 // get image
483 int GetImage(wxTreeItemIcon which) const { return m_images[which]; }
484 // change it
485 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
486
487private:
488 // all the images associated with the item
489 int m_images[wxTreeItemIcon_Max];
490
502a2b18 491 // the real client data
74b31181 492 wxTreeItemData *m_data;
22f3361e
VZ
493
494 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData)
74b31181
VZ
495};
496
23f681ec 497// ----------------------------------------------------------------------------
3f7bc32b 498// wxWin macros
08b7c251
VZ
499// ----------------------------------------------------------------------------
500
23f681ec 501IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
2bda0e17 502
08b7c251 503// ----------------------------------------------------------------------------
deb1de30 504// constants
08b7c251
VZ
505// ----------------------------------------------------------------------------
506
deb1de30
VZ
507// indices in gs_expandEvents table below
508enum
509{
510 IDX_COLLAPSE,
511 IDX_EXPAND,
512 IDX_WHAT_MAX
513};
514
515enum
516{
517 IDX_DONE,
518 IDX_DOING,
519 IDX_HOW_MAX
520};
521
522// handy table for sending events - it has to be initialized during run-time
523// now so can't be const any more
524static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX];
525
526/*
527 but logically it's a const table with the following entries:
528=
2bda0e17 529{
08b7c251
VZ
530 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
531 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
532};
deb1de30 533*/
08b7c251
VZ
534
535// ============================================================================
536// implementation
537// ============================================================================
538
9dfbf520
VZ
539// ----------------------------------------------------------------------------
540// tree traversal
541// ----------------------------------------------------------------------------
542
543bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
544{
545 if ( !OnVisit(root) )
04cd30de 546 return false;
9dfbf520
VZ
547
548 return Traverse(root, recursively);
549}
550
551bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
552{
ee4b2721 553 wxTreeItemIdValue cookie;
9dfbf520
VZ
554 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
555 while ( child.IsOk() )
556 {
557 // depth first traversal
04cd30de
RL
558 if ( recursively && !Traverse(child, true) )
559 return false;
9dfbf520
VZ
560
561 if ( !OnVisit(child) )
04cd30de 562 return false;
9dfbf520
VZ
563
564 child = m_tree->GetNextChild(root, cookie);
565 }
566
04cd30de 567 return true;
9dfbf520
VZ
568}
569
08b7c251
VZ
570// ----------------------------------------------------------------------------
571// construction and destruction
572// ----------------------------------------------------------------------------
573
574void wxTreeCtrl::Init()
575{
576 m_imageListNormal = NULL;
577 m_imageListState = NULL;
04cd30de 578 m_ownsImageListNormal = m_ownsImageListState = false;
08b7c251 579 m_textCtrl = NULL;
04cd30de 580 m_hasAnyAttr = false;
23f681ec 581 m_dragImage = NULL;
a9c1265f 582 m_pVirtualRoot = NULL;
9ab5ef13 583
deb1de30
VZ
584 // initialize the global array of events now as it can't be done statically
585 // with the wxEVT_XXX values being allocated during run-time only
586 gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED;
587 gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING;
588 gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED;
589 gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING;
2bda0e17
KB
590}
591
9dfbf520
VZ
592bool wxTreeCtrl::Create(wxWindow *parent,
593 wxWindowID id,
594 const wxPoint& pos,
595 const wxSize& size,
596 long style,
597 const wxValidator& validator,
08b7c251 598 const wxString& name)
2bda0e17 599{
08b7c251 600 Init();
2bda0e17 601
7699361c
JS
602 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
603 style |= wxBORDER_SUNKEN;
604
9dfbf520 605 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
04cd30de 606 return false;
2bda0e17 607
7699361c
JS
608 DWORD exStyle = 0;
609 DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle);
610 wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS;
2bda0e17 611
63da7df7
JS
612 if ((m_windowStyle & wxTR_NO_LINES) == 0)
613 wstyle |= TVS_HASLINES;
08b7c251
VZ
614 if ( m_windowStyle & wxTR_HAS_BUTTONS )
615 wstyle |= TVS_HASBUTTONS;
2bda0e17 616
08b7c251
VZ
617 if ( m_windowStyle & wxTR_EDIT_LABELS )
618 wstyle |= TVS_EDITLABELS;
2bda0e17 619
08b7c251
VZ
620 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
621 wstyle |= TVS_LINESATROOT;
deb1de30 622
c6f4913a 623 if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT )
deb1de30 624 {
c6f4913a
VS
625 if ( wxTheApp->GetComCtl32Version() >= 471 )
626 wstyle |= TVS_FULLROWSELECT;
627 }
628
3f7bc32b
VZ
629 // using TVS_CHECKBOXES for emulation of a multiselection tree control
630 // doesn't work without the new enough headers
631#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
632 !defined( __GNUWIN32_OLD__ ) && \
c42404a5
VZ
633 !defined( __BORLANDC__ ) && \
634 !defined( __WATCOMC__ ) && \
635 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
a20a10fe 636
9dfbf520
VZ
637 // we emulate the multiple selection tree controls by using checkboxes: set
638 // up the image list we need for this if we do have multiple selections
639 if ( m_windowStyle & wxTR_MULTIPLE )
10fcf31a 640 wstyle |= TVS_CHECKBOXES;
3f7bc32b 641#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520 642
08b7c251 643 // Create the tree control.
9dfbf520 644 if ( !MSWCreateControl(WC_TREEVIEW, wstyle) )
04cd30de 645 return false;
9dfbf520 646
f6bcfd97 647#if wxUSE_COMCTL32_SAFELY
a756f210 648 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97
BP
649 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
650#elif 1
a756f210 651 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97 652 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
fbdcff4a
JS
653#else
654 // This works around a bug in the Windows tree control whereby for some versions
655 // of comctrl32, setting any colour actually draws the background in black.
656 // This will initialise the background to the system colour.
f6bcfd97
BP
657 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
658 // Assume the user has an updated comctl32.dll.
fbdcff4a 659 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1);
a756f210 660 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97 661 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
fbdcff4a
JS
662#endif
663
5aeeab14 664
9dfbf520
VZ
665 // VZ: this is some experimental code which may be used to get the
666 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
667 // AFAIK, the standard DLL does about the same thing anyhow.
668#if 0
669 if ( m_windowStyle & wxTR_MULTIPLE )
670 {
671 wxBitmap bmp;
672
673 // create the DC compatible with the current screen
674 HDC hdcMem = CreateCompatibleDC(NULL);
675
676 // create a mono bitmap of the standard size
677 int x = GetSystemMetrics(SM_CXMENUCHECK);
678 int y = GetSystemMetrics(SM_CYMENUCHECK);
04cd30de 679 wxImageList imagelistCheckboxes(x, y, false, 2);
9dfbf520
VZ
680 HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size
681 1, // # of color planes
682 1, // # bits needed for one pixel
683 0); // array containing colour data
684 SelectObject(hdcMem, hbmpCheck);
685
686 // then draw a check mark into it
687 RECT rect = { 0, 0, x, y };
688 if ( !::DrawFrameControl(hdcMem, &rect,
689 DFC_BUTTON,
690 DFCS_BUTTONCHECK | DFCS_CHECKED) )
691 {
223d09f6 692 wxLogLastError(wxT("DrawFrameControl(check)"));
9dfbf520
VZ
693 }
694
695 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
696 imagelistCheckboxes.Add(bmp);
697
698 if ( !::DrawFrameControl(hdcMem, &rect,
699 DFC_BUTTON,
700 DFCS_BUTTONCHECK) )
701 {
223d09f6 702 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
9dfbf520
VZ
703 }
704
705 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
706 imagelistCheckboxes.Add(bmp);
707
708 // clean up
709 ::DeleteDC(hdcMem);
710
711 // set the imagelist
712 SetStateImageList(&imagelistCheckboxes);
713 }
714#endif // 0
715
716 SetSize(pos.x, pos.y, size.x, size.y);
2bda0e17 717
04cd30de 718 return true;
2bda0e17
KB
719}
720
08b7c251 721wxTreeCtrl::~wxTreeCtrl()
2bda0e17 722{
696e1ea0
VZ
723 // delete any attributes
724 if ( m_hasAnyAttr )
725 {
ee4b2721 726 WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs);
696e1ea0
VZ
727
728 // prevent TVN_DELETEITEM handler from deleting the attributes again!
04cd30de 729 m_hasAnyAttr = false;
696e1ea0
VZ
730 }
731
08b7c251 732 DeleteTextCtrl();
2bda0e17 733
08b7c251 734 // delete user data to prevent memory leaks
a9c1265f 735 // also deletes hidden root node storage.
08b7c251 736 DeleteAllItems();
33ac7e6f 737
0377efa2
VS
738 if (m_ownsImageListNormal) delete m_imageListNormal;
739 if (m_ownsImageListState) delete m_imageListState;
2bda0e17
KB
740}
741
08b7c251
VZ
742// ----------------------------------------------------------------------------
743// accessors
744// ----------------------------------------------------------------------------
2bda0e17 745
08b7c251 746// simple wrappers which add error checking in debug mode
2bda0e17 747
08b7c251 748bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
2bda0e17 749{
04cd30de 750 wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false,
a9c1265f
VZ
751 _T("can't retrieve virtual root item") );
752
d220ae32 753 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
2bda0e17 754 {
f6bcfd97 755 wxLogLastError(wxT("TreeView_GetItem"));
08b7c251 756
04cd30de 757 return false;
08b7c251
VZ
758 }
759
04cd30de 760 return true;
2bda0e17
KB
761}
762
08b7c251 763void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
2bda0e17 764{
d220ae32 765 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
2bda0e17 766 {
f6bcfd97 767 wxLogLastError(wxT("TreeView_SetItem"));
08b7c251 768 }
2bda0e17
KB
769}
770
08b7c251 771size_t wxTreeCtrl::GetCount() const
2bda0e17 772{
d220ae32 773 return (size_t)TreeView_GetCount(GetHwnd());
2bda0e17
KB
774}
775
08b7c251 776unsigned int wxTreeCtrl::GetIndent() const
2bda0e17 777{
d220ae32 778 return TreeView_GetIndent(GetHwnd());
2bda0e17
KB
779}
780
08b7c251 781void wxTreeCtrl::SetIndent(unsigned int indent)
2bda0e17 782{
d220ae32 783 TreeView_SetIndent(GetHwnd(), indent);
2bda0e17
KB
784}
785
08b7c251 786wxImageList *wxTreeCtrl::GetImageList() const
2bda0e17 787{
08b7c251 788 return m_imageListNormal;
2bda0e17
KB
789}
790
08b7c251 791wxImageList *wxTreeCtrl::GetStateImageList() const
2bda0e17 792{
8a000b6b 793 return m_imageListState;
2bda0e17
KB
794}
795
08b7c251 796void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
2bda0e17 797{
08b7c251 798 // no error return
d220ae32 799 TreeView_SetImageList(GetHwnd(),
08b7c251
VZ
800 imageList ? imageList->GetHIMAGELIST() : 0,
801 which);
2bda0e17
KB
802}
803
08b7c251 804void wxTreeCtrl::SetImageList(wxImageList *imageList)
2bda0e17 805{
a9c1265f
VZ
806 if (m_ownsImageListNormal)
807 delete m_imageListNormal;
808
08b7c251 809 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
04cd30de 810 m_ownsImageListNormal = false;
2bda0e17
KB
811}
812
08b7c251 813void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
2bda0e17 814{
0377efa2 815 if (m_ownsImageListState) delete m_imageListState;
08b7c251 816 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
04cd30de 817 m_ownsImageListState = false;
0377efa2
VS
818}
819
820void wxTreeCtrl::AssignImageList(wxImageList *imageList)
821{
822 SetImageList(imageList);
04cd30de 823 m_ownsImageListNormal = true;
0377efa2
VS
824}
825
826void wxTreeCtrl::AssignStateImageList(wxImageList *imageList)
827{
828 SetStateImageList(imageList);
04cd30de 829 m_ownsImageListState = true;
2bda0e17
KB
830}
831
33961d59
RR
832size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
833 bool recursively) const
834{
835 TraverseCounter counter(this, item, recursively);
23fd5130 836
73974df1 837 return counter.GetCount() - 1;
23fd5130
VZ
838}
839
bb448552
VZ
840// ----------------------------------------------------------------------------
841// control colours
842// ----------------------------------------------------------------------------
843
844bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
845{
f6bcfd97 846#if !wxUSE_COMCTL32_SAFELY
bb448552 847 if ( !wxWindowBase::SetBackgroundColour(colour) )
04cd30de 848 return false;
bb448552
VZ
849
850 SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
f6bcfd97 851#endif
bb448552 852
04cd30de 853 return true;
bb448552
VZ
854}
855
856bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
857{
f6bcfd97 858#if !wxUSE_COMCTL32_SAFELY
bb448552 859 if ( !wxWindowBase::SetForegroundColour(colour) )
04cd30de 860 return false;
bb448552
VZ
861
862 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
f6bcfd97 863#endif
bb448552 864
04cd30de 865 return true;
bb448552
VZ
866}
867
08b7c251
VZ
868// ----------------------------------------------------------------------------
869// Item access
870// ----------------------------------------------------------------------------
871
872wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
2bda0e17 873{
837e5743 874 wxChar buf[512]; // the size is arbitrary...
02ce7b72 875
08b7c251
VZ
876 wxTreeViewItem tvItem(item, TVIF_TEXT);
877 tvItem.pszText = buf;
878 tvItem.cchTextMax = WXSIZEOF(buf);
879 if ( !DoGetItem(&tvItem) )
880 {
881 // don't return some garbage which was on stack, but an empty string
223d09f6 882 buf[0] = wxT('\0');
08b7c251 883 }
2bda0e17 884
08b7c251
VZ
885 return wxString(buf);
886}
2bda0e17 887
08b7c251
VZ
888void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
889{
a9c1265f
VZ
890 if ( IS_VIRTUAL_ROOT(item) )
891 return;
892
08b7c251 893 wxTreeViewItem tvItem(item, TVIF_TEXT);
837e5743 894 tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
08b7c251 895 DoSetItem(&tvItem);
44fbc477
VZ
896
897 // when setting the text of the item being edited, the text control should
898 // be updated to reflect the new text as well, otherwise calling
899 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
900 //
901 // don't use GetEditControl() here because m_textCtrl is not set yet
902 HWND hwndEdit = TreeView_GetEditControl(GetHwnd());
903 if ( hwndEdit )
904 {
905 if ( item == GetSelection() )
906 {
907 ::SetWindowText(hwndEdit, text);
908 }
909 }
08b7c251 910}
2bda0e17 911
74b31181
VZ
912int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
913 wxTreeItemIcon which) const
914{
915 wxTreeViewItem tvItem(item, TVIF_PARAM);
916 if ( !DoGetItem(&tvItem) )
917 {
918 return -1;
919 }
920
921 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
922}
923
924void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
925 int image,
926 wxTreeItemIcon which) const
927{
928 wxTreeViewItem tvItem(item, TVIF_PARAM);
929 if ( !DoGetItem(&tvItem) )
930 {
931 return;
932 }
933
934 wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
935
936 data->SetImage(image, which);
937
938 // make sure that we have selected images as well
939 if ( which == wxTreeItemIcon_Normal &&
940 !data->HasImage(wxTreeItemIcon_Selected) )
941 {
942 data->SetImage(image, wxTreeItemIcon_Selected);
943 }
944
945 if ( which == wxTreeItemIcon_Expanded &&
946 !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
947 {
948 data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
949 }
950}
951
9dfbf520
VZ
952void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
953 int image,
954 int imageSel)
955{
956 wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
957 tvItem.iSelectedImage = imageSel;
958 tvItem.iImage = image;
959 DoSetItem(&tvItem);
960}
961
74b31181
VZ
962int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
963 wxTreeItemIcon which) const
08b7c251 964{
a9c1265f
VZ
965 if ( (HITEM(item) == TVI_ROOT) && (m_windowStyle & wxTR_HIDE_ROOT) )
966 {
967 // TODO: Maybe a hidden root can still provide images?
968 return -1;
969 }
970
74b31181
VZ
971 if ( HasIndirectData(item) )
972 {
973 return DoGetItemImageFromData(item, which);
974 }
2bda0e17 975
74b31181
VZ
976 UINT mask;
977 switch ( which )
978 {
979 default:
223d09f6 980 wxFAIL_MSG( wxT("unknown tree item image type") );
2bda0e17 981
74b31181
VZ
982 case wxTreeItemIcon_Normal:
983 mask = TVIF_IMAGE;
984 break;
2bda0e17 985
74b31181
VZ
986 case wxTreeItemIcon_Selected:
987 mask = TVIF_SELECTEDIMAGE;
988 break;
989
990 case wxTreeItemIcon_Expanded:
991 case wxTreeItemIcon_SelectedExpanded:
992 return -1;
993 }
994
995 wxTreeViewItem tvItem(item, mask);
08b7c251 996 DoGetItem(&tvItem);
2bda0e17 997
74b31181 998 return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
2bda0e17
KB
999}
1000
74b31181
VZ
1001void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
1002 wxTreeItemIcon which)
2bda0e17 1003{
a9c1265f
VZ
1004 if ( IS_VIRTUAL_ROOT(item) )
1005 {
1006 // TODO: Maybe a hidden root can still store images?
1007 return;
1008 }
1009
7ab0c3ad
VZ
1010 int imageNormal,
1011 imageSel;
1012
74b31181
VZ
1013 switch ( which )
1014 {
1015 default:
223d09f6 1016 wxFAIL_MSG( wxT("unknown tree item image type") );
7ab0c3ad 1017 // fall through
74b31181
VZ
1018
1019 case wxTreeItemIcon_Normal:
7ab0c3ad
VZ
1020 {
1021 const int imageNormalOld = GetItemImage(item);
b5f6b52a
MB
1022 const int imageSelOld =
1023 GetItemImage(item, wxTreeItemIcon_Selected);
7ab0c3ad
VZ
1024
1025 // always set the normal image
1026 imageNormal = image;
1027
1028 // if the selected and normal images were the same, they should
1029 // be the same after the update, otherwise leave the selected
1030 // image as it was
1031 imageSel = imageNormalOld == imageSelOld ? image : imageSelOld;
1032 }
74b31181
VZ
1033 break;
1034
1035 case wxTreeItemIcon_Selected:
1036 imageNormal = GetItemImage(item);
1037 imageSel = image;
1038 break;
1039
1040 case wxTreeItemIcon_Expanded:
1041 case wxTreeItemIcon_SelectedExpanded:
1042 if ( !HasIndirectData(item) )
1043 {
1044 // we need to get the old images first, because after we create
1045 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1046 // get the images
1047 imageNormal = GetItemImage(item);
b5f6b52a 1048 imageSel = GetItemImage(item, wxTreeItemIcon_Selected);
74b31181
VZ
1049
1050 // if it doesn't have it yet, add it
1051 wxTreeItemIndirectData *data = new
1052 wxTreeItemIndirectData(this, item);
1053
1054 // copy the data to the new location
1055 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
1056 data->SetImage(imageSel, wxTreeItemIcon_Selected);
1057 }
1058
1059 DoSetItemImageFromData(item, image, which);
1060
1061 // reset the normal/selected images because we won't use them any
1062 // more - now they're stored inside the indirect data
1063 imageNormal =
1064 imageSel = I_IMAGECALLBACK;
1065 break;
1066 }
1067
9dfbf520
VZ
1068 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1069 // change both normal and selected image - otherwise the change simply
1070 // doesn't take place!
74b31181 1071 DoSetItemImages(item, imageNormal, imageSel);
2bda0e17
KB
1072}
1073
08b7c251 1074wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
2bda0e17 1075{
08b7c251 1076 wxTreeViewItem tvItem(item, TVIF_PARAM);
a9c1265f
VZ
1077
1078 // Hidden root may have data.
1079 if ( IS_VIRTUAL_ROOT(item) )
1080 {
1081 return GET_VIRTUAL_ROOT()->GetData();
1082 }
1083
1084 // Visible node.
08b7c251
VZ
1085 if ( !DoGetItem(&tvItem) )
1086 {
1087 return NULL;
1088 }
2bda0e17 1089
502a2b18
VZ
1090 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1091 if ( IsDataIndirect(data) )
74b31181 1092 {
502a2b18 1093 data = ((wxTreeItemIndirectData *)data)->GetData();
74b31181 1094 }
502a2b18
VZ
1095
1096 return data;
2bda0e17
KB
1097}
1098
08b7c251 1099void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
2bda0e17 1100{
a9c1265f
VZ
1101 if ( IS_VIRTUAL_ROOT(item) )
1102 {
1103 GET_VIRTUAL_ROOT()->SetData(data);
1104 }
1105
188781db
VZ
1106 // first, associate this piece of data with this item
1107 if ( data )
1108 {
1109 data->SetId(item);
1110 }
1111
08b7c251 1112 wxTreeViewItem tvItem(item, TVIF_PARAM);
74b31181
VZ
1113
1114 if ( HasIndirectData(item) )
1115 {
1116 if ( DoGetItem(&tvItem) )
1117 {
1118 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
1119 }
1120 else
1121 {
223d09f6 1122 wxFAIL_MSG( wxT("failed to change tree items data") );
74b31181
VZ
1123 }
1124 }
1125 else
1126 {
1127 tvItem.lParam = (LPARAM)data;
1128 DoSetItem(&tvItem);
1129 }
1130}
1131
1132void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
1133 wxTreeItemIndirectData *data)
1134{
1135 // this should never happen because it's unnecessary and will probably lead
1136 // to crash too because the code elsewhere supposes that the pointer the
1137 // wxTreeItemIndirectData has is a real wxItemData and not
1138 // wxTreeItemIndirectData as well
223d09f6 1139 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
74b31181 1140
502a2b18 1141 SetItemData(item, data);
74b31181
VZ
1142}
1143
1144bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
1145{
502a2b18
VZ
1146 // query the item itself
1147 wxTreeViewItem tvItem(item, TVIF_PARAM);
1148 if ( !DoGetItem(&tvItem) )
1149 {
04cd30de 1150 return false;
502a2b18
VZ
1151 }
1152
1153 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1154
1155 return data && IsDataIndirect(data);
08b7c251 1156}
2bda0e17 1157
3a5a2f56
VZ
1158void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1159{
a9c1265f
VZ
1160 if ( IS_VIRTUAL_ROOT(item) )
1161 return;
1162
3a5a2f56
VZ
1163 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1164 tvItem.cChildren = (int)has;
1165 DoSetItem(&tvItem);
1166}
1167
add28c55
VZ
1168void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1169{
a9c1265f
VZ
1170 if ( IS_VIRTUAL_ROOT(item) )
1171 return;
1172
add28c55
VZ
1173 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1174 tvItem.state = bold ? TVIS_BOLD : 0;
1175 DoSetItem(&tvItem);
1176}
1177
58a8ab88
JS
1178void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
1179{
a9c1265f
VZ
1180 if ( IS_VIRTUAL_ROOT(item) )
1181 return;
1182
58a8ab88
JS
1183 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
1184 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
1185 DoSetItem(&tvItem);
1186}
1187
d00407b2
VZ
1188void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
1189{
a9c1265f
VZ
1190 if ( IS_VIRTUAL_ROOT(item) )
1191 return;
1192
d00407b2
VZ
1193 wxRect rect;
1194 if ( GetBoundingRect(item, rect) )
1195 {
1196 RefreshRect(rect);
1197 }
1198}
1199
2b5f62a0
VZ
1200wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
1201{
f888d614 1202 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
2b5f62a0 1203
ee4b2721 1204 return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour();
2b5f62a0
VZ
1205}
1206
1207wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
1208{
f888d614 1209 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
2b5f62a0 1210
ee4b2721 1211 return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour();
2b5f62a0
VZ
1212}
1213
1214wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
1215{
f888d614 1216 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
2b5f62a0 1217
ee4b2721 1218 return it == m_attrs.end() ? wxNullFont : it->second->GetFont();
2b5f62a0
VZ
1219}
1220
696e1ea0
VZ
1221void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1222 const wxColour& col)
1223{
ee4b2721 1224 wxTreeItemAttr *attr;
f888d614 1225 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1226 if ( it == m_attrs.end() )
696e1ea0 1227 {
ee4b2721
VZ
1228 m_hasAnyAttr = true;
1229
f888d614 1230 m_attrs[item.m_pItem] =
696e1ea0 1231 attr = new wxTreeItemAttr;
ee4b2721
VZ
1232 }
1233 else
1234 {
1235 attr = it->second;
696e1ea0
VZ
1236 }
1237
1238 attr->SetTextColour(col);
d00407b2
VZ
1239
1240 RefreshItem(item);
696e1ea0
VZ
1241}
1242
1243void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1244 const wxColour& col)
1245{
ee4b2721 1246 wxTreeItemAttr *attr;
f888d614 1247 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1248 if ( it == m_attrs.end() )
696e1ea0 1249 {
ee4b2721
VZ
1250 m_hasAnyAttr = true;
1251
f888d614 1252 m_attrs[item.m_pItem] =
696e1ea0 1253 attr = new wxTreeItemAttr;
ee4b2721
VZ
1254 }
1255 else // already in the hash
1256 {
1257 attr = it->second;
696e1ea0
VZ
1258 }
1259
1260 attr->SetBackgroundColour(col);
d00407b2
VZ
1261
1262 RefreshItem(item);
696e1ea0
VZ
1263}
1264
1265void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1266{
ee4b2721 1267 wxTreeItemAttr *attr;
f888d614 1268 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1269 if ( it == m_attrs.end() )
696e1ea0 1270 {
ee4b2721
VZ
1271 m_hasAnyAttr = true;
1272
f888d614 1273 m_attrs[item.m_pItem] =
696e1ea0 1274 attr = new wxTreeItemAttr;
ee4b2721
VZ
1275 }
1276 else // already in the hash
1277 {
1278 attr = it->second;
696e1ea0
VZ
1279 }
1280
1281 attr->SetFont(font);
d00407b2
VZ
1282
1283 RefreshItem(item);
696e1ea0
VZ
1284}
1285
08b7c251
VZ
1286// ----------------------------------------------------------------------------
1287// Item status
1288// ----------------------------------------------------------------------------
2bda0e17 1289
08b7c251
VZ
1290bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1291{
2b5f62a0
VZ
1292 if ( item == wxTreeItemId(TVI_ROOT) )
1293 {
1294 // virtual (hidden) root is never visible
04cd30de 1295 return false;
2b5f62a0
VZ
1296 }
1297
add28c55 1298 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
08b7c251 1299 RECT rect;
955be36c
VZ
1300
1301 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1302 // the HTREEITEM with TVM_GETITEMRECT
ee4b2721 1303 *(HTREEITEM *)&rect = HITEM(item);
955be36c 1304
04cd30de
RL
1305 // false means get item rect for the whole item, not only text
1306 return SendMessage(GetHwnd(), TVM_GETITEMRECT, false, (LPARAM)&rect) != 0;
2bda0e17
KB
1307}
1308
08b7c251 1309bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
2bda0e17 1310{
08b7c251
VZ
1311 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1312 DoGetItem(&tvItem);
2bda0e17 1313
08b7c251 1314 return tvItem.cChildren != 0;
2bda0e17
KB
1315}
1316
08b7c251 1317bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
2bda0e17 1318{
08b7c251
VZ
1319 // probably not a good idea to put it here
1320 //wxASSERT( ItemHasChildren(item) );
2bda0e17 1321
08b7c251
VZ
1322 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1323 DoGetItem(&tvItem);
2bda0e17 1324
08b7c251 1325 return (tvItem.state & TVIS_EXPANDED) != 0;
2bda0e17
KB
1326}
1327
08b7c251 1328bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
2bda0e17 1329{
08b7c251
VZ
1330 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1331 DoGetItem(&tvItem);
2bda0e17 1332
08b7c251 1333 return (tvItem.state & TVIS_SELECTED) != 0;
2bda0e17
KB
1334}
1335
add28c55
VZ
1336bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1337{
1338 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1339 DoGetItem(&tvItem);
1340
1341 return (tvItem.state & TVIS_BOLD) != 0;
1342}
1343
08b7c251
VZ
1344// ----------------------------------------------------------------------------
1345// navigation
1346// ----------------------------------------------------------------------------
2bda0e17 1347
08b7c251
VZ
1348wxTreeItemId wxTreeCtrl::GetRootItem() const
1349{
a9c1265f
VZ
1350 // Root may be real (visible) or virtual (hidden).
1351 if ( GET_VIRTUAL_ROOT() )
1352 return TVI_ROOT;
1353
ee4b2721 1354 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
08b7c251 1355}
2bda0e17 1356
08b7c251
VZ
1357wxTreeItemId wxTreeCtrl::GetSelection() const
1358{
f888d614 1359 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxTreeItemId(),
223d09f6 1360 wxT("this only works with single selection controls") );
9dfbf520 1361
ee4b2721 1362 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2bda0e17
KB
1363}
1364
99006e44 1365wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const
2bda0e17 1366{
c9b142c9
VZ
1367 HTREEITEM hItem;
1368
1369 if ( IS_VIRTUAL_ROOT(item) )
1370 {
1371 // no parent for the virtual root
1372 hItem = 0;
1373 }
1374 else // normal item
a9c1265f 1375 {
c9b142c9
VZ
1376 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1377 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1378 {
1379 // the top level items should have the virtual root as their parent
1380 hItem = TVI_ROOT;
1381 }
a9c1265f
VZ
1382 }
1383
ee4b2721 1384 return wxTreeItemId(hItem);
08b7c251 1385}
2bda0e17 1386
08b7c251 1387wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
ee4b2721 1388 wxTreeItemIdValue& cookie) const
08b7c251
VZ
1389{
1390 // remember the last child returned in 'cookie'
ee4b2721
VZ
1391 cookie = TreeView_GetChild(GetHwnd(), HITEM(item));
1392
1393 return wxTreeItemId(cookie);
1394}
1395
1396wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1397 wxTreeItemIdValue& cookie) const
1398{
1399 wxTreeItemId item(TreeView_GetNextSibling(GetHwnd(),
1400 HITEM(wxTreeItemId(cookie))));
1401 cookie = item.m_pItem;
1402
1403 return item;
1404}
2bda0e17 1405
ee4b2721
VZ
1406#if WXWIN_COMPATIBILITY_2_4
1407
1408wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1409 long& cookie) const
1410{
1411 cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item));
1412
1413 return wxTreeItemId((void *)cookie);
2bda0e17
KB
1414}
1415
08b7c251 1416wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
ee4b2721 1417 long& cookie) const
2bda0e17 1418{
ee4b2721
VZ
1419 wxTreeItemId item(TreeView_GetNextSibling
1420 (
1421 GetHwnd(),
1422 HITEM(wxTreeItemId((void *)cookie)
1423 )));
1424 cookie = (long)item.m_pItem;
23fd5130 1425
ee4b2721 1426 return item;
08b7c251 1427}
2bda0e17 1428
ee4b2721
VZ
1429#endif // WXWIN_COMPATIBILITY_2_4
1430
978f38c2
VZ
1431wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1432{
1433 // can this be done more efficiently?
ee4b2721 1434 wxTreeItemIdValue cookie;
978f38c2
VZ
1435
1436 wxTreeItemId childLast,
2165ad93 1437 child = GetFirstChild(item, cookie);
978f38c2
VZ
1438 while ( child.IsOk() )
1439 {
1440 childLast = child;
2165ad93 1441 child = GetNextChild(item, cookie);
978f38c2
VZ
1442 }
1443
1444 return childLast;
1445}
1446
08b7c251
VZ
1447wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1448{
ee4b2721 1449 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1450}
1451
08b7c251 1452wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
2bda0e17 1453{
ee4b2721 1454 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1455}
1456
08b7c251 1457wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
2bda0e17 1458{
ee4b2721 1459 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
2bda0e17
KB
1460}
1461
08b7c251 1462wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
2bda0e17 1463{
f6bcfd97 1464 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
02ce7b72 1465
ee4b2721 1466 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
08b7c251 1467}
02ce7b72 1468
08b7c251
VZ
1469wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1470{
f6bcfd97 1471 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
02ce7b72 1472
ee4b2721 1473 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
08b7c251 1474}
02ce7b72 1475
9dfbf520
VZ
1476// ----------------------------------------------------------------------------
1477// multiple selections emulation
1478// ----------------------------------------------------------------------------
1479
1480bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
1481{
1482 // receive the desired information.
1483 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1484 DoGetItem(&tvItem);
1485
1486 // state image indices are 1 based
1487 return ((tvItem.state >> 12) - 1) == 1;
1488}
1489
1490void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
1491{
1492 // receive the desired information.
1493 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1494
a9c1265f
VZ
1495 DoGetItem(&tvItem);
1496
9dfbf520
VZ
1497 // state images are one-based
1498 tvItem.state = (check ? 2 : 1) << 12;
1499
1500 DoSetItem(&tvItem);
1501}
1502
33961d59
RR
1503size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1504{
1505 TraverseSelections selector(this, selections);
9dfbf520 1506
e47c4d48 1507 return selector.GetCount();
9dfbf520
VZ
1508}
1509
08b7c251
VZ
1510// ----------------------------------------------------------------------------
1511// Usual operations
1512// ----------------------------------------------------------------------------
02ce7b72 1513
08b7c251
VZ
1514wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1515 wxTreeItemId hInsertAfter,
1516 const wxString& text,
1517 int image, int selectedImage,
1518 wxTreeItemData *data)
1519{
0e935169
VZ
1520 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1521 wxTreeItemId(),
1522 _T("can't have more than one root in the tree") );
1523
08b7c251 1524 TV_INSERTSTRUCT tvIns;
3f7bc32b
VZ
1525 tvIns.hParent = HITEM(parent);
1526 tvIns.hInsertAfter = HITEM(hInsertAfter);
58a8ab88 1527
74b31181
VZ
1528 // this is how we insert the item as the first child: supply a NULL
1529 // hInsertAfter
1530 if ( !tvIns.hInsertAfter )
58a8ab88
JS
1531 {
1532 tvIns.hInsertAfter = TVI_FIRST;
1533 }
1534
08b7c251
VZ
1535 UINT mask = 0;
1536 if ( !text.IsEmpty() )
1537 {
1538 mask |= TVIF_TEXT;
837e5743 1539 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
08b7c251 1540 }
f6bcfd97
BP
1541 else
1542 {
1543 tvIns.item.pszText = NULL;
1544 tvIns.item.cchTextMax = 0;
1545 }
02ce7b72 1546
08b7c251
VZ
1547 if ( image != -1 )
1548 {
1549 mask |= TVIF_IMAGE;
1550 tvIns.item.iImage = image;
3a5a2f56 1551
6b037754 1552 if ( selectedImage == -1 )
3a5a2f56
VZ
1553 {
1554 // take the same image for selected icon if not specified
1555 selectedImage = image;
1556 }
08b7c251 1557 }
02ce7b72 1558
08b7c251
VZ
1559 if ( selectedImage != -1 )
1560 {
1561 mask |= TVIF_SELECTEDIMAGE;
1562 tvIns.item.iSelectedImage = selectedImage;
1563 }
02ce7b72 1564
08b7c251
VZ
1565 if ( data != NULL )
1566 {
1567 mask |= TVIF_PARAM;
1568 tvIns.item.lParam = (LPARAM)data;
1569 }
02ce7b72 1570
08b7c251 1571 tvIns.item.mask = mask;
02ce7b72 1572
d220ae32 1573 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
08b7c251
VZ
1574 if ( id == 0 )
1575 {
f6bcfd97 1576 wxLogLastError(wxT("TreeView_InsertItem"));
08b7c251 1577 }
02ce7b72 1578
fd3f686c
VZ
1579 if ( data != NULL )
1580 {
1581 // associate the application tree item with Win32 tree item handle
ee4b2721 1582 data->SetId(id);
fd3f686c
VZ
1583 }
1584
ee4b2721 1585 return wxTreeItemId(id);
2bda0e17
KB
1586}
1587
08b7c251 1588// for compatibility only
ee4b2721
VZ
1589#if WXWIN_COMPATIBILITY_2_4
1590
08b7c251
VZ
1591wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1592 const wxString& text,
1593 int image, int selImage,
1594 long insertAfter)
2bda0e17 1595{
ee4b2721 1596 return DoInsertItem(parent, wxTreeItemId((void *)insertAfter), text,
08b7c251 1597 image, selImage, NULL);
2bda0e17
KB
1598}
1599
ee4b2721
VZ
1600#endif // WXWIN_COMPATIBILITY_2_4
1601
08b7c251
VZ
1602wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1603 int image, int selectedImage,
1604 wxTreeItemData *data)
2bda0e17 1605{
a9c1265f
VZ
1606
1607 if ( m_windowStyle & wxTR_HIDE_ROOT )
1608 {
1609 // create a virtual root item, the parent for all the others
1610 m_pVirtualRoot = new wxVirtualNode(data);
1611
1612 return TVI_ROOT;
1613 }
1614
f888d614 1615 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
08b7c251 1616 text, image, selectedImage, data);
2bda0e17
KB
1617}
1618
08b7c251
VZ
1619wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1620 const wxString& text,
1621 int image, int selectedImage,
1622 wxTreeItemData *data)
2bda0e17 1623{
ee4b2721 1624 return DoInsertItem(parent, TVI_FIRST,
08b7c251 1625 text, image, selectedImage, data);
2bda0e17
KB
1626}
1627
08b7c251
VZ
1628wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1629 const wxTreeItemId& idPrevious,
1630 const wxString& text,
1631 int image, int selectedImage,
1632 wxTreeItemData *data)
2bda0e17 1633{
08b7c251 1634 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
2bda0e17
KB
1635}
1636
2ef31e80
VZ
1637wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1638 size_t index,
1639 const wxString& text,
1640 int image, int selectedImage,
1641 wxTreeItemData *data)
1642{
1643 // find the item from index
ee4b2721 1644 wxTreeItemIdValue cookie;
2ef31e80
VZ
1645 wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie);
1646 while ( index != 0 && idCur.IsOk() )
1647 {
1648 index--;
1649
1650 idPrev = idCur;
1651 idCur = GetNextChild(parent, cookie);
1652 }
1653
1654 // assert, not check: if the index is invalid, we will append the item
1655 // to the end
1656 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1657
1658 return DoInsertItem(parent, idPrev, text, image, selectedImage, data);
1659}
1660
08b7c251
VZ
1661wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1662 const wxString& text,
1663 int image, int selectedImage,
1664 wxTreeItemData *data)
2bda0e17 1665{
ee4b2721 1666 return DoInsertItem(parent, TVI_LAST,
08b7c251 1667 text, image, selectedImage, data);
2bda0e17
KB
1668}
1669
08b7c251 1670void wxTreeCtrl::Delete(const wxTreeItemId& item)
2bda0e17 1671{
3f7bc32b 1672 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
bbcdf8bc 1673 {
f6bcfd97 1674 wxLogLastError(wxT("TreeView_DeleteItem"));
bbcdf8bc 1675 }
bbcdf8bc
JS
1676}
1677
23fd5130
VZ
1678// delete all children (but don't delete the item itself)
1679void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1680{
ee4b2721 1681 wxTreeItemIdValue cookie;
23fd5130 1682
ee4b2721 1683 wxArrayTreeItemIds children;
23fd5130
VZ
1684 wxTreeItemId child = GetFirstChild(item, cookie);
1685 while ( child.IsOk() )
1686 {
ee4b2721 1687 children.Add(child);
23fd5130
VZ
1688
1689 child = GetNextChild(item, cookie);
1690 }
1691
1692 size_t nCount = children.Count();
1693 for ( size_t n = 0; n < nCount; n++ )
1694 {
f888d614 1695 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children[n])) )
23fd5130 1696 {
f6bcfd97 1697 wxLogLastError(wxT("TreeView_DeleteItem"));
23fd5130
VZ
1698 }
1699 }
1700}
1701
08b7c251 1702void wxTreeCtrl::DeleteAllItems()
bbcdf8bc 1703{
bfedf621
VZ
1704 // delete the "virtual" root item.
1705 if ( GET_VIRTUAL_ROOT() )
1706 {
1707 delete GET_VIRTUAL_ROOT();
1708 m_pVirtualRoot = NULL;
1709 }
1710
1711 // and all the real items
a9c1265f 1712
d220ae32 1713 if ( !TreeView_DeleteAllItems(GetHwnd()) )
bbcdf8bc 1714 {
f6bcfd97 1715 wxLogLastError(wxT("TreeView_DeleteAllItems"));
bbcdf8bc 1716 }
2bda0e17
KB
1717}
1718
08b7c251 1719void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
2bda0e17 1720{
dd3646fd
VZ
1721 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1722 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1723 flag == TVE_EXPAND ||
1724 flag == TVE_TOGGLE,
223d09f6 1725 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
08b7c251 1726
a9c1265f 1727 // A hidden root can be neither expanded nor collapsed.
e9616381 1728 wxCHECK_RET( !(m_windowStyle & wxTR_HIDE_ROOT) || (HITEM(item) != TVI_ROOT),
0d6a0101 1729 wxT("Can't expand/collapse hidden root node!") )
a9c1265f 1730
08b7c251 1731 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
d220ae32
VZ
1732 // emulate them. This behaviour has changed slightly with comctl32.dll
1733 // v 4.70 - now it does send them but only the first time. To maintain
1734 // compatible behaviour and also in order to not have surprises with the
1735 // future versions, don't rely on this and still do everything ourselves.
1736 // To avoid that the messages be sent twice when the item is expanded for
1737 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1738
1739 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1740 tvItem.state = 0;
1741 DoSetItem(&tvItem);
1742
3f7bc32b 1743 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
08b7c251
VZ
1744 {
1745 wxTreeEvent event(wxEVT_NULL, m_windowId);
1746 event.m_item = item;
08b7c251 1747 event.SetEventObject(this);
2bda0e17 1748
deb1de30
VZ
1749 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1750 // itself
1751 event.SetEventType(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
1752 : IDX_COLLAPSE]
1753 [IDX_DONE]);
2bda0e17 1754
deb1de30 1755 (void)GetEventHandler()->ProcessEvent(event);
08b7c251 1756 }
d220ae32 1757 //else: change didn't took place, so do nothing at all
2bda0e17
KB
1758}
1759
08b7c251 1760void wxTreeCtrl::Expand(const wxTreeItemId& item)
2bda0e17 1761{
08b7c251 1762 DoExpand(item, TVE_EXPAND);
2bda0e17 1763}
2bda0e17 1764
08b7c251 1765void wxTreeCtrl::Collapse(const wxTreeItemId& item)
2bda0e17 1766{
08b7c251 1767 DoExpand(item, TVE_COLLAPSE);
2bda0e17
KB
1768}
1769
08b7c251 1770void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
2bda0e17 1771{
dd3646fd 1772 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
2bda0e17
KB
1773}
1774
08b7c251 1775void wxTreeCtrl::Toggle(const wxTreeItemId& item)
2bda0e17 1776{
08b7c251 1777 DoExpand(item, TVE_TOGGLE);
2bda0e17
KB
1778}
1779
b5f6b52a 1780#if WXWIN_COMPATIBILITY_2_4
42c5812d
UU
1781void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1782{
9dfbf520 1783 DoExpand(item, action);
42c5812d 1784}
b5f6b52a 1785#endif
42c5812d 1786
08b7c251 1787void wxTreeCtrl::Unselect()
2bda0e17 1788{
3f7bc32b
VZ
1789 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1790 wxT("doesn't make sense, may be you want UnselectAll()?") );
9dfbf520
VZ
1791
1792 // just remove the selection
ee4b2721 1793 SelectItem(wxTreeItemId());
08b7c251 1794}
02ce7b72 1795
9dfbf520 1796void wxTreeCtrl::UnselectAll()
08b7c251 1797{
9dfbf520 1798 if ( m_windowStyle & wxTR_MULTIPLE )
2bda0e17 1799 {
9dfbf520
VZ
1800 wxArrayTreeItemIds selections;
1801 size_t count = GetSelections(selections);
1802 for ( size_t n = 0; n < count; n++ )
d220ae32 1803 {
3f7bc32b 1804#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
f888d614 1805 SetItemCheck(HITEM_PTR(selections[n]), false);
3f7bc32b 1806#else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
f888d614 1807 ::UnselectItem(GetHwnd(), HITEM_PTR(selections[n]));
3f7bc32b 1808#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
d220ae32 1809 }
9dfbf520
VZ
1810 }
1811 else
1812 {
1813 // just remove the selection
1814 Unselect();
1815 }
1816}
1817
1818void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1819{
1820 if ( m_windowStyle & wxTR_MULTIPLE )
1821 {
3f7bc32b 1822#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520
VZ
1823 // selecting the item means checking it
1824 SetItemCheck(item);
3f7bc32b
VZ
1825#else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1826 ::SelectItem(GetHwnd(), HITEM(item));
1827#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520
VZ
1828 }
1829 else
1830 {
1831 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1832 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1833 // send them ourselves
1834
1835 wxTreeEvent event(wxEVT_NULL, m_windowId);
1836 event.m_item = item;
1837 event.SetEventObject(this);
1838
1839 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1840 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
d220ae32 1841 {
3f7bc32b 1842 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
9dfbf520 1843 {
f6bcfd97 1844 wxLogLastError(wxT("TreeView_SelectItem"));
9dfbf520
VZ
1845 }
1846 else
1847 {
1848 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1849 (void)GetEventHandler()->ProcessEvent(event);
1850 }
d220ae32 1851 }
9dfbf520 1852 //else: program vetoed the change
2bda0e17 1853 }
08b7c251 1854}
2bda0e17 1855
08b7c251
VZ
1856void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1857{
1858 // no error return
3f7bc32b 1859 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
08b7c251
VZ
1860}
1861
1862void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1863{
3f7bc32b 1864 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
2bda0e17 1865 {
f6bcfd97 1866 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
2bda0e17 1867 }
08b7c251
VZ
1868}
1869
44fbc477 1870wxTextCtrl *wxTreeCtrl::GetEditControl() const
08b7c251
VZ
1871{
1872 return m_textCtrl;
1873}
1874
1875void wxTreeCtrl::DeleteTextCtrl()
1876{
1877 if ( m_textCtrl )
2bda0e17 1878 {
b6a3d6ad
VZ
1879 // the HWND corresponding to this control is deleted by the tree
1880 // control itself and we don't know when exactly this happens, so check
1881 // if the window still exists before calling UnsubclassWin()
1882 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1883 {
1884 m_textCtrl->SetHWND(0);
1885 }
1886
08b7c251
VZ
1887 m_textCtrl->UnsubclassWin();
1888 m_textCtrl->SetHWND(0);
1889 delete m_textCtrl;
1890 m_textCtrl = NULL;
2bda0e17 1891 }
08b7c251 1892}
2bda0e17 1893
08b7c251
VZ
1894wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1895 wxClassInfo* textControlClass)
1896{
1897 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1898
b94ae1ea
VZ
1899 DeleteTextCtrl();
1900
cd6bd270 1901 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
3f7bc32b 1902 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
2bda0e17 1903
5ea47806 1904 // this is not an error - the TVN_BEGINLABELEDIT handler might have
04cd30de 1905 // returned false
5ea47806
VZ
1906 if ( !hWnd )
1907 {
cd6bd270
MB
1908 delete m_textCtrl;
1909 m_textCtrl = NULL;
5ea47806
VZ
1910 return NULL;
1911 }
2bda0e17 1912
cd6bd270 1913 // textctrl is subclassed in MSWOnNotify
08b7c251 1914 return m_textCtrl;
2bda0e17
KB
1915}
1916
08b7c251 1917// End label editing, optionally cancelling the edit
33ac7e6f 1918void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges)
2bda0e17 1919{
d220ae32 1920 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
08b7c251
VZ
1921
1922 DeleteTextCtrl();
2bda0e17
KB
1923}
1924
08b7c251 1925wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
2bda0e17 1926{
08b7c251
VZ
1927 TV_HITTESTINFO hitTestInfo;
1928 hitTestInfo.pt.x = (int)point.x;
1929 hitTestInfo.pt.y = (int)point.y;
2bda0e17 1930
d220ae32 1931 TreeView_HitTest(GetHwnd(), &hitTestInfo);
2bda0e17 1932
08b7c251
VZ
1933 flags = 0;
1934
1935 // avoid repetition
1936 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1937 flags |= wxTREE_HITTEST_##flag
1938
1939 TRANSLATE_FLAG(ABOVE);
1940 TRANSLATE_FLAG(BELOW);
1941 TRANSLATE_FLAG(NOWHERE);
1942 TRANSLATE_FLAG(ONITEMBUTTON);
1943 TRANSLATE_FLAG(ONITEMICON);
1944 TRANSLATE_FLAG(ONITEMINDENT);
1945 TRANSLATE_FLAG(ONITEMLABEL);
1946 TRANSLATE_FLAG(ONITEMRIGHT);
1947 TRANSLATE_FLAG(ONITEMSTATEICON);
1948 TRANSLATE_FLAG(TOLEFT);
1949 TRANSLATE_FLAG(TORIGHT);
2bda0e17 1950
08b7c251
VZ
1951 #undef TRANSLATE_FLAG
1952
ee4b2721 1953 return wxTreeItemId(hitTestInfo.hItem);
08b7c251
VZ
1954}
1955
f7c832a7
VZ
1956bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1957 wxRect& rect,
1958 bool textOnly) const
1959{
1960 RECT rc;
f2c3db9d
RD
1961
1962 // Virtual root items have no bounding rectangle
1963 if ( IS_VIRTUAL_ROOT(item) )
1964 {
1965 return false;
1966 }
1967
3f7bc32b 1968 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
f7c832a7
VZ
1969 &rc, textOnly) )
1970 {
1971 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1972
04cd30de 1973 return true;
f7c832a7
VZ
1974 }
1975 else
1976 {
1977 // couldn't retrieve rect: for example, item isn't visible
04cd30de 1978 return false;
f7c832a7
VZ
1979 }
1980}
1981
23fd5130
VZ
1982// ----------------------------------------------------------------------------
1983// sorting stuff
1984// ----------------------------------------------------------------------------
f7c832a7 1985
502a2b18
VZ
1986// this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1987// functions such as IsDataIndirect()
1988class wxTreeSortHelper
1989{
1990public:
1991 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
1992
1993private:
1994 static wxTreeItemId GetIdFromData(wxTreeCtrl *tree, LPARAM item)
1995 {
1996 wxTreeItemData *data = (wxTreeItemData *)item;
1997 if ( tree->IsDataIndirect(data) )
1998 {
1999 data = ((wxTreeItemIndirectData *)data)->GetData();
2000 }
2001
2002 return data->GetId();
2003 }
2004};
2005
2006int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
2007 LPARAM pItem2,
2008 LPARAM htree)
23fd5130 2009{
096c9f9b 2010 wxCHECK_MSG( pItem1 && pItem2, 0,
223d09f6 2011 wxT("sorting tree without data doesn't make sense") );
096c9f9b 2012
502a2b18
VZ
2013 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
2014
2015 return tree->OnCompareItems(GetIdFromData(tree, pItem1),
2016 GetIdFromData(tree, pItem2));
23fd5130
VZ
2017}
2018
95aabccc
VZ
2019int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
2020 const wxTreeItemId& item2)
08b7c251 2021{
837e5743 2022 return wxStrcmp(GetItemText(item1), GetItemText(item2));
95aabccc
VZ
2023}
2024
2025void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
2026{
2027 // rely on the fact that TreeView_SortChildren does the same thing as our
23fd5130
VZ
2028 // default behaviour, i.e. sorts items alphabetically and so call it
2029 // directly if we're not in derived class (much more efficient!)
2030 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2bda0e17 2031 {
3f7bc32b 2032 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
2bda0e17 2033 }
08b7c251 2034 else
2bda0e17 2035 {
62448488 2036 TV_SORTCB tvSort;
3f7bc32b 2037 tvSort.hParent = HITEM(item);
502a2b18 2038 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
23fd5130 2039 tvSort.lParam = (LPARAM)this;
d220ae32 2040 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2bda0e17 2041 }
08b7c251 2042}
2bda0e17 2043
08b7c251
VZ
2044// ----------------------------------------------------------------------------
2045// implementation
2046// ----------------------------------------------------------------------------
2bda0e17 2047
08b7c251
VZ
2048bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
2049{
2050 if ( cmd == EN_UPDATE )
2bda0e17 2051 {
08b7c251
VZ
2052 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
2053 event.SetEventObject( this );
2054 ProcessCommand(event);
2bda0e17 2055 }
08b7c251 2056 else if ( cmd == EN_KILLFOCUS )
2bda0e17 2057 {
08b7c251
VZ
2058 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
2059 event.SetEventObject( this );
2060 ProcessCommand(event);
2bda0e17 2061 }
08b7c251 2062 else
2bda0e17 2063 {
08b7c251 2064 // nothing done
04cd30de 2065 return false;
2bda0e17 2066 }
08b7c251
VZ
2067
2068 // command processed
04cd30de 2069 return true;
08b7c251
VZ
2070}
2071
23f681ec
VZ
2072// we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2073// only do it during dragging, minimize wxWin overhead (this is important for
2074// WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2075// instead of passing by wxWin events
2076long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2077{
04cd30de 2078 bool processed = false;
3f7bc32b 2079 long rc = 0;
3f7bc32b
VZ
2080 bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0;
2081
2082 if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
23f681ec 2083 {
7bb8798c
VZ
2084 // we only process mouse messages here and these parameters have the
2085 // same meaning for all of them
3f7bc32b
VZ
2086 int x = GET_X_LPARAM(lParam),
2087 y = GET_Y_LPARAM(lParam);
2088 HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
2089
23f681ec
VZ
2090 switch ( nMsg )
2091 {
de6ac339
JS
2092 case WM_RBUTTONDOWN:
2093 // if the item we are about to right click on
2094 // is not already select, remove the entire
2095 // previous selection
2096 if (!::IsItemSelected(GetHwnd(), htItem))
2097 {
2098 UnselectAll();
2099 }
2100
2101 // select item and set the focus to the
2102 // newly selected item
2103 ::SelectItem(GetHwnd(), htItem);
2104 ::SetFocus(GetHwnd(), htItem);
2105 break;
2106
3f7bc32b
VZ
2107#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2108 case WM_LBUTTONDOWN:
2109 if ( htItem && isMultiple )
23f681ec 2110 {
3f7bc32b
VZ
2111 if ( wParam & MK_CONTROL )
2112 {
2113 SetFocus();
23f681ec 2114
3f7bc32b
VZ
2115 // toggle selected state
2116 ToggleItemSelection(GetHwnd(), htItem);
2117
2118 ::SetFocus(GetHwnd(), htItem);
2119
2120 // reset on any click without Shift
f888d614 2121 m_htSelStart.Unset();
3f7bc32b 2122
04cd30de 2123 processed = true;
3f7bc32b
VZ
2124 }
2125 else if ( wParam & MK_SHIFT )
2126 {
2127 // this selects all items between the starting one and
2128 // the current
2129
2130 if ( !m_htSelStart )
2131 {
2132 // take the focused item
ee4b2721 2133 m_htSelStart = TreeView_GetSelection(GetHwnd());
3f7bc32b
VZ
2134 }
2135
2136 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
2137 !(wParam & MK_CONTROL));
2138
2139 ::SetFocus(GetHwnd(), htItem);
23f681ec 2140
04cd30de 2141 processed = true;
3f7bc32b
VZ
2142 }
2143 else // normal click
2144 {
df4ac4c7
VZ
2145 // avoid doing anything if we click on the only
2146 // currently selected item
35cf1ec6 2147
df4ac4c7
VZ
2148 wxArrayTreeItemIds selections;
2149 size_t count = GetSelections(selections);
2150 if ( count == 0 ||
35cf1ec6 2151 count > 1 ||
f888d614 2152 HITEM_PTR(selections[0]) != htItem )
df4ac4c7 2153 {
35cf1ec6
VZ
2154 // clear the previously selected items, if the
2155 // user clicked outside of the present selection.
2156 // otherwise, perform the deselection on mouse-up.
2157 // this allows multiple drag and drop to work.
2158
2159 if (IsItemSelected(GetHwnd(), htItem))
2160 {
2161 ::SetFocus(GetHwnd(), htItem);
2162 }
2163 else
2164 {
2165 UnselectAll();
2166
2167 // prevent the click from starting in-place editing
2168 // which should only happen if we click on the
2169 // already selected item (and nothing else is
2170 // selected)
2171
2172 TreeView_SelectItem(GetHwnd(), 0);
2173 ::SelectItem(GetHwnd(), htItem);
2174 }
df4ac4c7 2175 }
3f7bc32b
VZ
2176
2177 // reset on any click without Shift
f888d614 2178 m_htSelStart.Unset();
3f7bc32b
VZ
2179 }
2180 }
2181 break;
2182#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2183
2184 case WM_MOUSEMOVE:
2185 if ( m_dragImage )
2186 {
afff720b 2187 m_dragImage->Move(wxPoint(x, y));
3f7bc32b 2188 if ( htItem )
23f681ec
VZ
2189 {
2190 // highlight the item as target (hiding drag image is
2191 // necessary - otherwise the display will be corrupted)
68be9f09 2192 m_dragImage->Hide();
3f7bc32b 2193 TreeView_SelectDropTarget(GetHwnd(), htItem);
68be9f09 2194 m_dragImage->Show();
23f681ec
VZ
2195 }
2196 }
2197 break;
2198
2199 case WM_LBUTTONUP:
35cf1ec6
VZ
2200
2201 // facilitates multiple drag-and-drop
2202 if (htItem && isMultiple)
2203 {
2204 wxArrayTreeItemIds selections;
2205 size_t count = GetSelections(selections);
2206
2207 if (count > 1 &&
2208 !(wParam & MK_CONTROL) &&
2209 !(wParam & MK_SHIFT))
2210 {
2211 UnselectAll();
2212 TreeView_SelectItem(GetHwnd(), htItem);
2213 }
2214 }
2215
2216 // fall through
2217
23f681ec 2218 case WM_RBUTTONUP:
3f7bc32b 2219 if ( m_dragImage )
23f681ec 2220 {
68be9f09 2221 m_dragImage->EndDrag();
23f681ec
VZ
2222 delete m_dragImage;
2223 m_dragImage = NULL;
2224
2225 // generate the drag end event
2226 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
2227
ee4b2721 2228 event.m_item = htItem;
23f681ec
VZ
2229 event.m_pointDrag = wxPoint(x, y);
2230 event.SetEventObject(this);
2231
2232 (void)GetEventHandler()->ProcessEvent(event);
225fe9d6
VZ
2233
2234 // if we don't do it, the tree seems to think that 2 items
2235 // are selected simultaneously which is quite weird
2236 TreeView_SelectDropTarget(GetHwnd(), 0);
23f681ec
VZ
2237 }
2238 break;
2239 }
2240 }
3f7bc32b
VZ
2241#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2242 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
2243 {
2244 // the tree control greys out the selected item when it loses focus and
2245 // paints it as selected again when it regains it, but it won't do it
2246 // for the other items itself - help it
2247 wxArrayTreeItemIds selections;
2248 size_t count = GetSelections(selections);
2249 RECT rect;
2250 for ( size_t n = 0; n < count; n++ )
2251 {
04cd30de 2252 // TreeView_GetItemRect() will return false if item is not visible,
3f7bc32b 2253 // which may happen perfectly well
f888d614 2254 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections[n]),
04cd30de 2255 &rect, true) )
3f7bc32b 2256 {
04cd30de 2257 ::InvalidateRect(GetHwnd(), &rect, false);
3f7bc32b
VZ
2258 }
2259 }
2260 }
2261 else if ( nMsg == WM_KEYDOWN && isMultiple )
2262 {
2263 bool bCtrl = wxIsCtrlDown(),
2264 bShift = wxIsShiftDown();
2265
2266 // we handle.arrows and space, but not page up/down and home/end: the
2267 // latter should be easy, but not the former
2268
2c8e4738 2269 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
3f7bc32b
VZ
2270 if ( !m_htSelStart )
2271 {
ee4b2721 2272 m_htSelStart = htSel;
3f7bc32b
VZ
2273 }
2274
2275 if ( wParam == VK_SPACE )
2276 {
2277 if ( bCtrl )
2278 {
2279 ToggleItemSelection(GetHwnd(), htSel);
2280 }
2281 else
2282 {
2283 UnselectAll();
2284
2285 ::SelectItem(GetHwnd(), htSel);
2286 }
23f681ec 2287
04cd30de 2288 processed = true;
3f7bc32b
VZ
2289 }
2290 else if ( wParam == VK_UP || wParam == VK_DOWN )
2291 {
2292 if ( !bCtrl && !bShift )
2293 {
2294 // no modifiers, just clear selection and then let the default
2295 // processing to take place
2296 UnselectAll();
2297 }
2298 else if ( htSel )
2299 {
2300 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
2301
2c8e4738
VZ
2302 HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP
2303 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2304 : TreeView_GetNextVisible(GetHwnd(), htSel));
3f7bc32b
VZ
2305
2306 if ( !htNext )
2307 {
2308 // at the top/bottom
2309 htNext = htSel;
2310 }
2311
2312 if ( bShift )
2313 {
2314 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
2315 }
2316 else // bCtrl
2317 {
2318 // without changing selection
2319 ::SetFocus(GetHwnd(), htNext);
2320 }
2321
04cd30de 2322 processed = true;
3f7bc32b
VZ
2323 }
2324 }
2325 }
2326#endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
7bb8798c
VZ
2327 else if ( nMsg == WM_CHAR )
2328 {
2329 // don't let the control process Space and Return keys because it
2330 // doesn't do anything useful with them anyhow but always beeps
2331 // annoyingly when it receives them and there is no way to turn it off
2332 // simply if you just process TREEITEM_ACTIVATED event to which Space
2333 // and Enter presses are mapped in your code
2334 if ( wParam == VK_SPACE || wParam == VK_RETURN )
2335 {
2336 processed = true;
2337 }
2338 }
2339
3f7bc32b
VZ
2340 if ( !processed )
2341 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
2342
2343 return rc;
23f681ec
VZ
2344}
2345
08b7c251 2346// process WM_NOTIFY Windows message
a23fd0e1 2347bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
08b7c251
VZ
2348{
2349 wxTreeEvent event(wxEVT_NULL, m_windowId);
2350 wxEventType eventType = wxEVT_NULL;
2351 NMHDR *hdr = (NMHDR *)lParam;
2352
2353 switch ( hdr->code )
2bda0e17 2354 {
08b7c251
VZ
2355 case TVN_BEGINDRAG:
2356 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2357 // fall through
2358
2359 case TVN_BEGINRDRAG:
2360 {
2361 if ( eventType == wxEVT_NULL )
2362 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2363 //else: left drag, already set above
2364
2365 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2366
ee4b2721 2367 event.m_item = tv->itemNew.hItem;
08b7c251 2368 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
23f681ec
VZ
2369
2370 // don't allow dragging by default: the user code must
2371 // explicitly say that it wants to allow it to avoid breaking
2372 // the old apps
2373 event.Veto();
08b7c251 2374 }
696e1ea0 2375 break;
08b7c251
VZ
2376
2377 case TVN_BEGINLABELEDIT:
2378 {
2379 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
2380 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2381
ee4b2721 2382 event.m_item = info->item.hItem;
5ea47806 2383 event.m_label = info->item.pszText;
04cd30de 2384 event.m_editCancelled = false;
08b7c251 2385 }
696e1ea0 2386 break;
08b7c251
VZ
2387
2388 case TVN_DELETEITEM:
2389 {
2390 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
2391 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2392
ee4b2721 2393 event.m_item = tv->itemOld.hItem;
696e1ea0
VZ
2394
2395 if ( m_hasAnyAttr )
2396 {
ee4b2721
VZ
2397 wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem);
2398 if ( it != m_attrs.end() )
2399 {
2400 delete it->second;
2401 m_attrs.erase(it);
2402 }
696e1ea0 2403 }
08b7c251 2404 }
696e1ea0 2405 break;
08b7c251
VZ
2406
2407 case TVN_ENDLABELEDIT:
2408 {
2409 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
2410 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2411
ee4b2721 2412 event.m_item = info->item.hItem;
5ea47806 2413 event.m_label = info->item.pszText;
1ee4ead5 2414 if (info->item.pszText == NULL)
dd23c25c 2415 {
04cd30de 2416 event.m_editCancelled = true;
dd23c25c
JS
2417 }
2418 else
2419 {
04cd30de 2420 event.m_editCancelled = false;
dd23c25c 2421 }
08b7c251
VZ
2422 break;
2423 }
2424
2425 case TVN_GETDISPINFO:
2426 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2427 // fall through
2428
2429 case TVN_SETDISPINFO:
2430 {
2431 if ( eventType == wxEVT_NULL )
2432 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2433 //else: get, already set above
2434
2435 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2436
ee4b2721 2437 event.m_item = info->item.hItem;
08b7c251
VZ
2438 break;
2439 }
2440
2441 case TVN_ITEMEXPANDING:
08b7c251
VZ
2442 case TVN_ITEMEXPANDED:
2443 {
2444 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2445
deb1de30 2446 int what;
08b7c251
VZ
2447 switch ( tv->action )
2448 {
deb1de30
VZ
2449 default:
2450 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
2451 // fall through
2452
08b7c251 2453 case TVE_EXPAND:
deb1de30 2454 what = IDX_EXPAND;
08b7c251
VZ
2455 break;
2456
2457 case TVE_COLLAPSE:
deb1de30 2458 what = IDX_COLLAPSE;
08b7c251 2459 break;
08b7c251
VZ
2460 }
2461
2b5f62a0
VZ
2462 int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
2463 : IDX_DONE;
deb1de30
VZ
2464
2465 eventType = gs_expandEvents[what][how];
08b7c251 2466
ee4b2721 2467 event.m_item = tv->itemNew.hItem;
08b7c251 2468 }
696e1ea0 2469 break;
08b7c251
VZ
2470
2471 case TVN_KEYDOWN:
2472 {
2473 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2474 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2475
1944ad76
VZ
2476 // fabricate the lParam and wParam parameters sufficiently
2477 // similar to the ones from a "real" WM_KEYDOWN so that
2478 // CreateKeyEvent() works correctly
2479 WXLPARAM lParam =
1fdf858b 2480 (::GetKeyState(VK_MENU) < 0 ? KF_ALTDOWN : 0) << 16;
1944ad76
VZ
2481
2482 WXWPARAM wParam = info->wVKey;
2483
2484 int keyCode = wxCharCodeMSWToWX(info->wVKey);
2485 if ( !keyCode )
2486 {
2487 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2488 // simple ASCII key
2489 keyCode = wParam;
2490 }
2491
8191741f 2492 event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
1944ad76
VZ
2493 keyCode,
2494 lParam,
2495 wParam);
23fd5130 2496
3f7bc32b
VZ
2497 // a separate event for Space/Return
2498 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2499 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
23fd5130
VZ
2500 {
2501 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2502 m_windowId);
2503 event2.SetEventObject(this);
3f7bc32b
VZ
2504 if ( !(GetWindowStyle() & wxTR_MULTIPLE) )
2505 {
2506 event2.m_item = GetSelection();
2507 }
2508 //else: don't know how to get it
23fd5130 2509
3f7bc32b 2510 (void)GetEventHandler()->ProcessEvent(event2);
23fd5130 2511 }
08b7c251 2512 }
696e1ea0 2513 break;
08b7c251 2514
69a8b5b4
VS
2515 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2516 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2517 // we have to handle both messages:
2518 case TVN_SELCHANGEDA:
2519 case TVN_SELCHANGEDW:
08b7c251
VZ
2520 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2521 // fall through
2522
69a8b5b4
VS
2523 case TVN_SELCHANGINGA:
2524 case TVN_SELCHANGINGW:
08b7c251
VZ
2525 {
2526 if ( eventType == wxEVT_NULL )
2527 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2528 //else: already set above
2529
69a8b5b4
VS
2530 if (hdr->code == TVN_SELCHANGINGW ||
2531 hdr->code == TVN_SELCHANGEDW)
2532 {
2533 NM_TREEVIEWW* tv = (NM_TREEVIEWW *)lParam;
ee4b2721
VZ
2534 event.m_item = tv->itemNew.hItem;
2535 event.m_itemOld = tv->itemOld.hItem;
69a8b5b4
VS
2536 }
2537 else
2538 {
2539 NM_TREEVIEWA* tv = (NM_TREEVIEWA *)lParam;
ee4b2721
VZ
2540 event.m_item = tv->itemNew.hItem;
2541 event.m_itemOld = tv->itemOld.hItem;
69a8b5b4 2542 }
08b7c251 2543 }
696e1ea0
VZ
2544 break;
2545
bf9b6266 2546#if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
696e1ea0
VZ
2547 case NM_CUSTOMDRAW:
2548 {
2549 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2550 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
d7926e0b 2551 switch ( nmcd.dwDrawStage )
696e1ea0
VZ
2552 {
2553 case CDDS_PREPAINT:
2554 // if we've got any items with non standard attributes,
2555 // notify us before painting each item
2556 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2557 : CDRF_DODEFAULT;
d7926e0b 2558 break;
696e1ea0
VZ
2559
2560 case CDDS_ITEMPREPAINT:
2561 {
ee4b2721
VZ
2562 wxMapTreeAttr::iterator
2563 it = m_attrs.find((void *)nmcd.dwItemSpec);
696e1ea0 2564
ee4b2721 2565 if ( it == m_attrs.end() )
696e1ea0
VZ
2566 {
2567 // nothing to do for this item
d7926e0b
VZ
2568 *result = CDRF_DODEFAULT;
2569 break;
696e1ea0
VZ
2570 }
2571
ee4b2721
VZ
2572 wxTreeItemAttr * const attr = it->second;
2573
696e1ea0 2574 HFONT hFont;
696e1ea0
VZ
2575 if ( attr->HasFont() )
2576 {
2076893b 2577 hFont = GetHfontOf(attr->GetFont());
696e1ea0
VZ
2578 }
2579 else
2580 {
2581 hFont = 0;
2582 }
2583
2076893b 2584 wxColour colText;
696e1ea0
VZ
2585 if ( attr->HasTextColour() )
2586 {
2587 colText = attr->GetTextColour();
2588 }
2589 else
2590 {
2591 colText = GetForegroundColour();
2592 }
2593
2594 // selection colours should override ours
2595 if ( nmcd.uItemState & CDIS_SELECTED )
2596 {
2076893b
VZ
2597 lptvcd->clrTextBk =
2598 ::GetSysColor(COLOR_HIGHLIGHT);
2599 lptvcd->clrText =
2600 ::GetSysColor(COLOR_HIGHLIGHTTEXT);
696e1ea0 2601 }
2076893b 2602 else // !selected
696e1ea0 2603 {
2076893b 2604 wxColour colBack;
696e1ea0
VZ
2605 if ( attr->HasBackgroundColour() )
2606 {
2607 colBack = attr->GetBackgroundColour();
2608 }
2609 else
2610 {
2611 colBack = GetBackgroundColour();
2612 }
2613
2614 lptvcd->clrText = wxColourToRGB(colText);
2615 lptvcd->clrTextBk = wxColourToRGB(colBack);
2616 }
2617
2618 // note that if we wanted to set colours for
2619 // individual columns (subitems), we would have
2620 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2621 if ( hFont )
2622 {
2623 ::SelectObject(nmcd.hdc, hFont);
2624
2625 *result = CDRF_NEWFONT;
2626 }
2627 else
2628 {
2629 *result = CDRF_DODEFAULT;
2630 }
696e1ea0 2631 }
d7926e0b 2632 break;
696e1ea0
VZ
2633
2634 default:
2635 *result = CDRF_DODEFAULT;
696e1ea0
VZ
2636 }
2637 }
d7926e0b
VZ
2638
2639 // we always process it
04cd30de 2640 return true;
6ecfe2ac 2641#endif // _WIN32_IE >= 0x300
08b7c251 2642
8a000b6b
VZ
2643 case NM_CLICK:
2644 {
2645 DWORD pos = GetMessagePos();
2646 POINT point;
2647 point.x = LOWORD(pos);
2648 point.y = HIWORD(pos);
2649 ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1);
2650 int flags = 0;
2651 wxTreeItemId item = HitTest(wxPoint(point.x, point.y), flags);
2652 if (flags & wxTREE_HITTEST_ONITEMSTATEICON)
2653 {
2654 event.m_item = item;
2655 eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK;
2656 }
2657 break;
2658 }
2659
f6bcfd97
BP
2660 case NM_DBLCLK:
2661 case NM_RCLICK:
2662 {
2663 TV_HITTESTINFO tvhti;
2664 ::GetCursorPos(&tvhti.pt);
2665 ::ScreenToClient(GetHwnd(), &tvhti.pt);
2666 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2667 {
2668 if ( tvhti.flags & TVHT_ONITEM )
2669 {
ee4b2721 2670 event.m_item = tvhti.hItem;
f6bcfd97
BP
2671 eventType = (int)hdr->code == NM_DBLCLK
2672 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2673 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
8591268f
VZ
2674
2675 event.m_pointDrag.x = tvhti.pt.x;
2676 event.m_pointDrag.y = tvhti.pt.y;
f6bcfd97
BP
2677 }
2678
2679 break;
2680 }
2681 }
2682 // fall through
2683
08b7c251 2684 default:
a23fd0e1 2685 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2bda0e17 2686 }
08b7c251
VZ
2687
2688 event.SetEventObject(this);
2689 event.SetEventType(eventType);
2690
fd3f686c 2691 bool processed = GetEventHandler()->ProcessEvent(event);
08b7c251
VZ
2692
2693 // post processing
5ea47806 2694 switch ( hdr->code )
2bda0e17 2695 {
f6bcfd97
BP
2696 case NM_DBLCLK:
2697 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2698 // the return code of this event handler as the return value for
2699 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2700 // expanded status would never work
04cd30de 2701 *result = false;
f6bcfd97
BP
2702 break;
2703
23f681ec
VZ
2704 case TVN_BEGINDRAG:
2705 case TVN_BEGINRDRAG:
2706 if ( event.IsAllowed() )
2707 {
2708 // normally this is impossible because the m_dragImage is
2709 // deleted once the drag operation is over
2710 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2711
2712 m_dragImage = new wxDragImage(*this, event.m_item);
2713 m_dragImage->BeginDrag(wxPoint(0, 0), this);
68be9f09 2714 m_dragImage->Show();
23f681ec
VZ
2715 }
2716 break;
2717
5ea47806
VZ
2718 case TVN_DELETEITEM:
2719 {
2720 // NB: we might process this message using wxWindows event
2721 // tables, but due to overhead of wxWin event system we
2722 // prefer to do it here ourself (otherwise deleting a tree
2723 // with many items is just too slow)
2724 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
74b31181
VZ
2725
2726 wxTreeItemId item = event.m_item;
2727 if ( HasIndirectData(item) )
2728 {
2729 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
2730 tv->itemOld.lParam;
2731 delete data; // can't be NULL here
74b31181
VZ
2732 }
2733 else
2734 {
2735 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
2736 delete data; // may be NULL, ok
2737 }
08b7c251 2738
04cd30de 2739 processed = true; // Make sure we don't get called twice
5ea47806
VZ
2740 }
2741 break;
2742
2743 case TVN_BEGINLABELEDIT:
04cd30de 2744 // return true to cancel label editing
5ea47806 2745 *result = !event.IsAllowed();
cd6bd270
MB
2746 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2747 if(event.IsAllowed())
2748 {
2749 HWND hText = TreeView_GetEditControl(GetHwnd());
2750 if(hText != NULL)
2751 {
2752 // MBN: if m_textCtrl already has an HWND, it is a stale
2753 // pointer from a previous edit (because the user
2754 // didn't modify the label before dismissing the control,
2755 // and TVN_ENDLABELEDIT was not sent), so delete it
2756 if(m_textCtrl && m_textCtrl->GetHWND() != 0)
2757 DeleteTextCtrl();
2758 if(!m_textCtrl)
2759 m_textCtrl = new wxTextCtrl();
2760 m_textCtrl->SetParent(this);
2761 m_textCtrl->SetHWND((WXHWND)hText);
2762 m_textCtrl->SubclassWin((WXHWND)hText);
2763
2764 // set wxTE_PROCESS_ENTER style for the text control to
2765 // force it to process the Enter presses itself, otherwise
2766 // they could be stolen from it by the dialog
2767 // navigation code
2768 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle()
2769 | wxTE_PROCESS_ENTER);
2770 }
2771 }
5ea47806
VZ
2772 break;
2773
2774 case TVN_ENDLABELEDIT:
04cd30de 2775 // return true to set the label to the new string: note that we
188781db 2776 // also must pretend that we did process the message or it is going
04cd30de 2777 // to be passed to DefWindowProc() which will happily return false
188781db 2778 // cancelling the label change
5ea47806 2779 *result = event.IsAllowed();
04cd30de 2780 processed = true;
5ea47806
VZ
2781
2782 // ensure that we don't have the text ctrl which is going to be
2783 // deleted any more
2784 DeleteTextCtrl();
2785 break;
2786
2787 case TVN_SELCHANGING:
2788 case TVN_ITEMEXPANDING:
04cd30de 2789 // return true to prevent the action from happening
5ea47806
VZ
2790 *result = !event.IsAllowed();
2791 break;
2792
deb1de30
VZ
2793 case TVN_ITEMEXPANDED:
2794 // the item is not refreshed properly after expansion when it has
2795 // an image depending on the expanded/collapsed state - bug in
2796 // comctl32.dll or our code?
2797 {
bf43d750 2798 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
ee4b2721 2799 wxTreeItemId id(tv->itemNew.hItem);
deb1de30 2800
bf43d750
VZ
2801 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
2802 if ( image != -1 )
2803 {
2804 RefreshItem(id);
deb1de30
VZ
2805 }
2806 }
2807 break;
2808
74b31181
VZ
2809 case TVN_GETDISPINFO:
2810 // NB: so far the user can't set the image himself anyhow, so do it
2811 // anyway - but this may change later
bf43d750 2812 //if ( /* !processed && */ 1 )
74b31181
VZ
2813 {
2814 wxTreeItemId item = event.m_item;
2815 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2816 if ( info->item.mask & TVIF_IMAGE )
2817 {
2818 info->item.iImage =
2819 DoGetItemImageFromData
2820 (
2821 item,
2822 IsExpanded(item) ? wxTreeItemIcon_Expanded
2823 : wxTreeItemIcon_Normal
2824 );
2825 }
2826 if ( info->item.mask & TVIF_SELECTEDIMAGE )
2827 {
2828 info->item.iSelectedImage =
2829 DoGetItemImageFromData
2830 (
2831 item,
2832 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
2833 : wxTreeItemIcon_Selected
2834 );
2835 }
deb1de30 2836 }
74b31181
VZ
2837 break;
2838
5ea47806
VZ
2839 //default:
2840 // for the other messages the return value is ignored and there is
2841 // nothing special to do
2842 }
fd3f686c 2843 return processed;
2bda0e17
KB
2844}
2845
8a000b6b
VZ
2846// ----------------------------------------------------------------------------
2847// State control.
2848// ----------------------------------------------------------------------------
2849
2850// why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2851#define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2852
2853void wxTreeCtrl::SetState(const wxTreeItemId& node, int state)
2854{
2855 TV_ITEM tvi;
2856 tvi.hItem = (HTREEITEM)node.m_pItem;
2857 tvi.mask = TVIF_STATE;
2858 tvi.stateMask = TVIS_STATEIMAGEMASK;
2859
2860 // Select the specified state, or -1 == cycle to the next one.
2861 if ( state == -1 )
2862 {
2863 TreeView_GetItem(GetHwnd(), &tvi);
2864
2865 state = STATEIMAGEMASKTOINDEX(tvi.state) + 1;
2866 if ( state == m_imageListState->GetImageCount() )
2867 state = 1;
2868 }
2869
2870 wxCHECK_RET( state < m_imageListState->GetImageCount(),
2871 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2872
2873 tvi.state = INDEXTOSTATEIMAGEMASK(state);
2874
2875 TreeView_SetItem(GetHwnd(), &tvi);
2876}
2877
2878int wxTreeCtrl::GetState(const wxTreeItemId& node)
2879{
2880 TV_ITEM tvi;
2881 tvi.hItem = (HTREEITEM)node.m_pItem;
2882 tvi.mask = TVIF_STATE;
2883 tvi.stateMask = TVIS_STATEIMAGEMASK;
2884 TreeView_GetItem(GetHwnd(), &tvi);
2885
2886 return STATEIMAGEMASKTOINDEX(tvi.state);
2887}
2888
1e6feb95 2889#endif // wxUSE_TREECTRL
8a000b6b 2890