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