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