]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
fixed serious bug in wxFont::operator== (ignored weight)
[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$
c801d85f 8// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
29d87bba 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
f135ff73
VZ
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
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
f135ff73
VZ
41// -----------------------------------------------------------------------------
42// array types
43// -----------------------------------------------------------------------------
c801d85f 44
1e6d9499
JS
45class WXDLLEXPORT wxGenericTreeItem;
46
8a985b84 47WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
941830cb 48//WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
c801d85f 49
8dc99046
VZ
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
54static const int NO_IMAGE = -1;
55
3dbeaa52
VZ
56#define PIXELS_PER_UNIT 10
57
f135ff73
VZ
58// -----------------------------------------------------------------------------
59// private classes
60// -----------------------------------------------------------------------------
61
3dbeaa52
VZ
62// timer used for enabling in-place edit
63class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
64{
65public:
941830cb 66 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
3dbeaa52
VZ
67
68 void Notify();
69
70private:
941830cb 71 wxGenericTreeCtrl *m_owner;
3dbeaa52
VZ
72};
73
74// control used for in-place edit
75class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
76{
77public:
3dbeaa52
VZ
78 wxTreeTextCtrl( wxWindow *parent,
79 const wxWindowID id,
80 bool *accept,
81 wxString *res,
941830cb 82 wxGenericTreeCtrl *owner,
3dbeaa52
VZ
83 const wxString &value = wxEmptyString,
84 const wxPoint &pos = wxDefaultPosition,
85 const wxSize &size = wxDefaultSize,
27316302 86 int style = wxSIMPLE_BORDER,
3dbeaa52
VZ
87 const wxValidator& validator = wxDefaultValidator,
88 const wxString &name = wxTextCtrlNameStr );
89
90 void OnChar( wxKeyEvent &event );
c13cace1 91 void OnKeyUp( wxKeyEvent &event );
3dbeaa52
VZ
92 void OnKillFocus( wxFocusEvent &event );
93
94private:
95 bool *m_accept;
96 wxString *m_res;
618a5e38 97 wxGenericTreeCtrl *m_owner;
3dbeaa52
VZ
98 wxString m_startValue;
99
100 DECLARE_EVENT_TABLE()
3dbeaa52
VZ
101};
102
f135ff73
VZ
103// a tree item
104class WXDLLEXPORT wxGenericTreeItem
c801d85f 105{
f135ff73 106public:
9ec64fa7
VZ
107 // ctors & dtor
108 wxGenericTreeItem() { m_data = NULL; }
109 wxGenericTreeItem( wxGenericTreeItem *parent,
618a5e38
RR
110 const wxString& text,
111 wxDC& dc,
112 int image,
113 int selImage,
114 wxTreeItemData *data );
f135ff73 115
9ec64fa7 116 ~wxGenericTreeItem();
f135ff73 117
9ec64fa7
VZ
118 // trivial accessors
119 wxArrayGenericTreeItems& GetChildren() { return m_children; }
f135ff73 120
9ec64fa7
VZ
121 const wxString& GetText() const { return m_text; }
122 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
3dbeaa52 123 { return m_images[which]; }
9ec64fa7 124 wxTreeItemData *GetData() const { return m_data; }
f135ff73 125
9ec64fa7
VZ
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
8dc99046 129
9ec64fa7
VZ
130 void SetText( const wxString &text );
131 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
132 void SetData(wxTreeItemData *data) { m_data = data; }
f135ff73 133
9ec64fa7 134 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
f135ff73 135
9ec64fa7 136 void SetBold(bool bold) { m_isBold = bold; }
ef44a621 137
9ec64fa7
VZ
138 int GetX() const { return m_x; }
139 int GetY() const { return m_y; }
f135ff73 140
9ec64fa7
VZ
141 void SetX(int x) { m_x = x; }
142 void SetY(int y) { m_y = y; }
f135ff73 143
9ec64fa7
VZ
144 int GetHeight() const { return m_height; }
145 int GetWidth() const { return m_width; }
91b8de8d 146
9ec64fa7
VZ
147 void SetHeight(int h) { m_height = h; }
148 void SetWidth(int w) { m_width = w; }
91b8de8d 149
9ec64fa7 150 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 151
9ec64fa7
VZ
152 // operations
153 // deletes all children notifying the treectrl about it if !NULL
154 // pointer given
941830cb 155 void DeleteChildren(wxGenericTreeCtrl *tree = NULL);
f135ff73 156
9ec64fa7
VZ
157 // get count of all children (and grand children if 'recursively')
158 size_t GetChildrenCount(bool recursively = TRUE) const;
f135ff73 159
9ec64fa7 160 void Insert(wxGenericTreeItem *child, size_t index)
f135ff73
VZ
161 { m_children.Insert(child, index); }
162
941830cb 163 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
f135ff73 164
9ec64fa7
VZ
165 // return the item at given position (or NULL if no item), onButton is
166 // TRUE if the point belongs to the item's button, otherwise it lies
167 // on the button's label
618a5e38
RR
168 wxGenericTreeItem *HitTest( const wxPoint& point,
169 const wxGenericTreeCtrl *,
170 int &flags,
171 int level );
f135ff73 172
9ec64fa7
VZ
173 void Expand() { m_isCollapsed = FALSE; }
174 void Collapse() { m_isCollapsed = TRUE; }
f135ff73 175
9ec64fa7 176 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
f135ff73 177
9ec64fa7
VZ
178 // status inquiries
179 bool HasChildren() const { return !m_children.IsEmpty(); }
ef8698d6 180 bool IsSelected() const { return m_hasHilight != 0; }
9ec64fa7
VZ
181 bool IsExpanded() const { return !m_isCollapsed; }
182 bool HasPlus() const { return m_hasPlus || HasChildren(); }
ef8698d6 183 bool IsBold() const { return m_isBold != 0; }
9ec64fa7
VZ
184
185 // attributes
186 // get them - may be NULL
187 wxTreeItemAttr *GetAttributes() const { return m_attr; }
188 // get them ensuring that the pointer is not NULL
189 wxTreeItemAttr& Attr()
190 {
191 if ( !m_attr )
618a5e38 192 {
9ec64fa7 193 m_attr = new wxTreeItemAttr;
618a5e38
RR
194 m_ownsAttr = TRUE;
195 }
9ec64fa7
VZ
196 return *m_attr;
197 }
618a5e38
RR
198 // set them
199 void SetAttributes(wxTreeItemAttr *attr)
200 {
201 if ( m_ownsAttr ) delete m_attr;
202 m_attr = attr;
203 m_ownsAttr = FALSE;
204 }
205 // set them and delete when done
206 void AssignAttributes(wxTreeItemAttr *attr)
207 {
208 SetAttributes(attr);
209 m_ownsAttr = TRUE;
210 }
f135ff73
VZ
211
212private:
618a5e38
RR
213 // since there can be very many of these, we save size by chosing
214 // the smallest representation for the elements and by ordering
215 // the members to avoid padding.
216 wxString m_text; // label to be rendered for item
217
218 wxTreeItemData *m_data; // user-provided data
219
220 wxArrayGenericTreeItems m_children; // list of children
221 wxGenericTreeItem *m_parent; // parent of this item
222
223 wxTreeItemAttr *m_attr; // attributes???
9ec64fa7
VZ
224
225 // tree ctrl images for the normal, selected, expanded and
226 // expanded+selected states
618a5e38 227 short m_images[wxTreeItemIcon_Max];
9ec64fa7 228
618a5e38
RR
229 wxCoord m_x; // (virtual) offset from top
230 short m_y; // (virtual) offset from left
231 short m_width; // width of this item
232 unsigned char m_height; // height of this item
9ec64fa7
VZ
233
234 // use bitfields to save size
235 int m_isCollapsed :1;
236 int m_hasHilight :1; // same as focused
237 int m_hasPlus :1; // used for item which doesn't have
238 // children but has a [+] button
239 int m_isBold :1; // render the label in bold font
618a5e38 240 int m_ownsAttr :1; // delete attribute when done
f135ff73
VZ
241};
242
243// =============================================================================
244// implementation
245// =============================================================================
246
06b466c7
VZ
247// ----------------------------------------------------------------------------
248// private functions
249// ----------------------------------------------------------------------------
250
251// translate the key or mouse event flags to the type of selection we're
252// dealing with
253static void EventFlagsToSelType(long style,
254 bool shiftDown,
255 bool ctrlDown,
dfc6cd93
SB
256 bool &is_multiple,
257 bool &extended_select,
258 bool &unselect_others)
06b466c7 259{
dfc6cd93
SB
260 is_multiple = (style & wxTR_MULTIPLE) != 0;
261 extended_select = shiftDown && is_multiple;
262 unselect_others = !(extended_select || (ctrlDown && is_multiple));
06b466c7 263}
e179bd65
RR
264
265// -----------------------------------------------------------------------------
266// wxTreeRenameTimer (internal)
267// -----------------------------------------------------------------------------
268
941830cb 269wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
e179bd65
RR
270{
271 m_owner = owner;
272}
273
274void wxTreeRenameTimer::Notify()
275{
276 m_owner->OnRenameTimer();
277}
278
279//-----------------------------------------------------------------------------
280// wxTreeTextCtrl (internal)
281//-----------------------------------------------------------------------------
282
e179bd65
RR
283BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
284 EVT_CHAR (wxTreeTextCtrl::OnChar)
c13cace1 285 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
e179bd65
RR
286 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
287END_EVENT_TABLE()
288
674ac8b9
VZ
289wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent,
290 const wxWindowID id,
291 bool *accept,
292 wxString *res,
941830cb 293 wxGenericTreeCtrl *owner,
674ac8b9
VZ
294 const wxString &value,
295 const wxPoint &pos,
296 const wxSize &size,
297 int style,
298 const wxValidator& validator,
299 const wxString &name )
618a5e38 300 : wxTextCtrl( parent, id, value, pos, size, style, validator, name )
e179bd65
RR
301{
302 m_res = res;
303 m_accept = accept;
304 m_owner = owner;
5f1ea0ee 305 (*m_accept) = FALSE;
3dbeaa52 306 (*m_res) = wxEmptyString;
5f1ea0ee 307 m_startValue = value;
e179bd65
RR
308}
309
310void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
311{
618a5e38 312 // TODO focus doesn't return to the wxTextCtrl when this closes...
e179bd65
RR
313 if (event.m_keyCode == WXK_RETURN)
314 {
315 (*m_accept) = TRUE;
316 (*m_res) = GetValue();
33ac7e6f 317
bce1406b
RR
318 if ((*m_accept) && ((*m_res) != m_startValue))
319 m_owner->OnRenameAccept();
33ac7e6f 320
618a5e38
RR
321 if (!wxPendingDelete.Member(this))
322 wxPendingDelete.Append(this);
323
e179bd65
RR
324 return;
325 }
326 if (event.m_keyCode == WXK_ESCAPE)
327 {
328 (*m_accept) = FALSE;
329 (*m_res) = "";
33ac7e6f 330
bce1406b
RR
331 if (!wxPendingDelete.Member(this))
332 wxPendingDelete.Append(this);
33ac7e6f 333
e179bd65
RR
334 return;
335 }
336 event.Skip();
c13cace1
VS
337}
338
339void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
340{
341 // auto-grow the textctrl:
342 wxSize parentSize = m_owner->GetSize();
343 wxPoint myPos = GetPosition();
344 wxSize mySize = GetSize();
345 int sx, sy;
346 GetTextExtent(GetValue() + _T("MM"), &sx, &sy);
347 if (myPos.x + sx > parentSize.x) sx = parentSize.x - myPos.x;
348 if (mySize.x > sx) sx = mySize.x;
349 SetSize(sx, -1);
33ac7e6f 350
c13cace1 351 event.Skip();
e179bd65
RR
352}
353
354void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
355{
bce1406b
RR
356 if (!wxPendingDelete.Member(this))
357 wxPendingDelete.Append(this);
9dfbf520 358
5f1ea0ee
RR
359 if ((*m_accept) && ((*m_res) != m_startValue))
360 m_owner->OnRenameAccept();
e179bd65
RR
361}
362
941830cb 363#if 0
f135ff73 364// -----------------------------------------------------------------------------
c801d85f 365// wxTreeEvent
f135ff73 366// -----------------------------------------------------------------------------
c801d85f 367
e179bd65 368IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
9dfbf520 369
f135ff73 370wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
92976ab6 371 : wxNotifyEvent( commandType, id )
c801d85f 372{
00e12320
RR
373 m_code = 0;
374 m_itemOld = (wxGenericTreeItem *)NULL;
edaa81ae 375}
941830cb 376#endif
c801d85f 377
f135ff73 378// -----------------------------------------------------------------------------
c801d85f 379// wxGenericTreeItem
f135ff73 380// -----------------------------------------------------------------------------
c801d85f 381
f135ff73
VZ
382wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
383 const wxString& text,
406005d2 384 wxDC& WXUNUSED(dc),
f135ff73
VZ
385 int image, int selImage,
386 wxTreeItemData *data)
387 : m_text(text)
c801d85f 388{
00e12320
RR
389 m_images[wxTreeItemIcon_Normal] = image;
390 m_images[wxTreeItemIcon_Selected] = selImage;
391 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
392 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
8dc99046 393
00e12320
RR
394 m_data = data;
395 m_x = m_y = 0;
f135ff73 396
00e12320
RR
397 m_isCollapsed = TRUE;
398 m_hasHilight = FALSE;
399 m_hasPlus = FALSE;
400 m_isBold = FALSE;
c801d85f 401
00e12320 402 m_parent = parent;
f135ff73 403
00e12320 404 m_attr = (wxTreeItemAttr *)NULL;
618a5e38 405 m_ownsAttr = FALSE;
06b466c7 406
00e12320
RR
407 // We don't know the height here yet.
408 m_width = 0;
409 m_height = 0;
edaa81ae 410}
c801d85f 411
f135ff73 412wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 413{
00e12320 414 delete m_data;
4832f7c0 415
618a5e38 416 if (m_ownsAttr) delete m_attr;
9ec64fa7 417
00e12320
RR
418 wxASSERT_MSG( m_children.IsEmpty(),
419 wxT("please call DeleteChildren() before deleting the item") );
372edb9d
VZ
420}
421
941830cb 422void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
372edb9d 423{
00e12320
RR
424 size_t count = m_children.Count();
425 for ( size_t n = 0; n < count; n++ )
a43a4f9d 426 {
00e12320
RR
427 wxGenericTreeItem *child = m_children[n];
428 if (tree)
429 tree->SendDeleteEvent(child);
a43a4f9d 430
00e12320
RR
431 child->DeleteChildren(tree);
432 delete child;
433 }
372edb9d 434
00e12320 435 m_children.Empty();
edaa81ae 436}
c801d85f 437
91b8de8d 438void wxGenericTreeItem::SetText( const wxString &text )
c801d85f 439{
00e12320 440 m_text = text;
edaa81ae 441}
c801d85f 442
4832f7c0 443size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 444{
00e12320
RR
445 size_t count = m_children.Count();
446 if ( !recursively )
447 return count;
4832f7c0 448
00e12320 449 size_t total = count;
f2593d0d 450 for (size_t n = 0; n < count; ++n)
00e12320
RR
451 {
452 total += m_children[n]->GetChildrenCount();
453 }
c801d85f 454
00e12320 455 return total;
edaa81ae 456}
c801d85f 457
618a5e38
RR
458void wxGenericTreeItem::GetSize( int &x, int &y,
459 const wxGenericTreeCtrl *theButton )
c801d85f 460{
618a5e38 461 int bottomY=m_y+theButton->GetLineHeight(this);
00e12320
RR
462 if ( y < bottomY ) y = bottomY;
463 int width = m_x + m_width;
464 if ( x < width ) x = width;
f135ff73 465
00e12320 466 if (IsExpanded())
4832f7c0 467 {
00e12320
RR
468 size_t count = m_children.Count();
469 for ( size_t n = 0; n < count; ++n )
470 {
618a5e38 471 m_children[n]->GetSize( x, y, theButton );
00e12320 472 }
df875e59 473 }
edaa81ae 474}
c801d85f 475
618a5e38
RR
476wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point,
477 const wxGenericTreeCtrl *theCtrl,
478 int &flags,
479 int level)
c801d85f 480{
618a5e38
RR
481 // for a hidden root node, don't evaluate it, but do evaluate children
482 if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) )
c801d85f 483 {
618a5e38
RR
484 // evaluate the item
485 int h = theCtrl->GetLineHeight(this);
486 if ((point.y > m_y) && (point.y < m_y + h))
f2593d0d 487 {
618a5e38
RR
488 int y_mid = m_y + h/2;
489 if (point.y < y_mid )
490 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
491 else
492 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
d3a9f4af 493
618a5e38
RR
494 // 5 is the size of the plus sign
495 int xCross = m_x - theCtrl->GetSpacing();
496 if ((point.x > xCross-5) && (point.x < xCross+5) &&
497 (point.y > y_mid-5) && (point.y < y_mid+5) &&
498 HasPlus() && theCtrl->HasButtons() )
499 {
500 flags |= wxTREE_HITTEST_ONITEMBUTTON;
501 return this;
502 }
0ae7f2a2 503
618a5e38
RR
504 if ((point.x >= m_x) && (point.x <= m_x+m_width))
505 {
506 int image_w = -1;
507 int image_h;
91b8de8d 508
618a5e38
RR
509 // assuming every image (normal and selected) has the same size!
510 if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal )
511 theCtrl->m_imageListNormal->GetSize(GetImage(),
512 image_w, image_h);
513
514 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
515 flags |= wxTREE_HITTEST_ONITEMICON;
516 else
517 flags |= wxTREE_HITTEST_ONITEMLABEL;
518
519 return this;
520 }
521
522 if (point.x < m_x)
523 flags |= wxTREE_HITTEST_ONITEMINDENT;
524 if (point.x > m_x+m_width)
525 flags |= wxTREE_HITTEST_ONITEMRIGHT;
91b8de8d 526
f2593d0d
RR
527 return this;
528 }
91b8de8d 529
618a5e38
RR
530 // if children are expanded, fall through to evaluate them
531 if (m_isCollapsed) return (wxGenericTreeItem*) NULL;
f2593d0d 532 }
618a5e38
RR
533
534 // evaluate children
535 size_t count = m_children.Count();
536 for ( size_t n = 0; n < count; n++ )
c801d85f 537 {
618a5e38
RR
538 wxGenericTreeItem *res = m_children[n]->HitTest( point,
539 theCtrl,
540 flags,
541 level + 1 );
542 if ( res != NULL )
543 return res;
edaa81ae 544 }
f135ff73 545
f2593d0d 546 return (wxGenericTreeItem*) NULL;
edaa81ae 547}
c801d85f 548
8dc99046
VZ
549int wxGenericTreeItem::GetCurrentImage() const
550{
551 int image = NO_IMAGE;
552 if ( IsExpanded() )
553 {
554 if ( IsSelected() )
555 {
556 image = GetImage(wxTreeItemIcon_SelectedExpanded);
557 }
558
559 if ( image == NO_IMAGE )
560 {
561 // we usually fall back to the normal item, but try just the
562 // expanded one (and not selected) first in this case
563 image = GetImage(wxTreeItemIcon_Expanded);
564 }
565 }
566 else // not expanded
567 {
568 if ( IsSelected() )
569 image = GetImage(wxTreeItemIcon_Selected);
570 }
571
618a5e38
RR
572 // maybe it doesn't have the specific image we want,
573 // try the default one instead
574 if ( image == NO_IMAGE ) image = GetImage();
8dc99046
VZ
575
576 return image;
577}
578
f135ff73 579// -----------------------------------------------------------------------------
941830cb 580// wxGenericTreeCtrl implementation
f135ff73
VZ
581// -----------------------------------------------------------------------------
582
941830cb 583IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow)
f135ff73 584
941830cb
JS
585BEGIN_EVENT_TABLE(wxGenericTreeCtrl,wxScrolledWindow)
586 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
587 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
588 EVT_CHAR (wxGenericTreeCtrl::OnChar)
589 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
590 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
591 EVT_IDLE (wxGenericTreeCtrl::OnIdle)
f135ff73
VZ
592END_EVENT_TABLE()
593
618a5e38 594#if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
233058c7
JS
595/*
596 * wxTreeCtrl has to be a real class or we have problems with
597 * the run-time information.
598 */
599
600IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
601#endif
602
f135ff73
VZ
603// -----------------------------------------------------------------------------
604// construction/destruction
605// -----------------------------------------------------------------------------
91b8de8d 606
941830cb 607void wxGenericTreeCtrl::Init()
c801d85f 608{
618a5e38 609 m_current = m_key_current = m_anchor = (wxGenericTreeItem *) NULL;
00e12320
RR
610 m_hasFocus = FALSE;
611 m_dirty = FALSE;
f135ff73 612
00e12320
RR
613 m_lineHeight = 10;
614 m_indent = 15;
615 m_spacing = 18;
f135ff73 616
b771aa29
VZ
617 m_hilightBrush = new wxBrush
618 (
619 wxSystemSettings::GetSystemColour
620 (
621 wxSYS_COLOUR_HIGHLIGHT
622 ),
623 wxSOLID
624 );
625
626 m_hilightUnfocusedBrush = new wxBrush
627 (
628 wxSystemSettings::GetSystemColour
629 (
630 wxSYS_COLOUR_BTNSHADOW
631 ),
632 wxSOLID
633 );
f135ff73 634
618a5e38 635 m_imageListNormal = m_imageListButtons =
00e12320 636 m_imageListState = (wxImageList *) NULL;
618a5e38 637 m_ownsImageListNormal = m_ownsImageListButtons =
46cd520d 638 m_ownsImageListState = FALSE;
978f38c2 639
00e12320 640 m_dragCount = 0;
3dbeaa52 641 m_isDragging = FALSE;
618a5e38 642 m_dropTarget = m_oldSelection = (wxGenericTreeItem *)NULL;
9dfbf520 643
00e12320 644 m_renameTimer = new wxTreeRenameTimer( this );
f6bcfd97 645 m_lastOnSame = FALSE;
f38374d0 646
00e12320
RR
647 m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT );
648 m_boldFont = wxFont( m_normalFont.GetPointSize(),
618a5e38
RR
649 m_normalFont.GetFamily(),
650 m_normalFont.GetStyle(),
651 wxBOLD,
652 m_normalFont.GetUnderlined());
edaa81ae 653}
c801d85f 654
618a5e38
RR
655bool wxGenericTreeCtrl::Create(wxWindow *parent,
656 wxWindowID id,
657 const wxPoint& pos,
658 const wxSize& size,
659 long style,
660 const wxValidator &validator,
661 const wxString& name )
c801d85f 662{
618a5e38
RR
663 wxScrolledWindow::Create( parent, id, pos, size,
664 style|wxHSCROLL|wxVSCROLL, name );
665
666 // If the tree display has no buttons, but does have
667 // connecting lines, we can use a narrower layout.
668 // It may not be a good idea to force this...
669 if (!HasButtons() && !HasFlag(wxTR_NO_LINES))
670 {
671 m_indent= 10;
672 m_spacing = 10;
673 }
978f38c2 674
ce4169a4 675#if wxUSE_VALIDATORS
00e12320 676 SetValidator( validator );
ce4169a4 677#endif
f135ff73 678
91fc2bdc 679 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
c22886bd 680
00e12320
RR
681// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
682 m_dottedPen = wxPen( "grey", 0, 0 );
f135ff73 683
00e12320 684 return TRUE;
edaa81ae 685}
c801d85f 686
941830cb 687wxGenericTreeCtrl::~wxGenericTreeCtrl()
c801d85f 688{
b771aa29
VZ
689 delete m_hilightBrush;
690 delete m_hilightUnfocusedBrush;
a43a4f9d 691
00e12320 692 DeleteAllItems();
9dfbf520 693
00e12320 694 delete m_renameTimer;
46cd520d
VS
695 if (m_ownsImageListNormal) delete m_imageListNormal;
696 if (m_ownsImageListState) delete m_imageListState;
618a5e38 697 if (m_ownsImageListButtons) delete m_imageListButtons;
edaa81ae 698}
c801d85f 699
f135ff73
VZ
700// -----------------------------------------------------------------------------
701// accessors
702// -----------------------------------------------------------------------------
703
941830cb 704size_t wxGenericTreeCtrl::GetCount() const
c801d85f 705{
f2593d0d 706 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
edaa81ae 707}
c801d85f 708
941830cb 709void wxGenericTreeCtrl::SetIndent(unsigned int indent)
c801d85f 710{
f2593d0d
RR
711 m_indent = indent;
712 m_dirty = TRUE;
cf724bce
RR
713}
714
941830cb 715void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
cf724bce 716{
f2593d0d
RR
717 m_spacing = spacing;
718 m_dirty = TRUE;
f135ff73 719}
74bedbeb 720
941830cb 721size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
4832f7c0 722{
f2593d0d 723 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
4832f7c0 724
941830cb 725 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
4832f7c0
VZ
726}
727
618a5e38
RR
728void wxGenericTreeCtrl::SetWindowStyle(const long styles)
729{
730 // right now, just sets the styles. Eventually, we may
731 // want to update the inherited styles, but right now
732 // none of the parents has updatable styles
733 m_windowStyle = styles;
734 m_dirty = TRUE;
735}
736
f135ff73
VZ
737// -----------------------------------------------------------------------------
738// functions to work with tree items
739// -----------------------------------------------------------------------------
74bedbeb 740
941830cb 741wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
f135ff73 742{
f2593d0d 743 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
4832f7c0 744
941830cb 745 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
edaa81ae 746}
74bedbeb 747
941830cb 748int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
8dc99046 749 wxTreeItemIcon which) const
74bedbeb 750{
f2593d0d 751 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 752
941830cb 753 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
edaa81ae 754}
c801d85f 755
941830cb 756wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 757{
f2593d0d 758 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 759
941830cb 760 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
edaa81ae 761}
c801d85f 762
941830cb 763void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
f135ff73 764{
f2593d0d 765 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 766
f2593d0d 767 wxClientDC dc(this);
941830cb 768 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
769 pItem->SetText(text);
770 CalculateSize(pItem, dc);
771 RefreshLine(pItem);
f135ff73 772}
c801d85f 773
941830cb 774void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
8dc99046
VZ
775 int image,
776 wxTreeItemIcon which)
f135ff73 777{
223d09f6 778 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 779
941830cb 780 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
8dc99046 781 pItem->SetImage(image, which);
4832f7c0 782
8dc99046
VZ
783 wxClientDC dc(this);
784 CalculateSize(pItem, dc);
785 RefreshLine(pItem);
edaa81ae 786}
c801d85f 787
941830cb 788void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 789{
f2593d0d 790 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 791
941830cb 792 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
edaa81ae 793}
c801d85f 794
941830cb 795void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 796{
f2593d0d 797 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 798
941830cb 799 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
800 pItem->SetHasPlus(has);
801 RefreshLine(pItem);
edaa81ae 802}
c801d85f 803
941830cb 804void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
ef44a621 805{
9ec64fa7 806 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 807
9ec64fa7 808 // avoid redrawing the tree if no real change
941830cb 809 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
810 if ( pItem->IsBold() != bold )
811 {
812 pItem->SetBold(bold);
813 RefreshLine(pItem);
814 }
815}
816
941830cb 817void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
9ec64fa7
VZ
818 const wxColour& col)
819{
820 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
821
941830cb 822 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
823 pItem->Attr().SetTextColour(col);
824 RefreshLine(pItem);
825}
826
941830cb 827void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
9ec64fa7
VZ
828 const wxColour& col)
829{
830 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
831
941830cb 832 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
833 pItem->Attr().SetBackgroundColour(col);
834 RefreshLine(pItem);
835}
836
941830cb 837void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
9ec64fa7
VZ
838{
839 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
840
941830cb 841 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7 842 pItem->Attr().SetFont(font);
ef44a621 843 RefreshLine(pItem);
ef44a621
VZ
844}
845
3da2715f
JS
846bool wxGenericTreeCtrl::SetFont( const wxFont &font )
847{
848 wxScrolledWindow::SetFont(font);
849
850 m_normalFont = font ;
851 m_boldFont = wxFont( m_normalFont.GetPointSize(),
852 m_normalFont.GetFamily(),
853 m_normalFont.GetStyle(),
854 wxBOLD,
855 m_normalFont.GetUnderlined());
856
857 return TRUE;
858}
859
860
f135ff73
VZ
861// -----------------------------------------------------------------------------
862// item status inquiries
863// -----------------------------------------------------------------------------
864
1ed01484 865bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
c801d85f 866{
1ed01484
JS
867 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
868
c22886bd 869 // An item is only visible if it's not a descendant of a collapsed item
1ed01484 870 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
c22886bd
RR
871 wxGenericTreeItem* parent = pItem->GetParent();
872 while (parent)
873 {
874 if (!parent->IsExpanded())
875 return FALSE;
876 parent = parent->GetParent();
877 }
1ed01484
JS
878
879 int startX, startY;
880 GetViewStart(& startX, & startY);
881
882 wxSize clientSize = GetClientSize();
883
884 wxRect rect;
885 if (!GetBoundingRect(item, rect))
886 return FALSE;
c22886bd
RR
887 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
888 return FALSE;
1ed01484
JS
889 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
890 return FALSE;
891 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
892 return FALSE;
f135ff73 893
f2593d0d 894 return TRUE;
edaa81ae 895}
c801d85f 896
941830cb 897bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 898{
f2593d0d 899 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 900
941830cb 901 return !((wxGenericTreeItem*) item.m_pItem)->GetChildren().IsEmpty();
edaa81ae 902}
c801d85f 903
941830cb 904bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 905{
f2593d0d 906 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 907
941830cb 908 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
f135ff73 909}
29d87bba 910
941830cb 911bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
f135ff73 912{
f2593d0d 913 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 914
941830cb 915 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
f135ff73 916}
29d87bba 917
941830cb 918bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
ef44a621 919{
f2593d0d 920 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
ef44a621 921
941830cb 922 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
ef44a621
VZ
923}
924
f135ff73
VZ
925// -----------------------------------------------------------------------------
926// navigation
927// -----------------------------------------------------------------------------
29d87bba 928
941830cb 929wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const
f135ff73 930{
618a5e38 931 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 932
618a5e38 933 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
f135ff73 934}
29d87bba 935
941830cb 936wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
f135ff73 937{
618a5e38 938 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 939
618a5e38
RR
940 cookie = 0;
941 return GetNextChild(item, cookie);
f135ff73 942}
29d87bba 943
941830cb 944wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
f135ff73 945{
618a5e38 946 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 947
618a5e38
RR
948 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
949 if ( (size_t)cookie < children.Count() )
950 {
951 return children.Item((size_t)cookie++);
952 }
953 else
954 {
955 // there are no more of them
956 return wxTreeItemId();
957 }
f135ff73 958}
29d87bba 959
941830cb 960wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
978f38c2 961{
618a5e38 962 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 963
618a5e38
RR
964 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
965 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
966}
967
941830cb 968wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
f135ff73 969{
618a5e38 970 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 971
618a5e38
RR
972 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
973 wxGenericTreeItem *parent = i->GetParent();
974 if ( parent == NULL )
975 {
976 // root item doesn't have any siblings
977 return wxTreeItemId();
978 }
4832f7c0 979
618a5e38
RR
980 wxArrayGenericTreeItems& siblings = parent->GetChildren();
981 int index = siblings.Index(i);
982 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 983
618a5e38
RR
984 size_t n = (size_t)(index + 1);
985 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 986}
c801d85f 987
941830cb 988wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 989{
618a5e38 990 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 991
618a5e38
RR
992 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
993 wxGenericTreeItem *parent = i->GetParent();
994 if ( parent == NULL )
995 {
996 // root item doesn't have any siblings
997 return wxTreeItemId();
998 }
4832f7c0 999
618a5e38
RR
1000 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1001 int index = siblings.Index(i);
1002 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1003
618a5e38
RR
1004 return index == 0 ? wxTreeItemId()
1005 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 1006}
389cdc7a 1007
1ed01484
JS
1008// Only for internal use right now, but should probably be public
1009wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
f135ff73 1010{
618a5e38
RR
1011 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1012
1013 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1014
1015 // First see if there are any children.
1016 wxArrayGenericTreeItems& children = i->GetChildren();
1017 if (children.GetCount() > 0)
1018 {
1019 return children.Item(0);
1020 }
1021 else
1022 {
1023 // Try a sibling of this or ancestor instead
1024 wxTreeItemId p = item;
1025 wxTreeItemId toFind;
1026 do
1027 {
1028 toFind = GetNextSibling(p);
1029 p = GetParent(p);
1030 } while (p.IsOk() && !toFind.IsOk());
1031 return toFind;
1032 }
1ed01484
JS
1033}
1034
1ed01484
JS
1035wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1036{
618a5e38
RR
1037 wxTreeItemId id = GetRootItem();
1038 if (!id.IsOk())
1ed01484 1039 return id;
1ed01484 1040
618a5e38
RR
1041 do
1042 {
1043 if (IsVisible(id))
1044 return id;
1045 id = GetNext(id);
1046 } while (id.IsOk());
1047
1048 return wxTreeItemId();
1ed01484
JS
1049}
1050
941830cb 1051wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
f135ff73 1052{
618a5e38 1053 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1054
618a5e38
RR
1055 wxTreeItemId id = item;
1056 if (id.IsOk())
1057 {
1058 while (id = GetNext(id), id.IsOk())
1059 {
1060 if (IsVisible(id))
1061 return id;
1062 }
1063 }
1064 return wxTreeItemId();
f135ff73 1065}
29d87bba 1066
941830cb 1067wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
f135ff73 1068{
618a5e38 1069 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1070
618a5e38 1071 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1072
618a5e38 1073 return wxTreeItemId();
edaa81ae 1074}
c801d85f 1075
f135ff73
VZ
1076// -----------------------------------------------------------------------------
1077// operations
1078// -----------------------------------------------------------------------------
1079
941830cb 1080wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1081 size_t previous,
1082 const wxString& text,
1083 int image, int selImage,
1084 wxTreeItemData *data)
c801d85f 1085{
941830cb 1086 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f49f2b0c
RR
1087 if ( !parent )
1088 {
1089 // should we give a warning here?
1090 return AddRoot(text, image, selImage, data);
1091 }
4832f7c0 1092
618a5e38
RR
1093 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1094
f49f2b0c 1095 wxClientDC dc(this);
f38374d0 1096 wxGenericTreeItem *item =
f49f2b0c 1097 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
74bedbeb 1098
f49f2b0c
RR
1099 if ( data != NULL )
1100 {
40fab78b 1101 data->m_pItem = (long) item;
f49f2b0c 1102 }
74bedbeb 1103
f49f2b0c 1104 parent->Insert( item, previous );
ef44a621 1105
f49f2b0c 1106 return item;
4c681997
RR
1107}
1108
941830cb 1109wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
f135ff73
VZ
1110 int image, int selImage,
1111 wxTreeItemData *data)
4c681997 1112{
f49f2b0c 1113 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 1114
618a5e38
RR
1115 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1116
f49f2b0c
RR
1117 wxClientDC dc(this);
1118 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
f135ff73 1119 image, selImage, data);
618a5e38
RR
1120 if (HasFlag(wxTR_HIDE_ROOT))
1121 {
1122 // if root is hidden, make sure we can navigate
1123 // into children
1124 m_anchor->SetHasPlus();
1125 Expand(m_anchor);
1126 }
f49f2b0c
RR
1127 if ( data != NULL )
1128 {
40fab78b 1129 data->m_pItem = (long) m_anchor;
f49f2b0c 1130 }
f38374d0 1131
f49f2b0c
RR
1132 if (!HasFlag(wxTR_MULTIPLE))
1133 {
1134 m_current = m_key_current = m_anchor;
06b466c7 1135 m_current->SetHilight( TRUE );
f49f2b0c 1136 }
389cdc7a 1137
f49f2b0c 1138 return m_anchor;
edaa81ae 1139}
c801d85f 1140
941830cb 1141wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
f135ff73
VZ
1142 const wxString& text,
1143 int image, int selImage,
1144 wxTreeItemData *data)
c801d85f 1145{
f2593d0d 1146 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 1147}
c801d85f 1148
941830cb 1149wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1150 const wxTreeItemId& idPrevious,
1151 const wxString& text,
1152 int image, int selImage,
1153 wxTreeItemData *data)
c801d85f 1154{
941830cb 1155 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1156 if ( !parent )
1157 {
1158 // should we give a warning here?
1159 return AddRoot(text, image, selImage, data);
1160 }
c801d85f 1161
941830cb 1162 int index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
f2593d0d 1163 wxASSERT_MSG( index != wxNOT_FOUND,
941830cb 1164 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
06b466c7 1165
f2593d0d
RR
1166 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1167}
1168
941830cb 1169wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f2593d0d
RR
1170 size_t before,
1171 const wxString& text,
1172 int image, int selImage,
1173 wxTreeItemData *data)
1174{
941830cb 1175 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1176 if ( !parent )
1177 {
1178 // should we give a warning here?
1179 return AddRoot(text, image, selImage, data);
1180 }
1181
1182 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 1183}
c801d85f 1184
941830cb 1185wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
f135ff73
VZ
1186 const wxString& text,
1187 int image, int selImage,
1188 wxTreeItemData *data)
74bedbeb 1189{
941830cb 1190 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1191 if ( !parent )
1192 {
1193 // should we give a warning here?
1194 return AddRoot(text, image, selImage, data);
1195 }
f135ff73 1196
f2593d0d
RR
1197 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1198 image, selImage, data);
74bedbeb
VZ
1199}
1200
941830cb 1201void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
a43a4f9d 1202{
f2593d0d 1203 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
40fab78b 1204 event.m_item = (long) item;
f2593d0d
RR
1205 event.SetEventObject( this );
1206 ProcessEvent( event );
a43a4f9d
VZ
1207}
1208
941830cb 1209void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
372edb9d 1210{
618a5e38
RR
1211 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1212
941830cb 1213 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
a43a4f9d 1214 item->DeleteChildren(this);
372edb9d
VZ
1215}
1216
941830cb 1217void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1218{
618a5e38
RR
1219 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1220
941830cb 1221 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
ff5bf259 1222
618a5e38
RR
1223 // don't stay with invalid m_key_current or we will crash in
1224 // the next call to OnChar()
aaa2f297
VZ
1225 bool changeKeyCurrent = FALSE;
1226 wxGenericTreeItem *itemKey = m_key_current;
618a5e38 1227 while ( itemKey )
aaa2f297
VZ
1228 {
1229 if ( itemKey == item )
1230 {
1231 // m_key_current is a descendant of the item being deleted
1232 changeKeyCurrent = TRUE;
618a5e38 1233 break;
aaa2f297 1234 }
618a5e38 1235 itemKey = itemKey->GetParent();
aaa2f297
VZ
1236 }
1237
1238 wxGenericTreeItem *parent = item->GetParent();
97d7bfb8
RR
1239 if ( parent )
1240 {
1241 parent->GetChildren().Remove( item ); // remove by value
1242 }
f135ff73 1243
aaa2f297
VZ
1244 if ( changeKeyCurrent )
1245 {
1246 // may be NULL or not
1247 m_key_current = parent;
1248 }
1249
97d7bfb8
RR
1250 item->DeleteChildren(this);
1251 SendDeleteEvent(item);
1252 delete item;
edaa81ae 1253}
c801d85f 1254
941830cb 1255void wxGenericTreeCtrl::DeleteAllItems()
c801d85f 1256{
97d7bfb8
RR
1257 if ( m_anchor )
1258 {
618a5e38
RR
1259 m_dirty = TRUE;
1260
97d7bfb8
RR
1261 m_anchor->DeleteChildren(this);
1262 delete m_anchor;
a43a4f9d 1263
97d7bfb8 1264 m_anchor = NULL;
97d7bfb8 1265 }
edaa81ae
RR
1266}
1267
941830cb 1268void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1269{
941830cb 1270 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1271
941830cb 1272 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
f6bcfd97 1273
f2593d0d
RR
1274 if ( !item->HasPlus() )
1275 return;
978f38c2 1276
f2593d0d
RR
1277 if ( item->IsExpanded() )
1278 return;
f135ff73 1279
f2593d0d 1280 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
40fab78b 1281 event.m_item = (long) item;
f2593d0d 1282 event.SetEventObject( this );
004fd0c8 1283
f2593d0d
RR
1284 if ( ProcessEvent( event ) && !event.IsAllowed() )
1285 {
1286 // cancelled by program
1287 return;
1288 }
4832f7c0 1289
f2593d0d
RR
1290 item->Expand();
1291 CalculatePositions();
f135ff73 1292
f2593d0d 1293 RefreshSubtree(item);
f135ff73 1294
f2593d0d
RR
1295 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1296 ProcessEvent( event );
edaa81ae
RR
1297}
1298
941830cb 1299void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
f6bcfd97
BP
1300{
1301 Expand(item);
1302 if ( IsExpanded(item) )
1303 {
1304 long cookie;
1305 wxTreeItemId child = GetFirstChild(item, cookie);
1306 while ( child.IsOk() )
1307 {
1308 ExpandAll(child);
1309
1310 child = GetNextChild(item, cookie);
1311 }
1312 }
1313}
1314
941830cb 1315void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1316{
941830cb 1317 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1318
f2593d0d
RR
1319 if ( !item->IsExpanded() )
1320 return;
f135ff73 1321
f2593d0d 1322 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
40fab78b 1323 event.m_item = (long) item;
f2593d0d
RR
1324 event.SetEventObject( this );
1325 if ( ProcessEvent( event ) && !event.IsAllowed() )
1326 {
1327 // cancelled by program
1328 return;
1329 }
4832f7c0 1330
f2593d0d 1331 item->Collapse();
f135ff73 1332
618a5e38 1333#if 0 // TODO why should items be collapsed recursively?
f2593d0d
RR
1334 wxArrayGenericTreeItems& children = item->GetChildren();
1335 size_t count = children.Count();
1336 for ( size_t n = 0; n < count; n++ )
1337 {
1338 Collapse(children[n]);
1339 }
618a5e38 1340#endif
f135ff73 1341
f2593d0d 1342 CalculatePositions();
f135ff73 1343
f2593d0d 1344 RefreshSubtree(item);
f135ff73 1345
f2593d0d
RR
1346 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1347 ProcessEvent( event );
edaa81ae 1348}
c801d85f 1349
941830cb 1350void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1351{
00e12320
RR
1352 Collapse(item);
1353 DeleteChildren(item);
edaa81ae 1354}
c801d85f 1355
941830cb 1356void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1357{
941830cb 1358 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
389cdc7a 1359
00e12320
RR
1360 if (item->IsExpanded())
1361 Collapse(itemId);
1362 else
1363 Expand(itemId);
f135ff73 1364}
389cdc7a 1365
941830cb 1366void wxGenericTreeCtrl::Unselect()
f135ff73 1367{
00e12320
RR
1368 if (m_current)
1369 {
1370 m_current->SetHilight( FALSE );
1371 RefreshLine( m_current );
1372 }
edaa81ae 1373}
c801d85f 1374
941830cb 1375void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1376{
00e12320
RR
1377 if (item->IsSelected())
1378 {
1379 item->SetHilight(FALSE);
1380 RefreshLine(item);
1381 }
d3a9f4af 1382
00e12320 1383 if (item->HasChildren())
88ac883a 1384 {
00e12320
RR
1385 wxArrayGenericTreeItems& children = item->GetChildren();
1386 size_t count = children.Count();
1387 for ( size_t n = 0; n < count; ++n )
1388 {
1389 UnselectAllChildren(children[n]);
1390 }
88ac883a
VZ
1391 }
1392}
f135ff73 1393
941830cb 1394void wxGenericTreeCtrl::UnselectAll()
88ac883a 1395{
941830cb 1396 UnselectAllChildren((wxGenericTreeItem*) GetRootItem().m_pItem);
88ac883a
VZ
1397}
1398
1399// Recursive function !
1400// To stop we must have crt_item<last_item
91b8de8d 1401// Algorithm :
88ac883a 1402// Tag all next children, when no more children,
d3a9f4af 1403// Move to parent (not to tag)
91b8de8d 1404// Keep going... if we found last_item, we stop.
941830cb 1405bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a
VZ
1406{
1407 wxGenericTreeItem *parent = crt_item->GetParent();
1408
00e12320
RR
1409 if (parent == NULL) // This is root item
1410 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1411
91b8de8d 1412 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1413 int index = children.Index(crt_item);
1414 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1415
1416 size_t count = children.Count();
1417 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1418 {
1419 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1420 }
88ac883a
VZ
1421
1422 return TagNextChildren(parent, last_item, select);
1423}
1424
941830cb 1425bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a 1426{
00e12320
RR
1427 crt_item->SetHilight(select);
1428 RefreshLine(crt_item);
d3a9f4af 1429
06b466c7 1430 if (crt_item==last_item)
00e12320 1431 return TRUE;
88ac883a 1432
00e12320 1433 if (crt_item->HasChildren())
88ac883a 1434 {
00e12320
RR
1435 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1436 size_t count = children.Count();
1437 for ( size_t n = 0; n < count; ++n )
1438 {
06b466c7 1439 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1440 return TRUE;
06b466c7 1441 }
88ac883a 1442 }
d3a9f4af 1443
c0de7af4 1444 return FALSE;
88ac883a
VZ
1445}
1446
941830cb 1447void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
88ac883a 1448{
f2593d0d
RR
1449 // item2 is not necessary after item1
1450 wxGenericTreeItem *first=NULL, *last=NULL;
88ac883a 1451
f2593d0d
RR
1452 // choice first' and 'last' between item1 and item2
1453 if (item1->GetY()<item2->GetY())
06b466c7 1454 {
f2593d0d
RR
1455 first=item1;
1456 last=item2;
1457 }
1458 else
1459 {
1460 first=item2;
1461 last=item1;
1462 }
88ac883a 1463
f2593d0d 1464 bool select = m_current->IsSelected();
d3a9f4af 1465
f2593d0d
RR
1466 if ( TagAllChildrenUntilLast(first,last,select) )
1467 return;
88ac883a 1468
f2593d0d 1469 TagNextChildren(first,last,select);
88ac883a
VZ
1470}
1471
941830cb 1472void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
c193b707
VZ
1473 bool unselect_others,
1474 bool extended_select)
d3a9f4af 1475{
223d09f6 1476 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1477
88ac883a 1478 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
941830cb 1479 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
88ac883a
VZ
1480
1481 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1482 // wxT("this is a single selection tree") );
88ac883a
VZ
1483
1484 // to keep going anyhow !!!
d3a9f4af 1485 if (is_single)
8dc99046
VZ
1486 {
1487 if (item->IsSelected())
1488 return; // nothing to do
1489 unselect_others = TRUE;
1490 extended_select = FALSE;
1491 }
1492 else if ( unselect_others && item->IsSelected() )
1493 {
1494 // selection change if there is more than one item currently selected
1495 wxArrayTreeItemIds selected_items;
1496 if ( GetSelections(selected_items) == 1 )
1497 return;
1498 }
d3a9f4af 1499
f135ff73 1500 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
40fab78b
JS
1501 event.m_item = (long) item;
1502 event.m_itemOld = (long) m_current;
f135ff73 1503 event.SetEventObject( this );
91b8de8d 1504 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1505
f98e2558 1506 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
618a5e38 1507 return;
f135ff73 1508
2cc78389
RR
1509 wxTreeItemId parent = GetParent( itemId );
1510 while (parent.IsOk())
1511 {
1512 if (!IsExpanded(parent))
1513 Expand( parent );
cdb3cffe 1514
2cc78389
RR
1515 parent = GetParent( parent );
1516 }
cdb3cffe 1517
2cc78389 1518 EnsureVisible( itemId );
cdb3cffe 1519
88ac883a
VZ
1520 // ctrl press
1521 if (unselect_others)
389cdc7a 1522 {
88ac883a 1523 if (is_single) Unselect(); // to speed up thing
c193b707 1524 else UnselectAll();
edaa81ae 1525 }
f135ff73 1526
88ac883a 1527 // shift press
d3a9f4af 1528 if (extended_select)
88ac883a 1529 {
aaa2f297
VZ
1530 if ( !m_current )
1531 {
618a5e38 1532 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
1533 }
1534
88ac883a
VZ
1535 // don't change the mark (m_current)
1536 SelectItemRange(m_current, item);
1537 }
1538 else
1539 {
c0de7af4 1540 bool select=TRUE; // the default
88ac883a 1541
c193b707
VZ
1542 // Check if we need to toggle hilight (ctrl mode)
1543 if (!unselect_others)
618a5e38 1544 select=!item->IsSelected();
88ac883a 1545
91b8de8d 1546 m_current = m_key_current = item;
c193b707
VZ
1547 m_current->SetHilight(select);
1548 RefreshLine( m_current );
88ac883a 1549 }
389cdc7a 1550
f135ff73 1551 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1552 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1553}
1554
941830cb 1555void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
9dfbf520 1556 wxArrayTreeItemIds &array) const
c801d85f 1557{
8dc99046 1558 if ( item->IsSelected() )
9dfbf520 1559 array.Add(wxTreeItemId(item));
91b8de8d 1560
9dfbf520 1561 if ( item->HasChildren() )
91b8de8d 1562 {
9dfbf520
VZ
1563 wxArrayGenericTreeItems& children = item->GetChildren();
1564 size_t count = children.GetCount();
1565 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1566 FillArray(children[n], array);
91b8de8d
RR
1567 }
1568}
1569
941830cb 1570size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d 1571{
618a5e38
RR
1572 array.Empty();
1573 wxTreeItemId idRoot = GetRootItem();
1574 if ( idRoot.IsOk() )
1575 {
1576 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1577 }
1578 //else: the tree is empty, so no selections
91b8de8d 1579
618a5e38 1580 return array.Count();
91b8de8d
RR
1581}
1582
941830cb 1583void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1584{
91b8de8d
RR
1585 if (!item.IsOk()) return;
1586
941830cb 1587 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 1588
f65635b5
VZ
1589 // first expand all parent branches
1590 wxGenericTreeItem *parent = gitem->GetParent();
5391f772 1591 while ( parent )
f65635b5 1592 {
8dc99046 1593 Expand(parent);
f65635b5
VZ
1594 parent = parent->GetParent();
1595 }
1596
5391f772 1597 //if (parent) CalculatePositions();
91b8de8d
RR
1598
1599 ScrollTo(item);
1600}
1601
941830cb 1602void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d
RR
1603{
1604 if (!item.IsOk()) return;
1605
dc6c62a9
RR
1606 // We have to call this here because the label in
1607 // question might just have been added and no screen
1608 // update taken place.
cd66f45b 1609 if (m_dirty) wxYieldIfNeeded();
dc6c62a9 1610
941830cb 1611 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1612
f65635b5 1613 // now scroll to the item
0659e7ee 1614 int item_y = gitem->GetY();
8dc99046 1615
0659e7ee
RR
1616 int start_x = 0;
1617 int start_y = 0;
1e6feb95 1618 GetViewStart( &start_x, &start_y );
91b8de8d 1619 start_y *= PIXELS_PER_UNIT;
978f38c2 1620
a93109d5
RR
1621 int client_h = 0;
1622 int client_w = 0;
1623 GetClientSize( &client_w, &client_h );
ef44a621 1624
0659e7ee
RR
1625 if (item_y < start_y+3)
1626 {
91b8de8d 1627 // going down
0659e7ee
RR
1628 int x = 0;
1629 int y = 0;
91b8de8d
RR
1630 m_anchor->GetSize( x, y, this );
1631 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1632 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1633 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1634 // Item should appear at top
91b8de8d 1635 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1636 }
91b8de8d 1637 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1638 {
c7a9fa36
RR
1639 // going up
1640 int x = 0;
1641 int y = 0;
1642 m_anchor->GetSize( x, y, this );
1643 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1644 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1645 item_y += PIXELS_PER_UNIT+2;
1646 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1647 // Item should appear at bottom
c7a9fa36 1648 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 1649 }
edaa81ae 1650}
c801d85f 1651
e1ee62bd 1652// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 1653static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1654
004fd0c8 1655static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1656 wxGenericTreeItem **item2)
edaa81ae 1657{
941830cb 1658 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1659
1660 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1661}
1662
941830cb 1663int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
e1ee62bd 1664 const wxTreeItemId& item2)
0659e7ee 1665{
87138c52 1666 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1667}
1668
941830cb 1669void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 1670{
223d09f6 1671 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 1672
941830cb 1673 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 1674
e1ee62bd 1675 wxCHECK_RET( !s_treeBeingSorted,
941830cb 1676 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1677
91b8de8d 1678 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1679 if ( children.Count() > 1 )
1680 {
618a5e38
RR
1681 m_dirty = TRUE;
1682
e1ee62bd
VZ
1683 s_treeBeingSorted = this;
1684 children.Sort(tree_ctrl_compare_func);
1685 s_treeBeingSorted = NULL;
e1ee62bd
VZ
1686 }
1687 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1688}
1689
941830cb 1690wxImageList *wxGenericTreeCtrl::GetImageList() const
edaa81ae 1691{
f135ff73 1692 return m_imageListNormal;
edaa81ae
RR
1693}
1694
618a5e38
RR
1695wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
1696{
1697 return m_imageListButtons;
1698}
1699
941830cb 1700wxImageList *wxGenericTreeCtrl::GetStateImageList() const
c801d85f 1701{
f135ff73 1702 return m_imageListState;
edaa81ae 1703}
c801d85f 1704
618a5e38 1705void wxGenericTreeCtrl::CalculateLineHeight()
e2414cbe 1706{
f2593d0d
RR
1707 wxClientDC dc(this);
1708 m_lineHeight = (int)(dc.GetCharHeight() + 4);
06b466c7 1709
618a5e38
RR
1710 if ( m_imageListNormal )
1711 {
1712 // Calculate a m_lineHeight value from the normal Image sizes.
1713 // May be toggle off. Then wxGenericTreeCtrl will spread when
1714 // necessary (which might look ugly).
1715 int n = m_imageListNormal->GetImageCount();
1716 for (int i = 0; i < n ; i++)
1717 {
1718 int width = 0, height = 0;
1719 m_imageListNormal->GetSize(i, width, height);
1720 if (height > m_lineHeight) m_lineHeight = height;
1721 }
1722 }
1723
1724 if (m_imageListButtons)
f2593d0d 1725 {
618a5e38
RR
1726 // Calculate a m_lineHeight value from the Button image sizes.
1727 // May be toggle off. Then wxGenericTreeCtrl will spread when
1728 // necessary (which might look ugly).
1729 int n = m_imageListButtons->GetImageCount();
1730 for (int i = 0; i < n ; i++)
1731 {
1732 int width = 0, height = 0;
1733 m_imageListButtons->GetSize(i, width, height);
1734 if (height > m_lineHeight) m_lineHeight = height;
1735 }
f2593d0d 1736 }
91b8de8d 1737
618a5e38 1738 if (m_lineHeight < 30)
f2593d0d 1739 m_lineHeight += 2; // at least 2 pixels
06b466c7 1740 else
f2593d0d 1741 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 1742}
e2414cbe 1743
618a5e38
RR
1744void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
1745{
1746 if (m_ownsImageListNormal) delete m_imageListNormal;
1747 m_imageListNormal = imageList;
1748 m_ownsImageListNormal = FALSE;
1749 m_dirty = TRUE;
1750 CalculateLineHeight();
1751}
1752
941830cb 1753void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1754{
46cd520d 1755 if (m_ownsImageListState) delete m_imageListState;
f135ff73 1756 m_imageListState = imageList;
46cd520d
VS
1757 m_ownsImageListState = FALSE;
1758}
1759
618a5e38
RR
1760void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
1761{
1762 if (m_ownsImageListButtons) delete m_imageListButtons;
1763 m_imageListButtons = imageList;
1764 m_ownsImageListButtons = FALSE;
1765 m_dirty = TRUE;
1766 CalculateLineHeight();
1767}
1768
46cd520d
VS
1769void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
1770{
1771 SetImageList(imageList);
1772 m_ownsImageListNormal = TRUE;
1773}
1774
1775void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
1776{
1777 SetStateImageList(imageList);
1778 m_ownsImageListState = TRUE;
edaa81ae 1779}
e2414cbe 1780
618a5e38
RR
1781void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
1782{
1783 SetButtonsImageList(imageList);
1784 m_ownsImageListButtons = TRUE;
1785}
1786
f135ff73
VZ
1787// -----------------------------------------------------------------------------
1788// helpers
1789// -----------------------------------------------------------------------------
0659e7ee 1790
941830cb 1791void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 1792{
0659e7ee
RR
1793 if (m_anchor)
1794 {
618a5e38 1795 int x = 0, y = 0;
91b8de8d 1796 m_anchor->GetSize( x, y, this );
c193b707 1797 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1798 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1799 int x_pos = GetScrollPos( wxHORIZONTAL );
1800 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1801 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1802 }
1803 else
1804 {
1805 SetScrollbars( 0, 0, 0, 0 );
1806 }
edaa81ae 1807}
c801d85f 1808
941830cb 1809int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 1810{
dc6c62a9
RR
1811 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1812 return item->GetHeight();
1813 else
1814 return m_lineHeight;
91b8de8d
RR
1815}
1816
941830cb 1817void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 1818{
618a5e38
RR
1819 // TODO implement "state" icon on items
1820
9ec64fa7
VZ
1821 wxTreeItemAttr *attr = item->GetAttributes();
1822 if ( attr && attr->HasFont() )
1823 dc.SetFont(attr->GetFont());
1824 else if (item->IsBold())
eff869aa 1825 dc.SetFont(m_boldFont);
ef44a621 1826
618a5e38 1827 long text_w = 0, text_h = 0;
bbe0af5b 1828 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1829
618a5e38 1830 int image_h = 0, image_w = 0;
8dc99046
VZ
1831 int image = item->GetCurrentImage();
1832 if ( image != NO_IMAGE )
bbe0af5b 1833 {
2c8e4738
VZ
1834 if ( m_imageListNormal )
1835 {
1836 m_imageListNormal->GetSize( image, image_w, image_h );
1837 image_w += 4;
1838 }
1839 else
1840 {
1841 image = NO_IMAGE;
1842 }
bbe0af5b 1843 }
ef44a621 1844
91b8de8d
RR
1845 int total_h = GetLineHeight(item);
1846
b771aa29 1847 if ( item->IsSelected() )
cdb3cffe 1848 {
b771aa29 1849 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
cdb3cffe 1850 }
afa6a1a1 1851 else
c0fba4d1
VS
1852 {
1853 wxColour colBg;
1854 if ( attr && attr->HasBackgroundColour() )
1855 colBg = attr->GetBackgroundColour();
1856 else
1857 colBg = m_backgroundColour;
1858 dc.SetBrush(wxBrush(colBg, wxSOLID));
1859 }
afa6a1a1 1860
cdb3cffe 1861 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
b77d9650 1862
b771aa29 1863 if ( item->IsSelected() && image != NO_IMAGE )
a69cb1cc
JS
1864 {
1865 // If it's selected, and there's an image, then we should
1866 // take care to leave the area under the image painted in the
1867 // background colour.
cdb3cffe
VZ
1868 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
1869 item->GetWidth() - image_w + 2, total_h-offset );
a69cb1cc
JS
1870 }
1871 else
b77d9650 1872 {
cdb3cffe
VZ
1873 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
1874 item->GetWidth()+2, total_h-offset );
b77d9650 1875 }
ef44a621 1876
8dc99046 1877 if ( image != NO_IMAGE )
bbe0af5b 1878 {
d30b4d20 1879 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 1880 m_imageListNormal->Draw( image, dc,
49cd56ef 1881 item->GetX(),
d701d432 1882 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1883 wxIMAGELIST_DRAW_TRANSPARENT );
1884 dc.DestroyClippingRegion();
1885 }
ef44a621 1886
afa6a1a1 1887 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
1888 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
1889 dc.DrawText( item->GetText(),
1890 (wxCoord)(image_w + item->GetX()),
1891 (wxCoord)(item->GetY() + extraH));
ef44a621 1892
eff869aa
RR
1893 // restore normal font
1894 dc.SetFont( m_normalFont );
ef44a621
VZ
1895}
1896
91b8de8d 1897// Now y stands for the top of the item, whereas it used to stand for middle !
941830cb 1898void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1899{
618a5e38
RR
1900 int x = level*m_indent;
1901 if (!HasFlag(wxTR_HIDE_ROOT))
1902 {
1903 x += m_indent;
1904 }
1905 else if (level == 0)
1906 {
1907 wxArrayGenericTreeItems& children = item->GetChildren();
1908 size_t n, count = children.Count();
1909 for (n = 0; n < count; ++n)
1910 PaintLevel(children[n], dc, 1, y);
1911 return;
1912 }
91b8de8d 1913
618a5e38
RR
1914 item->SetX(x+m_spacing);
1915 item->SetY(y);
389cdc7a 1916
618a5e38
RR
1917 int h = GetLineHeight(item);
1918 int y_top = y;
1919 int y_mid = y_top + (h>>1);
1920 y += h;
4832f7c0 1921
618a5e38
RR
1922 int exposed_x = dc.LogicalToDeviceX(0);
1923 int exposed_y = dc.LogicalToDeviceY(y_top);
233058c7 1924
618a5e38 1925 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
bbe0af5b 1926 {
618a5e38 1927 if (item->HasPlus() && HasButtons()) // should the item show a button?
cdb3cffe 1928 {
618a5e38
RR
1929 if (!HasFlag(wxTR_NO_LINES))
1930 {
1931 if (x > (signed)m_indent || HasFlag(wxTR_LINES_AT_ROOT))
1932 dc.DrawLine(x - m_indent, y_mid, x - 5, y_mid);
1933 dc.DrawLine(x + 5, y_mid, x + m_spacing, y_mid);
1934 }
1935
1936 if (m_imageListButtons != NULL)
c22886bd 1937 {
618a5e38
RR
1938 // draw the image button here
1939 int image_h = 0, image_w = 0, image = wxCLOSED_BUTTON;
1940 if (item->IsExpanded()) image = wxOPEN_BUTTON;
1941 if (item->IsSelected())
1942 image += wxOPEN_BUTTON_SELECTED - wxOPEN_BUTTON;
1943 m_imageListButtons->GetSize(image, image_w, image_h);
1944 int xx = x - (image_w>>1);
1945 int yy = y_mid - (image_h>>1);
1946 dc.SetClippingRegion(xx, yy, image_w, image_h);
1947 m_imageListButtons->Draw(image, dc, xx, yy,
1948 wxIMAGELIST_DRAW_TRANSPARENT);
1949 dc.DestroyClippingRegion();
1950 }
1951 else if (HasFlag(wxTR_TWIST_BUTTONS))
1952 {
1953 // draw the twisty button here
1954 dc.SetPen(*wxBLACK_PEN);
1955 dc.SetBrush(*m_hilightBrush);
cdb3cffe 1956
c22886bd 1957 wxPoint button[3];
cdb3cffe 1958
c22886bd
RR
1959 if (item->IsExpanded())
1960 {
1961 button[0].x = x-5;
618a5e38 1962 button[0].y = y_mid-2;
c22886bd 1963 button[1].x = x+5;
618a5e38 1964 button[1].y = y_mid-2;
c22886bd 1965 button[2].x = x;
618a5e38 1966 button[2].y = y_mid+3;
c22886bd
RR
1967 }
1968 else
1969 {
618a5e38 1970 button[0].y = y_mid-5;
c22886bd 1971 button[0].x = x-2;
618a5e38 1972 button[1].y = y_mid+5;
c22886bd 1973 button[1].x = x-2;
618a5e38 1974 button[2].y = y_mid;
c22886bd
RR
1975 button[2].x = x+3;
1976 }
618a5e38 1977 dc.DrawPolygon(3, button);
cdb3cffe 1978
618a5e38 1979 dc.SetPen(m_dottedPen);
c22886bd 1980 }
618a5e38 1981 else // if (HasFlag(wxTR_HAS_BUTTONS))
c22886bd 1982 {
618a5e38
RR
1983 // draw the plus sign here
1984 dc.SetPen(*wxGREY_PEN);
1985 dc.SetBrush(*wxWHITE_BRUSH);
1986 dc.DrawRectangle(x-5, y_mid-4, 11, 9);
1987 dc.SetPen(*wxBLACK_PEN);
1988 dc.DrawLine(x-2, y_mid, x+3, y_mid);
c22886bd 1989 if (!item->IsExpanded())
618a5e38
RR
1990 dc.DrawLine(x, y_mid-2, x, y_mid+3);
1991 dc.SetPen(m_dottedPen);
c22886bd 1992 }
bbe0af5b 1993 }
618a5e38
RR
1994 else if (!HasFlag(wxTR_NO_LINES)) // no button; maybe a line?
1995 {
1996 // draw the horizontal line here
1997 int x_start = x;
1998 if (x > (signed)m_indent || HasFlag(wxTR_LINES_AT_ROOT))
1999 x_start -= m_indent;
2000 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2001 }
c801d85f 2002
618a5e38 2003 wxPen *pen =
cdebf985 2004#ifndef __WXMAC__
618a5e38
RR
2005 // don't draw rect outline if we already have the
2006 // background color under Mac
b771aa29 2007 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
cdebf985 2008#endif // !__WXMAC__
618a5e38 2009 wxTRANSPARENT_PEN;
cdebf985
VZ
2010
2011 wxColour colText;
de57703a 2012 if ( item->IsSelected() )
cdebf985 2013 {
618a5e38 2014 colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
bbe0af5b
RR
2015 }
2016 else
2017 {
9ec64fa7 2018 wxTreeItemAttr *attr = item->GetAttributes();
618a5e38 2019 if (attr && attr->HasTextColour())
9ec64fa7
VZ
2020 colText = attr->GetTextColour();
2021 else
618a5e38 2022 colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT);
bbe0af5b 2023 }
9ec64fa7
VZ
2024
2025 // prepare to draw
2026 dc.SetTextForeground(colText);
2027 dc.SetPen(*pen);
9ec64fa7
VZ
2028
2029 // draw
2030 PaintItem(item, dc);
2031
618a5e38 2032 if (HasFlag(wxTR_ROW_LINES))
b77d9650 2033 {
618a5e38
RR
2034 dc.SetPen(*wxWHITE_PEN);
2035 dc.DrawLine(0, y_top, 10000, y_top);
2036 dc.DrawLine(0, y, 10000, y);
b77d9650 2037 }
cdb3cffe 2038
9ec64fa7 2039 // restore DC objects
618a5e38
RR
2040 dc.SetBrush(*wxWHITE_BRUSH);
2041 dc.SetPen(m_dottedPen);
2042 dc.SetTextForeground(*wxBLACK);
f135ff73 2043 }
d3a9f4af 2044
bbe0af5b
RR
2045 if (item->IsExpanded())
2046 {
91b8de8d 2047 wxArrayGenericTreeItems& children = item->GetChildren();
618a5e38 2048 int count = children.Count();
f65635b5 2049 if (count > 0)
9ec64fa7 2050 {
618a5e38
RR
2051 int n = 0, oldY;
2052 ++level;
2053 do {
2054 oldY = y;
2055 PaintLevel(children[n], dc, level, y);
2056 } while (++n < count);
2057
2058 if (!HasFlag(wxTR_NO_LINES))
2059 {
2060 // draw line down to last child
2061 oldY += GetLineHeight(children[n-1])/2;
2062 if (HasButtons()) y_mid += 5;
2063 dc.DrawLine(x, y_mid, x, oldY);
2064 }
9ec64fa7 2065 }
bbe0af5b 2066 }
4c681997 2067}
c801d85f 2068
941830cb 2069void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
2070{
2071 if ( item )
2072 {
2073 if ( item->HasPlus() )
2074 {
2075 // it's a folder, indicate it by a border
2076 DrawBorder(item);
2077 }
2078 else
2079 {
2080 // draw a line under the drop target because the item will be
2081 // dropped there
2082 DrawLine(item, TRUE /* below */);
2083 }
2084
2085 SetCursor(wxCURSOR_BULLSEYE);
2086 }
2087 else
2088 {
2089 // can't drop here
2090 SetCursor(wxCURSOR_NO_ENTRY);
2091 }
2092}
2093
941830cb 2094void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 2095{
941830cb 2096 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2097
941830cb 2098 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2099
1044a386 2100 wxClientDC dc(this);
91b8de8d
RR
2101 PrepareDC( dc );
2102 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 2103 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 2104
3dbeaa52
VZ
2105 int w = i->GetWidth() + 2;
2106 int h = GetLineHeight(i) + 2;
91b8de8d 2107
3dbeaa52 2108 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
2109}
2110
941830cb 2111void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 2112{
941830cb 2113 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2114
941830cb 2115 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2116
1044a386 2117 wxClientDC dc(this);
91b8de8d
RR
2118 PrepareDC( dc );
2119 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 2120
3dbeaa52
VZ
2121 int x = i->GetX(),
2122 y = i->GetY();
2123 if ( below )
2124 {
2125 y += GetLineHeight(i) - 1;
2126 }
91b8de8d 2127
3dbeaa52 2128 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
2129}
2130
f135ff73
VZ
2131// -----------------------------------------------------------------------------
2132// wxWindows callbacks
2133// -----------------------------------------------------------------------------
2134
941830cb 2135void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 2136{
0659e7ee
RR
2137 wxPaintDC dc(this);
2138 PrepareDC( dc );
29d87bba 2139
941830cb
JS
2140 if ( !m_anchor)
2141 return;
2142
eff869aa 2143 dc.SetFont( m_normalFont );
0659e7ee 2144 dc.SetPen( m_dottedPen );
f38374d0 2145
eff869aa 2146 // this is now done dynamically
91b8de8d
RR
2147 //if(GetImageList() == NULL)
2148 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2149
91b8de8d 2150 int y = 2;
0659e7ee 2151 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 2152}
c801d85f 2153
b771aa29 2154void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
c801d85f 2155{
0659e7ee 2156 m_hasFocus = TRUE;
978f38c2 2157
b771aa29
VZ
2158 RefreshSelected();
2159
2160 event.Skip();
edaa81ae 2161}
c801d85f 2162
b771aa29 2163void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
c801d85f 2164{
0659e7ee 2165 m_hasFocus = FALSE;
978f38c2 2166
b771aa29
VZ
2167 RefreshSelected();
2168
2169 event.Skip();
edaa81ae 2170}
c801d85f 2171
941830cb 2172void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
c801d85f 2173{
978f38c2 2174 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
06b466c7 2175 te.m_code = (int)event.KeyCode();
978f38c2 2176 te.SetEventObject( this );
68eb5f63
VZ
2177 if ( GetEventHandler()->ProcessEvent( te ) )
2178 {
2179 // intercepted by the user code
2180 return;
2181 }
435fe83e 2182
91b8de8d 2183 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
2184 {
2185 event.Skip();
2186 return;
2187 }
ef44a621 2188
06b466c7
VZ
2189 // how should the selection work for this event?
2190 bool is_multiple, extended_select, unselect_others;
2191 EventFlagsToSelType(GetWindowStyleFlag(),
2192 event.ShiftDown(),
2193 event.ControlDown(),
dfc6cd93
SB
2194 is_multiple, extended_select, unselect_others);
2195
2196 // + : Expand
2197 // - : Collaspe
f6bcfd97 2198 // * : Expand all/Collapse all
dfc6cd93
SB
2199 // ' ' | return : activate
2200 // up : go up (not last children!)
2201 // down : go down
2202 // left : go to parent
2203 // right : open if parent and go next
2204 // home : go to root
2205 // end : go to last item without opening parents
978f38c2
VZ
2206 switch (event.KeyCode())
2207 {
2208 case '+':
2209 case WXK_ADD:
2210 if (m_current->HasPlus() && !IsExpanded(m_current))
2211 {
2212 Expand(m_current);
2213 }
2214 break;
ef44a621 2215
f6bcfd97
BP
2216 case '*':
2217 case WXK_MULTIPLY:
2218 if ( !IsExpanded(m_current) )
2219 {
2220 // expand all
2221 ExpandAll(m_current);
2222 break;
2223 }
2224 //else: fall through to Collapse() it
2225
978f38c2
VZ
2226 case '-':
2227 case WXK_SUBTRACT:
2228 if (IsExpanded(m_current))
2229 {
2230 Collapse(m_current);
2231 }
2232 break;
ef44a621 2233
978f38c2
VZ
2234 case ' ':
2235 case WXK_RETURN:
2236 {
2237 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
40fab78b 2238 event.m_item = (long) m_current;
978f38c2
VZ
2239 event.m_code = 0;
2240 event.SetEventObject( this );
2241 GetEventHandler()->ProcessEvent( event );
2242 }
2243 break;
ef44a621 2244
618a5e38
RR
2245 // up goes to the previous sibling or to the last
2246 // of its children if it's expanded
978f38c2
VZ
2247 case WXK_UP:
2248 {
91b8de8d 2249 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
2250 if (!prev)
2251 {
91b8de8d 2252 prev = GetParent( m_key_current );
618a5e38
RR
2253 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2254 {
2255 break; // don't go to root if it is hidden
2256 }
c193b707
VZ
2257 if (prev)
2258 {
618a5e38 2259 long cookie = 0;
91b8de8d 2260 wxTreeItemId current = m_key_current;
618a5e38
RR
2261 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2262 if (current == GetFirstChild( prev, cookie ))
90e58684
RR
2263 {
2264 // otherwise we return to where we came from
88ac883a 2265 SelectItem( prev, unselect_others, extended_select );
941830cb 2266 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
c193b707 2267 EnsureVisible( prev );
90e58684 2268 break;
c193b707 2269 }
978f38c2
VZ
2270 }
2271 }
2272 if (prev)
2273 {
69a282d4 2274 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 2275 {
69a282d4
VZ
2276 wxTreeItemId child = GetLastChild(prev);
2277 if ( child )
2278 {
2279 prev = child;
2280 }
978f38c2 2281 }
69a282d4 2282
88ac883a 2283 SelectItem( prev, unselect_others, extended_select );
941830cb 2284 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
2285 EnsureVisible( prev );
2286 }
2287 }
2288 break;
ef44a621 2289
978f38c2
VZ
2290 // left arrow goes to the parent
2291 case WXK_LEFT:
2292 {
2293 wxTreeItemId prev = GetParent( m_current );
618a5e38
RR
2294 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2295 {
2296 // don't go to root if it is hidden
2297 prev = GetPrevSibling( m_current );
2298 }
978f38c2
VZ
2299 if (prev)
2300 {
2301 EnsureVisible( prev );
88ac883a 2302 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2303 }
2304 }
2305 break;
ef44a621 2306
978f38c2 2307 case WXK_RIGHT:
618a5e38
RR
2308 // this works the same as the down arrow except that we
2309 // also expand the item if it wasn't expanded yet
978f38c2
VZ
2310 Expand(m_current);
2311 // fall through
2312
2313 case WXK_DOWN:
ef44a621 2314 {
91b8de8d 2315 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
2316 {
2317 long cookie = 0;
91b8de8d 2318 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 2319 SelectItem( child, unselect_others, extended_select );
941830cb 2320 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
2321 EnsureVisible( child );
2322 }
2323 else
2324 {
91b8de8d 2325 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2326 if (!next)
978f38c2 2327 {
91b8de8d 2328 wxTreeItemId current = m_key_current;
978f38c2
VZ
2329 while (current && !next)
2330 {
2331 current = GetParent( current );
2332 if (current) next = GetNextSibling( current );
2333 }
2334 }
b62c3631 2335 if (next)
978f38c2 2336 {
88ac883a 2337 SelectItem( next, unselect_others, extended_select );
941830cb 2338 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
2339 EnsureVisible( next );
2340 }
2341 }
ef44a621 2342 }
978f38c2 2343 break;
ef44a621 2344
978f38c2
VZ
2345 // <End> selects the last visible tree item
2346 case WXK_END:
2347 {
2348 wxTreeItemId last = GetRootItem();
2349
2350 while ( last.IsOk() && IsExpanded(last) )
2351 {
2352 wxTreeItemId lastChild = GetLastChild(last);
2353
2354 // it may happen if the item was expanded but then all of
2355 // its children have been deleted - so IsExpanded() returned
2356 // TRUE, but GetLastChild() returned invalid item
2357 if ( !lastChild )
2358 break;
2359
2360 last = lastChild;
2361 }
2362
2363 if ( last.IsOk() )
2364 {
2365 EnsureVisible( last );
88ac883a 2366 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2367 }
2368 }
2369 break;
2370
2371 // <Home> selects the root item
2372 case WXK_HOME:
2373 {
2374 wxTreeItemId prev = GetRootItem();
618a5e38
RR
2375 if (!prev) break;
2376 if (HasFlag(wxTR_HIDE_ROOT))
978f38c2 2377 {
618a5e38
RR
2378 long dummy;
2379 prev = GetFirstChild(prev, dummy);
2380 if (!prev) break;
978f38c2 2381 }
618a5e38
RR
2382 EnsureVisible( prev );
2383 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2384 }
2385 break;
2386
2387 default:
2388 event.Skip();
2389 }
edaa81ae 2390}
c801d85f 2391
941830cb 2392wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2393{
618a5e38
RR
2394 // JACS: removed wxYieldIfNeeded() because it can cause the window
2395 // to be deleted from under us if a close window event is pending
dc6c62a9 2396
91b8de8d
RR
2397 int w, h;
2398 GetSize(&w, &h);
91b8de8d 2399 flags=0;
618a5e38
RR
2400 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2401 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2402 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2403 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2404 if (flags) return wxTreeItemId();
d3a9f4af 2405
618a5e38
RR
2406 if (m_anchor == NULL)
2407 {
2408 flags = wxTREE_HITTEST_NOWHERE;
2409 return wxTreeItemId();
2410 }
5716a1ab 2411
618a5e38
RR
2412 wxClientDC dc(this);
2413 PrepareDC(dc);
2414 wxCoord x = dc.DeviceToLogicalX( point.x );
2415 wxCoord y = dc.DeviceToLogicalY( point.y );
2416 wxGenericTreeItem *hit = m_anchor->HitTest(wxPoint(x, y),
2417 this,
2418 flags,
2419 0 );
2420 if (hit == NULL)
2421 {
2422 flags = wxTREE_HITTEST_NOWHERE;
2423 return wxTreeItemId();
2424 }
2425 return hit;
4f22cf8d
RR
2426}
2427
8fb3bfe2
JS
2428// get the bounding rectangle of the item (or of its label only)
2429bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2430 wxRect& rect,
33ac7e6f 2431 bool WXUNUSED(textOnly)) const
8fb3bfe2 2432{
601c163b 2433 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
8fb3bfe2
JS
2434
2435 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2436
2437 int startX, startY;
2438 GetViewStart(& startX, & startY);
2439
1ed01484 2440 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
c22886bd 2441 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
1ed01484 2442 rect.width = i->GetWidth();
c22886bd
RR
2443 //rect.height = i->GetHeight();
2444 rect.height = GetLineHeight(i);
8fb3bfe2
JS
2445
2446 return TRUE;
8fb3bfe2
JS
2447}
2448
e179bd65
RR
2449/* **** */
2450
941830cb 2451void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
e179bd65
RR
2452{
2453 if (!item.IsOk()) return;
2454
941830cb 2455 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
9dfbf520 2456
e179bd65 2457 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
40fab78b 2458 te.m_item = (long) m_currentEdit;
e179bd65
RR
2459 te.SetEventObject( this );
2460 GetEventHandler()->ProcessEvent( te );
2461
2462 if (!te.IsAllowed()) return;
8dc99046 2463
dc6c62a9
RR
2464 // We have to call this here because the label in
2465 // question might just have been added and no screen
2466 // update taken place.
cd66f45b 2467 if (m_dirty) wxYieldIfNeeded();
9dfbf520 2468
e179bd65
RR
2469 wxString s = m_currentEdit->GetText();
2470 int x = m_currentEdit->GetX();
2471 int y = m_currentEdit->GetY();
2472 int w = m_currentEdit->GetWidth();
2473 int h = m_currentEdit->GetHeight();
9dfbf520 2474
5f1ea0ee
RR
2475 int image_h = 0;
2476 int image_w = 0;
8dc99046
VZ
2477
2478 int image = m_currentEdit->GetCurrentImage();
2479 if ( image != NO_IMAGE )
5f1ea0ee 2480 {
2c8e4738
VZ
2481 if ( m_imageListNormal )
2482 {
2483 m_imageListNormal->GetSize( image, image_w, image_h );
2484 image_w += 4;
2485 }
6359fa0e 2486 else
2c8e4738
VZ
2487 {
2488 wxFAIL_MSG(_T("you must create an image list to use images!"));
2489 }
5f1ea0ee
RR
2490 }
2491 x += image_w;
2492 w -= image_w + 4; // I don't know why +4 is needed
e179bd65
RR
2493
2494 wxClientDC dc(this);
2495 PrepareDC( dc );
2496 x = dc.LogicalToDeviceX( x );
2497 y = dc.LogicalToDeviceY( y );
2498
6359fa0e
VZ
2499 wxTreeTextCtrl *text = new wxTreeTextCtrl(this, -1,
2500 &m_renameAccept,
2501 &m_renameRes,
2502 this,
2503 s,
2504 wxPoint(x-4,y-4),
2505 wxSize(w+11,h+8));
e179bd65
RR
2506 text->SetFocus();
2507}
2508
941830cb 2509void wxGenericTreeCtrl::OnRenameTimer()
e179bd65
RR
2510{
2511 Edit( m_current );
2512}
2513
941830cb 2514void wxGenericTreeCtrl::OnRenameAccept()
e179bd65 2515{
618a5e38 2516 // TODO if the validator fails this causes a crash
e179bd65 2517 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
40fab78b 2518 le.m_item = (long) m_currentEdit;
e179bd65
RR
2519 le.SetEventObject( this );
2520 le.m_label = m_renameRes;
2521 GetEventHandler()->ProcessEvent( le );
9dfbf520 2522
e179bd65 2523 if (!le.IsAllowed()) return;
9dfbf520 2524
5f1ea0ee 2525 SetItemText( m_currentEdit, m_renameRes );
e179bd65 2526}
9dfbf520 2527
941830cb 2528void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2529{
bbe0af5b 2530 if ( !m_anchor ) return;
978f38c2 2531
3dbeaa52
VZ
2532 // we process left mouse up event (enables in-place edit), right down
2533 // (pass to the user code), left dbl click (activate item) and
2534 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
2535 if ( !(event.LeftDown() ||
2536 event.LeftUp() ||
3dbeaa52
VZ
2537 event.RightDown() ||
2538 event.LeftDClick() ||
2539 event.Dragging() ||
2540 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2541 {
2542 event.Skip();
2543
2544 return;
2545 }
2546
bbe0af5b
RR
2547 wxClientDC dc(this);
2548 PrepareDC(dc);
06b466c7
VZ
2549 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2550 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
29d87bba 2551
3dbeaa52 2552 int flags = 0;
618a5e38
RR
2553 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y),
2554 this,
2555 flags,
2556 0 );
3dbeaa52 2557
3dbeaa52 2558 if ( event.Dragging() && !m_isDragging )
bbe0af5b 2559 {
fd9811b1 2560 if (m_dragCount == 0)
8dc99046
VZ
2561 m_dragStart = wxPoint(x,y);
2562
fd9811b1 2563 m_dragCount++;
8dc99046 2564
3dbeaa52
VZ
2565 if (m_dragCount != 3)
2566 {
2567 // wait until user drags a bit further...
2568 return;
2569 }
8dc99046 2570
3dbeaa52
VZ
2571 wxEventType command = event.RightIsDown()
2572 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2573 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 2574
fd9811b1 2575 wxTreeEvent nevent( command, GetId() );
40fab78b 2576 nevent.m_item = (long) m_current;
fd9811b1 2577 nevent.SetEventObject(this);
3dbeaa52
VZ
2578
2579 // by default the dragging is not supported, the user code must
2580 // explicitly allow the event for it to take place
2581 nevent.Veto();
2582
2583 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2584 {
2585 // we're going to drag this item
2586 m_isDragging = TRUE;
2587
b3a7510d
VZ
2588 // remember the old cursor because we will change it while
2589 // dragging
2590 m_oldCursor = m_cursor;
2591
2592 // in a single selection control, hide the selection temporarily
2593 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2594 {
941830cb 2595 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
2596
2597 if ( m_oldSelection )
2598 {
2599 m_oldSelection->SetHilight(FALSE);
2600 RefreshLine(m_oldSelection);
2601 }
2602 }
2603
3dbeaa52
VZ
2604 CaptureMouse();
2605 }
bbe0af5b 2606 }
3dbeaa52 2607 else if ( event.Moving() )
fd9811b1 2608 {
3dbeaa52
VZ
2609 if ( item != m_dropTarget )
2610 {
2611 // unhighlight the previous drop target
2612 DrawDropEffect(m_dropTarget);
fd9811b1 2613
3dbeaa52 2614 m_dropTarget = item;
978f38c2 2615
3dbeaa52
VZ
2616 // highlight the current drop target if any
2617 DrawDropEffect(m_dropTarget);
fb882e1c 2618
cd66f45b 2619 wxYieldIfNeeded();
3dbeaa52 2620 }
e179bd65 2621 }
3dbeaa52
VZ
2622 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2623 {
2624 // erase the highlighting
2625 DrawDropEffect(m_dropTarget);
9dfbf520 2626
4f5fffcc
RR
2627 if ( m_oldSelection )
2628 {
2629 m_oldSelection->SetHilight(TRUE);
2630 RefreshLine(m_oldSelection);
2631 m_oldSelection = (wxGenericTreeItem *)NULL;
2632 }
2633
3dbeaa52
VZ
2634 // generate the drag end event
2635 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 2636
40fab78b 2637 event.m_item = (long) item;
3dbeaa52
VZ
2638 event.m_pointDrag = wxPoint(x, y);
2639 event.SetEventObject(this);
91b8de8d 2640
3dbeaa52 2641 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 2642
3dbeaa52
VZ
2643 m_isDragging = FALSE;
2644 m_dropTarget = (wxGenericTreeItem *)NULL;
2645
2646 ReleaseMouse();
2647
b3a7510d 2648 SetCursor(m_oldCursor);
3dbeaa52 2649
cd66f45b 2650 wxYieldIfNeeded();
3dbeaa52
VZ
2651 }
2652 else
bbe0af5b 2653 {
3dbeaa52
VZ
2654 // here we process only the messages which happen on tree items
2655
2656 m_dragCount = 0;
2657
2658 if (item == NULL) return; /* we hit the blank area */
2659
2660 if ( event.RightDown() )
2661 {
2662 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
40fab78b 2663 nevent.m_item = (long) item;
3dbeaa52 2664 nevent.m_code = 0;
05a7f61d
VZ
2665 CalcScrolledPosition(x, y,
2666 &nevent.m_pointDrag.x,
2667 &nevent.m_pointDrag.y);
3dbeaa52
VZ
2668 nevent.SetEventObject(this);
2669 GetEventHandler()->ProcessEvent(nevent);
2670 }
80d2803f 2671 else if ( event.LeftUp() )
f6bcfd97 2672 {
80d2803f 2673 if ( m_lastOnSame )
f6bcfd97 2674 {
80d2803f
VZ
2675 if ( (item == m_current) &&
2676 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2677 HasFlag(wxTR_EDIT_LABELS) )
2678 {
bdb310a7
VZ
2679 if ( m_renameTimer->IsRunning() )
2680 m_renameTimer->Stop();
2681
80d2803f
VZ
2682 m_renameTimer->Start( 100, TRUE );
2683 }
f6bcfd97 2684
80d2803f
VZ
2685 m_lastOnSame = FALSE;
2686 }
3dbeaa52 2687 }
0326c494 2688 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3dbeaa52 2689 {
f6bcfd97
BP
2690 if ( event.LeftDown() )
2691 {
2692 m_lastOnSame = item == m_current;
2693 }
2694
0326c494
VZ
2695 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
2696 {
2697 // only toggle the item for a single click, double click on
2698 // the button doesn't do anything (it toggles the item twice)
2699 if ( event.LeftDown() )
2700 {
2701 Toggle( item );
2702 }
2703
2704 // don't select the item if the button was clicked
2705 return;
2706 }
2707
3dbeaa52
VZ
2708 // how should the selection work for this event?
2709 bool is_multiple, extended_select, unselect_others;
2710 EventFlagsToSelType(GetWindowStyleFlag(),
2711 event.ShiftDown(),
2712 event.ControlDown(),
dfc6cd93 2713 is_multiple, extended_select, unselect_others);
3dbeaa52 2714
3dbeaa52
VZ
2715 SelectItem(item, unselect_others, extended_select);
2716
618a5e38
RR
2717 // For some reason, Windows isn't recognizing a left double-click,
2718 // so we need to simulate it here. Allow 200 milliseconds for now.
3dbeaa52
VZ
2719 if ( event.LeftDClick() )
2720 {
f6bcfd97
BP
2721 // double clicking should not start editing the item label
2722 m_renameTimer->Stop();
2723 m_lastOnSame = FALSE;
2724
ebb987b7
VZ
2725 // send activate event first
2726 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2727 nevent.m_item = (long) item;
2728 nevent.m_code = 0;
2729 CalcScrolledPosition(x, y,
2730 &nevent.m_pointDrag.x,
2731 &nevent.m_pointDrag.y);
2732 nevent.SetEventObject( this );
2733 if ( !GetEventHandler()->ProcessEvent( nevent ) )
618a5e38 2734 {
ebb987b7
VZ
2735 // if the user code didn't process the activate event,
2736 // handle it ourselves by toggling the item when it is
2737 // double clicked
2738 if ( item->HasPlus() )
2739 {
2740 Toggle(item);
2741 }
618a5e38 2742 }
3dbeaa52
VZ
2743 }
2744 }
bbe0af5b 2745 }
edaa81ae 2746}
c801d85f 2747
941830cb 2748void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
3db7be80 2749{
bbe0af5b
RR
2750 /* after all changes have been done to the tree control,
2751 * we actually redraw the tree when everything is over */
ef44a621 2752
618a5e38 2753 if (!m_dirty) return;
ef44a621 2754
bbe0af5b 2755 m_dirty = FALSE;
3db7be80 2756
bbe0af5b 2757 CalculatePositions();
91b8de8d 2758 Refresh();
bbe0af5b 2759 AdjustMyScrollbars();
3db7be80
RR
2760}
2761
941830cb 2762void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 2763{
e06b9569
JS
2764 wxCoord text_w = 0;
2765 wxCoord text_h = 0;
9dfbf520 2766
a62867a5 2767 if (item->IsBold())
eff869aa 2768 dc.SetFont(m_boldFont);
9dfbf520 2769
91b8de8d 2770 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 2771 text_h+=2;
91b8de8d 2772
eff869aa
RR
2773 // restore normal font
2774 dc.SetFont( m_normalFont );
9dfbf520 2775
91b8de8d
RR
2776 int image_h = 0;
2777 int image_w = 0;
8dc99046
VZ
2778 int image = item->GetCurrentImage();
2779 if ( image != NO_IMAGE )
91b8de8d 2780 {
2c8e4738
VZ
2781 if ( m_imageListNormal )
2782 {
2783 m_imageListNormal->GetSize( image, image_w, image_h );
2784 image_w += 4;
2785 }
91b8de8d
RR
2786 }
2787
2788 int total_h = (image_h > text_h) ? image_h : text_h;
2789
618a5e38 2790 if (total_h < 30)
f2593d0d 2791 total_h += 2; // at least 2 pixels
06b466c7 2792 else
f2593d0d 2793 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
2794
2795 item->SetHeight(total_h);
6cedba09
VZ
2796 if (total_h>m_lineHeight)
2797 m_lineHeight=total_h;
bbe0af5b 2798
91b8de8d
RR
2799 item->SetWidth(image_w+text_w+2);
2800}
2801
2802// -----------------------------------------------------------------------------
2803// for developper : y is now the top of the level
2804// not the middle of it !
941830cb 2805void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2806{
618a5e38
RR
2807 int x = level*m_indent;
2808 if (!HasFlag(wxTR_HIDE_ROOT))
2809 {
2810 x += m_indent;
2811 }
2812 else if (level == 0)
2813 {
2814 // a hidden root is not evaluated, but its
2815 // children are always calculated
2816 goto Recurse;
2817 }
389cdc7a 2818
91b8de8d 2819 CalculateSize( item, dc );
d3a9f4af 2820
91b8de8d 2821 // set its position
618a5e38 2822 item->SetX( x+m_spacing );
91b8de8d 2823 item->SetY( y );
618a5e38 2824 y += GetLineHeight(item);
4c681997 2825
bbe0af5b
RR
2826 if ( !item->IsExpanded() )
2827 {
618a5e38 2828 // we don't need to calculate collapsed branches
bbe0af5b
RR
2829 return;
2830 }
389cdc7a 2831
618a5e38 2832 Recurse:
91b8de8d
RR
2833 wxArrayGenericTreeItems& children = item->GetChildren();
2834 size_t n, count = children.Count();
618a5e38 2835 ++level;
91b8de8d 2836 for (n = 0; n < count; ++n )
618a5e38 2837 CalculateLevel( children[n], dc, level, y ); // recurse
edaa81ae 2838}
c801d85f 2839
941830cb 2840void wxGenericTreeCtrl::CalculatePositions()
c801d85f 2841{
bbe0af5b 2842 if ( !m_anchor ) return;
29d87bba 2843
bbe0af5b
RR
2844 wxClientDC dc(this);
2845 PrepareDC( dc );
29d87bba 2846
eff869aa 2847 dc.SetFont( m_normalFont );
29d87bba 2848
bbe0af5b 2849 dc.SetPen( m_dottedPen );
91b8de8d
RR
2850 //if(GetImageList() == NULL)
2851 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2852
8dc99046 2853 int y = 2;
f65635b5 2854 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 2855}
c801d85f 2856
941830cb 2857void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 2858{
e6527f9d
RR
2859 if (m_dirty) return;
2860
bbe0af5b
RR
2861 wxClientDC dc(this);
2862 PrepareDC(dc);
4832f7c0 2863
bbe0af5b
RR
2864 int cw = 0;
2865 int ch = 0;
2866 GetClientSize( &cw, &ch );
4832f7c0 2867
bbe0af5b
RR
2868 wxRect rect;
2869 rect.x = dc.LogicalToDeviceX( 0 );
2870 rect.width = cw;
2871 rect.y = dc.LogicalToDeviceY( item->GetY() );
2872 rect.height = ch;
f135ff73 2873
bbe0af5b 2874 Refresh( TRUE, &rect );
f135ff73 2875
bbe0af5b 2876 AdjustMyScrollbars();
edaa81ae 2877}
c801d85f 2878
941830cb 2879void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 2880{
e6527f9d
RR
2881 if (m_dirty) return;
2882
bbe0af5b
RR
2883 wxClientDC dc(this);
2884 PrepareDC( dc );
2885
5391f772
SB
2886 int cw = 0;
2887 int ch = 0;
2888 GetClientSize( &cw, &ch );
2889
bbe0af5b 2890 wxRect rect;
5391f772
SB
2891 rect.x = dc.LogicalToDeviceX( 0 );
2892 rect.y = dc.LogicalToDeviceY( item->GetY() );
2893 rect.width = cw;
91b8de8d 2894 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 2895
bbe0af5b 2896 Refresh( TRUE, &rect );
edaa81ae 2897}
c801d85f 2898
b771aa29
VZ
2899void wxGenericTreeCtrl::RefreshSelected()
2900{
2901 // TODO: this is awfully inefficient, we should keep the list of all
2902 // selected items internally, should be much faster
2903 if ( m_anchor )
2904 RefreshSelectedUnder(m_anchor);
2905}
2906
2907void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
2908{
2909 if ( item->IsSelected() )
2910 RefreshLine(item);
2911
2912 const wxArrayGenericTreeItems& children = item->GetChildren();
2913 size_t count = children.GetCount();
2914 for ( size_t n = 0; n < count; n++ )
2915 {
2916 RefreshSelectedUnder(children[n]);
2917 }
2918}
2919
7009f411
VZ
2920// ----------------------------------------------------------------------------
2921// changing colours: we need to refresh the tree control
2922// ----------------------------------------------------------------------------
2923
2924bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
2925{
2926 if ( !wxWindow::SetBackgroundColour(colour) )
2927 return FALSE;
2928
2929 Refresh();
2930
2931 return TRUE;
2932}
2933
0800a4ba 2934bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
7009f411
VZ
2935{
2936 if ( !wxWindow::SetForegroundColour(colour) )
2937 return FALSE;
2938
2939 Refresh();
2940
2941 return TRUE;
2942}
2943
1e6feb95 2944#endif // wxUSE_TREECTRL