]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
Added RTLD_GLOBAL to dlopen() flags which is needed if libraries depend
[wxWidgets.git] / src / generic / treectrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.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__
f135ff73 21 #pragma implementation "treectrl.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__
28#pragma hdrstop
29#endif
30
f98e2558 31#include "wx/treectrl.h"
f60d0f94 32#include "wx/generic/imaglist.h"
c801d85f 33#include "wx/settings.h"
389cdc7a 34#include "wx/log.h"
f135ff73
VZ
35#include "wx/intl.h"
36#include "wx/dynarray.h"
91b8de8d 37#include "wx/arrimpl.cpp"
f135ff73 38#include "wx/dcclient.h"
0659e7ee 39#include "wx/msgdlg.h"
c801d85f 40
f135ff73
VZ
41// -----------------------------------------------------------------------------
42// array types
43// -----------------------------------------------------------------------------
c801d85f 44
1e6d9499
JS
45class WXDLLEXPORT wxGenericTreeItem;
46
91b8de8d
RR
47WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
48WX_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:
66 wxTreeRenameTimer( wxTreeCtrl *owner );
67
68 void Notify();
69
70private:
71 wxTreeCtrl *m_owner;
72};
73
74// control used for in-place edit
75class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
76{
77public:
78 wxTreeTextCtrl() { }
79 wxTreeTextCtrl( wxWindow *parent,
80 const wxWindowID id,
81 bool *accept,
82 wxString *res,
83 wxTreeCtrl *owner,
84 const wxString &value = wxEmptyString,
85 const wxPoint &pos = wxDefaultPosition,
86 const wxSize &size = wxDefaultSize,
87 int style = 0,
88 const wxValidator& validator = wxDefaultValidator,
89 const wxString &name = wxTextCtrlNameStr );
90
91 void OnChar( wxKeyEvent &event );
92 void OnKillFocus( wxFocusEvent &event );
93
94private:
95 bool *m_accept;
96 wxString *m_res;
97 wxTreeCtrl *m_owner;
98 wxString m_startValue;
99
100 DECLARE_EVENT_TABLE()
101 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl);
102};
103
f135ff73
VZ
104// a tree item
105class WXDLLEXPORT wxGenericTreeItem
c801d85f 106{
f135ff73 107public:
9ec64fa7
VZ
108 // ctors & dtor
109 wxGenericTreeItem() { m_data = NULL; }
110 wxGenericTreeItem( wxGenericTreeItem *parent,
111 const wxString& text,
112 wxDC& dc,
113 int image, 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
RR
149
150
9ec64fa7 151 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 152
9ec64fa7
VZ
153 // operations
154 // deletes all children notifying the treectrl about it if !NULL
155 // pointer given
156 void DeleteChildren(wxTreeCtrl *tree = NULL);
157 // FIXME don't know what is it for
158 void Reset();
f135ff73 159
9ec64fa7
VZ
160 // get count of all children (and grand children if 'recursively')
161 size_t GetChildrenCount(bool recursively = TRUE) const;
f135ff73 162
9ec64fa7 163 void Insert(wxGenericTreeItem *child, size_t index)
f135ff73
VZ
164 { m_children.Insert(child, index); }
165
9ec64fa7
VZ
166 void SetCross( int x, int y );
167 void GetSize( int &x, int &y, const wxTreeCtrl* );
f135ff73 168
9ec64fa7
VZ
169 // return the item at given position (or NULL if no item), onButton is
170 // TRUE if the point belongs to the item's button, otherwise it lies
171 // on the button's label
172 wxGenericTreeItem *HitTest( const wxPoint& point, const wxTreeCtrl *, int &flags);
f135ff73 173
9ec64fa7
VZ
174 void Expand() { m_isCollapsed = FALSE; }
175 void Collapse() { m_isCollapsed = TRUE; }
f135ff73 176
9ec64fa7 177 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
f135ff73 178
9ec64fa7
VZ
179 // status inquiries
180 bool HasChildren() const { return !m_children.IsEmpty(); }
181 bool IsSelected() const { return m_hasHilight; }
182 bool IsExpanded() const { return !m_isCollapsed; }
183 bool HasPlus() const { return m_hasPlus || HasChildren(); }
184 bool IsBold() const { return m_isBold; }
185
186 // attributes
187 // get them - may be NULL
188 wxTreeItemAttr *GetAttributes() const { return m_attr; }
189 // get them ensuring that the pointer is not NULL
190 wxTreeItemAttr& Attr()
191 {
192 if ( !m_attr )
193 m_attr = new wxTreeItemAttr;
194
195 return *m_attr;
196 }
f135ff73
VZ
197
198private:
9ec64fa7
VZ
199 wxString m_text;
200
201 // tree ctrl images for the normal, selected, expanded and
202 // expanded+selected states
203 int m_images[wxTreeItemIcon_Max];
204
205 wxTreeItemData *m_data;
206
207 // use bitfields to save size
208 int m_isCollapsed :1;
209 int m_hasHilight :1; // same as focused
210 int m_hasPlus :1; // used for item which doesn't have
211 // children but has a [+] button
212 int m_isBold :1; // render the label in bold font
213
6f2a55e3
VZ
214 wxCoord m_x, m_y;
215 wxCoord m_height, m_width;
9ec64fa7
VZ
216 int m_xCross, m_yCross;
217 int m_level;
218
219 wxArrayGenericTreeItems m_children;
220 wxGenericTreeItem *m_parent;
221
222 wxTreeItemAttr *m_attr;
f135ff73
VZ
223};
224
225// =============================================================================
226// implementation
227// =============================================================================
228
06b466c7
VZ
229// ----------------------------------------------------------------------------
230// private functions
231// ----------------------------------------------------------------------------
232
233// translate the key or mouse event flags to the type of selection we're
234// dealing with
235static void EventFlagsToSelType(long style,
236 bool shiftDown,
237 bool ctrlDown,
dfc6cd93
SB
238 bool &is_multiple,
239 bool &extended_select,
240 bool &unselect_others)
06b466c7 241{
dfc6cd93
SB
242 is_multiple = (style & wxTR_MULTIPLE) != 0;
243 extended_select = shiftDown && is_multiple;
244 unselect_others = !(extended_select || (ctrlDown && is_multiple));
06b466c7 245}
e179bd65
RR
246
247// -----------------------------------------------------------------------------
248// wxTreeRenameTimer (internal)
249// -----------------------------------------------------------------------------
250
251wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl *owner )
252{
253 m_owner = owner;
254}
255
256void wxTreeRenameTimer::Notify()
257{
258 m_owner->OnRenameTimer();
259}
260
261//-----------------------------------------------------------------------------
262// wxTreeTextCtrl (internal)
263//-----------------------------------------------------------------------------
264
265IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl,wxTextCtrl);
266
267BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
268 EVT_CHAR (wxTreeTextCtrl::OnChar)
269 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
270END_EVENT_TABLE()
271
674ac8b9
VZ
272wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent,
273 const wxWindowID id,
274 bool *accept,
275 wxString *res,
276 wxTreeCtrl *owner,
277 const wxString &value,
278 const wxPoint &pos,
279 const wxSize &size,
280 int style,
281 const wxValidator& validator,
282 const wxString &name )
283 : wxTextCtrl( parent, id, value, pos, size, style, validator, name )
e179bd65
RR
284{
285 m_res = res;
286 m_accept = accept;
287 m_owner = owner;
5f1ea0ee 288 (*m_accept) = FALSE;
3dbeaa52 289 (*m_res) = wxEmptyString;
5f1ea0ee 290 m_startValue = value;
e179bd65
RR
291}
292
293void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
294{
295 if (event.m_keyCode == WXK_RETURN)
296 {
297 (*m_accept) = TRUE;
298 (*m_res) = GetValue();
bce1406b
RR
299
300 if (!wxPendingDelete.Member(this))
301 wxPendingDelete.Append(this);
302
303 if ((*m_accept) && ((*m_res) != m_startValue))
304 m_owner->OnRenameAccept();
305
e179bd65
RR
306 return;
307 }
308 if (event.m_keyCode == WXK_ESCAPE)
309 {
310 (*m_accept) = FALSE;
311 (*m_res) = "";
bce1406b
RR
312
313 if (!wxPendingDelete.Member(this))
314 wxPendingDelete.Append(this);
315
e179bd65
RR
316 return;
317 }
318 event.Skip();
319}
320
321void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
322{
bce1406b
RR
323 if (!wxPendingDelete.Member(this))
324 wxPendingDelete.Append(this);
9dfbf520 325
5f1ea0ee
RR
326 if ((*m_accept) && ((*m_res) != m_startValue))
327 m_owner->OnRenameAccept();
e179bd65
RR
328}
329
f135ff73 330// -----------------------------------------------------------------------------
c801d85f 331// wxTreeEvent
f135ff73 332// -----------------------------------------------------------------------------
c801d85f 333
e179bd65 334IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
9dfbf520 335
f135ff73 336wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
92976ab6 337 : wxNotifyEvent( commandType, id )
c801d85f 338{
00e12320
RR
339 m_code = 0;
340 m_itemOld = (wxGenericTreeItem *)NULL;
edaa81ae 341}
c801d85f 342
f135ff73 343// -----------------------------------------------------------------------------
c801d85f 344// wxGenericTreeItem
f135ff73 345// -----------------------------------------------------------------------------
c801d85f 346
f135ff73
VZ
347wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
348 const wxString& text,
406005d2 349 wxDC& WXUNUSED(dc),
f135ff73
VZ
350 int image, int selImage,
351 wxTreeItemData *data)
352 : m_text(text)
c801d85f 353{
00e12320
RR
354 m_images[wxTreeItemIcon_Normal] = image;
355 m_images[wxTreeItemIcon_Selected] = selImage;
356 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
357 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
8dc99046 358
00e12320
RR
359 m_data = data;
360 m_x = m_y = 0;
361 m_xCross = m_yCross = 0;
f135ff73 362
00e12320 363 m_level = 0;
f135ff73 364
00e12320
RR
365 m_isCollapsed = TRUE;
366 m_hasHilight = FALSE;
367 m_hasPlus = FALSE;
368 m_isBold = FALSE;
c801d85f 369
00e12320 370 m_parent = parent;
f135ff73 371
00e12320 372 m_attr = (wxTreeItemAttr *)NULL;
06b466c7 373
00e12320
RR
374 // We don't know the height here yet.
375 m_width = 0;
376 m_height = 0;
edaa81ae 377}
c801d85f 378
f135ff73 379wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 380{
00e12320 381 delete m_data;
4832f7c0 382
00e12320 383 delete m_attr;
9ec64fa7 384
00e12320
RR
385 wxASSERT_MSG( m_children.IsEmpty(),
386 wxT("please call DeleteChildren() before deleting the item") );
372edb9d
VZ
387}
388
a43a4f9d 389void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree)
372edb9d 390{
00e12320
RR
391 size_t count = m_children.Count();
392 for ( size_t n = 0; n < count; n++ )
a43a4f9d 393 {
00e12320
RR
394 wxGenericTreeItem *child = m_children[n];
395 if (tree)
396 tree->SendDeleteEvent(child);
a43a4f9d 397
00e12320
RR
398 child->DeleteChildren(tree);
399 delete child;
400 }
372edb9d 401
00e12320 402 m_children.Empty();
edaa81ae 403}
c801d85f 404
91b8de8d 405void wxGenericTreeItem::SetText( const wxString &text )
c801d85f 406{
00e12320 407 m_text = text;
edaa81ae 408}
c801d85f 409
74bedbeb 410void wxGenericTreeItem::Reset()
c801d85f 411{
00e12320
RR
412 m_text.Empty();
413 for ( int i = 0; i < wxTreeItemIcon_Max; i++ )
414 {
415 m_images[i] = NO_IMAGE;
416 }
8dc99046 417
00e12320
RR
418 m_data = NULL;
419 m_x = m_y =
420 m_height = m_width = 0;
421 m_xCross =
422 m_yCross = 0;
74bedbeb 423
00e12320 424 m_level = 0;
c801d85f 425
00e12320
RR
426 DeleteChildren();
427 m_isCollapsed = TRUE;
c801d85f 428
00e12320 429 m_parent = (wxGenericTreeItem *)NULL;
edaa81ae 430}
c801d85f 431
4832f7c0 432size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 433{
00e12320
RR
434 size_t count = m_children.Count();
435 if ( !recursively )
436 return count;
4832f7c0 437
00e12320 438 size_t total = count;
f2593d0d 439 for (size_t n = 0; n < count; ++n)
00e12320
RR
440 {
441 total += m_children[n]->GetChildrenCount();
442 }
c801d85f 443
00e12320 444 return total;
edaa81ae 445}
c801d85f
KB
446
447void wxGenericTreeItem::SetCross( int x, int y )
448{
00e12320
RR
449 m_xCross = x;
450 m_yCross = y;
edaa81ae 451}
c801d85f 452
91b8de8d 453void wxGenericTreeItem::GetSize( int &x, int &y, const wxTreeCtrl *theTree )
c801d85f 454{
00e12320
RR
455 int bottomY=m_y+theTree->GetLineHeight(this);
456 if ( y < bottomY ) y = bottomY;
457 int width = m_x + m_width;
458 if ( x < width ) x = width;
f135ff73 459
00e12320 460 if (IsExpanded())
4832f7c0 461 {
00e12320
RR
462 size_t count = m_children.Count();
463 for ( size_t n = 0; n < count; ++n )
464 {
465 m_children[n]->GetSize( x, y, theTree );
466 }
df875e59 467 }
edaa81ae 468}
c801d85f 469
f135ff73 470wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
c193b707
VZ
471 const wxTreeCtrl *theTree,
472 int &flags)
c801d85f 473{
f2593d0d 474 if ((point.y > m_y) && (point.y < m_y + theTree->GetLineHeight(this)))
c801d85f 475 {
3dbeaa52 476 if (point.y < m_y+theTree->GetLineHeight(this)/2 )
f2593d0d
RR
477 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
478 else
479 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
978f38c2 480
f2593d0d
RR
481 // 5 is the size of the plus sign
482 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
483 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
484 (IsExpanded() || HasPlus()))
485 {
486 flags|=wxTREE_HITTEST_ONITEMBUTTON;
487 return this;
488 }
d3a9f4af 489
f2593d0d
RR
490 if ((point.x >= m_x) && (point.x <= m_x+m_width))
491 {
492 int image_w = -1;
493 int image_h;
0ae7f2a2 494
f2593d0d
RR
495 // assuming every image (normal and selected ) has the same size !
496 if ( (GetImage() != NO_IMAGE) && theTree->m_imageListNormal )
497 theTree->m_imageListNormal->GetSize(GetImage(), image_w, image_h);
91b8de8d 498
f2593d0d
RR
499 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
500 flags |= wxTREE_HITTEST_ONITEMICON;
501 else
502 flags |= wxTREE_HITTEST_ONITEMLABEL;
91b8de8d 503
f2593d0d
RR
504 return this;
505 }
91b8de8d 506
f2593d0d
RR
507 if (point.x < m_x)
508 flags |= wxTREE_HITTEST_ONITEMINDENT;
509 if (point.x > m_x+m_width)
510 flags |= wxTREE_HITTEST_ONITEMRIGHT;
511
512 return this;
513 }
514 else
c801d85f 515 {
f2593d0d
RR
516 if (!m_isCollapsed)
517 {
518 size_t count = m_children.Count();
519 for ( size_t n = 0; n < count; n++ )
520 {
521 wxGenericTreeItem *res = m_children[n]->HitTest( point, theTree, flags );
522 if ( res != NULL )
523 return res;
524 }
525 }
edaa81ae 526 }
f135ff73 527
f2593d0d 528 flags|=wxTREE_HITTEST_NOWHERE;
06b466c7 529
f2593d0d 530 return (wxGenericTreeItem*) NULL;
edaa81ae 531}
c801d85f 532
8dc99046
VZ
533int wxGenericTreeItem::GetCurrentImage() const
534{
535 int image = NO_IMAGE;
536 if ( IsExpanded() )
537 {
538 if ( IsSelected() )
539 {
540 image = GetImage(wxTreeItemIcon_SelectedExpanded);
541 }
542
543 if ( image == NO_IMAGE )
544 {
545 // we usually fall back to the normal item, but try just the
546 // expanded one (and not selected) first in this case
547 image = GetImage(wxTreeItemIcon_Expanded);
548 }
549 }
550 else // not expanded
551 {
552 if ( IsSelected() )
553 image = GetImage(wxTreeItemIcon_Selected);
554 }
555
556 // may be it doesn't have the specific image we want, try the default one
557 // instead
558 if ( image == NO_IMAGE )
559 {
560 image = GetImage();
561 }
562
563 return image;
564}
565
f135ff73
VZ
566// -----------------------------------------------------------------------------
567// wxTreeCtrl implementation
568// -----------------------------------------------------------------------------
569
570IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow)
571
572BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
00e12320
RR
573 EVT_PAINT (wxTreeCtrl::OnPaint)
574 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
575 EVT_CHAR (wxTreeCtrl::OnChar)
576 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
577 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
578 EVT_IDLE (wxTreeCtrl::OnIdle)
f135ff73
VZ
579END_EVENT_TABLE()
580
581// -----------------------------------------------------------------------------
582// construction/destruction
583// -----------------------------------------------------------------------------
91b8de8d 584
f135ff73 585void wxTreeCtrl::Init()
c801d85f 586{
00e12320
RR
587 m_current =
588 m_key_current =
589 m_anchor = (wxGenericTreeItem *) NULL;
590 m_hasFocus = FALSE;
591 m_dirty = FALSE;
f135ff73 592
00e12320
RR
593 m_xScroll = 0;
594 m_yScroll = 0;
595 m_lineHeight = 10;
596 m_indent = 15;
597 m_spacing = 18;
f135ff73 598
00e12320 599 m_hilightBrush = new wxBrush
f135ff73
VZ
600 (
601 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
602 wxSOLID
603 );
604
00e12320
RR
605 m_imageListNormal =
606 m_imageListState = (wxImageList *) NULL;
978f38c2 607
00e12320 608 m_dragCount = 0;
3dbeaa52 609 m_isDragging = FALSE;
b3a7510d
VZ
610 m_dropTarget =
611 m_oldSelection = (wxGenericTreeItem *)NULL;
9dfbf520 612
00e12320 613 m_renameTimer = new wxTreeRenameTimer( this );
f38374d0 614
00e12320
RR
615 m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT );
616 m_boldFont = wxFont( m_normalFont.GetPointSize(),
eff869aa
RR
617 m_normalFont.GetFamily(),
618 m_normalFont.GetStyle(),
619 wxBOLD,
620 m_normalFont.GetUnderlined());
edaa81ae 621}
c801d85f 622
f135ff73
VZ
623bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
624 const wxPoint& pos, const wxSize& size,
978f38c2 625 long style,
674ac8b9
VZ
626 const wxValidator &validator,
627 const wxString& name )
c801d85f 628{
00e12320 629 Init();
f135ff73 630
00e12320 631 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
978f38c2 632
ce4169a4 633#if wxUSE_VALIDATORS
00e12320 634 SetValidator( validator );
ce4169a4 635#endif
f135ff73 636
91fc2bdc 637 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
00e12320
RR
638// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
639 m_dottedPen = wxPen( "grey", 0, 0 );
f135ff73 640
00e12320 641 return TRUE;
edaa81ae 642}
c801d85f 643
f135ff73 644wxTreeCtrl::~wxTreeCtrl()
c801d85f 645{
00e12320 646 wxDELETE( m_hilightBrush );
a43a4f9d 647
00e12320 648 DeleteAllItems();
9dfbf520 649
00e12320 650 delete m_renameTimer;
edaa81ae 651}
c801d85f 652
f135ff73
VZ
653// -----------------------------------------------------------------------------
654// accessors
655// -----------------------------------------------------------------------------
656
657size_t wxTreeCtrl::GetCount() const
c801d85f 658{
f2593d0d 659 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
edaa81ae 660}
c801d85f 661
f135ff73 662void wxTreeCtrl::SetIndent(unsigned int indent)
c801d85f 663{
f2593d0d
RR
664 m_indent = indent;
665 m_dirty = TRUE;
cf724bce
RR
666}
667
668void wxTreeCtrl::SetSpacing(unsigned int spacing)
669{
f2593d0d
RR
670 m_spacing = spacing;
671 m_dirty = TRUE;
f135ff73 672}
74bedbeb 673
4832f7c0
VZ
674size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
675{
f2593d0d 676 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
4832f7c0 677
f2593d0d 678 return item.m_pItem->GetChildrenCount(recursively);
4832f7c0
VZ
679}
680
f135ff73
VZ
681// -----------------------------------------------------------------------------
682// functions to work with tree items
683// -----------------------------------------------------------------------------
74bedbeb 684
f135ff73
VZ
685wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
686{
f2593d0d 687 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
4832f7c0 688
f2593d0d 689 return item.m_pItem->GetText();
edaa81ae 690}
74bedbeb 691
8dc99046
VZ
692int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
693 wxTreeItemIcon which) const
74bedbeb 694{
f2593d0d 695 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 696
f2593d0d 697 return item.m_pItem->GetImage(which);
edaa81ae 698}
c801d85f 699
f135ff73 700wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 701{
f2593d0d 702 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 703
f2593d0d 704 return item.m_pItem->GetData();
edaa81ae 705}
c801d85f 706
f135ff73
VZ
707void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
708{
f2593d0d 709 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 710
f2593d0d
RR
711 wxClientDC dc(this);
712 wxGenericTreeItem *pItem = item.m_pItem;
713 pItem->SetText(text);
714 CalculateSize(pItem, dc);
715 RefreshLine(pItem);
f135ff73 716}
c801d85f 717
8dc99046
VZ
718void wxTreeCtrl::SetItemImage(const wxTreeItemId& item,
719 int image,
720 wxTreeItemIcon which)
f135ff73 721{
223d09f6 722 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 723
8dc99046
VZ
724 wxGenericTreeItem *pItem = item.m_pItem;
725 pItem->SetImage(image, which);
4832f7c0 726
8dc99046
VZ
727 wxClientDC dc(this);
728 CalculateSize(pItem, dc);
729 RefreshLine(pItem);
edaa81ae 730}
c801d85f 731
f135ff73 732void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 733{
f2593d0d 734 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 735
f2593d0d 736 item.m_pItem->SetData(data);
edaa81ae 737}
c801d85f 738
f135ff73 739void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 740{
f2593d0d 741 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 742
f2593d0d
RR
743 wxGenericTreeItem *pItem = item.m_pItem;
744 pItem->SetHasPlus(has);
745 RefreshLine(pItem);
edaa81ae 746}
c801d85f 747
ef44a621
VZ
748void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
749{
9ec64fa7 750 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 751
9ec64fa7
VZ
752 // avoid redrawing the tree if no real change
753 wxGenericTreeItem *pItem = item.m_pItem;
754 if ( pItem->IsBold() != bold )
755 {
756 pItem->SetBold(bold);
757 RefreshLine(pItem);
758 }
759}
760
761void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
762 const wxColour& col)
763{
764 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
765
766 wxGenericTreeItem *pItem = item.m_pItem;
767 pItem->Attr().SetTextColour(col);
768 RefreshLine(pItem);
769}
770
771void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
772 const wxColour& col)
773{
774 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
775
776 wxGenericTreeItem *pItem = item.m_pItem;
777 pItem->Attr().SetBackgroundColour(col);
778 RefreshLine(pItem);
779}
780
781void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
782{
783 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
784
785 wxGenericTreeItem *pItem = item.m_pItem;
786 pItem->Attr().SetFont(font);
ef44a621 787 RefreshLine(pItem);
ef44a621
VZ
788}
789
f135ff73
VZ
790// -----------------------------------------------------------------------------
791// item status inquiries
792// -----------------------------------------------------------------------------
793
df875e59 794bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
c801d85f 795{
f2593d0d 796 wxFAIL_MSG(wxT("not implemented"));
f135ff73 797
f2593d0d 798 return TRUE;
edaa81ae 799}
c801d85f 800
f135ff73 801bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 802{
f2593d0d 803 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 804
f2593d0d 805 return !item.m_pItem->GetChildren().IsEmpty();
edaa81ae 806}
c801d85f 807
f135ff73 808bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 809{
f2593d0d 810 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 811
f2593d0d 812 return item.m_pItem->IsExpanded();
f135ff73 813}
29d87bba 814
f135ff73
VZ
815bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
816{
f2593d0d 817 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 818
f2593d0d 819 return item.m_pItem->IsSelected();
f135ff73 820}
29d87bba 821
ef44a621
VZ
822bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
823{
f2593d0d 824 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
ef44a621 825
f2593d0d 826 return item.m_pItem->IsBold();
ef44a621
VZ
827}
828
f135ff73
VZ
829// -----------------------------------------------------------------------------
830// navigation
831// -----------------------------------------------------------------------------
29d87bba 832
f135ff73
VZ
833wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
834{
223d09f6 835 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 836
f135ff73
VZ
837 return item.m_pItem->GetParent();
838}
29d87bba 839
f135ff73
VZ
840wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
841{
223d09f6 842 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 843
f135ff73
VZ
844 cookie = 0;
845 return GetNextChild(item, cookie);
846}
29d87bba 847
f135ff73
VZ
848wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
849{
223d09f6 850 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 851
91b8de8d 852 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
4832f7c0
VZ
853 if ( (size_t)cookie < children.Count() )
854 {
06b466c7 855 return children.Item((size_t)cookie++);
4832f7c0
VZ
856 }
857 else
858 {
859 // there are no more of them
1e6d9499 860 return wxTreeItemId();
4832f7c0 861 }
f135ff73 862}
29d87bba 863
978f38c2
VZ
864wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
865{
223d09f6 866 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 867
91b8de8d 868 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
0a240683 869 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
870}
871
f135ff73
VZ
872wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
873{
223d09f6 874 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73
VZ
875
876 wxGenericTreeItem *i = item.m_pItem;
877 wxGenericTreeItem *parent = i->GetParent();
878 if ( parent == NULL )
879 {
880 // root item doesn't have any siblings
1e6d9499 881 return wxTreeItemId();
edaa81ae 882 }
4832f7c0 883
91b8de8d 884 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 885 int index = siblings.Index(i);
3c67202d 886 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 887
f135ff73 888 size_t n = (size_t)(index + 1);
fdd8d7b5 889 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 890}
c801d85f 891
f135ff73 892wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 893{
223d09f6 894 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73
VZ
895
896 wxGenericTreeItem *i = item.m_pItem;
897 wxGenericTreeItem *parent = i->GetParent();
898 if ( parent == NULL )
c801d85f 899 {
f135ff73 900 // root item doesn't have any siblings
1e6d9499 901 return wxTreeItemId();
edaa81ae 902 }
4832f7c0 903
91b8de8d 904 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 905 int index = siblings.Index(i);
3c67202d 906 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 907
fdd8d7b5
VZ
908 return index == 0 ? wxTreeItemId()
909 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 910}
389cdc7a 911
f135ff73
VZ
912wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
913{
223d09f6 914 wxFAIL_MSG(wxT("not implemented"));
29d87bba 915
1e6d9499 916 return wxTreeItemId();
f135ff73 917}
29d87bba 918
f135ff73
VZ
919wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
920{
223d09f6 921 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 922
223d09f6 923 wxFAIL_MSG(wxT("not implemented"));
29d87bba 924
1e6d9499 925 return wxTreeItemId();
f135ff73 926}
29d87bba 927
f135ff73
VZ
928wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
929{
223d09f6 930 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 931
223d09f6 932 wxFAIL_MSG(wxT("not implemented"));
29d87bba 933
1e6d9499 934 return wxTreeItemId();
edaa81ae 935}
c801d85f 936
f135ff73
VZ
937// -----------------------------------------------------------------------------
938// operations
939// -----------------------------------------------------------------------------
940
941wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
942 size_t previous,
943 const wxString& text,
944 int image, int selImage,
945 wxTreeItemData *data)
c801d85f 946{
f49f2b0c
RR
947 wxGenericTreeItem *parent = parentId.m_pItem;
948 if ( !parent )
949 {
950 // should we give a warning here?
951 return AddRoot(text, image, selImage, data);
952 }
4832f7c0 953
f49f2b0c 954 wxClientDC dc(this);
f38374d0 955 wxGenericTreeItem *item =
f49f2b0c 956 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
74bedbeb 957
f49f2b0c
RR
958 if ( data != NULL )
959 {
960 data->m_pItem = item;
961 }
74bedbeb 962
f49f2b0c 963 parent->Insert( item, previous );
ef44a621 964
f49f2b0c 965 m_dirty = TRUE;
389cdc7a 966
f49f2b0c 967 return item;
4c681997
RR
968}
969
f135ff73
VZ
970wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
971 int image, int selImage,
972 wxTreeItemData *data)
4c681997 973{
f49f2b0c 974 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 975
f49f2b0c
RR
976 wxClientDC dc(this);
977 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
f135ff73 978 image, selImage, data);
f49f2b0c
RR
979 if ( data != NULL )
980 {
981 data->m_pItem = m_anchor;
982 }
f38374d0 983
f49f2b0c
RR
984 if (!HasFlag(wxTR_MULTIPLE))
985 {
986 m_current = m_key_current = m_anchor;
06b466c7 987 m_current->SetHilight( TRUE );
f49f2b0c 988 }
389cdc7a 989
f49f2b0c
RR
990 Refresh();
991 AdjustMyScrollbars();
a32dd690 992
f49f2b0c 993 return m_anchor;
edaa81ae 994}
c801d85f 995
f135ff73
VZ
996wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
997 const wxString& text,
998 int image, int selImage,
999 wxTreeItemData *data)
c801d85f 1000{
f2593d0d 1001 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 1002}
c801d85f 1003
f135ff73
VZ
1004wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1005 const wxTreeItemId& idPrevious,
1006 const wxString& text,
1007 int image, int selImage,
1008 wxTreeItemData *data)
c801d85f 1009{
f2593d0d
RR
1010 wxGenericTreeItem *parent = parentId.m_pItem;
1011 if ( !parent )
1012 {
1013 // should we give a warning here?
1014 return AddRoot(text, image, selImage, data);
1015 }
c801d85f 1016
f2593d0d
RR
1017 int index = parent->GetChildren().Index(idPrevious.m_pItem);
1018 wxASSERT_MSG( index != wxNOT_FOUND,
1019 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
06b466c7 1020
f2593d0d
RR
1021 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1022}
1023
1024wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1025 size_t before,
1026 const wxString& text,
1027 int image, int selImage,
1028 wxTreeItemData *data)
1029{
1030 wxGenericTreeItem *parent = parentId.m_pItem;
1031 if ( !parent )
1032 {
1033 // should we give a warning here?
1034 return AddRoot(text, image, selImage, data);
1035 }
1036
1037 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 1038}
c801d85f 1039
f135ff73
VZ
1040wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
1041 const wxString& text,
1042 int image, int selImage,
1043 wxTreeItemData *data)
74bedbeb 1044{
f2593d0d
RR
1045 wxGenericTreeItem *parent = parentId.m_pItem;
1046 if ( !parent )
1047 {
1048 // should we give a warning here?
1049 return AddRoot(text, image, selImage, data);
1050 }
f135ff73 1051
f2593d0d
RR
1052 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1053 image, selImage, data);
74bedbeb
VZ
1054}
1055
a43a4f9d
VZ
1056void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1057{
f2593d0d
RR
1058 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
1059 event.m_item = item;
1060 event.SetEventObject( this );
1061 ProcessEvent( event );
a43a4f9d
VZ
1062}
1063
372edb9d
VZ
1064void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1065{
1066 wxGenericTreeItem *item = itemId.m_pItem;
a43a4f9d 1067 item->DeleteChildren(this);
372edb9d
VZ
1068
1069 m_dirty = TRUE;
1070}
1071
f135ff73 1072void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1073{
97d7bfb8 1074 wxGenericTreeItem *item = itemId.m_pItem;
ff5bf259 1075
aaa2f297
VZ
1076 // don't stay with invalid m_key_current or we will crash in the next call
1077 // to OnChar()
1078 bool changeKeyCurrent = FALSE;
1079 wxGenericTreeItem *itemKey = m_key_current;
1080 while ( itemKey && !changeKeyCurrent )
1081 {
1082 if ( itemKey == item )
1083 {
1084 // m_key_current is a descendant of the item being deleted
1085 changeKeyCurrent = TRUE;
1086 }
1087 else
1088 {
1089 itemKey = itemKey->GetParent();
1090 }
1091 }
1092
1093 wxGenericTreeItem *parent = item->GetParent();
97d7bfb8
RR
1094 if ( parent )
1095 {
1096 parent->GetChildren().Remove( item ); // remove by value
1097 }
f135ff73 1098
aaa2f297
VZ
1099 if ( changeKeyCurrent )
1100 {
1101 // may be NULL or not
1102 m_key_current = parent;
1103 }
1104
97d7bfb8
RR
1105 item->DeleteChildren(this);
1106 SendDeleteEvent(item);
1107 delete item;
f135ff73 1108
97d7bfb8 1109 m_dirty = TRUE;
edaa81ae 1110}
c801d85f 1111
f135ff73 1112void wxTreeCtrl::DeleteAllItems()
c801d85f 1113{
97d7bfb8
RR
1114 if ( m_anchor )
1115 {
1116 m_anchor->DeleteChildren(this);
1117 delete m_anchor;
a43a4f9d 1118
97d7bfb8 1119 m_anchor = NULL;
f135ff73 1120
97d7bfb8
RR
1121 m_dirty = TRUE;
1122 }
edaa81ae
RR
1123}
1124
f135ff73 1125void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1126{
f2593d0d 1127 wxGenericTreeItem *item = itemId.m_pItem;
f135ff73 1128
f2593d0d
RR
1129 if ( !item->HasPlus() )
1130 return;
978f38c2 1131
f2593d0d
RR
1132 if ( item->IsExpanded() )
1133 return;
f135ff73 1134
f2593d0d
RR
1135 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1136 event.m_item = item;
1137 event.SetEventObject( this );
004fd0c8 1138
d5a07b9e 1139// if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
f2593d0d
RR
1140 if ( ProcessEvent( event ) && !event.IsAllowed() )
1141 {
1142 // cancelled by program
1143 return;
1144 }
4832f7c0 1145
f2593d0d
RR
1146 item->Expand();
1147 CalculatePositions();
f135ff73 1148
f2593d0d 1149 RefreshSubtree(item);
f135ff73 1150
f2593d0d
RR
1151 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1152 ProcessEvent( event );
edaa81ae
RR
1153}
1154
f135ff73 1155void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1156{
f2593d0d 1157 wxGenericTreeItem *item = itemId.m_pItem;
f135ff73 1158
f2593d0d
RR
1159 if ( !item->IsExpanded() )
1160 return;
f135ff73 1161
f2593d0d
RR
1162 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1163 event.m_item = item;
1164 event.SetEventObject( this );
1165 if ( ProcessEvent( event ) && !event.IsAllowed() )
1166 {
1167 // cancelled by program
1168 return;
1169 }
4832f7c0 1170
f2593d0d 1171 item->Collapse();
f135ff73 1172
f2593d0d
RR
1173 wxArrayGenericTreeItems& children = item->GetChildren();
1174 size_t count = children.Count();
1175 for ( size_t n = 0; n < count; n++ )
1176 {
1177 Collapse(children[n]);
1178 }
f135ff73 1179
f2593d0d 1180 CalculatePositions();
f135ff73 1181
f2593d0d 1182 RefreshSubtree(item);
f135ff73 1183
f2593d0d
RR
1184 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1185 ProcessEvent( event );
edaa81ae 1186}
c801d85f 1187
f135ff73 1188void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1189{
00e12320
RR
1190 Collapse(item);
1191 DeleteChildren(item);
edaa81ae 1192}
c801d85f 1193
f135ff73 1194void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1195{
00e12320 1196 wxGenericTreeItem *item = itemId.m_pItem;
389cdc7a 1197
00e12320
RR
1198 if (item->IsExpanded())
1199 Collapse(itemId);
1200 else
1201 Expand(itemId);
f135ff73 1202}
389cdc7a 1203
f135ff73
VZ
1204void wxTreeCtrl::Unselect()
1205{
00e12320
RR
1206 if (m_current)
1207 {
1208 m_current->SetHilight( FALSE );
1209 RefreshLine( m_current );
1210 }
edaa81ae 1211}
c801d85f 1212
88ac883a 1213void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1214{
00e12320
RR
1215 if (item->IsSelected())
1216 {
1217 item->SetHilight(FALSE);
1218 RefreshLine(item);
1219 }
d3a9f4af 1220
00e12320 1221 if (item->HasChildren())
88ac883a 1222 {
00e12320
RR
1223 wxArrayGenericTreeItems& children = item->GetChildren();
1224 size_t count = children.Count();
1225 for ( size_t n = 0; n < count; ++n )
1226 {
1227 UnselectAllChildren(children[n]);
1228 }
88ac883a
VZ
1229 }
1230}
f135ff73 1231
88ac883a
VZ
1232void wxTreeCtrl::UnselectAll()
1233{
00e12320 1234 UnselectAllChildren(GetRootItem().m_pItem);
88ac883a
VZ
1235}
1236
1237// Recursive function !
1238// To stop we must have crt_item<last_item
91b8de8d 1239// Algorithm :
88ac883a 1240// Tag all next children, when no more children,
d3a9f4af 1241// Move to parent (not to tag)
91b8de8d 1242// Keep going... if we found last_item, we stop.
88ac883a
VZ
1243bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1244{
1245 wxGenericTreeItem *parent = crt_item->GetParent();
1246
00e12320
RR
1247 if (parent == NULL) // This is root item
1248 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1249
91b8de8d 1250 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1251 int index = children.Index(crt_item);
1252 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1253
1254 size_t count = children.Count();
1255 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1256 {
1257 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1258 }
88ac883a
VZ
1259
1260 return TagNextChildren(parent, last_item, select);
1261}
1262
1263bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1264{
00e12320
RR
1265 crt_item->SetHilight(select);
1266 RefreshLine(crt_item);
d3a9f4af 1267
06b466c7 1268 if (crt_item==last_item)
00e12320 1269 return TRUE;
88ac883a 1270
00e12320 1271 if (crt_item->HasChildren())
88ac883a 1272 {
00e12320
RR
1273 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1274 size_t count = children.Count();
1275 for ( size_t n = 0; n < count; ++n )
1276 {
06b466c7 1277 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1278 return TRUE;
06b466c7 1279 }
88ac883a 1280 }
d3a9f4af 1281
c0de7af4 1282 return FALSE;
88ac883a
VZ
1283}
1284
1285void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1286{
f2593d0d
RR
1287 // item2 is not necessary after item1
1288 wxGenericTreeItem *first=NULL, *last=NULL;
88ac883a 1289
f2593d0d
RR
1290 // choice first' and 'last' between item1 and item2
1291 if (item1->GetY()<item2->GetY())
06b466c7 1292 {
f2593d0d
RR
1293 first=item1;
1294 last=item2;
1295 }
1296 else
1297 {
1298 first=item2;
1299 last=item1;
1300 }
88ac883a 1301
f2593d0d 1302 bool select = m_current->IsSelected();
d3a9f4af 1303
f2593d0d
RR
1304 if ( TagAllChildrenUntilLast(first,last,select) )
1305 return;
88ac883a 1306
f2593d0d 1307 TagNextChildren(first,last,select);
88ac883a
VZ
1308}
1309
d3a9f4af 1310void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId,
c193b707
VZ
1311 bool unselect_others,
1312 bool extended_select)
d3a9f4af 1313{
223d09f6 1314 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1315
88ac883a 1316 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
5391f772 1317 wxGenericTreeItem *item = itemId.m_pItem;
88ac883a
VZ
1318
1319 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1320 // wxT("this is a single selection tree") );
88ac883a
VZ
1321
1322 // to keep going anyhow !!!
d3a9f4af 1323 if (is_single)
8dc99046
VZ
1324 {
1325 if (item->IsSelected())
1326 return; // nothing to do
1327 unselect_others = TRUE;
1328 extended_select = FALSE;
1329 }
1330 else if ( unselect_others && item->IsSelected() )
1331 {
1332 // selection change if there is more than one item currently selected
1333 wxArrayTreeItemIds selected_items;
1334 if ( GetSelections(selected_items) == 1 )
1335 return;
1336 }
d3a9f4af 1337
f135ff73
VZ
1338 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1339 event.m_item = item;
1340 event.m_itemOld = m_current;
1341 event.SetEventObject( this );
91b8de8d 1342 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1343
f98e2558 1344 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
f135ff73
VZ
1345 return;
1346
88ac883a
VZ
1347 // ctrl press
1348 if (unselect_others)
389cdc7a 1349 {
88ac883a 1350 if (is_single) Unselect(); // to speed up thing
c193b707 1351 else UnselectAll();
edaa81ae 1352 }
f135ff73 1353
88ac883a 1354 // shift press
d3a9f4af 1355 if (extended_select)
88ac883a 1356 {
aaa2f297
VZ
1357 if ( !m_current )
1358 {
1359 m_current =
1360 m_key_current = GetRootItem().m_pItem;
1361 }
1362
88ac883a
VZ
1363 // don't change the mark (m_current)
1364 SelectItemRange(m_current, item);
1365 }
1366 else
1367 {
c0de7af4 1368 bool select=TRUE; // the default
88ac883a 1369
c193b707
VZ
1370 // Check if we need to toggle hilight (ctrl mode)
1371 if (!unselect_others)
8dc99046 1372 select=!item->IsSelected();
88ac883a 1373
91b8de8d 1374 m_current = m_key_current = item;
c193b707
VZ
1375 m_current->SetHilight(select);
1376 RefreshLine( m_current );
88ac883a 1377 }
389cdc7a 1378
f135ff73 1379 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1380 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1381}
1382
9dfbf520
VZ
1383void wxTreeCtrl::FillArray(wxGenericTreeItem *item,
1384 wxArrayTreeItemIds &array) const
c801d85f 1385{
8dc99046 1386 if ( item->IsSelected() )
9dfbf520 1387 array.Add(wxTreeItemId(item));
91b8de8d 1388
9dfbf520 1389 if ( item->HasChildren() )
91b8de8d 1390 {
9dfbf520
VZ
1391 wxArrayGenericTreeItems& children = item->GetChildren();
1392 size_t count = children.GetCount();
1393 for ( size_t n = 0; n < count; ++n )
1394 FillArray(children[n],array);
91b8de8d
RR
1395 }
1396}
1397
1398size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1399{
1400 array.Empty();
1401 FillArray(GetRootItem().m_pItem, array);
1402
1403 return array.Count();
1404}
1405
1406void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1407{
91b8de8d
RR
1408 if (!item.IsOk()) return;
1409
0659e7ee 1410 wxGenericTreeItem *gitem = item.m_pItem;
ef44a621 1411
f65635b5
VZ
1412 // first expand all parent branches
1413 wxGenericTreeItem *parent = gitem->GetParent();
5391f772 1414 while ( parent )
f65635b5 1415 {
8dc99046 1416 Expand(parent);
f65635b5
VZ
1417 parent = parent->GetParent();
1418 }
1419
5391f772 1420 //if (parent) CalculatePositions();
91b8de8d
RR
1421
1422 ScrollTo(item);
1423}
1424
1425void wxTreeCtrl::ScrollTo(const wxTreeItemId &item)
1426{
1427 if (!item.IsOk()) return;
1428
dc6c62a9
RR
1429 // We have to call this here because the label in
1430 // question might just have been added and no screen
1431 // update taken place.
1432 if (m_dirty) wxYield();
1433
91b8de8d
RR
1434 wxGenericTreeItem *gitem = item.m_pItem;
1435
f65635b5 1436 // now scroll to the item
0659e7ee 1437 int item_y = gitem->GetY();
8dc99046 1438
0659e7ee
RR
1439 int start_x = 0;
1440 int start_y = 0;
1441 ViewStart( &start_x, &start_y );
91b8de8d 1442 start_y *= PIXELS_PER_UNIT;
978f38c2 1443
a93109d5
RR
1444 int client_h = 0;
1445 int client_w = 0;
1446 GetClientSize( &client_w, &client_h );
ef44a621 1447
0659e7ee
RR
1448 if (item_y < start_y+3)
1449 {
91b8de8d 1450 // going down
0659e7ee
RR
1451 int x = 0;
1452 int y = 0;
91b8de8d
RR
1453 m_anchor->GetSize( x, y, this );
1454 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1455 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1456 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1457 // Item should appear at top
91b8de8d 1458 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1459 }
91b8de8d 1460 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1461 {
c7a9fa36
RR
1462 // going up
1463 int x = 0;
1464 int y = 0;
1465 m_anchor->GetSize( x, y, this );
1466 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1467 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1468 item_y += PIXELS_PER_UNIT+2;
1469 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1470 // Item should appear at bottom
c7a9fa36 1471 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 1472 }
edaa81ae 1473}
c801d85f 1474
e1ee62bd
VZ
1475// FIXME: tree sorting functions are not reentrant and not MT-safe!
1476static wxTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1477
004fd0c8 1478static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1479 wxGenericTreeItem **item2)
edaa81ae 1480{
223d09f6 1481 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1482
1483 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1484}
1485
e1ee62bd
VZ
1486int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1487 const wxTreeItemId& item2)
0659e7ee 1488{
87138c52 1489 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1490}
1491
1492void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1493{
223d09f6 1494 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd
VZ
1495
1496 wxGenericTreeItem *item = itemId.m_pItem;
978f38c2 1497
e1ee62bd 1498 wxCHECK_RET( !s_treeBeingSorted,
223d09f6 1499 wxT("wxTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1500
91b8de8d 1501 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1502 if ( children.Count() > 1 )
1503 {
1504 s_treeBeingSorted = this;
1505 children.Sort(tree_ctrl_compare_func);
1506 s_treeBeingSorted = NULL;
978f38c2 1507
e1ee62bd
VZ
1508 m_dirty = TRUE;
1509 }
1510 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1511}
1512
f135ff73 1513wxImageList *wxTreeCtrl::GetImageList() const
edaa81ae 1514{
f135ff73 1515 return m_imageListNormal;
edaa81ae
RR
1516}
1517
f135ff73 1518wxImageList *wxTreeCtrl::GetStateImageList() const
c801d85f 1519{
f135ff73 1520 return m_imageListState;
edaa81ae 1521}
c801d85f 1522
f135ff73 1523void wxTreeCtrl::SetImageList(wxImageList *imageList)
e2414cbe 1524{
f2593d0d 1525 m_imageListNormal = imageList;
91b8de8d 1526
2c8e4738
VZ
1527 if ( !m_imageListNormal )
1528 return;
1529
f2593d0d
RR
1530 // Calculate a m_lineHeight value from the image sizes.
1531 // May be toggle off. Then wxTreeCtrl will spread when
1532 // necessary (which might look ugly).
f2593d0d
RR
1533 wxClientDC dc(this);
1534 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1535 int width = 0, height = 0,
1536 n = m_imageListNormal->GetImageCount();
06b466c7 1537
f2593d0d
RR
1538 for (int i = 0; i < n ; i++)
1539 {
1540 m_imageListNormal->GetSize(i, width, height);
1541 if (height > m_lineHeight) m_lineHeight = height;
1542 }
91b8de8d 1543
06b466c7 1544 if (m_lineHeight < 40)
f2593d0d 1545 m_lineHeight += 2; // at least 2 pixels
06b466c7 1546 else
f2593d0d 1547 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 1548}
e2414cbe 1549
f135ff73 1550void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1551{
f135ff73 1552 m_imageListState = imageList;
edaa81ae 1553}
e2414cbe 1554
f135ff73
VZ
1555// -----------------------------------------------------------------------------
1556// helpers
1557// -----------------------------------------------------------------------------
0659e7ee 1558
74bedbeb 1559void wxTreeCtrl::AdjustMyScrollbars()
c801d85f 1560{
0659e7ee
RR
1561 if (m_anchor)
1562 {
1563 int x = 0;
1564 int y = 0;
91b8de8d 1565 m_anchor->GetSize( x, y, this );
c193b707 1566 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1567 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1568 int x_pos = GetScrollPos( wxHORIZONTAL );
1569 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1570 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1571 }
1572 else
1573 {
1574 SetScrollbars( 0, 0, 0, 0 );
1575 }
edaa81ae 1576}
c801d85f 1577
91b8de8d
RR
1578int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1579{
dc6c62a9
RR
1580 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1581 return item->GetHeight();
1582 else
1583 return m_lineHeight;
91b8de8d
RR
1584}
1585
ef44a621
VZ
1586void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1587{
9ec64fa7
VZ
1588 wxTreeItemAttr *attr = item->GetAttributes();
1589 if ( attr && attr->HasFont() )
1590 dc.SetFont(attr->GetFont());
1591 else if (item->IsBold())
eff869aa 1592 dc.SetFont(m_boldFont);
ef44a621 1593
bbe0af5b
RR
1594 long text_w = 0;
1595 long text_h = 0;
1596 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1597
bbe0af5b
RR
1598 int image_h = 0;
1599 int image_w = 0;
8dc99046
VZ
1600 int image = item->GetCurrentImage();
1601 if ( image != NO_IMAGE )
bbe0af5b 1602 {
2c8e4738
VZ
1603 if ( m_imageListNormal )
1604 {
1605 m_imageListNormal->GetSize( image, image_w, image_h );
1606 image_w += 4;
1607 }
1608 else
1609 {
1610 image = NO_IMAGE;
1611 }
bbe0af5b 1612 }
ef44a621 1613
91b8de8d
RR
1614 int total_h = GetLineHeight(item);
1615
c0fba4d1
VS
1616 if (item->IsSelected())
1617 dc.SetBrush(*m_hilightBrush);
afa6a1a1 1618 else
c0fba4d1
VS
1619 {
1620 wxColour colBg;
1621 if ( attr && attr->HasBackgroundColour() )
1622 colBg = attr->GetBackgroundColour();
1623 else
1624 colBg = m_backgroundColour;
1625 dc.SetBrush(wxBrush(colBg, wxSOLID));
1626 }
afa6a1a1 1627
91b8de8d 1628 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
ef44a621 1629
8dc99046 1630 if ( image != NO_IMAGE )
bbe0af5b 1631 {
d30b4d20 1632 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 1633 m_imageListNormal->Draw( image, dc,
49cd56ef 1634 item->GetX(),
d701d432 1635 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1636 wxIMAGELIST_DRAW_TRANSPARENT );
1637 dc.DestroyClippingRegion();
1638 }
ef44a621 1639
afa6a1a1 1640 dc.SetBackgroundMode(wxTRANSPARENT);
6cedba09 1641 dc.DrawText( item->GetText(), image_w + item->GetX(), (wxCoord)item->GetY()
49cd56ef 1642 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
ef44a621 1643
eff869aa
RR
1644 // restore normal font
1645 dc.SetFont( m_normalFont );
ef44a621
VZ
1646}
1647
91b8de8d 1648// Now y stands for the top of the item, whereas it used to stand for middle !
16c1f7f3 1649void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1650{
bbe0af5b 1651 int horizX = level*m_indent;
389cdc7a 1652
cf724bce 1653 item->SetX( horizX+m_indent+m_spacing );
91b8de8d 1654 item->SetY( y );
4c681997 1655
bbe0af5b 1656 int oldY = y;
91b8de8d
RR
1657 y+=GetLineHeight(item)/2;
1658
1659 item->SetCross( horizX+m_indent, y );
389cdc7a 1660
bbe0af5b 1661 int exposed_x = dc.LogicalToDeviceX( 0 );
5391f772 1662 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
4832f7c0 1663
5391f772 1664 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
bbe0af5b
RR
1665 {
1666 int startX = horizX;
cf724bce 1667 int endX = horizX + (m_indent-5);
29d87bba 1668
cf724bce 1669// if (!item->HasChildren()) endX += (m_indent+5);
bbe0af5b 1670 if (!item->HasChildren()) endX += 20;
4832f7c0 1671
bbe0af5b 1672 dc.DrawLine( startX, y, endX, y );
29d87bba 1673
bbe0af5b
RR
1674 if (item->HasPlus())
1675 {
cf724bce 1676 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
bbe0af5b
RR
1677 dc.SetPen( *wxGREY_PEN );
1678 dc.SetBrush( *wxWHITE_BRUSH );
cf724bce 1679 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
9dfbf520 1680
bbe0af5b 1681 dc.SetPen( *wxBLACK_PEN );
cf724bce 1682 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
bbe0af5b 1683 if (!item->IsExpanded())
cf724bce 1684 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
9dfbf520 1685
112c5086 1686 dc.SetPen( m_dottedPen );
bbe0af5b 1687 }
c801d85f 1688
9ec64fa7 1689 wxPen *pen = wxTRANSPARENT_PEN;
9ec64fa7 1690 wxColour colText;
a367b9b3 1691
9ec64fa7
VZ
1692 if ( item->IsSelected() )
1693 {
1694 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
4832f7c0 1695
9ec64fa7
VZ
1696 if ( m_hasFocus )
1697 pen = wxBLACK_PEN;
f135ff73 1698
bbe0af5b
RR
1699 }
1700 else
1701 {
9ec64fa7
VZ
1702 wxTreeItemAttr *attr = item->GetAttributes();
1703 if ( attr && attr->HasTextColour() )
1704 colText = attr->GetTextColour();
1705 else
1706 colText = *wxBLACK;
bbe0af5b 1707 }
9ec64fa7
VZ
1708
1709 // prepare to draw
1710 dc.SetTextForeground(colText);
1711 dc.SetPen(*pen);
9ec64fa7
VZ
1712
1713 // draw
1714 PaintItem(item, dc);
1715
1716 // restore DC objects
1717 dc.SetBrush( *wxWHITE_BRUSH );
1718 dc.SetPen( m_dottedPen );
1719 dc.SetTextForeground( *wxBLACK );
f135ff73 1720 }
d3a9f4af 1721
91b8de8d 1722 y = oldY+GetLineHeight(item);
e2414cbe 1723
bbe0af5b
RR
1724 if (item->IsExpanded())
1725 {
91b8de8d 1726 oldY+=GetLineHeight(item)/2;
9ec64fa7 1727 int semiOldY=0;
389cdc7a 1728
91b8de8d
RR
1729 wxArrayGenericTreeItems& children = item->GetChildren();
1730 size_t n, count = children.Count();
1731 for ( n = 0; n < count; ++n )
c193b707
VZ
1732 {
1733 semiOldY=y;
1734 PaintLevel( children[n], dc, level+1, y );
1735 }
389cdc7a 1736
f65635b5
VZ
1737 // it may happen that the item is expanded but has no items (when you
1738 // delete all its children for example) - don't draw the vertical line
1739 // in this case
1740 if (count > 0)
9ec64fa7 1741 {
c193b707 1742 semiOldY+=GetLineHeight(children[--n])/2;
cf724bce 1743 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
9ec64fa7 1744 }
bbe0af5b 1745 }
4c681997 1746}
c801d85f 1747
3dbeaa52
VZ
1748void wxTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
1749{
1750 if ( item )
1751 {
1752 if ( item->HasPlus() )
1753 {
1754 // it's a folder, indicate it by a border
1755 DrawBorder(item);
1756 }
1757 else
1758 {
1759 // draw a line under the drop target because the item will be
1760 // dropped there
1761 DrawLine(item, TRUE /* below */);
1762 }
1763
1764 SetCursor(wxCURSOR_BULLSEYE);
1765 }
1766 else
1767 {
1768 // can't drop here
1769 SetCursor(wxCURSOR_NO_ENTRY);
1770 }
1771}
1772
1773void wxTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 1774{
3dbeaa52 1775 wxCHECK_RET( item.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") );
91b8de8d 1776
3dbeaa52 1777 wxGenericTreeItem *i = item.m_pItem;
91b8de8d 1778
1044a386 1779 wxClientDC dc(this);
91b8de8d
RR
1780 PrepareDC( dc );
1781 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 1782 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 1783
3dbeaa52
VZ
1784 int w = i->GetWidth() + 2;
1785 int h = GetLineHeight(i) + 2;
91b8de8d 1786
3dbeaa52 1787 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
1788}
1789
3dbeaa52 1790void wxTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 1791{
3dbeaa52 1792 wxCHECK_RET( item.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") );
91b8de8d 1793
3dbeaa52 1794 wxGenericTreeItem *i = item.m_pItem;
91b8de8d 1795
1044a386 1796 wxClientDC dc(this);
91b8de8d
RR
1797 PrepareDC( dc );
1798 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 1799
3dbeaa52
VZ
1800 int x = i->GetX(),
1801 y = i->GetY();
1802 if ( below )
1803 {
1804 y += GetLineHeight(i) - 1;
1805 }
91b8de8d 1806
3dbeaa52 1807 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
1808}
1809
f135ff73
VZ
1810// -----------------------------------------------------------------------------
1811// wxWindows callbacks
1812// -----------------------------------------------------------------------------
1813
3db7be80 1814void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 1815{
91b8de8d 1816 if ( !m_anchor)
0659e7ee 1817 return;
c801d85f 1818
0659e7ee
RR
1819 wxPaintDC dc(this);
1820 PrepareDC( dc );
29d87bba 1821
eff869aa 1822 dc.SetFont( m_normalFont );
0659e7ee 1823 dc.SetPen( m_dottedPen );
f38374d0 1824
eff869aa 1825 // this is now done dynamically
91b8de8d
RR
1826 //if(GetImageList() == NULL)
1827 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1828
91b8de8d 1829 int y = 2;
0659e7ee 1830 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 1831}
c801d85f 1832
3db7be80 1833void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1834{
0659e7ee 1835 m_hasFocus = TRUE;
978f38c2 1836
bbe0af5b 1837 if (m_current) RefreshLine( m_current );
edaa81ae 1838}
c801d85f 1839
3db7be80 1840void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1841{
0659e7ee 1842 m_hasFocus = FALSE;
978f38c2 1843
bbe0af5b 1844 if (m_current) RefreshLine( m_current );
edaa81ae 1845}
c801d85f
KB
1846
1847void wxTreeCtrl::OnChar( wxKeyEvent &event )
1848{
978f38c2 1849 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
06b466c7 1850 te.m_code = (int)event.KeyCode();
978f38c2
VZ
1851 te.SetEventObject( this );
1852 GetEventHandler()->ProcessEvent( te );
435fe83e 1853
91b8de8d 1854 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
1855 {
1856 event.Skip();
1857 return;
1858 }
ef44a621 1859
06b466c7
VZ
1860 // how should the selection work for this event?
1861 bool is_multiple, extended_select, unselect_others;
1862 EventFlagsToSelType(GetWindowStyleFlag(),
1863 event.ShiftDown(),
1864 event.ControlDown(),
dfc6cd93
SB
1865 is_multiple, extended_select, unselect_others);
1866
1867 // + : Expand
1868 // - : Collaspe
1869 // * : Toggle Expand/Collapse
1870 // ' ' | return : activate
1871 // up : go up (not last children!)
1872 // down : go down
1873 // left : go to parent
1874 // right : open if parent and go next
1875 // home : go to root
1876 // end : go to last item without opening parents
978f38c2
VZ
1877 switch (event.KeyCode())
1878 {
1879 case '+':
1880 case WXK_ADD:
1881 if (m_current->HasPlus() && !IsExpanded(m_current))
1882 {
1883 Expand(m_current);
1884 }
1885 break;
ef44a621 1886
978f38c2
VZ
1887 case '-':
1888 case WXK_SUBTRACT:
1889 if (IsExpanded(m_current))
1890 {
1891 Collapse(m_current);
1892 }
1893 break;
ef44a621 1894
978f38c2
VZ
1895 case '*':
1896 case WXK_MULTIPLY:
1897 Toggle(m_current);
1898 break;
ef44a621 1899
978f38c2
VZ
1900 case ' ':
1901 case WXK_RETURN:
1902 {
1903 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1904 event.m_item = m_current;
1905 event.m_code = 0;
1906 event.SetEventObject( this );
1907 GetEventHandler()->ProcessEvent( event );
1908 }
1909 break;
ef44a621 1910
978f38c2
VZ
1911 // up goes to the previous sibling or to the last of its children if
1912 // it's expanded
1913 case WXK_UP:
1914 {
91b8de8d 1915 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
1916 if (!prev)
1917 {
91b8de8d 1918 prev = GetParent( m_key_current );
c193b707
VZ
1919 if (prev)
1920 {
90e58684 1921 long cockie = 0;
91b8de8d 1922 wxTreeItemId current = m_key_current;
90e58684
RR
1923 if (current == GetFirstChild( prev, cockie ))
1924 {
1925 // otherwise we return to where we came from
88ac883a 1926 SelectItem( prev, unselect_others, extended_select );
c193b707
VZ
1927 m_key_current=prev.m_pItem;
1928 EnsureVisible( prev );
90e58684 1929 break;
c193b707 1930 }
978f38c2
VZ
1931 }
1932 }
1933 if (prev)
1934 {
69a282d4 1935 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 1936 {
69a282d4
VZ
1937 wxTreeItemId child = GetLastChild(prev);
1938 if ( child )
1939 {
1940 prev = child;
1941 }
978f38c2 1942 }
69a282d4 1943
88ac883a 1944 SelectItem( prev, unselect_others, extended_select );
c193b707 1945 m_key_current=prev.m_pItem;
978f38c2
VZ
1946 EnsureVisible( prev );
1947 }
1948 }
1949 break;
ef44a621 1950
978f38c2
VZ
1951 // left arrow goes to the parent
1952 case WXK_LEFT:
1953 {
1954 wxTreeItemId prev = GetParent( m_current );
1955 if (prev)
1956 {
1957 EnsureVisible( prev );
88ac883a 1958 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
1959 }
1960 }
1961 break;
ef44a621 1962
978f38c2
VZ
1963 case WXK_RIGHT:
1964 // this works the same as the down arrow except that we also expand the
1965 // item if it wasn't expanded yet
1966 Expand(m_current);
1967 // fall through
1968
1969 case WXK_DOWN:
ef44a621 1970 {
91b8de8d 1971 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
1972 {
1973 long cookie = 0;
91b8de8d 1974 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 1975 SelectItem( child, unselect_others, extended_select );
c193b707 1976 m_key_current=child.m_pItem;
978f38c2
VZ
1977 EnsureVisible( child );
1978 }
1979 else
1980 {
91b8de8d 1981 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 1982 if (!next)
978f38c2 1983 {
91b8de8d 1984 wxTreeItemId current = m_key_current;
978f38c2
VZ
1985 while (current && !next)
1986 {
1987 current = GetParent( current );
1988 if (current) next = GetNextSibling( current );
1989 }
1990 }
b62c3631 1991 if (next)
978f38c2 1992 {
88ac883a 1993 SelectItem( next, unselect_others, extended_select );
c193b707 1994 m_key_current=next.m_pItem;
978f38c2
VZ
1995 EnsureVisible( next );
1996 }
1997 }
ef44a621 1998 }
978f38c2 1999 break;
ef44a621 2000
978f38c2
VZ
2001 // <End> selects the last visible tree item
2002 case WXK_END:
2003 {
2004 wxTreeItemId last = GetRootItem();
2005
2006 while ( last.IsOk() && IsExpanded(last) )
2007 {
2008 wxTreeItemId lastChild = GetLastChild(last);
2009
2010 // it may happen if the item was expanded but then all of
2011 // its children have been deleted - so IsExpanded() returned
2012 // TRUE, but GetLastChild() returned invalid item
2013 if ( !lastChild )
2014 break;
2015
2016 last = lastChild;
2017 }
2018
2019 if ( last.IsOk() )
2020 {
2021 EnsureVisible( last );
88ac883a 2022 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2023 }
2024 }
2025 break;
2026
2027 // <Home> selects the root item
2028 case WXK_HOME:
2029 {
2030 wxTreeItemId prev = GetRootItem();
2031 if (prev)
2032 {
2033 EnsureVisible( prev );
88ac883a 2034 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2035 }
2036 }
2037 break;
2038
2039 default:
2040 event.Skip();
2041 }
edaa81ae 2042}
c801d85f 2043
91b8de8d 2044wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2045{
dc6c62a9
RR
2046 // We have to call this here because the label in
2047 // question might just have been added and no screen
2048 // update taken place.
2049 if (m_dirty) wxYield();
2050
67a7abf7
VZ
2051 wxClientDC dc(this);
2052 PrepareDC(dc);
06b466c7
VZ
2053 wxCoord x = dc.DeviceToLogicalX( point.x );
2054 wxCoord y = dc.DeviceToLogicalY( point.y );
91b8de8d
RR
2055 int w, h;
2056 GetSize(&w, &h);
2057
2058 flags=0;
2059 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
2060 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
2061 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
2062 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
d3a9f4af 2063
91b8de8d 2064 return m_anchor->HitTest( wxPoint(x, y), this, flags);
4f22cf8d
RR
2065}
2066
e179bd65
RR
2067/* **** */
2068
2069void wxTreeCtrl::Edit( const wxTreeItemId& item )
2070{
2071 if (!item.IsOk()) return;
2072
2073 m_currentEdit = item.m_pItem;
9dfbf520 2074
e179bd65
RR
2075 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
2076 te.m_item = m_currentEdit;
2077 te.SetEventObject( this );
2078 GetEventHandler()->ProcessEvent( te );
2079
2080 if (!te.IsAllowed()) return;
8dc99046 2081
dc6c62a9
RR
2082 // We have to call this here because the label in
2083 // question might just have been added and no screen
2084 // update taken place.
2085 if (m_dirty) wxYield();
9dfbf520 2086
e179bd65
RR
2087 wxString s = m_currentEdit->GetText();
2088 int x = m_currentEdit->GetX();
2089 int y = m_currentEdit->GetY();
2090 int w = m_currentEdit->GetWidth();
2091 int h = m_currentEdit->GetHeight();
9dfbf520 2092
5f1ea0ee
RR
2093 int image_h = 0;
2094 int image_w = 0;
8dc99046
VZ
2095
2096 int image = m_currentEdit->GetCurrentImage();
2097 if ( image != NO_IMAGE )
5f1ea0ee 2098 {
2c8e4738
VZ
2099 if ( m_imageListNormal )
2100 {
2101 m_imageListNormal->GetSize( image, image_w, image_h );
2102 image_w += 4;
2103 }
2104 else
2105 {
2106 wxFAIL_MSG(_T("you must create an image list to use images!"));
2107 }
5f1ea0ee
RR
2108 }
2109 x += image_w;
2110 w -= image_w + 4; // I don't know why +4 is needed
e179bd65
RR
2111
2112 wxClientDC dc(this);
2113 PrepareDC( dc );
2114 x = dc.LogicalToDeviceX( x );
2115 y = dc.LogicalToDeviceY( y );
2116
2117 wxTreeTextCtrl *text = new wxTreeTextCtrl(
2118 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
2119 text->SetFocus();
2120}
2121
2122void wxTreeCtrl::OnRenameTimer()
2123{
2124 Edit( m_current );
2125}
2126
2127void wxTreeCtrl::OnRenameAccept()
2128{
2129 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2130 le.m_item = m_currentEdit;
2131 le.SetEventObject( this );
2132 le.m_label = m_renameRes;
2133 GetEventHandler()->ProcessEvent( le );
9dfbf520 2134
e179bd65 2135 if (!le.IsAllowed()) return;
9dfbf520 2136
5f1ea0ee 2137 SetItemText( m_currentEdit, m_renameRes );
e179bd65 2138}
9dfbf520 2139
3db7be80 2140void wxTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2141{
bbe0af5b 2142 if ( !m_anchor ) return;
978f38c2 2143
3dbeaa52
VZ
2144 // we process left mouse up event (enables in-place edit), right down
2145 // (pass to the user code), left dbl click (activate item) and
2146 // dragging/moving events for items drag-and-drop
2147 if ( !(event.LeftUp() ||
2148 event.RightDown() ||
2149 event.LeftDClick() ||
2150 event.Dragging() ||
2151 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2152 {
2153 event.Skip();
2154
2155 return;
2156 }
2157
bbe0af5b
RR
2158 wxClientDC dc(this);
2159 PrepareDC(dc);
06b466c7
VZ
2160 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2161 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
29d87bba 2162
3dbeaa52 2163 int flags = 0;
91b8de8d 2164 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
3dbeaa52 2165
91b8de8d 2166 bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON;
978f38c2 2167
3dbeaa52 2168 if ( event.Dragging() && !m_isDragging )
bbe0af5b 2169 {
fd9811b1 2170 if (m_dragCount == 0)
8dc99046
VZ
2171 m_dragStart = wxPoint(x,y);
2172
fd9811b1 2173 m_dragCount++;
8dc99046 2174
3dbeaa52
VZ
2175 if (m_dragCount != 3)
2176 {
2177 // wait until user drags a bit further...
2178 return;
2179 }
8dc99046 2180
3dbeaa52
VZ
2181 wxEventType command = event.RightIsDown()
2182 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2183 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 2184
fd9811b1
RR
2185 wxTreeEvent nevent( command, GetId() );
2186 nevent.m_item = m_current;
2187 nevent.SetEventObject(this);
3dbeaa52
VZ
2188
2189 // by default the dragging is not supported, the user code must
2190 // explicitly allow the event for it to take place
2191 nevent.Veto();
2192
2193 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2194 {
2195 // we're going to drag this item
2196 m_isDragging = TRUE;
2197
b3a7510d
VZ
2198 // remember the old cursor because we will change it while
2199 // dragging
2200 m_oldCursor = m_cursor;
2201
2202 // in a single selection control, hide the selection temporarily
2203 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2204 {
2205 m_oldSelection = GetSelection().m_pItem;
2206
2207 if ( m_oldSelection )
2208 {
2209 m_oldSelection->SetHilight(FALSE);
2210 RefreshLine(m_oldSelection);
2211 }
2212 }
2213
3dbeaa52
VZ
2214 CaptureMouse();
2215 }
bbe0af5b 2216 }
3dbeaa52 2217 else if ( event.Moving() )
fd9811b1 2218 {
3dbeaa52
VZ
2219 if ( item != m_dropTarget )
2220 {
2221 // unhighlight the previous drop target
2222 DrawDropEffect(m_dropTarget);
fd9811b1 2223
3dbeaa52 2224 m_dropTarget = item;
978f38c2 2225
3dbeaa52
VZ
2226 // highlight the current drop target if any
2227 DrawDropEffect(m_dropTarget);
fb882e1c 2228
3dbeaa52
VZ
2229 wxYield();
2230 }
e179bd65 2231 }
3dbeaa52
VZ
2232 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2233 {
2234 // erase the highlighting
2235 DrawDropEffect(m_dropTarget);
9dfbf520 2236
3dbeaa52
VZ
2237 // generate the drag end event
2238 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 2239
3dbeaa52
VZ
2240 event.m_item = item;
2241 event.m_pointDrag = wxPoint(x, y);
2242 event.SetEventObject(this);
91b8de8d 2243
3dbeaa52 2244 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 2245
3dbeaa52
VZ
2246 m_isDragging = FALSE;
2247 m_dropTarget = (wxGenericTreeItem *)NULL;
2248
b3a7510d
VZ
2249 if ( m_oldSelection )
2250 {
2251 m_oldSelection->SetHilight(TRUE);
2252 RefreshLine(m_oldSelection);
2253 m_oldSelection = (wxGenericTreeItem *)NULL;
2254 }
2255
3dbeaa52
VZ
2256 ReleaseMouse();
2257
b3a7510d 2258 SetCursor(m_oldCursor);
3dbeaa52
VZ
2259
2260 wxYield();
2261 }
2262 else
bbe0af5b 2263 {
3dbeaa52
VZ
2264 // here we process only the messages which happen on tree items
2265
2266 m_dragCount = 0;
2267
2268 if (item == NULL) return; /* we hit the blank area */
2269
2270 if ( event.RightDown() )
2271 {
2272 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
2273 nevent.m_item = item;
2274 nevent.m_code = 0;
2275 nevent.SetEventObject(this);
2276 GetEventHandler()->ProcessEvent(nevent);
2277 }
2278 else if ( event.LeftUp() && (item == m_current) &&
2279 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2280 HasFlag(wxTR_EDIT_LABELS) )
2281 {
2282 m_renameTimer->Start( 100, TRUE );
2283 }
2284 else
2285 {
2286 // how should the selection work for this event?
2287 bool is_multiple, extended_select, unselect_others;
2288 EventFlagsToSelType(GetWindowStyleFlag(),
2289 event.ShiftDown(),
2290 event.ControlDown(),
dfc6cd93 2291 is_multiple, extended_select, unselect_others);
3dbeaa52
VZ
2292
2293 if ( onButton )
2294 {
2295 Toggle( item );
2296 if ( is_multiple )
2297 return;
2298 }
2299
2300 SelectItem(item, unselect_others, extended_select);
2301
2302 if ( event.LeftDClick() )
2303 {
2304 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2305 nevent.m_item = item;
2306 nevent.m_code = 0;
2307 nevent.SetEventObject( this );
2308 GetEventHandler()->ProcessEvent( nevent );
2309 }
2310 }
bbe0af5b 2311 }
edaa81ae 2312}
c801d85f 2313
3db7be80
RR
2314void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2315{
bbe0af5b
RR
2316 /* after all changes have been done to the tree control,
2317 * we actually redraw the tree when everything is over */
ef44a621 2318
f65635b5
VZ
2319 if (!m_dirty)
2320 return;
ef44a621 2321
bbe0af5b 2322 m_dirty = FALSE;
3db7be80 2323
bbe0af5b 2324 CalculatePositions();
91b8de8d 2325 Refresh();
bbe0af5b 2326 AdjustMyScrollbars();
3db7be80
RR
2327}
2328
91b8de8d 2329void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 2330{
e06b9569
JS
2331 wxCoord text_w = 0;
2332 wxCoord text_h = 0;
9dfbf520 2333
a62867a5 2334 if (item->IsBold())
eff869aa 2335 dc.SetFont(m_boldFont);
9dfbf520 2336
91b8de8d 2337 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 2338 text_h+=2;
91b8de8d 2339
eff869aa
RR
2340 // restore normal font
2341 dc.SetFont( m_normalFont );
9dfbf520 2342
91b8de8d
RR
2343 int image_h = 0;
2344 int image_w = 0;
8dc99046
VZ
2345 int image = item->GetCurrentImage();
2346 if ( image != NO_IMAGE )
91b8de8d 2347 {
2c8e4738
VZ
2348 if ( m_imageListNormal )
2349 {
2350 m_imageListNormal->GetSize( image, image_w, image_h );
2351 image_w += 4;
2352 }
91b8de8d
RR
2353 }
2354
2355 int total_h = (image_h > text_h) ? image_h : text_h;
2356
06b466c7 2357 if (total_h < 40)
f2593d0d 2358 total_h += 2; // at least 2 pixels
06b466c7 2359 else
f2593d0d 2360 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
2361
2362 item->SetHeight(total_h);
6cedba09
VZ
2363 if (total_h>m_lineHeight)
2364 m_lineHeight=total_h;
bbe0af5b 2365
91b8de8d
RR
2366 item->SetWidth(image_w+text_w+2);
2367}
2368
2369// -----------------------------------------------------------------------------
2370// for developper : y is now the top of the level
2371// not the middle of it !
bbe0af5b 2372void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2373{
bbe0af5b 2374 int horizX = level*m_indent;
389cdc7a 2375
91b8de8d 2376 CalculateSize( item, dc );
d3a9f4af 2377
91b8de8d 2378 // set its position
cf724bce 2379 item->SetX( horizX+m_indent+m_spacing );
91b8de8d
RR
2380 item->SetY( y );
2381 y+=GetLineHeight(item);
4c681997 2382
bbe0af5b
RR
2383 if ( !item->IsExpanded() )
2384 {
f65635b5 2385 // we dont need to calculate collapsed branches
bbe0af5b
RR
2386 return;
2387 }
389cdc7a 2388
91b8de8d
RR
2389 wxArrayGenericTreeItems& children = item->GetChildren();
2390 size_t n, count = children.Count();
2391 for (n = 0; n < count; ++n )
f2593d0d 2392 CalculateLevel( children[n], dc, level+1, y ); // recurse
edaa81ae 2393}
c801d85f 2394
74bedbeb 2395void wxTreeCtrl::CalculatePositions()
c801d85f 2396{
bbe0af5b 2397 if ( !m_anchor ) return;
29d87bba 2398
bbe0af5b
RR
2399 wxClientDC dc(this);
2400 PrepareDC( dc );
29d87bba 2401
eff869aa 2402 dc.SetFont( m_normalFont );
29d87bba 2403
bbe0af5b 2404 dc.SetPen( m_dottedPen );
91b8de8d
RR
2405 //if(GetImageList() == NULL)
2406 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2407
8dc99046 2408 int y = 2;
f65635b5 2409 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 2410}
c801d85f 2411
f135ff73 2412void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 2413{
e6527f9d
RR
2414 if (m_dirty) return;
2415
bbe0af5b
RR
2416 wxClientDC dc(this);
2417 PrepareDC(dc);
4832f7c0 2418
bbe0af5b
RR
2419 int cw = 0;
2420 int ch = 0;
2421 GetClientSize( &cw, &ch );
4832f7c0 2422
bbe0af5b
RR
2423 wxRect rect;
2424 rect.x = dc.LogicalToDeviceX( 0 );
2425 rect.width = cw;
2426 rect.y = dc.LogicalToDeviceY( item->GetY() );
2427 rect.height = ch;
f135ff73 2428
bbe0af5b 2429 Refresh( TRUE, &rect );
f135ff73 2430
bbe0af5b 2431 AdjustMyScrollbars();
edaa81ae 2432}
c801d85f
KB
2433
2434void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2435{
e6527f9d
RR
2436 if (m_dirty) return;
2437
bbe0af5b
RR
2438 wxClientDC dc(this);
2439 PrepareDC( dc );
2440
5391f772
SB
2441 int cw = 0;
2442 int ch = 0;
2443 GetClientSize( &cw, &ch );
2444
bbe0af5b 2445 wxRect rect;
5391f772
SB
2446 rect.x = dc.LogicalToDeviceX( 0 );
2447 rect.y = dc.LogicalToDeviceY( item->GetY() );
2448 rect.width = cw;
91b8de8d 2449 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 2450
bbe0af5b 2451 Refresh( TRUE, &rect );
edaa81ae 2452}
c801d85f 2453