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