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