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