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