]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
typo/spelling fixes (patch 983448)
[wxWidgets.git] / src / generic / treectlg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
941830cb 2// Name: treectlg.cpp
f135ff73 3// Purpose: generic tree control implementation
c801d85f
KB
4// Author: Robert Roebling
5// Created: 01/02/97
f135ff73 6// Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
389cdc7a 7// Id: $Id$
6aa89a22 8// Copyright: (c) 1998 Robert Roebling and Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
f135ff73
VZ
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
941830cb 21 #pragma implementation "treectlg.h"
c801d85f
KB
22#endif
23
1e6d9499
JS
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
1e6feb95 28 #pragma hdrstop
1e6d9499
JS
29#endif
30
1e6feb95
VZ
31#if wxUSE_TREECTRL
32
618a5e38 33#include "wx/treebase.h"
941830cb 34#include "wx/generic/treectlg.h"
618a5e38
RR
35#include "wx/timer.h"
36#include "wx/textctrl.h"
941830cb 37#include "wx/imaglist.h"
c801d85f 38#include "wx/settings.h"
f135ff73 39#include "wx/dcclient.h"
c801d85f 40
9c7f49f5 41#include "wx/renderer.h"
44c44c82 42
f89d65ea
SC
43#ifdef __WXMAC__
44 #include "wx/mac/private.h"
45#endif
ca65c044 46
f135ff73
VZ
47// -----------------------------------------------------------------------------
48// array types
49// -----------------------------------------------------------------------------
c801d85f 50
1e6d9499
JS
51class WXDLLEXPORT wxGenericTreeItem;
52
d5d29b8a 53WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem *, wxArrayGenericTreeItems);
c801d85f 54
8dc99046
VZ
55// ----------------------------------------------------------------------------
56// constants
57// ----------------------------------------------------------------------------
58
59static const int NO_IMAGE = -1;
60
cb59313c 61static const int PIXELS_PER_UNIT = 10;
3dbeaa52 62
f135ff73
VZ
63// -----------------------------------------------------------------------------
64// private classes
65// -----------------------------------------------------------------------------
66
3dbeaa52
VZ
67// timer used for enabling in-place edit
68class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
69{
70public:
cb59313c
VZ
71 // start editing the current item after half a second (if the mouse hasn't
72 // been clicked/moved)
7a944d2f 73 enum { DELAY = 500 };
cb59313c 74
941830cb 75 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
3dbeaa52 76
cb59313c 77 virtual void Notify();
3dbeaa52
VZ
78
79private:
cb59313c 80 wxGenericTreeCtrl *m_owner;
22f3361e
VZ
81
82 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer)
3dbeaa52
VZ
83};
84
85// control used for in-place edit
86class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
87{
88public:
edb8f298 89 wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item);
3dbeaa52 90
edb8f298 91protected:
3dbeaa52 92 void OnChar( wxKeyEvent &event );
c13cace1 93 void OnKeyUp( wxKeyEvent &event );
3dbeaa52
VZ
94 void OnKillFocus( wxFocusEvent &event );
95
edb8f298
VZ
96 bool AcceptChanges();
97 void Finish();
98
3dbeaa52 99private:
618a5e38 100 wxGenericTreeCtrl *m_owner;
edb8f298 101 wxGenericTreeItem *m_itemEdited;
3dbeaa52 102 wxString m_startValue;
dd5a32cc 103 bool m_finished;
3dbeaa52
VZ
104
105 DECLARE_EVENT_TABLE()
22f3361e 106 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl)
3dbeaa52
VZ
107};
108
cb59313c
VZ
109// timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
110// for a sufficiently long time
111class WXDLLEXPORT wxTreeFindTimer : public wxTimer
112{
113public:
114 // reset the current prefix after half a second of inactivity
7a944d2f 115 enum { DELAY = 500 };
cb59313c
VZ
116
117 wxTreeFindTimer( wxGenericTreeCtrl *owner ) { m_owner = owner; }
118
119 virtual void Notify() { m_owner->m_findPrefix.clear(); }
120
121private:
122 wxGenericTreeCtrl *m_owner;
22f3361e
VZ
123
124 DECLARE_NO_COPY_CLASS(wxTreeFindTimer)
cb59313c
VZ
125};
126
f135ff73
VZ
127// a tree item
128class WXDLLEXPORT wxGenericTreeItem
c801d85f 129{
f135ff73 130public:
9ec64fa7
VZ
131 // ctors & dtor
132 wxGenericTreeItem() { m_data = NULL; }
133 wxGenericTreeItem( wxGenericTreeItem *parent,
618a5e38 134 const wxString& text,
618a5e38
RR
135 int image,
136 int selImage,
137 wxTreeItemData *data );
f135ff73 138
9ec64fa7 139 ~wxGenericTreeItem();
f135ff73 140
9ec64fa7
VZ
141 // trivial accessors
142 wxArrayGenericTreeItems& GetChildren() { return m_children; }
f135ff73 143
9ec64fa7
VZ
144 const wxString& GetText() const { return m_text; }
145 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
3dbeaa52 146 { return m_images[which]; }
9ec64fa7 147 wxTreeItemData *GetData() const { return m_data; }
f135ff73 148
9ec64fa7
VZ
149 // returns the current image for the item (depending on its
150 // selected/expanded/whatever state)
151 int GetCurrentImage() const;
8dc99046 152
9ec64fa7
VZ
153 void SetText( const wxString &text );
154 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
155 void SetData(wxTreeItemData *data) { m_data = data; }
f135ff73 156
ca65c044 157 void SetHasPlus(bool has = true) { m_hasPlus = has; }
f135ff73 158
9ec64fa7 159 void SetBold(bool bold) { m_isBold = bold; }
ef44a621 160
9ec64fa7
VZ
161 int GetX() const { return m_x; }
162 int GetY() const { return m_y; }
f135ff73 163
9ec64fa7
VZ
164 void SetX(int x) { m_x = x; }
165 void SetY(int y) { m_y = y; }
f135ff73 166
9ec64fa7
VZ
167 int GetHeight() const { return m_height; }
168 int GetWidth() const { return m_width; }
91b8de8d 169
9ec64fa7
VZ
170 void SetHeight(int h) { m_height = h; }
171 void SetWidth(int w) { m_width = w; }
91b8de8d 172
9ec64fa7 173 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 174
9ec64fa7
VZ
175 // operations
176 // deletes all children notifying the treectrl about it if !NULL
177 // pointer given
941830cb 178 void DeleteChildren(wxGenericTreeCtrl *tree = NULL);
f135ff73 179
9ec64fa7 180 // get count of all children (and grand children if 'recursively')
ca65c044 181 size_t GetChildrenCount(bool recursively = true) const;
f135ff73 182
9ec64fa7 183 void Insert(wxGenericTreeItem *child, size_t index)
f135ff73
VZ
184 { m_children.Insert(child, index); }
185
941830cb 186 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
f135ff73 187
9ec64fa7 188 // return the item at given position (or NULL if no item), onButton is
ca65c044 189 // true if the point belongs to the item's button, otherwise it lies
f8b043e7 190 // on the item's label
618a5e38
RR
191 wxGenericTreeItem *HitTest( const wxPoint& point,
192 const wxGenericTreeCtrl *,
193 int &flags,
194 int level );
f135ff73 195
ca65c044
WS
196 void Expand() { m_isCollapsed = false; }
197 void Collapse() { m_isCollapsed = true; }
f135ff73 198
ca65c044 199 void SetHilight( bool set = true ) { m_hasHilight = set; }
f135ff73 200
9ec64fa7
VZ
201 // status inquiries
202 bool HasChildren() const { return !m_children.IsEmpty(); }
ef8698d6 203 bool IsSelected() const { return m_hasHilight != 0; }
9ec64fa7
VZ
204 bool IsExpanded() const { return !m_isCollapsed; }
205 bool HasPlus() const { return m_hasPlus || HasChildren(); }
ef8698d6 206 bool IsBold() const { return m_isBold != 0; }
9ec64fa7
VZ
207
208 // attributes
209 // get them - may be NULL
210 wxTreeItemAttr *GetAttributes() const { return m_attr; }
211 // get them ensuring that the pointer is not NULL
212 wxTreeItemAttr& Attr()
213 {
214 if ( !m_attr )
618a5e38 215 {
9ec64fa7 216 m_attr = new wxTreeItemAttr;
ca65c044 217 m_ownsAttr = true;
618a5e38 218 }
9ec64fa7
VZ
219 return *m_attr;
220 }
618a5e38
RR
221 // set them
222 void SetAttributes(wxTreeItemAttr *attr)
223 {
224 if ( m_ownsAttr ) delete m_attr;
225 m_attr = attr;
ca65c044 226 m_ownsAttr = false;
618a5e38
RR
227 }
228 // set them and delete when done
229 void AssignAttributes(wxTreeItemAttr *attr)
230 {
231 SetAttributes(attr);
ca65c044 232 m_ownsAttr = true;
618a5e38 233 }
f135ff73
VZ
234
235private:
618a5e38
RR
236 // since there can be very many of these, we save size by chosing
237 // the smallest representation for the elements and by ordering
238 // the members to avoid padding.
239 wxString m_text; // label to be rendered for item
240
241 wxTreeItemData *m_data; // user-provided data
242
243 wxArrayGenericTreeItems m_children; // list of children
244 wxGenericTreeItem *m_parent; // parent of this item
245
246 wxTreeItemAttr *m_attr; // attributes???
9ec64fa7
VZ
247
248 // tree ctrl images for the normal, selected, expanded and
249 // expanded+selected states
618a5e38 250 short m_images[wxTreeItemIcon_Max];
9ec64fa7 251
618a5e38 252 wxCoord m_x; // (virtual) offset from top
e549bec4 253 wxCoord m_y; // (virtual) offset from left
618a5e38
RR
254 short m_width; // width of this item
255 unsigned char m_height; // height of this item
9ec64fa7
VZ
256
257 // use bitfields to save size
258 int m_isCollapsed :1;
259 int m_hasHilight :1; // same as focused
260 int m_hasPlus :1; // used for item which doesn't have
261 // children but has a [+] button
262 int m_isBold :1; // render the label in bold font
618a5e38 263 int m_ownsAttr :1; // delete attribute when done
22f3361e
VZ
264
265 DECLARE_NO_COPY_CLASS(wxGenericTreeItem)
f135ff73
VZ
266};
267
268// =============================================================================
269// implementation
270// =============================================================================
271
06b466c7
VZ
272// ----------------------------------------------------------------------------
273// private functions
274// ----------------------------------------------------------------------------
275
276// translate the key or mouse event flags to the type of selection we're
277// dealing with
278static void EventFlagsToSelType(long style,
279 bool shiftDown,
280 bool ctrlDown,
dfc6cd93
SB
281 bool &is_multiple,
282 bool &extended_select,
283 bool &unselect_others)
06b466c7 284{
dfc6cd93
SB
285 is_multiple = (style & wxTR_MULTIPLE) != 0;
286 extended_select = shiftDown && is_multiple;
287 unselect_others = !(extended_select || (ctrlDown && is_multiple));
06b466c7 288}
e179bd65 289
7475e8a3
VZ
290// check if the given item is under another one
291static bool IsDescendantOf(wxGenericTreeItem *parent, wxGenericTreeItem *item)
292{
293 while ( item )
294 {
295 if ( item == parent )
296 {
297 // item is a descendant of parent
ca65c044 298 return true;
7475e8a3
VZ
299 }
300
301 item = item->GetParent();
302 }
303
ca65c044 304 return false;
7475e8a3
VZ
305}
306
e179bd65
RR
307// -----------------------------------------------------------------------------
308// wxTreeRenameTimer (internal)
309// -----------------------------------------------------------------------------
310
941830cb 311wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
e179bd65
RR
312{
313 m_owner = owner;
314}
315
316void wxTreeRenameTimer::Notify()
317{
318 m_owner->OnRenameTimer();
319}
320
321//-----------------------------------------------------------------------------
322// wxTreeTextCtrl (internal)
323//-----------------------------------------------------------------------------
324
e179bd65
RR
325BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
326 EVT_CHAR (wxTreeTextCtrl::OnChar)
c13cace1 327 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
e179bd65
RR
328 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
329END_EVENT_TABLE()
330
edb8f298
VZ
331wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
332 wxGenericTreeItem *item)
333 : m_itemEdited(item), m_startValue(item->GetText())
334{
e179bd65 335 m_owner = owner;
ca65c044 336 m_finished = false;
e179bd65 337
edb8f298
VZ
338 int w = m_itemEdited->GetWidth(),
339 h = m_itemEdited->GetHeight();
340
341 int x, y;
342 m_owner->CalcScrolledPosition(item->GetX(), item->GetY(), &x, &y);
343
344 int image_h = 0,
345 image_w = 0;
346
347 int image = item->GetCurrentImage();
348 if ( image != NO_IMAGE )
e179bd65 349 {
edb8f298
VZ
350 if ( m_owner->m_imageListNormal )
351 {
352 m_owner->m_imageListNormal->GetSize( image, image_w, image_h );
353 image_w += 4;
354 }
355 else
356 {
357 wxFAIL_MSG(_T("you must create an image list to use images!"));
358 }
359 }
33ac7e6f 360
edb8f298
VZ
361 // FIXME: what are all these hardcoded 4, 8 and 11s really?
362 x += image_w;
363 w -= image_w + 4;
33ac7e6f 364
edb8f298
VZ
365 (void)Create(m_owner, wxID_ANY, m_startValue,
366 wxPoint(x - 4, y - 4), wxSize(w + 11, h + 8));
367}
574c939e 368
edb8f298
VZ
369bool wxTreeTextCtrl::AcceptChanges()
370{
371 const wxString value = GetValue();
618a5e38 372
edb8f298
VZ
373 if ( value == m_startValue )
374 {
375 // nothing changed, always accept
ca65c044 376 return true;
e179bd65 377 }
edb8f298
VZ
378
379 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
e179bd65 380 {
edb8f298 381 // vetoed by the user
ca65c044 382 return false;
edb8f298
VZ
383 }
384
385 // accepted, do rename the item
386 m_owner->SetItemText(m_itemEdited, value);
33ac7e6f 387
ca65c044 388 return true;
edb8f298
VZ
389}
390
391void wxTreeTextCtrl::Finish()
392{
393 if ( !m_finished )
394 {
fbb12260
JS
395 m_owner->ResetTextControl();
396
edb8f298 397 wxPendingDelete.Append(this);
33ac7e6f 398
ca65c044 399 m_finished = true;
edb8f298 400
dd5a32cc 401 m_owner->SetFocus(); // This doesn't work. TODO.
edb8f298
VZ
402 }
403}
dd5a32cc 404
edb8f298
VZ
405void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
406{
407 switch ( event.m_keyCode )
408 {
409 case WXK_RETURN:
2b4f324a 410 if ( AcceptChanges() )
edb8f298 411 {
2b4f324a
VZ
412 // Close the text control, changes were accepted
413 Finish();
edb8f298 414 }
2b4f324a
VZ
415 // else do nothing, do not accept and do not close
416
417 break;
edb8f298
VZ
418
419 case WXK_ESCAPE:
420 Finish();
dd23c25c 421 m_owner->OnRenameCancelled(m_itemEdited);
edb8f298
VZ
422 break;
423
424 default:
425 event.Skip();
e179bd65 426 }
c13cace1
VS
427}
428
429void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
430{
edb8f298 431 if ( !m_finished )
dd5a32cc 432 {
edb8f298
VZ
433 // auto-grow the textctrl:
434 wxSize parentSize = m_owner->GetSize();
435 wxPoint myPos = GetPosition();
436 wxSize mySize = GetSize();
437 int sx, sy;
438 GetTextExtent(GetValue() + _T("M"), &sx, &sy);
439 if (myPos.x + sx > parentSize.x)
440 sx = parentSize.x - myPos.x;
441 if (mySize.x > sx)
442 sx = mySize.x;
ca65c044 443 SetSize(sx, wxDefaultSize.y);
dd5a32cc
RR
444 }
445
c13cace1 446 event.Skip();
e179bd65
RR
447}
448
dd5a32cc 449void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
e179bd65 450{
edb8f298 451 if ( m_finished )
dd5a32cc
RR
452 {
453 event.Skip();
454 return;
455 }
456
edb8f298
VZ
457 if ( AcceptChanges() )
458 {
459 Finish();
460 }
e179bd65
RR
461}
462
f135ff73 463// -----------------------------------------------------------------------------
c801d85f 464// wxGenericTreeItem
f135ff73 465// -----------------------------------------------------------------------------
c801d85f 466
f135ff73
VZ
467wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
468 const wxString& text,
f135ff73
VZ
469 int image, int selImage,
470 wxTreeItemData *data)
471 : m_text(text)
c801d85f 472{
00e12320
RR
473 m_images[wxTreeItemIcon_Normal] = image;
474 m_images[wxTreeItemIcon_Selected] = selImage;
475 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
476 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
8dc99046 477
00e12320
RR
478 m_data = data;
479 m_x = m_y = 0;
f135ff73 480
ca65c044
WS
481 m_isCollapsed = true;
482 m_hasHilight = false;
483 m_hasPlus = false;
484 m_isBold = false;
c801d85f 485
00e12320 486 m_parent = parent;
f135ff73 487
00e12320 488 m_attr = (wxTreeItemAttr *)NULL;
ca65c044 489 m_ownsAttr = false;
06b466c7 490
00e12320
RR
491 // We don't know the height here yet.
492 m_width = 0;
493 m_height = 0;
edaa81ae 494}
c801d85f 495
f135ff73 496wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 497{
00e12320 498 delete m_data;
4832f7c0 499
618a5e38 500 if (m_ownsAttr) delete m_attr;
9ec64fa7 501
00e12320
RR
502 wxASSERT_MSG( m_children.IsEmpty(),
503 wxT("please call DeleteChildren() before deleting the item") );
372edb9d
VZ
504}
505
941830cb 506void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
372edb9d 507{
00e12320
RR
508 size_t count = m_children.Count();
509 for ( size_t n = 0; n < count; n++ )
a43a4f9d 510 {
00e12320
RR
511 wxGenericTreeItem *child = m_children[n];
512 if (tree)
513 tree->SendDeleteEvent(child);
a43a4f9d 514
00e12320
RR
515 child->DeleteChildren(tree);
516 delete child;
517 }
372edb9d 518
00e12320 519 m_children.Empty();
edaa81ae 520}
c801d85f 521
91b8de8d 522void wxGenericTreeItem::SetText( const wxString &text )
c801d85f 523{
00e12320 524 m_text = text;
edaa81ae 525}
c801d85f 526
4832f7c0 527size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 528{
00e12320
RR
529 size_t count = m_children.Count();
530 if ( !recursively )
531 return count;
4832f7c0 532
00e12320 533 size_t total = count;
f2593d0d 534 for (size_t n = 0; n < count; ++n)
00e12320
RR
535 {
536 total += m_children[n]->GetChildrenCount();
537 }
c801d85f 538
00e12320 539 return total;
edaa81ae 540}
c801d85f 541
618a5e38
RR
542void wxGenericTreeItem::GetSize( int &x, int &y,
543 const wxGenericTreeCtrl *theButton )
c801d85f 544{
618a5e38 545 int bottomY=m_y+theButton->GetLineHeight(this);
00e12320
RR
546 if ( y < bottomY ) y = bottomY;
547 int width = m_x + m_width;
548 if ( x < width ) x = width;
f135ff73 549
00e12320 550 if (IsExpanded())
4832f7c0 551 {
00e12320
RR
552 size_t count = m_children.Count();
553 for ( size_t n = 0; n < count; ++n )
554 {
618a5e38 555 m_children[n]->GetSize( x, y, theButton );
00e12320 556 }
df875e59 557 }
edaa81ae 558}
c801d85f 559
618a5e38
RR
560wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point,
561 const wxGenericTreeCtrl *theCtrl,
562 int &flags,
563 int level)
c801d85f 564{
618a5e38
RR
565 // for a hidden root node, don't evaluate it, but do evaluate children
566 if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) )
c801d85f 567 {
618a5e38
RR
568 // evaluate the item
569 int h = theCtrl->GetLineHeight(this);
570 if ((point.y > m_y) && (point.y < m_y + h))
f2593d0d 571 {
618a5e38
RR
572 int y_mid = m_y + h/2;
573 if (point.y < y_mid )
574 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
575 else
576 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
d3a9f4af 577
618a5e38 578 int xCross = m_x - theCtrl->GetSpacing();
a6b0ca75
SC
579#ifdef __WXMAC__
580 // according to the drawing code the triangels are drawn
581 // at -4 , -4 from the position up to +10/+10 max
582 if ((point.x > xCross-4) && (point.x < xCross+10) &&
583 (point.y > y_mid-4) && (point.y < y_mid+10) &&
584 HasPlus() && theCtrl->HasButtons() )
585#else
586 // 5 is the size of the plus sign
f8b043e7
RR
587 if ((point.x > xCross-6) && (point.x < xCross+6) &&
588 (point.y > y_mid-6) && (point.y < y_mid+6) &&
618a5e38 589 HasPlus() && theCtrl->HasButtons() )
a6b0ca75 590#endif
618a5e38
RR
591 {
592 flags |= wxTREE_HITTEST_ONITEMBUTTON;
593 return this;
594 }
0ae7f2a2 595
618a5e38
RR
596 if ((point.x >= m_x) && (point.x <= m_x+m_width))
597 {
598 int image_w = -1;
599 int image_h;
91b8de8d 600
618a5e38
RR
601 // assuming every image (normal and selected) has the same size!
602 if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal )
603 theCtrl->m_imageListNormal->GetSize(GetImage(),
604 image_w, image_h);
605
606 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
607 flags |= wxTREE_HITTEST_ONITEMICON;
608 else
609 flags |= wxTREE_HITTEST_ONITEMLABEL;
610
611 return this;
612 }
613
614 if (point.x < m_x)
615 flags |= wxTREE_HITTEST_ONITEMINDENT;
616 if (point.x > m_x+m_width)
617 flags |= wxTREE_HITTEST_ONITEMRIGHT;
91b8de8d 618
f2593d0d
RR
619 return this;
620 }
91b8de8d 621
618a5e38
RR
622 // if children are expanded, fall through to evaluate them
623 if (m_isCollapsed) return (wxGenericTreeItem*) NULL;
f2593d0d 624 }
618a5e38
RR
625
626 // evaluate children
627 size_t count = m_children.Count();
628 for ( size_t n = 0; n < count; n++ )
c801d85f 629 {
618a5e38
RR
630 wxGenericTreeItem *res = m_children[n]->HitTest( point,
631 theCtrl,
632 flags,
633 level + 1 );
634 if ( res != NULL )
635 return res;
edaa81ae 636 }
f135ff73 637
f2593d0d 638 return (wxGenericTreeItem*) NULL;
edaa81ae 639}
c801d85f 640
8dc99046
VZ
641int wxGenericTreeItem::GetCurrentImage() const
642{
643 int image = NO_IMAGE;
644 if ( IsExpanded() )
645 {
646 if ( IsSelected() )
647 {
648 image = GetImage(wxTreeItemIcon_SelectedExpanded);
649 }
650
651 if ( image == NO_IMAGE )
652 {
653 // we usually fall back to the normal item, but try just the
654 // expanded one (and not selected) first in this case
655 image = GetImage(wxTreeItemIcon_Expanded);
656 }
657 }
658 else // not expanded
659 {
660 if ( IsSelected() )
661 image = GetImage(wxTreeItemIcon_Selected);
662 }
663
618a5e38
RR
664 // maybe it doesn't have the specific image we want,
665 // try the default one instead
666 if ( image == NO_IMAGE ) image = GetImage();
8dc99046
VZ
667
668 return image;
669}
670
f135ff73 671// -----------------------------------------------------------------------------
941830cb 672// wxGenericTreeCtrl implementation
f135ff73
VZ
673// -----------------------------------------------------------------------------
674
941830cb 675IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow)
f135ff73 676
941830cb
JS
677BEGIN_EVENT_TABLE(wxGenericTreeCtrl,wxScrolledWindow)
678 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
679 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
680 EVT_CHAR (wxGenericTreeCtrl::OnChar)
681 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
682 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
ca65c044 683 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY, wxGenericTreeCtrl::OnGetToolTip)
f135ff73
VZ
684END_EVENT_TABLE()
685
3a5bcc4d 686#if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
233058c7
JS
687/*
688 * wxTreeCtrl has to be a real class or we have problems with
689 * the run-time information.
690 */
691
692IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
693#endif
694
f135ff73
VZ
695// -----------------------------------------------------------------------------
696// construction/destruction
697// -----------------------------------------------------------------------------
91b8de8d 698
941830cb 699void wxGenericTreeCtrl::Init()
c801d85f 700{
3e3a7b97 701 m_current = m_key_current = m_anchor = m_select_me = (wxGenericTreeItem *) NULL;
ca65c044
WS
702 m_hasFocus = false;
703 m_dirty = false;
f135ff73 704
00e12320
RR
705 m_lineHeight = 10;
706 m_indent = 15;
707 m_spacing = 18;
f135ff73 708
b771aa29
VZ
709 m_hilightBrush = new wxBrush
710 (
a756f210 711 wxSystemSettings::GetColour
b771aa29
VZ
712 (
713 wxSYS_COLOUR_HIGHLIGHT
714 ),
715 wxSOLID
716 );
717
718 m_hilightUnfocusedBrush = new wxBrush
719 (
a756f210 720 wxSystemSettings::GetColour
b771aa29
VZ
721 (
722 wxSYS_COLOUR_BTNSHADOW
723 ),
724 wxSOLID
725 );
f135ff73 726
618a5e38 727 m_imageListNormal = m_imageListButtons =
00e12320 728 m_imageListState = (wxImageList *) NULL;
618a5e38 729 m_ownsImageListNormal = m_ownsImageListButtons =
ca65c044 730 m_ownsImageListState = false;
978f38c2 731
00e12320 732 m_dragCount = 0;
ca65c044 733 m_isDragging = false;
f8b043e7
RR
734 m_dropTarget = m_oldSelection = NULL;
735 m_underMouse = NULL;
fbb12260 736 m_textCtrl = NULL;
9dfbf520 737
cb59313c 738 m_renameTimer = NULL;
e1983ab5
VZ
739 m_freezeCount = 0;
740
cb59313c
VZ
741 m_findTimer = NULL;
742
ca65c044 743 m_lastOnSame = false;
f38374d0 744
f89d65ea
SC
745#if defined( __WXMAC__ ) && __WXMAC_CARBON__
746 m_normalFont.MacCreateThemeFont( kThemeViewsFont ) ;
747#else
a756f210 748 m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
f89d65ea 749#endif
2b5f62a0
VZ
750 m_boldFont = wxFont(m_normalFont.GetPointSize(),
751 m_normalFont.GetFamily(),
752 m_normalFont.GetStyle(),
753 wxBOLD,
754 m_normalFont.GetUnderlined(),
755 m_normalFont.GetFaceName(),
756 m_normalFont.GetEncoding());
edaa81ae 757}
c801d85f 758
618a5e38
RR
759bool wxGenericTreeCtrl::Create(wxWindow *parent,
760 wxWindowID id,
761 const wxPoint& pos,
762 const wxSize& size,
763 long style,
ac8d0c11 764 const wxValidator& wxVALIDATOR_PARAM(validator),
618a5e38 765 const wxString& name )
c801d85f 766{
2593f033
RR
767#ifdef __WXMAC__
768 int major,minor;
769 wxGetOsVersion( &major, &minor );
574c939e 770
2593f033
RR
771 style &= ~wxTR_LINES_AT_ROOT;
772 style |= wxTR_NO_LINES;
773 if (major < 10)
774 style |= wxTR_ROW_LINES;
9c7f49f5 775#endif // __WXMAC__
17d5bdf9 776
618a5e38
RR
777 wxScrolledWindow::Create( parent, id, pos, size,
778 style|wxHSCROLL|wxVSCROLL, name );
779
6f0dd6b2
VZ
780 // If the tree display has no buttons, but does have
781 // connecting lines, we can use a narrower layout.
782 // It may not be a good idea to force this...
618a5e38
RR
783 if (!HasButtons() && !HasFlag(wxTR_NO_LINES))
784 {
785 m_indent= 10;
786 m_spacing = 10;
787 }
978f38c2 788
ce4169a4 789#if wxUSE_VALIDATORS
00e12320 790 SetValidator( validator );
ce4169a4 791#endif
f135ff73 792
ca65c044 793 wxVisualAttributes attr = GetDefaultAttributes();
fa47d7a7
VS
794 SetOwnForegroundColour( attr.colFg );
795 SetOwnBackgroundColour( attr.colBg );
796 SetOwnFont(attr.font);
c22886bd 797
00e12320 798// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
9e0a12c9 799 m_dottedPen = wxPen( wxT("grey"), 0, 0 );
f135ff73 800
35d4c967 801 SetBestSize(size);
ca65c044
WS
802
803 return true;
edaa81ae 804}
c801d85f 805
941830cb 806wxGenericTreeCtrl::~wxGenericTreeCtrl()
c801d85f 807{
b771aa29
VZ
808 delete m_hilightBrush;
809 delete m_hilightUnfocusedBrush;
574c939e 810
00e12320 811 DeleteAllItems();
9dfbf520 812
00e12320 813 delete m_renameTimer;
cb59313c
VZ
814 delete m_findTimer;
815
816 if (m_ownsImageListNormal)
817 delete m_imageListNormal;
818 if (m_ownsImageListState)
819 delete m_imageListState;
820 if (m_ownsImageListButtons)
821 delete m_imageListButtons;
edaa81ae 822}
c801d85f 823
f135ff73
VZ
824// -----------------------------------------------------------------------------
825// accessors
826// -----------------------------------------------------------------------------
827
941830cb 828size_t wxGenericTreeCtrl::GetCount() const
c801d85f 829{
ffda4dac
VZ
830 if ( !m_anchor )
831 {
832 // the tree is empty
833 return 0;
834 }
835
836 size_t count = m_anchor->GetChildrenCount();
837 if ( !HasFlag(wxTR_HIDE_ROOT) )
838 {
839 // take the root itself into account
840 count++;
841 }
842
843 return count;
edaa81ae 844}
c801d85f 845
941830cb 846void wxGenericTreeCtrl::SetIndent(unsigned int indent)
c801d85f 847{
574c939e 848 m_indent = (unsigned short) indent;
ca65c044 849 m_dirty = true;
cf724bce
RR
850}
851
941830cb 852void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
cf724bce 853{
574c939e 854 m_spacing = (unsigned short) spacing;
ca65c044 855 m_dirty = true;
f135ff73 856}
74bedbeb 857
df74fdea
VZ
858size_t
859wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
860 bool recursively) const
4832f7c0 861{
f2593d0d 862 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
4832f7c0 863
941830cb 864 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
4832f7c0
VZ
865}
866
618a5e38
RR
867void wxGenericTreeCtrl::SetWindowStyle(const long styles)
868{
374a8e5c
VS
869 if (!HasFlag(wxTR_HIDE_ROOT) && (styles & wxTR_HIDE_ROOT))
870 {
871 // if we will hide the root, make sure children are visible
872 m_anchor->SetHasPlus();
873 m_anchor->Expand();
874 CalculatePositions();
875 }
876
877 // right now, just sets the styles. Eventually, we may
878 // want to update the inherited styles, but right now
879 // none of the parents has updatable styles
618a5e38 880 m_windowStyle = styles;
ca65c044 881 m_dirty = true;
618a5e38
RR
882}
883
f135ff73
VZ
884// -----------------------------------------------------------------------------
885// functions to work with tree items
886// -----------------------------------------------------------------------------
74bedbeb 887
941830cb 888wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
f135ff73 889{
f2593d0d 890 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
4832f7c0 891
941830cb 892 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
edaa81ae 893}
74bedbeb 894
941830cb 895int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
8dc99046 896 wxTreeItemIcon which) const
74bedbeb 897{
f2593d0d 898 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 899
941830cb 900 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
edaa81ae 901}
c801d85f 902
941830cb 903wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 904{
f2593d0d 905 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 906
941830cb 907 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
edaa81ae 908}
c801d85f 909
2b5f62a0
VZ
910wxColour wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
911{
912 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
913
914 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
915 return pItem->Attr().GetTextColour();
916}
917
918wxColour wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
919{
920 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
921
922 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
923 return pItem->Attr().GetBackgroundColour();
924}
925
926wxFont wxGenericTreeCtrl::GetItemFont(const wxTreeItemId& item) const
927{
928 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
929
930 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
931 return pItem->Attr().GetFont();
932}
933
941830cb 934void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
f135ff73 935{
f2593d0d 936 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 937
f2593d0d 938 wxClientDC dc(this);
941830cb 939 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
940 pItem->SetText(text);
941 CalculateSize(pItem, dc);
942 RefreshLine(pItem);
f135ff73 943}
c801d85f 944
941830cb 945void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
8dc99046
VZ
946 int image,
947 wxTreeItemIcon which)
f135ff73 948{
223d09f6 949 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 950
941830cb 951 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
8dc99046 952 pItem->SetImage(image, which);
4832f7c0 953
8dc99046
VZ
954 wxClientDC dc(this);
955 CalculateSize(pItem, dc);
956 RefreshLine(pItem);
edaa81ae 957}
c801d85f 958
941830cb 959void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 960{
f2593d0d 961 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 962
74707d69
RR
963 if (data)
964 data->SetId( item );
ca65c044 965
941830cb 966 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
edaa81ae 967}
c801d85f 968
941830cb 969void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 970{
f2593d0d 971 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 972
941830cb 973 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
974 pItem->SetHasPlus(has);
975 RefreshLine(pItem);
edaa81ae 976}
c801d85f 977
941830cb 978void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
ef44a621 979{
9ec64fa7 980 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 981
9ec64fa7 982 // avoid redrawing the tree if no real change
941830cb 983 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
984 if ( pItem->IsBold() != bold )
985 {
986 pItem->SetBold(bold);
987 RefreshLine(pItem);
988 }
989}
990
941830cb 991void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
9ec64fa7
VZ
992 const wxColour& col)
993{
994 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
995
941830cb 996 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
997 pItem->Attr().SetTextColour(col);
998 RefreshLine(pItem);
999}
1000
941830cb 1001void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
9ec64fa7
VZ
1002 const wxColour& col)
1003{
1004 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1005
941830cb 1006 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
1007 pItem->Attr().SetBackgroundColour(col);
1008 RefreshLine(pItem);
1009}
1010
941830cb 1011void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
9ec64fa7
VZ
1012{
1013 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1014
941830cb 1015 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7 1016 pItem->Attr().SetFont(font);
ef44a621 1017 RefreshLine(pItem);
ef44a621
VZ
1018}
1019
3da2715f
JS
1020bool wxGenericTreeCtrl::SetFont( const wxFont &font )
1021{
1022 wxScrolledWindow::SetFont(font);
1023
1024 m_normalFont = font ;
2b5f62a0
VZ
1025 m_boldFont = wxFont(m_normalFont.GetPointSize(),
1026 m_normalFont.GetFamily(),
1027 m_normalFont.GetStyle(),
1028 wxBOLD,
1029 m_normalFont.GetUnderlined(),
1030 m_normalFont.GetFaceName(),
1031 m_normalFont.GetEncoding());
3da2715f 1032
ca65c044 1033 return true;
3da2715f
JS
1034}
1035
1036
f135ff73
VZ
1037// -----------------------------------------------------------------------------
1038// item status inquiries
1039// -----------------------------------------------------------------------------
1040
1ed01484 1041bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
c801d85f 1042{
ca65c044 1043 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1ed01484 1044
c22886bd 1045 // An item is only visible if it's not a descendant of a collapsed item
1ed01484 1046 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
c22886bd
RR
1047 wxGenericTreeItem* parent = pItem->GetParent();
1048 while (parent)
1049 {
1050 if (!parent->IsExpanded())
ca65c044 1051 return false;
c22886bd
RR
1052 parent = parent->GetParent();
1053 }
1ed01484
JS
1054
1055 int startX, startY;
1056 GetViewStart(& startX, & startY);
1057
1058 wxSize clientSize = GetClientSize();
1059
1060 wxRect rect;
1061 if (!GetBoundingRect(item, rect))
ca65c044 1062 return false;
c22886bd 1063 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
ca65c044 1064 return false;
1ed01484 1065 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
ca65c044 1066 return false;
1ed01484 1067 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
ca65c044 1068 return false;
f135ff73 1069
ca65c044 1070 return true;
edaa81ae 1071}
c801d85f 1072
941830cb 1073bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 1074{
ca65c044 1075 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
4832f7c0 1076
59a2e635
VZ
1077 // consider that the item does have children if it has the "+" button: it
1078 // might not have them (if it had never been expanded yet) but then it
1079 // could have them as well and it's better to err on this side rather than
1080 // disabling some operations which are restricted to the items with
1081 // children for an item which does have them
607d9cfc 1082 return ((wxGenericTreeItem*) item.m_pItem)->HasPlus();
edaa81ae 1083}
c801d85f 1084
941830cb 1085bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 1086{
ca65c044 1087 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
4832f7c0 1088
941830cb 1089 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
f135ff73 1090}
29d87bba 1091
941830cb 1092bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
f135ff73 1093{
ca65c044 1094 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
4832f7c0 1095
941830cb 1096 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
f135ff73 1097}
29d87bba 1098
941830cb 1099bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
ef44a621 1100{
ca65c044 1101 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
ef44a621 1102
941830cb 1103 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
ef44a621
VZ
1104}
1105
f135ff73
VZ
1106// -----------------------------------------------------------------------------
1107// navigation
1108// -----------------------------------------------------------------------------
29d87bba 1109
99006e44 1110wxTreeItemId wxGenericTreeCtrl::GetItemParent(const wxTreeItemId& item) const
f135ff73 1111{
618a5e38 1112 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 1113
618a5e38 1114 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
f135ff73 1115}
29d87bba 1116
ee4b2721
VZ
1117wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1118 wxTreeItemIdValue& cookie) const
f135ff73 1119{
618a5e38 1120 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1121
618a5e38
RR
1122 cookie = 0;
1123 return GetNextChild(item, cookie);
f135ff73 1124}
29d87bba 1125
ee4b2721
VZ
1126wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1127 wxTreeItemIdValue& cookie) const
1128{
1129 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1130
1131 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1132
1133 // it's ok to cast cookie to size_t, we never have indices big enough to
1134 // overflow "void *"
1135 size_t *pIndex = (size_t *)&cookie;
1136 if ( *pIndex < children.Count() )
1137 {
836543b8 1138 return children.Item((*pIndex)++);
ee4b2721
VZ
1139 }
1140 else
1141 {
1142 // there are no more of them
1143 return wxTreeItemId();
1144 }
1145}
1146
1147#if WXWIN_COMPATIBILITY_2_4
1148
1149wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1150 long& cookie) const
1151{
1152 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1153
1154 cookie = 0;
1155 return GetNextChild(item, cookie);
1156}
1157
1158wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1159 long& cookie) const
f135ff73 1160{
618a5e38 1161 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1162
618a5e38
RR
1163 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1164 if ( (size_t)cookie < children.Count() )
1165 {
1166 return children.Item((size_t)cookie++);
1167 }
1168 else
1169 {
1170 // there are no more of them
1171 return wxTreeItemId();
1172 }
f135ff73 1173}
29d87bba 1174
ee4b2721
VZ
1175#endif // WXWIN_COMPATIBILITY_2_4
1176
941830cb 1177wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
978f38c2 1178{
618a5e38 1179 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 1180
618a5e38
RR
1181 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1182 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
1183}
1184
941830cb 1185wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
f135ff73 1186{
618a5e38 1187 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1188
618a5e38
RR
1189 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1190 wxGenericTreeItem *parent = i->GetParent();
1191 if ( parent == NULL )
1192 {
1193 // root item doesn't have any siblings
1194 return wxTreeItemId();
1195 }
4832f7c0 1196
618a5e38
RR
1197 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1198 int index = siblings.Index(i);
1199 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1200
618a5e38
RR
1201 size_t n = (size_t)(index + 1);
1202 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 1203}
c801d85f 1204
941830cb 1205wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 1206{
618a5e38 1207 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1208
618a5e38
RR
1209 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1210 wxGenericTreeItem *parent = i->GetParent();
1211 if ( parent == NULL )
1212 {
1213 // root item doesn't have any siblings
1214 return wxTreeItemId();
1215 }
4832f7c0 1216
618a5e38
RR
1217 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1218 int index = siblings.Index(i);
1219 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1220
618a5e38
RR
1221 return index == 0 ? wxTreeItemId()
1222 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 1223}
389cdc7a 1224
1ed01484
JS
1225// Only for internal use right now, but should probably be public
1226wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
f135ff73 1227{
618a5e38
RR
1228 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1229
1230 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1231
1232 // First see if there are any children.
1233 wxArrayGenericTreeItems& children = i->GetChildren();
1234 if (children.GetCount() > 0)
1235 {
1236 return children.Item(0);
1237 }
1238 else
1239 {
1240 // Try a sibling of this or ancestor instead
1241 wxTreeItemId p = item;
1242 wxTreeItemId toFind;
1243 do
1244 {
1245 toFind = GetNextSibling(p);
99006e44 1246 p = GetItemParent(p);
618a5e38
RR
1247 } while (p.IsOk() && !toFind.IsOk());
1248 return toFind;
1249 }
1ed01484
JS
1250}
1251
1ed01484
JS
1252wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1253{
618a5e38
RR
1254 wxTreeItemId id = GetRootItem();
1255 if (!id.IsOk())
1ed01484 1256 return id;
1ed01484 1257
618a5e38
RR
1258 do
1259 {
1260 if (IsVisible(id))
1261 return id;
1262 id = GetNext(id);
1263 } while (id.IsOk());
1264
1265 return wxTreeItemId();
1ed01484
JS
1266}
1267
941830cb 1268wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
f135ff73 1269{
618a5e38 1270 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1271
618a5e38
RR
1272 wxTreeItemId id = item;
1273 if (id.IsOk())
1274 {
1275 while (id = GetNext(id), id.IsOk())
1276 {
1277 if (IsVisible(id))
1278 return id;
1279 }
1280 }
1281 return wxTreeItemId();
f135ff73 1282}
29d87bba 1283
941830cb 1284wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
f135ff73 1285{
618a5e38 1286 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1287
618a5e38 1288 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1289
618a5e38 1290 return wxTreeItemId();
edaa81ae 1291}
c801d85f 1292
fbb12260
JS
1293// called by wxTextTreeCtrl when it marks itself for deletion
1294void wxGenericTreeCtrl::ResetTextControl()
1295{
1296 m_textCtrl = NULL;
1297}
1298
cb59313c
VZ
1299// find the first item starting with the given prefix after the given item
1300wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent,
1301 const wxString& prefixOrig) const
1302{
1303 // match is case insensitive as this is more convenient to the user: having
1304 // to press Shift-letter to go to the item starting with a capital letter
1305 // would be too bothersome
1306 wxString prefix = prefixOrig.Lower();
1307
526b8142
VZ
1308 // determine the starting point: we shouldn't take the current item (this
1309 // allows to switch between two items starting with the same letter just by
1310 // pressing it) but we shouldn't jump to the next one if the user is
1311 // continuing to type as otherwise he might easily skip the item he wanted
cb59313c 1312 wxTreeItemId id = idParent;
526b8142
VZ
1313 if ( prefix.length() == 1 )
1314 {
1315 id = GetNext(id);
1316 }
cb59313c 1317
526b8142 1318 // look for the item starting with the given prefix after it
cb59313c
VZ
1319 while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) )
1320 {
1321 id = GetNext(id);
1322 }
1323
526b8142
VZ
1324 // if we haven't found anything...
1325 if ( !id.IsOk() )
1326 {
1327 // ... wrap to the beginning
1328 id = GetRootItem();
1329 if ( HasFlag(wxTR_HIDE_ROOT) )
1330 {
1331 // can't select virtual root
1332 id = GetNext(id);
1333 }
1334
1335 // and try all the items (stop when we get to the one we started from)
1336 while ( id != idParent && !GetItemText(id).Lower().StartsWith(prefix) )
1337 {
1338 id = GetNext(id);
1339 }
1340 }
1341
cb59313c
VZ
1342 return id;
1343}
1344
f135ff73
VZ
1345// -----------------------------------------------------------------------------
1346// operations
1347// -----------------------------------------------------------------------------
1348
941830cb 1349wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1350 size_t previous,
1351 const wxString& text,
1352 int image, int selImage,
1353 wxTreeItemData *data)
c801d85f 1354{
941830cb 1355 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f49f2b0c
RR
1356 if ( !parent )
1357 {
1358 // should we give a warning here?
1359 return AddRoot(text, image, selImage, data);
1360 }
4832f7c0 1361
ca65c044 1362 m_dirty = true; // do this first so stuff below doesn't cause flicker
618a5e38 1363
f38374d0 1364 wxGenericTreeItem *item =
2b84e565 1365 new wxGenericTreeItem( parent, text, image, selImage, data );
74bedbeb 1366
f49f2b0c
RR
1367 if ( data != NULL )
1368 {
ee4b2721 1369 data->m_pItem = item;
f49f2b0c 1370 }
74bedbeb 1371
f49f2b0c 1372 parent->Insert( item, previous );
ef44a621 1373
f49f2b0c 1374 return item;
4c681997
RR
1375}
1376
941830cb 1377wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
f135ff73
VZ
1378 int image, int selImage,
1379 wxTreeItemData *data)
4c681997 1380{
f49f2b0c 1381 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 1382
ca65c044 1383 m_dirty = true; // do this first so stuff below doesn't cause flicker
618a5e38 1384
2b84e565 1385 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text,
f135ff73 1386 image, selImage, data);
cf31a1d7
VS
1387 if ( data != NULL )
1388 {
ee4b2721 1389 data->m_pItem = m_anchor;
cf31a1d7
VS
1390 }
1391
618a5e38
RR
1392 if (HasFlag(wxTR_HIDE_ROOT))
1393 {
1394 // if root is hidden, make sure we can navigate
1395 // into children
1396 m_anchor->SetHasPlus();
999723f3
VS
1397 m_anchor->Expand();
1398 CalculatePositions();
618a5e38 1399 }
f38374d0 1400
f49f2b0c
RR
1401 if (!HasFlag(wxTR_MULTIPLE))
1402 {
1403 m_current = m_key_current = m_anchor;
ca65c044 1404 m_current->SetHilight( true );
f49f2b0c 1405 }
389cdc7a 1406
f49f2b0c 1407 return m_anchor;
edaa81ae 1408}
c801d85f 1409
941830cb 1410wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
f135ff73
VZ
1411 const wxString& text,
1412 int image, int selImage,
1413 wxTreeItemData *data)
c801d85f 1414{
f2593d0d 1415 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 1416}
c801d85f 1417
941830cb 1418wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1419 const wxTreeItemId& idPrevious,
1420 const wxString& text,
1421 int image, int selImage,
1422 wxTreeItemData *data)
c801d85f 1423{
941830cb 1424 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1425 if ( !parent )
1426 {
1427 // should we give a warning here?
1428 return AddRoot(text, image, selImage, data);
1429 }
c801d85f 1430
4855a477
JS
1431 int index = -1;
1432 if (idPrevious.IsOk())
1433 {
1434 index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1435 wxASSERT_MSG( index != wxNOT_FOUND,
1436 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1437 }
06b466c7 1438
f2593d0d
RR
1439 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1440}
1441
941830cb 1442wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f2593d0d
RR
1443 size_t before,
1444 const wxString& text,
1445 int image, int selImage,
1446 wxTreeItemData *data)
1447{
941830cb 1448 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1449 if ( !parent )
1450 {
1451 // should we give a warning here?
1452 return AddRoot(text, image, selImage, data);
1453 }
1454
1455 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 1456}
c801d85f 1457
941830cb 1458wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
f135ff73
VZ
1459 const wxString& text,
1460 int image, int selImage,
1461 wxTreeItemData *data)
74bedbeb 1462{
941830cb 1463 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1464 if ( !parent )
1465 {
1466 // should we give a warning here?
1467 return AddRoot(text, image, selImage, data);
1468 }
f135ff73 1469
f2593d0d
RR
1470 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1471 image, selImage, data);
74bedbeb
VZ
1472}
1473
941830cb 1474void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
a43a4f9d 1475{
f2593d0d 1476 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
ee4b2721 1477 event.m_item = item;
f2593d0d
RR
1478 event.SetEventObject( this );
1479 ProcessEvent( event );
a43a4f9d
VZ
1480}
1481
941830cb 1482void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
372edb9d 1483{
ca65c044 1484 m_dirty = true; // do this first so stuff below doesn't cause flicker
618a5e38 1485
941830cb 1486 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
a43a4f9d 1487 item->DeleteChildren(this);
372edb9d
VZ
1488}
1489
941830cb 1490void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1491{
ca65c044 1492 m_dirty = true; // do this first so stuff below doesn't cause flicker
618a5e38 1493
941830cb 1494 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
ff5bf259 1495
7475e8a3
VZ
1496 wxGenericTreeItem *parent = item->GetParent();
1497
1498 // don't keep stale pointers around!
1499 if ( IsDescendantOf(item, m_key_current) )
aaa2f297 1500 {
3e3a7b97
JS
1501 // Don't silently change the selection:
1502 // do it properly in idle time, so event
1503 // handlers get called.
ca65c044 1504
3e3a7b97
JS
1505 // m_key_current = parent;
1506 m_key_current = NULL;
1507 }
1508
1509 // m_select_me records whether we need to select
1510 // a different item, in idle time.
1511 if ( m_select_me && IsDescendantOf(item, m_select_me) )
1512 {
1513 m_select_me = parent;
aaa2f297
VZ
1514 }
1515
7475e8a3
VZ
1516 if ( IsDescendantOf(item, m_current) )
1517 {
3e3a7b97
JS
1518 // Don't silently change the selection:
1519 // do it properly in idle time, so event
1520 // handlers get called.
ca65c044 1521
3e3a7b97
JS
1522 // m_current = parent;
1523 m_current = NULL;
1524 m_select_me = parent;
7475e8a3
VZ
1525 }
1526
1527 // remove the item from the tree
97d7bfb8
RR
1528 if ( parent )
1529 {
1530 parent->GetChildren().Remove( item ); // remove by value
1531 }
7475e8a3 1532 else // deleting the root
aaa2f297 1533 {
7475e8a3
VZ
1534 // nothing will be left in the tree
1535 m_anchor = NULL;
aaa2f297
VZ
1536 }
1537
7475e8a3 1538 // and delete all of its children and the item itself now
97d7bfb8
RR
1539 item->DeleteChildren(this);
1540 SendDeleteEvent(item);
1541 delete item;
edaa81ae 1542}
c801d85f 1543
941830cb 1544void wxGenericTreeCtrl::DeleteAllItems()
c801d85f 1545{
97d7bfb8
RR
1546 if ( m_anchor )
1547 {
7475e8a3 1548 Delete(m_anchor);
97d7bfb8 1549 }
edaa81ae
RR
1550}
1551
941830cb 1552void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1553{
941830cb 1554 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1555
941830cb 1556 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
7a944d2f 1557 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
999723f3 1558 _T("can't expand hidden root") );
f6bcfd97 1559
f2593d0d
RR
1560 if ( !item->HasPlus() )
1561 return;
978f38c2 1562
f2593d0d
RR
1563 if ( item->IsExpanded() )
1564 return;
f135ff73 1565
f2593d0d 1566 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
ee4b2721 1567 event.m_item = item;
f2593d0d 1568 event.SetEventObject( this );
004fd0c8 1569
f2593d0d
RR
1570 if ( ProcessEvent( event ) && !event.IsAllowed() )
1571 {
1572 // cancelled by program
1573 return;
1574 }
4832f7c0 1575
f2593d0d
RR
1576 item->Expand();
1577 CalculatePositions();
f135ff73 1578
f2593d0d 1579 RefreshSubtree(item);
f135ff73 1580
f2593d0d
RR
1581 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1582 ProcessEvent( event );
edaa81ae
RR
1583}
1584
941830cb 1585void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
f6bcfd97 1586{
1fc32b12 1587 if ( !HasFlag(wxTR_HIDE_ROOT) || item != GetRootItem())
f6bcfd97 1588 {
1fc32b12
JS
1589 Expand(item);
1590 if ( !IsExpanded(item) )
1591 return;
1592 }
6151e144 1593
2d75caaa 1594 wxTreeItemIdValue cookie;
1fc32b12
JS
1595 wxTreeItemId child = GetFirstChild(item, cookie);
1596 while ( child.IsOk() )
1597 {
1598 ExpandAll(child);
6151e144 1599
1fc32b12 1600 child = GetNextChild(item, cookie);
f6bcfd97
BP
1601 }
1602}
1603
941830cb 1604void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1605{
7a944d2f 1606 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
999723f3
VS
1607 _T("can't collapse hidden root") );
1608
941830cb 1609 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1610
f2593d0d
RR
1611 if ( !item->IsExpanded() )
1612 return;
f135ff73 1613
f2593d0d 1614 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
ee4b2721 1615 event.m_item = item;
f2593d0d
RR
1616 event.SetEventObject( this );
1617 if ( ProcessEvent( event ) && !event.IsAllowed() )
1618 {
1619 // cancelled by program
1620 return;
1621 }
4832f7c0 1622
f2593d0d 1623 item->Collapse();
f135ff73 1624
618a5e38 1625#if 0 // TODO why should items be collapsed recursively?
f2593d0d
RR
1626 wxArrayGenericTreeItems& children = item->GetChildren();
1627 size_t count = children.Count();
1628 for ( size_t n = 0; n < count; n++ )
1629 {
1630 Collapse(children[n]);
1631 }
618a5e38 1632#endif
f135ff73 1633
f2593d0d 1634 CalculatePositions();
f135ff73 1635
f2593d0d 1636 RefreshSubtree(item);
f135ff73 1637
f2593d0d
RR
1638 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1639 ProcessEvent( event );
edaa81ae 1640}
c801d85f 1641
941830cb 1642void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1643{
00e12320
RR
1644 Collapse(item);
1645 DeleteChildren(item);
edaa81ae 1646}
c801d85f 1647
941830cb 1648void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1649{
941830cb 1650 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
389cdc7a 1651
00e12320
RR
1652 if (item->IsExpanded())
1653 Collapse(itemId);
1654 else
1655 Expand(itemId);
f135ff73 1656}
389cdc7a 1657
941830cb 1658void wxGenericTreeCtrl::Unselect()
f135ff73 1659{
00e12320
RR
1660 if (m_current)
1661 {
ca65c044 1662 m_current->SetHilight( false );
00e12320 1663 RefreshLine( m_current );
02fe25c2
VZ
1664
1665 m_current = NULL;
3e3a7b97 1666 m_select_me = NULL;
00e12320 1667 }
edaa81ae 1668}
c801d85f 1669
941830cb 1670void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1671{
00e12320
RR
1672 if (item->IsSelected())
1673 {
ca65c044 1674 item->SetHilight(false);
00e12320
RR
1675 RefreshLine(item);
1676 }
d3a9f4af 1677
00e12320 1678 if (item->HasChildren())
88ac883a 1679 {
00e12320
RR
1680 wxArrayGenericTreeItems& children = item->GetChildren();
1681 size_t count = children.Count();
1682 for ( size_t n = 0; n < count; ++n )
1683 {
1684 UnselectAllChildren(children[n]);
1685 }
88ac883a
VZ
1686 }
1687}
f135ff73 1688
941830cb 1689void wxGenericTreeCtrl::UnselectAll()
88ac883a 1690{
0a33446c
VZ
1691 wxTreeItemId rootItem = GetRootItem();
1692
1693 // the tree might not have the root item at all
1694 if ( rootItem )
1695 {
1696 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1697 }
88ac883a
VZ
1698}
1699
1700// Recursive function !
1701// To stop we must have crt_item<last_item
91b8de8d 1702// Algorithm :
88ac883a 1703// Tag all next children, when no more children,
d3a9f4af 1704// Move to parent (not to tag)
91b8de8d 1705// Keep going... if we found last_item, we stop.
941830cb 1706bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a
VZ
1707{
1708 wxGenericTreeItem *parent = crt_item->GetParent();
1709
00e12320
RR
1710 if (parent == NULL) // This is root item
1711 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1712
91b8de8d 1713 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1714 int index = children.Index(crt_item);
1715 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1716
1717 size_t count = children.Count();
1718 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320 1719 {
ca65c044 1720 if (TagAllChildrenUntilLast(children[n], last_item, select)) return true;
00e12320 1721 }
88ac883a
VZ
1722
1723 return TagNextChildren(parent, last_item, select);
1724}
1725
941830cb 1726bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a 1727{
00e12320
RR
1728 crt_item->SetHilight(select);
1729 RefreshLine(crt_item);
d3a9f4af 1730
06b466c7 1731 if (crt_item==last_item)
ca65c044 1732 return true;
88ac883a 1733
00e12320 1734 if (crt_item->HasChildren())
88ac883a 1735 {
00e12320
RR
1736 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1737 size_t count = children.Count();
1738 for ( size_t n = 0; n < count; ++n )
1739 {
06b466c7 1740 if (TagAllChildrenUntilLast(children[n], last_item, select))
ca65c044 1741 return true;
06b466c7 1742 }
88ac883a 1743 }
d3a9f4af 1744
ca65c044 1745 return false;
88ac883a
VZ
1746}
1747
941830cb 1748void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
88ac883a 1749{
3e3a7b97 1750 m_select_me = NULL;
88ac883a 1751
999836aa 1752 // item2 is not necessary after item1
f2593d0d 1753 // choice first' and 'last' between item1 and item2
999836aa
VZ
1754 wxGenericTreeItem *first= (item1->GetY()<item2->GetY()) ? item1 : item2;
1755 wxGenericTreeItem *last = (item1->GetY()<item2->GetY()) ? item2 : item1;
88ac883a 1756
f2593d0d 1757 bool select = m_current->IsSelected();
d3a9f4af 1758
f2593d0d
RR
1759 if ( TagAllChildrenUntilLast(first,last,select) )
1760 return;
88ac883a 1761
f2593d0d 1762 TagNextChildren(first,last,select);
88ac883a
VZ
1763}
1764
78f12104
VZ
1765void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId& itemId,
1766 bool unselect_others,
1767 bool extended_select)
d3a9f4af 1768{
223d09f6 1769 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1770
3e3a7b97 1771 m_select_me = NULL;
ca65c044 1772
88ac883a 1773 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
941830cb 1774 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
88ac883a
VZ
1775
1776 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1777 // wxT("this is a single selection tree") );
88ac883a
VZ
1778
1779 // to keep going anyhow !!!
d3a9f4af 1780 if (is_single)
8dc99046
VZ
1781 {
1782 if (item->IsSelected())
1783 return; // nothing to do
ca65c044
WS
1784 unselect_others = true;
1785 extended_select = false;
8dc99046
VZ
1786 }
1787 else if ( unselect_others && item->IsSelected() )
1788 {
1789 // selection change if there is more than one item currently selected
1790 wxArrayTreeItemIds selected_items;
1791 if ( GetSelections(selected_items) == 1 )
1792 return;
1793 }
d3a9f4af 1794
f135ff73 1795 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
ee4b2721
VZ
1796 event.m_item = item;
1797 event.m_itemOld = m_current;
f135ff73 1798 event.SetEventObject( this );
91b8de8d 1799 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1800
f98e2558 1801 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
618a5e38 1802 return;
f135ff73 1803
99006e44 1804 wxTreeItemId parent = GetItemParent( itemId );
2cc78389
RR
1805 while (parent.IsOk())
1806 {
1807 if (!IsExpanded(parent))
1808 Expand( parent );
cdb3cffe 1809
99006e44 1810 parent = GetItemParent( parent );
2cc78389 1811 }
cdb3cffe 1812
2cc78389 1813 EnsureVisible( itemId );
cdb3cffe 1814
88ac883a
VZ
1815 // ctrl press
1816 if (unselect_others)
389cdc7a 1817 {
88ac883a 1818 if (is_single) Unselect(); // to speed up thing
c193b707 1819 else UnselectAll();
edaa81ae 1820 }
f135ff73 1821
88ac883a 1822 // shift press
d3a9f4af 1823 if (extended_select)
88ac883a 1824 {
aaa2f297
VZ
1825 if ( !m_current )
1826 {
618a5e38 1827 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
1828 }
1829
88ac883a
VZ
1830 // don't change the mark (m_current)
1831 SelectItemRange(m_current, item);
1832 }
1833 else
1834 {
ca65c044 1835 bool select = true; // the default
88ac883a 1836
c193b707
VZ
1837 // Check if we need to toggle hilight (ctrl mode)
1838 if (!unselect_others)
618a5e38 1839 select=!item->IsSelected();
88ac883a 1840
91b8de8d 1841 m_current = m_key_current = item;
c193b707
VZ
1842 m_current->SetHilight(select);
1843 RefreshLine( m_current );
88ac883a 1844 }
389cdc7a 1845
f135ff73 1846 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1847 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1848}
1849
78f12104
VZ
1850void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, bool select)
1851{
1852 if ( select )
1853 {
1854 DoSelectItem(itemId);
1855 }
1856 else // deselect
1857 {
1858 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1859 wxCHECK_RET( item, wxT("SelectItem(): invalid tree item") );
1860
ca65c044 1861 item->SetHilight(false);
78f12104
VZ
1862 RefreshLine(item);
1863 }
1864}
1865
941830cb 1866void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
cb59313c 1867 wxArrayTreeItemIds &array) const
c801d85f 1868{
8dc99046 1869 if ( item->IsSelected() )
9dfbf520 1870 array.Add(wxTreeItemId(item));
91b8de8d 1871
9dfbf520 1872 if ( item->HasChildren() )
91b8de8d 1873 {
9dfbf520
VZ
1874 wxArrayGenericTreeItems& children = item->GetChildren();
1875 size_t count = children.GetCount();
1876 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1877 FillArray(children[n], array);
91b8de8d
RR
1878 }
1879}
1880
941830cb 1881size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d 1882{
618a5e38
RR
1883 array.Empty();
1884 wxTreeItemId idRoot = GetRootItem();
1885 if ( idRoot.IsOk() )
1886 {
1887 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1888 }
1889 //else: the tree is empty, so no selections
91b8de8d 1890
618a5e38 1891 return array.Count();
91b8de8d
RR
1892}
1893
941830cb 1894void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1895{
05b2a432
RD
1896 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1897
91b8de8d
RR
1898 if (!item.IsOk()) return;
1899
941830cb 1900 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 1901
f65635b5
VZ
1902 // first expand all parent branches
1903 wxGenericTreeItem *parent = gitem->GetParent();
999723f3
VS
1904
1905 if ( HasFlag(wxTR_HIDE_ROOT) )
f65635b5 1906 {
ff38281a 1907 while ( parent && parent != m_anchor )
999723f3
VS
1908 {
1909 Expand(parent);
1910 parent = parent->GetParent();
1911 }
1912 }
1913 else
1914 {
1915 while ( parent )
1916 {
1917 Expand(parent);
1918 parent = parent->GetParent();
1919 }
f65635b5
VZ
1920 }
1921
5391f772 1922 //if (parent) CalculatePositions();
91b8de8d
RR
1923
1924 ScrollTo(item);
1925}
1926
941830cb 1927void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d
RR
1928{
1929 if (!item.IsOk()) return;
1930
dc6c62a9
RR
1931 // We have to call this here because the label in
1932 // question might just have been added and no screen
1933 // update taken place.
ca65c044 1934 if (m_dirty)
f89d65ea
SC
1935#if defined( __WXMSW__ ) || defined(__WXMAC__)
1936 Update();
1937#else
1938 wxYieldIfNeeded();
1939#endif
941830cb 1940 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1941
f65635b5 1942 // now scroll to the item
0659e7ee 1943 int item_y = gitem->GetY();
8dc99046 1944
0659e7ee
RR
1945 int start_x = 0;
1946 int start_y = 0;
1e6feb95 1947 GetViewStart( &start_x, &start_y );
91b8de8d 1948 start_y *= PIXELS_PER_UNIT;
978f38c2 1949
a93109d5
RR
1950 int client_h = 0;
1951 int client_w = 0;
1952 GetClientSize( &client_w, &client_h );
ef44a621 1953
0659e7ee
RR
1954 if (item_y < start_y+3)
1955 {
91b8de8d 1956 // going down
0659e7ee
RR
1957 int x = 0;
1958 int y = 0;
91b8de8d
RR
1959 m_anchor->GetSize( x, y, this );
1960 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1961 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1962 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1963 // Item should appear at top
91b8de8d 1964 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1965 }
91b8de8d 1966 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1967 {
c7a9fa36
RR
1968 // going up
1969 int x = 0;
1970 int y = 0;
1971 m_anchor->GetSize( x, y, this );
1972 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1973 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1974 item_y += PIXELS_PER_UNIT+2;
1975 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1976 // Item should appear at bottom
c7a9fa36 1977 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, (item_y+GetLineHeight(gitem)-client_h)/PIXELS_PER_UNIT );
0659e7ee 1978 }
edaa81ae 1979}
c801d85f 1980
e1ee62bd 1981// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 1982static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1983
004fd0c8 1984static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1985 wxGenericTreeItem **item2)
edaa81ae 1986{
941830cb 1987 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1988
1989 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1990}
1991
941830cb 1992int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
e1ee62bd 1993 const wxTreeItemId& item2)
0659e7ee 1994{
87138c52 1995 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1996}
1997
941830cb 1998void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 1999{
223d09f6 2000 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 2001
941830cb 2002 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 2003
e1ee62bd 2004 wxCHECK_RET( !s_treeBeingSorted,
941830cb 2005 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 2006
91b8de8d 2007 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
2008 if ( children.Count() > 1 )
2009 {
ca65c044 2010 m_dirty = true;
618a5e38 2011
e1ee62bd
VZ
2012 s_treeBeingSorted = this;
2013 children.Sort(tree_ctrl_compare_func);
2014 s_treeBeingSorted = NULL;
e1ee62bd
VZ
2015 }
2016 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
2017}
2018
941830cb 2019wxImageList *wxGenericTreeCtrl::GetImageList() const
edaa81ae 2020{
f135ff73 2021 return m_imageListNormal;
edaa81ae
RR
2022}
2023
618a5e38
RR
2024wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
2025{
2026 return m_imageListButtons;
2027}
2028
941830cb 2029wxImageList *wxGenericTreeCtrl::GetStateImageList() const
c801d85f 2030{
f135ff73 2031 return m_imageListState;
edaa81ae 2032}
c801d85f 2033
618a5e38 2034void wxGenericTreeCtrl::CalculateLineHeight()
e2414cbe 2035{
f2593d0d
RR
2036 wxClientDC dc(this);
2037 m_lineHeight = (int)(dc.GetCharHeight() + 4);
06b466c7 2038
618a5e38
RR
2039 if ( m_imageListNormal )
2040 {
2041 // Calculate a m_lineHeight value from the normal Image sizes.
2042 // May be toggle off. Then wxGenericTreeCtrl will spread when
2043 // necessary (which might look ugly).
2044 int n = m_imageListNormal->GetImageCount();
2045 for (int i = 0; i < n ; i++)
2046 {
2047 int width = 0, height = 0;
2048 m_imageListNormal->GetSize(i, width, height);
2049 if (height > m_lineHeight) m_lineHeight = height;
2050 }
2051 }
2052
2053 if (m_imageListButtons)
f2593d0d 2054 {
618a5e38
RR
2055 // Calculate a m_lineHeight value from the Button image sizes.
2056 // May be toggle off. Then wxGenericTreeCtrl will spread when
2057 // necessary (which might look ugly).
2058 int n = m_imageListButtons->GetImageCount();
2059 for (int i = 0; i < n ; i++)
2060 {
2061 int width = 0, height = 0;
2062 m_imageListButtons->GetSize(i, width, height);
2063 if (height > m_lineHeight) m_lineHeight = height;
2064 }
f2593d0d 2065 }
91b8de8d 2066
618a5e38 2067 if (m_lineHeight < 30)
f2593d0d 2068 m_lineHeight += 2; // at least 2 pixels
06b466c7 2069 else
f2593d0d 2070 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 2071}
e2414cbe 2072
618a5e38
RR
2073void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
2074{
2075 if (m_ownsImageListNormal) delete m_imageListNormal;
2076 m_imageListNormal = imageList;
ca65c044
WS
2077 m_ownsImageListNormal = false;
2078 m_dirty = true;
f4bd6759
JS
2079 // Don't do any drawing if we're setting the list to NULL,
2080 // since we may be in the process of deleting the tree control.
2081 if (imageList)
2082 CalculateLineHeight();
618a5e38
RR
2083}
2084
941830cb 2085void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 2086{
46cd520d 2087 if (m_ownsImageListState) delete m_imageListState;
f135ff73 2088 m_imageListState = imageList;
ca65c044 2089 m_ownsImageListState = false;
46cd520d
VS
2090}
2091
618a5e38
RR
2092void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
2093{
2094 if (m_ownsImageListButtons) delete m_imageListButtons;
2095 m_imageListButtons = imageList;
ca65c044
WS
2096 m_ownsImageListButtons = false;
2097 m_dirty = true;
618a5e38
RR
2098 CalculateLineHeight();
2099}
2100
46cd520d
VS
2101void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
2102{
2103 SetImageList(imageList);
ca65c044 2104 m_ownsImageListNormal = true;
46cd520d
VS
2105}
2106
2107void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
2108{
2109 SetStateImageList(imageList);
ca65c044 2110 m_ownsImageListState = true;
edaa81ae 2111}
e2414cbe 2112
618a5e38
RR
2113void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
2114{
2115 SetButtonsImageList(imageList);
ca65c044 2116 m_ownsImageListButtons = true;
618a5e38
RR
2117}
2118
f135ff73
VZ
2119// -----------------------------------------------------------------------------
2120// helpers
2121// -----------------------------------------------------------------------------
0659e7ee 2122
941830cb 2123void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 2124{
0659e7ee
RR
2125 if (m_anchor)
2126 {
618a5e38 2127 int x = 0, y = 0;
91b8de8d 2128 m_anchor->GetSize( x, y, this );
c193b707 2129 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 2130 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
2131 int x_pos = GetScrollPos( wxHORIZONTAL );
2132 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 2133 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
2134 }
2135 else
2136 {
2137 SetScrollbars( 0, 0, 0, 0 );
2138 }
edaa81ae 2139}
c801d85f 2140
941830cb 2141int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 2142{
dc6c62a9
RR
2143 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2144 return item->GetHeight();
2145 else
2146 return m_lineHeight;
91b8de8d
RR
2147}
2148
941830cb 2149void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 2150{
618a5e38
RR
2151 // TODO implement "state" icon on items
2152
9ec64fa7
VZ
2153 wxTreeItemAttr *attr = item->GetAttributes();
2154 if ( attr && attr->HasFont() )
2155 dc.SetFont(attr->GetFont());
2156 else if (item->IsBold())
eff869aa 2157 dc.SetFont(m_boldFont);
ef44a621 2158
618a5e38 2159 long text_w = 0, text_h = 0;
bbe0af5b 2160 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 2161
618a5e38 2162 int image_h = 0, image_w = 0;
8dc99046
VZ
2163 int image = item->GetCurrentImage();
2164 if ( image != NO_IMAGE )
bbe0af5b 2165 {
2c8e4738
VZ
2166 if ( m_imageListNormal )
2167 {
2168 m_imageListNormal->GetSize( image, image_w, image_h );
2169 image_w += 4;
2170 }
2171 else
2172 {
2173 image = NO_IMAGE;
2174 }
bbe0af5b 2175 }
ef44a621 2176
91b8de8d
RR
2177 int total_h = GetLineHeight(item);
2178
b771aa29 2179 if ( item->IsSelected() )
cdb3cffe 2180 {
44c44c82
SC
2181// under mac selections are only a rectangle in case they don't have the focus
2182#ifdef __WXMAC__
2183 if ( !m_hasFocus )
2184 {
2185 dc.SetBrush( *wxTRANSPARENT_BRUSH ) ;
2186 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) , 1 , wxSOLID ) ) ;
2187 }
2188 else
2189 {
2190 dc.SetBrush( *m_hilightBrush ) ;
2191 }
2192#else
b771aa29 2193 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
44c44c82 2194#endif
cdb3cffe 2195 }
afa6a1a1 2196 else
c0fba4d1
VS
2197 {
2198 wxColour colBg;
2199 if ( attr && attr->HasBackgroundColour() )
2200 colBg = attr->GetBackgroundColour();
2201 else
2202 colBg = m_backgroundColour;
2203 dc.SetBrush(wxBrush(colBg, wxSOLID));
2204 }
afa6a1a1 2205
cdb3cffe 2206 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
b77d9650 2207
c6f4913a 2208 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
a69cb1cc 2209 {
c6f4913a
VS
2210 int x, y, w, h;
2211
2212 DoGetPosition(&x, &y);
2213 DoGetSize(&w, &h);
2214 dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset);
a69cb1cc
JS
2215 }
2216 else
b77d9650 2217 {
c6f4913a
VS
2218 if ( item->IsSelected() && image != NO_IMAGE )
2219 {
2220 // If it's selected, and there's an image, then we should
2221 // take care to leave the area under the image painted in the
2222 // background colour.
2223 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
2224 item->GetWidth() - image_w + 2, total_h-offset );
2225 }
2226 else
2227 {
2228 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
2229 item->GetWidth()+2, total_h-offset );
2230 }
b77d9650 2231 }
ef44a621 2232
8dc99046 2233 if ( image != NO_IMAGE )
bbe0af5b 2234 {
d30b4d20 2235 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 2236 m_imageListNormal->Draw( image, dc,
49cd56ef 2237 item->GetX(),
d701d432 2238 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
2239 wxIMAGELIST_DRAW_TRANSPARENT );
2240 dc.DestroyClippingRegion();
2241 }
ef44a621 2242
afa6a1a1 2243 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
2244 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2245 dc.DrawText( item->GetText(),
2246 (wxCoord)(image_w + item->GetX()),
2247 (wxCoord)(item->GetY() + extraH));
ef44a621 2248
eff869aa
RR
2249 // restore normal font
2250 dc.SetFont( m_normalFont );
ef44a621
VZ
2251}
2252
91b8de8d 2253// Now y stands for the top of the item, whereas it used to stand for middle !
941830cb 2254void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2255{
618a5e38
RR
2256 int x = level*m_indent;
2257 if (!HasFlag(wxTR_HIDE_ROOT))
2258 {
2259 x += m_indent;
2260 }
2261 else if (level == 0)
2262 {
2b84e565
VS
2263 // always expand hidden root
2264 int origY = y;
618a5e38 2265 wxArrayGenericTreeItems& children = item->GetChildren();
2b84e565
VS
2266 int count = children.Count();
2267 if (count > 0)
2268 {
2269 int n = 0, oldY;
2270 do {
2271 oldY = y;
2272 PaintLevel(children[n], dc, 1, y);
2273 } while (++n < count);
2274
e76520fd 2275 if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
2b84e565
VS
2276 {
2277 // draw line down to last child
2278 origY += GetLineHeight(children[0])>>1;
2279 oldY += GetLineHeight(children[n-1])>>1;
2280 dc.DrawLine(3, origY, 3, oldY);
2281 }
2282 }
618a5e38
RR
2283 return;
2284 }
91b8de8d 2285
618a5e38
RR
2286 item->SetX(x+m_spacing);
2287 item->SetY(y);
389cdc7a 2288
618a5e38
RR
2289 int h = GetLineHeight(item);
2290 int y_top = y;
2291 int y_mid = y_top + (h>>1);
2292 y += h;
4832f7c0 2293
618a5e38
RR
2294 int exposed_x = dc.LogicalToDeviceX(0);
2295 int exposed_y = dc.LogicalToDeviceY(y_top);
233058c7 2296
618a5e38 2297 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
bbe0af5b 2298 {
c6f4913a
VS
2299 wxPen *pen =
2300#ifndef __WXMAC__
2301 // don't draw rect outline if we already have the
2302 // background color under Mac
2303 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2304#endif // !__WXMAC__
2305 wxTRANSPARENT_PEN;
2306
2307 wxColour colText;
2308 if ( item->IsSelected() )
2309 {
a756f210 2310 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
c6f4913a
VS
2311 }
2312 else
2313 {
2314 wxTreeItemAttr *attr = item->GetAttributes();
2315 if (attr && attr->HasTextColour())
2316 colText = attr->GetTextColour();
2317 else
6f0dd6b2 2318 colText = GetForegroundColour();
c6f4913a
VS
2319 }
2320
2321 // prepare to draw
2322 dc.SetTextForeground(colText);
2323 dc.SetPen(*pen);
2324
2325 // draw
2326 PaintItem(item, dc);
2327
2328 if (HasFlag(wxTR_ROW_LINES))
2329 {
2330 // if the background colour is white, choose a
2331 // contrasting color for the lines
2332 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2333 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2334 dc.DrawLine(0, y_top, 10000, y_top);
2335 dc.DrawLine(0, y, 10000, y);
2336 }
2337
2338 // restore DC objects
2339 dc.SetBrush(*wxWHITE_BRUSH);
2340 dc.SetPen(m_dottedPen);
2341 dc.SetTextForeground(*wxBLACK);
2342
9c7f49f5 2343 if ( !HasFlag(wxTR_NO_LINES) )
cdb3cffe 2344 {
9c7f49f5
VZ
2345 // draw the horizontal line here
2346 int x_start = x;
2347 if (x > (signed)m_indent)
2348 x_start -= m_indent;
2349 else if (HasFlag(wxTR_LINES_AT_ROOT))
2350 x_start = 3;
2351 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2352 }
618a5e38 2353
9c7f49f5
VZ
2354 // should the item show a button?
2355 if ( item->HasPlus() && HasButtons() )
2356 {
2357 if ( m_imageListButtons )
c22886bd 2358 {
618a5e38 2359 // draw the image button here
9c7f49f5
VZ
2360 int image_h = 0,
2361 image_w = 0;
2362 int image = item->IsExpanded() ? wxTreeItemIcon_Expanded
2363 : wxTreeItemIcon_Normal;
2364 if ( item->IsSelected() )
2b84e565 2365 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
9c7f49f5 2366
618a5e38 2367 m_imageListButtons->GetSize(image, image_w, image_h);
9c7f49f5
VZ
2368 int xx = x - image_w/2;
2369 int yy = y_mid - image_h/2;
2370
2371 wxDCClipper clip(dc, xx, yy, image_w, image_h);
618a5e38
RR
2372 m_imageListButtons->Draw(image, dc, xx, yy,
2373 wxIMAGELIST_DRAW_TRANSPARENT);
c22886bd 2374 }
9c7f49f5 2375 else // no custom buttons
c22886bd 2376 {
0e7761fa
VZ
2377 static const int wImage = 9;
2378 static const int hImage = 9;
ca65c044 2379
f8b043e7
RR
2380 int flag = 0;
2381 if (item->IsExpanded())
2382 flag |= wxCONTROL_EXPANDED;
2383 if (item == m_underMouse)
2384 flag |= wxCONTROL_CURRENT;
ca65c044 2385
9c7f49f5
VZ
2386 wxRendererNative::Get().DrawTreeItemButton
2387 (
2388 this,
2389 dc,
2390 wxRect(x - wImage/2,
2391 y_mid - hImage/2,
2392 wImage, hImage),
f8b043e7 2393 flag
9c7f49f5 2394 );
c22886bd 2395 }
bbe0af5b 2396 }
f135ff73 2397 }
d3a9f4af 2398
bbe0af5b
RR
2399 if (item->IsExpanded())
2400 {
91b8de8d 2401 wxArrayGenericTreeItems& children = item->GetChildren();
618a5e38 2402 int count = children.Count();
f65635b5 2403 if (count > 0)
9ec64fa7 2404 {
618a5e38
RR
2405 int n = 0, oldY;
2406 ++level;
2407 do {
2408 oldY = y;
2409 PaintLevel(children[n], dc, level, y);
2410 } while (++n < count);
2411
e76520fd 2412 if (!HasFlag(wxTR_NO_LINES) && count > 0)
618a5e38
RR
2413 {
2414 // draw line down to last child
2b84e565 2415 oldY += GetLineHeight(children[n-1])>>1;
618a5e38 2416 if (HasButtons()) y_mid += 5;
dd360466
RD
2417
2418 // Only draw the portion of the line that is visible, in case it is huge
ca65c044 2419 wxCoord xOrigin=0, yOrigin=0, width, height;
dd360466
RD
2420 dc.GetDeviceOrigin(&xOrigin, &yOrigin);
2421 yOrigin = abs(yOrigin);
2422 GetClientSize(&width, &height);
2423
2424 // Move end points to the begining/end of the view?
2425 if (y_mid < yOrigin)
2426 y_mid = yOrigin;
2427 if (oldY > yOrigin + height)
2428 oldY = yOrigin + height;
2429
2430 // after the adjustments if y_mid is larger than oldY then the line
2431 // isn't visible at all so don't draw anything
2432 if (y_mid < oldY)
2433 dc.DrawLine(x, y_mid, x, oldY);
618a5e38 2434 }
9ec64fa7 2435 }
bbe0af5b 2436 }
4c681997 2437}
c801d85f 2438
941830cb 2439void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
2440{
2441 if ( item )
2442 {
2443 if ( item->HasPlus() )
2444 {
2445 // it's a folder, indicate it by a border
2446 DrawBorder(item);
2447 }
2448 else
2449 {
2450 // draw a line under the drop target because the item will be
2451 // dropped there
ca65c044 2452 DrawLine(item, true /* below */);
3dbeaa52
VZ
2453 }
2454
2455 SetCursor(wxCURSOR_BULLSEYE);
2456 }
2457 else
2458 {
2459 // can't drop here
2460 SetCursor(wxCURSOR_NO_ENTRY);
2461 }
2462}
2463
941830cb 2464void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 2465{
941830cb 2466 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2467
941830cb 2468 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2469
1044a386 2470 wxClientDC dc(this);
91b8de8d
RR
2471 PrepareDC( dc );
2472 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 2473 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 2474
3dbeaa52
VZ
2475 int w = i->GetWidth() + 2;
2476 int h = GetLineHeight(i) + 2;
91b8de8d 2477
3dbeaa52 2478 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
2479}
2480
941830cb 2481void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 2482{
941830cb 2483 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2484
941830cb 2485 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2486
1044a386 2487 wxClientDC dc(this);
91b8de8d
RR
2488 PrepareDC( dc );
2489 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 2490
3dbeaa52
VZ
2491 int x = i->GetX(),
2492 y = i->GetY();
2493 if ( below )
2494 {
2495 y += GetLineHeight(i) - 1;
2496 }
91b8de8d 2497
3dbeaa52 2498 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
2499}
2500
f135ff73 2501// -----------------------------------------------------------------------------
77ffb593 2502// wxWidgets callbacks
f135ff73
VZ
2503// -----------------------------------------------------------------------------
2504
941830cb 2505void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 2506{
0659e7ee
RR
2507 wxPaintDC dc(this);
2508 PrepareDC( dc );
29d87bba 2509
941830cb
JS
2510 if ( !m_anchor)
2511 return;
2512
eff869aa 2513 dc.SetFont( m_normalFont );
0659e7ee 2514 dc.SetPen( m_dottedPen );
f38374d0 2515
eff869aa 2516 // this is now done dynamically
91b8de8d
RR
2517 //if(GetImageList() == NULL)
2518 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2519
91b8de8d 2520 int y = 2;
0659e7ee 2521 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 2522}
c801d85f 2523
b771aa29 2524void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
c801d85f 2525{
ca65c044 2526 m_hasFocus = true;
978f38c2 2527
b771aa29
VZ
2528 RefreshSelected();
2529
2530 event.Skip();
edaa81ae 2531}
c801d85f 2532
b771aa29 2533void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
c801d85f 2534{
ca65c044 2535 m_hasFocus = false;
978f38c2 2536
b771aa29
VZ
2537 RefreshSelected();
2538
2539 event.Skip();
edaa81ae 2540}
c801d85f 2541
941830cb 2542void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
c801d85f 2543{
978f38c2 2544 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
b09bda68 2545 te.m_evtKey = event;
978f38c2 2546 te.SetEventObject( this );
68eb5f63
VZ
2547 if ( GetEventHandler()->ProcessEvent( te ) )
2548 {
2549 // intercepted by the user code
2550 return;
2551 }
435fe83e 2552
91b8de8d 2553 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
2554 {
2555 event.Skip();
2556 return;
2557 }
ef44a621 2558
06b466c7
VZ
2559 // how should the selection work for this event?
2560 bool is_multiple, extended_select, unselect_others;
2561 EventFlagsToSelType(GetWindowStyleFlag(),
2562 event.ShiftDown(),
2563 event.ControlDown(),
dfc6cd93
SB
2564 is_multiple, extended_select, unselect_others);
2565
2566 // + : Expand
2567 // - : Collaspe
f6bcfd97 2568 // * : Expand all/Collapse all
dfc6cd93
SB
2569 // ' ' | return : activate
2570 // up : go up (not last children!)
2571 // down : go down
2572 // left : go to parent
2573 // right : open if parent and go next
2574 // home : go to root
2575 // end : go to last item without opening parents
cb59313c 2576 // alnum : start or continue searching for the item with this prefix
12a3f227 2577 int keyCode = event.GetKeyCode();
cb59313c 2578 switch ( keyCode )
978f38c2
VZ
2579 {
2580 case '+':
2581 case WXK_ADD:
2582 if (m_current->HasPlus() && !IsExpanded(m_current))
2583 {
2584 Expand(m_current);
2585 }
2586 break;
ef44a621 2587
f6bcfd97
BP
2588 case '*':
2589 case WXK_MULTIPLY:
2590 if ( !IsExpanded(m_current) )
2591 {
2592 // expand all
2593 ExpandAll(m_current);
2594 break;
2595 }
2596 //else: fall through to Collapse() it
2597
978f38c2
VZ
2598 case '-':
2599 case WXK_SUBTRACT:
2600 if (IsExpanded(m_current))
2601 {
2602 Collapse(m_current);
2603 }
2604 break;
ef44a621 2605
978f38c2
VZ
2606 case ' ':
2607 case WXK_RETURN:
6151e144 2608 if ( !event.HasModifiers() )
978f38c2
VZ
2609 {
2610 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
ee4b2721 2611 event.m_item = m_current;
978f38c2
VZ
2612 event.SetEventObject( this );
2613 GetEventHandler()->ProcessEvent( event );
2614 }
6151e144
VZ
2615
2616 // in any case, also generate the normal key event for this key,
2617 // even if we generated the ACTIVATED event above: this is what
2618 // wxMSW does and it makes sense because you might not want to
2619 // process ACTIVATED event at all and handle Space and Return
2620 // directly (and differently) which would be impossible otherwise
2621 event.Skip();
978f38c2 2622 break;
ef44a621 2623
618a5e38
RR
2624 // up goes to the previous sibling or to the last
2625 // of its children if it's expanded
978f38c2
VZ
2626 case WXK_UP:
2627 {
91b8de8d 2628 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
2629 if (!prev)
2630 {
99006e44 2631 prev = GetItemParent( m_key_current );
618a5e38
RR
2632 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2633 {
2634 break; // don't go to root if it is hidden
2635 }
c193b707
VZ
2636 if (prev)
2637 {
2d75caaa 2638 wxTreeItemIdValue cookie;
91b8de8d 2639 wxTreeItemId current = m_key_current;
618a5e38
RR
2640 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2641 if (current == GetFirstChild( prev, cookie ))
90e58684
RR
2642 {
2643 // otherwise we return to where we came from
78f12104 2644 DoSelectItem( prev, unselect_others, extended_select );
941830cb 2645 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
90e58684 2646 break;
c193b707 2647 }
978f38c2
VZ
2648 }
2649 }
2650 if (prev)
2651 {
69a282d4 2652 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 2653 {
69a282d4
VZ
2654 wxTreeItemId child = GetLastChild(prev);
2655 if ( child )
2656 {
2657 prev = child;
2658 }
978f38c2 2659 }
69a282d4 2660
78f12104 2661 DoSelectItem( prev, unselect_others, extended_select );
941830cb 2662 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
2663 }
2664 }
2665 break;
ef44a621 2666
978f38c2
VZ
2667 // left arrow goes to the parent
2668 case WXK_LEFT:
2669 {
99006e44 2670 wxTreeItemId prev = GetItemParent( m_current );
618a5e38
RR
2671 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2672 {
2673 // don't go to root if it is hidden
2674 prev = GetPrevSibling( m_current );
2675 }
978f38c2
VZ
2676 if (prev)
2677 {
78f12104 2678 DoSelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2679 }
2680 }
2681 break;
ef44a621 2682
978f38c2 2683 case WXK_RIGHT:
618a5e38
RR
2684 // this works the same as the down arrow except that we
2685 // also expand the item if it wasn't expanded yet
978f38c2
VZ
2686 Expand(m_current);
2687 // fall through
2688
2689 case WXK_DOWN:
ef44a621 2690 {
91b8de8d 2691 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2 2692 {
2d75caaa 2693 wxTreeItemIdValue cookie;
91b8de8d 2694 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
78f12104 2695 DoSelectItem( child, unselect_others, extended_select );
941830cb 2696 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
2697 }
2698 else
2699 {
91b8de8d 2700 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2701 if (!next)
978f38c2 2702 {
91b8de8d 2703 wxTreeItemId current = m_key_current;
a0fad723 2704 while (current.IsOk() && !next)
978f38c2 2705 {
99006e44 2706 current = GetItemParent( current );
978f38c2
VZ
2707 if (current) next = GetNextSibling( current );
2708 }
2709 }
b62c3631 2710 if (next)
978f38c2 2711 {
78f12104 2712 DoSelectItem( next, unselect_others, extended_select );
941830cb 2713 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
2714 }
2715 }
ef44a621 2716 }
978f38c2 2717 break;
ef44a621 2718
978f38c2
VZ
2719 // <End> selects the last visible tree item
2720 case WXK_END:
2721 {
2722 wxTreeItemId last = GetRootItem();
2723
2724 while ( last.IsOk() && IsExpanded(last) )
2725 {
2726 wxTreeItemId lastChild = GetLastChild(last);
2727
2728 // it may happen if the item was expanded but then all of
2729 // its children have been deleted - so IsExpanded() returned
ca65c044 2730 // true, but GetLastChild() returned invalid item
978f38c2
VZ
2731 if ( !lastChild )
2732 break;
2733
2734 last = lastChild;
2735 }
2736
2737 if ( last.IsOk() )
2738 {
78f12104 2739 DoSelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2740 }
2741 }
2742 break;
2743
2744 // <Home> selects the root item
2745 case WXK_HOME:
2746 {
2747 wxTreeItemId prev = GetRootItem();
cb59313c
VZ
2748 if (!prev)
2749 break;
2750
2751 if ( HasFlag(wxTR_HIDE_ROOT) )
978f38c2 2752 {
2d75caaa
VZ
2753 wxTreeItemIdValue cookie;
2754 prev = GetFirstChild(prev, cookie);
cb59313c
VZ
2755 if (!prev)
2756 break;
978f38c2 2757 }
cb59313c 2758
78f12104 2759 DoSelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2760 }
2761 break;
2762
2763 default:
cb59313c 2764 // do not use wxIsalnum() here
6151e144 2765 if ( !event.HasModifiers() &&
1ec3a984
RR
2766 ((keyCode >= '0' && keyCode <= '9') ||
2767 (keyCode >= 'a' && keyCode <= 'z') ||
2768 (keyCode >= 'A' && keyCode <= 'Z' )))
cb59313c
VZ
2769 {
2770 // find the next item starting with the given prefix
bef7a62d 2771 wxChar ch = (wxChar)keyCode;
6151e144 2772
bef7a62d 2773 wxTreeItemId id = FindItem(m_current, m_findPrefix + ch);
cb59313c
VZ
2774 if ( !id.IsOk() )
2775 {
2776 // no such item
2777 break;
2778 }
2779
2780 SelectItem(id);
2781
2782 m_findPrefix += ch;
2783
2784 // also start the timer to reset the current prefix if the user
2785 // doesn't press any more alnum keys soon -- we wouldn't want
2786 // to use this prefix for a new item search
2787 if ( !m_findTimer )
2788 {
2789 m_findTimer = new wxTreeFindTimer(this);
2790 }
2791
2792 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
2793 }
2794 else
2795 {
2796 event.Skip();
2797 }
978f38c2 2798 }
edaa81ae 2799}
c801d85f 2800
941830cb 2801wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2802{
618a5e38
RR
2803 // JACS: removed wxYieldIfNeeded() because it can cause the window
2804 // to be deleted from under us if a close window event is pending
dc6c62a9 2805
91b8de8d
RR
2806 int w, h;
2807 GetSize(&w, &h);
91b8de8d 2808 flags=0;
618a5e38
RR
2809 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2810 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2811 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2812 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2813 if (flags) return wxTreeItemId();
d3a9f4af 2814
618a5e38
RR
2815 if (m_anchor == NULL)
2816 {
2817 flags = wxTREE_HITTEST_NOWHERE;
2818 return wxTreeItemId();
2819 }
5716a1ab 2820
d027179b
VS
2821 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
2822 this, flags, 0);
618a5e38
RR
2823 if (hit == NULL)
2824 {
2825 flags = wxTREE_HITTEST_NOWHERE;
2826 return wxTreeItemId();
2827 }
2828 return hit;
4f22cf8d
RR
2829}
2830
8fb3bfe2
JS
2831// get the bounding rectangle of the item (or of its label only)
2832bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
edb8f298
VZ
2833 wxRect& rect,
2834 bool WXUNUSED(textOnly)) const
8fb3bfe2 2835{
ca65c044 2836 wxCHECK_MSG( item.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
8fb3bfe2
JS
2837
2838 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2839
2840 int startX, startY;
2841 GetViewStart(& startX, & startY);
2842
1ed01484 2843 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
c22886bd 2844 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
1ed01484 2845 rect.width = i->GetWidth();
c22886bd
RR
2846 //rect.height = i->GetHeight();
2847 rect.height = GetLineHeight(i);
8fb3bfe2 2848
ca65c044 2849 return true;
8fb3bfe2
JS
2850}
2851
941830cb 2852void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
e179bd65 2853{
edb8f298 2854 wxCHECK_RET( item.IsOk(), _T("can't edit an invalid item") );
e179bd65 2855
edb8f298 2856 wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem;
9dfbf520 2857
e179bd65 2858 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
ee4b2721 2859 te.m_item = itemEdit;
e179bd65 2860 te.SetEventObject( this );
edb8f298
VZ
2861 if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() )
2862 {
2863 // vetoed by user
2864 return;
2865 }
8dc99046 2866
dc6c62a9
RR
2867 // We have to call this here because the label in
2868 // question might just have been added and no screen
2869 // update taken place.
edb8f298 2870 if ( m_dirty )
f89d65ea
SC
2871#if defined( __WXMSW__ ) || defined(__WXMAC__)
2872 Update();
2873#else
edb8f298 2874 wxYieldIfNeeded();
f89d65ea 2875#endif
9dfbf520 2876
fbb12260 2877 m_textCtrl = new wxTreeTextCtrl(this, itemEdit);
8dc99046 2878
fbb12260
JS
2879 m_textCtrl->SetFocus();
2880}
2881
2882// returns a pointer to the text edit control if the item is being
2883// edited, NULL otherwise (it's assumed that no more than one item may
2884// be edited simultaneously)
2885wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const
2886{
2887 return m_textCtrl;
e179bd65
RR
2888}
2889
edb8f298
VZ
2890bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
2891 const wxString& value)
e179bd65 2892{
e179bd65 2893 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
ee4b2721 2894 le.m_item = item;
e179bd65 2895 le.SetEventObject( this );
edb8f298 2896 le.m_label = value;
ca65c044 2897 le.m_editCancelled = false;
9dfbf520 2898
edb8f298
VZ
2899 return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2900}
9dfbf520 2901
dd23c25c
JS
2902void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item)
2903{
2904 // let owner know that the edit was cancelled
2905 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
ee4b2721 2906 le.m_item = item;
dd23c25c
JS
2907 le.SetEventObject( this );
2908 le.m_label = wxEmptyString;
ca65c044 2909 le.m_editCancelled = true;
dd23c25c
JS
2910
2911 GetEventHandler()->ProcessEvent( le );
2912}
2913
2914
2915
2916
edb8f298
VZ
2917void wxGenericTreeCtrl::OnRenameTimer()
2918{
2919 Edit( m_current );
e179bd65 2920}
9dfbf520 2921
941830cb 2922void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2923{
bbe0af5b 2924 if ( !m_anchor ) return;
978f38c2 2925
f8b043e7 2926 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
ca65c044 2927
f8b043e7
RR
2928 // Is the mouse over a tree item button?
2929 int flags = 0;
73bb6776
JS
2930 wxGenericTreeItem *thisItem = m_anchor->HitTest(pt, this, flags, 0);
2931 wxGenericTreeItem *underMouse = thisItem;
0efd5732 2932#if wxUSE_TOOLTIPS
73bb6776 2933 bool underMouseChanged = (underMouse != m_underMouse) ;
0efd5732 2934#endif // wxUSE_TOOLTIPS
73bb6776 2935
f8b043e7
RR
2936 if ((underMouse) &&
2937 (flags & wxTREE_HITTEST_ONITEMBUTTON) &&
2938 (!event.LeftIsDown()) &&
ca65c044 2939 (!m_isDragging) &&
f8b043e7
RR
2940 (!m_renameTimer || !m_renameTimer->IsRunning()))
2941 {
2942 }
2943 else
2944 {
2945 underMouse = NULL;
2946 }
ca65c044 2947
f8b043e7
RR
2948 if (underMouse != m_underMouse)
2949 {
2950 if (m_underMouse)
2951 {
2952 // unhighlight old item
2953 wxGenericTreeItem *tmp = m_underMouse;
2954 m_underMouse = NULL;
2955 RefreshLine( tmp );
2956 }
ca65c044 2957
f8b043e7
RR
2958 m_underMouse = underMouse;
2959 if (m_underMouse)
2960 RefreshLine( m_underMouse );
2961 }
2962
5b5ea466 2963#if wxUSE_TOOLTIPS
4a5d781c 2964 // Determines what item we are hovering over and need a tooltip for
73bb6776 2965 wxTreeItemId hoverItem = thisItem;
4a5d781c
JS
2966
2967 // We do not want a tooltip if we are dragging, or if the rename timer is running
73bb6776 2968 if (underMouseChanged && hoverItem.IsOk() && !m_isDragging && (!m_renameTimer || !m_renameTimer->IsRunning()))
4a5d781c
JS
2969 {
2970 // Ask the tree control what tooltip (if any) should be shown
2971 wxTreeEvent hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, GetId());
bc0aebab 2972 hevent.m_item = hoverItem;
4a5d781c
JS
2973 hevent.SetEventObject(this);
2974
2975 if ( GetEventHandler()->ProcessEvent(hevent) && hevent.IsAllowed() )
2976 {
2977 SetToolTip(hevent.m_label);
2978 }
2979 }
5b5ea466 2980#endif
ca65c044 2981
3dbeaa52
VZ
2982 // we process left mouse up event (enables in-place edit), right down
2983 // (pass to the user code), left dbl click (activate item) and
2984 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
2985 if ( !(event.LeftDown() ||
2986 event.LeftUp() ||
3dbeaa52
VZ
2987 event.RightDown() ||
2988 event.LeftDClick() ||
2989 event.Dragging() ||
2990 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2991 {
2992 event.Skip();
2993
2994 return;
2995 }
2996
29d87bba 2997
f8b043e7 2998 flags = 0;
d027179b 2999 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
3dbeaa52 3000
3dbeaa52 3001 if ( event.Dragging() && !m_isDragging )
bbe0af5b 3002 {
fd9811b1 3003 if (m_dragCount == 0)
d027179b 3004 m_dragStart = pt;
8dc99046 3005
fd9811b1 3006 m_dragCount++;
8dc99046 3007
3dbeaa52
VZ
3008 if (m_dragCount != 3)
3009 {
3010 // wait until user drags a bit further...
3011 return;
3012 }
8dc99046 3013
3dbeaa52
VZ
3014 wxEventType command = event.RightIsDown()
3015 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3016 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 3017
fd9811b1 3018 wxTreeEvent nevent( command, GetId() );
ee4b2721 3019 nevent.m_item = m_current;
fd9811b1 3020 nevent.SetEventObject(this);
3dbeaa52
VZ
3021
3022 // by default the dragging is not supported, the user code must
3023 // explicitly allow the event for it to take place
3024 nevent.Veto();
3025
3026 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
3027 {
3028 // we're going to drag this item
ca65c044 3029 m_isDragging = true;
3dbeaa52 3030
b3a7510d
VZ
3031 // remember the old cursor because we will change it while
3032 // dragging
3033 m_oldCursor = m_cursor;
3034
3035 // in a single selection control, hide the selection temporarily
3036 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
3037 {
941830cb 3038 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
3039
3040 if ( m_oldSelection )
3041 {
ca65c044 3042 m_oldSelection->SetHilight(false);
b3a7510d
VZ
3043 RefreshLine(m_oldSelection);
3044 }
3045 }
3046
3dbeaa52
VZ
3047 CaptureMouse();
3048 }
bbe0af5b 3049 }
3dbeaa52 3050 else if ( event.Moving() )
fd9811b1 3051 {
3dbeaa52
VZ
3052 if ( item != m_dropTarget )
3053 {
3054 // unhighlight the previous drop target
3055 DrawDropEffect(m_dropTarget);
fd9811b1 3056
3dbeaa52 3057 m_dropTarget = item;
978f38c2 3058
3dbeaa52
VZ
3059 // highlight the current drop target if any
3060 DrawDropEffect(m_dropTarget);
fb882e1c 3061
f89d65ea
SC
3062#if defined( __WXMSW__ ) || defined(__WXMAC__)
3063 Update();
3064#else
cd66f45b 3065 wxYieldIfNeeded();
f89d65ea 3066#endif
3dbeaa52 3067 }
e179bd65 3068 }
3dbeaa52
VZ
3069 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
3070 {
3071 // erase the highlighting
3072 DrawDropEffect(m_dropTarget);
9dfbf520 3073
4f5fffcc
RR
3074 if ( m_oldSelection )
3075 {
ca65c044 3076 m_oldSelection->SetHilight(true);
4f5fffcc
RR
3077 RefreshLine(m_oldSelection);
3078 m_oldSelection = (wxGenericTreeItem *)NULL;
3079 }
3080
3dbeaa52
VZ
3081 // generate the drag end event
3082 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 3083
ee4b2721 3084 event.m_item = item;
d027179b 3085 event.m_pointDrag = pt;
3dbeaa52 3086 event.SetEventObject(this);
91b8de8d 3087
3dbeaa52 3088 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 3089
ca65c044 3090 m_isDragging = false;
3dbeaa52
VZ
3091 m_dropTarget = (wxGenericTreeItem *)NULL;
3092
3093 ReleaseMouse();
3094
b3a7510d 3095 SetCursor(m_oldCursor);
3dbeaa52 3096
f89d65ea
SC
3097#if defined( __WXMSW__ ) || defined(__WXMAC__)
3098 Update();
3099#else
cd66f45b 3100 wxYieldIfNeeded();
f89d65ea 3101#endif
3dbeaa52
VZ
3102 }
3103 else
bbe0af5b 3104 {
3dbeaa52
VZ
3105 // here we process only the messages which happen on tree items
3106
3107 m_dragCount = 0;
3108
3109 if (item == NULL) return; /* we hit the blank area */
3110
3111 if ( event.RightDown() )
3112 {
3113 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
ee4b2721 3114 nevent.m_item = item;
d027179b 3115 nevent.m_pointDrag = CalcScrolledPosition(pt);
3dbeaa52
VZ
3116 nevent.SetEventObject(this);
3117 GetEventHandler()->ProcessEvent(nevent);
3118 }
80d2803f 3119 else if ( event.LeftUp() )
f6bcfd97 3120 {
35cf1ec6
VZ
3121 // this facilitates multiple-item drag-and-drop
3122
3123 if (item && HasFlag(wxTR_MULTIPLE))
3124 {
3125 wxArrayTreeItemIds selections;
3126 size_t count = GetSelections(selections);
3127
3128 if (count > 1 &&
3129 !event.ControlDown() &&
3130 !event.ShiftDown())
3131 {
78f12104 3132 DoSelectItem(item, true, false);
35cf1ec6
VZ
3133 }
3134 }
3135
80d2803f 3136 if ( m_lastOnSame )
f6bcfd97 3137 {
80d2803f
VZ
3138 if ( (item == m_current) &&
3139 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
3140 HasFlag(wxTR_EDIT_LABELS) )
3141 {
cb59313c
VZ
3142 if ( m_renameTimer )
3143 {
3144 if ( m_renameTimer->IsRunning() )
3145 m_renameTimer->Stop();
3146 }
3147 else
3148 {
3149 m_renameTimer = new wxTreeRenameTimer( this );
3150 }
bdb310a7 3151
ca65c044 3152 m_renameTimer->Start( wxTreeRenameTimer::DELAY, true );
80d2803f 3153 }
f6bcfd97 3154
ca65c044 3155 m_lastOnSame = false;
80d2803f 3156 }
3dbeaa52 3157 }
0326c494 3158 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3dbeaa52 3159 {
f6bcfd97
BP
3160 if ( event.LeftDown() )
3161 {
3162 m_lastOnSame = item == m_current;
3163 }
3164
0326c494
VZ
3165 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
3166 {
3167 // only toggle the item for a single click, double click on
3168 // the button doesn't do anything (it toggles the item twice)
3169 if ( event.LeftDown() )
3170 {
3171 Toggle( item );
3172 }
3173
3174 // don't select the item if the button was clicked
3175 return;
3176 }
3177
3dbeaa52 3178
35cf1ec6
VZ
3179 // clear the previously selected items, if the
3180 // user clicked outside of the present selection.
3181 // otherwise, perform the deselection on mouse-up.
3182 // this allows multiple drag and drop to work.
3183
3184 if (!IsSelected(item))
3185 {
3186 // how should the selection work for this event?
3187 bool is_multiple, extended_select, unselect_others;
3188 EventFlagsToSelType(GetWindowStyleFlag(),
3189 event.ShiftDown(),
3190 event.ControlDown(),
3191 is_multiple, extended_select, unselect_others);
3192
78f12104 3193 DoSelectItem(item, unselect_others, extended_select);
35cf1ec6
VZ
3194 }
3195
3dbeaa52 3196
618a5e38
RR
3197 // For some reason, Windows isn't recognizing a left double-click,
3198 // so we need to simulate it here. Allow 200 milliseconds for now.
3dbeaa52
VZ
3199 if ( event.LeftDClick() )
3200 {
f6bcfd97 3201 // double clicking should not start editing the item label
cb59313c
VZ
3202 if ( m_renameTimer )
3203 m_renameTimer->Stop();
3204
ca65c044 3205 m_lastOnSame = false;
f6bcfd97 3206
ebb987b7
VZ
3207 // send activate event first
3208 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
ee4b2721 3209 nevent.m_item = item;
d027179b 3210 nevent.m_pointDrag = CalcScrolledPosition(pt);
ebb987b7
VZ
3211 nevent.SetEventObject( this );
3212 if ( !GetEventHandler()->ProcessEvent( nevent ) )
618a5e38 3213 {
ebb987b7
VZ
3214 // if the user code didn't process the activate event,
3215 // handle it ourselves by toggling the item when it is
3216 // double clicked
3217 if ( item->HasPlus() )
3218 {
3219 Toggle(item);
3220 }
618a5e38 3221 }
3dbeaa52
VZ
3222 }
3223 }
bbe0af5b 3224 }
edaa81ae 3225}
c801d85f 3226
5180055b 3227void wxGenericTreeCtrl::OnInternalIdle()
3db7be80 3228{
5180055b 3229 wxWindow::OnInternalIdle();
ca65c044 3230
3e3a7b97
JS
3231 // Check if we need to select the root item
3232 // because nothing else has been selected.
3233 // Delaying it means that we can invoke event handlers
3234 // as required, when a first item is selected.
3235 if (!HasFlag(wxTR_MULTIPLE) && !GetSelection().IsOk())
3236 {
3237 if (m_select_me)
3238 SelectItem(m_select_me);
3239 else if (GetRootItem().IsOk())
3240 SelectItem(GetRootItem());
3241 }
3242
bbe0af5b
RR
3243 /* after all changes have been done to the tree control,
3244 * we actually redraw the tree when everything is over */
ef44a621 3245
618a5e38 3246 if (!m_dirty) return;
e1983ab5 3247 if (m_freezeCount) return;
ca65c044
WS
3248
3249 m_dirty = false;
3db7be80 3250
bbe0af5b 3251 CalculatePositions();
91b8de8d 3252 Refresh();
bbe0af5b 3253 AdjustMyScrollbars();
3db7be80
RR
3254}
3255
941830cb 3256void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 3257{
e06b9569
JS
3258 wxCoord text_w = 0;
3259 wxCoord text_h = 0;
9dfbf520 3260
40e7f56c
VZ
3261 wxTreeItemAttr *attr = item->GetAttributes();
3262 if ( attr && attr->HasFont() )
3263 dc.SetFont(attr->GetFont());
3264 else if ( item->IsBold() )
eff869aa 3265 dc.SetFont(m_boldFont);
c8fce1a4
VS
3266 else
3267 dc.SetFont(m_normalFont);
9dfbf520 3268
91b8de8d 3269 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 3270 text_h+=2;
91b8de8d 3271
eff869aa
RR
3272 // restore normal font
3273 dc.SetFont( m_normalFont );
9dfbf520 3274
91b8de8d
RR
3275 int image_h = 0;
3276 int image_w = 0;
8dc99046
VZ
3277 int image = item->GetCurrentImage();
3278 if ( image != NO_IMAGE )
91b8de8d 3279 {
2c8e4738
VZ
3280 if ( m_imageListNormal )
3281 {
3282 m_imageListNormal->GetSize( image, image_w, image_h );
3283 image_w += 4;
3284 }
91b8de8d
RR
3285 }
3286
3287 int total_h = (image_h > text_h) ? image_h : text_h;
3288
618a5e38 3289 if (total_h < 30)
f2593d0d 3290 total_h += 2; // at least 2 pixels
06b466c7 3291 else
f2593d0d 3292 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
3293
3294 item->SetHeight(total_h);
6cedba09
VZ
3295 if (total_h>m_lineHeight)
3296 m_lineHeight=total_h;
bbe0af5b 3297
91b8de8d
RR
3298 item->SetWidth(image_w+text_w+2);
3299}
3300
3301// -----------------------------------------------------------------------------
3302// for developper : y is now the top of the level
3303// not the middle of it !
941830cb 3304void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 3305{
618a5e38
RR
3306 int x = level*m_indent;
3307 if (!HasFlag(wxTR_HIDE_ROOT))
3308 {
3309 x += m_indent;
3310 }
3311 else if (level == 0)
3312 {
3313 // a hidden root is not evaluated, but its
3314 // children are always calculated
3315 goto Recurse;
3316 }
389cdc7a 3317
91b8de8d 3318 CalculateSize( item, dc );
d3a9f4af 3319
91b8de8d 3320 // set its position
618a5e38 3321 item->SetX( x+m_spacing );
91b8de8d 3322 item->SetY( y );
618a5e38 3323 y += GetLineHeight(item);
4c681997 3324
bbe0af5b
RR
3325 if ( !item->IsExpanded() )
3326 {
618a5e38 3327 // we don't need to calculate collapsed branches
bbe0af5b
RR
3328 return;
3329 }
389cdc7a 3330
618a5e38 3331 Recurse:
91b8de8d
RR
3332 wxArrayGenericTreeItems& children = item->GetChildren();
3333 size_t n, count = children.Count();
618a5e38 3334 ++level;
91b8de8d 3335 for (n = 0; n < count; ++n )
618a5e38 3336 CalculateLevel( children[n], dc, level, y ); // recurse
edaa81ae 3337}
c801d85f 3338
941830cb 3339void wxGenericTreeCtrl::CalculatePositions()
c801d85f 3340{
bbe0af5b 3341 if ( !m_anchor ) return;
29d87bba 3342
bbe0af5b
RR
3343 wxClientDC dc(this);
3344 PrepareDC( dc );
29d87bba 3345
eff869aa 3346 dc.SetFont( m_normalFont );
29d87bba 3347
bbe0af5b 3348 dc.SetPen( m_dottedPen );
91b8de8d
RR
3349 //if(GetImageList() == NULL)
3350 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 3351
8dc99046 3352 int y = 2;
f65635b5 3353 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 3354}
c801d85f 3355
941830cb 3356void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 3357{
e6527f9d 3358 if (m_dirty) return;
e1983ab5 3359 if (m_freezeCount) return;
e6527f9d 3360
d027179b 3361 wxSize client = GetClientSize();
4832f7c0 3362
bbe0af5b 3363 wxRect rect;
5941c5de 3364 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b
VS
3365 rect.width = client.x;
3366 rect.height = client.y;
f135ff73 3367
ca65c044 3368 Refresh(true, &rect);
f135ff73 3369
bbe0af5b 3370 AdjustMyScrollbars();
edaa81ae 3371}
c801d85f 3372
941830cb 3373void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 3374{
e6527f9d 3375 if (m_dirty) return;
e1983ab5 3376 if (m_freezeCount) return;
e6527f9d 3377
bbe0af5b 3378 wxRect rect;
5941c5de 3379 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b 3380 rect.width = GetClientSize().x;
91b8de8d 3381 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 3382
ca65c044 3383 Refresh(true, &rect);
edaa81ae 3384}
c801d85f 3385
b771aa29
VZ
3386void wxGenericTreeCtrl::RefreshSelected()
3387{
e1983ab5 3388 if (m_freezeCount) return;
ca65c044 3389
b771aa29
VZ
3390 // TODO: this is awfully inefficient, we should keep the list of all
3391 // selected items internally, should be much faster
3392 if ( m_anchor )
3393 RefreshSelectedUnder(m_anchor);
3394}
3395
3396void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3397{
e1983ab5 3398 if (m_freezeCount) return;
ca65c044 3399
b771aa29
VZ
3400 if ( item->IsSelected() )
3401 RefreshLine(item);
3402
3403 const wxArrayGenericTreeItems& children = item->GetChildren();
3404 size_t count = children.GetCount();
3405 for ( size_t n = 0; n < count; n++ )
3406 {
3407 RefreshSelectedUnder(children[n]);
3408 }
3409}
3410
e1983ab5
VZ
3411void wxGenericTreeCtrl::Freeze()
3412{
3413 m_freezeCount++;
3414}
3415
3416void wxGenericTreeCtrl::Thaw()
3417{
3418 wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen tree control?") );
ca65c044 3419
e1983ab5
VZ
3420 if ( !--m_freezeCount )
3421 {
3422 Refresh();
3423 }
3424}
3425
7009f411
VZ
3426// ----------------------------------------------------------------------------
3427// changing colours: we need to refresh the tree control
3428// ----------------------------------------------------------------------------
3429
3430bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3431{
3432 if ( !wxWindow::SetBackgroundColour(colour) )
ca65c044
WS
3433 return false;
3434
3435 if (m_freezeCount) return true;
7009f411
VZ
3436
3437 Refresh();
3438
ca65c044 3439 return true;
7009f411
VZ
3440}
3441
0800a4ba 3442bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
7009f411
VZ
3443{
3444 if ( !wxWindow::SetForegroundColour(colour) )
ca65c044
WS
3445 return false;
3446
3447 if (m_freezeCount) return true;
7009f411
VZ
3448
3449 Refresh();
3450
ca65c044 3451 return true;
7009f411
VZ
3452}
3453
73bb6776
JS
3454// Process the tooltip event, to speed up event processing.
3455// Doesn't actually get a tooltip.
3456void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent &event )
3457{
3458 event.Veto();
3459}
3460
35d4c967
RD
3461
3462// NOTE: If using the wxListBox visual attributes works everywhere then this can
3463// be removed, as well as the #else case below.
3464#define _USE_VISATTR 0
3465
944b1f5e 3466#if _USE_VISATTR
35d4c967 3467#include "wx/listbox.h"
944b1f5e 3468#endif
35d4c967
RD
3469
3470//static
3471wxVisualAttributes
3f927d50 3472#if _USE_VISATTR
35d4c967 3473wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
3f927d50
DS
3474#else
3475wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
3476#endif
35d4c967
RD
3477{
3478#if _USE_VISATTR
3479 // Use the same color scheme as wxListBox
3480 return wxListBox::GetClassDefaultAttributes(variant);
3481#else
3482 wxVisualAttributes attr;
3483 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
3484 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
3485 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3486 return attr;
3487#endif
3488}
3489
1e6feb95 3490#endif // wxUSE_TREECTRL