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