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