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