]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
fixes for trailing commas for OS/2 from WX_2_2_BRANCH
[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 );
f6bcfd97 614 m_lastOnSame = FALSE;
f38374d0 615
00e12320
RR
616 m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT );
617 m_boldFont = wxFont( m_normalFont.GetPointSize(),
eff869aa
RR
618 m_normalFont.GetFamily(),
619 m_normalFont.GetStyle(),
620 wxBOLD,
621 m_normalFont.GetUnderlined());
edaa81ae 622}
c801d85f 623
f135ff73
VZ
624bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
625 const wxPoint& pos, const wxSize& size,
978f38c2 626 long style,
674ac8b9
VZ
627 const wxValidator &validator,
628 const wxString& name )
c801d85f 629{
00e12320 630 Init();
f135ff73 631
00e12320 632 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
978f38c2 633
ce4169a4 634#if wxUSE_VALIDATORS
00e12320 635 SetValidator( validator );
ce4169a4 636#endif
f135ff73 637
91fc2bdc 638 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
00e12320
RR
639// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
640 m_dottedPen = wxPen( "grey", 0, 0 );
f135ff73 641
00e12320 642 return TRUE;
edaa81ae 643}
c801d85f 644
f135ff73 645wxTreeCtrl::~wxTreeCtrl()
c801d85f 646{
00e12320 647 wxDELETE( m_hilightBrush );
a43a4f9d 648
00e12320 649 DeleteAllItems();
9dfbf520 650
00e12320 651 delete m_renameTimer;
edaa81ae 652}
c801d85f 653
f135ff73
VZ
654// -----------------------------------------------------------------------------
655// accessors
656// -----------------------------------------------------------------------------
657
658size_t wxTreeCtrl::GetCount() const
c801d85f 659{
f2593d0d 660 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
edaa81ae 661}
c801d85f 662
f135ff73 663void wxTreeCtrl::SetIndent(unsigned int indent)
c801d85f 664{
f2593d0d
RR
665 m_indent = indent;
666 m_dirty = TRUE;
cf724bce
RR
667}
668
669void wxTreeCtrl::SetSpacing(unsigned int spacing)
670{
f2593d0d
RR
671 m_spacing = spacing;
672 m_dirty = TRUE;
f135ff73 673}
74bedbeb 674
4832f7c0
VZ
675size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
676{
f2593d0d 677 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
4832f7c0 678
f2593d0d 679 return item.m_pItem->GetChildrenCount(recursively);
4832f7c0
VZ
680}
681
f135ff73
VZ
682// -----------------------------------------------------------------------------
683// functions to work with tree items
684// -----------------------------------------------------------------------------
74bedbeb 685
f135ff73
VZ
686wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
687{
f2593d0d 688 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
4832f7c0 689
f2593d0d 690 return item.m_pItem->GetText();
edaa81ae 691}
74bedbeb 692
8dc99046
VZ
693int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
694 wxTreeItemIcon which) const
74bedbeb 695{
f2593d0d 696 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 697
f2593d0d 698 return item.m_pItem->GetImage(which);
edaa81ae 699}
c801d85f 700
f135ff73 701wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 702{
f2593d0d 703 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 704
f2593d0d 705 return item.m_pItem->GetData();
edaa81ae 706}
c801d85f 707
f135ff73
VZ
708void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
709{
f2593d0d 710 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 711
f2593d0d
RR
712 wxClientDC dc(this);
713 wxGenericTreeItem *pItem = item.m_pItem;
714 pItem->SetText(text);
715 CalculateSize(pItem, dc);
716 RefreshLine(pItem);
f135ff73 717}
c801d85f 718
8dc99046
VZ
719void wxTreeCtrl::SetItemImage(const wxTreeItemId& item,
720 int image,
721 wxTreeItemIcon which)
f135ff73 722{
223d09f6 723 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 724
8dc99046
VZ
725 wxGenericTreeItem *pItem = item.m_pItem;
726 pItem->SetImage(image, which);
4832f7c0 727
8dc99046
VZ
728 wxClientDC dc(this);
729 CalculateSize(pItem, dc);
730 RefreshLine(pItem);
edaa81ae 731}
c801d85f 732
f135ff73 733void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 734{
f2593d0d 735 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 736
f2593d0d 737 item.m_pItem->SetData(data);
edaa81ae 738}
c801d85f 739
f135ff73 740void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 741{
f2593d0d 742 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 743
f2593d0d
RR
744 wxGenericTreeItem *pItem = item.m_pItem;
745 pItem->SetHasPlus(has);
746 RefreshLine(pItem);
edaa81ae 747}
c801d85f 748
ef44a621
VZ
749void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
750{
9ec64fa7 751 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 752
9ec64fa7
VZ
753 // avoid redrawing the tree if no real change
754 wxGenericTreeItem *pItem = item.m_pItem;
755 if ( pItem->IsBold() != bold )
756 {
757 pItem->SetBold(bold);
758 RefreshLine(pItem);
759 }
760}
761
762void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
763 const wxColour& col)
764{
765 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
766
767 wxGenericTreeItem *pItem = item.m_pItem;
768 pItem->Attr().SetTextColour(col);
769 RefreshLine(pItem);
770}
771
772void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
773 const wxColour& col)
774{
775 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
776
777 wxGenericTreeItem *pItem = item.m_pItem;
778 pItem->Attr().SetBackgroundColour(col);
779 RefreshLine(pItem);
780}
781
782void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
783{
784 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
785
786 wxGenericTreeItem *pItem = item.m_pItem;
787 pItem->Attr().SetFont(font);
ef44a621 788 RefreshLine(pItem);
ef44a621
VZ
789}
790
f135ff73
VZ
791// -----------------------------------------------------------------------------
792// item status inquiries
793// -----------------------------------------------------------------------------
794
df875e59 795bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
c801d85f 796{
f2593d0d 797 wxFAIL_MSG(wxT("not implemented"));
f135ff73 798
f2593d0d 799 return TRUE;
edaa81ae 800}
c801d85f 801
f135ff73 802bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 803{
f2593d0d 804 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 805
f2593d0d 806 return !item.m_pItem->GetChildren().IsEmpty();
edaa81ae 807}
c801d85f 808
f135ff73 809bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 810{
f2593d0d 811 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 812
f2593d0d 813 return item.m_pItem->IsExpanded();
f135ff73 814}
29d87bba 815
f135ff73
VZ
816bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
817{
f2593d0d 818 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 819
f2593d0d 820 return item.m_pItem->IsSelected();
f135ff73 821}
29d87bba 822
ef44a621
VZ
823bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
824{
f2593d0d 825 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
ef44a621 826
f2593d0d 827 return item.m_pItem->IsBold();
ef44a621
VZ
828}
829
f135ff73
VZ
830// -----------------------------------------------------------------------------
831// navigation
832// -----------------------------------------------------------------------------
29d87bba 833
f135ff73
VZ
834wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
835{
223d09f6 836 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 837
f135ff73
VZ
838 return item.m_pItem->GetParent();
839}
29d87bba 840
f135ff73
VZ
841wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
842{
223d09f6 843 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 844
f135ff73
VZ
845 cookie = 0;
846 return GetNextChild(item, cookie);
847}
29d87bba 848
f135ff73
VZ
849wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
850{
223d09f6 851 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 852
91b8de8d 853 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
4832f7c0
VZ
854 if ( (size_t)cookie < children.Count() )
855 {
06b466c7 856 return children.Item((size_t)cookie++);
4832f7c0
VZ
857 }
858 else
859 {
860 // there are no more of them
1e6d9499 861 return wxTreeItemId();
4832f7c0 862 }
f135ff73 863}
29d87bba 864
978f38c2
VZ
865wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
866{
223d09f6 867 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 868
91b8de8d 869 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
0a240683 870 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
871}
872
f135ff73
VZ
873wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
874{
223d09f6 875 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73
VZ
876
877 wxGenericTreeItem *i = item.m_pItem;
878 wxGenericTreeItem *parent = i->GetParent();
879 if ( parent == NULL )
880 {
881 // root item doesn't have any siblings
1e6d9499 882 return wxTreeItemId();
edaa81ae 883 }
4832f7c0 884
91b8de8d 885 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 886 int index = siblings.Index(i);
3c67202d 887 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 888
f135ff73 889 size_t n = (size_t)(index + 1);
fdd8d7b5 890 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 891}
c801d85f 892
f135ff73 893wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 894{
223d09f6 895 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73
VZ
896
897 wxGenericTreeItem *i = item.m_pItem;
898 wxGenericTreeItem *parent = i->GetParent();
899 if ( parent == NULL )
c801d85f 900 {
f135ff73 901 // root item doesn't have any siblings
1e6d9499 902 return wxTreeItemId();
edaa81ae 903 }
4832f7c0 904
91b8de8d 905 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 906 int index = siblings.Index(i);
3c67202d 907 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 908
fdd8d7b5
VZ
909 return index == 0 ? wxTreeItemId()
910 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 911}
389cdc7a 912
f135ff73
VZ
913wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
914{
223d09f6 915 wxFAIL_MSG(wxT("not implemented"));
29d87bba 916
1e6d9499 917 return wxTreeItemId();
f135ff73 918}
29d87bba 919
f135ff73
VZ
920wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
921{
223d09f6 922 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 923
223d09f6 924 wxFAIL_MSG(wxT("not implemented"));
29d87bba 925
1e6d9499 926 return wxTreeItemId();
f135ff73 927}
29d87bba 928
f135ff73
VZ
929wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
930{
223d09f6 931 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 932
223d09f6 933 wxFAIL_MSG(wxT("not implemented"));
29d87bba 934
1e6d9499 935 return wxTreeItemId();
edaa81ae 936}
c801d85f 937
f135ff73
VZ
938// -----------------------------------------------------------------------------
939// operations
940// -----------------------------------------------------------------------------
941
942wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
943 size_t previous,
944 const wxString& text,
945 int image, int selImage,
946 wxTreeItemData *data)
c801d85f 947{
f49f2b0c
RR
948 wxGenericTreeItem *parent = parentId.m_pItem;
949 if ( !parent )
950 {
951 // should we give a warning here?
952 return AddRoot(text, image, selImage, data);
953 }
4832f7c0 954
f49f2b0c 955 wxClientDC dc(this);
f38374d0 956 wxGenericTreeItem *item =
f49f2b0c 957 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
74bedbeb 958
f49f2b0c
RR
959 if ( data != NULL )
960 {
961 data->m_pItem = item;
962 }
74bedbeb 963
f49f2b0c 964 parent->Insert( item, previous );
ef44a621 965
f49f2b0c 966 m_dirty = TRUE;
389cdc7a 967
f49f2b0c 968 return item;
4c681997
RR
969}
970
f135ff73
VZ
971wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
972 int image, int selImage,
973 wxTreeItemData *data)
4c681997 974{
f49f2b0c 975 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 976
f49f2b0c
RR
977 wxClientDC dc(this);
978 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
f135ff73 979 image, selImage, data);
f49f2b0c
RR
980 if ( data != NULL )
981 {
982 data->m_pItem = m_anchor;
983 }
f38374d0 984
f49f2b0c
RR
985 if (!HasFlag(wxTR_MULTIPLE))
986 {
987 m_current = m_key_current = m_anchor;
06b466c7 988 m_current->SetHilight( TRUE );
f49f2b0c 989 }
389cdc7a 990
f6bcfd97 991 m_dirty = TRUE;
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
f6bcfd97
BP
1129 wxCHECK_RET( item, _T("invalid item in wxTreeCtrl::Expand") );
1130
f2593d0d
RR
1131 if ( !item->HasPlus() )
1132 return;
978f38c2 1133
f2593d0d
RR
1134 if ( item->IsExpanded() )
1135 return;
f135ff73 1136
f2593d0d
RR
1137 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1138 event.m_item = item;
1139 event.SetEventObject( this );
004fd0c8 1140
f2593d0d
RR
1141 if ( ProcessEvent( event ) && !event.IsAllowed() )
1142 {
1143 // cancelled by program
1144 return;
1145 }
4832f7c0 1146
f2593d0d
RR
1147 item->Expand();
1148 CalculatePositions();
f135ff73 1149
f2593d0d 1150 RefreshSubtree(item);
f135ff73 1151
f2593d0d
RR
1152 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1153 ProcessEvent( event );
edaa81ae
RR
1154}
1155
f6bcfd97
BP
1156void wxTreeCtrl::ExpandAll(const wxTreeItemId& item)
1157{
1158 Expand(item);
1159 if ( IsExpanded(item) )
1160 {
1161 long cookie;
1162 wxTreeItemId child = GetFirstChild(item, cookie);
1163 while ( child.IsOk() )
1164 {
1165 ExpandAll(child);
1166
1167 child = GetNextChild(item, cookie);
1168 }
1169 }
1170}
1171
f135ff73 1172void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1173{
f2593d0d 1174 wxGenericTreeItem *item = itemId.m_pItem;
f135ff73 1175
f2593d0d
RR
1176 if ( !item->IsExpanded() )
1177 return;
f135ff73 1178
f2593d0d
RR
1179 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1180 event.m_item = item;
1181 event.SetEventObject( this );
1182 if ( ProcessEvent( event ) && !event.IsAllowed() )
1183 {
1184 // cancelled by program
1185 return;
1186 }
4832f7c0 1187
f2593d0d 1188 item->Collapse();
f135ff73 1189
f2593d0d
RR
1190 wxArrayGenericTreeItems& children = item->GetChildren();
1191 size_t count = children.Count();
1192 for ( size_t n = 0; n < count; n++ )
1193 {
1194 Collapse(children[n]);
1195 }
f135ff73 1196
f2593d0d 1197 CalculatePositions();
f135ff73 1198
f2593d0d 1199 RefreshSubtree(item);
f135ff73 1200
f2593d0d
RR
1201 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1202 ProcessEvent( event );
edaa81ae 1203}
c801d85f 1204
f135ff73 1205void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1206{
00e12320
RR
1207 Collapse(item);
1208 DeleteChildren(item);
edaa81ae 1209}
c801d85f 1210
f135ff73 1211void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1212{
00e12320 1213 wxGenericTreeItem *item = itemId.m_pItem;
389cdc7a 1214
00e12320
RR
1215 if (item->IsExpanded())
1216 Collapse(itemId);
1217 else
1218 Expand(itemId);
f135ff73 1219}
389cdc7a 1220
f135ff73
VZ
1221void wxTreeCtrl::Unselect()
1222{
00e12320
RR
1223 if (m_current)
1224 {
1225 m_current->SetHilight( FALSE );
1226 RefreshLine( m_current );
1227 }
edaa81ae 1228}
c801d85f 1229
88ac883a 1230void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1231{
00e12320
RR
1232 if (item->IsSelected())
1233 {
1234 item->SetHilight(FALSE);
1235 RefreshLine(item);
1236 }
d3a9f4af 1237
00e12320 1238 if (item->HasChildren())
88ac883a 1239 {
00e12320
RR
1240 wxArrayGenericTreeItems& children = item->GetChildren();
1241 size_t count = children.Count();
1242 for ( size_t n = 0; n < count; ++n )
1243 {
1244 UnselectAllChildren(children[n]);
1245 }
88ac883a
VZ
1246 }
1247}
f135ff73 1248
88ac883a
VZ
1249void wxTreeCtrl::UnselectAll()
1250{
00e12320 1251 UnselectAllChildren(GetRootItem().m_pItem);
88ac883a
VZ
1252}
1253
1254// Recursive function !
1255// To stop we must have crt_item<last_item
91b8de8d 1256// Algorithm :
88ac883a 1257// Tag all next children, when no more children,
d3a9f4af 1258// Move to parent (not to tag)
91b8de8d 1259// Keep going... if we found last_item, we stop.
88ac883a
VZ
1260bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1261{
1262 wxGenericTreeItem *parent = crt_item->GetParent();
1263
00e12320
RR
1264 if (parent == NULL) // This is root item
1265 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1266
91b8de8d 1267 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1268 int index = children.Index(crt_item);
1269 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1270
1271 size_t count = children.Count();
1272 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1273 {
1274 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1275 }
88ac883a
VZ
1276
1277 return TagNextChildren(parent, last_item, select);
1278}
1279
1280bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1281{
00e12320
RR
1282 crt_item->SetHilight(select);
1283 RefreshLine(crt_item);
d3a9f4af 1284
06b466c7 1285 if (crt_item==last_item)
00e12320 1286 return TRUE;
88ac883a 1287
00e12320 1288 if (crt_item->HasChildren())
88ac883a 1289 {
00e12320
RR
1290 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1291 size_t count = children.Count();
1292 for ( size_t n = 0; n < count; ++n )
1293 {
06b466c7 1294 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1295 return TRUE;
06b466c7 1296 }
88ac883a 1297 }
d3a9f4af 1298
c0de7af4 1299 return FALSE;
88ac883a
VZ
1300}
1301
1302void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1303{
f2593d0d
RR
1304 // item2 is not necessary after item1
1305 wxGenericTreeItem *first=NULL, *last=NULL;
88ac883a 1306
f2593d0d
RR
1307 // choice first' and 'last' between item1 and item2
1308 if (item1->GetY()<item2->GetY())
06b466c7 1309 {
f2593d0d
RR
1310 first=item1;
1311 last=item2;
1312 }
1313 else
1314 {
1315 first=item2;
1316 last=item1;
1317 }
88ac883a 1318
f2593d0d 1319 bool select = m_current->IsSelected();
d3a9f4af 1320
f2593d0d
RR
1321 if ( TagAllChildrenUntilLast(first,last,select) )
1322 return;
88ac883a 1323
f2593d0d 1324 TagNextChildren(first,last,select);
88ac883a
VZ
1325}
1326
d3a9f4af 1327void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId,
c193b707
VZ
1328 bool unselect_others,
1329 bool extended_select)
d3a9f4af 1330{
223d09f6 1331 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1332
88ac883a 1333 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
5391f772 1334 wxGenericTreeItem *item = itemId.m_pItem;
88ac883a
VZ
1335
1336 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1337 // wxT("this is a single selection tree") );
88ac883a
VZ
1338
1339 // to keep going anyhow !!!
d3a9f4af 1340 if (is_single)
8dc99046
VZ
1341 {
1342 if (item->IsSelected())
1343 return; // nothing to do
1344 unselect_others = TRUE;
1345 extended_select = FALSE;
1346 }
1347 else if ( unselect_others && item->IsSelected() )
1348 {
1349 // selection change if there is more than one item currently selected
1350 wxArrayTreeItemIds selected_items;
1351 if ( GetSelections(selected_items) == 1 )
1352 return;
1353 }
d3a9f4af 1354
f135ff73
VZ
1355 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1356 event.m_item = item;
1357 event.m_itemOld = m_current;
1358 event.SetEventObject( this );
91b8de8d 1359 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1360
f98e2558 1361 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
f135ff73
VZ
1362 return;
1363
88ac883a
VZ
1364 // ctrl press
1365 if (unselect_others)
389cdc7a 1366 {
88ac883a 1367 if (is_single) Unselect(); // to speed up thing
c193b707 1368 else UnselectAll();
edaa81ae 1369 }
f135ff73 1370
88ac883a 1371 // shift press
d3a9f4af 1372 if (extended_select)
88ac883a 1373 {
aaa2f297
VZ
1374 if ( !m_current )
1375 {
1376 m_current =
1377 m_key_current = GetRootItem().m_pItem;
1378 }
1379
88ac883a
VZ
1380 // don't change the mark (m_current)
1381 SelectItemRange(m_current, item);
1382 }
1383 else
1384 {
c0de7af4 1385 bool select=TRUE; // the default
88ac883a 1386
c193b707
VZ
1387 // Check if we need to toggle hilight (ctrl mode)
1388 if (!unselect_others)
8dc99046 1389 select=!item->IsSelected();
88ac883a 1390
91b8de8d 1391 m_current = m_key_current = item;
c193b707
VZ
1392 m_current->SetHilight(select);
1393 RefreshLine( m_current );
88ac883a 1394 }
389cdc7a 1395
f135ff73 1396 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1397 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1398}
1399
9dfbf520
VZ
1400void wxTreeCtrl::FillArray(wxGenericTreeItem *item,
1401 wxArrayTreeItemIds &array) const
c801d85f 1402{
8dc99046 1403 if ( item->IsSelected() )
9dfbf520 1404 array.Add(wxTreeItemId(item));
91b8de8d 1405
9dfbf520 1406 if ( item->HasChildren() )
91b8de8d 1407 {
9dfbf520
VZ
1408 wxArrayGenericTreeItems& children = item->GetChildren();
1409 size_t count = children.GetCount();
1410 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1411 FillArray(children[n], array);
91b8de8d
RR
1412 }
1413}
1414
1415size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1416{
1417 array.Empty();
f6bcfd97
BP
1418 wxTreeItemId idRoot = GetRootItem();
1419 if ( idRoot.IsOk() )
1420 {
1421 FillArray(idRoot.m_pItem, array);
1422 }
1423 //else: the tree is empty, so no selections
91b8de8d
RR
1424
1425 return array.Count();
1426}
1427
1428void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1429{
91b8de8d
RR
1430 if (!item.IsOk()) return;
1431
0659e7ee 1432 wxGenericTreeItem *gitem = item.m_pItem;
ef44a621 1433
f65635b5
VZ
1434 // first expand all parent branches
1435 wxGenericTreeItem *parent = gitem->GetParent();
5391f772 1436 while ( parent )
f65635b5 1437 {
8dc99046 1438 Expand(parent);
f65635b5
VZ
1439 parent = parent->GetParent();
1440 }
1441
5391f772 1442 //if (parent) CalculatePositions();
91b8de8d
RR
1443
1444 ScrollTo(item);
1445}
1446
1447void wxTreeCtrl::ScrollTo(const wxTreeItemId &item)
1448{
1449 if (!item.IsOk()) return;
1450
dc6c62a9
RR
1451 // We have to call this here because the label in
1452 // question might just have been added and no screen
1453 // update taken place.
1454 if (m_dirty) wxYield();
1455
91b8de8d
RR
1456 wxGenericTreeItem *gitem = item.m_pItem;
1457
f65635b5 1458 // now scroll to the item
0659e7ee 1459 int item_y = gitem->GetY();
8dc99046 1460
0659e7ee
RR
1461 int start_x = 0;
1462 int start_y = 0;
1463 ViewStart( &start_x, &start_y );
91b8de8d 1464 start_y *= PIXELS_PER_UNIT;
978f38c2 1465
a93109d5
RR
1466 int client_h = 0;
1467 int client_w = 0;
1468 GetClientSize( &client_w, &client_h );
ef44a621 1469
0659e7ee
RR
1470 if (item_y < start_y+3)
1471 {
91b8de8d 1472 // going down
0659e7ee
RR
1473 int x = 0;
1474 int y = 0;
91b8de8d
RR
1475 m_anchor->GetSize( x, y, this );
1476 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1477 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1478 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1479 // Item should appear at top
91b8de8d 1480 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1481 }
91b8de8d 1482 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1483 {
c7a9fa36
RR
1484 // going up
1485 int x = 0;
1486 int y = 0;
1487 m_anchor->GetSize( x, y, this );
1488 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1489 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1490 item_y += PIXELS_PER_UNIT+2;
1491 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1492 // Item should appear at bottom
c7a9fa36 1493 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 1494 }
edaa81ae 1495}
c801d85f 1496
e1ee62bd
VZ
1497// FIXME: tree sorting functions are not reentrant and not MT-safe!
1498static wxTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1499
004fd0c8 1500static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1501 wxGenericTreeItem **item2)
edaa81ae 1502{
223d09f6 1503 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1504
1505 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1506}
1507
e1ee62bd
VZ
1508int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1509 const wxTreeItemId& item2)
0659e7ee 1510{
87138c52 1511 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1512}
1513
1514void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1515{
223d09f6 1516 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd
VZ
1517
1518 wxGenericTreeItem *item = itemId.m_pItem;
978f38c2 1519
e1ee62bd 1520 wxCHECK_RET( !s_treeBeingSorted,
223d09f6 1521 wxT("wxTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1522
91b8de8d 1523 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1524 if ( children.Count() > 1 )
1525 {
1526 s_treeBeingSorted = this;
1527 children.Sort(tree_ctrl_compare_func);
1528 s_treeBeingSorted = NULL;
978f38c2 1529
e1ee62bd
VZ
1530 m_dirty = TRUE;
1531 }
1532 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1533}
1534
f135ff73 1535wxImageList *wxTreeCtrl::GetImageList() const
edaa81ae 1536{
f135ff73 1537 return m_imageListNormal;
edaa81ae
RR
1538}
1539
f135ff73 1540wxImageList *wxTreeCtrl::GetStateImageList() const
c801d85f 1541{
f135ff73 1542 return m_imageListState;
edaa81ae 1543}
c801d85f 1544
f135ff73 1545void wxTreeCtrl::SetImageList(wxImageList *imageList)
e2414cbe 1546{
f2593d0d 1547 m_imageListNormal = imageList;
91b8de8d 1548
2c8e4738
VZ
1549 if ( !m_imageListNormal )
1550 return;
1551
f2593d0d
RR
1552 // Calculate a m_lineHeight value from the image sizes.
1553 // May be toggle off. Then wxTreeCtrl will spread when
1554 // necessary (which might look ugly).
f2593d0d
RR
1555 wxClientDC dc(this);
1556 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1557 int width = 0, height = 0,
1558 n = m_imageListNormal->GetImageCount();
06b466c7 1559
f2593d0d
RR
1560 for (int i = 0; i < n ; i++)
1561 {
1562 m_imageListNormal->GetSize(i, width, height);
1563 if (height > m_lineHeight) m_lineHeight = height;
1564 }
91b8de8d 1565
06b466c7 1566 if (m_lineHeight < 40)
f2593d0d 1567 m_lineHeight += 2; // at least 2 pixels
06b466c7 1568 else
f2593d0d 1569 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 1570}
e2414cbe 1571
f135ff73 1572void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1573{
f135ff73 1574 m_imageListState = imageList;
edaa81ae 1575}
e2414cbe 1576
f135ff73
VZ
1577// -----------------------------------------------------------------------------
1578// helpers
1579// -----------------------------------------------------------------------------
0659e7ee 1580
74bedbeb 1581void wxTreeCtrl::AdjustMyScrollbars()
c801d85f 1582{
0659e7ee
RR
1583 if (m_anchor)
1584 {
1585 int x = 0;
1586 int y = 0;
91b8de8d 1587 m_anchor->GetSize( x, y, this );
c193b707 1588 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1589 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1590 int x_pos = GetScrollPos( wxHORIZONTAL );
1591 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1592 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1593 }
1594 else
1595 {
1596 SetScrollbars( 0, 0, 0, 0 );
1597 }
edaa81ae 1598}
c801d85f 1599
91b8de8d
RR
1600int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1601{
dc6c62a9
RR
1602 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1603 return item->GetHeight();
1604 else
1605 return m_lineHeight;
91b8de8d
RR
1606}
1607
ef44a621
VZ
1608void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1609{
9ec64fa7
VZ
1610 wxTreeItemAttr *attr = item->GetAttributes();
1611 if ( attr && attr->HasFont() )
1612 dc.SetFont(attr->GetFont());
1613 else if (item->IsBold())
eff869aa 1614 dc.SetFont(m_boldFont);
ef44a621 1615
bbe0af5b
RR
1616 long text_w = 0;
1617 long text_h = 0;
1618 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1619
bbe0af5b
RR
1620 int image_h = 0;
1621 int image_w = 0;
8dc99046
VZ
1622 int image = item->GetCurrentImage();
1623 if ( image != NO_IMAGE )
bbe0af5b 1624 {
2c8e4738
VZ
1625 if ( m_imageListNormal )
1626 {
1627 m_imageListNormal->GetSize( image, image_w, image_h );
1628 image_w += 4;
1629 }
1630 else
1631 {
1632 image = NO_IMAGE;
1633 }
bbe0af5b 1634 }
ef44a621 1635
91b8de8d
RR
1636 int total_h = GetLineHeight(item);
1637
c0fba4d1
VS
1638 if (item->IsSelected())
1639 dc.SetBrush(*m_hilightBrush);
afa6a1a1 1640 else
c0fba4d1
VS
1641 {
1642 wxColour colBg;
1643 if ( attr && attr->HasBackgroundColour() )
1644 colBg = attr->GetBackgroundColour();
1645 else
1646 colBg = m_backgroundColour;
1647 dc.SetBrush(wxBrush(colBg, wxSOLID));
1648 }
afa6a1a1 1649
91b8de8d 1650 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
ef44a621 1651
8dc99046 1652 if ( image != NO_IMAGE )
bbe0af5b 1653 {
d30b4d20 1654 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 1655 m_imageListNormal->Draw( image, dc,
49cd56ef 1656 item->GetX(),
d701d432 1657 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1658 wxIMAGELIST_DRAW_TRANSPARENT );
1659 dc.DestroyClippingRegion();
1660 }
ef44a621 1661
afa6a1a1 1662 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
1663 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
1664 dc.DrawText( item->GetText(),
1665 (wxCoord)(image_w + item->GetX()),
1666 (wxCoord)(item->GetY() + extraH));
ef44a621 1667
eff869aa
RR
1668 // restore normal font
1669 dc.SetFont( m_normalFont );
ef44a621
VZ
1670}
1671
91b8de8d 1672// Now y stands for the top of the item, whereas it used to stand for middle !
16c1f7f3 1673void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1674{
bbe0af5b 1675 int horizX = level*m_indent;
389cdc7a 1676
cf724bce 1677 item->SetX( horizX+m_indent+m_spacing );
91b8de8d 1678 item->SetY( y );
4c681997 1679
bbe0af5b 1680 int oldY = y;
91b8de8d
RR
1681 y+=GetLineHeight(item)/2;
1682
1683 item->SetCross( horizX+m_indent, y );
389cdc7a 1684
bbe0af5b 1685 int exposed_x = dc.LogicalToDeviceX( 0 );
5391f772 1686 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
4832f7c0 1687
5391f772 1688 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
bbe0af5b
RR
1689 {
1690 int startX = horizX;
cf724bce 1691 int endX = horizX + (m_indent-5);
29d87bba 1692
cf724bce 1693// if (!item->HasChildren()) endX += (m_indent+5);
bbe0af5b 1694 if (!item->HasChildren()) endX += 20;
4832f7c0 1695
bbe0af5b 1696 dc.DrawLine( startX, y, endX, y );
29d87bba 1697
bbe0af5b
RR
1698 if (item->HasPlus())
1699 {
cf724bce 1700 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
bbe0af5b
RR
1701 dc.SetPen( *wxGREY_PEN );
1702 dc.SetBrush( *wxWHITE_BRUSH );
cf724bce 1703 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
9dfbf520 1704
bbe0af5b 1705 dc.SetPen( *wxBLACK_PEN );
cf724bce 1706 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
bbe0af5b 1707 if (!item->IsExpanded())
cf724bce 1708 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
9dfbf520 1709
112c5086 1710 dc.SetPen( m_dottedPen );
bbe0af5b 1711 }
c801d85f 1712
9ec64fa7 1713 wxPen *pen = wxTRANSPARENT_PEN;
9ec64fa7 1714 wxColour colText;
a367b9b3 1715
9ec64fa7
VZ
1716 if ( item->IsSelected() )
1717 {
1718 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
4832f7c0 1719
9ec64fa7
VZ
1720 if ( m_hasFocus )
1721 pen = wxBLACK_PEN;
f135ff73 1722
bbe0af5b
RR
1723 }
1724 else
1725 {
9ec64fa7
VZ
1726 wxTreeItemAttr *attr = item->GetAttributes();
1727 if ( attr && attr->HasTextColour() )
1728 colText = attr->GetTextColour();
1729 else
1730 colText = *wxBLACK;
bbe0af5b 1731 }
9ec64fa7
VZ
1732
1733 // prepare to draw
1734 dc.SetTextForeground(colText);
1735 dc.SetPen(*pen);
9ec64fa7
VZ
1736
1737 // draw
1738 PaintItem(item, dc);
1739
1740 // restore DC objects
1741 dc.SetBrush( *wxWHITE_BRUSH );
1742 dc.SetPen( m_dottedPen );
1743 dc.SetTextForeground( *wxBLACK );
f135ff73 1744 }
d3a9f4af 1745
91b8de8d 1746 y = oldY+GetLineHeight(item);
e2414cbe 1747
bbe0af5b
RR
1748 if (item->IsExpanded())
1749 {
91b8de8d 1750 oldY+=GetLineHeight(item)/2;
9ec64fa7 1751 int semiOldY=0;
389cdc7a 1752
91b8de8d
RR
1753 wxArrayGenericTreeItems& children = item->GetChildren();
1754 size_t n, count = children.Count();
1755 for ( n = 0; n < count; ++n )
c193b707
VZ
1756 {
1757 semiOldY=y;
1758 PaintLevel( children[n], dc, level+1, y );
1759 }
389cdc7a 1760
f65635b5
VZ
1761 // it may happen that the item is expanded but has no items (when you
1762 // delete all its children for example) - don't draw the vertical line
1763 // in this case
1764 if (count > 0)
9ec64fa7 1765 {
c193b707 1766 semiOldY+=GetLineHeight(children[--n])/2;
cf724bce 1767 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
9ec64fa7 1768 }
bbe0af5b 1769 }
4c681997 1770}
c801d85f 1771
3dbeaa52
VZ
1772void wxTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
1773{
1774 if ( item )
1775 {
1776 if ( item->HasPlus() )
1777 {
1778 // it's a folder, indicate it by a border
1779 DrawBorder(item);
1780 }
1781 else
1782 {
1783 // draw a line under the drop target because the item will be
1784 // dropped there
1785 DrawLine(item, TRUE /* below */);
1786 }
1787
1788 SetCursor(wxCURSOR_BULLSEYE);
1789 }
1790 else
1791 {
1792 // can't drop here
1793 SetCursor(wxCURSOR_NO_ENTRY);
1794 }
1795}
1796
1797void wxTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 1798{
3dbeaa52 1799 wxCHECK_RET( item.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") );
91b8de8d 1800
3dbeaa52 1801 wxGenericTreeItem *i = item.m_pItem;
91b8de8d 1802
1044a386 1803 wxClientDC dc(this);
91b8de8d
RR
1804 PrepareDC( dc );
1805 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 1806 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 1807
3dbeaa52
VZ
1808 int w = i->GetWidth() + 2;
1809 int h = GetLineHeight(i) + 2;
91b8de8d 1810
3dbeaa52 1811 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
1812}
1813
3dbeaa52 1814void wxTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 1815{
3dbeaa52 1816 wxCHECK_RET( item.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") );
91b8de8d 1817
3dbeaa52 1818 wxGenericTreeItem *i = item.m_pItem;
91b8de8d 1819
1044a386 1820 wxClientDC dc(this);
91b8de8d
RR
1821 PrepareDC( dc );
1822 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 1823
3dbeaa52
VZ
1824 int x = i->GetX(),
1825 y = i->GetY();
1826 if ( below )
1827 {
1828 y += GetLineHeight(i) - 1;
1829 }
91b8de8d 1830
3dbeaa52 1831 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
1832}
1833
f135ff73
VZ
1834// -----------------------------------------------------------------------------
1835// wxWindows callbacks
1836// -----------------------------------------------------------------------------
1837
3db7be80 1838void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 1839{
91b8de8d 1840 if ( !m_anchor)
0659e7ee 1841 return;
c801d85f 1842
0659e7ee
RR
1843 wxPaintDC dc(this);
1844 PrepareDC( dc );
29d87bba 1845
eff869aa 1846 dc.SetFont( m_normalFont );
0659e7ee 1847 dc.SetPen( m_dottedPen );
f38374d0 1848
eff869aa 1849 // this is now done dynamically
91b8de8d
RR
1850 //if(GetImageList() == NULL)
1851 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1852
91b8de8d 1853 int y = 2;
0659e7ee 1854 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 1855}
c801d85f 1856
3db7be80 1857void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1858{
0659e7ee 1859 m_hasFocus = TRUE;
978f38c2 1860
bbe0af5b 1861 if (m_current) RefreshLine( m_current );
edaa81ae 1862}
c801d85f 1863
3db7be80 1864void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1865{
0659e7ee 1866 m_hasFocus = FALSE;
978f38c2 1867
bbe0af5b 1868 if (m_current) RefreshLine( m_current );
edaa81ae 1869}
c801d85f
KB
1870
1871void wxTreeCtrl::OnChar( wxKeyEvent &event )
1872{
978f38c2 1873 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
06b466c7 1874 te.m_code = (int)event.KeyCode();
978f38c2
VZ
1875 te.SetEventObject( this );
1876 GetEventHandler()->ProcessEvent( te );
435fe83e 1877
91b8de8d 1878 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
1879 {
1880 event.Skip();
1881 return;
1882 }
ef44a621 1883
06b466c7
VZ
1884 // how should the selection work for this event?
1885 bool is_multiple, extended_select, unselect_others;
1886 EventFlagsToSelType(GetWindowStyleFlag(),
1887 event.ShiftDown(),
1888 event.ControlDown(),
dfc6cd93
SB
1889 is_multiple, extended_select, unselect_others);
1890
1891 // + : Expand
1892 // - : Collaspe
f6bcfd97 1893 // * : Expand all/Collapse all
dfc6cd93
SB
1894 // ' ' | return : activate
1895 // up : go up (not last children!)
1896 // down : go down
1897 // left : go to parent
1898 // right : open if parent and go next
1899 // home : go to root
1900 // end : go to last item without opening parents
978f38c2
VZ
1901 switch (event.KeyCode())
1902 {
1903 case '+':
1904 case WXK_ADD:
1905 if (m_current->HasPlus() && !IsExpanded(m_current))
1906 {
1907 Expand(m_current);
1908 }
1909 break;
ef44a621 1910
f6bcfd97
BP
1911 case '*':
1912 case WXK_MULTIPLY:
1913 if ( !IsExpanded(m_current) )
1914 {
1915 // expand all
1916 ExpandAll(m_current);
1917 break;
1918 }
1919 //else: fall through to Collapse() it
1920
978f38c2
VZ
1921 case '-':
1922 case WXK_SUBTRACT:
1923 if (IsExpanded(m_current))
1924 {
1925 Collapse(m_current);
1926 }
1927 break;
ef44a621 1928
978f38c2
VZ
1929 case ' ':
1930 case WXK_RETURN:
1931 {
1932 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1933 event.m_item = m_current;
1934 event.m_code = 0;
1935 event.SetEventObject( this );
1936 GetEventHandler()->ProcessEvent( event );
1937 }
1938 break;
ef44a621 1939
978f38c2
VZ
1940 // up goes to the previous sibling or to the last of its children if
1941 // it's expanded
1942 case WXK_UP:
1943 {
91b8de8d 1944 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
1945 if (!prev)
1946 {
91b8de8d 1947 prev = GetParent( m_key_current );
c193b707
VZ
1948 if (prev)
1949 {
90e58684 1950 long cockie = 0;
91b8de8d 1951 wxTreeItemId current = m_key_current;
90e58684
RR
1952 if (current == GetFirstChild( prev, cockie ))
1953 {
1954 // otherwise we return to where we came from
88ac883a 1955 SelectItem( prev, unselect_others, extended_select );
c193b707
VZ
1956 m_key_current=prev.m_pItem;
1957 EnsureVisible( prev );
90e58684 1958 break;
c193b707 1959 }
978f38c2
VZ
1960 }
1961 }
1962 if (prev)
1963 {
69a282d4 1964 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 1965 {
69a282d4
VZ
1966 wxTreeItemId child = GetLastChild(prev);
1967 if ( child )
1968 {
1969 prev = child;
1970 }
978f38c2 1971 }
69a282d4 1972
88ac883a 1973 SelectItem( prev, unselect_others, extended_select );
c193b707 1974 m_key_current=prev.m_pItem;
978f38c2
VZ
1975 EnsureVisible( prev );
1976 }
1977 }
1978 break;
ef44a621 1979
978f38c2
VZ
1980 // left arrow goes to the parent
1981 case WXK_LEFT:
1982 {
1983 wxTreeItemId prev = GetParent( m_current );
1984 if (prev)
1985 {
1986 EnsureVisible( prev );
88ac883a 1987 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
1988 }
1989 }
1990 break;
ef44a621 1991
978f38c2
VZ
1992 case WXK_RIGHT:
1993 // this works the same as the down arrow except that we also expand the
1994 // item if it wasn't expanded yet
1995 Expand(m_current);
1996 // fall through
1997
1998 case WXK_DOWN:
ef44a621 1999 {
91b8de8d 2000 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
2001 {
2002 long cookie = 0;
91b8de8d 2003 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 2004 SelectItem( child, unselect_others, extended_select );
c193b707 2005 m_key_current=child.m_pItem;
978f38c2
VZ
2006 EnsureVisible( child );
2007 }
2008 else
2009 {
91b8de8d 2010 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2011 if (!next)
978f38c2 2012 {
91b8de8d 2013 wxTreeItemId current = m_key_current;
978f38c2
VZ
2014 while (current && !next)
2015 {
2016 current = GetParent( current );
2017 if (current) next = GetNextSibling( current );
2018 }
2019 }
b62c3631 2020 if (next)
978f38c2 2021 {
88ac883a 2022 SelectItem( next, unselect_others, extended_select );
c193b707 2023 m_key_current=next.m_pItem;
978f38c2
VZ
2024 EnsureVisible( next );
2025 }
2026 }
ef44a621 2027 }
978f38c2 2028 break;
ef44a621 2029
978f38c2
VZ
2030 // <End> selects the last visible tree item
2031 case WXK_END:
2032 {
2033 wxTreeItemId last = GetRootItem();
2034
2035 while ( last.IsOk() && IsExpanded(last) )
2036 {
2037 wxTreeItemId lastChild = GetLastChild(last);
2038
2039 // it may happen if the item was expanded but then all of
2040 // its children have been deleted - so IsExpanded() returned
2041 // TRUE, but GetLastChild() returned invalid item
2042 if ( !lastChild )
2043 break;
2044
2045 last = lastChild;
2046 }
2047
2048 if ( last.IsOk() )
2049 {
2050 EnsureVisible( last );
88ac883a 2051 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2052 }
2053 }
2054 break;
2055
2056 // <Home> selects the root item
2057 case WXK_HOME:
2058 {
2059 wxTreeItemId prev = GetRootItem();
2060 if (prev)
2061 {
2062 EnsureVisible( prev );
88ac883a 2063 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2064 }
2065 }
2066 break;
2067
2068 default:
2069 event.Skip();
2070 }
edaa81ae 2071}
c801d85f 2072
91b8de8d 2073wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2074{
dc6c62a9
RR
2075 // We have to call this here because the label in
2076 // question might just have been added and no screen
2077 // update taken place.
2078 if (m_dirty) wxYield();
2079
67a7abf7
VZ
2080 wxClientDC dc(this);
2081 PrepareDC(dc);
06b466c7
VZ
2082 wxCoord x = dc.DeviceToLogicalX( point.x );
2083 wxCoord y = dc.DeviceToLogicalY( point.y );
91b8de8d
RR
2084 int w, h;
2085 GetSize(&w, &h);
2086
2087 flags=0;
2088 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
2089 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
2090 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
2091 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
d3a9f4af 2092
91b8de8d 2093 return m_anchor->HitTest( wxPoint(x, y), this, flags);
4f22cf8d
RR
2094}
2095
e179bd65
RR
2096/* **** */
2097
2098void wxTreeCtrl::Edit( const wxTreeItemId& item )
2099{
2100 if (!item.IsOk()) return;
2101
2102 m_currentEdit = item.m_pItem;
9dfbf520 2103
e179bd65
RR
2104 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
2105 te.m_item = m_currentEdit;
2106 te.SetEventObject( this );
2107 GetEventHandler()->ProcessEvent( te );
2108
2109 if (!te.IsAllowed()) return;
8dc99046 2110
dc6c62a9
RR
2111 // We have to call this here because the label in
2112 // question might just have been added and no screen
2113 // update taken place.
2114 if (m_dirty) wxYield();
9dfbf520 2115
e179bd65
RR
2116 wxString s = m_currentEdit->GetText();
2117 int x = m_currentEdit->GetX();
2118 int y = m_currentEdit->GetY();
2119 int w = m_currentEdit->GetWidth();
2120 int h = m_currentEdit->GetHeight();
9dfbf520 2121
5f1ea0ee
RR
2122 int image_h = 0;
2123 int image_w = 0;
8dc99046
VZ
2124
2125 int image = m_currentEdit->GetCurrentImage();
2126 if ( image != NO_IMAGE )
5f1ea0ee 2127 {
2c8e4738
VZ
2128 if ( m_imageListNormal )
2129 {
2130 m_imageListNormal->GetSize( image, image_w, image_h );
2131 image_w += 4;
2132 }
2133 else
2134 {
2135 wxFAIL_MSG(_T("you must create an image list to use images!"));
2136 }
5f1ea0ee
RR
2137 }
2138 x += image_w;
2139 w -= image_w + 4; // I don't know why +4 is needed
e179bd65
RR
2140
2141 wxClientDC dc(this);
2142 PrepareDC( dc );
2143 x = dc.LogicalToDeviceX( x );
2144 y = dc.LogicalToDeviceY( y );
2145
2146 wxTreeTextCtrl *text = new wxTreeTextCtrl(
2147 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
2148 text->SetFocus();
2149}
2150
2151void wxTreeCtrl::OnRenameTimer()
2152{
2153 Edit( m_current );
2154}
2155
2156void wxTreeCtrl::OnRenameAccept()
2157{
2158 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2159 le.m_item = m_currentEdit;
2160 le.SetEventObject( this );
2161 le.m_label = m_renameRes;
2162 GetEventHandler()->ProcessEvent( le );
9dfbf520 2163
e179bd65 2164 if (!le.IsAllowed()) return;
9dfbf520 2165
5f1ea0ee 2166 SetItemText( m_currentEdit, m_renameRes );
e179bd65 2167}
9dfbf520 2168
3db7be80 2169void wxTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2170{
bbe0af5b 2171 if ( !m_anchor ) return;
978f38c2 2172
3dbeaa52
VZ
2173 // we process left mouse up event (enables in-place edit), right down
2174 // (pass to the user code), left dbl click (activate item) and
2175 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
2176 if ( !(event.LeftDown() ||
2177 event.LeftUp() ||
3dbeaa52
VZ
2178 event.RightDown() ||
2179 event.LeftDClick() ||
2180 event.Dragging() ||
2181 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2182 {
2183 event.Skip();
2184
2185 return;
2186 }
2187
bbe0af5b
RR
2188 wxClientDC dc(this);
2189 PrepareDC(dc);
06b466c7
VZ
2190 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2191 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
29d87bba 2192
3dbeaa52 2193 int flags = 0;
91b8de8d 2194 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
3dbeaa52 2195
3dbeaa52 2196 if ( event.Dragging() && !m_isDragging )
bbe0af5b 2197 {
fd9811b1 2198 if (m_dragCount == 0)
8dc99046
VZ
2199 m_dragStart = wxPoint(x,y);
2200
fd9811b1 2201 m_dragCount++;
8dc99046 2202
3dbeaa52
VZ
2203 if (m_dragCount != 3)
2204 {
2205 // wait until user drags a bit further...
2206 return;
2207 }
8dc99046 2208
3dbeaa52
VZ
2209 wxEventType command = event.RightIsDown()
2210 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2211 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 2212
fd9811b1
RR
2213 wxTreeEvent nevent( command, GetId() );
2214 nevent.m_item = m_current;
2215 nevent.SetEventObject(this);
3dbeaa52
VZ
2216
2217 // by default the dragging is not supported, the user code must
2218 // explicitly allow the event for it to take place
2219 nevent.Veto();
2220
2221 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2222 {
2223 // we're going to drag this item
2224 m_isDragging = TRUE;
2225
b3a7510d
VZ
2226 // remember the old cursor because we will change it while
2227 // dragging
2228 m_oldCursor = m_cursor;
2229
2230 // in a single selection control, hide the selection temporarily
2231 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2232 {
2233 m_oldSelection = GetSelection().m_pItem;
2234
2235 if ( m_oldSelection )
2236 {
2237 m_oldSelection->SetHilight(FALSE);
2238 RefreshLine(m_oldSelection);
2239 }
2240 }
2241
3dbeaa52
VZ
2242 CaptureMouse();
2243 }
bbe0af5b 2244 }
3dbeaa52 2245 else if ( event.Moving() )
fd9811b1 2246 {
3dbeaa52
VZ
2247 if ( item != m_dropTarget )
2248 {
2249 // unhighlight the previous drop target
2250 DrawDropEffect(m_dropTarget);
fd9811b1 2251
3dbeaa52 2252 m_dropTarget = item;
978f38c2 2253
3dbeaa52
VZ
2254 // highlight the current drop target if any
2255 DrawDropEffect(m_dropTarget);
fb882e1c 2256
3dbeaa52
VZ
2257 wxYield();
2258 }
e179bd65 2259 }
3dbeaa52
VZ
2260 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2261 {
2262 // erase the highlighting
2263 DrawDropEffect(m_dropTarget);
9dfbf520 2264
3dbeaa52
VZ
2265 // generate the drag end event
2266 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 2267
3dbeaa52
VZ
2268 event.m_item = item;
2269 event.m_pointDrag = wxPoint(x, y);
2270 event.SetEventObject(this);
91b8de8d 2271
3dbeaa52 2272 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 2273
3dbeaa52
VZ
2274 m_isDragging = FALSE;
2275 m_dropTarget = (wxGenericTreeItem *)NULL;
2276
b3a7510d
VZ
2277 if ( m_oldSelection )
2278 {
2279 m_oldSelection->SetHilight(TRUE);
2280 RefreshLine(m_oldSelection);
2281 m_oldSelection = (wxGenericTreeItem *)NULL;
2282 }
2283
3dbeaa52
VZ
2284 ReleaseMouse();
2285
b3a7510d 2286 SetCursor(m_oldCursor);
3dbeaa52
VZ
2287
2288 wxYield();
2289 }
2290 else
bbe0af5b 2291 {
3dbeaa52
VZ
2292 // here we process only the messages which happen on tree items
2293
2294 m_dragCount = 0;
2295
2296 if (item == NULL) return; /* we hit the blank area */
2297
2298 if ( event.RightDown() )
2299 {
2300 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
2301 nevent.m_item = item;
2302 nevent.m_code = 0;
2303 nevent.SetEventObject(this);
2304 GetEventHandler()->ProcessEvent(nevent);
2305 }
f6bcfd97
BP
2306 else if ( event.LeftUp() && m_lastOnSame )
2307 {
2308 if ( (item == m_current) &&
3dbeaa52
VZ
2309 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2310 HasFlag(wxTR_EDIT_LABELS) )
f6bcfd97
BP
2311 {
2312 m_renameTimer->Start( 100, TRUE );
2313 }
2314
2315 m_lastOnSame = FALSE;
3dbeaa52
VZ
2316 }
2317 else
2318 {
f6bcfd97
BP
2319 if ( event.LeftDown() )
2320 {
2321 m_lastOnSame = item == m_current;
2322 }
2323
3dbeaa52
VZ
2324 // how should the selection work for this event?
2325 bool is_multiple, extended_select, unselect_others;
2326 EventFlagsToSelType(GetWindowStyleFlag(),
2327 event.ShiftDown(),
2328 event.ControlDown(),
dfc6cd93 2329 is_multiple, extended_select, unselect_others);
3dbeaa52 2330
f6bcfd97 2331 if ( (flags & wxTREE_HITTEST_ONITEMBUTTON) && event.LeftDown() )
3dbeaa52
VZ
2332 {
2333 Toggle( item );
2334 if ( is_multiple )
2335 return;
2336 }
2337
2338 SelectItem(item, unselect_others, extended_select);
2339
2340 if ( event.LeftDClick() )
2341 {
f6bcfd97
BP
2342 // double clicking should not start editing the item label
2343 m_renameTimer->Stop();
2344 m_lastOnSame = FALSE;
2345
3dbeaa52
VZ
2346 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2347 nevent.m_item = item;
2348 nevent.m_code = 0;
2349 nevent.SetEventObject( this );
2350 GetEventHandler()->ProcessEvent( nevent );
2351 }
2352 }
bbe0af5b 2353 }
edaa81ae 2354}
c801d85f 2355
3db7be80
RR
2356void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2357{
bbe0af5b
RR
2358 /* after all changes have been done to the tree control,
2359 * we actually redraw the tree when everything is over */
ef44a621 2360
f65635b5
VZ
2361 if (!m_dirty)
2362 return;
ef44a621 2363
bbe0af5b 2364 m_dirty = FALSE;
3db7be80 2365
bbe0af5b 2366 CalculatePositions();
91b8de8d 2367 Refresh();
bbe0af5b 2368 AdjustMyScrollbars();
3db7be80
RR
2369}
2370
91b8de8d 2371void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 2372{
e06b9569
JS
2373 wxCoord text_w = 0;
2374 wxCoord text_h = 0;
9dfbf520 2375
a62867a5 2376 if (item->IsBold())
eff869aa 2377 dc.SetFont(m_boldFont);
9dfbf520 2378
91b8de8d 2379 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 2380 text_h+=2;
91b8de8d 2381
eff869aa
RR
2382 // restore normal font
2383 dc.SetFont( m_normalFont );
9dfbf520 2384
91b8de8d
RR
2385 int image_h = 0;
2386 int image_w = 0;
8dc99046
VZ
2387 int image = item->GetCurrentImage();
2388 if ( image != NO_IMAGE )
91b8de8d 2389 {
2c8e4738
VZ
2390 if ( m_imageListNormal )
2391 {
2392 m_imageListNormal->GetSize( image, image_w, image_h );
2393 image_w += 4;
2394 }
91b8de8d
RR
2395 }
2396
2397 int total_h = (image_h > text_h) ? image_h : text_h;
2398
06b466c7 2399 if (total_h < 40)
f2593d0d 2400 total_h += 2; // at least 2 pixels
06b466c7 2401 else
f2593d0d 2402 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
2403
2404 item->SetHeight(total_h);
6cedba09
VZ
2405 if (total_h>m_lineHeight)
2406 m_lineHeight=total_h;
bbe0af5b 2407
91b8de8d
RR
2408 item->SetWidth(image_w+text_w+2);
2409}
2410
2411// -----------------------------------------------------------------------------
2412// for developper : y is now the top of the level
2413// not the middle of it !
bbe0af5b 2414void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2415{
bbe0af5b 2416 int horizX = level*m_indent;
389cdc7a 2417
91b8de8d 2418 CalculateSize( item, dc );
d3a9f4af 2419
91b8de8d 2420 // set its position
cf724bce 2421 item->SetX( horizX+m_indent+m_spacing );
91b8de8d
RR
2422 item->SetY( y );
2423 y+=GetLineHeight(item);
4c681997 2424
bbe0af5b
RR
2425 if ( !item->IsExpanded() )
2426 {
f65635b5 2427 // we dont need to calculate collapsed branches
bbe0af5b
RR
2428 return;
2429 }
389cdc7a 2430
91b8de8d
RR
2431 wxArrayGenericTreeItems& children = item->GetChildren();
2432 size_t n, count = children.Count();
2433 for (n = 0; n < count; ++n )
f2593d0d 2434 CalculateLevel( children[n], dc, level+1, y ); // recurse
edaa81ae 2435}
c801d85f 2436
74bedbeb 2437void wxTreeCtrl::CalculatePositions()
c801d85f 2438{
bbe0af5b 2439 if ( !m_anchor ) return;
29d87bba 2440
bbe0af5b
RR
2441 wxClientDC dc(this);
2442 PrepareDC( dc );
29d87bba 2443
eff869aa 2444 dc.SetFont( m_normalFont );
29d87bba 2445
bbe0af5b 2446 dc.SetPen( m_dottedPen );
91b8de8d
RR
2447 //if(GetImageList() == NULL)
2448 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2449
8dc99046 2450 int y = 2;
f65635b5 2451 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 2452}
c801d85f 2453
f135ff73 2454void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 2455{
e6527f9d
RR
2456 if (m_dirty) return;
2457
bbe0af5b
RR
2458 wxClientDC dc(this);
2459 PrepareDC(dc);
4832f7c0 2460
bbe0af5b
RR
2461 int cw = 0;
2462 int ch = 0;
2463 GetClientSize( &cw, &ch );
4832f7c0 2464
bbe0af5b
RR
2465 wxRect rect;
2466 rect.x = dc.LogicalToDeviceX( 0 );
2467 rect.width = cw;
2468 rect.y = dc.LogicalToDeviceY( item->GetY() );
2469 rect.height = ch;
f135ff73 2470
bbe0af5b 2471 Refresh( TRUE, &rect );
f135ff73 2472
bbe0af5b 2473 AdjustMyScrollbars();
edaa81ae 2474}
c801d85f
KB
2475
2476void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2477{
e6527f9d
RR
2478 if (m_dirty) return;
2479
bbe0af5b
RR
2480 wxClientDC dc(this);
2481 PrepareDC( dc );
2482
5391f772
SB
2483 int cw = 0;
2484 int ch = 0;
2485 GetClientSize( &cw, &ch );
2486
bbe0af5b 2487 wxRect rect;
5391f772
SB
2488 rect.x = dc.LogicalToDeviceX( 0 );
2489 rect.y = dc.LogicalToDeviceY( item->GetY() );
2490 rect.width = cw;
91b8de8d 2491 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 2492
bbe0af5b 2493 Refresh( TRUE, &rect );
edaa81ae 2494}
c801d85f 2495