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