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