]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
Avoid events when implicitly selecting first wxBookCtrl page.
[wxWidgets.git] / src / generic / treectlg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
ed4b0fdc 2// Name: src/generic/treectlg.cpp
f135ff73 3// Purpose: generic tree control implementation
c801d85f
KB
4// Author: Robert Roebling
5// Created: 01/02/97
f135ff73 6// Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
389cdc7a 7// Id: $Id$
6aa89a22 8// Copyright: (c) 1998 Robert Roebling and Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
f135ff73
VZ
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
19
1e6d9499
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
1e6feb95 24 #pragma hdrstop
1e6d9499
JS
25#endif
26
1e6feb95
VZ
27#if wxUSE_TREECTRL
28
8cee4a30 29#include "wx/treectrl.h"
ed4b0fdc
WS
30
31#ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
c0badb70 33 #include "wx/timer.h"
9eddec69 34 #include "wx/settings.h"
2a673eb1 35 #include "wx/listbox.h"
fec9cc08 36 #include "wx/textctrl.h"
ed4b0fdc
WS
37#endif
38
941830cb
JS
39#include "wx/generic/treectlg.h"
40#include "wx/imaglist.h"
c801d85f 41
9c7f49f5 42#include "wx/renderer.h"
44c44c82 43
f89d65ea 44#ifdef __WXMAC__
33ddeaba 45 #include "wx/osx/private.h"
f89d65ea 46#endif
ca65c044 47
f135ff73
VZ
48// -----------------------------------------------------------------------------
49// array types
50// -----------------------------------------------------------------------------
c801d85f 51
b5dbe15d 52class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem;
1e6d9499 53
ec64d5ca 54WX_DEFINE_ARRAY_PTR(wxGenericTreeItem *, wxArrayGenericTreeItems);
c801d85f 55
8dc99046
VZ
56// ----------------------------------------------------------------------------
57// constants
58// ----------------------------------------------------------------------------
59
60static const int NO_IMAGE = -1;
61
cb59313c 62static const int PIXELS_PER_UNIT = 10;
3dbeaa52 63
03966fcb
RR
64// the margin between the item state image and the item normal image
65static const int MARGIN_BETWEEN_STATE_AND_IMAGE = 2;
66
3e4f8ee2
VZ
67// the margin between the item image and the item text
68static const int MARGIN_BETWEEN_IMAGE_AND_TEXT = 4;
69
f135ff73
VZ
70// -----------------------------------------------------------------------------
71// private classes
72// -----------------------------------------------------------------------------
73
3dbeaa52
VZ
74// timer used for enabling in-place edit
75class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
76{
77public:
cb59313c
VZ
78 // start editing the current item after half a second (if the mouse hasn't
79 // been clicked/moved)
7a944d2f 80 enum { DELAY = 500 };
cb59313c 81
941830cb 82 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
3dbeaa52 83
cb59313c 84 virtual void Notify();
3dbeaa52
VZ
85
86private:
cb59313c 87 wxGenericTreeCtrl *m_owner;
22f3361e 88
c0c133e1 89 wxDECLARE_NO_COPY_CLASS(wxTreeRenameTimer);
3dbeaa52
VZ
90};
91
92// control used for in-place edit
93class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
94{
95public:
edb8f298 96 wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item);
3dbeaa52 97
a8a89154 98 void EndEdit( bool discardChanges );
6621957f 99
0cf3b542 100 const wxGenericTreeItem* item() const { return m_itemEdited; }
eb7f24c1 101
edb8f298 102protected:
3dbeaa52 103 void OnChar( wxKeyEvent &event );
c13cace1 104 void OnKeyUp( wxKeyEvent &event );
3dbeaa52
VZ
105 void OnKillFocus( wxFocusEvent &event );
106
edb8f298 107 bool AcceptChanges();
a8a89154 108 void Finish( bool setfocus );
edb8f298 109
3dbeaa52 110private:
618a5e38 111 wxGenericTreeCtrl *m_owner;
edb8f298 112 wxGenericTreeItem *m_itemEdited;
3dbeaa52 113 wxString m_startValue;
33fe475a 114 bool m_aboutToFinish;
3dbeaa52
VZ
115
116 DECLARE_EVENT_TABLE()
c0c133e1 117 wxDECLARE_NO_COPY_CLASS(wxTreeTextCtrl);
3dbeaa52
VZ
118};
119
cb59313c
VZ
120// timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
121// for a sufficiently long time
122class WXDLLEXPORT wxTreeFindTimer : public wxTimer
123{
124public:
125 // reset the current prefix after half a second of inactivity
7a944d2f 126 enum { DELAY = 500 };
cb59313c
VZ
127
128 wxTreeFindTimer( wxGenericTreeCtrl *owner ) { m_owner = owner; }
129
130 virtual void Notify() { m_owner->m_findPrefix.clear(); }
131
132private:
133 wxGenericTreeCtrl *m_owner;
22f3361e 134
c0c133e1 135 wxDECLARE_NO_COPY_CLASS(wxTreeFindTimer);
cb59313c
VZ
136};
137
f135ff73
VZ
138// a tree item
139class WXDLLEXPORT wxGenericTreeItem
c801d85f 140{
f135ff73 141public:
9ec64fa7 142 // ctors & dtor
cac09ce8
VZ
143 wxGenericTreeItem()
144 {
145 m_data = NULL;
146 m_widthText =
147 m_heightText = -1;
148 }
149
9ec64fa7 150 wxGenericTreeItem( wxGenericTreeItem *parent,
618a5e38 151 const wxString& text,
618a5e38
RR
152 int image,
153 int selImage,
154 wxTreeItemData *data );
f135ff73 155
9ec64fa7 156 ~wxGenericTreeItem();
f135ff73 157
9ec64fa7
VZ
158 // trivial accessors
159 wxArrayGenericTreeItems& GetChildren() { return m_children; }
f135ff73 160
9ec64fa7
VZ
161 const wxString& GetText() const { return m_text; }
162 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
3dbeaa52 163 { return m_images[which]; }
9ec64fa7 164 wxTreeItemData *GetData() const { return m_data; }
03966fcb 165 int GetState() const { return m_state; }
f135ff73 166
9ec64fa7
VZ
167 // returns the current image for the item (depending on its
168 // selected/expanded/whatever state)
169 int GetCurrentImage() const;
8dc99046 170
cac09ce8
VZ
171 void SetText(const wxString& text)
172 {
173 m_text = text;
174
175 ResetTextSize();
176 }
177
178 void SetImage(int image, wxTreeItemIcon which)
179 {
180 m_images[which] = image;
181 m_width = 0;
182 }
183
9ec64fa7 184 void SetData(wxTreeItemData *data) { m_data = data; }
cac09ce8 185 void SetState(int state) { m_state = state; m_width = 0; }
f135ff73 186
ca65c044 187 void SetHasPlus(bool has = true) { m_hasPlus = has; }
f135ff73 188
cac09ce8
VZ
189 void SetBold(bool bold)
190 {
191 m_isBold = bold;
192
193 ResetTextSize();
194 }
ef44a621 195
9ec64fa7
VZ
196 int GetX() const { return m_x; }
197 int GetY() const { return m_y; }
f135ff73 198
9ec64fa7
VZ
199 void SetX(int x) { m_x = x; }
200 void SetY(int y) { m_y = y; }
f135ff73 201
cac09ce8
VZ
202 int GetHeight() const { return m_height; }
203 int GetWidth() const { return m_width; }
204
205 int GetTextHeight() const
206 {
207 wxASSERT_MSG( m_heightText != -1, "must call CalculateSize() first" );
208
209 return m_heightText;
210 }
211
212 int GetTextWidth() const
213 {
214 wxASSERT_MSG( m_widthText != -1, "must call CalculateSize() first" );
91b8de8d 215
cac09ce8
VZ
216 return m_widthText;
217 }
91b8de8d 218
9ec64fa7 219 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 220
cac09ce8
VZ
221 // sets the items font for the specified DC if it uses any special font or
222 // simply returns false otherwise
223 bool SetFont(wxGenericTreeCtrl *control, wxDC& dc) const
224 {
225 wxFont font;
226
227 wxTreeItemAttr * const attr = GetAttributes();
228 if ( attr && attr->HasFont() )
229 font = attr->GetFont();
230 else if ( IsBold() )
231 font = control->m_boldFont;
232 else
233 return false;
234
235 dc.SetFont(font);
236 return true;
237 }
238
9ec64fa7 239 // operations
66619ee5
VZ
240
241 // deletes all children notifying the treectrl about it
242 void DeleteChildren(wxGenericTreeCtrl *tree);
f135ff73 243
9ec64fa7 244 // get count of all children (and grand children if 'recursively')
ca65c044 245 size_t GetChildrenCount(bool recursively = true) const;
f135ff73 246
9ec64fa7 247 void Insert(wxGenericTreeItem *child, size_t index)
8239070b 248 { m_children.Insert(child, index); }
f135ff73 249
cac09ce8
VZ
250 // calculate and cache the item size using either the provided DC (which is
251 // supposed to have wxGenericTreeCtrl::m_normalFont selected into it!) or a
252 // wxClientDC on the control window
253 void CalculateSize(wxGenericTreeCtrl *control, wxDC& dc)
254 { DoCalculateSize(control, dc, true /* dc uses normal font */); }
255 void CalculateSize(wxGenericTreeCtrl *control);
256
941830cb 257 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
f135ff73 258
cac09ce8
VZ
259 void ResetSize() { m_width = 0; }
260 void ResetTextSize() { m_width = 0; m_widthText = -1; }
261 void RecursiveResetSize();
262 void RecursiveResetTextSize();
263
9ec64fa7 264 // return the item at given position (or NULL if no item), onButton is
ca65c044 265 // true if the point belongs to the item's button, otherwise it lies
f8b043e7 266 // on the item's label
618a5e38
RR
267 wxGenericTreeItem *HitTest( const wxPoint& point,
268 const wxGenericTreeCtrl *,
269 int &flags,
270 int level );
f135ff73 271
ca65c044
WS
272 void Expand() { m_isCollapsed = false; }
273 void Collapse() { m_isCollapsed = true; }
f135ff73 274
ca65c044 275 void SetHilight( bool set = true ) { m_hasHilight = set; }
f135ff73 276
9ec64fa7
VZ
277 // status inquiries
278 bool HasChildren() const { return !m_children.IsEmpty(); }
ef8698d6 279 bool IsSelected() const { return m_hasHilight != 0; }
9ec64fa7
VZ
280 bool IsExpanded() const { return !m_isCollapsed; }
281 bool HasPlus() const { return m_hasPlus || HasChildren(); }
ef8698d6 282 bool IsBold() const { return m_isBold != 0; }
9ec64fa7
VZ
283
284 // attributes
285 // get them - may be NULL
286 wxTreeItemAttr *GetAttributes() const { return m_attr; }
287 // get them ensuring that the pointer is not NULL
288 wxTreeItemAttr& Attr()
289 {
290 if ( !m_attr )
618a5e38 291 {
9ec64fa7 292 m_attr = new wxTreeItemAttr;
ca65c044 293 m_ownsAttr = true;
618a5e38 294 }
9ec64fa7
VZ
295 return *m_attr;
296 }
618a5e38
RR
297 // set them
298 void SetAttributes(wxTreeItemAttr *attr)
299 {
300 if ( m_ownsAttr ) delete m_attr;
301 m_attr = attr;
ca65c044 302 m_ownsAttr = false;
cac09ce8
VZ
303 m_width = 0;
304 m_widthText = -1;
618a5e38
RR
305 }
306 // set them and delete when done
307 void AssignAttributes(wxTreeItemAttr *attr)
308 {
309 SetAttributes(attr);
ca65c044 310 m_ownsAttr = true;
cac09ce8
VZ
311 m_width = 0;
312 m_widthText = -1;
618a5e38 313 }
f135ff73
VZ
314
315private:
cac09ce8
VZ
316 // calculate the size of this item, i.e. set m_width, m_height and
317 // m_widthText and m_heightText properly
318 //
319 // if dcUsesNormalFont is true, the current dc font must be the normal tree
320 // control font
321 void DoCalculateSize(wxGenericTreeCtrl *control,
322 wxDC& dc,
323 bool dcUsesNormalFont);
324
618a5e38
RR
325 // since there can be very many of these, we save size by chosing
326 // the smallest representation for the elements and by ordering
327 // the members to avoid padding.
328 wxString m_text; // label to be rendered for item
cac09ce8
VZ
329 int m_widthText;
330 int m_heightText;
618a5e38
RR
331
332 wxTreeItemData *m_data; // user-provided data
333
03966fcb
RR
334 int m_state; // item state
335
618a5e38
RR
336 wxArrayGenericTreeItems m_children; // list of children
337 wxGenericTreeItem *m_parent; // parent of this item
338
339 wxTreeItemAttr *m_attr; // attributes???
9ec64fa7
VZ
340
341 // tree ctrl images for the normal, selected, expanded and
342 // expanded+selected states
8253f2e0 343 int m_images[wxTreeItemIcon_Max];
9ec64fa7 344
618a5e38 345 wxCoord m_x; // (virtual) offset from top
e549bec4 346 wxCoord m_y; // (virtual) offset from left
8253f2e0
WS
347 int m_width; // width of this item
348 int m_height; // height of this item
9ec64fa7
VZ
349
350 // use bitfields to save size
9bb50fd0
VZ
351 unsigned int m_isCollapsed :1;
352 unsigned int m_hasHilight :1; // same as focused
353 unsigned int m_hasPlus :1; // used for item which doesn't have
9ec64fa7 354 // children but has a [+] button
9bb50fd0
VZ
355 unsigned int m_isBold :1; // render the label in bold font
356 unsigned int m_ownsAttr :1; // delete attribute when done
22f3361e 357
c0c133e1 358 wxDECLARE_NO_COPY_CLASS(wxGenericTreeItem);
f135ff73
VZ
359};
360
361// =============================================================================
362// implementation
363// =============================================================================
364
06b466c7
VZ
365// ----------------------------------------------------------------------------
366// private functions
367// ----------------------------------------------------------------------------
368
369// translate the key or mouse event flags to the type of selection we're
370// dealing with
371static void EventFlagsToSelType(long style,
372 bool shiftDown,
373 bool ctrlDown,
dfc6cd93
SB
374 bool &is_multiple,
375 bool &extended_select,
376 bool &unselect_others)
06b466c7 377{
dfc6cd93
SB
378 is_multiple = (style & wxTR_MULTIPLE) != 0;
379 extended_select = shiftDown && is_multiple;
380 unselect_others = !(extended_select || (ctrlDown && is_multiple));
06b466c7 381}
e179bd65 382
7475e8a3 383// check if the given item is under another one
8239070b
VZ
384static bool
385IsDescendantOf(const wxGenericTreeItem *parent, const wxGenericTreeItem *item)
7475e8a3
VZ
386{
387 while ( item )
388 {
389 if ( item == parent )
390 {
391 // item is a descendant of parent
ca65c044 392 return true;
7475e8a3
VZ
393 }
394
395 item = item->GetParent();
396 }
397
ca65c044 398 return false;
7475e8a3
VZ
399}
400
e179bd65
RR
401// -----------------------------------------------------------------------------
402// wxTreeRenameTimer (internal)
403// -----------------------------------------------------------------------------
404
941830cb 405wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
e179bd65
RR
406{
407 m_owner = owner;
408}
409
410void wxTreeRenameTimer::Notify()
411{
412 m_owner->OnRenameTimer();
413}
414
415//-----------------------------------------------------------------------------
416// wxTreeTextCtrl (internal)
417//-----------------------------------------------------------------------------
418
e179bd65
RR
419BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
420 EVT_CHAR (wxTreeTextCtrl::OnChar)
c13cace1 421 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
e179bd65
RR
422 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
423END_EVENT_TABLE()
424
edb8f298
VZ
425wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
426 wxGenericTreeItem *item)
427 : m_itemEdited(item), m_startValue(item->GetText())
428{
e179bd65 429 m_owner = owner;
33fe475a 430 m_aboutToFinish = false;
e179bd65 431
ce43f9e1
VZ
432 wxRect rect;
433 m_owner->GetBoundingRect(m_itemEdited, rect, true);
434
ec676e4f 435 // corrects position and size for better appearance
ce43f9e1
VZ
436#ifdef __WXMSW__
437 rect.x -= 5;
438 rect.width += 10;
ec676e4f 439#elif defined(__WXGTK__)
ce43f9e1
VZ
440 rect.x -= 5;
441 rect.y -= 2;
442 rect.width += 8;
443 rect.height += 4;
ec676e4f
VZ
444#elif defined(__WXMAC__)
445 int bestHeight = GetBestSize().y - 8;
446 if ( rect.height > bestHeight )
447 {
448 int diff = rect.height - bestHeight;
ce43f9e1
VZ
449 rect.height -= diff;
450 rect.y += diff / 2;
ec676e4f
VZ
451 }
452#endif // platforms
33ac7e6f 453
edb8f298 454 (void)Create(m_owner, wxID_ANY, m_startValue,
ce43f9e1
VZ
455 rect.GetPosition(), rect.GetSize());
456
457 SetSelection(-1, -1);
edb8f298 458}
574c939e 459
d0e2c75c
RR
460void wxTreeTextCtrl::EndEdit(bool discardChanges)
461{
462 m_aboutToFinish = true;
6621957f 463
d0e2c75c
RR
464 if ( discardChanges )
465 {
466 m_owner->OnRenameCancelled(m_itemEdited);
6621957f 467
a8a89154 468 Finish( true );
d0e2c75c
RR
469 }
470 else
471 {
472 // Notify the owner about the changes
473 AcceptChanges();
474
475 // Even if vetoed, close the control (consistent with MSW)
a8a89154 476 Finish( true );
d0e2c75c
RR
477 }
478}
479
edb8f298
VZ
480bool wxTreeTextCtrl::AcceptChanges()
481{
482 const wxString value = GetValue();
618a5e38 483
edb8f298
VZ
484 if ( value == m_startValue )
485 {
486 // nothing changed, always accept
c145a47c
RD
487 // when an item remains unchanged, the owner
488 // needs to be notified that the user decided
489 // not to change the tree item label, and that
490 // the edit has been cancelled
491
492 m_owner->OnRenameCancelled(m_itemEdited);
ca65c044 493 return true;
e179bd65 494 }
edb8f298
VZ
495
496 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
e179bd65 497 {
edb8f298 498 // vetoed by the user
ca65c044 499 return false;
edb8f298
VZ
500 }
501
502 // accepted, do rename the item
503 m_owner->SetItemText(m_itemEdited, value);
33ac7e6f 504
ca65c044 505 return true;
edb8f298
VZ
506}
507
d0e2c75c 508void wxTreeTextCtrl::Finish( bool setfocus )
edb8f298 509{
d0e2c75c 510 m_owner->ResetTextControl();
33ac7e6f 511
d0e2c75c 512 wxPendingDelete.Append(this);
9548f380 513
d0e2c75c 514 if (setfocus)
8cee4a30 515 m_owner->SetFocus();
edb8f298 516}
dd5a32cc 517
edb8f298
VZ
518void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
519{
520 switch ( event.m_keyCode )
521 {
522 case WXK_RETURN:
d0e2c75c 523 EndEdit( false );
2b4f324a 524 break;
edb8f298
VZ
525
526 case WXK_ESCAPE:
d0e2c75c 527 EndEdit( true );
edb8f298
VZ
528 break;
529
530 default:
531 event.Skip();
e179bd65 532 }
c13cace1
VS
533}
534
535void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
536{
d0e2c75c 537 if ( !m_aboutToFinish )
dd5a32cc 538 {
edb8f298
VZ
539 // auto-grow the textctrl:
540 wxSize parentSize = m_owner->GetSize();
541 wxPoint myPos = GetPosition();
542 wxSize mySize = GetSize();
543 int sx, sy;
9a83f860 544 GetTextExtent(GetValue() + wxT("M"), &sx, &sy);
edb8f298
VZ
545 if (myPos.x + sx > parentSize.x)
546 sx = parentSize.x - myPos.x;
547 if (mySize.x > sx)
548 sx = mySize.x;
422d0ff0 549 SetSize(sx, wxDefaultCoord);
dd5a32cc
RR
550 }
551
c13cace1 552 event.Skip();
e179bd65
RR
553}
554
dd5a32cc 555void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
e179bd65 556{
d0e2c75c 557 if ( !m_aboutToFinish )
edb8f298 558 {
33fe475a
RR
559 if ( !AcceptChanges() )
560 m_owner->OnRenameCancelled( m_itemEdited );
6621957f 561
d0e2c75c 562 Finish( false );
edb8f298 563 }
9146872c 564
d0e2c75c 565 // We should let the native text control handle focus, too.
9146872c 566 event.Skip();
e179bd65
RR
567}
568
f135ff73 569// -----------------------------------------------------------------------------
c801d85f 570// wxGenericTreeItem
f135ff73 571// -----------------------------------------------------------------------------
c801d85f 572
f135ff73
VZ
573wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
574 const wxString& text,
f135ff73
VZ
575 int image, int selImage,
576 wxTreeItemData *data)
577 : m_text(text)
c801d85f 578{
00e12320
RR
579 m_images[wxTreeItemIcon_Normal] = image;
580 m_images[wxTreeItemIcon_Selected] = selImage;
581 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
582 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
8dc99046 583
00e12320 584 m_data = data;
03966fcb 585 m_state = wxTREE_ITEMSTATE_NONE;
00e12320 586 m_x = m_y = 0;
f135ff73 587
ca65c044
WS
588 m_isCollapsed = true;
589 m_hasHilight = false;
590 m_hasPlus = false;
591 m_isBold = false;
c801d85f 592
00e12320 593 m_parent = parent;
f135ff73 594
d3b9f782 595 m_attr = NULL;
ca65c044 596 m_ownsAttr = false;
06b466c7 597
00e12320
RR
598 // We don't know the height here yet.
599 m_width = 0;
600 m_height = 0;
cac09ce8
VZ
601
602 m_widthText = -1;
603 m_heightText = -1;
edaa81ae 604}
c801d85f 605
f135ff73 606wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 607{
00e12320 608 delete m_data;
4832f7c0 609
618a5e38 610 if (m_ownsAttr) delete m_attr;
9ec64fa7 611
00e12320 612 wxASSERT_MSG( m_children.IsEmpty(),
8239070b 613 "must call DeleteChildren() before deleting the item" );
372edb9d
VZ
614}
615
941830cb 616void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
372edb9d 617{
b4a980f4 618 size_t count = m_children.GetCount();
00e12320 619 for ( size_t n = 0; n < count; n++ )
a43a4f9d 620 {
00e12320 621 wxGenericTreeItem *child = m_children[n];
66619ee5 622 tree->SendDeleteEvent(child);
a43a4f9d 623
00e12320 624 child->DeleteChildren(tree);
66619ee5 625 if ( child == tree->m_select_me )
5117f9bf 626 tree->m_select_me = NULL;
00e12320
RR
627 delete child;
628 }
372edb9d 629
00e12320 630 m_children.Empty();
edaa81ae 631}
c801d85f 632
4832f7c0 633size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 634{
b4a980f4 635 size_t count = m_children.GetCount();
00e12320
RR
636 if ( !recursively )
637 return count;
4832f7c0 638
00e12320 639 size_t total = count;
f2593d0d 640 for (size_t n = 0; n < count; ++n)
00e12320
RR
641 {
642 total += m_children[n]->GetChildrenCount();
643 }
c801d85f 644
00e12320 645 return total;
edaa81ae 646}
c801d85f 647
618a5e38
RR
648void wxGenericTreeItem::GetSize( int &x, int &y,
649 const wxGenericTreeCtrl *theButton )
c801d85f 650{
618a5e38 651 int bottomY=m_y+theButton->GetLineHeight(this);
00e12320
RR
652 if ( y < bottomY ) y = bottomY;
653 int width = m_x + m_width;
654 if ( x < width ) x = width;
f135ff73 655
00e12320 656 if (IsExpanded())
4832f7c0 657 {
b4a980f4 658 size_t count = m_children.GetCount();
00e12320
RR
659 for ( size_t n = 0; n < count; ++n )
660 {
618a5e38 661 m_children[n]->GetSize( x, y, theButton );
00e12320 662 }
df875e59 663 }
edaa81ae 664}
c801d85f 665
618a5e38
RR
666wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point,
667 const wxGenericTreeCtrl *theCtrl,
668 int &flags,
669 int level)
c801d85f 670{
618a5e38
RR
671 // for a hidden root node, don't evaluate it, but do evaluate children
672 if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) )
c801d85f 673 {
618a5e38
RR
674 // evaluate the item
675 int h = theCtrl->GetLineHeight(this);
676 if ((point.y > m_y) && (point.y < m_y + h))
f2593d0d 677 {
618a5e38
RR
678 int y_mid = m_y + h/2;
679 if (point.y < y_mid )
680 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
681 else
682 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
d3a9f4af 683
618a5e38 684 int xCross = m_x - theCtrl->GetSpacing();
a6b0ca75
SC
685#ifdef __WXMAC__
686 // according to the drawing code the triangels are drawn
687 // at -4 , -4 from the position up to +10/+10 max
688 if ((point.x > xCross-4) && (point.x < xCross+10) &&
689 (point.y > y_mid-4) && (point.y < y_mid+10) &&
690 HasPlus() && theCtrl->HasButtons() )
691#else
692 // 5 is the size of the plus sign
f8b043e7
RR
693 if ((point.x > xCross-6) && (point.x < xCross+6) &&
694 (point.y > y_mid-6) && (point.y < y_mid+6) &&
618a5e38 695 HasPlus() && theCtrl->HasButtons() )
a6b0ca75 696#endif
618a5e38
RR
697 {
698 flags |= wxTREE_HITTEST_ONITEMBUTTON;
699 return this;
700 }
0ae7f2a2 701
618a5e38
RR
702 if ((point.x >= m_x) && (point.x <= m_x+m_width))
703 {
704 int image_w = -1;
705 int image_h;
91b8de8d 706
618a5e38
RR
707 // assuming every image (normal and selected) has the same size!
708 if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal )
8239070b
VZ
709 {
710 theCtrl->m_imageListNormal->GetSize(GetImage(),
711 image_w, image_h);
712 }
03966fcb
RR
713
714 int state_w = -1;
715 int state_h;
618a5e38 716
8239070b
VZ
717 if ( (GetState() != wxTREE_ITEMSTATE_NONE) &&
718 theCtrl->m_imageListState )
719 {
720 theCtrl->m_imageListState->GetSize(GetState(),
721 state_w, state_h);
722 }
03966fcb
RR
723
724 if ((state_w != -1) && (point.x <= m_x + state_w + 1))
725 flags |= wxTREE_HITTEST_ONITEMSTATEICON;
726 else if ((image_w != -1) &&
727 (point.x <= m_x +
8239070b
VZ
728 (state_w != -1 ? state_w +
729 MARGIN_BETWEEN_STATE_AND_IMAGE
730 : 0)
03966fcb 731 + image_w + 1))
618a5e38
RR
732 flags |= wxTREE_HITTEST_ONITEMICON;
733 else
734 flags |= wxTREE_HITTEST_ONITEMLABEL;
735
736 return this;
737 }
738
739 if (point.x < m_x)
740 flags |= wxTREE_HITTEST_ONITEMINDENT;
741 if (point.x > m_x+m_width)
742 flags |= wxTREE_HITTEST_ONITEMRIGHT;
91b8de8d 743
f2593d0d
RR
744 return this;
745 }
91b8de8d 746
618a5e38 747 // if children are expanded, fall through to evaluate them
d3b9f782 748 if (m_isCollapsed) return NULL;
f2593d0d 749 }
618a5e38
RR
750
751 // evaluate children
b4a980f4 752 size_t count = m_children.GetCount();
618a5e38 753 for ( size_t n = 0; n < count; n++ )
c801d85f 754 {
618a5e38
RR
755 wxGenericTreeItem *res = m_children[n]->HitTest( point,
756 theCtrl,
757 flags,
758 level + 1 );
759 if ( res != NULL )
760 return res;
edaa81ae 761 }
f135ff73 762
d3b9f782 763 return NULL;
edaa81ae 764}
c801d85f 765
8dc99046
VZ
766int wxGenericTreeItem::GetCurrentImage() const
767{
768 int image = NO_IMAGE;
769 if ( IsExpanded() )
770 {
771 if ( IsSelected() )
772 {
773 image = GetImage(wxTreeItemIcon_SelectedExpanded);
774 }
775
776 if ( image == NO_IMAGE )
777 {
778 // we usually fall back to the normal item, but try just the
779 // expanded one (and not selected) first in this case
780 image = GetImage(wxTreeItemIcon_Expanded);
781 }
782 }
783 else // not expanded
784 {
785 if ( IsSelected() )
786 image = GetImage(wxTreeItemIcon_Selected);
787 }
788
618a5e38
RR
789 // maybe it doesn't have the specific image we want,
790 // try the default one instead
791 if ( image == NO_IMAGE ) image = GetImage();
8dc99046
VZ
792
793 return image;
794}
795
cac09ce8
VZ
796void wxGenericTreeItem::CalculateSize(wxGenericTreeCtrl* control)
797{
798 // check if we need to do anything before creating the DC
799 if ( m_width != 0 )
800 return;
801
802 wxClientDC dc(control);
803 DoCalculateSize(control, dc, false /* normal font not used */);
804}
805
806void
807wxGenericTreeItem::DoCalculateSize(wxGenericTreeCtrl* control,
808 wxDC& dc,
809 bool dcUsesNormalFont)
810{
811 if ( m_width != 0 ) // Size known, nothing to do
812 return;
813
814 if ( m_widthText == -1 )
815 {
816 bool fontChanged;
817 if ( SetFont(control, dc) )
818 {
819 fontChanged = true;
820 }
821 else // we have no special font
822 {
823 if ( !dcUsesNormalFont )
824 {
825 // but we do need to ensure that the normal font is used: notice
826 // that this doesn't count as changing the font as we don't need
827 // to restore it
828 dc.SetFont(control->m_normalFont);
829 }
830
831 fontChanged = false;
832 }
833
834 dc.GetTextExtent( GetText(), &m_widthText, &m_heightText );
835
836 // restore normal font if the DC used it previously and we changed it
837 if ( fontChanged )
838 dc.SetFont(control->m_normalFont);
839 }
840
841 int text_h = m_heightText + 2;
842
136b7e61 843 int image_h = 0, image_w = 0;
cac09ce8 844 int image = GetCurrentImage();
136b7e61 845 if ( image != NO_IMAGE && control->m_imageListNormal )
cac09ce8 846 {
136b7e61
VZ
847 control->m_imageListNormal->GetSize(image, image_w, image_h);
848 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
cac09ce8
VZ
849 }
850
851 int state_h = 0, state_w = 0;
852 int state = GetState();
136b7e61 853 if ( state != wxTREE_ITEMSTATE_NONE && control->m_imageListState )
cac09ce8 854 {
136b7e61
VZ
855 control->m_imageListState->GetSize(state, state_w, state_h);
856 if ( image_w != 0 )
857 state_w += MARGIN_BETWEEN_STATE_AND_IMAGE;
858 else
859 state_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
cac09ce8
VZ
860 }
861
136b7e61
VZ
862 int img_h = wxMax(state_h, image_h);
863 m_height = wxMax(img_h, text_h);
cac09ce8
VZ
864
865 if (m_height < 30)
866 m_height += 2; // at least 2 pixels
867 else
868 m_height += m_height / 10; // otherwise 10% extra spacing
869
870 if (m_height > control->m_lineHeight)
871 control->m_lineHeight = m_height;
872
873 m_width = state_w + image_w + m_widthText + 2;
874}
875
876void wxGenericTreeItem::RecursiveResetSize()
877{
878 m_width = 0;
879
880 const size_t count = m_children.Count();
881 for (size_t i = 0; i < count; i++ )
882 m_children[i]->RecursiveResetSize();
883}
884
885void wxGenericTreeItem::RecursiveResetTextSize()
886{
887 m_width = 0;
888 m_widthText = -1;
889
890 const size_t count = m_children.Count();
891 for (size_t i = 0; i < count; i++ )
892 m_children[i]->RecursiveResetTextSize();
893}
894
f135ff73 895// -----------------------------------------------------------------------------
941830cb 896// wxGenericTreeCtrl implementation
f135ff73
VZ
897// -----------------------------------------------------------------------------
898
8cee4a30 899IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxControl)
f135ff73 900
8cee4a30 901BEGIN_EVENT_TABLE(wxGenericTreeCtrl, wxTreeCtrlBase)
941830cb 902 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
ccdbdc89 903 EVT_SIZE (wxGenericTreeCtrl::OnSize)
941830cb 904 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
2a4a928d 905 EVT_KEY_DOWN (wxGenericTreeCtrl::OnKeyDown)
941830cb
JS
906 EVT_CHAR (wxGenericTreeCtrl::OnChar)
907 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
908 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
ca65c044 909 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY, wxGenericTreeCtrl::OnGetToolTip)
f135ff73
VZ
910END_EVENT_TABLE()
911
3a5bcc4d 912#if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
233058c7
JS
913/*
914 * wxTreeCtrl has to be a real class or we have problems with
915 * the run-time information.
916 */
917
918IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
919#endif
920
f135ff73
VZ
921// -----------------------------------------------------------------------------
922// construction/destruction
923// -----------------------------------------------------------------------------
91b8de8d 924
941830cb 925void wxGenericTreeCtrl::Init()
c801d85f 926{
8cee4a30
VZ
927 m_current =
928 m_key_current =
929 m_anchor =
d3b9f782 930 m_select_me = NULL;
ca65c044
WS
931 m_hasFocus = false;
932 m_dirty = false;
f135ff73 933
00e12320
RR
934 m_lineHeight = 10;
935 m_indent = 15;
936 m_spacing = 18;
f135ff73 937
b771aa29
VZ
938 m_hilightBrush = new wxBrush
939 (
a756f210 940 wxSystemSettings::GetColour
b771aa29
VZ
941 (
942 wxSYS_COLOUR_HIGHLIGHT
943 ),
04ee05f9 944 wxBRUSHSTYLE_SOLID
b771aa29
VZ
945 );
946
947 m_hilightUnfocusedBrush = new wxBrush
948 (
a756f210 949 wxSystemSettings::GetColour
b771aa29
VZ
950 (
951 wxSYS_COLOUR_BTNSHADOW
952 ),
04ee05f9 953 wxBRUSHSTYLE_SOLID
b771aa29 954 );
a4609ab8 955
8cee4a30
VZ
956 m_imageListButtons = NULL;
957 m_ownsImageListButtons = false;
978f38c2 958
00e12320 959 m_dragCount = 0;
ca65c044 960 m_isDragging = false;
f8b043e7
RR
961 m_dropTarget = m_oldSelection = NULL;
962 m_underMouse = NULL;
fbb12260 963 m_textCtrl = NULL;
9dfbf520 964
cb59313c 965 m_renameTimer = NULL;
e1983ab5 966
cb59313c
VZ
967 m_findTimer = NULL;
968
e3d64157 969 m_dropEffectAboveItem = false;
ec676e4f 970
c0d2308b
RR
971 m_dndEffect = NoEffect;
972 m_dndEffectItem = NULL;
e3d64157 973
ca65c044 974 m_lastOnSame = false;
f38374d0 975
cac09ce8 976#if defined( __WXMAC__ )
7eb8aeb8 977 m_normalFont = wxFont(wxOSX_SYSTEM_FONT_VIEWS);
f89d65ea 978#else
a756f210 979 m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
f89d65ea 980#endif
e90c93b6 981 m_boldFont = m_normalFont.Bold();
edaa81ae 982}
c801d85f 983
618a5e38
RR
984bool wxGenericTreeCtrl::Create(wxWindow *parent,
985 wxWindowID id,
986 const wxPoint& pos,
987 const wxSize& size,
988 long style,
8cee4a30 989 const wxValidator& validator,
618a5e38 990 const wxString& name )
c801d85f 991{
2593f033 992#ifdef __WXMAC__
6621957f
VZ
993 int major, minor;
994 wxGetOsVersion(&major, &minor);
574c939e 995
2593f033
RR
996 if (major < 10)
997 style |= wxTR_ROW_LINES;
842adaf6 998
ec2f1752
RR
999 if (style & wxTR_HAS_BUTTONS)
1000 style |= wxTR_NO_LINES;
9c7f49f5 1001#endif // __WXMAC__
17d5bdf9 1002
ec2f1752
RR
1003#ifdef __WXGTK20__
1004 if (style & wxTR_HAS_BUTTONS)
1005 style |= wxTR_NO_LINES;
1006#endif
1007
8cee4a30
VZ
1008 if ( !wxControl::Create( parent, id, pos, size,
1009 style|wxHSCROLL|wxVSCROLL,
1010 validator,
1011 name ) )
1012 return false;
618a5e38 1013
6f0dd6b2
VZ
1014 // If the tree display has no buttons, but does have
1015 // connecting lines, we can use a narrower layout.
1016 // It may not be a good idea to force this...
618a5e38
RR
1017 if (!HasButtons() && !HasFlag(wxTR_NO_LINES))
1018 {
1019 m_indent= 10;
1020 m_spacing = 10;
1021 }
978f38c2 1022
ca65c044 1023 wxVisualAttributes attr = GetDefaultAttributes();
fa47d7a7
VS
1024 SetOwnForegroundColour( attr.colFg );
1025 SetOwnBackgroundColour( attr.colBg );
92a4e4de
VZ
1026 if (!m_hasFont)
1027 SetOwnFont(attr.font);
c22886bd 1028
f0eebb39
VZ
1029 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1030 // style because we apparently get performance problems when using dotted
1031 // pen for drawing in some ports -- but under MSW it seems to work fine
1032#ifdef __WXMSW__
04ee05f9 1033 m_dottedPen = wxPen(*wxLIGHT_GREY, 0, wxPENSTYLE_DOT);
f0eebb39
VZ
1034#else
1035 m_dottedPen = *wxGREY_PEN;
1036#endif
f135ff73 1037
170acdc9 1038 SetInitialSize(size);
ca65c044
WS
1039
1040 return true;
edaa81ae 1041}
c801d85f 1042
941830cb 1043wxGenericTreeCtrl::~wxGenericTreeCtrl()
c801d85f 1044{
b771aa29
VZ
1045 delete m_hilightBrush;
1046 delete m_hilightUnfocusedBrush;
574c939e 1047
00e12320 1048 DeleteAllItems();
9dfbf520 1049
00e12320 1050 delete m_renameTimer;
cb59313c
VZ
1051 delete m_findTimer;
1052
cb59313c
VZ
1053 if (m_ownsImageListButtons)
1054 delete m_imageListButtons;
edaa81ae 1055}
c801d85f 1056
f135ff73
VZ
1057// -----------------------------------------------------------------------------
1058// accessors
1059// -----------------------------------------------------------------------------
1060
027d45e8 1061unsigned int wxGenericTreeCtrl::GetCount() const
c801d85f 1062{
ffda4dac
VZ
1063 if ( !m_anchor )
1064 {
1065 // the tree is empty
1066 return 0;
1067 }
1068
027d45e8 1069 unsigned int count = m_anchor->GetChildrenCount();
ffda4dac
VZ
1070 if ( !HasFlag(wxTR_HIDE_ROOT) )
1071 {
1072 // take the root itself into account
1073 count++;
1074 }
1075
1076 return count;
edaa81ae 1077}
c801d85f 1078
941830cb 1079void wxGenericTreeCtrl::SetIndent(unsigned int indent)
c801d85f 1080{
574c939e 1081 m_indent = (unsigned short) indent;
ca65c044 1082 m_dirty = true;
cf724bce
RR
1083}
1084
df74fdea
VZ
1085size_t
1086wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
1087 bool recursively) const
4832f7c0 1088{
f2593d0d 1089 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
4832f7c0 1090
941830cb 1091 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
4832f7c0
VZ
1092}
1093
618a5e38
RR
1094void wxGenericTreeCtrl::SetWindowStyle(const long styles)
1095{
867f2ca4
KH
1096 // Do not try to expand the root node if it hasn't been created yet
1097 if (m_anchor && !HasFlag(wxTR_HIDE_ROOT) && (styles & wxTR_HIDE_ROOT))
374a8e5c
VS
1098 {
1099 // if we will hide the root, make sure children are visible
1100 m_anchor->SetHasPlus();
1101 m_anchor->Expand();
1102 CalculatePositions();
1103 }
1104
1105 // right now, just sets the styles. Eventually, we may
1106 // want to update the inherited styles, but right now
1107 // none of the parents has updatable styles
618a5e38 1108 m_windowStyle = styles;
ca65c044 1109 m_dirty = true;
618a5e38
RR
1110}
1111
f135ff73
VZ
1112// -----------------------------------------------------------------------------
1113// functions to work with tree items
1114// -----------------------------------------------------------------------------
74bedbeb 1115
941830cb 1116wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
f135ff73 1117{
9548f380 1118 wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
4832f7c0 1119
941830cb 1120 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
edaa81ae 1121}
74bedbeb 1122
941830cb 1123int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
8dc99046 1124 wxTreeItemIcon which) const
74bedbeb 1125{
f2593d0d 1126 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 1127
941830cb 1128 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
edaa81ae 1129}
c801d85f 1130
941830cb 1131wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 1132{
f2593d0d 1133 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 1134
941830cb 1135 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
edaa81ae 1136}
c801d85f 1137
03966fcb
RR
1138int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId& item) const
1139{
1140 wxCHECK_MSG( item.IsOk(), wxTREE_ITEMSTATE_NONE, wxT("invalid tree item") );
1141
1142 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1143 return pItem->GetState();
1144}
1145
2b5f62a0
VZ
1146wxColour wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
1147{
1148 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
1149
1150 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1151 return pItem->Attr().GetTextColour();
1152}
1153
8239070b
VZ
1154wxColour
1155wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
2b5f62a0
VZ
1156{
1157 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
1158
1159 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1160 return pItem->Attr().GetBackgroundColour();
1161}
1162
1163wxFont wxGenericTreeCtrl::GetItemFont(const wxTreeItemId& item) const
1164{
1165 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
1166
1167 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1168 return pItem->Attr().GetFont();
1169}
1170
8239070b
VZ
1171void
1172wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
f135ff73 1173{
f2593d0d 1174 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 1175
941830cb 1176 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d 1177 pItem->SetText(text);
cac09ce8 1178 pItem->CalculateSize(this);
f2593d0d 1179 RefreshLine(pItem);
f135ff73 1180}
c801d85f 1181
941830cb 1182void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
8dc99046
VZ
1183 int image,
1184 wxTreeItemIcon which)
f135ff73 1185{
223d09f6 1186 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 1187
941830cb 1188 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
8dc99046 1189 pItem->SetImage(image, which);
cac09ce8 1190 pItem->CalculateSize(this);
8dc99046 1191 RefreshLine(pItem);
edaa81ae 1192}
c801d85f 1193
8239070b
VZ
1194void
1195wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 1196{
f2593d0d 1197 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 1198
74707d69
RR
1199 if (data)
1200 data->SetId( item );
ca65c044 1201
941830cb 1202 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
edaa81ae 1203}
c801d85f 1204
03966fcb
RR
1205void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId& item, int state)
1206{
1207 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1208
1209 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1210 pItem->SetState(state);
cac09ce8 1211 pItem->CalculateSize(this);
03966fcb
RR
1212 RefreshLine(pItem);
1213}
1214
941830cb 1215void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 1216{
f2593d0d 1217 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 1218
941830cb 1219 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
1220 pItem->SetHasPlus(has);
1221 RefreshLine(pItem);
edaa81ae 1222}
c801d85f 1223
941830cb 1224void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
ef44a621 1225{
9ec64fa7 1226 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 1227
9ec64fa7 1228 // avoid redrawing the tree if no real change
941830cb 1229 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
1230 if ( pItem->IsBold() != bold )
1231 {
1232 pItem->SetBold(bold);
3a1a76a8
VZ
1233
1234 // recalculate the item size as bold and non bold fonts have different
1235 // widths
cac09ce8 1236 pItem->CalculateSize(this);
9ec64fa7
VZ
1237 RefreshLine(pItem);
1238 }
1239}
1240
9fce43b7
RR
1241void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item,
1242 bool highlight)
1243{
1244 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1245
1246 wxColour fg, bg;
1247
1248 if (highlight)
1249 {
1250 bg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1251 fg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1252 }
1253
1254 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1255 pItem->Attr().SetTextColour(fg);
1256 pItem->Attr().SetBackgroundColour(bg);
1257 RefreshLine(pItem);
1258}
1259
941830cb 1260void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
9ec64fa7
VZ
1261 const wxColour& col)
1262{
1263 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1264
941830cb 1265 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
1266 pItem->Attr().SetTextColour(col);
1267 RefreshLine(pItem);
1268}
1269
941830cb 1270void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
9ec64fa7
VZ
1271 const wxColour& col)
1272{
1273 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1274
941830cb 1275 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
1276 pItem->Attr().SetBackgroundColour(col);
1277 RefreshLine(pItem);
1278}
1279
8239070b
VZ
1280void
1281wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
9ec64fa7
VZ
1282{
1283 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1284
941830cb 1285 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7 1286 pItem->Attr().SetFont(font);
cac09ce8
VZ
1287 pItem->ResetTextSize();
1288 pItem->CalculateSize(this);
ef44a621 1289 RefreshLine(pItem);
ef44a621
VZ
1290}
1291
3da2715f
JS
1292bool wxGenericTreeCtrl::SetFont( const wxFont &font )
1293{
8cee4a30 1294 wxTreeCtrlBase::SetFont(font);
3da2715f 1295
e90c93b6
VZ
1296 m_normalFont = font;
1297 m_boldFont = m_normalFont.Bold();
3da2715f 1298
cac09ce8
VZ
1299 if (m_anchor)
1300 m_anchor->RecursiveResetTextSize();
1301
ca65c044 1302 return true;
3da2715f
JS
1303}
1304
1305
f135ff73
VZ
1306// -----------------------------------------------------------------------------
1307// item status inquiries
1308// -----------------------------------------------------------------------------
1309
1ed01484 1310bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
c801d85f 1311{
ca65c044 1312 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1ed01484 1313
c22886bd 1314 // An item is only visible if it's not a descendant of a collapsed item
1ed01484 1315 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
c22886bd
RR
1316 wxGenericTreeItem* parent = pItem->GetParent();
1317 while (parent)
1318 {
1319 if (!parent->IsExpanded())
ca65c044 1320 return false;
c22886bd
RR
1321 parent = parent->GetParent();
1322 }
1ed01484
JS
1323
1324 int startX, startY;
1325 GetViewStart(& startX, & startY);
1326
1327 wxSize clientSize = GetClientSize();
1328
1329 wxRect rect;
1330 if (!GetBoundingRect(item, rect))
ca65c044 1331 return false;
c22886bd 1332 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
ca65c044 1333 return false;
1ed01484 1334 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
ca65c044 1335 return false;
1ed01484 1336 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
ca65c044 1337 return false;
f135ff73 1338
ca65c044 1339 return true;
edaa81ae 1340}
c801d85f 1341
941830cb 1342bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 1343{
ca65c044 1344 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
4832f7c0 1345
59a2e635
VZ
1346 // consider that the item does have children if it has the "+" button: it
1347 // might not have them (if it had never been expanded yet) but then it
1348 // could have them as well and it's better to err on this side rather than
1349 // disabling some operations which are restricted to the items with
1350 // children for an item which does have them
607d9cfc 1351 return ((wxGenericTreeItem*) item.m_pItem)->HasPlus();
edaa81ae 1352}
c801d85f 1353
941830cb 1354bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 1355{
ca65c044 1356 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
4832f7c0 1357
941830cb 1358 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
f135ff73 1359}
29d87bba 1360
941830cb 1361bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
f135ff73 1362{
ca65c044 1363 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
4832f7c0 1364
941830cb 1365 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
f135ff73 1366}
29d87bba 1367
941830cb 1368bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
ef44a621 1369{
ca65c044 1370 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
ef44a621 1371
941830cb 1372 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
ef44a621
VZ
1373}
1374
f135ff73
VZ
1375// -----------------------------------------------------------------------------
1376// navigation
1377// -----------------------------------------------------------------------------
29d87bba 1378
99006e44 1379wxTreeItemId wxGenericTreeCtrl::GetItemParent(const wxTreeItemId& item) const
f135ff73 1380{
618a5e38 1381 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 1382
618a5e38 1383 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
f135ff73 1384}
29d87bba 1385
ee4b2721
VZ
1386wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1387 wxTreeItemIdValue& cookie) const
f135ff73 1388{
618a5e38 1389 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1390
618a5e38
RR
1391 cookie = 0;
1392 return GetNextChild(item, cookie);
f135ff73 1393}
29d87bba 1394
ee4b2721
VZ
1395wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1396 wxTreeItemIdValue& cookie) const
1397{
1398 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1399
8239070b
VZ
1400 wxArrayGenericTreeItems&
1401 children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
ee4b2721
VZ
1402
1403 // it's ok to cast cookie to size_t, we never have indices big enough to
1404 // overflow "void *"
1405 size_t *pIndex = (size_t *)&cookie;
b4a980f4 1406 if ( *pIndex < children.GetCount() )
ee4b2721 1407 {
836543b8 1408 return children.Item((*pIndex)++);
ee4b2721
VZ
1409 }
1410 else
1411 {
1412 // there are no more of them
1413 return wxTreeItemId();
1414 }
1415}
1416
941830cb 1417wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
978f38c2 1418{
618a5e38 1419 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 1420
8239070b
VZ
1421 wxArrayGenericTreeItems&
1422 children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1423 return children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last());
978f38c2
VZ
1424}
1425
941830cb 1426wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
f135ff73 1427{
618a5e38 1428 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1429
618a5e38
RR
1430 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1431 wxGenericTreeItem *parent = i->GetParent();
1432 if ( parent == NULL )
1433 {
1434 // root item doesn't have any siblings
1435 return wxTreeItemId();
1436 }
4832f7c0 1437
618a5e38
RR
1438 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1439 int index = siblings.Index(i);
1440 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1441
618a5e38 1442 size_t n = (size_t)(index + 1);
8239070b
VZ
1443 return n == siblings.GetCount() ? wxTreeItemId()
1444 : wxTreeItemId(siblings[n]);
edaa81ae 1445}
c801d85f 1446
941830cb 1447wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 1448{
618a5e38 1449 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1450
618a5e38
RR
1451 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1452 wxGenericTreeItem *parent = i->GetParent();
1453 if ( parent == NULL )
1454 {
1455 // root item doesn't have any siblings
1456 return wxTreeItemId();
1457 }
4832f7c0 1458
618a5e38
RR
1459 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1460 int index = siblings.Index(i);
1461 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1462
618a5e38
RR
1463 return index == 0 ? wxTreeItemId()
1464 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 1465}
389cdc7a 1466
1ed01484
JS
1467// Only for internal use right now, but should probably be public
1468wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
f135ff73 1469{
618a5e38
RR
1470 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1471
1472 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1473
1474 // First see if there are any children.
1475 wxArrayGenericTreeItems& children = i->GetChildren();
1476 if (children.GetCount() > 0)
1477 {
1478 return children.Item(0);
1479 }
1480 else
1481 {
1482 // Try a sibling of this or ancestor instead
1483 wxTreeItemId p = item;
1484 wxTreeItemId toFind;
1485 do
1486 {
1487 toFind = GetNextSibling(p);
99006e44 1488 p = GetItemParent(p);
618a5e38
RR
1489 } while (p.IsOk() && !toFind.IsOk());
1490 return toFind;
1491 }
1ed01484
JS
1492}
1493
1ed01484
JS
1494wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1495{
618a5e38
RR
1496 wxTreeItemId id = GetRootItem();
1497 if (!id.IsOk())
1ed01484 1498 return id;
1ed01484 1499
618a5e38
RR
1500 do
1501 {
1502 if (IsVisible(id))
1503 return id;
1504 id = GetNext(id);
1505 } while (id.IsOk());
1506
1507 return wxTreeItemId();
1ed01484
JS
1508}
1509
941830cb 1510wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
f135ff73 1511{
618a5e38 1512 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f73eddd2 1513 wxASSERT_MSG( IsVisible(item), wxT("this item itself should be visible") );
29d87bba 1514
618a5e38
RR
1515 wxTreeItemId id = item;
1516 if (id.IsOk())
1517 {
1518 while (id = GetNext(id), id.IsOk())
1519 {
1520 if (IsVisible(id))
1521 return id;
1522 }
1523 }
1524 return wxTreeItemId();
f135ff73 1525}
29d87bba 1526
941830cb 1527wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
f135ff73 1528{
618a5e38 1529 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f73eddd2 1530 wxASSERT_MSG( IsVisible(item), wxT("this item itself should be visible") );
29d87bba 1531
f73eddd2
VZ
1532 // find out the starting point
1533 wxTreeItemId prevItem = GetPrevSibling(item);
1534 if ( !prevItem.IsOk() )
1535 {
1536 prevItem = GetItemParent(item);
1537 }
29d87bba 1538
f73eddd2
VZ
1539 // find the first visible item after it
1540 while ( prevItem.IsOk() && !IsVisible(prevItem) )
1541 {
1542 prevItem = GetNext(prevItem);
1543 if ( !prevItem.IsOk() || prevItem == item )
1544 {
1545 // there are no visible items before item
1546 return wxTreeItemId();
1547 }
1548 }
1549
1550 // from there we must be able to navigate until this item
1551 while ( prevItem.IsOk() )
1552 {
1553 const wxTreeItemId nextItem = GetNextVisible(prevItem);
1554 if ( !nextItem.IsOk() || nextItem == item )
1555 break;
1556
1557 prevItem = nextItem;
1558 }
1559
1560 return prevItem;
edaa81ae 1561}
c801d85f 1562
fbb12260
JS
1563// called by wxTextTreeCtrl when it marks itself for deletion
1564void wxGenericTreeCtrl::ResetTextControl()
1565{
eb7f24c1 1566 m_textCtrl = NULL;
fbb12260
JS
1567}
1568
cb59313c
VZ
1569// find the first item starting with the given prefix after the given item
1570wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent,
1571 const wxString& prefixOrig) const
1572{
1573 // match is case insensitive as this is more convenient to the user: having
1574 // to press Shift-letter to go to the item starting with a capital letter
1575 // would be too bothersome
1576 wxString prefix = prefixOrig.Lower();
1577
526b8142
VZ
1578 // determine the starting point: we shouldn't take the current item (this
1579 // allows to switch between two items starting with the same letter just by
1580 // pressing it) but we shouldn't jump to the next one if the user is
1581 // continuing to type as otherwise he might easily skip the item he wanted
cb59313c 1582 wxTreeItemId id = idParent;
526b8142
VZ
1583 if ( prefix.length() == 1 )
1584 {
1585 id = GetNext(id);
1586 }
cb59313c 1587
526b8142 1588 // look for the item starting with the given prefix after it
cb59313c
VZ
1589 while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) )
1590 {
1591 id = GetNext(id);
1592 }
1593
526b8142
VZ
1594 // if we haven't found anything...
1595 if ( !id.IsOk() )
1596 {
1597 // ... wrap to the beginning
1598 id = GetRootItem();
1599 if ( HasFlag(wxTR_HIDE_ROOT) )
1600 {
1601 // can't select virtual root
1602 id = GetNext(id);
1603 }
1604
1605 // and try all the items (stop when we get to the one we started from)
8239070b
VZ
1606 while ( id.IsOk() && id != idParent &&
1607 !GetItemText(id).Lower().StartsWith(prefix) )
526b8142
VZ
1608 {
1609 id = GetNext(id);
1610 }
b9d880d6
RR
1611 // If we haven't found the item, id.IsOk() will be false, as per
1612 // documentation
526b8142
VZ
1613 }
1614
cb59313c
VZ
1615 return id;
1616}
1617
f135ff73
VZ
1618// -----------------------------------------------------------------------------
1619// operations
1620// -----------------------------------------------------------------------------
1621
941830cb 1622wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
8cee4a30
VZ
1623 size_t previous,
1624 const wxString& text,
1625 int image,
1626 int selImage,
1627 wxTreeItemData *data)
c801d85f 1628{
941830cb 1629 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f49f2b0c
RR
1630 if ( !parent )
1631 {
1632 // should we give a warning here?
1633 return AddRoot(text, image, selImage, data);
1634 }
4832f7c0 1635
ca65c044 1636 m_dirty = true; // do this first so stuff below doesn't cause flicker
618a5e38 1637
f38374d0 1638 wxGenericTreeItem *item =
2b84e565 1639 new wxGenericTreeItem( parent, text, image, selImage, data );
74bedbeb 1640
f49f2b0c
RR
1641 if ( data != NULL )
1642 {
ee4b2721 1643 data->m_pItem = item;
f49f2b0c 1644 }
74bedbeb 1645
8cee4a30
VZ
1646 parent->Insert( item, previous == (size_t)-1 ? parent->GetChildren().size()
1647 : previous );
ef44a621 1648
96aaaff8 1649 InvalidateBestSize();
f49f2b0c 1650 return item;
4c681997
RR
1651}
1652
941830cb 1653wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
8cee4a30
VZ
1654 int image,
1655 int selImage,
1656 wxTreeItemData *data)
4c681997 1657{
8239070b 1658 wxCHECK_MSG( !m_anchor, wxTreeItemId(), "tree can have only one root" );
389cdc7a 1659
ca65c044 1660 m_dirty = true; // do this first so stuff below doesn't cause flicker
618a5e38 1661
d3b9f782 1662 m_anchor = new wxGenericTreeItem(NULL, text,
f135ff73 1663 image, selImage, data);
cf31a1d7
VS
1664 if ( data != NULL )
1665 {
ee4b2721 1666 data->m_pItem = m_anchor;
cf31a1d7
VS
1667 }
1668
618a5e38
RR
1669 if (HasFlag(wxTR_HIDE_ROOT))
1670 {
1671 // if root is hidden, make sure we can navigate
1672 // into children
1673 m_anchor->SetHasPlus();
999723f3
VS
1674 m_anchor->Expand();
1675 CalculatePositions();
618a5e38 1676 }
f38374d0 1677
f49f2b0c
RR
1678 if (!HasFlag(wxTR_MULTIPLE))
1679 {
1680 m_current = m_key_current = m_anchor;
ca65c044 1681 m_current->SetHilight( true );
f49f2b0c 1682 }
389cdc7a 1683
96aaaff8 1684 InvalidateBestSize();
f49f2b0c 1685 return m_anchor;
edaa81ae 1686}
c801d85f 1687
8cee4a30
VZ
1688wxTreeItemId wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId& parentId,
1689 const wxTreeItemId& idPrevious,
1690 const wxString& text,
1691 int image, int selImage,
1692 wxTreeItemData *data)
c801d85f 1693{
941830cb 1694 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1695 if ( !parent )
1696 {
1697 // should we give a warning here?
1698 return AddRoot(text, image, selImage, data);
1699 }
c801d85f 1700
4855a477
JS
1701 int index = -1;
1702 if (idPrevious.IsOk())
1703 {
8239070b
VZ
1704 index = parent->GetChildren().Index(
1705 (wxGenericTreeItem*) idPrevious.m_pItem);
4855a477 1706 wxASSERT_MSG( index != wxNOT_FOUND,
8239070b
VZ
1707 "previous item in wxGenericTreeCtrl::InsertItem() "
1708 "is not a sibling" );
4855a477 1709 }
06b466c7 1710
f2593d0d
RR
1711 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1712}
1713
74bedbeb 1714
941830cb 1715void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
a43a4f9d 1716{
09f277d6 1717 wxTreeEvent event(wxEVT_COMMAND_TREE_DELETE_ITEM, this, item);
eafd76b0 1718 GetEventHandler()->ProcessEvent( event );
a43a4f9d
VZ
1719}
1720
0cf3b542
RR
1721// Don't leave edit or selection on a child which is about to disappear
1722void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem* item)
1723{
8239070b
VZ
1724 if ( m_textCtrl && item != m_textCtrl->item() &&
1725 IsDescendantOf(item, m_textCtrl->item()) )
1726 {
d0e2c75c 1727 m_textCtrl->EndEdit( true );
0cf3b542 1728 }
8239070b
VZ
1729
1730 if ( item != m_key_current && IsDescendantOf(item, m_key_current) )
1731 {
0cf3b542
RR
1732 m_key_current = NULL;
1733 }
8239070b
VZ
1734
1735 if ( IsDescendantOf(item, m_select_me) )
1736 {
0cf3b542
RR
1737 m_select_me = item;
1738 }
8239070b
VZ
1739
1740 if ( item != m_current && IsDescendantOf(item, m_current) )
1741 {
af3961b3 1742 m_current->SetHilight( false );
0cf3b542
RR
1743 m_current = NULL;
1744 m_select_me = item;
1745 }
1746}
1747
941830cb 1748void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
372edb9d 1749{
ca65c044 1750 m_dirty = true; // do this first so stuff below doesn't cause flicker
618a5e38 1751
941830cb 1752 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
0cf3b542 1753 ChildrenClosing(item);
a43a4f9d 1754 item->DeleteChildren(this);
96aaaff8 1755 InvalidateBestSize();
372edb9d
VZ
1756}
1757
941830cb 1758void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1759{
ca65c044 1760 m_dirty = true; // do this first so stuff below doesn't cause flicker
618a5e38 1761
941830cb 1762 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
ff5bf259 1763
0cf3b542 1764 if (m_textCtrl != NULL && IsDescendantOf(item, m_textCtrl->item()))
eb7f24c1
VZ
1765 {
1766 // can't delete the item being edited, cancel editing it first
d0e2c75c 1767 m_textCtrl->EndEdit( true );
eb7f24c1
VZ
1768 }
1769
7475e8a3 1770 wxGenericTreeItem *parent = item->GetParent();
842adaf6 1771
668674c1
RR
1772 // if the selected item will be deleted, select the parent ...
1773 wxGenericTreeItem *to_be_selected = parent;
1774 if (parent)
1775 {
1776 // .. unless there is a next sibling like wxMSW does it
1777 int pos = parent->GetChildren().Index( item );
1778 if ((int)(parent->GetChildren().GetCount()) > pos+1)
1779 to_be_selected = parent->GetChildren().Item( pos+1 );
1780 }
7475e8a3
VZ
1781
1782 // don't keep stale pointers around!
1783 if ( IsDescendantOf(item, m_key_current) )
aaa2f297 1784 {
3e3a7b97
JS
1785 // Don't silently change the selection:
1786 // do it properly in idle time, so event
1787 // handlers get called.
ca65c044 1788
3e3a7b97
JS
1789 // m_key_current = parent;
1790 m_key_current = NULL;
1791 }
1792
1793 // m_select_me records whether we need to select
1794 // a different item, in idle time.
1795 if ( m_select_me && IsDescendantOf(item, m_select_me) )
1796 {
668674c1 1797 m_select_me = to_be_selected;
aaa2f297
VZ
1798 }
1799
7475e8a3
VZ
1800 if ( IsDescendantOf(item, m_current) )
1801 {
3e3a7b97
JS
1802 // Don't silently change the selection:
1803 // do it properly in idle time, so event
1804 // handlers get called.
ca65c044 1805
3e3a7b97
JS
1806 // m_current = parent;
1807 m_current = NULL;
668674c1 1808 m_select_me = to_be_selected;
7475e8a3
VZ
1809 }
1810
1811 // remove the item from the tree
97d7bfb8
RR
1812 if ( parent )
1813 {
1814 parent->GetChildren().Remove( item ); // remove by value
1815 }
7475e8a3 1816 else // deleting the root
aaa2f297 1817 {
7475e8a3
VZ
1818 // nothing will be left in the tree
1819 m_anchor = NULL;
aaa2f297
VZ
1820 }
1821
7475e8a3 1822 // and delete all of its children and the item itself now
97d7bfb8
RR
1823 item->DeleteChildren(this);
1824 SendDeleteEvent(item);
5117f9bf
JS
1825
1826 if (item == m_select_me)
1827 m_select_me = NULL;
1729813a 1828
97d7bfb8 1829 delete item;
96aaaff8
RD
1830
1831 InvalidateBestSize();
edaa81ae 1832}
c801d85f 1833
941830cb 1834void wxGenericTreeCtrl::DeleteAllItems()
c801d85f 1835{
97d7bfb8
RR
1836 if ( m_anchor )
1837 {
7475e8a3 1838 Delete(m_anchor);
97d7bfb8 1839 }
edaa81ae
RR
1840}
1841
941830cb 1842void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1843{
941830cb 1844 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1845
9a83f860 1846 wxCHECK_RET( item, wxT("invalid item in wxGenericTreeCtrl::Expand") );
7a944d2f 1847 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
9a83f860 1848 wxT("can't expand hidden root") );
f6bcfd97 1849
f2593d0d
RR
1850 if ( !item->HasPlus() )
1851 return;
978f38c2 1852
f2593d0d
RR
1853 if ( item->IsExpanded() )
1854 return;
f135ff73 1855
09f277d6 1856 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_EXPANDING, this, item);
004fd0c8 1857
eafd76b0 1858 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
f2593d0d
RR
1859 {
1860 // cancelled by program
1861 return;
1862 }
4832f7c0 1863
f2593d0d 1864 item->Expand();
17808a75 1865 if ( !IsFrozen() )
624f89c2
VZ
1866 {
1867 CalculatePositions();
f135ff73 1868
624f89c2
VZ
1869 RefreshSubtree(item);
1870 }
1871 else // frozen
1872 {
1873 m_dirty = true;
1874 }
f135ff73 1875
f2593d0d 1876 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
eafd76b0 1877 GetEventHandler()->ProcessEvent( event );
edaa81ae
RR
1878}
1879
941830cb 1880void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1881{
7a944d2f 1882 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
9a83f860 1883 wxT("can't collapse hidden root") );
999723f3 1884
941830cb 1885 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1886
f2593d0d
RR
1887 if ( !item->IsExpanded() )
1888 return;
f135ff73 1889
09f277d6 1890 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, this, item);
eafd76b0 1891 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
f2593d0d
RR
1892 {
1893 // cancelled by program
1894 return;
1895 }
4832f7c0 1896
0cf3b542 1897 ChildrenClosing(item);
f2593d0d 1898 item->Collapse();
f135ff73 1899
618a5e38 1900#if 0 // TODO why should items be collapsed recursively?
f2593d0d 1901 wxArrayGenericTreeItems& children = item->GetChildren();
b4a980f4 1902 size_t count = children.GetCount();
f2593d0d
RR
1903 for ( size_t n = 0; n < count; n++ )
1904 {
1905 Collapse(children[n]);
1906 }
618a5e38 1907#endif
f135ff73 1908
f2593d0d 1909 CalculatePositions();
f135ff73 1910
f2593d0d 1911 RefreshSubtree(item);
f135ff73 1912
f2593d0d 1913 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
eafd76b0 1914 GetEventHandler()->ProcessEvent( event );
edaa81ae 1915}
c801d85f 1916
941830cb 1917void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1918{
00e12320
RR
1919 Collapse(item);
1920 DeleteChildren(item);
edaa81ae 1921}
c801d85f 1922
941830cb 1923void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1924{
941830cb 1925 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
389cdc7a 1926
00e12320
RR
1927 if (item->IsExpanded())
1928 Collapse(itemId);
1929 else
1930 Expand(itemId);
f135ff73 1931}
389cdc7a 1932
941830cb 1933void wxGenericTreeCtrl::Unselect()
f135ff73 1934{
00e12320
RR
1935 if (m_current)
1936 {
ca65c044 1937 m_current->SetHilight( false );
00e12320 1938 RefreshLine( m_current );
02fe25c2
VZ
1939
1940 m_current = NULL;
3e3a7b97 1941 m_select_me = NULL;
00e12320 1942 }
edaa81ae 1943}
c801d85f 1944
5708ae18
VZ
1945void wxGenericTreeCtrl::ClearFocusedItem()
1946{
1947 wxTreeItemId item = GetFocusedItem();
1948 if ( item.IsOk() )
1949 SelectItem(item, false);
1950}
1951
1952void wxGenericTreeCtrl::SetFocusedItem(const wxTreeItemId& item)
1953{
1954 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1955
1956 SelectItem(item, true);
1957}
1958
941830cb 1959void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1960{
00e12320
RR
1961 if (item->IsSelected())
1962 {
ca65c044 1963 item->SetHilight(false);
00e12320
RR
1964 RefreshLine(item);
1965 }
d3a9f4af 1966
00e12320 1967 if (item->HasChildren())
88ac883a 1968 {
00e12320 1969 wxArrayGenericTreeItems& children = item->GetChildren();
b4a980f4 1970 size_t count = children.GetCount();
00e12320
RR
1971 for ( size_t n = 0; n < count; ++n )
1972 {
1973 UnselectAllChildren(children[n]);
1974 }
88ac883a
VZ
1975 }
1976}
f135ff73 1977
941830cb 1978void wxGenericTreeCtrl::UnselectAll()
88ac883a 1979{
0a33446c
VZ
1980 wxTreeItemId rootItem = GetRootItem();
1981
1982 // the tree might not have the root item at all
1983 if ( rootItem )
1984 {
1985 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1986 }
88ac883a
VZ
1987}
1988
5cb3a695
VZ
1989void wxGenericTreeCtrl::SelectChildren(const wxTreeItemId& parent)
1990{
1991 wxCHECK_RET( HasFlag(wxTR_MULTIPLE),
1992 "this only works with multiple selection controls" );
1993
1994 UnselectAll();
1995
1996 if ( !HasChildren(parent) )
1997 return;
1998
1999
2000 wxArrayGenericTreeItems&
2001 children = ((wxGenericTreeItem*) parent.m_pItem)->GetChildren();
2002 size_t count = children.GetCount();
2003
2004 wxGenericTreeItem *
2005 item = (wxGenericTreeItem*) ((wxTreeItemId)children[0]).m_pItem;
2006 wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
2007 event.m_itemOld = m_current;
2008
2009 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
2010 return;
2011
2012 for ( size_t n = 0; n < count; ++n )
2013 {
2014 m_current = m_key_current = children[n];
2015 m_current->SetHilight(true);
2016 RefreshSelected();
2017 }
2018
2019
2020 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
2021 GetEventHandler()->ProcessEvent( event );
2022}
2023
2024
88ac883a
VZ
2025// Recursive function !
2026// To stop we must have crt_item<last_item
91b8de8d 2027// Algorithm :
88ac883a 2028// Tag all next children, when no more children,
d3a9f4af 2029// Move to parent (not to tag)
91b8de8d 2030// Keep going... if we found last_item, we stop.
8239070b
VZ
2031bool
2032wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item,
2033 wxGenericTreeItem *last_item,
2034 bool select)
88ac883a
VZ
2035{
2036 wxGenericTreeItem *parent = crt_item->GetParent();
2037
00e12320
RR
2038 if (parent == NULL) // This is root item
2039 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 2040
91b8de8d 2041 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
2042 int index = children.Index(crt_item);
2043 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
2044
b4a980f4 2045 size_t count = children.GetCount();
88ac883a 2046 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320 2047 {
8239070b
VZ
2048 if ( TagAllChildrenUntilLast(children[n], last_item, select) )
2049 return true;
00e12320 2050 }
88ac883a
VZ
2051
2052 return TagNextChildren(parent, last_item, select);
2053}
2054
8239070b
VZ
2055bool
2056wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item,
2057 wxGenericTreeItem *last_item,
2058 bool select)
88ac883a 2059{
00e12320
RR
2060 crt_item->SetHilight(select);
2061 RefreshLine(crt_item);
d3a9f4af 2062
06b466c7 2063 if (crt_item==last_item)
ca65c044 2064 return true;
88ac883a 2065
00e12320 2066 if (crt_item->HasChildren())
88ac883a 2067 {
00e12320 2068 wxArrayGenericTreeItems& children = crt_item->GetChildren();
b4a980f4 2069 size_t count = children.GetCount();
00e12320
RR
2070 for ( size_t n = 0; n < count; ++n )
2071 {
06b466c7 2072 if (TagAllChildrenUntilLast(children[n], last_item, select))
ca65c044 2073 return true;
06b466c7 2074 }
88ac883a 2075 }
d3a9f4af 2076
ca65c044 2077 return false;
88ac883a
VZ
2078}
2079
8239070b
VZ
2080void
2081wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1,
2082 wxGenericTreeItem *item2)
88ac883a 2083{
3e3a7b97 2084 m_select_me = NULL;
88ac883a 2085
999836aa 2086 // item2 is not necessary after item1
f2593d0d 2087 // choice first' and 'last' between item1 and item2
999836aa
VZ
2088 wxGenericTreeItem *first= (item1->GetY()<item2->GetY()) ? item1 : item2;
2089 wxGenericTreeItem *last = (item1->GetY()<item2->GetY()) ? item2 : item1;
88ac883a 2090
f2593d0d 2091 bool select = m_current->IsSelected();
d3a9f4af 2092
f2593d0d
RR
2093 if ( TagAllChildrenUntilLast(first,last,select) )
2094 return;
88ac883a 2095
f2593d0d 2096 TagNextChildren(first,last,select);
88ac883a
VZ
2097}
2098
78f12104
VZ
2099void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId& itemId,
2100 bool unselect_others,
2101 bool extended_select)
d3a9f4af 2102{
223d09f6 2103 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 2104
3e3a7b97 2105 m_select_me = NULL;
ca65c044 2106
88ac883a 2107 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
941830cb 2108 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
88ac883a
VZ
2109
2110 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 2111 // wxT("this is a single selection tree") );
88ac883a
VZ
2112
2113 // to keep going anyhow !!!
d3a9f4af 2114 if (is_single)
8dc99046
VZ
2115 {
2116 if (item->IsSelected())
2117 return; // nothing to do
ca65c044
WS
2118 unselect_others = true;
2119 extended_select = false;
8dc99046
VZ
2120 }
2121 else if ( unselect_others && item->IsSelected() )
2122 {
2123 // selection change if there is more than one item currently selected
2124 wxArrayTreeItemIds selected_items;
2125 if ( GetSelections(selected_items) == 1 )
2126 return;
2127 }
d3a9f4af 2128
09f277d6 2129 wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
ee4b2721 2130 event.m_itemOld = m_current;
91b8de8d 2131 // TODO : Here we don't send any selection mode yet !
d3a9f4af 2132
f98e2558 2133 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
618a5e38 2134 return;
f135ff73 2135
99006e44 2136 wxTreeItemId parent = GetItemParent( itemId );
2cc78389
RR
2137 while (parent.IsOk())
2138 {
2139 if (!IsExpanded(parent))
2140 Expand( parent );
cdb3cffe 2141
99006e44 2142 parent = GetItemParent( parent );
2cc78389 2143 }
cdb3cffe 2144
88ac883a
VZ
2145 // ctrl press
2146 if (unselect_others)
389cdc7a 2147 {
88ac883a 2148 if (is_single) Unselect(); // to speed up thing
c193b707 2149 else UnselectAll();
edaa81ae 2150 }
f135ff73 2151
88ac883a 2152 // shift press
d3a9f4af 2153 if (extended_select)
88ac883a 2154 {
aaa2f297
VZ
2155 if ( !m_current )
2156 {
8239070b
VZ
2157 m_current =
2158 m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
2159 }
2160
88ac883a
VZ
2161 // don't change the mark (m_current)
2162 SelectItemRange(m_current, item);
2163 }
2164 else
2165 {
ca65c044 2166 bool select = true; // the default
88ac883a 2167
c193b707
VZ
2168 // Check if we need to toggle hilight (ctrl mode)
2169 if (!unselect_others)
618a5e38 2170 select=!item->IsSelected();
88ac883a 2171
91b8de8d 2172 m_current = m_key_current = item;
c193b707
VZ
2173 m_current->SetHilight(select);
2174 RefreshLine( m_current );
88ac883a 2175 }
389cdc7a 2176
7ede7389
JS
2177 // This can cause idle processing to select the root
2178 // if no item is selected, so it must be after the
2179 // selection is set
2180 EnsureVisible( itemId );
2181
f135ff73 2182 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 2183 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
2184}
2185
78f12104
VZ
2186void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, bool select)
2187{
756c98b0
VZ
2188 wxGenericTreeItem * const item = (wxGenericTreeItem*) itemId.m_pItem;
2189 wxCHECK_RET( item, wxT("SelectItem(): invalid tree item") );
2190
78f12104
VZ
2191 if ( select )
2192 {
756c98b0
VZ
2193 if ( !item->IsSelected() )
2194 DoSelectItem(itemId, !HasFlag(wxTR_MULTIPLE));
78f12104
VZ
2195 }
2196 else // deselect
2197 {
714dfaa6
RR
2198 wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
2199 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
2200 return;
2201
ca65c044 2202 item->SetHilight(false);
78f12104 2203 RefreshLine(item);
b9d880d6 2204
714dfaa6
RR
2205 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
2206 GetEventHandler()->ProcessEvent( event );
78f12104
VZ
2207 }
2208}
2209
941830cb 2210void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
cb59313c 2211 wxArrayTreeItemIds &array) const
c801d85f 2212{
8dc99046 2213 if ( item->IsSelected() )
9dfbf520 2214 array.Add(wxTreeItemId(item));
91b8de8d 2215
9dfbf520 2216 if ( item->HasChildren() )
91b8de8d 2217 {
9dfbf520
VZ
2218 wxArrayGenericTreeItems& children = item->GetChildren();
2219 size_t count = children.GetCount();
2220 for ( size_t n = 0; n < count; ++n )
f6bcfd97 2221 FillArray(children[n], array);
91b8de8d
RR
2222 }
2223}
2224
941830cb 2225size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d 2226{
618a5e38
RR
2227 array.Empty();
2228 wxTreeItemId idRoot = GetRootItem();
2229 if ( idRoot.IsOk() )
2230 {
2231 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
2232 }
2233 //else: the tree is empty, so no selections
91b8de8d 2234
b4a980f4 2235 return array.GetCount();
91b8de8d
RR
2236}
2237
941830cb 2238void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 2239{
05b2a432
RD
2240 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
2241
91b8de8d
RR
2242 if (!item.IsOk()) return;
2243
941830cb 2244 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 2245
f65635b5
VZ
2246 // first expand all parent branches
2247 wxGenericTreeItem *parent = gitem->GetParent();
999723f3
VS
2248
2249 if ( HasFlag(wxTR_HIDE_ROOT) )
f65635b5 2250 {
ff38281a 2251 while ( parent && parent != m_anchor )
999723f3
VS
2252 {
2253 Expand(parent);
2254 parent = parent->GetParent();
2255 }
2256 }
2257 else
2258 {
2259 while ( parent )
2260 {
2261 Expand(parent);
2262 parent = parent->GetParent();
2263 }
f65635b5
VZ
2264 }
2265
5391f772 2266 //if (parent) CalculatePositions();
91b8de8d
RR
2267
2268 ScrollTo(item);
2269}
2270
941830cb 2271void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d 2272{
842adaf6
VZ
2273 if (!item.IsOk())
2274 return;
91b8de8d 2275
842adaf6 2276 // update the control before scrolling it
ca65c044 2277 if (m_dirty)
f89d65ea
SC
2278#if defined( __WXMSW__ ) || defined(__WXMAC__)
2279 Update();
2280#else
27f8357f 2281 DoDirtyProcessing();
f89d65ea 2282#endif
842adaf6 2283
941830cb 2284 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2285
842adaf6 2286 int itemY = gitem->GetY();
8dc99046 2287
0659e7ee
RR
2288 int start_x = 0;
2289 int start_y = 0;
1e6feb95 2290 GetViewStart( &start_x, &start_y );
842adaf6
VZ
2291
2292 const int clientHeight = GetClientSize().y;
2293
2294 const int itemHeight = GetLineHeight(gitem) + 2;
2295
2296 if ( itemY + itemHeight > start_y*PIXELS_PER_UNIT + clientHeight )
2297 {
2298 // need to scroll up by enough to show this item fully
2299 itemY += itemHeight - clientHeight;
2300 }
2301 else if ( itemY > start_y*PIXELS_PER_UNIT )
2302 {
2303 // item is already fully visible, don't do anything
2304 return;
0659e7ee 2305 }
842adaf6
VZ
2306 //else: scroll down to make this item the top one displayed
2307
2308 Scroll(-1, itemY/PIXELS_PER_UNIT);
edaa81ae 2309}
c801d85f 2310
e1ee62bd 2311// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 2312static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 2313
004fd0c8 2314static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 2315 wxGenericTreeItem **item2)
edaa81ae 2316{
8239070b
VZ
2317 wxCHECK_MSG( s_treeBeingSorted, 0,
2318 "bug in wxGenericTreeCtrl::SortChildren()" );
e1ee62bd
VZ
2319
2320 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
2321}
2322
941830cb 2323void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 2324{
223d09f6 2325 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 2326
941830cb 2327 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 2328
e1ee62bd 2329 wxCHECK_RET( !s_treeBeingSorted,
941830cb 2330 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 2331
91b8de8d 2332 wxArrayGenericTreeItems& children = item->GetChildren();
b4a980f4 2333 if ( children.GetCount() > 1 )
e1ee62bd 2334 {
ca65c044 2335 m_dirty = true;
618a5e38 2336
e1ee62bd
VZ
2337 s_treeBeingSorted = this;
2338 children.Sort(tree_ctrl_compare_func);
2339 s_treeBeingSorted = NULL;
e1ee62bd
VZ
2340 }
2341 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
2342}
2343
618a5e38 2344void wxGenericTreeCtrl::CalculateLineHeight()
e2414cbe 2345{
f2593d0d
RR
2346 wxClientDC dc(this);
2347 m_lineHeight = (int)(dc.GetCharHeight() + 4);
06b466c7 2348
618a5e38
RR
2349 if ( m_imageListNormal )
2350 {
2351 // Calculate a m_lineHeight value from the normal Image sizes.
2352 // May be toggle off. Then wxGenericTreeCtrl will spread when
2353 // necessary (which might look ugly).
2354 int n = m_imageListNormal->GetImageCount();
2355 for (int i = 0; i < n ; i++)
2356 {
2357 int width = 0, height = 0;
2358 m_imageListNormal->GetSize(i, width, height);
2359 if (height > m_lineHeight) m_lineHeight = height;
2360 }
2361 }
2362
4754ab16
RR
2363 if ( m_imageListState )
2364 {
2365 // Calculate a m_lineHeight value from the state Image sizes.
2366 // May be toggle off. Then wxGenericTreeCtrl will spread when
2367 // necessary (which might look ugly).
2368 int n = m_imageListState->GetImageCount();
2369 for (int i = 0; i < n ; i++)
2370 {
2371 int width = 0, height = 0;
2372 m_imageListState->GetSize(i, width, height);
2373 if (height > m_lineHeight) m_lineHeight = height;
2374 }
2375 }
2376
618a5e38 2377 if (m_imageListButtons)
f2593d0d 2378 {
618a5e38
RR
2379 // Calculate a m_lineHeight value from the Button image sizes.
2380 // May be toggle off. Then wxGenericTreeCtrl will spread when
2381 // necessary (which might look ugly).
2382 int n = m_imageListButtons->GetImageCount();
2383 for (int i = 0; i < n ; i++)
2384 {
2385 int width = 0, height = 0;
2386 m_imageListButtons->GetSize(i, width, height);
2387 if (height > m_lineHeight) m_lineHeight = height;
2388 }
f2593d0d 2389 }
91b8de8d 2390
618a5e38 2391 if (m_lineHeight < 30)
f2593d0d 2392 m_lineHeight += 2; // at least 2 pixels
06b466c7 2393 else
f2593d0d 2394 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 2395}
e2414cbe 2396
618a5e38
RR
2397void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
2398{
2399 if (m_ownsImageListNormal) delete m_imageListNormal;
2400 m_imageListNormal = imageList;
ca65c044
WS
2401 m_ownsImageListNormal = false;
2402 m_dirty = true;
cac09ce8
VZ
2403
2404 if (m_anchor)
2405 m_anchor->RecursiveResetSize();
2406
f4bd6759
JS
2407 // Don't do any drawing if we're setting the list to NULL,
2408 // since we may be in the process of deleting the tree control.
2409 if (imageList)
2410 CalculateLineHeight();
618a5e38
RR
2411}
2412
941830cb 2413void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 2414{
46cd520d 2415 if (m_ownsImageListState) delete m_imageListState;
f135ff73 2416 m_imageListState = imageList;
ca65c044 2417 m_ownsImageListState = false;
4754ab16 2418 m_dirty = true;
cac09ce8
VZ
2419
2420 if (m_anchor)
2421 m_anchor->RecursiveResetSize();
2422
4754ab16
RR
2423 // Don't do any drawing if we're setting the list to NULL,
2424 // since we may be in the process of deleting the tree control.
2425 if (imageList)
2426 CalculateLineHeight();
46cd520d
VS
2427}
2428
618a5e38
RR
2429void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
2430{
2431 if (m_ownsImageListButtons) delete m_imageListButtons;
2432 m_imageListButtons = imageList;
ca65c044
WS
2433 m_ownsImageListButtons = false;
2434 m_dirty = true;
cac09ce8
VZ
2435
2436 if (m_anchor)
2437 m_anchor->RecursiveResetSize();
2438
618a5e38
RR
2439 CalculateLineHeight();
2440}
2441
618a5e38
RR
2442void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
2443{
2444 SetButtonsImageList(imageList);
ca65c044 2445 m_ownsImageListButtons = true;
618a5e38
RR
2446}
2447
f135ff73
VZ
2448// -----------------------------------------------------------------------------
2449// helpers
2450// -----------------------------------------------------------------------------
0659e7ee 2451
941830cb 2452void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 2453{
0659e7ee
RR
2454 if (m_anchor)
2455 {
618a5e38 2456 int x = 0, y = 0;
91b8de8d 2457 m_anchor->GetSize( x, y, this );
c193b707 2458 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 2459 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
2460 int x_pos = GetScrollPos( wxHORIZONTAL );
2461 int y_pos = GetScrollPos( wxVERTICAL );
8239070b
VZ
2462 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT,
2463 x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT,
2464 x_pos, y_pos );
0659e7ee
RR
2465 }
2466 else
2467 {
2468 SetScrollbars( 0, 0, 0, 0 );
2469 }
edaa81ae 2470}
c801d85f 2471
941830cb 2472int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 2473{
dc6c62a9
RR
2474 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2475 return item->GetHeight();
2476 else
2477 return m_lineHeight;
91b8de8d
RR
2478}
2479
941830cb 2480void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 2481{
cac09ce8 2482 item->SetFont(this, dc);
e253521d 2483 item->CalculateSize(this, dc);
ef44a621 2484
cac09ce8 2485 wxCoord text_h = item->GetTextHeight();
ef44a621 2486
618a5e38 2487 int image_h = 0, image_w = 0;
8dc99046
VZ
2488 int image = item->GetCurrentImage();
2489 if ( image != NO_IMAGE )
bbe0af5b 2490 {
2c8e4738
VZ
2491 if ( m_imageListNormal )
2492 {
136b7e61 2493 m_imageListNormal->GetSize(image, image_w, image_h);
3e4f8ee2 2494 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
2c8e4738
VZ
2495 }
2496 else
2497 {
2498 image = NO_IMAGE;
2499 }
bbe0af5b 2500 }
ef44a621 2501
03966fcb
RR
2502 int state_h = 0, state_w = 0;
2503 int state = item->GetState();
2504 if ( state != wxTREE_ITEMSTATE_NONE )
2505 {
2506 if ( m_imageListState )
2507 {
136b7e61
VZ
2508 m_imageListState->GetSize(state, state_w, state_h);
2509 if ( image_w != 0 )
03966fcb
RR
2510 state_w += MARGIN_BETWEEN_STATE_AND_IMAGE;
2511 else
2512 state_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
2513 }
2514 else
2515 {
2516 state = wxTREE_ITEMSTATE_NONE;
2517 }
2518 }
2519
91b8de8d 2520 int total_h = GetLineHeight(item);
cac09ce8
VZ
2521 bool drawItemBackground = false,
2522 hasBgColour = false;
91b8de8d 2523
b771aa29 2524 if ( item->IsSelected() )
cdb3cffe 2525 {
b771aa29 2526 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
6c051af4 2527 drawItemBackground = true;
cdb3cffe 2528 }
afa6a1a1 2529 else
c0fba4d1
VS
2530 {
2531 wxColour colBg;
cac09ce8 2532 wxTreeItemAttr * const attr = item->GetAttributes();
c0fba4d1 2533 if ( attr && attr->HasBackgroundColour() )
902725ee 2534 {
cac09ce8
VZ
2535 drawItemBackground =
2536 hasBgColour = true;
c0fba4d1 2537 colBg = attr->GetBackgroundColour();
902725ee 2538 }
c0fba4d1 2539 else
902725ee 2540 {
8cee4a30 2541 colBg = GetBackgroundColour();
902725ee 2542 }
04ee05f9 2543 dc.SetBrush(wxBrush(colBg, wxBRUSHSTYLE_SOLID));
c0fba4d1 2544 }
afa6a1a1 2545
cdb3cffe 2546 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
b77d9650 2547
c6f4913a 2548 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
a69cb1cc 2549 {
9e435585
JS
2550 int x, w, h;
2551 x=0;
2552 GetVirtualSize(&w, &h);
ccdbdc89 2553 wxRect rect( x, item->GetY()+offset, w, total_h-offset);
ccdbdc89
RR
2554 if (!item->IsSelected())
2555 {
2556 dc.DrawRectangle(rect);
2557 }
2558 else
2559 {
05d97538 2560 int flags = wxCONTROL_SELECTED;
ed9a7a63 2561 if (m_hasFocus
530a427a 2562#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
ed9a7a63
KO
2563 && IsControlActive( (ControlRef)GetHandle() )
2564#endif
2565 )
05d97538 2566 flags |= wxCONTROL_FOCUSED;
ccdbdc89 2567 if ((item == m_current) && (m_hasFocus))
05d97538 2568 flags |= wxCONTROL_CURRENT;
8239070b
VZ
2569
2570 wxRendererNative::Get().
2571 DrawItemSelectionRect(this, dc, rect, flags);
ccdbdc89 2572 }
a69cb1cc 2573 }
419875a6 2574 else // no full row highlight
b77d9650 2575 {
03966fcb
RR
2576 if ( item->IsSelected() &&
2577 (state != wxTREE_ITEMSTATE_NONE || image != NO_IMAGE) )
c6f4913a 2578 {
03966fcb
RR
2579 // If it's selected, and there's an state image or normal image,
2580 // then we should take care to leave the area under the image
2581 // painted in the background colour.
8239070b
VZ
2582 wxRect rect( item->GetX() + state_w + image_w - 2,
2583 item->GetY() + offset,
2584 item->GetWidth() - state_w - image_w + 2,
2585 total_h - offset );
a4609ab8 2586#if !defined(__WXGTK20__) && !defined(__WXMAC__)
ccdbdc89
RR
2587 dc.DrawRectangle( rect );
2588#else
ccdbdc89
RR
2589 rect.x -= 1;
2590 rect.width += 2;
6621957f 2591
05d97538
RR
2592 int flags = wxCONTROL_SELECTED;
2593 if (m_hasFocus)
2594 flags |= wxCONTROL_FOCUSED;
ccdbdc89 2595 if ((item == m_current) && (m_hasFocus))
05d97538 2596 flags |= wxCONTROL_CURRENT;
8239070b
VZ
2597 wxRendererNative::Get().
2598 DrawItemSelectionRect(this, dc, rect, flags);
ccdbdc89 2599#endif
c6f4913a 2600 }
602f0c99
JS
2601 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2602 // don't allow backgrounds to be customized. Not drawing the background,
2603 // except for custom item backgrounds, works for both kinds of theme.
6c051af4 2604 else if (drawItemBackground)
c6f4913a 2605 {
136b7e61
VZ
2606 wxRect rect( item->GetX() + state_w + image_w - 2,
2607 item->GetY() + offset,
2608 item->GetWidth() - state_w - image_w + 2,
2609 total_h - offset );
cac09ce8 2610 if ( hasBgColour )
ccdbdc89
RR
2611 {
2612 dc.DrawRectangle( rect );
2613 }
419875a6 2614 else // no specific background colour
ccdbdc89 2615 {
ccdbdc89
RR
2616 rect.x -= 1;
2617 rect.width += 2;
6621957f 2618
05d97538
RR
2619 int flags = wxCONTROL_SELECTED;
2620 if (m_hasFocus)
2621 flags |= wxCONTROL_FOCUSED;
ccdbdc89 2622 if ((item == m_current) && (m_hasFocus))
05d97538 2623 flags |= wxCONTROL_CURRENT;
8239070b
VZ
2624 wxRendererNative::Get().
2625 DrawItemSelectionRect(this, dc, rect, flags);
ccdbdc89 2626 }
c6f4913a 2627 }
b77d9650 2628 }
ef44a621 2629
03966fcb
RR
2630 if ( state != wxTREE_ITEMSTATE_NONE )
2631 {
2632 dc.SetClippingRegion( item->GetX(), item->GetY(), state_w, total_h );
2633 m_imageListState->Draw( state, dc,
2634 item->GetX(),
8239070b
VZ
2635 item->GetY() +
2636 (total_h > state_h ? (total_h-state_h)/2
2637 : 0),
03966fcb
RR
2638 wxIMAGELIST_DRAW_TRANSPARENT );
2639 dc.DestroyClippingRegion();
2640 }
2641
8dc99046 2642 if ( image != NO_IMAGE )
bbe0af5b 2643 {
8239070b
VZ
2644 dc.SetClippingRegion(item->GetX() + state_w, item->GetY(),
2645 image_w, total_h);
8dc99046 2646 m_imageListNormal->Draw( image, dc,
4754ab16 2647 item->GetX() + state_w,
8239070b
VZ
2648 item->GetY() +
2649 (total_h > image_h ? (total_h-image_h)/2
2650 : 0),
bbe0af5b
RR
2651 wxIMAGELIST_DRAW_TRANSPARENT );
2652 dc.DestroyClippingRegion();
2653 }
ef44a621 2654
04ee05f9 2655 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
f6bcfd97
BP
2656 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2657 dc.DrawText( item->GetText(),
03966fcb 2658 (wxCoord)(state_w + image_w + item->GetX()),
f6bcfd97 2659 (wxCoord)(item->GetY() + extraH));
ef44a621 2660
eff869aa
RR
2661 // restore normal font
2662 dc.SetFont( m_normalFont );
ec676e4f 2663
c0d2308b
RR
2664 if (item == m_dndEffectItem)
2665 {
2666 dc.SetPen( *wxBLACK_PEN );
2667 // DnD visual effects
2668 switch (m_dndEffect)
2669 {
2670 case BorderEffect:
2671 {
2672 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2673 int w = item->GetWidth() + 2;
2674 int h = total_h + 2;
2675 dc.DrawRectangle( item->GetX() - 1, item->GetY() - 1, w, h);
2676 break;
2677 }
2678 case AboveEffect:
2679 {
2680 int x = item->GetX(),
2681 y = item->GetY();
2682 dc.DrawLine( x, y, x + item->GetWidth(), y);
2683 break;
2684 }
2685 case BelowEffect:
2686 {
2687 int x = item->GetX(),
2688 y = item->GetY();
2689 y += total_h - 1;
2690 dc.DrawLine( x, y, x + item->GetWidth(), y);
2691 break;
2692 }
2693 case NoEffect:
2694 break;
2695 }
2696 }
ef44a621
VZ
2697}
2698
8239070b
VZ
2699void
2700wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem *item,
2701 wxDC &dc,
2702 int level,
2703 int &y)
c801d85f 2704{
618a5e38
RR
2705 int x = level*m_indent;
2706 if (!HasFlag(wxTR_HIDE_ROOT))
2707 {
2708 x += m_indent;
2709 }
2710 else if (level == 0)
2711 {
2b84e565
VS
2712 // always expand hidden root
2713 int origY = y;
618a5e38 2714 wxArrayGenericTreeItems& children = item->GetChildren();
b4a980f4 2715 int count = children.GetCount();
2b84e565
VS
2716 if (count > 0)
2717 {
2718 int n = 0, oldY;
2719 do {
2720 oldY = y;
2721 PaintLevel(children[n], dc, 1, y);
2722 } while (++n < count);
2723
8239070b
VZ
2724 if ( !HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT)
2725 && count > 0 )
2b84e565
VS
2726 {
2727 // draw line down to last child
2728 origY += GetLineHeight(children[0])>>1;
2729 oldY += GetLineHeight(children[n-1])>>1;
2730 dc.DrawLine(3, origY, 3, oldY);
2731 }
2732 }
618a5e38
RR
2733 return;
2734 }
91b8de8d 2735
618a5e38
RR
2736 item->SetX(x+m_spacing);
2737 item->SetY(y);
389cdc7a 2738
618a5e38
RR
2739 int h = GetLineHeight(item);
2740 int y_top = y;
2741 int y_mid = y_top + (h>>1);
2742 y += h;
4832f7c0 2743
618a5e38
RR
2744 int exposed_x = dc.LogicalToDeviceX(0);
2745 int exposed_y = dc.LogicalToDeviceY(y_top);
233058c7 2746
618a5e38 2747 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
bbe0af5b 2748 {
f516d986 2749 const wxPen *pen =
c6f4913a
VS
2750#ifndef __WXMAC__
2751 // don't draw rect outline if we already have the
2752 // background color under Mac
2753 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2754#endif // !__WXMAC__
2755 wxTRANSPARENT_PEN;
2756
2757 wxColour colText;
633ecf26 2758 if ( item->IsSelected()
530a427a 2759#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
633ecf26
RD
2760 // On wxMac, if the tree doesn't have the focus we draw an empty
2761 // rectangle, so we want to make sure that the text is visible
2762 // against the normal background, not the highlightbackground, so
2763 // don't use the highlight text colour unless we have the focus.
ed9a7a63 2764 && m_hasFocus && IsControlActive( (ControlRef)GetHandle() )
633ecf26
RD
2765#endif
2766 )
c6f4913a 2767 {
4b8fa634
KO
2768#ifdef __WXMAC__
2769 colText = *wxWHITE;
2770#else
a756f210 2771 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
4b8fa634 2772#endif
c6f4913a
VS
2773 }
2774 else
2775 {
2776 wxTreeItemAttr *attr = item->GetAttributes();
2777 if (attr && attr->HasTextColour())
2778 colText = attr->GetTextColour();
2779 else
6f0dd6b2 2780 colText = GetForegroundColour();
c6f4913a
VS
2781 }
2782
2783 // prepare to draw
2784 dc.SetTextForeground(colText);
2785 dc.SetPen(*pen);
2786
2787 // draw
2788 PaintItem(item, dc);
2789
2790 if (HasFlag(wxTR_ROW_LINES))
2791 {
2792 // if the background colour is white, choose a
2793 // contrasting color for the lines
2794 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2795 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2796 dc.DrawLine(0, y_top, 10000, y_top);
2797 dc.DrawLine(0, y, 10000, y);
2798 }
2799
2800 // restore DC objects
2801 dc.SetBrush(*wxWHITE_BRUSH);
2802 dc.SetPen(m_dottedPen);
2803 dc.SetTextForeground(*wxBLACK);
2804
9c7f49f5 2805 if ( !HasFlag(wxTR_NO_LINES) )
cdb3cffe 2806 {
9c7f49f5
VZ
2807 // draw the horizontal line here
2808 int x_start = x;
2809 if (x > (signed)m_indent)
2810 x_start -= m_indent;
2811 else if (HasFlag(wxTR_LINES_AT_ROOT))
2812 x_start = 3;
2813 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2814 }
618a5e38 2815
9c7f49f5
VZ
2816 // should the item show a button?
2817 if ( item->HasPlus() && HasButtons() )
2818 {
2819 if ( m_imageListButtons )
c22886bd 2820 {
618a5e38 2821 // draw the image button here
9c7f49f5
VZ
2822 int image_h = 0,
2823 image_w = 0;
2824 int image = item->IsExpanded() ? wxTreeItemIcon_Expanded
2825 : wxTreeItemIcon_Normal;
2826 if ( item->IsSelected() )
2b84e565 2827 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
9c7f49f5 2828
618a5e38 2829 m_imageListButtons->GetSize(image, image_w, image_h);
9c7f49f5
VZ
2830 int xx = x - image_w/2;
2831 int yy = y_mid - image_h/2;
2832
2833 wxDCClipper clip(dc, xx, yy, image_w, image_h);
618a5e38
RR
2834 m_imageListButtons->Draw(image, dc, xx, yy,
2835 wxIMAGELIST_DRAW_TRANSPARENT);
c22886bd 2836 }
9c7f49f5 2837 else // no custom buttons
c22886bd 2838 {
0e7761fa
VZ
2839 static const int wImage = 9;
2840 static const int hImage = 9;
ca65c044 2841
f8b043e7
RR
2842 int flag = 0;
2843 if (item->IsExpanded())
2844 flag |= wxCONTROL_EXPANDED;
2845 if (item == m_underMouse)
2846 flag |= wxCONTROL_CURRENT;
ca65c044 2847
9c7f49f5
VZ
2848 wxRendererNative::Get().DrawTreeItemButton
2849 (
2850 this,
2851 dc,
2852 wxRect(x - wImage/2,
2853 y_mid - hImage/2,
2854 wImage, hImage),
f8b043e7 2855 flag
9c7f49f5 2856 );
c22886bd 2857 }
bbe0af5b 2858 }
f135ff73 2859 }
d3a9f4af 2860
bbe0af5b
RR
2861 if (item->IsExpanded())
2862 {
91b8de8d 2863 wxArrayGenericTreeItems& children = item->GetChildren();
b4a980f4 2864 int count = children.GetCount();
f65635b5 2865 if (count > 0)
9ec64fa7 2866 {
618a5e38
RR
2867 int n = 0, oldY;
2868 ++level;
2869 do {
2870 oldY = y;
2871 PaintLevel(children[n], dc, level, y);
2872 } while (++n < count);
2873
e76520fd 2874 if (!HasFlag(wxTR_NO_LINES) && count > 0)
618a5e38
RR
2875 {
2876 // draw line down to last child
2b84e565 2877 oldY += GetLineHeight(children[n-1])>>1;
618a5e38 2878 if (HasButtons()) y_mid += 5;
dd360466 2879
8239070b
VZ
2880 // Only draw the portion of the line that is visible, in case
2881 // it is huge
ca65c044 2882 wxCoord xOrigin=0, yOrigin=0, width, height;
dd360466
RD
2883 dc.GetDeviceOrigin(&xOrigin, &yOrigin);
2884 yOrigin = abs(yOrigin);
2885 GetClientSize(&width, &height);
2886
2887 // Move end points to the begining/end of the view?
2888 if (y_mid < yOrigin)
2889 y_mid = yOrigin;
2890 if (oldY > yOrigin + height)
2891 oldY = yOrigin + height;
2892
8239070b
VZ
2893 // after the adjustments if y_mid is larger than oldY then the
2894 // line isn't visible at all so don't draw anything
dd360466
RD
2895 if (y_mid < oldY)
2896 dc.DrawLine(x, y_mid, x, oldY);
618a5e38 2897 }
9ec64fa7 2898 }
bbe0af5b 2899 }
4c681997 2900}
c801d85f 2901
941830cb 2902void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
2903{
2904 if ( item )
2905 {
2906 if ( item->HasPlus() )
2907 {
2908 // it's a folder, indicate it by a border
2909 DrawBorder(item);
2910 }
2911 else
2912 {
2913 // draw a line under the drop target because the item will be
2914 // dropped there
e3d64157 2915 DrawLine(item, !m_dropEffectAboveItem );
3dbeaa52
VZ
2916 }
2917
c0d2308b 2918 SetCursor(*wxSTANDARD_CURSOR);
3dbeaa52
VZ
2919 }
2920 else
2921 {
2922 // can't drop here
2923 SetCursor(wxCURSOR_NO_ENTRY);
2924 }
2925}
2926
941830cb 2927void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 2928{
8239070b 2929 wxCHECK_RET( item.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
91b8de8d 2930
941830cb 2931 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2932
c0d2308b
RR
2933 if (m_dndEffect == NoEffect)
2934 {
2935 m_dndEffect = BorderEffect;
2936 m_dndEffectItem = i;
2937 }
2938 else
2939 {
2940 m_dndEffect = NoEffect;
2941 m_dndEffectItem = NULL;
2942 }
ec676e4f 2943
c0d2308b
RR
2944 wxRect rect( i->GetX()-1, i->GetY()-1, i->GetWidth()+2, GetLineHeight(i)+2 );
2945 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2946 RefreshRect( rect );
91b8de8d
RR
2947}
2948
941830cb 2949void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 2950{
8239070b 2951 wxCHECK_RET( item.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
91b8de8d 2952
941830cb 2953 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2954
c0d2308b 2955 if (m_dndEffect == NoEffect)
3dbeaa52 2956 {
c0d2308b
RR
2957 if (below)
2958 m_dndEffect = BelowEffect;
2959 else
2960 m_dndEffect = AboveEffect;
2961 m_dndEffectItem = i;
3dbeaa52 2962 }
c0d2308b
RR
2963 else
2964 {
2965 m_dndEffect = NoEffect;
2966 m_dndEffectItem = NULL;
2967 }
ec676e4f 2968
c0d2308b
RR
2969 wxRect rect( i->GetX()-1, i->GetY()-1, i->GetWidth()+2, GetLineHeight(i)+2 );
2970 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2971 RefreshRect( rect );
91b8de8d
RR
2972}
2973
f135ff73 2974// -----------------------------------------------------------------------------
77ffb593 2975// wxWidgets callbacks
f135ff73
VZ
2976// -----------------------------------------------------------------------------
2977
ccdbdc89
RR
2978void wxGenericTreeCtrl::OnSize( wxSizeEvent &event )
2979{
2980#ifdef __WXGTK__
2981 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT) && m_current)
2982 RefreshLine( m_current );
2983#endif
2984
2985 event.Skip(true);
2986}
2987
941830cb 2988void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 2989{
0659e7ee
RR
2990 wxPaintDC dc(this);
2991 PrepareDC( dc );
29d87bba 2992
941830cb
JS
2993 if ( !m_anchor)
2994 return;
2995
eff869aa 2996 dc.SetFont( m_normalFont );
0659e7ee 2997 dc.SetPen( m_dottedPen );
f38374d0 2998
eff869aa 2999 // this is now done dynamically
91b8de8d
RR
3000 //if(GetImageList() == NULL)
3001 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 3002
91b8de8d 3003 int y = 2;
0659e7ee 3004 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 3005}
c801d85f 3006
b771aa29 3007void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
c801d85f 3008{
ca65c044 3009 m_hasFocus = true;
978f38c2 3010
b771aa29
VZ
3011 RefreshSelected();
3012
3013 event.Skip();
edaa81ae 3014}
c801d85f 3015
b771aa29 3016void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
c801d85f 3017{
ca65c044 3018 m_hasFocus = false;
978f38c2 3019
b771aa29
VZ
3020 RefreshSelected();
3021
3022 event.Skip();
edaa81ae 3023}
c801d85f 3024
2a4a928d 3025void wxGenericTreeCtrl::OnKeyDown( wxKeyEvent &event )
c801d85f 3026{
2a4a928d 3027 // send a tree event
09f277d6 3028 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, this);
b09bda68 3029 te.m_evtKey = event;
68eb5f63 3030 if ( GetEventHandler()->ProcessEvent( te ) )
68eb5f63 3031 return;
435fe83e 3032
2a4a928d
SL
3033 event.Skip();
3034}
3035
3036void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
3037{
91b8de8d 3038 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
3039 {
3040 event.Skip();
3041 return;
3042 }
ef44a621 3043
06b466c7
VZ
3044 // how should the selection work for this event?
3045 bool is_multiple, extended_select, unselect_others;
3046 EventFlagsToSelType(GetWindowStyleFlag(),
3047 event.ShiftDown(),
105fc244 3048 event.CmdDown(),
dfc6cd93
SB
3049 is_multiple, extended_select, unselect_others);
3050
03703fc0
RR
3051 if (GetLayoutDirection() == wxLayout_RightToLeft)
3052 {
3053 if (event.GetKeyCode() == WXK_RIGHT)
3054 event.m_keyCode = WXK_LEFT;
3055 else if (event.GetKeyCode() == WXK_LEFT)
3056 event.m_keyCode = WXK_RIGHT;
3057 }
3058
dfc6cd93
SB
3059 // + : Expand
3060 // - : Collaspe
f6bcfd97 3061 // * : Expand all/Collapse all
dfc6cd93
SB
3062 // ' ' | return : activate
3063 // up : go up (not last children!)
3064 // down : go down
3065 // left : go to parent
3066 // right : open if parent and go next
3067 // home : go to root
3068 // end : go to last item without opening parents
cb59313c 3069 // alnum : start or continue searching for the item with this prefix
12a3f227 3070 int keyCode = event.GetKeyCode();
fbf33856
VZ
3071
3072#ifdef __WXOSX__
3073 // Make the keys work as they do in the native control:
3074 // right => expand
3075 // left => collapse if current item is expanded
3076 if (keyCode == WXK_RIGHT)
3077 {
3078 keyCode = '+';
3079 }
3080 else if (keyCode == WXK_LEFT && IsExpanded(m_current))
3081 {
3082 keyCode = '-';
3083 }
3084#endif // __WXOSX__
3085
cb59313c 3086 switch ( keyCode )
978f38c2
VZ
3087 {
3088 case '+':
3089 case WXK_ADD:
3090 if (m_current->HasPlus() && !IsExpanded(m_current))
3091 {
3092 Expand(m_current);
3093 }
3094 break;
ef44a621 3095
f6bcfd97
BP
3096 case '*':
3097 case WXK_MULTIPLY:
3098 if ( !IsExpanded(m_current) )
3099 {
3100 // expand all
1a51c85f 3101 ExpandAllChildren(m_current);
f6bcfd97
BP
3102 break;
3103 }
3104 //else: fall through to Collapse() it
3105
978f38c2
VZ
3106 case '-':
3107 case WXK_SUBTRACT:
3108 if (IsExpanded(m_current))
3109 {
3110 Collapse(m_current);
3111 }
3112 break;
ef44a621 3113
f7c6f947
RR
3114 case WXK_MENU:
3115 {
8239070b
VZ
3116 // Use the item's bounding rectangle to determine position for
3117 // the event
39faaf39
KH
3118 wxRect ItemRect;
3119 GetBoundingRect(m_current, ItemRect, true);
3120
8239070b
VZ
3121 wxTreeEvent
3122 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU, this, m_current);
39faaf39 3123 // Use the left edge, vertical middle
4e115ed2 3124 eventMenu.m_pointDrag = wxPoint(ItemRect.GetX(),
8239070b
VZ
3125 ItemRect.GetY() +
3126 ItemRect.GetHeight() / 2);
4e115ed2 3127 GetEventHandler()->ProcessEvent( eventMenu );
f7c6f947 3128 }
09f277d6
VZ
3129 break;
3130
978f38c2
VZ
3131 case ' ':
3132 case WXK_RETURN:
6151e144 3133 if ( !event.HasModifiers() )
978f38c2 3134 {
8239070b
VZ
3135 wxTreeEvent
3136 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, this, m_current);
4e115ed2 3137 GetEventHandler()->ProcessEvent( eventAct );
978f38c2 3138 }
6151e144
VZ
3139
3140 // in any case, also generate the normal key event for this key,
3141 // even if we generated the ACTIVATED event above: this is what
3142 // wxMSW does and it makes sense because you might not want to
3143 // process ACTIVATED event at all and handle Space and Return
3144 // directly (and differently) which would be impossible otherwise
3145 event.Skip();
978f38c2 3146 break;
ef44a621 3147
618a5e38
RR
3148 // up goes to the previous sibling or to the last
3149 // of its children if it's expanded
978f38c2
VZ
3150 case WXK_UP:
3151 {
91b8de8d 3152 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
3153 if (!prev)
3154 {
99006e44 3155 prev = GetItemParent( m_key_current );
618a5e38
RR
3156 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
3157 {
3158 break; // don't go to root if it is hidden
3159 }
c193b707
VZ
3160 if (prev)
3161 {
2d75caaa 3162 wxTreeItemIdValue cookie;
91b8de8d 3163 wxTreeItemId current = m_key_current;
8239070b
VZ
3164 // TODO: Huh? If we get here, we'd better be the first
3165 // child of our parent. How else could it be?
618a5e38 3166 if (current == GetFirstChild( prev, cookie ))
90e58684
RR
3167 {
3168 // otherwise we return to where we came from
8239070b
VZ
3169 DoSelectItem(prev,
3170 unselect_others,
3171 extended_select);
3172 m_key_current = (wxGenericTreeItem*) prev.m_pItem;
90e58684 3173 break;
c193b707 3174 }
978f38c2
VZ
3175 }
3176 }
3177 if (prev)
3178 {
69a282d4 3179 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 3180 {
69a282d4
VZ
3181 wxTreeItemId child = GetLastChild(prev);
3182 if ( child )
3183 {
3184 prev = child;
3185 }
978f38c2 3186 }
69a282d4 3187
78f12104 3188 DoSelectItem( prev, unselect_others, extended_select );
941830cb 3189 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
3190 }
3191 }
3192 break;
ef44a621 3193
978f38c2
VZ
3194 // left arrow goes to the parent
3195 case WXK_LEFT:
3196 {
99006e44 3197 wxTreeItemId prev = GetItemParent( m_current );
618a5e38
RR
3198 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
3199 {
3200 // don't go to root if it is hidden
3201 prev = GetPrevSibling( m_current );
3202 }
978f38c2
VZ
3203 if (prev)
3204 {
78f12104 3205 DoSelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
3206 }
3207 }
3208 break;
ef44a621 3209
978f38c2 3210 case WXK_RIGHT:
618a5e38
RR
3211 // this works the same as the down arrow except that we
3212 // also expand the item if it wasn't expanded yet
1457ea31 3213 if (m_current != GetRootItem().m_pItem || !HasFlag(wxTR_HIDE_ROOT))
6f7f0d0a
VZ
3214 Expand(m_current);
3215 //else: don't try to expand hidden root item (which can be the
3216 // current one when the tree is empty)
3217
978f38c2
VZ
3218 // fall through
3219
3220 case WXK_DOWN:
ef44a621 3221 {
91b8de8d 3222 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2 3223 {
2d75caaa 3224 wxTreeItemIdValue cookie;
91b8de8d 3225 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
6f7f0d0a
VZ
3226 if ( !child )
3227 break;
3228
78f12104 3229 DoSelectItem( child, unselect_others, extended_select );
941830cb 3230 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
3231 }
3232 else
3233 {
91b8de8d 3234 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 3235 if (!next)
978f38c2 3236 {
91b8de8d 3237 wxTreeItemId current = m_key_current;
a0fad723 3238 while (current.IsOk() && !next)
978f38c2 3239 {
99006e44 3240 current = GetItemParent( current );
978f38c2
VZ
3241 if (current) next = GetNextSibling( current );
3242 }
3243 }
b62c3631 3244 if (next)
978f38c2 3245 {
78f12104 3246 DoSelectItem( next, unselect_others, extended_select );
941830cb 3247 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
3248 }
3249 }
ef44a621 3250 }
978f38c2 3251 break;
ef44a621 3252
978f38c2
VZ
3253 // <End> selects the last visible tree item
3254 case WXK_END:
3255 {
3256 wxTreeItemId last = GetRootItem();
3257
3258 while ( last.IsOk() && IsExpanded(last) )
3259 {
3260 wxTreeItemId lastChild = GetLastChild(last);
3261
3262 // it may happen if the item was expanded but then all of
3263 // its children have been deleted - so IsExpanded() returned
ca65c044 3264 // true, but GetLastChild() returned invalid item
978f38c2
VZ
3265 if ( !lastChild )
3266 break;
3267
3268 last = lastChild;
3269 }
3270
3271 if ( last.IsOk() )
3272 {
78f12104 3273 DoSelectItem( last, unselect_others, extended_select );
978f38c2
VZ
3274 }
3275 }
3276 break;
3277
3278 // <Home> selects the root item
3279 case WXK_HOME:
3280 {
3281 wxTreeItemId prev = GetRootItem();
cb59313c
VZ
3282 if (!prev)
3283 break;
3284
3285 if ( HasFlag(wxTR_HIDE_ROOT) )
978f38c2 3286 {
2d75caaa
VZ
3287 wxTreeItemIdValue cookie;
3288 prev = GetFirstChild(prev, cookie);
cb59313c
VZ
3289 if (!prev)
3290 break;
978f38c2 3291 }
cb59313c 3292
78f12104 3293 DoSelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
3294 }
3295 break;
3296
3297 default:
cb59313c 3298 // do not use wxIsalnum() here
6151e144 3299 if ( !event.HasModifiers() &&
1ec3a984
RR
3300 ((keyCode >= '0' && keyCode <= '9') ||
3301 (keyCode >= 'a' && keyCode <= 'z') ||
3302 (keyCode >= 'A' && keyCode <= 'Z' )))
cb59313c
VZ
3303 {
3304 // find the next item starting with the given prefix
bef7a62d 3305 wxChar ch = (wxChar)keyCode;
6151e144 3306
bef7a62d 3307 wxTreeItemId id = FindItem(m_current, m_findPrefix + ch);
cb59313c
VZ
3308 if ( !id.IsOk() )
3309 {
3310 // no such item
3311 break;
3312 }
3313
3314 SelectItem(id);
3315
3316 m_findPrefix += ch;
3317
3318 // also start the timer to reset the current prefix if the user
3319 // doesn't press any more alnum keys soon -- we wouldn't want
3320 // to use this prefix for a new item search
3321 if ( !m_findTimer )
3322 {
3323 m_findTimer = new wxTreeFindTimer(this);
3324 }
3325
3326 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
3327 }
3328 else
3329 {
3330 event.Skip();
3331 }
978f38c2 3332 }
edaa81ae 3333}
c801d85f 3334
be0e5d69
VZ
3335wxTreeItemId
3336wxGenericTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const
4f22cf8d 3337{
91b8de8d
RR
3338 int w, h;
3339 GetSize(&w, &h);
91b8de8d 3340 flags=0;
618a5e38
RR
3341 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
3342 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
3343 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
3344 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
3345 if (flags) return wxTreeItemId();
d3a9f4af 3346
618a5e38
RR
3347 if (m_anchor == NULL)
3348 {
3349 flags = wxTREE_HITTEST_NOWHERE;
3350 return wxTreeItemId();
3351 }
5716a1ab 3352
d027179b
VS
3353 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
3354 this, flags, 0);
618a5e38
RR
3355 if (hit == NULL)
3356 {
3357 flags = wxTREE_HITTEST_NOWHERE;
3358 return wxTreeItemId();
3359 }
3360 return hit;
4f22cf8d
RR
3361}
3362
8fb3bfe2
JS
3363// get the bounding rectangle of the item (or of its label only)
3364bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
edb8f298 3365 wxRect& rect,
3e4f8ee2 3366 bool textOnly) const
8fb3bfe2 3367{
8239070b
VZ
3368 wxCHECK_MSG( item.IsOk(), false,
3369 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
8fb3bfe2
JS
3370
3371 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
3372
3e4f8ee2
VZ
3373 if ( textOnly )
3374 {
136b7e61
VZ
3375 int image_h = 0, image_w = 0;
3376 int image = ((wxGenericTreeItem*) item.m_pItem)->GetCurrentImage();
3377 if ( image != NO_IMAGE && m_imageListNormal )
3378 {
3379 m_imageListNormal->GetSize( image, image_w, image_h );
3380 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
3381 }
3e4f8ee2 3382
136b7e61
VZ
3383 int state_h = 0, state_w = 0;
3384 int state = ((wxGenericTreeItem*) item.m_pItem)->GetState();
3385 if ( state != wxTREE_ITEMSTATE_NONE && m_imageListState )
3e4f8ee2 3386 {
136b7e61
VZ
3387 m_imageListState->GetSize( state, state_w, state_h );
3388 if ( image_w != 0 )
3389 state_w += MARGIN_BETWEEN_STATE_AND_IMAGE;
3390 else
3391 state_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
3e4f8ee2 3392 }
136b7e61
VZ
3393
3394 rect.x = i->GetX() + state_w + image_w;
3395 rect.width = i->GetWidth() - state_w - image_w;
3396
3e4f8ee2
VZ
3397 }
3398 else // the entire line
3399 {
3400 rect.x = 0;
3401 rect.width = GetClientSize().x;
3402 }
3403
402dfce7 3404 rect.y = i->GetY();
c22886bd 3405 rect.height = GetLineHeight(i);
8fb3bfe2 3406
402dfce7 3407 // we have to return the logical coordinates, not physical ones
619297ab 3408 rect.SetTopLeft(CalcScrolledPosition(rect.GetTopLeft()));
402dfce7 3409
ca65c044 3410 return true;
8fb3bfe2
JS
3411}
3412
8cee4a30
VZ
3413wxTextCtrl *wxGenericTreeCtrl::EditLabel(const wxTreeItemId& item,
3414 wxClassInfo * WXUNUSED(textCtrlClass))
e179bd65 3415{
9a83f860 3416 wxCHECK_MSG( item.IsOk(), NULL, wxT("can't edit an invalid item") );
e179bd65 3417
edb8f298 3418 wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem;
9dfbf520 3419
09f277d6 3420 wxTreeEvent te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, this, itemEdit);
edb8f298
VZ
3421 if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() )
3422 {
3423 // vetoed by user
8cee4a30 3424 return NULL;
edb8f298 3425 }
8dc99046 3426
dc6c62a9
RR
3427 // We have to call this here because the label in
3428 // question might just have been added and no screen
3429 // update taken place.
edb8f298 3430 if ( m_dirty )
f89d65ea
SC
3431#if defined( __WXMSW__ ) || defined(__WXMAC__)
3432 Update();
3433#else
27f8357f 3434 DoDirtyProcessing();
f89d65ea 3435#endif
9dfbf520 3436
8cee4a30 3437 // TODO: use textCtrlClass here to create the control of correct class
fbb12260 3438 m_textCtrl = new wxTreeTextCtrl(this, itemEdit);
8dc99046 3439
fbb12260 3440 m_textCtrl->SetFocus();
8cee4a30
VZ
3441
3442 return m_textCtrl;
fbb12260
JS
3443}
3444
3445// returns a pointer to the text edit control if the item is being
3446// edited, NULL otherwise (it's assumed that no more than one item may
3447// be edited simultaneously)
3448wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const
3449{
3450 return m_textCtrl;
e179bd65
RR
3451}
3452
8cee4a30
VZ
3453void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item),
3454 bool discardChanges)
3455{
9a83f860 3456 wxCHECK_RET( m_textCtrl, wxT("not editing label") );
8cee4a30
VZ
3457
3458 m_textCtrl->EndEdit(discardChanges);
3459}
3460
edb8f298
VZ
3461bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
3462 const wxString& value)
e179bd65 3463{
09f277d6 3464 wxTreeEvent le(wxEVT_COMMAND_TREE_END_LABEL_EDIT, this, item);
edb8f298 3465 le.m_label = value;
ca65c044 3466 le.m_editCancelled = false;
9dfbf520 3467
edb8f298
VZ
3468 return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
3469}
9dfbf520 3470
dd23c25c
JS
3471void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item)
3472{
3473 // let owner know that the edit was cancelled
09f277d6 3474 wxTreeEvent le(wxEVT_COMMAND_TREE_END_LABEL_EDIT, this, item);
dd23c25c 3475 le.m_label = wxEmptyString;
ca65c044 3476 le.m_editCancelled = true;
dd23c25c
JS
3477
3478 GetEventHandler()->ProcessEvent( le );
3479}
3480
edb8f298
VZ
3481void wxGenericTreeCtrl::OnRenameTimer()
3482{
642446e3 3483 EditLabel( m_current );
e179bd65 3484}
9dfbf520 3485
941830cb 3486void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 3487{
09f277d6 3488 if ( !m_anchor )return;
978f38c2 3489
f8b043e7 3490 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
ca65c044 3491
f8b043e7
RR
3492 // Is the mouse over a tree item button?
3493 int flags = 0;
73bb6776
JS
3494 wxGenericTreeItem *thisItem = m_anchor->HitTest(pt, this, flags, 0);
3495 wxGenericTreeItem *underMouse = thisItem;
0efd5732 3496#if wxUSE_TOOLTIPS
73bb6776 3497 bool underMouseChanged = (underMouse != m_underMouse) ;
0efd5732 3498#endif // wxUSE_TOOLTIPS
73bb6776 3499
f8b043e7
RR
3500 if ((underMouse) &&
3501 (flags & wxTREE_HITTEST_ONITEMBUTTON) &&
3502 (!event.LeftIsDown()) &&
ca65c044 3503 (!m_isDragging) &&
f8b043e7
RR
3504 (!m_renameTimer || !m_renameTimer->IsRunning()))
3505 {
3506 }
3507 else
3508 {
3509 underMouse = NULL;
3510 }
ca65c044 3511
f8b043e7
RR
3512 if (underMouse != m_underMouse)
3513 {
3514 if (m_underMouse)
3515 {
3516 // unhighlight old item
3517 wxGenericTreeItem *tmp = m_underMouse;
3518 m_underMouse = NULL;
3519 RefreshLine( tmp );
3520 }
ca65c044 3521
f8b043e7
RR
3522 m_underMouse = underMouse;
3523 if (m_underMouse)
3524 RefreshLine( m_underMouse );
3525 }
3526
5b5ea466 3527#if wxUSE_TOOLTIPS
4a5d781c 3528 // Determines what item we are hovering over and need a tooltip for
73bb6776 3529 wxTreeItemId hoverItem = thisItem;
4a5d781c 3530
8239070b
VZ
3531 // We do not want a tooltip if we are dragging, or if the rename timer is
3532 // running
3533 if ( underMouseChanged &&
3534 hoverItem.IsOk() &&
3535 !m_isDragging &&
3536 (!m_renameTimer || !m_renameTimer->IsRunning()) )
4a5d781c
JS
3537 {
3538 // Ask the tree control what tooltip (if any) should be shown
8239070b
VZ
3539 wxTreeEvent
3540 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, this, hoverItem);
4a5d781c
JS
3541
3542 if ( GetEventHandler()->ProcessEvent(hevent) && hevent.IsAllowed() )
3543 {
3544 SetToolTip(hevent.m_label);
3545 }
3546 }
5b5ea466 3547#endif
ca65c044 3548
da87911d 3549 // we process left mouse up event (enables in-place edit), middle/right down
3dbeaa52
VZ
3550 // (pass to the user code), left dbl click (activate item) and
3551 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
3552 if ( !(event.LeftDown() ||
3553 event.LeftUp() ||
da87911d 3554 event.MiddleDown() ||
3dbeaa52
VZ
3555 event.RightDown() ||
3556 event.LeftDClick() ||
3557 event.Dragging() ||
3558 ((event.Moving() || event.RightUp()) && m_isDragging)) )
3559 {
3560 event.Skip();
3561
3562 return;
3563 }
3564
29d87bba 3565
f8b043e7 3566 flags = 0;
d027179b 3567 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
3dbeaa52 3568
3dbeaa52 3569 if ( event.Dragging() && !m_isDragging )
bbe0af5b 3570 {
fd9811b1 3571 if (m_dragCount == 0)
d027179b 3572 m_dragStart = pt;
8dc99046 3573
fd9811b1 3574 m_dragCount++;
8dc99046 3575
3dbeaa52
VZ
3576 if (m_dragCount != 3)
3577 {
3578 // wait until user drags a bit further...
3579 return;
3580 }
8dc99046 3581
3dbeaa52
VZ
3582 wxEventType command = event.RightIsDown()
3583 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3584 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 3585
09f277d6 3586 wxTreeEvent nevent(command, this, m_current);
99411465 3587 nevent.SetPoint(CalcScrolledPosition(pt));
3dbeaa52
VZ
3588
3589 // by default the dragging is not supported, the user code must
3590 // explicitly allow the event for it to take place
3591 nevent.Veto();
3592
3593 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
3594 {
3595 // we're going to drag this item
ca65c044 3596 m_isDragging = true;
3dbeaa52 3597
b3a7510d
VZ
3598 // remember the old cursor because we will change it while
3599 // dragging
3600 m_oldCursor = m_cursor;
a6fb8636 3601
b3a7510d
VZ
3602 // in a single selection control, hide the selection temporarily
3603 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
3604 {
941830cb 3605 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
3606
3607 if ( m_oldSelection )
3608 {
ca65c044 3609 m_oldSelection->SetHilight(false);
b3a7510d
VZ
3610 RefreshLine(m_oldSelection);
3611 }
3612 }
3613
3dbeaa52
VZ
3614 CaptureMouse();
3615 }
bbe0af5b 3616 }
daa7ae0c 3617 else if ( event.Dragging() )
fd9811b1 3618 {
3dbeaa52
VZ
3619 if ( item != m_dropTarget )
3620 {
3621 // unhighlight the previous drop target
3622 DrawDropEffect(m_dropTarget);
fd9811b1 3623
3dbeaa52 3624 m_dropTarget = item;
978f38c2 3625
3dbeaa52
VZ
3626 // highlight the current drop target if any
3627 DrawDropEffect(m_dropTarget);
fb882e1c 3628
daa7ae0c 3629#if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
f89d65ea
SC
3630 Update();
3631#else
977a41ec
FM
3632 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3633 // instead (needs to be tested!)
cd66f45b 3634 wxYieldIfNeeded();
f89d65ea 3635#endif
3dbeaa52 3636 }
e179bd65 3637 }
3dbeaa52
VZ
3638 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
3639 {
f0bc6afb
RR
3640 ReleaseMouse();
3641
3dbeaa52
VZ
3642 // erase the highlighting
3643 DrawDropEffect(m_dropTarget);
9dfbf520 3644
4f5fffcc
RR
3645 if ( m_oldSelection )
3646 {
ca65c044 3647 m_oldSelection->SetHilight(true);
4f5fffcc 3648 RefreshLine(m_oldSelection);
d3b9f782 3649 m_oldSelection = NULL;
4f5fffcc
RR
3650 }
3651
3dbeaa52 3652 // generate the drag end event
09f277d6 3653 wxTreeEvent eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG, this, item);
88ac883a 3654
99411465 3655 eventEndDrag.m_pointDrag = CalcScrolledPosition(pt);
91b8de8d 3656
4e115ed2 3657 (void)GetEventHandler()->ProcessEvent(eventEndDrag);
29d87bba 3658
ca65c044 3659 m_isDragging = false;
d3b9f782 3660 m_dropTarget = NULL;
3dbeaa52 3661
b3a7510d 3662 SetCursor(m_oldCursor);
3dbeaa52 3663
977a41ec 3664#if defined( __WXMSW__ ) || defined(__WXMAC__) || defined(__WXGTK20__)
f89d65ea
SC
3665 Update();
3666#else
977a41ec
FM
3667 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3668 // instead (needs to be tested!)
cd66f45b 3669 wxYieldIfNeeded();
f89d65ea 3670#endif
3dbeaa52
VZ
3671 }
3672 else
bbe0af5b 3673 {
e4899fd7 3674 // If we got to this point, we are not dragging or moving the mouse.
8239070b
VZ
3675 // Because the code in carbon/toplevel.cpp will only set focus to the
3676 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3677 // focus to work.
e4899fd7 3678 // We skip even if we didn't hit an item because we still should
8239070b
VZ
3679 // restore focus to the tree control even if we didn't exactly hit an
3680 // item.
e4899fd7
KH
3681 if ( event.LeftDown() )
3682 {
3683 event.Skip();
3684 }
3685
3dbeaa52
VZ
3686 // here we process only the messages which happen on tree items
3687
3688 m_dragCount = 0;
3689
3690 if (item == NULL) return; /* we hit the blank area */
3691
3692 if ( event.RightDown() )
3693 {
e8cf9a5f 3694 // If the item is already selected, do not update the selection.
8239070b
VZ
3695 // Multi-selections should not be cleared if a selected item is
3696 // clicked.
e8cf9a5f
KH
3697 if (!IsSelected(item))
3698 {
3699 DoSelectItem(item, true, false);
3700 }
3701
8239070b
VZ
3702 wxTreeEvent
3703 nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, this, item);
d027179b 3704 nevent.m_pointDrag = CalcScrolledPosition(pt);
ac103441 3705 event.Skip(!GetEventHandler()->ProcessEvent(nevent));
39faaf39
KH
3706
3707 // Consistent with MSW (for now), send the ITEM_MENU *after*
3708 // the RIGHT_CLICK event. TODO: This behavior may change.
09f277d6 3709 wxTreeEvent nevent2(wxEVT_COMMAND_TREE_ITEM_MENU, this, item);
39faaf39 3710 nevent2.m_pointDrag = CalcScrolledPosition(pt);
39faaf39 3711 GetEventHandler()->ProcessEvent(nevent2);
3dbeaa52 3712 }
da87911d
VZ
3713 else if ( event.MiddleDown() )
3714 {
8239070b
VZ
3715 wxTreeEvent
3716 nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK, this, item);
da87911d
VZ
3717 nevent.m_pointDrag = CalcScrolledPosition(pt);
3718 event.Skip(!GetEventHandler()->ProcessEvent(nevent));
3719 }
80d2803f 3720 else if ( event.LeftUp() )
f6bcfd97 3721 {
03966fcb
RR
3722 if (flags & wxTREE_HITTEST_ONITEMSTATEICON)
3723 {
8239070b
VZ
3724 wxTreeEvent
3725 nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK, this, item);
03966fcb
RR
3726 GetEventHandler()->ProcessEvent(nevent);
3727 }
3728
35cf1ec6
VZ
3729 // this facilitates multiple-item drag-and-drop
3730
902725ee 3731 if ( /* item && */ HasFlag(wxTR_MULTIPLE))
35cf1ec6
VZ
3732 {
3733 wxArrayTreeItemIds selections;
3734 size_t count = GetSelections(selections);
3735
3736 if (count > 1 &&
105fc244 3737 !event.CmdDown() &&
35cf1ec6
VZ
3738 !event.ShiftDown())
3739 {
78f12104 3740 DoSelectItem(item, true, false);
35cf1ec6
VZ
3741 }
3742 }
3743
80d2803f 3744 if ( m_lastOnSame )
f6bcfd97 3745 {
80d2803f
VZ
3746 if ( (item == m_current) &&
3747 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
3748 HasFlag(wxTR_EDIT_LABELS) )
3749 {
cb59313c
VZ
3750 if ( m_renameTimer )
3751 {
3752 if ( m_renameTimer->IsRunning() )
3753 m_renameTimer->Stop();
3754 }
3755 else
3756 {
3757 m_renameTimer = new wxTreeRenameTimer( this );
3758 }
bdb310a7 3759
ca65c044 3760 m_renameTimer->Start( wxTreeRenameTimer::DELAY, true );
80d2803f 3761 }
f6bcfd97 3762
ca65c044 3763 m_lastOnSame = false;
80d2803f 3764 }
3dbeaa52 3765 }
8239070b 3766 else // !RightDown() && !MiddleDown() && !LeftUp()
3dbeaa52 3767 {
8239070b 3768 // ==> LeftDown() || LeftDClick()
f6bcfd97
BP
3769 if ( event.LeftDown() )
3770 {
3771 m_lastOnSame = item == m_current;
3772 }
3773
0326c494
VZ
3774 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
3775 {
3776 // only toggle the item for a single click, double click on
3777 // the button doesn't do anything (it toggles the item twice)
3778 if ( event.LeftDown() )
3779 {
3780 Toggle( item );
3781 }
3782
3783 // don't select the item if the button was clicked
3784 return;
3785 }
3786
3dbeaa52 3787
35cf1ec6
VZ
3788 // clear the previously selected items, if the
3789 // user clicked outside of the present selection.
3790 // otherwise, perform the deselection on mouse-up.
3791 // this allows multiple drag and drop to work.
105fc244
JS
3792 // but if Cmd is down, toggle selection of the clicked item
3793 if (!IsSelected(item) || event.CmdDown())
35cf1ec6
VZ
3794 {
3795 // how should the selection work for this event?
3796 bool is_multiple, extended_select, unselect_others;
3797 EventFlagsToSelType(GetWindowStyleFlag(),
3798 event.ShiftDown(),
105fc244 3799 event.CmdDown(),
8239070b
VZ
3800 is_multiple,
3801 extended_select,
3802 unselect_others);
35cf1ec6 3803
78f12104 3804 DoSelectItem(item, unselect_others, extended_select);
35cf1ec6
VZ
3805 }
3806
3dbeaa52 3807
618a5e38
RR
3808 // For some reason, Windows isn't recognizing a left double-click,
3809 // so we need to simulate it here. Allow 200 milliseconds for now.
3dbeaa52
VZ
3810 if ( event.LeftDClick() )
3811 {
f6bcfd97 3812 // double clicking should not start editing the item label
cb59313c
VZ
3813 if ( m_renameTimer )
3814 m_renameTimer->Stop();
3815
ca65c044 3816 m_lastOnSame = false;
f6bcfd97 3817
ebb987b7 3818 // send activate event first
8239070b
VZ
3819 wxTreeEvent
3820 nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, this, item);
d027179b 3821 nevent.m_pointDrag = CalcScrolledPosition(pt);
ebb987b7 3822 if ( !GetEventHandler()->ProcessEvent( nevent ) )
618a5e38 3823 {
ebb987b7
VZ
3824 // if the user code didn't process the activate event,
3825 // handle it ourselves by toggling the item when it is
3826 // double clicked
3827 if ( item->HasPlus() )
3828 {
3829 Toggle(item);
3830 }
618a5e38 3831 }
3dbeaa52
VZ
3832 }
3833 }
bbe0af5b 3834 }
edaa81ae 3835}
c801d85f 3836
5180055b 3837void wxGenericTreeCtrl::OnInternalIdle()
3db7be80 3838{
5180055b 3839 wxWindow::OnInternalIdle();
ca65c044 3840
3e3a7b97
JS
3841 // Check if we need to select the root item
3842 // because nothing else has been selected.
3843 // Delaying it means that we can invoke event handlers
3844 // as required, when a first item is selected.
3845 if (!HasFlag(wxTR_MULTIPLE) && !GetSelection().IsOk())
3846 {
3847 if (m_select_me)
3848 SelectItem(m_select_me);
3849 else if (GetRootItem().IsOk())
3850 SelectItem(GetRootItem());
3851 }
3852
27f8357f
VZ
3853 // after all changes have been done to the tree control,
3854 // actually redraw the tree when everything is over
3855 if (m_dirty)
3856 DoDirtyProcessing();
3db7be80
RR
3857}
3858
8239070b
VZ
3859void
3860wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem *item,
3861 wxDC &dc,
3862 int level,
3863 int &y )
c801d85f 3864{
618a5e38
RR
3865 int x = level*m_indent;
3866 if (!HasFlag(wxTR_HIDE_ROOT))
3867 {
3868 x += m_indent;
3869 }
3870 else if (level == 0)
3871 {
3872 // a hidden root is not evaluated, but its
3873 // children are always calculated
3874 goto Recurse;
3875 }
389cdc7a 3876
cac09ce8 3877 item->CalculateSize(this, dc);
d3a9f4af 3878
91b8de8d 3879 // set its position
618a5e38 3880 item->SetX( x+m_spacing );
91b8de8d 3881 item->SetY( y );
618a5e38 3882 y += GetLineHeight(item);
4c681997 3883
bbe0af5b
RR
3884 if ( !item->IsExpanded() )
3885 {
618a5e38 3886 // we don't need to calculate collapsed branches
bbe0af5b
RR
3887 return;
3888 }
389cdc7a 3889
618a5e38 3890 Recurse:
91b8de8d 3891 wxArrayGenericTreeItems& children = item->GetChildren();
b4a980f4 3892 size_t n, count = children.GetCount();
618a5e38 3893 ++level;
91b8de8d 3894 for (n = 0; n < count; ++n )
618a5e38 3895 CalculateLevel( children[n], dc, level, y ); // recurse
edaa81ae 3896}
c801d85f 3897
941830cb 3898void wxGenericTreeCtrl::CalculatePositions()
c801d85f 3899{
bbe0af5b 3900 if ( !m_anchor ) return;
29d87bba 3901
bbe0af5b
RR
3902 wxClientDC dc(this);
3903 PrepareDC( dc );
29d87bba 3904
eff869aa 3905 dc.SetFont( m_normalFont );
29d87bba 3906
bbe0af5b 3907 dc.SetPen( m_dottedPen );
91b8de8d
RR
3908 //if(GetImageList() == NULL)
3909 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 3910
8dc99046 3911 int y = 2;
f65635b5 3912 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 3913}
c801d85f 3914
3e6e5147
VZ
3915void wxGenericTreeCtrl::Refresh(bool eraseBackground, const wxRect *rect)
3916{
17808a75 3917 if ( !IsFrozen() )
3e6e5147
VZ
3918 wxTreeCtrlBase::Refresh(eraseBackground, rect);
3919}
3920
941830cb 3921void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 3922{
17808a75 3923 if (m_dirty || IsFrozen() )
3e6e5147 3924 return;
e6527f9d 3925
d027179b 3926 wxSize client = GetClientSize();
4832f7c0 3927
bbe0af5b 3928 wxRect rect;
5941c5de 3929 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b
VS
3930 rect.width = client.x;
3931 rect.height = client.y;
f135ff73 3932
ca65c044 3933 Refresh(true, &rect);
f135ff73 3934
bbe0af5b 3935 AdjustMyScrollbars();
edaa81ae 3936}
c801d85f 3937
941830cb 3938void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 3939{
17808a75 3940 if (m_dirty || IsFrozen() )
3e6e5147 3941 return;
e6527f9d 3942
bbe0af5b 3943 wxRect rect;
5941c5de 3944 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b 3945 rect.width = GetClientSize().x;
91b8de8d 3946 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 3947
ca65c044 3948 Refresh(true, &rect);
edaa81ae 3949}
c801d85f 3950
b771aa29
VZ
3951void wxGenericTreeCtrl::RefreshSelected()
3952{
17808a75 3953 if (IsFrozen())
3e6e5147 3954 return;
ca65c044 3955
b771aa29
VZ
3956 // TODO: this is awfully inefficient, we should keep the list of all
3957 // selected items internally, should be much faster
3958 if ( m_anchor )
3959 RefreshSelectedUnder(m_anchor);
3960}
3961
3962void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3963{
17808a75 3964 if (IsFrozen())
3e6e5147 3965 return;
ca65c044 3966
b771aa29
VZ
3967 if ( item->IsSelected() )
3968 RefreshLine(item);
3969
3970 const wxArrayGenericTreeItems& children = item->GetChildren();
3971 size_t count = children.GetCount();
3972 for ( size_t n = 0; n < count; n++ )
3973 {
3974 RefreshSelectedUnder(children[n]);
3975 }
3976}
3977
17808a75 3978void wxGenericTreeCtrl::DoThaw()
e1983ab5 3979{
ad1c5f45
VS
3980 wxTreeCtrlBase::DoThaw();
3981
17808a75
VZ
3982 if ( m_dirty )
3983 DoDirtyProcessing();
3984 else
3985 Refresh();
e1983ab5
VZ
3986}
3987
7009f411
VZ
3988// ----------------------------------------------------------------------------
3989// changing colours: we need to refresh the tree control
3990// ----------------------------------------------------------------------------
3991
3992bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3993{
3994 if ( !wxWindow::SetBackgroundColour(colour) )
ca65c044
WS
3995 return false;
3996
7009f411
VZ
3997 Refresh();
3998
ca65c044 3999 return true;
7009f411
VZ
4000}
4001
0800a4ba 4002bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
7009f411
VZ
4003{
4004 if ( !wxWindow::SetForegroundColour(colour) )
ca65c044
WS
4005 return false;
4006
7009f411
VZ
4007 Refresh();
4008
ca65c044 4009 return true;
7009f411
VZ
4010}
4011
73bb6776
JS
4012// Process the tooltip event, to speed up event processing.
4013// Doesn't actually get a tooltip.
4014void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent &event )
4015{
4016 event.Veto();
4017}
4018
35d4c967
RD
4019
4020// NOTE: If using the wxListBox visual attributes works everywhere then this can
4021// be removed, as well as the #else case below.
4022#define _USE_VISATTR 0
4023
35d4c967
RD
4024//static
4025wxVisualAttributes
3f927d50 4026#if _USE_VISATTR
35d4c967 4027wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
3f927d50
DS
4028#else
4029wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
4030#endif
35d4c967
RD
4031{
4032#if _USE_VISATTR
4033 // Use the same color scheme as wxListBox
4034 return wxListBox::GetClassDefaultAttributes(variant);
4035#else
4036 wxVisualAttributes attr;
9f2968ad 4037 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
35d4c967
RD
4038 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
4039 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
4040 return attr;
4041#endif
4042}
4043
27f8357f
VZ
4044void wxGenericTreeCtrl::DoDirtyProcessing()
4045{
17808a75 4046 if (IsFrozen())
27f8357f
VZ
4047 return;
4048
4049 m_dirty = false;
4050
4051 CalculatePositions();
4052 Refresh();
4053 AdjustMyScrollbars();
4054}
4055
54a4121a
VZ
4056wxSize wxGenericTreeCtrl::DoGetBestSize() const
4057{
1f640c45
VZ
4058 // make sure all positions are calculated as normally this only done during
4059 // idle time but we need them for base class DoGetBestSize() to return the
4060 // correct result
4061 wxConstCast(this, wxGenericTreeCtrl)->CalculatePositions();
4062
54a4121a
VZ
4063 wxSize size = wxTreeCtrlBase::DoGetBestSize();
4064
b9643cd6
VZ
4065 // there seems to be an implicit extra border around the items, although
4066 // I'm not really sure where does it come from -- but without this, the
4067 // scrollbars appear in a tree with default/best size
54a4121a
VZ
4068 size.IncBy(4, 4);
4069
1f640c45
VZ
4070 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
4071 // scrollbars still appear
4072 const wxSize& borderSize = GetWindowBorderSize();
4073
4074 int dx = (size.x - borderSize.x) % PIXELS_PER_UNIT;
4075 if ( dx )
4076 size.x += PIXELS_PER_UNIT - dx;
4077 int dy = (size.y - borderSize.y) % PIXELS_PER_UNIT;
4078 if ( dy )
4079 size.y += PIXELS_PER_UNIT - dy;
4080
4081 // we need to update the cache too as the base class cached its own value
4082 CacheBestSize(size);
b9643cd6 4083
54a4121a
VZ
4084 return size;
4085}
4086
1e6feb95 4087#endif // wxUSE_TREECTRL