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