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