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