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