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