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