]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
removed extra wxSTD
[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
1035wxTreeItemId wxGenericTreeCtrl::GetPrev(const wxTreeItemId& item) const
1036{
618a5e38 1037 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1ed01484 1038
618a5e38 1039 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1040
618a5e38 1041 return wxTreeItemId();
f135ff73 1042}
29d87bba 1043
1ed01484
JS
1044wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1045{
618a5e38
RR
1046 wxTreeItemId id = GetRootItem();
1047 if (!id.IsOk())
1ed01484 1048 return id;
1ed01484 1049
618a5e38
RR
1050 do
1051 {
1052 if (IsVisible(id))
1053 return id;
1054 id = GetNext(id);
1055 } while (id.IsOk());
1056
1057 return wxTreeItemId();
1ed01484
JS
1058}
1059
941830cb 1060wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
f135ff73 1061{
618a5e38 1062 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1063
618a5e38
RR
1064 wxTreeItemId id = item;
1065 if (id.IsOk())
1066 {
1067 while (id = GetNext(id), id.IsOk())
1068 {
1069 if (IsVisible(id))
1070 return id;
1071 }
1072 }
1073 return wxTreeItemId();
f135ff73 1074}
29d87bba 1075
941830cb 1076wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
f135ff73 1077{
618a5e38 1078 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1079
618a5e38 1080 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1081
618a5e38 1082 return wxTreeItemId();
edaa81ae 1083}
c801d85f 1084
f135ff73
VZ
1085// -----------------------------------------------------------------------------
1086// operations
1087// -----------------------------------------------------------------------------
1088
941830cb 1089wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1090 size_t previous,
1091 const wxString& text,
1092 int image, int selImage,
1093 wxTreeItemData *data)
c801d85f 1094{
941830cb 1095 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f49f2b0c
RR
1096 if ( !parent )
1097 {
1098 // should we give a warning here?
1099 return AddRoot(text, image, selImage, data);
1100 }
4832f7c0 1101
618a5e38
RR
1102 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1103
f49f2b0c 1104 wxClientDC dc(this);
f38374d0 1105 wxGenericTreeItem *item =
f49f2b0c 1106 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
74bedbeb 1107
f49f2b0c
RR
1108 if ( data != NULL )
1109 {
40fab78b 1110 data->m_pItem = (long) item;
f49f2b0c 1111 }
74bedbeb 1112
f49f2b0c 1113 parent->Insert( item, previous );
ef44a621 1114
f49f2b0c 1115 return item;
4c681997
RR
1116}
1117
941830cb 1118wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
f135ff73
VZ
1119 int image, int selImage,
1120 wxTreeItemData *data)
4c681997 1121{
f49f2b0c 1122 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 1123
618a5e38
RR
1124 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1125
f49f2b0c
RR
1126 wxClientDC dc(this);
1127 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
f135ff73 1128 image, selImage, data);
618a5e38
RR
1129 if (HasFlag(wxTR_HIDE_ROOT))
1130 {
1131 // if root is hidden, make sure we can navigate
1132 // into children
1133 m_anchor->SetHasPlus();
1134 Expand(m_anchor);
1135 }
f49f2b0c
RR
1136 if ( data != NULL )
1137 {
40fab78b 1138 data->m_pItem = (long) m_anchor;
f49f2b0c 1139 }
f38374d0 1140
f49f2b0c
RR
1141 if (!HasFlag(wxTR_MULTIPLE))
1142 {
1143 m_current = m_key_current = m_anchor;
06b466c7 1144 m_current->SetHilight( TRUE );
f49f2b0c 1145 }
389cdc7a 1146
f49f2b0c 1147 return m_anchor;
edaa81ae 1148}
c801d85f 1149
941830cb 1150wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
f135ff73
VZ
1151 const wxString& text,
1152 int image, int selImage,
1153 wxTreeItemData *data)
c801d85f 1154{
f2593d0d 1155 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 1156}
c801d85f 1157
941830cb 1158wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1159 const wxTreeItemId& idPrevious,
1160 const wxString& text,
1161 int image, int selImage,
1162 wxTreeItemData *data)
c801d85f 1163{
941830cb 1164 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1165 if ( !parent )
1166 {
1167 // should we give a warning here?
1168 return AddRoot(text, image, selImage, data);
1169 }
c801d85f 1170
941830cb 1171 int index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
f2593d0d 1172 wxASSERT_MSG( index != wxNOT_FOUND,
941830cb 1173 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
06b466c7 1174
f2593d0d
RR
1175 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1176}
1177
941830cb 1178wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f2593d0d
RR
1179 size_t before,
1180 const wxString& text,
1181 int image, int selImage,
1182 wxTreeItemData *data)
1183{
941830cb 1184 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1185 if ( !parent )
1186 {
1187 // should we give a warning here?
1188 return AddRoot(text, image, selImage, data);
1189 }
1190
1191 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 1192}
c801d85f 1193
941830cb 1194wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
f135ff73
VZ
1195 const wxString& text,
1196 int image, int selImage,
1197 wxTreeItemData *data)
74bedbeb 1198{
941830cb 1199 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1200 if ( !parent )
1201 {
1202 // should we give a warning here?
1203 return AddRoot(text, image, selImage, data);
1204 }
f135ff73 1205
f2593d0d
RR
1206 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1207 image, selImage, data);
74bedbeb
VZ
1208}
1209
941830cb 1210void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
a43a4f9d 1211{
f2593d0d 1212 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
40fab78b 1213 event.m_item = (long) item;
f2593d0d
RR
1214 event.SetEventObject( this );
1215 ProcessEvent( event );
a43a4f9d
VZ
1216}
1217
941830cb 1218void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
372edb9d 1219{
618a5e38
RR
1220 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1221
941830cb 1222 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
a43a4f9d 1223 item->DeleteChildren(this);
372edb9d
VZ
1224}
1225
941830cb 1226void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1227{
618a5e38
RR
1228 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1229
941830cb 1230 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
ff5bf259 1231
618a5e38
RR
1232 // don't stay with invalid m_key_current or we will crash in
1233 // the next call to OnChar()
aaa2f297
VZ
1234 bool changeKeyCurrent = FALSE;
1235 wxGenericTreeItem *itemKey = m_key_current;
618a5e38 1236 while ( itemKey )
aaa2f297
VZ
1237 {
1238 if ( itemKey == item )
1239 {
1240 // m_key_current is a descendant of the item being deleted
1241 changeKeyCurrent = TRUE;
618a5e38 1242 break;
aaa2f297 1243 }
618a5e38 1244 itemKey = itemKey->GetParent();
aaa2f297
VZ
1245 }
1246
1247 wxGenericTreeItem *parent = item->GetParent();
97d7bfb8
RR
1248 if ( parent )
1249 {
1250 parent->GetChildren().Remove( item ); // remove by value
1251 }
f135ff73 1252
aaa2f297
VZ
1253 if ( changeKeyCurrent )
1254 {
1255 // may be NULL or not
1256 m_key_current = parent;
1257 }
1258
97d7bfb8
RR
1259 item->DeleteChildren(this);
1260 SendDeleteEvent(item);
1261 delete item;
edaa81ae 1262}
c801d85f 1263
941830cb 1264void wxGenericTreeCtrl::DeleteAllItems()
c801d85f 1265{
97d7bfb8
RR
1266 if ( m_anchor )
1267 {
618a5e38
RR
1268 m_dirty = TRUE;
1269
97d7bfb8
RR
1270 m_anchor->DeleteChildren(this);
1271 delete m_anchor;
a43a4f9d 1272
97d7bfb8 1273 m_anchor = NULL;
97d7bfb8 1274 }
edaa81ae
RR
1275}
1276
941830cb 1277void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1278{
941830cb 1279 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1280
941830cb 1281 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
f6bcfd97 1282
f2593d0d
RR
1283 if ( !item->HasPlus() )
1284 return;
978f38c2 1285
f2593d0d
RR
1286 if ( item->IsExpanded() )
1287 return;
f135ff73 1288
f2593d0d 1289 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
40fab78b 1290 event.m_item = (long) item;
f2593d0d 1291 event.SetEventObject( this );
004fd0c8 1292
f2593d0d
RR
1293 if ( ProcessEvent( event ) && !event.IsAllowed() )
1294 {
1295 // cancelled by program
1296 return;
1297 }
4832f7c0 1298
f2593d0d
RR
1299 item->Expand();
1300 CalculatePositions();
f135ff73 1301
f2593d0d 1302 RefreshSubtree(item);
f135ff73 1303
f2593d0d
RR
1304 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1305 ProcessEvent( event );
edaa81ae
RR
1306}
1307
941830cb 1308void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
f6bcfd97
BP
1309{
1310 Expand(item);
1311 if ( IsExpanded(item) )
1312 {
1313 long cookie;
1314 wxTreeItemId child = GetFirstChild(item, cookie);
1315 while ( child.IsOk() )
1316 {
1317 ExpandAll(child);
1318
1319 child = GetNextChild(item, cookie);
1320 }
1321 }
1322}
1323
941830cb 1324void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1325{
941830cb 1326 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1327
f2593d0d
RR
1328 if ( !item->IsExpanded() )
1329 return;
f135ff73 1330
f2593d0d 1331 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
40fab78b 1332 event.m_item = (long) item;
f2593d0d
RR
1333 event.SetEventObject( this );
1334 if ( ProcessEvent( event ) && !event.IsAllowed() )
1335 {
1336 // cancelled by program
1337 return;
1338 }
4832f7c0 1339
f2593d0d 1340 item->Collapse();
f135ff73 1341
618a5e38 1342#if 0 // TODO why should items be collapsed recursively?
f2593d0d
RR
1343 wxArrayGenericTreeItems& children = item->GetChildren();
1344 size_t count = children.Count();
1345 for ( size_t n = 0; n < count; n++ )
1346 {
1347 Collapse(children[n]);
1348 }
618a5e38 1349#endif
f135ff73 1350
f2593d0d 1351 CalculatePositions();
f135ff73 1352
f2593d0d 1353 RefreshSubtree(item);
f135ff73 1354
f2593d0d
RR
1355 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1356 ProcessEvent( event );
edaa81ae 1357}
c801d85f 1358
941830cb 1359void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1360{
00e12320
RR
1361 Collapse(item);
1362 DeleteChildren(item);
edaa81ae 1363}
c801d85f 1364
941830cb 1365void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1366{
941830cb 1367 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
389cdc7a 1368
00e12320
RR
1369 if (item->IsExpanded())
1370 Collapse(itemId);
1371 else
1372 Expand(itemId);
f135ff73 1373}
389cdc7a 1374
941830cb 1375void wxGenericTreeCtrl::Unselect()
f135ff73 1376{
00e12320
RR
1377 if (m_current)
1378 {
1379 m_current->SetHilight( FALSE );
1380 RefreshLine( m_current );
1381 }
edaa81ae 1382}
c801d85f 1383
941830cb 1384void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1385{
00e12320
RR
1386 if (item->IsSelected())
1387 {
1388 item->SetHilight(FALSE);
1389 RefreshLine(item);
1390 }
d3a9f4af 1391
00e12320 1392 if (item->HasChildren())
88ac883a 1393 {
00e12320
RR
1394 wxArrayGenericTreeItems& children = item->GetChildren();
1395 size_t count = children.Count();
1396 for ( size_t n = 0; n < count; ++n )
1397 {
1398 UnselectAllChildren(children[n]);
1399 }
88ac883a
VZ
1400 }
1401}
f135ff73 1402
941830cb 1403void wxGenericTreeCtrl::UnselectAll()
88ac883a 1404{
941830cb 1405 UnselectAllChildren((wxGenericTreeItem*) GetRootItem().m_pItem);
88ac883a
VZ
1406}
1407
1408// Recursive function !
1409// To stop we must have crt_item<last_item
91b8de8d 1410// Algorithm :
88ac883a 1411// Tag all next children, when no more children,
d3a9f4af 1412// Move to parent (not to tag)
91b8de8d 1413// Keep going... if we found last_item, we stop.
941830cb 1414bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a
VZ
1415{
1416 wxGenericTreeItem *parent = crt_item->GetParent();
1417
00e12320
RR
1418 if (parent == NULL) // This is root item
1419 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1420
91b8de8d 1421 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1422 int index = children.Index(crt_item);
1423 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1424
1425 size_t count = children.Count();
1426 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1427 {
1428 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1429 }
88ac883a
VZ
1430
1431 return TagNextChildren(parent, last_item, select);
1432}
1433
941830cb 1434bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a 1435{
00e12320
RR
1436 crt_item->SetHilight(select);
1437 RefreshLine(crt_item);
d3a9f4af 1438
06b466c7 1439 if (crt_item==last_item)
00e12320 1440 return TRUE;
88ac883a 1441
00e12320 1442 if (crt_item->HasChildren())
88ac883a 1443 {
00e12320
RR
1444 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1445 size_t count = children.Count();
1446 for ( size_t n = 0; n < count; ++n )
1447 {
06b466c7 1448 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1449 return TRUE;
06b466c7 1450 }
88ac883a 1451 }
d3a9f4af 1452
c0de7af4 1453 return FALSE;
88ac883a
VZ
1454}
1455
941830cb 1456void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
88ac883a 1457{
f2593d0d
RR
1458 // item2 is not necessary after item1
1459 wxGenericTreeItem *first=NULL, *last=NULL;
88ac883a 1460
f2593d0d
RR
1461 // choice first' and 'last' between item1 and item2
1462 if (item1->GetY()<item2->GetY())
06b466c7 1463 {
f2593d0d
RR
1464 first=item1;
1465 last=item2;
1466 }
1467 else
1468 {
1469 first=item2;
1470 last=item1;
1471 }
88ac883a 1472
f2593d0d 1473 bool select = m_current->IsSelected();
d3a9f4af 1474
f2593d0d
RR
1475 if ( TagAllChildrenUntilLast(first,last,select) )
1476 return;
88ac883a 1477
f2593d0d 1478 TagNextChildren(first,last,select);
88ac883a
VZ
1479}
1480
941830cb 1481void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
c193b707
VZ
1482 bool unselect_others,
1483 bool extended_select)
d3a9f4af 1484{
223d09f6 1485 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1486
88ac883a 1487 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
941830cb 1488 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
88ac883a
VZ
1489
1490 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1491 // wxT("this is a single selection tree") );
88ac883a
VZ
1492
1493 // to keep going anyhow !!!
d3a9f4af 1494 if (is_single)
8dc99046
VZ
1495 {
1496 if (item->IsSelected())
1497 return; // nothing to do
1498 unselect_others = TRUE;
1499 extended_select = FALSE;
1500 }
1501 else if ( unselect_others && item->IsSelected() )
1502 {
1503 // selection change if there is more than one item currently selected
1504 wxArrayTreeItemIds selected_items;
1505 if ( GetSelections(selected_items) == 1 )
1506 return;
1507 }
d3a9f4af 1508
f135ff73 1509 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
40fab78b
JS
1510 event.m_item = (long) item;
1511 event.m_itemOld = (long) m_current;
f135ff73 1512 event.SetEventObject( this );
91b8de8d 1513 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1514
f98e2558 1515 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
618a5e38 1516 return;
f135ff73 1517
2cc78389
RR
1518 wxTreeItemId parent = GetParent( itemId );
1519 while (parent.IsOk())
1520 {
1521 if (!IsExpanded(parent))
1522 Expand( parent );
cdb3cffe 1523
2cc78389
RR
1524 parent = GetParent( parent );
1525 }
cdb3cffe 1526
2cc78389 1527 EnsureVisible( itemId );
cdb3cffe 1528
88ac883a
VZ
1529 // ctrl press
1530 if (unselect_others)
389cdc7a 1531 {
88ac883a 1532 if (is_single) Unselect(); // to speed up thing
c193b707 1533 else UnselectAll();
edaa81ae 1534 }
f135ff73 1535
88ac883a 1536 // shift press
d3a9f4af 1537 if (extended_select)
88ac883a 1538 {
aaa2f297
VZ
1539 if ( !m_current )
1540 {
618a5e38 1541 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
1542 }
1543
88ac883a
VZ
1544 // don't change the mark (m_current)
1545 SelectItemRange(m_current, item);
1546 }
1547 else
1548 {
c0de7af4 1549 bool select=TRUE; // the default
88ac883a 1550
c193b707
VZ
1551 // Check if we need to toggle hilight (ctrl mode)
1552 if (!unselect_others)
618a5e38 1553 select=!item->IsSelected();
88ac883a 1554
91b8de8d 1555 m_current = m_key_current = item;
c193b707
VZ
1556 m_current->SetHilight(select);
1557 RefreshLine( m_current );
88ac883a 1558 }
389cdc7a 1559
f135ff73 1560 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1561 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1562}
1563
941830cb 1564void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
9dfbf520 1565 wxArrayTreeItemIds &array) const
c801d85f 1566{
8dc99046 1567 if ( item->IsSelected() )
9dfbf520 1568 array.Add(wxTreeItemId(item));
91b8de8d 1569
9dfbf520 1570 if ( item->HasChildren() )
91b8de8d 1571 {
9dfbf520
VZ
1572 wxArrayGenericTreeItems& children = item->GetChildren();
1573 size_t count = children.GetCount();
1574 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1575 FillArray(children[n], array);
91b8de8d
RR
1576 }
1577}
1578
941830cb 1579size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d 1580{
618a5e38
RR
1581 array.Empty();
1582 wxTreeItemId idRoot = GetRootItem();
1583 if ( idRoot.IsOk() )
1584 {
1585 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1586 }
1587 //else: the tree is empty, so no selections
91b8de8d 1588
618a5e38 1589 return array.Count();
91b8de8d
RR
1590}
1591
941830cb 1592void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1593{
91b8de8d
RR
1594 if (!item.IsOk()) return;
1595
941830cb 1596 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 1597
f65635b5
VZ
1598 // first expand all parent branches
1599 wxGenericTreeItem *parent = gitem->GetParent();
5391f772 1600 while ( parent )
f65635b5 1601 {
8dc99046 1602 Expand(parent);
f65635b5
VZ
1603 parent = parent->GetParent();
1604 }
1605
5391f772 1606 //if (parent) CalculatePositions();
91b8de8d
RR
1607
1608 ScrollTo(item);
1609}
1610
941830cb 1611void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d
RR
1612{
1613 if (!item.IsOk()) return;
1614
dc6c62a9
RR
1615 // We have to call this here because the label in
1616 // question might just have been added and no screen
1617 // update taken place.
cd66f45b 1618 if (m_dirty) wxYieldIfNeeded();
dc6c62a9 1619
941830cb 1620 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1621
f65635b5 1622 // now scroll to the item
0659e7ee 1623 int item_y = gitem->GetY();
8dc99046 1624
0659e7ee
RR
1625 int start_x = 0;
1626 int start_y = 0;
1e6feb95 1627 GetViewStart( &start_x, &start_y );
91b8de8d 1628 start_y *= PIXELS_PER_UNIT;
978f38c2 1629
a93109d5
RR
1630 int client_h = 0;
1631 int client_w = 0;
1632 GetClientSize( &client_w, &client_h );
ef44a621 1633
0659e7ee
RR
1634 if (item_y < start_y+3)
1635 {
91b8de8d 1636 // going down
0659e7ee
RR
1637 int x = 0;
1638 int y = 0;
91b8de8d
RR
1639 m_anchor->GetSize( x, y, this );
1640 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1641 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1642 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1643 // Item should appear at top
91b8de8d 1644 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1645 }
91b8de8d 1646 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1647 {
c7a9fa36
RR
1648 // going up
1649 int x = 0;
1650 int y = 0;
1651 m_anchor->GetSize( x, y, this );
1652 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1653 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1654 item_y += PIXELS_PER_UNIT+2;
1655 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1656 // Item should appear at bottom
c7a9fa36 1657 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 1658 }
edaa81ae 1659}
c801d85f 1660
e1ee62bd 1661// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 1662static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1663
004fd0c8 1664static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1665 wxGenericTreeItem **item2)
edaa81ae 1666{
941830cb 1667 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1668
1669 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1670}
1671
941830cb 1672int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
e1ee62bd 1673 const wxTreeItemId& item2)
0659e7ee 1674{
87138c52 1675 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1676}
1677
941830cb 1678void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 1679{
223d09f6 1680 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 1681
941830cb 1682 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 1683
e1ee62bd 1684 wxCHECK_RET( !s_treeBeingSorted,
941830cb 1685 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1686
91b8de8d 1687 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1688 if ( children.Count() > 1 )
1689 {
618a5e38
RR
1690 m_dirty = TRUE;
1691
e1ee62bd
VZ
1692 s_treeBeingSorted = this;
1693 children.Sort(tree_ctrl_compare_func);
1694 s_treeBeingSorted = NULL;
e1ee62bd
VZ
1695 }
1696 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1697}
1698
941830cb 1699wxImageList *wxGenericTreeCtrl::GetImageList() const
edaa81ae 1700{
f135ff73 1701 return m_imageListNormal;
edaa81ae
RR
1702}
1703
618a5e38
RR
1704wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
1705{
1706 return m_imageListButtons;
1707}
1708
941830cb 1709wxImageList *wxGenericTreeCtrl::GetStateImageList() const
c801d85f 1710{
f135ff73 1711 return m_imageListState;
edaa81ae 1712}
c801d85f 1713
618a5e38 1714void wxGenericTreeCtrl::CalculateLineHeight()
e2414cbe 1715{
f2593d0d
RR
1716 wxClientDC dc(this);
1717 m_lineHeight = (int)(dc.GetCharHeight() + 4);
06b466c7 1718
618a5e38
RR
1719 if ( m_imageListNormal )
1720 {
1721 // Calculate a m_lineHeight value from the normal Image sizes.
1722 // May be toggle off. Then wxGenericTreeCtrl will spread when
1723 // necessary (which might look ugly).
1724 int n = m_imageListNormal->GetImageCount();
1725 for (int i = 0; i < n ; i++)
1726 {
1727 int width = 0, height = 0;
1728 m_imageListNormal->GetSize(i, width, height);
1729 if (height > m_lineHeight) m_lineHeight = height;
1730 }
1731 }
1732
1733 if (m_imageListButtons)
f2593d0d 1734 {
618a5e38
RR
1735 // Calculate a m_lineHeight value from the Button image sizes.
1736 // May be toggle off. Then wxGenericTreeCtrl will spread when
1737 // necessary (which might look ugly).
1738 int n = m_imageListButtons->GetImageCount();
1739 for (int i = 0; i < n ; i++)
1740 {
1741 int width = 0, height = 0;
1742 m_imageListButtons->GetSize(i, width, height);
1743 if (height > m_lineHeight) m_lineHeight = height;
1744 }
f2593d0d 1745 }
91b8de8d 1746
618a5e38 1747 if (m_lineHeight < 30)
f2593d0d 1748 m_lineHeight += 2; // at least 2 pixels
06b466c7 1749 else
f2593d0d 1750 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 1751}
e2414cbe 1752
618a5e38
RR
1753void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
1754{
1755 if (m_ownsImageListNormal) delete m_imageListNormal;
1756 m_imageListNormal = imageList;
1757 m_ownsImageListNormal = FALSE;
1758 m_dirty = TRUE;
1759 CalculateLineHeight();
1760}
1761
941830cb 1762void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1763{
46cd520d 1764 if (m_ownsImageListState) delete m_imageListState;
f135ff73 1765 m_imageListState = imageList;
46cd520d
VS
1766 m_ownsImageListState = FALSE;
1767}
1768
618a5e38
RR
1769void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
1770{
1771 if (m_ownsImageListButtons) delete m_imageListButtons;
1772 m_imageListButtons = imageList;
1773 m_ownsImageListButtons = FALSE;
1774 m_dirty = TRUE;
1775 CalculateLineHeight();
1776}
1777
46cd520d
VS
1778void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
1779{
1780 SetImageList(imageList);
1781 m_ownsImageListNormal = TRUE;
1782}
1783
1784void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
1785{
1786 SetStateImageList(imageList);
1787 m_ownsImageListState = TRUE;
edaa81ae 1788}
e2414cbe 1789
618a5e38
RR
1790void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
1791{
1792 SetButtonsImageList(imageList);
1793 m_ownsImageListButtons = TRUE;
1794}
1795
f135ff73
VZ
1796// -----------------------------------------------------------------------------
1797// helpers
1798// -----------------------------------------------------------------------------
0659e7ee 1799
941830cb 1800void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 1801{
0659e7ee
RR
1802 if (m_anchor)
1803 {
618a5e38 1804 int x = 0, y = 0;
91b8de8d 1805 m_anchor->GetSize( x, y, this );
c193b707 1806 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1807 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1808 int x_pos = GetScrollPos( wxHORIZONTAL );
1809 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1810 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1811 }
1812 else
1813 {
1814 SetScrollbars( 0, 0, 0, 0 );
1815 }
edaa81ae 1816}
c801d85f 1817
941830cb 1818int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 1819{
dc6c62a9
RR
1820 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1821 return item->GetHeight();
1822 else
1823 return m_lineHeight;
91b8de8d
RR
1824}
1825
941830cb 1826void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 1827{
618a5e38
RR
1828 // TODO implement "state" icon on items
1829
9ec64fa7
VZ
1830 wxTreeItemAttr *attr = item->GetAttributes();
1831 if ( attr && attr->HasFont() )
1832 dc.SetFont(attr->GetFont());
1833 else if (item->IsBold())
eff869aa 1834 dc.SetFont(m_boldFont);
ef44a621 1835
618a5e38 1836 long text_w = 0, text_h = 0;
bbe0af5b 1837 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1838
618a5e38 1839 int image_h = 0, image_w = 0;
8dc99046
VZ
1840 int image = item->GetCurrentImage();
1841 if ( image != NO_IMAGE )
bbe0af5b 1842 {
2c8e4738
VZ
1843 if ( m_imageListNormal )
1844 {
1845 m_imageListNormal->GetSize( image, image_w, image_h );
1846 image_w += 4;
1847 }
1848 else
1849 {
1850 image = NO_IMAGE;
1851 }
bbe0af5b 1852 }
ef44a621 1853
91b8de8d
RR
1854 int total_h = GetLineHeight(item);
1855
b771aa29 1856 if ( item->IsSelected() )
cdb3cffe 1857 {
b771aa29 1858 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
cdb3cffe 1859 }
afa6a1a1 1860 else
c0fba4d1
VS
1861 {
1862 wxColour colBg;
1863 if ( attr && attr->HasBackgroundColour() )
1864 colBg = attr->GetBackgroundColour();
1865 else
1866 colBg = m_backgroundColour;
1867 dc.SetBrush(wxBrush(colBg, wxSOLID));
1868 }
afa6a1a1 1869
cdb3cffe 1870 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
b77d9650 1871
b771aa29 1872 if ( item->IsSelected() && image != NO_IMAGE )
a69cb1cc
JS
1873 {
1874 // If it's selected, and there's an image, then we should
1875 // take care to leave the area under the image painted in the
1876 // background colour.
cdb3cffe
VZ
1877 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
1878 item->GetWidth() - image_w + 2, total_h-offset );
a69cb1cc
JS
1879 }
1880 else
b77d9650 1881 {
cdb3cffe
VZ
1882 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
1883 item->GetWidth()+2, total_h-offset );
b77d9650 1884 }
ef44a621 1885
8dc99046 1886 if ( image != NO_IMAGE )
bbe0af5b 1887 {
d30b4d20 1888 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 1889 m_imageListNormal->Draw( image, dc,
49cd56ef 1890 item->GetX(),
d701d432 1891 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1892 wxIMAGELIST_DRAW_TRANSPARENT );
1893 dc.DestroyClippingRegion();
1894 }
ef44a621 1895
afa6a1a1 1896 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
1897 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
1898 dc.DrawText( item->GetText(),
1899 (wxCoord)(image_w + item->GetX()),
1900 (wxCoord)(item->GetY() + extraH));
ef44a621 1901
eff869aa
RR
1902 // restore normal font
1903 dc.SetFont( m_normalFont );
ef44a621
VZ
1904}
1905
91b8de8d 1906// Now y stands for the top of the item, whereas it used to stand for middle !
941830cb 1907void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1908{
618a5e38
RR
1909 int x = level*m_indent;
1910 if (!HasFlag(wxTR_HIDE_ROOT))
1911 {
1912 x += m_indent;
1913 }
1914 else if (level == 0)
1915 {
1916 wxArrayGenericTreeItems& children = item->GetChildren();
1917 size_t n, count = children.Count();
1918 for (n = 0; n < count; ++n)
1919 PaintLevel(children[n], dc, 1, y);
1920 return;
1921 }
91b8de8d 1922
618a5e38
RR
1923 item->SetX(x+m_spacing);
1924 item->SetY(y);
389cdc7a 1925
618a5e38
RR
1926 int h = GetLineHeight(item);
1927 int y_top = y;
1928 int y_mid = y_top + (h>>1);
1929 y += h;
4832f7c0 1930
618a5e38
RR
1931 int exposed_x = dc.LogicalToDeviceX(0);
1932 int exposed_y = dc.LogicalToDeviceY(y_top);
233058c7 1933
618a5e38 1934 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
bbe0af5b 1935 {
618a5e38 1936 if (item->HasPlus() && HasButtons()) // should the item show a button?
cdb3cffe 1937 {
618a5e38
RR
1938 if (!HasFlag(wxTR_NO_LINES))
1939 {
1940 if (x > (signed)m_indent || HasFlag(wxTR_LINES_AT_ROOT))
1941 dc.DrawLine(x - m_indent, y_mid, x - 5, y_mid);
1942 dc.DrawLine(x + 5, y_mid, x + m_spacing, y_mid);
1943 }
1944
1945 if (m_imageListButtons != NULL)
c22886bd 1946 {
618a5e38
RR
1947 // draw the image button here
1948 int image_h = 0, image_w = 0, image = wxCLOSED_BUTTON;
1949 if (item->IsExpanded()) image = wxOPEN_BUTTON;
1950 if (item->IsSelected())
1951 image += wxOPEN_BUTTON_SELECTED - wxOPEN_BUTTON;
1952 m_imageListButtons->GetSize(image, image_w, image_h);
1953 int xx = x - (image_w>>1);
1954 int yy = y_mid - (image_h>>1);
1955 dc.SetClippingRegion(xx, yy, image_w, image_h);
1956 m_imageListButtons->Draw(image, dc, xx, yy,
1957 wxIMAGELIST_DRAW_TRANSPARENT);
1958 dc.DestroyClippingRegion();
1959 }
1960 else if (HasFlag(wxTR_TWIST_BUTTONS))
1961 {
1962 // draw the twisty button here
1963 dc.SetPen(*wxBLACK_PEN);
1964 dc.SetBrush(*m_hilightBrush);
cdb3cffe 1965
c22886bd 1966 wxPoint button[3];
cdb3cffe 1967
c22886bd
RR
1968 if (item->IsExpanded())
1969 {
1970 button[0].x = x-5;
618a5e38 1971 button[0].y = y_mid-2;
c22886bd 1972 button[1].x = x+5;
618a5e38 1973 button[1].y = y_mid-2;
c22886bd 1974 button[2].x = x;
618a5e38 1975 button[2].y = y_mid+3;
c22886bd
RR
1976 }
1977 else
1978 {
618a5e38 1979 button[0].y = y_mid-5;
c22886bd 1980 button[0].x = x-2;
618a5e38 1981 button[1].y = y_mid+5;
c22886bd 1982 button[1].x = x-2;
618a5e38 1983 button[2].y = y_mid;
c22886bd
RR
1984 button[2].x = x+3;
1985 }
618a5e38 1986 dc.DrawPolygon(3, button);
cdb3cffe 1987
618a5e38 1988 dc.SetPen(m_dottedPen);
c22886bd 1989 }
618a5e38 1990 else // if (HasFlag(wxTR_HAS_BUTTONS))
c22886bd 1991 {
618a5e38
RR
1992 // draw the plus sign here
1993 dc.SetPen(*wxGREY_PEN);
1994 dc.SetBrush(*wxWHITE_BRUSH);
1995 dc.DrawRectangle(x-5, y_mid-4, 11, 9);
1996 dc.SetPen(*wxBLACK_PEN);
1997 dc.DrawLine(x-2, y_mid, x+3, y_mid);
c22886bd 1998 if (!item->IsExpanded())
618a5e38
RR
1999 dc.DrawLine(x, y_mid-2, x, y_mid+3);
2000 dc.SetPen(m_dottedPen);
c22886bd 2001 }
bbe0af5b 2002 }
618a5e38
RR
2003 else if (!HasFlag(wxTR_NO_LINES)) // no button; maybe a line?
2004 {
2005 // draw the horizontal line here
2006 int x_start = x;
2007 if (x > (signed)m_indent || HasFlag(wxTR_LINES_AT_ROOT))
2008 x_start -= m_indent;
2009 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2010 }
c801d85f 2011
618a5e38 2012 wxPen *pen =
cdebf985 2013#ifndef __WXMAC__
618a5e38
RR
2014 // don't draw rect outline if we already have the
2015 // background color under Mac
b771aa29 2016 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
cdebf985 2017#endif // !__WXMAC__
618a5e38 2018 wxTRANSPARENT_PEN;
cdebf985
VZ
2019
2020 wxColour colText;
de57703a 2021 if ( item->IsSelected() )
cdebf985 2022 {
618a5e38 2023 colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
bbe0af5b
RR
2024 }
2025 else
2026 {
9ec64fa7 2027 wxTreeItemAttr *attr = item->GetAttributes();
618a5e38 2028 if (attr && attr->HasTextColour())
9ec64fa7
VZ
2029 colText = attr->GetTextColour();
2030 else
618a5e38 2031 colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT);
bbe0af5b 2032 }
9ec64fa7
VZ
2033
2034 // prepare to draw
2035 dc.SetTextForeground(colText);
2036 dc.SetPen(*pen);
9ec64fa7
VZ
2037
2038 // draw
2039 PaintItem(item, dc);
2040
618a5e38 2041 if (HasFlag(wxTR_ROW_LINES))
b77d9650 2042 {
618a5e38
RR
2043 dc.SetPen(*wxWHITE_PEN);
2044 dc.DrawLine(0, y_top, 10000, y_top);
2045 dc.DrawLine(0, y, 10000, y);
b77d9650 2046 }
cdb3cffe 2047
9ec64fa7 2048 // restore DC objects
618a5e38
RR
2049 dc.SetBrush(*wxWHITE_BRUSH);
2050 dc.SetPen(m_dottedPen);
2051 dc.SetTextForeground(*wxBLACK);
f135ff73 2052 }
d3a9f4af 2053
bbe0af5b
RR
2054 if (item->IsExpanded())
2055 {
91b8de8d 2056 wxArrayGenericTreeItems& children = item->GetChildren();
618a5e38 2057 int count = children.Count();
f65635b5 2058 if (count > 0)
9ec64fa7 2059 {
618a5e38
RR
2060 int n = 0, oldY;
2061 ++level;
2062 do {
2063 oldY = y;
2064 PaintLevel(children[n], dc, level, y);
2065 } while (++n < count);
2066
2067 if (!HasFlag(wxTR_NO_LINES))
2068 {
2069 // draw line down to last child
2070 oldY += GetLineHeight(children[n-1])/2;
2071 if (HasButtons()) y_mid += 5;
2072 dc.DrawLine(x, y_mid, x, oldY);
2073 }
9ec64fa7 2074 }
bbe0af5b 2075 }
4c681997 2076}
c801d85f 2077
941830cb 2078void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
2079{
2080 if ( item )
2081 {
2082 if ( item->HasPlus() )
2083 {
2084 // it's a folder, indicate it by a border
2085 DrawBorder(item);
2086 }
2087 else
2088 {
2089 // draw a line under the drop target because the item will be
2090 // dropped there
2091 DrawLine(item, TRUE /* below */);
2092 }
2093
2094 SetCursor(wxCURSOR_BULLSEYE);
2095 }
2096 else
2097 {
2098 // can't drop here
2099 SetCursor(wxCURSOR_NO_ENTRY);
2100 }
2101}
2102
941830cb 2103void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 2104{
941830cb 2105 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2106
941830cb 2107 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2108
1044a386 2109 wxClientDC dc(this);
91b8de8d
RR
2110 PrepareDC( dc );
2111 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 2112 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 2113
3dbeaa52
VZ
2114 int w = i->GetWidth() + 2;
2115 int h = GetLineHeight(i) + 2;
91b8de8d 2116
3dbeaa52 2117 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
2118}
2119
941830cb 2120void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 2121{
941830cb 2122 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2123
941830cb 2124 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2125
1044a386 2126 wxClientDC dc(this);
91b8de8d
RR
2127 PrepareDC( dc );
2128 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 2129
3dbeaa52
VZ
2130 int x = i->GetX(),
2131 y = i->GetY();
2132 if ( below )
2133 {
2134 y += GetLineHeight(i) - 1;
2135 }
91b8de8d 2136
3dbeaa52 2137 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
2138}
2139
f135ff73
VZ
2140// -----------------------------------------------------------------------------
2141// wxWindows callbacks
2142// -----------------------------------------------------------------------------
2143
941830cb 2144void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 2145{
0659e7ee
RR
2146 wxPaintDC dc(this);
2147 PrepareDC( dc );
29d87bba 2148
941830cb
JS
2149 if ( !m_anchor)
2150 return;
2151
eff869aa 2152 dc.SetFont( m_normalFont );
0659e7ee 2153 dc.SetPen( m_dottedPen );
f38374d0 2154
eff869aa 2155 // this is now done dynamically
91b8de8d
RR
2156 //if(GetImageList() == NULL)
2157 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2158
91b8de8d 2159 int y = 2;
0659e7ee 2160 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 2161}
c801d85f 2162
b771aa29 2163void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
c801d85f 2164{
0659e7ee 2165 m_hasFocus = TRUE;
978f38c2 2166
b771aa29
VZ
2167 RefreshSelected();
2168
2169 event.Skip();
edaa81ae 2170}
c801d85f 2171
b771aa29 2172void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
c801d85f 2173{
0659e7ee 2174 m_hasFocus = FALSE;
978f38c2 2175
b771aa29
VZ
2176 RefreshSelected();
2177
2178 event.Skip();
edaa81ae 2179}
c801d85f 2180
941830cb 2181void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
c801d85f 2182{
978f38c2 2183 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
06b466c7 2184 te.m_code = (int)event.KeyCode();
978f38c2 2185 te.SetEventObject( this );
68eb5f63
VZ
2186 if ( GetEventHandler()->ProcessEvent( te ) )
2187 {
2188 // intercepted by the user code
2189 return;
2190 }
435fe83e 2191
91b8de8d 2192 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
2193 {
2194 event.Skip();
2195 return;
2196 }
ef44a621 2197
06b466c7
VZ
2198 // how should the selection work for this event?
2199 bool is_multiple, extended_select, unselect_others;
2200 EventFlagsToSelType(GetWindowStyleFlag(),
2201 event.ShiftDown(),
2202 event.ControlDown(),
dfc6cd93
SB
2203 is_multiple, extended_select, unselect_others);
2204
2205 // + : Expand
2206 // - : Collaspe
f6bcfd97 2207 // * : Expand all/Collapse all
dfc6cd93
SB
2208 // ' ' | return : activate
2209 // up : go up (not last children!)
2210 // down : go down
2211 // left : go to parent
2212 // right : open if parent and go next
2213 // home : go to root
2214 // end : go to last item without opening parents
978f38c2
VZ
2215 switch (event.KeyCode())
2216 {
2217 case '+':
2218 case WXK_ADD:
2219 if (m_current->HasPlus() && !IsExpanded(m_current))
2220 {
2221 Expand(m_current);
2222 }
2223 break;
ef44a621 2224
f6bcfd97
BP
2225 case '*':
2226 case WXK_MULTIPLY:
2227 if ( !IsExpanded(m_current) )
2228 {
2229 // expand all
2230 ExpandAll(m_current);
2231 break;
2232 }
2233 //else: fall through to Collapse() it
2234
978f38c2
VZ
2235 case '-':
2236 case WXK_SUBTRACT:
2237 if (IsExpanded(m_current))
2238 {
2239 Collapse(m_current);
2240 }
2241 break;
ef44a621 2242
978f38c2
VZ
2243 case ' ':
2244 case WXK_RETURN:
2245 {
2246 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
40fab78b 2247 event.m_item = (long) m_current;
978f38c2
VZ
2248 event.m_code = 0;
2249 event.SetEventObject( this );
2250 GetEventHandler()->ProcessEvent( event );
2251 }
2252 break;
ef44a621 2253
618a5e38
RR
2254 // up goes to the previous sibling or to the last
2255 // of its children if it's expanded
978f38c2
VZ
2256 case WXK_UP:
2257 {
91b8de8d 2258 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
2259 if (!prev)
2260 {
91b8de8d 2261 prev = GetParent( m_key_current );
618a5e38
RR
2262 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2263 {
2264 break; // don't go to root if it is hidden
2265 }
c193b707
VZ
2266 if (prev)
2267 {
618a5e38 2268 long cookie = 0;
91b8de8d 2269 wxTreeItemId current = m_key_current;
618a5e38
RR
2270 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2271 if (current == GetFirstChild( prev, cookie ))
90e58684
RR
2272 {
2273 // otherwise we return to where we came from
88ac883a 2274 SelectItem( prev, unselect_others, extended_select );
941830cb 2275 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
c193b707 2276 EnsureVisible( prev );
90e58684 2277 break;
c193b707 2278 }
978f38c2
VZ
2279 }
2280 }
2281 if (prev)
2282 {
69a282d4 2283 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 2284 {
69a282d4
VZ
2285 wxTreeItemId child = GetLastChild(prev);
2286 if ( child )
2287 {
2288 prev = child;
2289 }
978f38c2 2290 }
69a282d4 2291
88ac883a 2292 SelectItem( prev, unselect_others, extended_select );
941830cb 2293 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
2294 EnsureVisible( prev );
2295 }
2296 }
2297 break;
ef44a621 2298
978f38c2
VZ
2299 // left arrow goes to the parent
2300 case WXK_LEFT:
2301 {
2302 wxTreeItemId prev = GetParent( m_current );
618a5e38
RR
2303 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2304 {
2305 // don't go to root if it is hidden
2306 prev = GetPrevSibling( m_current );
2307 }
978f38c2
VZ
2308 if (prev)
2309 {
2310 EnsureVisible( prev );
88ac883a 2311 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2312 }
2313 }
2314 break;
ef44a621 2315
978f38c2 2316 case WXK_RIGHT:
618a5e38
RR
2317 // this works the same as the down arrow except that we
2318 // also expand the item if it wasn't expanded yet
978f38c2
VZ
2319 Expand(m_current);
2320 // fall through
2321
2322 case WXK_DOWN:
ef44a621 2323 {
91b8de8d 2324 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
2325 {
2326 long cookie = 0;
91b8de8d 2327 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 2328 SelectItem( child, unselect_others, extended_select );
941830cb 2329 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
2330 EnsureVisible( child );
2331 }
2332 else
2333 {
91b8de8d 2334 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2335 if (!next)
978f38c2 2336 {
91b8de8d 2337 wxTreeItemId current = m_key_current;
978f38c2
VZ
2338 while (current && !next)
2339 {
2340 current = GetParent( current );
2341 if (current) next = GetNextSibling( current );
2342 }
2343 }
b62c3631 2344 if (next)
978f38c2 2345 {
88ac883a 2346 SelectItem( next, unselect_others, extended_select );
941830cb 2347 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
2348 EnsureVisible( next );
2349 }
2350 }
ef44a621 2351 }
978f38c2 2352 break;
ef44a621 2353
978f38c2
VZ
2354 // <End> selects the last visible tree item
2355 case WXK_END:
2356 {
2357 wxTreeItemId last = GetRootItem();
2358
2359 while ( last.IsOk() && IsExpanded(last) )
2360 {
2361 wxTreeItemId lastChild = GetLastChild(last);
2362
2363 // it may happen if the item was expanded but then all of
2364 // its children have been deleted - so IsExpanded() returned
2365 // TRUE, but GetLastChild() returned invalid item
2366 if ( !lastChild )
2367 break;
2368
2369 last = lastChild;
2370 }
2371
2372 if ( last.IsOk() )
2373 {
2374 EnsureVisible( last );
88ac883a 2375 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2376 }
2377 }
2378 break;
2379
2380 // <Home> selects the root item
2381 case WXK_HOME:
2382 {
2383 wxTreeItemId prev = GetRootItem();
618a5e38
RR
2384 if (!prev) break;
2385 if (HasFlag(wxTR_HIDE_ROOT))
978f38c2 2386 {
618a5e38
RR
2387 long dummy;
2388 prev = GetFirstChild(prev, dummy);
2389 if (!prev) break;
978f38c2 2390 }
618a5e38
RR
2391 EnsureVisible( prev );
2392 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2393 }
2394 break;
2395
2396 default:
2397 event.Skip();
2398 }
edaa81ae 2399}
c801d85f 2400
941830cb 2401wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2402{
618a5e38
RR
2403 // JACS: removed wxYieldIfNeeded() because it can cause the window
2404 // to be deleted from under us if a close window event is pending
dc6c62a9 2405
91b8de8d
RR
2406 int w, h;
2407 GetSize(&w, &h);
91b8de8d 2408 flags=0;
618a5e38
RR
2409 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2410 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2411 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2412 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2413 if (flags) return wxTreeItemId();
d3a9f4af 2414
618a5e38
RR
2415 if (m_anchor == NULL)
2416 {
2417 flags = wxTREE_HITTEST_NOWHERE;
2418 return wxTreeItemId();
2419 }
5716a1ab 2420
618a5e38
RR
2421 wxClientDC dc(this);
2422 PrepareDC(dc);
2423 wxCoord x = dc.DeviceToLogicalX( point.x );
2424 wxCoord y = dc.DeviceToLogicalY( point.y );
2425 wxGenericTreeItem *hit = m_anchor->HitTest(wxPoint(x, y),
2426 this,
2427 flags,
2428 0 );
2429 if (hit == NULL)
2430 {
2431 flags = wxTREE_HITTEST_NOWHERE;
2432 return wxTreeItemId();
2433 }
2434 return hit;
4f22cf8d
RR
2435}
2436
8fb3bfe2
JS
2437// get the bounding rectangle of the item (or of its label only)
2438bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2439 wxRect& rect,
33ac7e6f 2440 bool WXUNUSED(textOnly)) const
8fb3bfe2 2441{
601c163b 2442 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
8fb3bfe2
JS
2443
2444 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2445
2446 int startX, startY;
2447 GetViewStart(& startX, & startY);
2448
1ed01484 2449 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
c22886bd 2450 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
1ed01484 2451 rect.width = i->GetWidth();
c22886bd
RR
2452 //rect.height = i->GetHeight();
2453 rect.height = GetLineHeight(i);
8fb3bfe2
JS
2454
2455 return TRUE;
8fb3bfe2
JS
2456}
2457
e179bd65
RR
2458/* **** */
2459
941830cb 2460void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
e179bd65
RR
2461{
2462 if (!item.IsOk()) return;
2463
941830cb 2464 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
9dfbf520 2465
e179bd65 2466 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
40fab78b 2467 te.m_item = (long) m_currentEdit;
e179bd65
RR
2468 te.SetEventObject( this );
2469 GetEventHandler()->ProcessEvent( te );
2470
2471 if (!te.IsAllowed()) return;
8dc99046 2472
dc6c62a9
RR
2473 // We have to call this here because the label in
2474 // question might just have been added and no screen
2475 // update taken place.
cd66f45b 2476 if (m_dirty) wxYieldIfNeeded();
9dfbf520 2477
e179bd65
RR
2478 wxString s = m_currentEdit->GetText();
2479 int x = m_currentEdit->GetX();
2480 int y = m_currentEdit->GetY();
2481 int w = m_currentEdit->GetWidth();
2482 int h = m_currentEdit->GetHeight();
9dfbf520 2483
5f1ea0ee
RR
2484 int image_h = 0;
2485 int image_w = 0;
8dc99046
VZ
2486
2487 int image = m_currentEdit->GetCurrentImage();
2488 if ( image != NO_IMAGE )
5f1ea0ee 2489 {
2c8e4738
VZ
2490 if ( m_imageListNormal )
2491 {
2492 m_imageListNormal->GetSize( image, image_w, image_h );
2493 image_w += 4;
2494 }
6359fa0e 2495 else
2c8e4738
VZ
2496 {
2497 wxFAIL_MSG(_T("you must create an image list to use images!"));
2498 }
5f1ea0ee
RR
2499 }
2500 x += image_w;
2501 w -= image_w + 4; // I don't know why +4 is needed
e179bd65
RR
2502
2503 wxClientDC dc(this);
2504 PrepareDC( dc );
2505 x = dc.LogicalToDeviceX( x );
2506 y = dc.LogicalToDeviceY( y );
2507
6359fa0e
VZ
2508 wxTreeTextCtrl *text = new wxTreeTextCtrl(this, -1,
2509 &m_renameAccept,
2510 &m_renameRes,
2511 this,
2512 s,
2513 wxPoint(x-4,y-4),
2514 wxSize(w+11,h+8));
e179bd65
RR
2515 text->SetFocus();
2516}
2517
941830cb 2518void wxGenericTreeCtrl::OnRenameTimer()
e179bd65
RR
2519{
2520 Edit( m_current );
2521}
2522
941830cb 2523void wxGenericTreeCtrl::OnRenameAccept()
e179bd65 2524{
618a5e38 2525 // TODO if the validator fails this causes a crash
e179bd65 2526 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
40fab78b 2527 le.m_item = (long) m_currentEdit;
e179bd65
RR
2528 le.SetEventObject( this );
2529 le.m_label = m_renameRes;
2530 GetEventHandler()->ProcessEvent( le );
9dfbf520 2531
e179bd65 2532 if (!le.IsAllowed()) return;
9dfbf520 2533
5f1ea0ee 2534 SetItemText( m_currentEdit, m_renameRes );
e179bd65 2535}
9dfbf520 2536
941830cb 2537void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2538{
bbe0af5b 2539 if ( !m_anchor ) return;
978f38c2 2540
3dbeaa52
VZ
2541 // we process left mouse up event (enables in-place edit), right down
2542 // (pass to the user code), left dbl click (activate item) and
2543 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
2544 if ( !(event.LeftDown() ||
2545 event.LeftUp() ||
3dbeaa52
VZ
2546 event.RightDown() ||
2547 event.LeftDClick() ||
2548 event.Dragging() ||
2549 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2550 {
2551 event.Skip();
2552
2553 return;
2554 }
2555
bbe0af5b
RR
2556 wxClientDC dc(this);
2557 PrepareDC(dc);
06b466c7
VZ
2558 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2559 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
29d87bba 2560
3dbeaa52 2561 int flags = 0;
618a5e38
RR
2562 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y),
2563 this,
2564 flags,
2565 0 );
3dbeaa52 2566
3dbeaa52 2567 if ( event.Dragging() && !m_isDragging )
bbe0af5b 2568 {
fd9811b1 2569 if (m_dragCount == 0)
8dc99046
VZ
2570 m_dragStart = wxPoint(x,y);
2571
fd9811b1 2572 m_dragCount++;
8dc99046 2573
3dbeaa52
VZ
2574 if (m_dragCount != 3)
2575 {
2576 // wait until user drags a bit further...
2577 return;
2578 }
8dc99046 2579
3dbeaa52
VZ
2580 wxEventType command = event.RightIsDown()
2581 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2582 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 2583
fd9811b1 2584 wxTreeEvent nevent( command, GetId() );
40fab78b 2585 nevent.m_item = (long) m_current;
fd9811b1 2586 nevent.SetEventObject(this);
3dbeaa52
VZ
2587
2588 // by default the dragging is not supported, the user code must
2589 // explicitly allow the event for it to take place
2590 nevent.Veto();
2591
2592 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2593 {
2594 // we're going to drag this item
2595 m_isDragging = TRUE;
2596
b3a7510d
VZ
2597 // remember the old cursor because we will change it while
2598 // dragging
2599 m_oldCursor = m_cursor;
2600
2601 // in a single selection control, hide the selection temporarily
2602 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2603 {
941830cb 2604 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
2605
2606 if ( m_oldSelection )
2607 {
2608 m_oldSelection->SetHilight(FALSE);
2609 RefreshLine(m_oldSelection);
2610 }
2611 }
2612
3dbeaa52
VZ
2613 CaptureMouse();
2614 }
bbe0af5b 2615 }
3dbeaa52 2616 else if ( event.Moving() )
fd9811b1 2617 {
3dbeaa52
VZ
2618 if ( item != m_dropTarget )
2619 {
2620 // unhighlight the previous drop target
2621 DrawDropEffect(m_dropTarget);
fd9811b1 2622
3dbeaa52 2623 m_dropTarget = item;
978f38c2 2624
3dbeaa52
VZ
2625 // highlight the current drop target if any
2626 DrawDropEffect(m_dropTarget);
fb882e1c 2627
cd66f45b 2628 wxYieldIfNeeded();
3dbeaa52 2629 }
e179bd65 2630 }
3dbeaa52
VZ
2631 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2632 {
2633 // erase the highlighting
2634 DrawDropEffect(m_dropTarget);
9dfbf520 2635
4f5fffcc
RR
2636 if ( m_oldSelection )
2637 {
2638 m_oldSelection->SetHilight(TRUE);
2639 RefreshLine(m_oldSelection);
2640 m_oldSelection = (wxGenericTreeItem *)NULL;
2641 }
2642
3dbeaa52
VZ
2643 // generate the drag end event
2644 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 2645
40fab78b 2646 event.m_item = (long) item;
3dbeaa52
VZ
2647 event.m_pointDrag = wxPoint(x, y);
2648 event.SetEventObject(this);
91b8de8d 2649
3dbeaa52 2650 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 2651
3dbeaa52
VZ
2652 m_isDragging = FALSE;
2653 m_dropTarget = (wxGenericTreeItem *)NULL;
2654
2655 ReleaseMouse();
2656
b3a7510d 2657 SetCursor(m_oldCursor);
3dbeaa52 2658
cd66f45b 2659 wxYieldIfNeeded();
3dbeaa52
VZ
2660 }
2661 else
bbe0af5b 2662 {
3dbeaa52
VZ
2663 // here we process only the messages which happen on tree items
2664
2665 m_dragCount = 0;
2666
2667 if (item == NULL) return; /* we hit the blank area */
2668
2669 if ( event.RightDown() )
2670 {
2671 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
40fab78b 2672 nevent.m_item = (long) item;
3dbeaa52 2673 nevent.m_code = 0;
05a7f61d
VZ
2674 CalcScrolledPosition(x, y,
2675 &nevent.m_pointDrag.x,
2676 &nevent.m_pointDrag.y);
3dbeaa52
VZ
2677 nevent.SetEventObject(this);
2678 GetEventHandler()->ProcessEvent(nevent);
2679 }
80d2803f 2680 else if ( event.LeftUp() )
f6bcfd97 2681 {
80d2803f 2682 if ( m_lastOnSame )
f6bcfd97 2683 {
80d2803f
VZ
2684 if ( (item == m_current) &&
2685 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2686 HasFlag(wxTR_EDIT_LABELS) )
2687 {
bdb310a7
VZ
2688 if ( m_renameTimer->IsRunning() )
2689 m_renameTimer->Stop();
2690
80d2803f
VZ
2691 m_renameTimer->Start( 100, TRUE );
2692 }
f6bcfd97 2693
80d2803f
VZ
2694 m_lastOnSame = FALSE;
2695 }
3dbeaa52 2696 }
0326c494 2697 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3dbeaa52 2698 {
f6bcfd97
BP
2699 if ( event.LeftDown() )
2700 {
2701 m_lastOnSame = item == m_current;
2702 }
2703
0326c494
VZ
2704 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
2705 {
2706 // only toggle the item for a single click, double click on
2707 // the button doesn't do anything (it toggles the item twice)
2708 if ( event.LeftDown() )
2709 {
2710 Toggle( item );
2711 }
2712
2713 // don't select the item if the button was clicked
2714 return;
2715 }
2716
3dbeaa52
VZ
2717 // how should the selection work for this event?
2718 bool is_multiple, extended_select, unselect_others;
2719 EventFlagsToSelType(GetWindowStyleFlag(),
2720 event.ShiftDown(),
2721 event.ControlDown(),
dfc6cd93 2722 is_multiple, extended_select, unselect_others);
3dbeaa52 2723
3dbeaa52
VZ
2724 SelectItem(item, unselect_others, extended_select);
2725
618a5e38
RR
2726 // For some reason, Windows isn't recognizing a left double-click,
2727 // so we need to simulate it here. Allow 200 milliseconds for now.
3dbeaa52
VZ
2728 if ( event.LeftDClick() )
2729 {
f6bcfd97
BP
2730 // double clicking should not start editing the item label
2731 m_renameTimer->Stop();
2732 m_lastOnSame = FALSE;
2733
ebb987b7
VZ
2734 // send activate event first
2735 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2736 nevent.m_item = (long) item;
2737 nevent.m_code = 0;
2738 CalcScrolledPosition(x, y,
2739 &nevent.m_pointDrag.x,
2740 &nevent.m_pointDrag.y);
2741 nevent.SetEventObject( this );
2742 if ( !GetEventHandler()->ProcessEvent( nevent ) )
618a5e38 2743 {
ebb987b7
VZ
2744 // if the user code didn't process the activate event,
2745 // handle it ourselves by toggling the item when it is
2746 // double clicked
2747 if ( item->HasPlus() )
2748 {
2749 Toggle(item);
2750 }
618a5e38 2751 }
3dbeaa52
VZ
2752 }
2753 }
bbe0af5b 2754 }
edaa81ae 2755}
c801d85f 2756
941830cb 2757void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
3db7be80 2758{
bbe0af5b
RR
2759 /* after all changes have been done to the tree control,
2760 * we actually redraw the tree when everything is over */
ef44a621 2761
618a5e38 2762 if (!m_dirty) return;
ef44a621 2763
bbe0af5b 2764 m_dirty = FALSE;
3db7be80 2765
bbe0af5b 2766 CalculatePositions();
91b8de8d 2767 Refresh();
bbe0af5b 2768 AdjustMyScrollbars();
3db7be80
RR
2769}
2770
941830cb 2771void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 2772{
e06b9569
JS
2773 wxCoord text_w = 0;
2774 wxCoord text_h = 0;
9dfbf520 2775
a62867a5 2776 if (item->IsBold())
eff869aa 2777 dc.SetFont(m_boldFont);
9dfbf520 2778
91b8de8d 2779 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 2780 text_h+=2;
91b8de8d 2781
eff869aa
RR
2782 // restore normal font
2783 dc.SetFont( m_normalFont );
9dfbf520 2784
91b8de8d
RR
2785 int image_h = 0;
2786 int image_w = 0;
8dc99046
VZ
2787 int image = item->GetCurrentImage();
2788 if ( image != NO_IMAGE )
91b8de8d 2789 {
2c8e4738
VZ
2790 if ( m_imageListNormal )
2791 {
2792 m_imageListNormal->GetSize( image, image_w, image_h );
2793 image_w += 4;
2794 }
91b8de8d
RR
2795 }
2796
2797 int total_h = (image_h > text_h) ? image_h : text_h;
2798
618a5e38 2799 if (total_h < 30)
f2593d0d 2800 total_h += 2; // at least 2 pixels
06b466c7 2801 else
f2593d0d 2802 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
2803
2804 item->SetHeight(total_h);
6cedba09
VZ
2805 if (total_h>m_lineHeight)
2806 m_lineHeight=total_h;
bbe0af5b 2807
91b8de8d
RR
2808 item->SetWidth(image_w+text_w+2);
2809}
2810
2811// -----------------------------------------------------------------------------
2812// for developper : y is now the top of the level
2813// not the middle of it !
941830cb 2814void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2815{
618a5e38
RR
2816 int x = level*m_indent;
2817 if (!HasFlag(wxTR_HIDE_ROOT))
2818 {
2819 x += m_indent;
2820 }
2821 else if (level == 0)
2822 {
2823 // a hidden root is not evaluated, but its
2824 // children are always calculated
2825 goto Recurse;
2826 }
389cdc7a 2827
91b8de8d 2828 CalculateSize( item, dc );
d3a9f4af 2829
91b8de8d 2830 // set its position
618a5e38 2831 item->SetX( x+m_spacing );
91b8de8d 2832 item->SetY( y );
618a5e38 2833 y += GetLineHeight(item);
4c681997 2834
bbe0af5b
RR
2835 if ( !item->IsExpanded() )
2836 {
618a5e38 2837 // we don't need to calculate collapsed branches
bbe0af5b
RR
2838 return;
2839 }
389cdc7a 2840
618a5e38 2841 Recurse:
91b8de8d
RR
2842 wxArrayGenericTreeItems& children = item->GetChildren();
2843 size_t n, count = children.Count();
618a5e38 2844 ++level;
91b8de8d 2845 for (n = 0; n < count; ++n )
618a5e38 2846 CalculateLevel( children[n], dc, level, y ); // recurse
edaa81ae 2847}
c801d85f 2848
941830cb 2849void wxGenericTreeCtrl::CalculatePositions()
c801d85f 2850{
bbe0af5b 2851 if ( !m_anchor ) return;
29d87bba 2852
bbe0af5b
RR
2853 wxClientDC dc(this);
2854 PrepareDC( dc );
29d87bba 2855
eff869aa 2856 dc.SetFont( m_normalFont );
29d87bba 2857
bbe0af5b 2858 dc.SetPen( m_dottedPen );
91b8de8d
RR
2859 //if(GetImageList() == NULL)
2860 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2861
8dc99046 2862 int y = 2;
f65635b5 2863 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 2864}
c801d85f 2865
941830cb 2866void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 2867{
e6527f9d
RR
2868 if (m_dirty) return;
2869
bbe0af5b
RR
2870 wxClientDC dc(this);
2871 PrepareDC(dc);
4832f7c0 2872
bbe0af5b
RR
2873 int cw = 0;
2874 int ch = 0;
2875 GetClientSize( &cw, &ch );
4832f7c0 2876
bbe0af5b
RR
2877 wxRect rect;
2878 rect.x = dc.LogicalToDeviceX( 0 );
2879 rect.width = cw;
2880 rect.y = dc.LogicalToDeviceY( item->GetY() );
2881 rect.height = ch;
f135ff73 2882
bbe0af5b 2883 Refresh( TRUE, &rect );
f135ff73 2884
bbe0af5b 2885 AdjustMyScrollbars();
edaa81ae 2886}
c801d85f 2887
941830cb 2888void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 2889{
e6527f9d
RR
2890 if (m_dirty) return;
2891
bbe0af5b
RR
2892 wxClientDC dc(this);
2893 PrepareDC( dc );
2894
5391f772
SB
2895 int cw = 0;
2896 int ch = 0;
2897 GetClientSize( &cw, &ch );
2898
bbe0af5b 2899 wxRect rect;
5391f772
SB
2900 rect.x = dc.LogicalToDeviceX( 0 );
2901 rect.y = dc.LogicalToDeviceY( item->GetY() );
2902 rect.width = cw;
91b8de8d 2903 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 2904
bbe0af5b 2905 Refresh( TRUE, &rect );
edaa81ae 2906}
c801d85f 2907
b771aa29
VZ
2908void wxGenericTreeCtrl::RefreshSelected()
2909{
2910 // TODO: this is awfully inefficient, we should keep the list of all
2911 // selected items internally, should be much faster
2912 if ( m_anchor )
2913 RefreshSelectedUnder(m_anchor);
2914}
2915
2916void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
2917{
2918 if ( item->IsSelected() )
2919 RefreshLine(item);
2920
2921 const wxArrayGenericTreeItems& children = item->GetChildren();
2922 size_t count = children.GetCount();
2923 for ( size_t n = 0; n < count; n++ )
2924 {
2925 RefreshSelectedUnder(children[n]);
2926 }
2927}
2928
1e6feb95 2929#endif // wxUSE_TREECTRL