]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
added method for finding out, whether the macos system is fully initialized for this...
[wxWidgets.git] / src / generic / treectlg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
941830cb 2// Name: 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$
c801d85f 8// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
29d87bba 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
f135ff73
VZ
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
941830cb 21 #pragma implementation "treectlg.h"
c801d85f
KB
22#endif
23
1e6d9499
JS
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
1e6feb95 28 #pragma hdrstop
1e6d9499
JS
29#endif
30
1e6feb95
VZ
31#if wxUSE_TREECTRL
32
618a5e38 33#include "wx/treebase.h"
941830cb 34#include "wx/generic/treectlg.h"
618a5e38
RR
35#include "wx/timer.h"
36#include "wx/textctrl.h"
941830cb 37#include "wx/imaglist.h"
c801d85f 38#include "wx/settings.h"
f135ff73 39#include "wx/dcclient.h"
c801d85f 40
f135ff73
VZ
41// -----------------------------------------------------------------------------
42// array types
43// -----------------------------------------------------------------------------
c801d85f 44
1e6d9499
JS
45class WXDLLEXPORT wxGenericTreeItem;
46
8a985b84 47WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
941830cb 48//WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
c801d85f 49
8dc99046
VZ
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
54static const int NO_IMAGE = -1;
55
3dbeaa52
VZ
56#define PIXELS_PER_UNIT 10
57
d21d2e5a
RR
58// ----------------------------------------------------------------------------
59// Aqua arrows
60// ----------------------------------------------------------------------------
61
62 /* XPM */
63 static char *aqua_arrow_right[] = {
64 /* columns rows colors chars-per-pixel */
65 "13 11 4 1",
66 " c None",
67 "b c #C0C0C0",
68 "c c #707070",
69 "d c #A0A0A0",
70 /* pixels */
71 " b ",
72 " ddb ",
73 " cccdb ",
74 " cccccd ",
75 " ccccccdb ",
76 " ccccccccd",
77 " ccccccdb ",
78 " cccccb ",
79 " cccdb ",
80 " ddb ",
81 " b "
82 };
83
84 /* XPM */
85 static char *aqua_arrow_down[] = {
86 /* columns rows colors chars-per-pixel */
87 "13 11 4 1",
88 " c None",
89 "b c #C0C0C0",
90 "c c #707070",
91 "d c #A0A0A0",
92 /* pixels */
93 " ",
94 " ",
95 " bdcccccccdb ",
96 " dcccccccd ",
97 " bcccccccb ",
98 " dcccccd ",
99 " bcccccb ",
100 " bcccd ",
101 " dcd ",
102 " bcb ",
103 " d "
104 };
105
f135ff73
VZ
106// -----------------------------------------------------------------------------
107// private classes
108// -----------------------------------------------------------------------------
109
3dbeaa52
VZ
110// timer used for enabling in-place edit
111class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
112{
113public:
941830cb 114 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
3dbeaa52
VZ
115
116 void Notify();
117
118private:
941830cb 119 wxGenericTreeCtrl *m_owner;
3dbeaa52
VZ
120};
121
122// control used for in-place edit
123class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
124{
125public:
3dbeaa52
VZ
126 wxTreeTextCtrl( wxWindow *parent,
127 const wxWindowID id,
128 bool *accept,
129 wxString *res,
941830cb 130 wxGenericTreeCtrl *owner,
3dbeaa52
VZ
131 const wxString &value = wxEmptyString,
132 const wxPoint &pos = wxDefaultPosition,
133 const wxSize &size = wxDefaultSize,
27316302 134 int style = wxSIMPLE_BORDER,
3dbeaa52
VZ
135 const wxValidator& validator = wxDefaultValidator,
136 const wxString &name = wxTextCtrlNameStr );
137
138 void OnChar( wxKeyEvent &event );
c13cace1 139 void OnKeyUp( wxKeyEvent &event );
3dbeaa52
VZ
140 void OnKillFocus( wxFocusEvent &event );
141
142private:
143 bool *m_accept;
144 wxString *m_res;
618a5e38 145 wxGenericTreeCtrl *m_owner;
3dbeaa52 146 wxString m_startValue;
dd5a32cc 147 bool m_finished;
3dbeaa52
VZ
148
149 DECLARE_EVENT_TABLE()
3dbeaa52
VZ
150};
151
f135ff73
VZ
152// a tree item
153class WXDLLEXPORT wxGenericTreeItem
c801d85f 154{
f135ff73 155public:
9ec64fa7
VZ
156 // ctors & dtor
157 wxGenericTreeItem() { m_data = NULL; }
158 wxGenericTreeItem( wxGenericTreeItem *parent,
618a5e38 159 const wxString& text,
618a5e38
RR
160 int image,
161 int selImage,
162 wxTreeItemData *data );
f135ff73 163
9ec64fa7 164 ~wxGenericTreeItem();
f135ff73 165
9ec64fa7
VZ
166 // trivial accessors
167 wxArrayGenericTreeItems& GetChildren() { return m_children; }
f135ff73 168
9ec64fa7
VZ
169 const wxString& GetText() const { return m_text; }
170 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
3dbeaa52 171 { return m_images[which]; }
9ec64fa7 172 wxTreeItemData *GetData() const { return m_data; }
f135ff73 173
9ec64fa7
VZ
174 // returns the current image for the item (depending on its
175 // selected/expanded/whatever state)
176 int GetCurrentImage() const;
8dc99046 177
9ec64fa7
VZ
178 void SetText( const wxString &text );
179 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
180 void SetData(wxTreeItemData *data) { m_data = data; }
f135ff73 181
9ec64fa7 182 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
f135ff73 183
9ec64fa7 184 void SetBold(bool bold) { m_isBold = bold; }
ef44a621 185
9ec64fa7
VZ
186 int GetX() const { return m_x; }
187 int GetY() const { return m_y; }
f135ff73 188
9ec64fa7
VZ
189 void SetX(int x) { m_x = x; }
190 void SetY(int y) { m_y = y; }
f135ff73 191
9ec64fa7
VZ
192 int GetHeight() const { return m_height; }
193 int GetWidth() const { return m_width; }
91b8de8d 194
9ec64fa7
VZ
195 void SetHeight(int h) { m_height = h; }
196 void SetWidth(int w) { m_width = w; }
91b8de8d 197
9ec64fa7 198 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 199
9ec64fa7
VZ
200 // operations
201 // deletes all children notifying the treectrl about it if !NULL
202 // pointer given
941830cb 203 void DeleteChildren(wxGenericTreeCtrl *tree = NULL);
f135ff73 204
9ec64fa7
VZ
205 // get count of all children (and grand children if 'recursively')
206 size_t GetChildrenCount(bool recursively = TRUE) const;
f135ff73 207
9ec64fa7 208 void Insert(wxGenericTreeItem *child, size_t index)
f135ff73
VZ
209 { m_children.Insert(child, index); }
210
941830cb 211 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
f135ff73 212
9ec64fa7
VZ
213 // return the item at given position (or NULL if no item), onButton is
214 // TRUE if the point belongs to the item's button, otherwise it lies
215 // on the button's label
618a5e38
RR
216 wxGenericTreeItem *HitTest( const wxPoint& point,
217 const wxGenericTreeCtrl *,
218 int &flags,
219 int level );
f135ff73 220
9ec64fa7
VZ
221 void Expand() { m_isCollapsed = FALSE; }
222 void Collapse() { m_isCollapsed = TRUE; }
f135ff73 223
9ec64fa7 224 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
f135ff73 225
9ec64fa7
VZ
226 // status inquiries
227 bool HasChildren() const { return !m_children.IsEmpty(); }
ef8698d6 228 bool IsSelected() const { return m_hasHilight != 0; }
9ec64fa7
VZ
229 bool IsExpanded() const { return !m_isCollapsed; }
230 bool HasPlus() const { return m_hasPlus || HasChildren(); }
ef8698d6 231 bool IsBold() const { return m_isBold != 0; }
9ec64fa7
VZ
232
233 // attributes
234 // get them - may be NULL
235 wxTreeItemAttr *GetAttributes() const { return m_attr; }
236 // get them ensuring that the pointer is not NULL
237 wxTreeItemAttr& Attr()
238 {
239 if ( !m_attr )
618a5e38 240 {
9ec64fa7 241 m_attr = new wxTreeItemAttr;
618a5e38
RR
242 m_ownsAttr = TRUE;
243 }
9ec64fa7
VZ
244 return *m_attr;
245 }
618a5e38
RR
246 // set them
247 void SetAttributes(wxTreeItemAttr *attr)
248 {
249 if ( m_ownsAttr ) delete m_attr;
250 m_attr = attr;
251 m_ownsAttr = FALSE;
252 }
253 // set them and delete when done
254 void AssignAttributes(wxTreeItemAttr *attr)
255 {
256 SetAttributes(attr);
257 m_ownsAttr = TRUE;
258 }
f135ff73
VZ
259
260private:
618a5e38
RR
261 // since there can be very many of these, we save size by chosing
262 // the smallest representation for the elements and by ordering
263 // the members to avoid padding.
264 wxString m_text; // label to be rendered for item
265
266 wxTreeItemData *m_data; // user-provided data
267
268 wxArrayGenericTreeItems m_children; // list of children
269 wxGenericTreeItem *m_parent; // parent of this item
270
271 wxTreeItemAttr *m_attr; // attributes???
9ec64fa7
VZ
272
273 // tree ctrl images for the normal, selected, expanded and
274 // expanded+selected states
618a5e38 275 short m_images[wxTreeItemIcon_Max];
9ec64fa7 276
618a5e38
RR
277 wxCoord m_x; // (virtual) offset from top
278 short m_y; // (virtual) offset from left
279 short m_width; // width of this item
280 unsigned char m_height; // height of this item
9ec64fa7
VZ
281
282 // use bitfields to save size
283 int m_isCollapsed :1;
284 int m_hasHilight :1; // same as focused
285 int m_hasPlus :1; // used for item which doesn't have
286 // children but has a [+] button
287 int m_isBold :1; // render the label in bold font
618a5e38 288 int m_ownsAttr :1; // delete attribute when done
f135ff73
VZ
289};
290
291// =============================================================================
292// implementation
293// =============================================================================
294
06b466c7
VZ
295// ----------------------------------------------------------------------------
296// private functions
297// ----------------------------------------------------------------------------
298
299// translate the key or mouse event flags to the type of selection we're
300// dealing with
301static void EventFlagsToSelType(long style,
302 bool shiftDown,
303 bool ctrlDown,
dfc6cd93
SB
304 bool &is_multiple,
305 bool &extended_select,
306 bool &unselect_others)
06b466c7 307{
dfc6cd93
SB
308 is_multiple = (style & wxTR_MULTIPLE) != 0;
309 extended_select = shiftDown && is_multiple;
310 unselect_others = !(extended_select || (ctrlDown && is_multiple));
06b466c7 311}
e179bd65
RR
312
313// -----------------------------------------------------------------------------
314// wxTreeRenameTimer (internal)
315// -----------------------------------------------------------------------------
316
941830cb 317wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
e179bd65
RR
318{
319 m_owner = owner;
320}
321
322void wxTreeRenameTimer::Notify()
323{
324 m_owner->OnRenameTimer();
325}
326
327//-----------------------------------------------------------------------------
328// wxTreeTextCtrl (internal)
329//-----------------------------------------------------------------------------
330
e179bd65
RR
331BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
332 EVT_CHAR (wxTreeTextCtrl::OnChar)
c13cace1 333 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
e179bd65
RR
334 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
335END_EVENT_TABLE()
336
674ac8b9
VZ
337wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent,
338 const wxWindowID id,
339 bool *accept,
340 wxString *res,
941830cb 341 wxGenericTreeCtrl *owner,
674ac8b9
VZ
342 const wxString &value,
343 const wxPoint &pos,
344 const wxSize &size,
345 int style,
346 const wxValidator& validator,
347 const wxString &name )
618a5e38 348 : wxTextCtrl( parent, id, value, pos, size, style, validator, name )
e179bd65
RR
349{
350 m_res = res;
351 m_accept = accept;
352 m_owner = owner;
5f1ea0ee 353 (*m_accept) = FALSE;
3dbeaa52 354 (*m_res) = wxEmptyString;
5f1ea0ee 355 m_startValue = value;
dd5a32cc 356 m_finished = FALSE;
e179bd65
RR
357}
358
359void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
360{
361 if (event.m_keyCode == WXK_RETURN)
362 {
363 (*m_accept) = TRUE;
364 (*m_res) = GetValue();
33ac7e6f 365
dd5a32cc 366 if ((*m_res) != m_startValue)
bce1406b 367 m_owner->OnRenameAccept();
33ac7e6f 368
618a5e38
RR
369 if (!wxPendingDelete.Member(this))
370 wxPendingDelete.Append(this);
dd5a32cc
RR
371
372 m_finished = TRUE;
373 m_owner->SetFocus(); // This doesn't work. TODO.
618a5e38 374
e179bd65
RR
375 return;
376 }
377 if (event.m_keyCode == WXK_ESCAPE)
378 {
379 (*m_accept) = FALSE;
380 (*m_res) = "";
33ac7e6f 381
bce1406b
RR
382 if (!wxPendingDelete.Member(this))
383 wxPendingDelete.Append(this);
33ac7e6f 384
dd5a32cc
RR
385 m_finished = TRUE;
386 m_owner->SetFocus(); // This doesn't work. TODO.
387
e179bd65
RR
388 return;
389 }
390 event.Skip();
c13cace1
VS
391}
392
393void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
394{
dd5a32cc
RR
395 if (m_finished)
396 {
397 event.Skip();
398 return;
399 }
400
c13cace1
VS
401 // auto-grow the textctrl:
402 wxSize parentSize = m_owner->GetSize();
403 wxPoint myPos = GetPosition();
404 wxSize mySize = GetSize();
405 int sx, sy;
dd5a32cc 406 GetTextExtent(GetValue() + _T("M"), &sx, &sy);
c13cace1
VS
407 if (myPos.x + sx > parentSize.x) sx = parentSize.x - myPos.x;
408 if (mySize.x > sx) sx = mySize.x;
409 SetSize(sx, -1);
33ac7e6f 410
c13cace1 411 event.Skip();
e179bd65
RR
412}
413
dd5a32cc 414void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
e179bd65 415{
dd5a32cc
RR
416 if (m_finished)
417 {
418 event.Skip();
419 return;
420 }
421
bce1406b
RR
422 if (!wxPendingDelete.Member(this))
423 wxPendingDelete.Append(this);
9dfbf520 424
dd5a32cc
RR
425 (*m_accept) = TRUE;
426 (*m_res) = GetValue();
427
428 if ((*m_res) != m_startValue)
5f1ea0ee 429 m_owner->OnRenameAccept();
e179bd65
RR
430}
431
f135ff73 432// -----------------------------------------------------------------------------
c801d85f 433// wxGenericTreeItem
f135ff73 434// -----------------------------------------------------------------------------
c801d85f 435
f135ff73
VZ
436wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
437 const wxString& text,
f135ff73
VZ
438 int image, int selImage,
439 wxTreeItemData *data)
440 : m_text(text)
c801d85f 441{
00e12320
RR
442 m_images[wxTreeItemIcon_Normal] = image;
443 m_images[wxTreeItemIcon_Selected] = selImage;
444 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
445 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
8dc99046 446
00e12320
RR
447 m_data = data;
448 m_x = m_y = 0;
f135ff73 449
00e12320
RR
450 m_isCollapsed = TRUE;
451 m_hasHilight = FALSE;
452 m_hasPlus = FALSE;
453 m_isBold = FALSE;
c801d85f 454
00e12320 455 m_parent = parent;
f135ff73 456
00e12320 457 m_attr = (wxTreeItemAttr *)NULL;
618a5e38 458 m_ownsAttr = FALSE;
06b466c7 459
00e12320
RR
460 // We don't know the height here yet.
461 m_width = 0;
462 m_height = 0;
edaa81ae 463}
c801d85f 464
f135ff73 465wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 466{
00e12320 467 delete m_data;
4832f7c0 468
618a5e38 469 if (m_ownsAttr) delete m_attr;
9ec64fa7 470
00e12320
RR
471 wxASSERT_MSG( m_children.IsEmpty(),
472 wxT("please call DeleteChildren() before deleting the item") );
372edb9d
VZ
473}
474
941830cb 475void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
372edb9d 476{
00e12320
RR
477 size_t count = m_children.Count();
478 for ( size_t n = 0; n < count; n++ )
a43a4f9d 479 {
00e12320
RR
480 wxGenericTreeItem *child = m_children[n];
481 if (tree)
482 tree->SendDeleteEvent(child);
a43a4f9d 483
00e12320
RR
484 child->DeleteChildren(tree);
485 delete child;
486 }
372edb9d 487
00e12320 488 m_children.Empty();
edaa81ae 489}
c801d85f 490
91b8de8d 491void wxGenericTreeItem::SetText( const wxString &text )
c801d85f 492{
00e12320 493 m_text = text;
edaa81ae 494}
c801d85f 495
4832f7c0 496size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 497{
00e12320
RR
498 size_t count = m_children.Count();
499 if ( !recursively )
500 return count;
4832f7c0 501
00e12320 502 size_t total = count;
f2593d0d 503 for (size_t n = 0; n < count; ++n)
00e12320
RR
504 {
505 total += m_children[n]->GetChildrenCount();
506 }
c801d85f 507
00e12320 508 return total;
edaa81ae 509}
c801d85f 510
618a5e38
RR
511void wxGenericTreeItem::GetSize( int &x, int &y,
512 const wxGenericTreeCtrl *theButton )
c801d85f 513{
618a5e38 514 int bottomY=m_y+theButton->GetLineHeight(this);
00e12320
RR
515 if ( y < bottomY ) y = bottomY;
516 int width = m_x + m_width;
517 if ( x < width ) x = width;
f135ff73 518
00e12320 519 if (IsExpanded())
4832f7c0 520 {
00e12320
RR
521 size_t count = m_children.Count();
522 for ( size_t n = 0; n < count; ++n )
523 {
618a5e38 524 m_children[n]->GetSize( x, y, theButton );
00e12320 525 }
df875e59 526 }
edaa81ae 527}
c801d85f 528
618a5e38
RR
529wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point,
530 const wxGenericTreeCtrl *theCtrl,
531 int &flags,
532 int level)
c801d85f 533{
618a5e38
RR
534 // for a hidden root node, don't evaluate it, but do evaluate children
535 if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) )
c801d85f 536 {
618a5e38
RR
537 // evaluate the item
538 int h = theCtrl->GetLineHeight(this);
539 if ((point.y > m_y) && (point.y < m_y + h))
f2593d0d 540 {
618a5e38
RR
541 int y_mid = m_y + h/2;
542 if (point.y < y_mid )
543 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
544 else
545 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
d3a9f4af 546
618a5e38
RR
547 // 5 is the size of the plus sign
548 int xCross = m_x - theCtrl->GetSpacing();
549 if ((point.x > xCross-5) && (point.x < xCross+5) &&
550 (point.y > y_mid-5) && (point.y < y_mid+5) &&
551 HasPlus() && theCtrl->HasButtons() )
552 {
553 flags |= wxTREE_HITTEST_ONITEMBUTTON;
554 return this;
555 }
0ae7f2a2 556
618a5e38
RR
557 if ((point.x >= m_x) && (point.x <= m_x+m_width))
558 {
559 int image_w = -1;
560 int image_h;
91b8de8d 561
618a5e38
RR
562 // assuming every image (normal and selected) has the same size!
563 if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal )
564 theCtrl->m_imageListNormal->GetSize(GetImage(),
565 image_w, image_h);
566
567 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
568 flags |= wxTREE_HITTEST_ONITEMICON;
569 else
570 flags |= wxTREE_HITTEST_ONITEMLABEL;
571
572 return this;
573 }
574
575 if (point.x < m_x)
576 flags |= wxTREE_HITTEST_ONITEMINDENT;
577 if (point.x > m_x+m_width)
578 flags |= wxTREE_HITTEST_ONITEMRIGHT;
91b8de8d 579
f2593d0d
RR
580 return this;
581 }
91b8de8d 582
618a5e38
RR
583 // if children are expanded, fall through to evaluate them
584 if (m_isCollapsed) return (wxGenericTreeItem*) NULL;
f2593d0d 585 }
618a5e38
RR
586
587 // evaluate children
588 size_t count = m_children.Count();
589 for ( size_t n = 0; n < count; n++ )
c801d85f 590 {
618a5e38
RR
591 wxGenericTreeItem *res = m_children[n]->HitTest( point,
592 theCtrl,
593 flags,
594 level + 1 );
595 if ( res != NULL )
596 return res;
edaa81ae 597 }
f135ff73 598
f2593d0d 599 return (wxGenericTreeItem*) NULL;
edaa81ae 600}
c801d85f 601
8dc99046
VZ
602int wxGenericTreeItem::GetCurrentImage() const
603{
604 int image = NO_IMAGE;
605 if ( IsExpanded() )
606 {
607 if ( IsSelected() )
608 {
609 image = GetImage(wxTreeItemIcon_SelectedExpanded);
610 }
611
612 if ( image == NO_IMAGE )
613 {
614 // we usually fall back to the normal item, but try just the
615 // expanded one (and not selected) first in this case
616 image = GetImage(wxTreeItemIcon_Expanded);
617 }
618 }
619 else // not expanded
620 {
621 if ( IsSelected() )
622 image = GetImage(wxTreeItemIcon_Selected);
623 }
624
618a5e38
RR
625 // maybe it doesn't have the specific image we want,
626 // try the default one instead
627 if ( image == NO_IMAGE ) image = GetImage();
8dc99046
VZ
628
629 return image;
630}
631
f135ff73 632// -----------------------------------------------------------------------------
941830cb 633// wxGenericTreeCtrl implementation
f135ff73
VZ
634// -----------------------------------------------------------------------------
635
941830cb 636IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow)
f135ff73 637
941830cb
JS
638BEGIN_EVENT_TABLE(wxGenericTreeCtrl,wxScrolledWindow)
639 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
640 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
641 EVT_CHAR (wxGenericTreeCtrl::OnChar)
642 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
643 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
644 EVT_IDLE (wxGenericTreeCtrl::OnIdle)
f135ff73
VZ
645END_EVENT_TABLE()
646
618a5e38 647#if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
233058c7
JS
648/*
649 * wxTreeCtrl has to be a real class or we have problems with
650 * the run-time information.
651 */
652
653IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
654#endif
655
f135ff73
VZ
656// -----------------------------------------------------------------------------
657// construction/destruction
658// -----------------------------------------------------------------------------
91b8de8d 659
941830cb 660void wxGenericTreeCtrl::Init()
c801d85f 661{
618a5e38 662 m_current = m_key_current = m_anchor = (wxGenericTreeItem *) NULL;
00e12320
RR
663 m_hasFocus = FALSE;
664 m_dirty = FALSE;
f135ff73 665
00e12320
RR
666 m_lineHeight = 10;
667 m_indent = 15;
668 m_spacing = 18;
f135ff73 669
b771aa29
VZ
670 m_hilightBrush = new wxBrush
671 (
672 wxSystemSettings::GetSystemColour
673 (
674 wxSYS_COLOUR_HIGHLIGHT
675 ),
676 wxSOLID
677 );
678
679 m_hilightUnfocusedBrush = new wxBrush
680 (
681 wxSystemSettings::GetSystemColour
682 (
683 wxSYS_COLOUR_BTNSHADOW
684 ),
685 wxSOLID
686 );
f135ff73 687
618a5e38 688 m_imageListNormal = m_imageListButtons =
00e12320 689 m_imageListState = (wxImageList *) NULL;
618a5e38 690 m_ownsImageListNormal = m_ownsImageListButtons =
46cd520d 691 m_ownsImageListState = FALSE;
978f38c2 692
00e12320 693 m_dragCount = 0;
3dbeaa52 694 m_isDragging = FALSE;
618a5e38 695 m_dropTarget = m_oldSelection = (wxGenericTreeItem *)NULL;
9dfbf520 696
00e12320 697 m_renameTimer = new wxTreeRenameTimer( this );
f6bcfd97 698 m_lastOnSame = FALSE;
f38374d0 699
00e12320
RR
700 m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT );
701 m_boldFont = wxFont( m_normalFont.GetPointSize(),
618a5e38
RR
702 m_normalFont.GetFamily(),
703 m_normalFont.GetStyle(),
704 wxBOLD,
705 m_normalFont.GetUnderlined());
edaa81ae 706}
c801d85f 707
618a5e38
RR
708bool wxGenericTreeCtrl::Create(wxWindow *parent,
709 wxWindowID id,
710 const wxPoint& pos,
711 const wxSize& size,
712 long style,
713 const wxValidator &validator,
714 const wxString& name )
c801d85f 715{
2593f033
RR
716#ifdef __WXMAC__
717 int major,minor;
718 wxGetOsVersion( &major, &minor );
719
720 if (style & wxTR_HAS_BUTTONS) style |= wxTR_MAC_BUTTONS;
f6eaeda1 721 if (style & wxTR_HAS_BUTTONS) style &= ~wxTR_HAS_BUTTONS;
2593f033
RR
722 style &= ~wxTR_LINES_AT_ROOT;
723 style |= wxTR_NO_LINES;
724 if (major < 10)
725 style |= wxTR_ROW_LINES;
17d5bdf9
RR
726 if (major >= 10)
727 style |= wxTR_AQUA_BUTTONS;
2593f033
RR
728#endif
729
d21d2e5a
RR
730 if (style & wxTR_AQUA_BUTTONS)
731 {
732 m_arrowRight = new wxBitmap( aqua_arrow_right );
733 m_arrowDown = new wxBitmap( aqua_arrow_down );
734 }
735 else
736 {
737 m_arrowRight = NULL;
738 m_arrowDown = NULL;
739 }
17d5bdf9 740
618a5e38
RR
741 wxScrolledWindow::Create( parent, id, pos, size,
742 style|wxHSCROLL|wxVSCROLL, name );
743
744 // If the tree display has no buttons, but does have
745 // connecting lines, we can use a narrower layout.
746 // It may not be a good idea to force this...
747 if (!HasButtons() && !HasFlag(wxTR_NO_LINES))
748 {
749 m_indent= 10;
750 m_spacing = 10;
751 }
978f38c2 752
ce4169a4 753#if wxUSE_VALIDATORS
00e12320 754 SetValidator( validator );
ce4169a4 755#endif
f135ff73 756
91fc2bdc 757 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
c22886bd 758
00e12320 759// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
9e0a12c9 760 m_dottedPen = wxPen( wxT("grey"), 0, 0 );
f135ff73 761
00e12320 762 return TRUE;
edaa81ae 763}
c801d85f 764
941830cb 765wxGenericTreeCtrl::~wxGenericTreeCtrl()
c801d85f 766{
b771aa29
VZ
767 delete m_hilightBrush;
768 delete m_hilightUnfocusedBrush;
d21d2e5a
RR
769
770 if (m_arrowRight) delete m_arrowRight;
771 if (m_arrowDown) delete m_arrowDown;
a43a4f9d 772
00e12320 773 DeleteAllItems();
9dfbf520 774
00e12320 775 delete m_renameTimer;
46cd520d
VS
776 if (m_ownsImageListNormal) delete m_imageListNormal;
777 if (m_ownsImageListState) delete m_imageListState;
618a5e38 778 if (m_ownsImageListButtons) delete m_imageListButtons;
edaa81ae 779}
c801d85f 780
f135ff73
VZ
781// -----------------------------------------------------------------------------
782// accessors
783// -----------------------------------------------------------------------------
784
941830cb 785size_t wxGenericTreeCtrl::GetCount() const
c801d85f 786{
f2593d0d 787 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
edaa81ae 788}
c801d85f 789
941830cb 790void wxGenericTreeCtrl::SetIndent(unsigned int indent)
c801d85f 791{
f2593d0d
RR
792 m_indent = indent;
793 m_dirty = TRUE;
cf724bce
RR
794}
795
941830cb 796void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
cf724bce 797{
f2593d0d
RR
798 m_spacing = spacing;
799 m_dirty = TRUE;
f135ff73 800}
74bedbeb 801
941830cb 802size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
4832f7c0 803{
f2593d0d 804 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
4832f7c0 805
941830cb 806 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
4832f7c0
VZ
807}
808
618a5e38
RR
809void wxGenericTreeCtrl::SetWindowStyle(const long styles)
810{
811 // right now, just sets the styles. Eventually, we may
812 // want to update the inherited styles, but right now
813 // none of the parents has updatable styles
814 m_windowStyle = styles;
815 m_dirty = TRUE;
816}
817
f135ff73
VZ
818// -----------------------------------------------------------------------------
819// functions to work with tree items
820// -----------------------------------------------------------------------------
74bedbeb 821
941830cb 822wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
f135ff73 823{
f2593d0d 824 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
4832f7c0 825
941830cb 826 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
edaa81ae 827}
74bedbeb 828
941830cb 829int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
8dc99046 830 wxTreeItemIcon which) const
74bedbeb 831{
f2593d0d 832 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 833
941830cb 834 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
edaa81ae 835}
c801d85f 836
941830cb 837wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 838{
f2593d0d 839 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 840
941830cb 841 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
edaa81ae 842}
c801d85f 843
941830cb 844void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
f135ff73 845{
f2593d0d 846 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 847
f2593d0d 848 wxClientDC dc(this);
941830cb 849 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
850 pItem->SetText(text);
851 CalculateSize(pItem, dc);
852 RefreshLine(pItem);
f135ff73 853}
c801d85f 854
941830cb 855void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
8dc99046
VZ
856 int image,
857 wxTreeItemIcon which)
f135ff73 858{
223d09f6 859 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 860
941830cb 861 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
8dc99046 862 pItem->SetImage(image, which);
4832f7c0 863
8dc99046
VZ
864 wxClientDC dc(this);
865 CalculateSize(pItem, dc);
866 RefreshLine(pItem);
edaa81ae 867}
c801d85f 868
941830cb 869void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 870{
f2593d0d 871 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 872
941830cb 873 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
edaa81ae 874}
c801d85f 875
941830cb 876void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 877{
f2593d0d 878 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 879
941830cb 880 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
881 pItem->SetHasPlus(has);
882 RefreshLine(pItem);
edaa81ae 883}
c801d85f 884
941830cb 885void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
ef44a621 886{
9ec64fa7 887 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 888
9ec64fa7 889 // avoid redrawing the tree if no real change
941830cb 890 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
891 if ( pItem->IsBold() != bold )
892 {
893 pItem->SetBold(bold);
894 RefreshLine(pItem);
895 }
896}
897
941830cb 898void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
9ec64fa7
VZ
899 const wxColour& col)
900{
901 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
902
941830cb 903 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
904 pItem->Attr().SetTextColour(col);
905 RefreshLine(pItem);
906}
907
941830cb 908void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
9ec64fa7
VZ
909 const wxColour& col)
910{
911 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
912
941830cb 913 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
914 pItem->Attr().SetBackgroundColour(col);
915 RefreshLine(pItem);
916}
917
941830cb 918void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
9ec64fa7
VZ
919{
920 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
921
941830cb 922 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7 923 pItem->Attr().SetFont(font);
ef44a621 924 RefreshLine(pItem);
ef44a621
VZ
925}
926
3da2715f
JS
927bool wxGenericTreeCtrl::SetFont( const wxFont &font )
928{
929 wxScrolledWindow::SetFont(font);
930
931 m_normalFont = font ;
932 m_boldFont = wxFont( m_normalFont.GetPointSize(),
933 m_normalFont.GetFamily(),
934 m_normalFont.GetStyle(),
935 wxBOLD,
936 m_normalFont.GetUnderlined());
937
938 return TRUE;
939}
940
941
f135ff73
VZ
942// -----------------------------------------------------------------------------
943// item status inquiries
944// -----------------------------------------------------------------------------
945
1ed01484 946bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
c801d85f 947{
1ed01484
JS
948 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
949
c22886bd 950 // An item is only visible if it's not a descendant of a collapsed item
1ed01484 951 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
c22886bd
RR
952 wxGenericTreeItem* parent = pItem->GetParent();
953 while (parent)
954 {
955 if (!parent->IsExpanded())
956 return FALSE;
957 parent = parent->GetParent();
958 }
1ed01484
JS
959
960 int startX, startY;
961 GetViewStart(& startX, & startY);
962
963 wxSize clientSize = GetClientSize();
964
965 wxRect rect;
966 if (!GetBoundingRect(item, rect))
967 return FALSE;
c22886bd
RR
968 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
969 return FALSE;
1ed01484
JS
970 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
971 return FALSE;
972 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
973 return FALSE;
f135ff73 974
f2593d0d 975 return TRUE;
edaa81ae 976}
c801d85f 977
941830cb 978bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 979{
f2593d0d 980 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 981
59a2e635
VZ
982 // consider that the item does have children if it has the "+" button: it
983 // might not have them (if it had never been expanded yet) but then it
984 // could have them as well and it's better to err on this side rather than
985 // disabling some operations which are restricted to the items with
986 // children for an item which does have them
607d9cfc 987 return ((wxGenericTreeItem*) item.m_pItem)->HasPlus();
edaa81ae 988}
c801d85f 989
941830cb 990bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 991{
f2593d0d 992 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 993
941830cb 994 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
f135ff73 995}
29d87bba 996
941830cb 997bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
f135ff73 998{
f2593d0d 999 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 1000
941830cb 1001 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
f135ff73 1002}
29d87bba 1003
941830cb 1004bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
ef44a621 1005{
f2593d0d 1006 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
ef44a621 1007
941830cb 1008 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
ef44a621
VZ
1009}
1010
f135ff73
VZ
1011// -----------------------------------------------------------------------------
1012// navigation
1013// -----------------------------------------------------------------------------
29d87bba 1014
941830cb 1015wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const
f135ff73 1016{
618a5e38 1017 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 1018
618a5e38 1019 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
f135ff73 1020}
29d87bba 1021
941830cb 1022wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
f135ff73 1023{
618a5e38 1024 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1025
618a5e38
RR
1026 cookie = 0;
1027 return GetNextChild(item, cookie);
f135ff73 1028}
29d87bba 1029
941830cb 1030wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
f135ff73 1031{
618a5e38 1032 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1033
618a5e38
RR
1034 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1035 if ( (size_t)cookie < children.Count() )
1036 {
1037 return children.Item((size_t)cookie++);
1038 }
1039 else
1040 {
1041 // there are no more of them
1042 return wxTreeItemId();
1043 }
f135ff73 1044}
29d87bba 1045
941830cb 1046wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
978f38c2 1047{
618a5e38 1048 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 1049
618a5e38
RR
1050 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1051 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
1052}
1053
941830cb 1054wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
f135ff73 1055{
618a5e38 1056 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1057
618a5e38
RR
1058 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1059 wxGenericTreeItem *parent = i->GetParent();
1060 if ( parent == NULL )
1061 {
1062 // root item doesn't have any siblings
1063 return wxTreeItemId();
1064 }
4832f7c0 1065
618a5e38
RR
1066 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1067 int index = siblings.Index(i);
1068 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1069
618a5e38
RR
1070 size_t n = (size_t)(index + 1);
1071 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 1072}
c801d85f 1073
941830cb 1074wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 1075{
618a5e38 1076 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1077
618a5e38
RR
1078 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1079 wxGenericTreeItem *parent = i->GetParent();
1080 if ( parent == NULL )
1081 {
1082 // root item doesn't have any siblings
1083 return wxTreeItemId();
1084 }
4832f7c0 1085
618a5e38
RR
1086 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1087 int index = siblings.Index(i);
1088 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1089
618a5e38
RR
1090 return index == 0 ? wxTreeItemId()
1091 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 1092}
389cdc7a 1093
1ed01484
JS
1094// Only for internal use right now, but should probably be public
1095wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
f135ff73 1096{
618a5e38
RR
1097 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1098
1099 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1100
1101 // First see if there are any children.
1102 wxArrayGenericTreeItems& children = i->GetChildren();
1103 if (children.GetCount() > 0)
1104 {
1105 return children.Item(0);
1106 }
1107 else
1108 {
1109 // Try a sibling of this or ancestor instead
1110 wxTreeItemId p = item;
1111 wxTreeItemId toFind;
1112 do
1113 {
1114 toFind = GetNextSibling(p);
1115 p = GetParent(p);
1116 } while (p.IsOk() && !toFind.IsOk());
1117 return toFind;
1118 }
1ed01484
JS
1119}
1120
1ed01484
JS
1121wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1122{
618a5e38
RR
1123 wxTreeItemId id = GetRootItem();
1124 if (!id.IsOk())
1ed01484 1125 return id;
1ed01484 1126
618a5e38
RR
1127 do
1128 {
1129 if (IsVisible(id))
1130 return id;
1131 id = GetNext(id);
1132 } while (id.IsOk());
1133
1134 return wxTreeItemId();
1ed01484
JS
1135}
1136
941830cb 1137wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
f135ff73 1138{
618a5e38 1139 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1140
618a5e38
RR
1141 wxTreeItemId id = item;
1142 if (id.IsOk())
1143 {
1144 while (id = GetNext(id), id.IsOk())
1145 {
1146 if (IsVisible(id))
1147 return id;
1148 }
1149 }
1150 return wxTreeItemId();
f135ff73 1151}
29d87bba 1152
941830cb 1153wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
f135ff73 1154{
618a5e38 1155 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1156
618a5e38 1157 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1158
618a5e38 1159 return wxTreeItemId();
edaa81ae 1160}
c801d85f 1161
f135ff73
VZ
1162// -----------------------------------------------------------------------------
1163// operations
1164// -----------------------------------------------------------------------------
1165
941830cb 1166wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1167 size_t previous,
1168 const wxString& text,
1169 int image, int selImage,
1170 wxTreeItemData *data)
c801d85f 1171{
941830cb 1172 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f49f2b0c
RR
1173 if ( !parent )
1174 {
1175 // should we give a warning here?
1176 return AddRoot(text, image, selImage, data);
1177 }
4832f7c0 1178
618a5e38
RR
1179 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1180
f38374d0 1181 wxGenericTreeItem *item =
2b84e565 1182 new wxGenericTreeItem( parent, text, image, selImage, data );
74bedbeb 1183
f49f2b0c
RR
1184 if ( data != NULL )
1185 {
40fab78b 1186 data->m_pItem = (long) item;
f49f2b0c 1187 }
74bedbeb 1188
f49f2b0c 1189 parent->Insert( item, previous );
ef44a621 1190
f49f2b0c 1191 return item;
4c681997
RR
1192}
1193
941830cb 1194wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
f135ff73
VZ
1195 int image, int selImage,
1196 wxTreeItemData *data)
4c681997 1197{
f49f2b0c 1198 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 1199
618a5e38
RR
1200 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1201
2b84e565 1202 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text,
f135ff73 1203 image, selImage, data);
618a5e38
RR
1204 if (HasFlag(wxTR_HIDE_ROOT))
1205 {
1206 // if root is hidden, make sure we can navigate
1207 // into children
1208 m_anchor->SetHasPlus();
1209 Expand(m_anchor);
1210 }
f49f2b0c
RR
1211 if ( data != NULL )
1212 {
40fab78b 1213 data->m_pItem = (long) m_anchor;
f49f2b0c 1214 }
f38374d0 1215
f49f2b0c
RR
1216 if (!HasFlag(wxTR_MULTIPLE))
1217 {
1218 m_current = m_key_current = m_anchor;
06b466c7 1219 m_current->SetHilight( TRUE );
f49f2b0c 1220 }
389cdc7a 1221
f49f2b0c 1222 return m_anchor;
edaa81ae 1223}
c801d85f 1224
941830cb 1225wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
f135ff73
VZ
1226 const wxString& text,
1227 int image, int selImage,
1228 wxTreeItemData *data)
c801d85f 1229{
f2593d0d 1230 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 1231}
c801d85f 1232
941830cb 1233wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1234 const wxTreeItemId& idPrevious,
1235 const wxString& text,
1236 int image, int selImage,
1237 wxTreeItemData *data)
c801d85f 1238{
941830cb 1239 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1240 if ( !parent )
1241 {
1242 // should we give a warning here?
1243 return AddRoot(text, image, selImage, data);
1244 }
c801d85f 1245
941830cb 1246 int index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
f2593d0d 1247 wxASSERT_MSG( index != wxNOT_FOUND,
941830cb 1248 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
06b466c7 1249
f2593d0d
RR
1250 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1251}
1252
941830cb 1253wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f2593d0d
RR
1254 size_t before,
1255 const wxString& text,
1256 int image, int selImage,
1257 wxTreeItemData *data)
1258{
941830cb 1259 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1260 if ( !parent )
1261 {
1262 // should we give a warning here?
1263 return AddRoot(text, image, selImage, data);
1264 }
1265
1266 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 1267}
c801d85f 1268
941830cb 1269wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
f135ff73
VZ
1270 const wxString& text,
1271 int image, int selImage,
1272 wxTreeItemData *data)
74bedbeb 1273{
941830cb 1274 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1275 if ( !parent )
1276 {
1277 // should we give a warning here?
1278 return AddRoot(text, image, selImage, data);
1279 }
f135ff73 1280
f2593d0d
RR
1281 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1282 image, selImage, data);
74bedbeb
VZ
1283}
1284
941830cb 1285void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
a43a4f9d 1286{
f2593d0d 1287 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
40fab78b 1288 event.m_item = (long) item;
f2593d0d
RR
1289 event.SetEventObject( this );
1290 ProcessEvent( event );
a43a4f9d
VZ
1291}
1292
941830cb 1293void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
372edb9d 1294{
618a5e38
RR
1295 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1296
941830cb 1297 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
a43a4f9d 1298 item->DeleteChildren(this);
372edb9d
VZ
1299}
1300
941830cb 1301void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1302{
618a5e38
RR
1303 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1304
941830cb 1305 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
ff5bf259 1306
618a5e38
RR
1307 // don't stay with invalid m_key_current or we will crash in
1308 // the next call to OnChar()
aaa2f297
VZ
1309 bool changeKeyCurrent = FALSE;
1310 wxGenericTreeItem *itemKey = m_key_current;
618a5e38 1311 while ( itemKey )
aaa2f297
VZ
1312 {
1313 if ( itemKey == item )
1314 {
1315 // m_key_current is a descendant of the item being deleted
1316 changeKeyCurrent = TRUE;
618a5e38 1317 break;
aaa2f297 1318 }
618a5e38 1319 itemKey = itemKey->GetParent();
aaa2f297
VZ
1320 }
1321
1322 wxGenericTreeItem *parent = item->GetParent();
97d7bfb8
RR
1323 if ( parent )
1324 {
1325 parent->GetChildren().Remove( item ); // remove by value
1326 }
f135ff73 1327
aaa2f297
VZ
1328 if ( changeKeyCurrent )
1329 {
1330 // may be NULL or not
1331 m_key_current = parent;
1332 }
1333
97d7bfb8
RR
1334 item->DeleteChildren(this);
1335 SendDeleteEvent(item);
1336 delete item;
edaa81ae 1337}
c801d85f 1338
941830cb 1339void wxGenericTreeCtrl::DeleteAllItems()
c801d85f 1340{
97d7bfb8
RR
1341 if ( m_anchor )
1342 {
618a5e38
RR
1343 m_dirty = TRUE;
1344
97d7bfb8
RR
1345 m_anchor->DeleteChildren(this);
1346 delete m_anchor;
a43a4f9d 1347
97d7bfb8 1348 m_anchor = NULL;
97d7bfb8 1349 }
edaa81ae
RR
1350}
1351
941830cb 1352void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1353{
941830cb 1354 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1355
941830cb 1356 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
f6bcfd97 1357
f2593d0d
RR
1358 if ( !item->HasPlus() )
1359 return;
978f38c2 1360
f2593d0d
RR
1361 if ( item->IsExpanded() )
1362 return;
f135ff73 1363
f2593d0d 1364 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
40fab78b 1365 event.m_item = (long) item;
f2593d0d 1366 event.SetEventObject( this );
004fd0c8 1367
f2593d0d
RR
1368 if ( ProcessEvent( event ) && !event.IsAllowed() )
1369 {
1370 // cancelled by program
1371 return;
1372 }
4832f7c0 1373
f2593d0d
RR
1374 item->Expand();
1375 CalculatePositions();
f135ff73 1376
f2593d0d 1377 RefreshSubtree(item);
f135ff73 1378
f2593d0d
RR
1379 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1380 ProcessEvent( event );
edaa81ae
RR
1381}
1382
941830cb 1383void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
f6bcfd97
BP
1384{
1385 Expand(item);
1386 if ( IsExpanded(item) )
1387 {
1388 long cookie;
1389 wxTreeItemId child = GetFirstChild(item, cookie);
1390 while ( child.IsOk() )
1391 {
1392 ExpandAll(child);
1393
1394 child = GetNextChild(item, cookie);
1395 }
1396 }
1397}
1398
941830cb 1399void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1400{
941830cb 1401 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1402
f2593d0d
RR
1403 if ( !item->IsExpanded() )
1404 return;
f135ff73 1405
f2593d0d 1406 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
40fab78b 1407 event.m_item = (long) item;
f2593d0d
RR
1408 event.SetEventObject( this );
1409 if ( ProcessEvent( event ) && !event.IsAllowed() )
1410 {
1411 // cancelled by program
1412 return;
1413 }
4832f7c0 1414
f2593d0d 1415 item->Collapse();
f135ff73 1416
618a5e38 1417#if 0 // TODO why should items be collapsed recursively?
f2593d0d
RR
1418 wxArrayGenericTreeItems& children = item->GetChildren();
1419 size_t count = children.Count();
1420 for ( size_t n = 0; n < count; n++ )
1421 {
1422 Collapse(children[n]);
1423 }
618a5e38 1424#endif
f135ff73 1425
f2593d0d 1426 CalculatePositions();
f135ff73 1427
f2593d0d 1428 RefreshSubtree(item);
f135ff73 1429
f2593d0d
RR
1430 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1431 ProcessEvent( event );
edaa81ae 1432}
c801d85f 1433
941830cb 1434void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1435{
00e12320
RR
1436 Collapse(item);
1437 DeleteChildren(item);
edaa81ae 1438}
c801d85f 1439
941830cb 1440void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1441{
941830cb 1442 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
389cdc7a 1443
00e12320
RR
1444 if (item->IsExpanded())
1445 Collapse(itemId);
1446 else
1447 Expand(itemId);
f135ff73 1448}
389cdc7a 1449
941830cb 1450void wxGenericTreeCtrl::Unselect()
f135ff73 1451{
00e12320
RR
1452 if (m_current)
1453 {
1454 m_current->SetHilight( FALSE );
1455 RefreshLine( m_current );
1456 }
edaa81ae 1457}
c801d85f 1458
941830cb 1459void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1460{
00e12320
RR
1461 if (item->IsSelected())
1462 {
1463 item->SetHilight(FALSE);
1464 RefreshLine(item);
1465 }
d3a9f4af 1466
00e12320 1467 if (item->HasChildren())
88ac883a 1468 {
00e12320
RR
1469 wxArrayGenericTreeItems& children = item->GetChildren();
1470 size_t count = children.Count();
1471 for ( size_t n = 0; n < count; ++n )
1472 {
1473 UnselectAllChildren(children[n]);
1474 }
88ac883a
VZ
1475 }
1476}
f135ff73 1477
941830cb 1478void wxGenericTreeCtrl::UnselectAll()
88ac883a 1479{
0a33446c
VZ
1480 wxTreeItemId rootItem = GetRootItem();
1481
1482 // the tree might not have the root item at all
1483 if ( rootItem )
1484 {
1485 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1486 }
88ac883a
VZ
1487}
1488
1489// Recursive function !
1490// To stop we must have crt_item<last_item
91b8de8d 1491// Algorithm :
88ac883a 1492// Tag all next children, when no more children,
d3a9f4af 1493// Move to parent (not to tag)
91b8de8d 1494// Keep going... if we found last_item, we stop.
941830cb 1495bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a
VZ
1496{
1497 wxGenericTreeItem *parent = crt_item->GetParent();
1498
00e12320
RR
1499 if (parent == NULL) // This is root item
1500 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1501
91b8de8d 1502 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1503 int index = children.Index(crt_item);
1504 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1505
1506 size_t count = children.Count();
1507 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1508 {
1509 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1510 }
88ac883a
VZ
1511
1512 return TagNextChildren(parent, last_item, select);
1513}
1514
941830cb 1515bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a 1516{
00e12320
RR
1517 crt_item->SetHilight(select);
1518 RefreshLine(crt_item);
d3a9f4af 1519
06b466c7 1520 if (crt_item==last_item)
00e12320 1521 return TRUE;
88ac883a 1522
00e12320 1523 if (crt_item->HasChildren())
88ac883a 1524 {
00e12320
RR
1525 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1526 size_t count = children.Count();
1527 for ( size_t n = 0; n < count; ++n )
1528 {
06b466c7 1529 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1530 return TRUE;
06b466c7 1531 }
88ac883a 1532 }
d3a9f4af 1533
c0de7af4 1534 return FALSE;
88ac883a
VZ
1535}
1536
941830cb 1537void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
88ac883a 1538{
f2593d0d
RR
1539 // item2 is not necessary after item1
1540 wxGenericTreeItem *first=NULL, *last=NULL;
88ac883a 1541
f2593d0d
RR
1542 // choice first' and 'last' between item1 and item2
1543 if (item1->GetY()<item2->GetY())
06b466c7 1544 {
f2593d0d
RR
1545 first=item1;
1546 last=item2;
1547 }
1548 else
1549 {
1550 first=item2;
1551 last=item1;
1552 }
88ac883a 1553
f2593d0d 1554 bool select = m_current->IsSelected();
d3a9f4af 1555
f2593d0d
RR
1556 if ( TagAllChildrenUntilLast(first,last,select) )
1557 return;
88ac883a 1558
f2593d0d 1559 TagNextChildren(first,last,select);
88ac883a
VZ
1560}
1561
941830cb 1562void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
c193b707
VZ
1563 bool unselect_others,
1564 bool extended_select)
d3a9f4af 1565{
223d09f6 1566 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1567
88ac883a 1568 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
941830cb 1569 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
88ac883a
VZ
1570
1571 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1572 // wxT("this is a single selection tree") );
88ac883a
VZ
1573
1574 // to keep going anyhow !!!
d3a9f4af 1575 if (is_single)
8dc99046
VZ
1576 {
1577 if (item->IsSelected())
1578 return; // nothing to do
1579 unselect_others = TRUE;
1580 extended_select = FALSE;
1581 }
1582 else if ( unselect_others && item->IsSelected() )
1583 {
1584 // selection change if there is more than one item currently selected
1585 wxArrayTreeItemIds selected_items;
1586 if ( GetSelections(selected_items) == 1 )
1587 return;
1588 }
d3a9f4af 1589
f135ff73 1590 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
40fab78b
JS
1591 event.m_item = (long) item;
1592 event.m_itemOld = (long) m_current;
f135ff73 1593 event.SetEventObject( this );
91b8de8d 1594 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1595
f98e2558 1596 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
618a5e38 1597 return;
f135ff73 1598
2cc78389
RR
1599 wxTreeItemId parent = GetParent( itemId );
1600 while (parent.IsOk())
1601 {
1602 if (!IsExpanded(parent))
1603 Expand( parent );
cdb3cffe 1604
2cc78389
RR
1605 parent = GetParent( parent );
1606 }
cdb3cffe 1607
2cc78389 1608 EnsureVisible( itemId );
cdb3cffe 1609
88ac883a
VZ
1610 // ctrl press
1611 if (unselect_others)
389cdc7a 1612 {
88ac883a 1613 if (is_single) Unselect(); // to speed up thing
c193b707 1614 else UnselectAll();
edaa81ae 1615 }
f135ff73 1616
88ac883a 1617 // shift press
d3a9f4af 1618 if (extended_select)
88ac883a 1619 {
aaa2f297
VZ
1620 if ( !m_current )
1621 {
618a5e38 1622 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
1623 }
1624
88ac883a
VZ
1625 // don't change the mark (m_current)
1626 SelectItemRange(m_current, item);
1627 }
1628 else
1629 {
c0de7af4 1630 bool select=TRUE; // the default
88ac883a 1631
c193b707
VZ
1632 // Check if we need to toggle hilight (ctrl mode)
1633 if (!unselect_others)
618a5e38 1634 select=!item->IsSelected();
88ac883a 1635
91b8de8d 1636 m_current = m_key_current = item;
c193b707
VZ
1637 m_current->SetHilight(select);
1638 RefreshLine( m_current );
88ac883a 1639 }
389cdc7a 1640
f135ff73 1641 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1642 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1643}
1644
941830cb 1645void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
9dfbf520 1646 wxArrayTreeItemIds &array) const
c801d85f 1647{
8dc99046 1648 if ( item->IsSelected() )
9dfbf520 1649 array.Add(wxTreeItemId(item));
91b8de8d 1650
9dfbf520 1651 if ( item->HasChildren() )
91b8de8d 1652 {
9dfbf520
VZ
1653 wxArrayGenericTreeItems& children = item->GetChildren();
1654 size_t count = children.GetCount();
1655 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1656 FillArray(children[n], array);
91b8de8d
RR
1657 }
1658}
1659
941830cb 1660size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d 1661{
618a5e38
RR
1662 array.Empty();
1663 wxTreeItemId idRoot = GetRootItem();
1664 if ( idRoot.IsOk() )
1665 {
1666 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1667 }
1668 //else: the tree is empty, so no selections
91b8de8d 1669
618a5e38 1670 return array.Count();
91b8de8d
RR
1671}
1672
941830cb 1673void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1674{
91b8de8d
RR
1675 if (!item.IsOk()) return;
1676
941830cb 1677 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 1678
f65635b5
VZ
1679 // first expand all parent branches
1680 wxGenericTreeItem *parent = gitem->GetParent();
5391f772 1681 while ( parent )
f65635b5 1682 {
8dc99046 1683 Expand(parent);
f65635b5
VZ
1684 parent = parent->GetParent();
1685 }
1686
5391f772 1687 //if (parent) CalculatePositions();
91b8de8d
RR
1688
1689 ScrollTo(item);
1690}
1691
941830cb 1692void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d
RR
1693{
1694 if (!item.IsOk()) return;
1695
dc6c62a9
RR
1696 // We have to call this here because the label in
1697 // question might just have been added and no screen
1698 // update taken place.
cd66f45b 1699 if (m_dirty) wxYieldIfNeeded();
dc6c62a9 1700
941830cb 1701 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1702
f65635b5 1703 // now scroll to the item
0659e7ee 1704 int item_y = gitem->GetY();
8dc99046 1705
0659e7ee
RR
1706 int start_x = 0;
1707 int start_y = 0;
1e6feb95 1708 GetViewStart( &start_x, &start_y );
91b8de8d 1709 start_y *= PIXELS_PER_UNIT;
978f38c2 1710
a93109d5
RR
1711 int client_h = 0;
1712 int client_w = 0;
1713 GetClientSize( &client_w, &client_h );
ef44a621 1714
0659e7ee
RR
1715 if (item_y < start_y+3)
1716 {
91b8de8d 1717 // going down
0659e7ee
RR
1718 int x = 0;
1719 int y = 0;
91b8de8d
RR
1720 m_anchor->GetSize( x, y, this );
1721 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1722 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1723 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1724 // Item should appear at top
91b8de8d 1725 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1726 }
91b8de8d 1727 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1728 {
c7a9fa36
RR
1729 // going up
1730 int x = 0;
1731 int y = 0;
1732 m_anchor->GetSize( x, y, this );
1733 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1734 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1735 item_y += PIXELS_PER_UNIT+2;
1736 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1737 // Item should appear at bottom
c7a9fa36 1738 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 1739 }
edaa81ae 1740}
c801d85f 1741
e1ee62bd 1742// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 1743static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1744
004fd0c8 1745static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1746 wxGenericTreeItem **item2)
edaa81ae 1747{
941830cb 1748 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1749
1750 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1751}
1752
941830cb 1753int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
e1ee62bd 1754 const wxTreeItemId& item2)
0659e7ee 1755{
87138c52 1756 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1757}
1758
941830cb 1759void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 1760{
223d09f6 1761 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 1762
941830cb 1763 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 1764
e1ee62bd 1765 wxCHECK_RET( !s_treeBeingSorted,
941830cb 1766 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1767
91b8de8d 1768 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1769 if ( children.Count() > 1 )
1770 {
618a5e38
RR
1771 m_dirty = TRUE;
1772
e1ee62bd
VZ
1773 s_treeBeingSorted = this;
1774 children.Sort(tree_ctrl_compare_func);
1775 s_treeBeingSorted = NULL;
e1ee62bd
VZ
1776 }
1777 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1778}
1779
941830cb 1780wxImageList *wxGenericTreeCtrl::GetImageList() const
edaa81ae 1781{
f135ff73 1782 return m_imageListNormal;
edaa81ae
RR
1783}
1784
618a5e38
RR
1785wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
1786{
1787 return m_imageListButtons;
1788}
1789
941830cb 1790wxImageList *wxGenericTreeCtrl::GetStateImageList() const
c801d85f 1791{
f135ff73 1792 return m_imageListState;
edaa81ae 1793}
c801d85f 1794
618a5e38 1795void wxGenericTreeCtrl::CalculateLineHeight()
e2414cbe 1796{
f2593d0d
RR
1797 wxClientDC dc(this);
1798 m_lineHeight = (int)(dc.GetCharHeight() + 4);
06b466c7 1799
618a5e38
RR
1800 if ( m_imageListNormal )
1801 {
1802 // Calculate a m_lineHeight value from the normal Image sizes.
1803 // May be toggle off. Then wxGenericTreeCtrl will spread when
1804 // necessary (which might look ugly).
1805 int n = m_imageListNormal->GetImageCount();
1806 for (int i = 0; i < n ; i++)
1807 {
1808 int width = 0, height = 0;
1809 m_imageListNormal->GetSize(i, width, height);
1810 if (height > m_lineHeight) m_lineHeight = height;
1811 }
1812 }
1813
1814 if (m_imageListButtons)
f2593d0d 1815 {
618a5e38
RR
1816 // Calculate a m_lineHeight value from the Button image sizes.
1817 // May be toggle off. Then wxGenericTreeCtrl will spread when
1818 // necessary (which might look ugly).
1819 int n = m_imageListButtons->GetImageCount();
1820 for (int i = 0; i < n ; i++)
1821 {
1822 int width = 0, height = 0;
1823 m_imageListButtons->GetSize(i, width, height);
1824 if (height > m_lineHeight) m_lineHeight = height;
1825 }
f2593d0d 1826 }
91b8de8d 1827
618a5e38 1828 if (m_lineHeight < 30)
f2593d0d 1829 m_lineHeight += 2; // at least 2 pixels
06b466c7 1830 else
f2593d0d 1831 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 1832}
e2414cbe 1833
618a5e38
RR
1834void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
1835{
1836 if (m_ownsImageListNormal) delete m_imageListNormal;
1837 m_imageListNormal = imageList;
1838 m_ownsImageListNormal = FALSE;
1839 m_dirty = TRUE;
1840 CalculateLineHeight();
1841}
1842
941830cb 1843void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1844{
46cd520d 1845 if (m_ownsImageListState) delete m_imageListState;
f135ff73 1846 m_imageListState = imageList;
46cd520d
VS
1847 m_ownsImageListState = FALSE;
1848}
1849
618a5e38
RR
1850void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
1851{
1852 if (m_ownsImageListButtons) delete m_imageListButtons;
1853 m_imageListButtons = imageList;
1854 m_ownsImageListButtons = FALSE;
1855 m_dirty = TRUE;
1856 CalculateLineHeight();
1857}
1858
46cd520d
VS
1859void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
1860{
1861 SetImageList(imageList);
1862 m_ownsImageListNormal = TRUE;
1863}
1864
1865void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
1866{
1867 SetStateImageList(imageList);
1868 m_ownsImageListState = TRUE;
edaa81ae 1869}
e2414cbe 1870
618a5e38
RR
1871void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
1872{
1873 SetButtonsImageList(imageList);
1874 m_ownsImageListButtons = TRUE;
1875}
1876
f135ff73
VZ
1877// -----------------------------------------------------------------------------
1878// helpers
1879// -----------------------------------------------------------------------------
0659e7ee 1880
941830cb 1881void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 1882{
0659e7ee
RR
1883 if (m_anchor)
1884 {
618a5e38 1885 int x = 0, y = 0;
91b8de8d 1886 m_anchor->GetSize( x, y, this );
c193b707 1887 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1888 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1889 int x_pos = GetScrollPos( wxHORIZONTAL );
1890 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1891 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1892 }
1893 else
1894 {
1895 SetScrollbars( 0, 0, 0, 0 );
1896 }
edaa81ae 1897}
c801d85f 1898
941830cb 1899int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 1900{
dc6c62a9
RR
1901 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1902 return item->GetHeight();
1903 else
1904 return m_lineHeight;
91b8de8d
RR
1905}
1906
941830cb 1907void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 1908{
618a5e38
RR
1909 // TODO implement "state" icon on items
1910
9ec64fa7
VZ
1911 wxTreeItemAttr *attr = item->GetAttributes();
1912 if ( attr && attr->HasFont() )
1913 dc.SetFont(attr->GetFont());
1914 else if (item->IsBold())
eff869aa 1915 dc.SetFont(m_boldFont);
ef44a621 1916
618a5e38 1917 long text_w = 0, text_h = 0;
bbe0af5b 1918 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1919
618a5e38 1920 int image_h = 0, image_w = 0;
8dc99046
VZ
1921 int image = item->GetCurrentImage();
1922 if ( image != NO_IMAGE )
bbe0af5b 1923 {
2c8e4738
VZ
1924 if ( m_imageListNormal )
1925 {
1926 m_imageListNormal->GetSize( image, image_w, image_h );
1927 image_w += 4;
1928 }
1929 else
1930 {
1931 image = NO_IMAGE;
1932 }
bbe0af5b 1933 }
ef44a621 1934
91b8de8d
RR
1935 int total_h = GetLineHeight(item);
1936
b771aa29 1937 if ( item->IsSelected() )
cdb3cffe 1938 {
b771aa29 1939 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
cdb3cffe 1940 }
afa6a1a1 1941 else
c0fba4d1
VS
1942 {
1943 wxColour colBg;
1944 if ( attr && attr->HasBackgroundColour() )
1945 colBg = attr->GetBackgroundColour();
1946 else
1947 colBg = m_backgroundColour;
1948 dc.SetBrush(wxBrush(colBg, wxSOLID));
1949 }
afa6a1a1 1950
cdb3cffe 1951 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
b77d9650 1952
c6f4913a 1953 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
a69cb1cc 1954 {
c6f4913a
VS
1955 int x, y, w, h;
1956
1957 DoGetPosition(&x, &y);
1958 DoGetSize(&w, &h);
1959 dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset);
a69cb1cc
JS
1960 }
1961 else
b77d9650 1962 {
c6f4913a
VS
1963 if ( item->IsSelected() && image != NO_IMAGE )
1964 {
1965 // If it's selected, and there's an image, then we should
1966 // take care to leave the area under the image painted in the
1967 // background colour.
1968 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
1969 item->GetWidth() - image_w + 2, total_h-offset );
1970 }
1971 else
1972 {
1973 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
1974 item->GetWidth()+2, total_h-offset );
1975 }
b77d9650 1976 }
ef44a621 1977
8dc99046 1978 if ( image != NO_IMAGE )
bbe0af5b 1979 {
d30b4d20 1980 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 1981 m_imageListNormal->Draw( image, dc,
49cd56ef 1982 item->GetX(),
d701d432 1983 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1984 wxIMAGELIST_DRAW_TRANSPARENT );
1985 dc.DestroyClippingRegion();
1986 }
ef44a621 1987
afa6a1a1 1988 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
1989 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
1990 dc.DrawText( item->GetText(),
1991 (wxCoord)(image_w + item->GetX()),
1992 (wxCoord)(item->GetY() + extraH));
ef44a621 1993
eff869aa
RR
1994 // restore normal font
1995 dc.SetFont( m_normalFont );
ef44a621
VZ
1996}
1997
91b8de8d 1998// Now y stands for the top of the item, whereas it used to stand for middle !
941830cb 1999void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2000{
618a5e38
RR
2001 int x = level*m_indent;
2002 if (!HasFlag(wxTR_HIDE_ROOT))
2003 {
2004 x += m_indent;
2005 }
2006 else if (level == 0)
2007 {
2b84e565
VS
2008 // always expand hidden root
2009 int origY = y;
618a5e38 2010 wxArrayGenericTreeItems& children = item->GetChildren();
2b84e565
VS
2011 int count = children.Count();
2012 if (count > 0)
2013 {
2014 int n = 0, oldY;
2015 do {
2016 oldY = y;
2017 PaintLevel(children[n], dc, 1, y);
2018 } while (++n < count);
2019
e76520fd 2020 if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
2b84e565
VS
2021 {
2022 // draw line down to last child
2023 origY += GetLineHeight(children[0])>>1;
2024 oldY += GetLineHeight(children[n-1])>>1;
2025 dc.DrawLine(3, origY, 3, oldY);
2026 }
2027 }
618a5e38
RR
2028 return;
2029 }
91b8de8d 2030
618a5e38
RR
2031 item->SetX(x+m_spacing);
2032 item->SetY(y);
389cdc7a 2033
618a5e38
RR
2034 int h = GetLineHeight(item);
2035 int y_top = y;
2036 int y_mid = y_top + (h>>1);
2037 y += h;
4832f7c0 2038
618a5e38
RR
2039 int exposed_x = dc.LogicalToDeviceX(0);
2040 int exposed_y = dc.LogicalToDeviceY(y_top);
233058c7 2041
618a5e38 2042 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
bbe0af5b 2043 {
c6f4913a
VS
2044 wxPen *pen =
2045#ifndef __WXMAC__
2046 // don't draw rect outline if we already have the
2047 // background color under Mac
2048 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2049#endif // !__WXMAC__
2050 wxTRANSPARENT_PEN;
2051
2052 wxColour colText;
2053 if ( item->IsSelected() )
2054 {
2055 colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
2056 }
2057 else
2058 {
2059 wxTreeItemAttr *attr = item->GetAttributes();
2060 if (attr && attr->HasTextColour())
2061 colText = attr->GetTextColour();
2062 else
2063 colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT);
2064 }
2065
2066 // prepare to draw
2067 dc.SetTextForeground(colText);
2068 dc.SetPen(*pen);
2069
2070 // draw
2071 PaintItem(item, dc);
2072
2073 if (HasFlag(wxTR_ROW_LINES))
2074 {
2075 // if the background colour is white, choose a
2076 // contrasting color for the lines
2077 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2078 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2079 dc.DrawLine(0, y_top, 10000, y_top);
2080 dc.DrawLine(0, y, 10000, y);
2081 }
2082
2083 // restore DC objects
2084 dc.SetBrush(*wxWHITE_BRUSH);
2085 dc.SetPen(m_dottedPen);
2086 dc.SetTextForeground(*wxBLACK);
2087
618a5e38 2088 if (item->HasPlus() && HasButtons()) // should the item show a button?
cdb3cffe 2089 {
618a5e38
RR
2090 if (!HasFlag(wxTR_NO_LINES))
2091 {
2b84e565 2092 if (x > (signed)m_indent)
618a5e38 2093 dc.DrawLine(x - m_indent, y_mid, x - 5, y_mid);
2b84e565
VS
2094 else if (HasFlag(wxTR_LINES_AT_ROOT))
2095 dc.DrawLine(3, y_mid, x - 5, y_mid);
618a5e38
RR
2096 dc.DrawLine(x + 5, y_mid, x + m_spacing, y_mid);
2097 }
2098
2099 if (m_imageListButtons != NULL)
c22886bd 2100 {
618a5e38 2101 // draw the image button here
2b84e565
VS
2102 int image_h = 0, image_w = 0, image = wxTreeItemIcon_Normal;
2103 if (item->IsExpanded()) image = wxTreeItemIcon_Expanded;
618a5e38 2104 if (item->IsSelected())
2b84e565 2105 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
618a5e38
RR
2106 m_imageListButtons->GetSize(image, image_w, image_h);
2107 int xx = x - (image_w>>1);
2108 int yy = y_mid - (image_h>>1);
2109 dc.SetClippingRegion(xx, yy, image_w, image_h);
2110 m_imageListButtons->Draw(image, dc, xx, yy,
2111 wxIMAGELIST_DRAW_TRANSPARENT);
2112 dc.DestroyClippingRegion();
2113 }
2114 else if (HasFlag(wxTR_TWIST_BUTTONS))
2115 {
2116 // draw the twisty button here
17d5bdf9 2117
17d5bdf9 2118 if (HasFlag(wxTR_AQUA_BUTTONS))
c22886bd 2119 {
17d5bdf9 2120 if (item->IsExpanded())
d21d2e5a 2121 dc.DrawBitmap( *m_arrowDown, x-5, y_mid-6, TRUE );
17d5bdf9 2122 else
d21d2e5a 2123 dc.DrawBitmap( *m_arrowRight, x-5, y_mid-6, TRUE );
c22886bd
RR
2124 }
2125 else
2126 {
d21d2e5a 2127 dc.SetBrush(*m_hilightBrush);
17d5bdf9 2128 dc.SetPen(*wxBLACK_PEN);
d21d2e5a 2129 wxPoint button[3];
17d5bdf9
RR
2130
2131 if (item->IsExpanded())
2132 {
2133 button[0].x = x-5;
2134 button[0].y = y_mid-2;
2135 button[1].x = x+5;
2136 button[1].y = y_mid-2;
2137 button[2].x = x;
2138 button[2].y = y_mid+3;
2139 }
2140 else
2141 {
2142 button[0].y = y_mid-5;
2143 button[0].x = x-2;
2144 button[1].y = y_mid+5;
2145 button[1].x = x-2;
2146 button[2].y = y_mid;
2147 button[2].x = x+3;
2148 }
d21d2e5a
RR
2149 dc.DrawPolygon(3, button);
2150 dc.SetPen(m_dottedPen);
c22886bd 2151 }
c22886bd 2152 }
618a5e38 2153 else // if (HasFlag(wxTR_HAS_BUTTONS))
c22886bd 2154 {
618a5e38
RR
2155 // draw the plus sign here
2156 dc.SetPen(*wxGREY_PEN);
2157 dc.SetBrush(*wxWHITE_BRUSH);
2158 dc.DrawRectangle(x-5, y_mid-4, 11, 9);
2159 dc.SetPen(*wxBLACK_PEN);
2160 dc.DrawLine(x-2, y_mid, x+3, y_mid);
c22886bd 2161 if (!item->IsExpanded())
618a5e38
RR
2162 dc.DrawLine(x, y_mid-2, x, y_mid+3);
2163 dc.SetPen(m_dottedPen);
c22886bd 2164 }
bbe0af5b 2165 }
618a5e38
RR
2166 else if (!HasFlag(wxTR_NO_LINES)) // no button; maybe a line?
2167 {
2168 // draw the horizontal line here
2169 int x_start = x;
2b84e565 2170 if (x > (signed)m_indent)
618a5e38 2171 x_start -= m_indent;
2b84e565
VS
2172 else if (HasFlag(wxTR_LINES_AT_ROOT))
2173 x_start = 3;
618a5e38
RR
2174 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2175 }
f135ff73 2176 }
d3a9f4af 2177
bbe0af5b
RR
2178 if (item->IsExpanded())
2179 {
91b8de8d 2180 wxArrayGenericTreeItems& children = item->GetChildren();
618a5e38 2181 int count = children.Count();
f65635b5 2182 if (count > 0)
9ec64fa7 2183 {
618a5e38
RR
2184 int n = 0, oldY;
2185 ++level;
2186 do {
2187 oldY = y;
2188 PaintLevel(children[n], dc, level, y);
2189 } while (++n < count);
2190
e76520fd 2191 if (!HasFlag(wxTR_NO_LINES) && count > 0)
618a5e38
RR
2192 {
2193 // draw line down to last child
2b84e565 2194 oldY += GetLineHeight(children[n-1])>>1;
618a5e38
RR
2195 if (HasButtons()) y_mid += 5;
2196 dc.DrawLine(x, y_mid, x, oldY);
2197 }
9ec64fa7 2198 }
bbe0af5b 2199 }
4c681997 2200}
c801d85f 2201
941830cb 2202void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
2203{
2204 if ( item )
2205 {
2206 if ( item->HasPlus() )
2207 {
2208 // it's a folder, indicate it by a border
2209 DrawBorder(item);
2210 }
2211 else
2212 {
2213 // draw a line under the drop target because the item will be
2214 // dropped there
2215 DrawLine(item, TRUE /* below */);
2216 }
2217
2218 SetCursor(wxCURSOR_BULLSEYE);
2219 }
2220 else
2221 {
2222 // can't drop here
2223 SetCursor(wxCURSOR_NO_ENTRY);
2224 }
2225}
2226
941830cb 2227void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 2228{
941830cb 2229 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2230
941830cb 2231 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2232
1044a386 2233 wxClientDC dc(this);
91b8de8d
RR
2234 PrepareDC( dc );
2235 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 2236 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 2237
3dbeaa52
VZ
2238 int w = i->GetWidth() + 2;
2239 int h = GetLineHeight(i) + 2;
91b8de8d 2240
3dbeaa52 2241 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
2242}
2243
941830cb 2244void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 2245{
941830cb 2246 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2247
941830cb 2248 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2249
1044a386 2250 wxClientDC dc(this);
91b8de8d
RR
2251 PrepareDC( dc );
2252 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 2253
3dbeaa52
VZ
2254 int x = i->GetX(),
2255 y = i->GetY();
2256 if ( below )
2257 {
2258 y += GetLineHeight(i) - 1;
2259 }
91b8de8d 2260
3dbeaa52 2261 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
2262}
2263
f135ff73
VZ
2264// -----------------------------------------------------------------------------
2265// wxWindows callbacks
2266// -----------------------------------------------------------------------------
2267
941830cb 2268void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 2269{
0659e7ee
RR
2270 wxPaintDC dc(this);
2271 PrepareDC( dc );
29d87bba 2272
941830cb
JS
2273 if ( !m_anchor)
2274 return;
2275
eff869aa 2276 dc.SetFont( m_normalFont );
0659e7ee 2277 dc.SetPen( m_dottedPen );
f38374d0 2278
eff869aa 2279 // this is now done dynamically
91b8de8d
RR
2280 //if(GetImageList() == NULL)
2281 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2282
91b8de8d 2283 int y = 2;
0659e7ee 2284 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 2285}
c801d85f 2286
b771aa29 2287void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
c801d85f 2288{
0659e7ee 2289 m_hasFocus = TRUE;
978f38c2 2290
b771aa29
VZ
2291 RefreshSelected();
2292
2293 event.Skip();
edaa81ae 2294}
c801d85f 2295
b771aa29 2296void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
c801d85f 2297{
0659e7ee 2298 m_hasFocus = FALSE;
978f38c2 2299
b771aa29
VZ
2300 RefreshSelected();
2301
2302 event.Skip();
edaa81ae 2303}
c801d85f 2304
941830cb 2305void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
c801d85f 2306{
978f38c2 2307 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
b09bda68 2308 te.m_evtKey = event;
978f38c2 2309 te.SetEventObject( this );
68eb5f63
VZ
2310 if ( GetEventHandler()->ProcessEvent( te ) )
2311 {
2312 // intercepted by the user code
2313 return;
2314 }
435fe83e 2315
91b8de8d 2316 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
2317 {
2318 event.Skip();
2319 return;
2320 }
ef44a621 2321
06b466c7
VZ
2322 // how should the selection work for this event?
2323 bool is_multiple, extended_select, unselect_others;
2324 EventFlagsToSelType(GetWindowStyleFlag(),
2325 event.ShiftDown(),
2326 event.ControlDown(),
dfc6cd93
SB
2327 is_multiple, extended_select, unselect_others);
2328
2329 // + : Expand
2330 // - : Collaspe
f6bcfd97 2331 // * : Expand all/Collapse all
dfc6cd93
SB
2332 // ' ' | return : activate
2333 // up : go up (not last children!)
2334 // down : go down
2335 // left : go to parent
2336 // right : open if parent and go next
2337 // home : go to root
2338 // end : go to last item without opening parents
978f38c2
VZ
2339 switch (event.KeyCode())
2340 {
2341 case '+':
2342 case WXK_ADD:
2343 if (m_current->HasPlus() && !IsExpanded(m_current))
2344 {
2345 Expand(m_current);
2346 }
2347 break;
ef44a621 2348
f6bcfd97
BP
2349 case '*':
2350 case WXK_MULTIPLY:
2351 if ( !IsExpanded(m_current) )
2352 {
2353 // expand all
2354 ExpandAll(m_current);
2355 break;
2356 }
2357 //else: fall through to Collapse() it
2358
978f38c2
VZ
2359 case '-':
2360 case WXK_SUBTRACT:
2361 if (IsExpanded(m_current))
2362 {
2363 Collapse(m_current);
2364 }
2365 break;
ef44a621 2366
978f38c2
VZ
2367 case ' ':
2368 case WXK_RETURN:
2369 {
2370 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
40fab78b 2371 event.m_item = (long) m_current;
978f38c2
VZ
2372 event.SetEventObject( this );
2373 GetEventHandler()->ProcessEvent( event );
2374 }
2375 break;
ef44a621 2376
618a5e38
RR
2377 // up goes to the previous sibling or to the last
2378 // of its children if it's expanded
978f38c2
VZ
2379 case WXK_UP:
2380 {
91b8de8d 2381 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
2382 if (!prev)
2383 {
91b8de8d 2384 prev = GetParent( m_key_current );
618a5e38
RR
2385 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2386 {
2387 break; // don't go to root if it is hidden
2388 }
c193b707
VZ
2389 if (prev)
2390 {
618a5e38 2391 long cookie = 0;
91b8de8d 2392 wxTreeItemId current = m_key_current;
618a5e38
RR
2393 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2394 if (current == GetFirstChild( prev, cookie ))
90e58684
RR
2395 {
2396 // otherwise we return to where we came from
88ac883a 2397 SelectItem( prev, unselect_others, extended_select );
941830cb 2398 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
c193b707 2399 EnsureVisible( prev );
90e58684 2400 break;
c193b707 2401 }
978f38c2
VZ
2402 }
2403 }
2404 if (prev)
2405 {
69a282d4 2406 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 2407 {
69a282d4
VZ
2408 wxTreeItemId child = GetLastChild(prev);
2409 if ( child )
2410 {
2411 prev = child;
2412 }
978f38c2 2413 }
69a282d4 2414
88ac883a 2415 SelectItem( prev, unselect_others, extended_select );
941830cb 2416 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
2417 EnsureVisible( prev );
2418 }
2419 }
2420 break;
ef44a621 2421
978f38c2
VZ
2422 // left arrow goes to the parent
2423 case WXK_LEFT:
2424 {
2425 wxTreeItemId prev = GetParent( m_current );
618a5e38
RR
2426 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2427 {
2428 // don't go to root if it is hidden
2429 prev = GetPrevSibling( m_current );
2430 }
978f38c2
VZ
2431 if (prev)
2432 {
2433 EnsureVisible( prev );
88ac883a 2434 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2435 }
2436 }
2437 break;
ef44a621 2438
978f38c2 2439 case WXK_RIGHT:
618a5e38
RR
2440 // this works the same as the down arrow except that we
2441 // also expand the item if it wasn't expanded yet
978f38c2
VZ
2442 Expand(m_current);
2443 // fall through
2444
2445 case WXK_DOWN:
ef44a621 2446 {
91b8de8d 2447 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
2448 {
2449 long cookie = 0;
91b8de8d 2450 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 2451 SelectItem( child, unselect_others, extended_select );
941830cb 2452 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
2453 EnsureVisible( child );
2454 }
2455 else
2456 {
91b8de8d 2457 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2458 if (!next)
978f38c2 2459 {
91b8de8d 2460 wxTreeItemId current = m_key_current;
978f38c2
VZ
2461 while (current && !next)
2462 {
2463 current = GetParent( current );
2464 if (current) next = GetNextSibling( current );
2465 }
2466 }
b62c3631 2467 if (next)
978f38c2 2468 {
88ac883a 2469 SelectItem( next, unselect_others, extended_select );
941830cb 2470 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
2471 EnsureVisible( next );
2472 }
2473 }
ef44a621 2474 }
978f38c2 2475 break;
ef44a621 2476
978f38c2
VZ
2477 // <End> selects the last visible tree item
2478 case WXK_END:
2479 {
2480 wxTreeItemId last = GetRootItem();
2481
2482 while ( last.IsOk() && IsExpanded(last) )
2483 {
2484 wxTreeItemId lastChild = GetLastChild(last);
2485
2486 // it may happen if the item was expanded but then all of
2487 // its children have been deleted - so IsExpanded() returned
2488 // TRUE, but GetLastChild() returned invalid item
2489 if ( !lastChild )
2490 break;
2491
2492 last = lastChild;
2493 }
2494
2495 if ( last.IsOk() )
2496 {
2497 EnsureVisible( last );
88ac883a 2498 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2499 }
2500 }
2501 break;
2502
2503 // <Home> selects the root item
2504 case WXK_HOME:
2505 {
2506 wxTreeItemId prev = GetRootItem();
618a5e38
RR
2507 if (!prev) break;
2508 if (HasFlag(wxTR_HIDE_ROOT))
978f38c2 2509 {
618a5e38
RR
2510 long dummy;
2511 prev = GetFirstChild(prev, dummy);
2512 if (!prev) break;
978f38c2 2513 }
618a5e38
RR
2514 EnsureVisible( prev );
2515 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2516 }
2517 break;
2518
2519 default:
2520 event.Skip();
2521 }
edaa81ae 2522}
c801d85f 2523
941830cb 2524wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2525{
618a5e38
RR
2526 // JACS: removed wxYieldIfNeeded() because it can cause the window
2527 // to be deleted from under us if a close window event is pending
dc6c62a9 2528
91b8de8d
RR
2529 int w, h;
2530 GetSize(&w, &h);
91b8de8d 2531 flags=0;
618a5e38
RR
2532 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2533 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2534 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2535 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2536 if (flags) return wxTreeItemId();
d3a9f4af 2537
618a5e38
RR
2538 if (m_anchor == NULL)
2539 {
2540 flags = wxTREE_HITTEST_NOWHERE;
2541 return wxTreeItemId();
2542 }
5716a1ab 2543
618a5e38
RR
2544 wxClientDC dc(this);
2545 PrepareDC(dc);
2546 wxCoord x = dc.DeviceToLogicalX( point.x );
2547 wxCoord y = dc.DeviceToLogicalY( point.y );
2548 wxGenericTreeItem *hit = m_anchor->HitTest(wxPoint(x, y),
2549 this,
2550 flags,
2551 0 );
2552 if (hit == NULL)
2553 {
2554 flags = wxTREE_HITTEST_NOWHERE;
2555 return wxTreeItemId();
2556 }
2557 return hit;
4f22cf8d
RR
2558}
2559
8fb3bfe2
JS
2560// get the bounding rectangle of the item (or of its label only)
2561bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2562 wxRect& rect,
33ac7e6f 2563 bool WXUNUSED(textOnly)) const
8fb3bfe2 2564{
601c163b 2565 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
8fb3bfe2
JS
2566
2567 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2568
2569 int startX, startY;
2570 GetViewStart(& startX, & startY);
2571
1ed01484 2572 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
c22886bd 2573 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
1ed01484 2574 rect.width = i->GetWidth();
c22886bd
RR
2575 //rect.height = i->GetHeight();
2576 rect.height = GetLineHeight(i);
8fb3bfe2
JS
2577
2578 return TRUE;
8fb3bfe2
JS
2579}
2580
e179bd65
RR
2581/* **** */
2582
941830cb 2583void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
e179bd65
RR
2584{
2585 if (!item.IsOk()) return;
2586
941830cb 2587 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
9dfbf520 2588
e179bd65 2589 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
40fab78b 2590 te.m_item = (long) m_currentEdit;
e179bd65
RR
2591 te.SetEventObject( this );
2592 GetEventHandler()->ProcessEvent( te );
2593
2594 if (!te.IsAllowed()) return;
8dc99046 2595
dc6c62a9
RR
2596 // We have to call this here because the label in
2597 // question might just have been added and no screen
2598 // update taken place.
cd66f45b 2599 if (m_dirty) wxYieldIfNeeded();
9dfbf520 2600
e179bd65
RR
2601 wxString s = m_currentEdit->GetText();
2602 int x = m_currentEdit->GetX();
2603 int y = m_currentEdit->GetY();
2604 int w = m_currentEdit->GetWidth();
2605 int h = m_currentEdit->GetHeight();
9dfbf520 2606
5f1ea0ee
RR
2607 int image_h = 0;
2608 int image_w = 0;
8dc99046
VZ
2609
2610 int image = m_currentEdit->GetCurrentImage();
2611 if ( image != NO_IMAGE )
5f1ea0ee 2612 {
2c8e4738
VZ
2613 if ( m_imageListNormal )
2614 {
2615 m_imageListNormal->GetSize( image, image_w, image_h );
2616 image_w += 4;
2617 }
6359fa0e 2618 else
2c8e4738
VZ
2619 {
2620 wxFAIL_MSG(_T("you must create an image list to use images!"));
2621 }
5f1ea0ee
RR
2622 }
2623 x += image_w;
2624 w -= image_w + 4; // I don't know why +4 is needed
e179bd65
RR
2625
2626 wxClientDC dc(this);
2627 PrepareDC( dc );
2628 x = dc.LogicalToDeviceX( x );
2629 y = dc.LogicalToDeviceY( y );
2630
6359fa0e
VZ
2631 wxTreeTextCtrl *text = new wxTreeTextCtrl(this, -1,
2632 &m_renameAccept,
2633 &m_renameRes,
2634 this,
2635 s,
2636 wxPoint(x-4,y-4),
2637 wxSize(w+11,h+8));
e179bd65
RR
2638 text->SetFocus();
2639}
2640
941830cb 2641void wxGenericTreeCtrl::OnRenameTimer()
e179bd65
RR
2642{
2643 Edit( m_current );
2644}
2645
941830cb 2646void wxGenericTreeCtrl::OnRenameAccept()
e179bd65 2647{
618a5e38 2648 // TODO if the validator fails this causes a crash
e179bd65 2649 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
40fab78b 2650 le.m_item = (long) m_currentEdit;
e179bd65
RR
2651 le.SetEventObject( this );
2652 le.m_label = m_renameRes;
2653 GetEventHandler()->ProcessEvent( le );
9dfbf520 2654
e179bd65 2655 if (!le.IsAllowed()) return;
9dfbf520 2656
5f1ea0ee 2657 SetItemText( m_currentEdit, m_renameRes );
e179bd65 2658}
9dfbf520 2659
941830cb 2660void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2661{
bbe0af5b 2662 if ( !m_anchor ) return;
978f38c2 2663
3dbeaa52
VZ
2664 // we process left mouse up event (enables in-place edit), right down
2665 // (pass to the user code), left dbl click (activate item) and
2666 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
2667 if ( !(event.LeftDown() ||
2668 event.LeftUp() ||
3dbeaa52
VZ
2669 event.RightDown() ||
2670 event.LeftDClick() ||
2671 event.Dragging() ||
2672 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2673 {
2674 event.Skip();
2675
2676 return;
2677 }
2678
bbe0af5b
RR
2679 wxClientDC dc(this);
2680 PrepareDC(dc);
06b466c7
VZ
2681 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2682 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
29d87bba 2683
3dbeaa52 2684 int flags = 0;
618a5e38
RR
2685 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y),
2686 this,
2687 flags,
2688 0 );
3dbeaa52 2689
3dbeaa52 2690 if ( event.Dragging() && !m_isDragging )
bbe0af5b 2691 {
fd9811b1 2692 if (m_dragCount == 0)
8dc99046
VZ
2693 m_dragStart = wxPoint(x,y);
2694
fd9811b1 2695 m_dragCount++;
8dc99046 2696
3dbeaa52
VZ
2697 if (m_dragCount != 3)
2698 {
2699 // wait until user drags a bit further...
2700 return;
2701 }
8dc99046 2702
3dbeaa52
VZ
2703 wxEventType command = event.RightIsDown()
2704 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2705 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 2706
fd9811b1 2707 wxTreeEvent nevent( command, GetId() );
40fab78b 2708 nevent.m_item = (long) m_current;
fd9811b1 2709 nevent.SetEventObject(this);
3dbeaa52
VZ
2710
2711 // by default the dragging is not supported, the user code must
2712 // explicitly allow the event for it to take place
2713 nevent.Veto();
2714
2715 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2716 {
2717 // we're going to drag this item
2718 m_isDragging = TRUE;
2719
b3a7510d
VZ
2720 // remember the old cursor because we will change it while
2721 // dragging
2722 m_oldCursor = m_cursor;
2723
2724 // in a single selection control, hide the selection temporarily
2725 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2726 {
941830cb 2727 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
2728
2729 if ( m_oldSelection )
2730 {
2731 m_oldSelection->SetHilight(FALSE);
2732 RefreshLine(m_oldSelection);
2733 }
2734 }
2735
3dbeaa52
VZ
2736 CaptureMouse();
2737 }
bbe0af5b 2738 }
3dbeaa52 2739 else if ( event.Moving() )
fd9811b1 2740 {
3dbeaa52
VZ
2741 if ( item != m_dropTarget )
2742 {
2743 // unhighlight the previous drop target
2744 DrawDropEffect(m_dropTarget);
fd9811b1 2745
3dbeaa52 2746 m_dropTarget = item;
978f38c2 2747
3dbeaa52
VZ
2748 // highlight the current drop target if any
2749 DrawDropEffect(m_dropTarget);
fb882e1c 2750
cd66f45b 2751 wxYieldIfNeeded();
3dbeaa52 2752 }
e179bd65 2753 }
3dbeaa52
VZ
2754 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2755 {
2756 // erase the highlighting
2757 DrawDropEffect(m_dropTarget);
9dfbf520 2758
4f5fffcc
RR
2759 if ( m_oldSelection )
2760 {
2761 m_oldSelection->SetHilight(TRUE);
2762 RefreshLine(m_oldSelection);
2763 m_oldSelection = (wxGenericTreeItem *)NULL;
2764 }
2765
3dbeaa52
VZ
2766 // generate the drag end event
2767 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 2768
40fab78b 2769 event.m_item = (long) item;
3dbeaa52
VZ
2770 event.m_pointDrag = wxPoint(x, y);
2771 event.SetEventObject(this);
91b8de8d 2772
3dbeaa52 2773 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 2774
3dbeaa52
VZ
2775 m_isDragging = FALSE;
2776 m_dropTarget = (wxGenericTreeItem *)NULL;
2777
2778 ReleaseMouse();
2779
b3a7510d 2780 SetCursor(m_oldCursor);
3dbeaa52 2781
cd66f45b 2782 wxYieldIfNeeded();
3dbeaa52
VZ
2783 }
2784 else
bbe0af5b 2785 {
3dbeaa52
VZ
2786 // here we process only the messages which happen on tree items
2787
2788 m_dragCount = 0;
2789
2790 if (item == NULL) return; /* we hit the blank area */
2791
2792 if ( event.RightDown() )
2793 {
2794 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
40fab78b 2795 nevent.m_item = (long) item;
05a7f61d
VZ
2796 CalcScrolledPosition(x, y,
2797 &nevent.m_pointDrag.x,
2798 &nevent.m_pointDrag.y);
3dbeaa52
VZ
2799 nevent.SetEventObject(this);
2800 GetEventHandler()->ProcessEvent(nevent);
2801 }
80d2803f 2802 else if ( event.LeftUp() )
f6bcfd97 2803 {
80d2803f 2804 if ( m_lastOnSame )
f6bcfd97 2805 {
80d2803f
VZ
2806 if ( (item == m_current) &&
2807 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2808 HasFlag(wxTR_EDIT_LABELS) )
2809 {
bdb310a7
VZ
2810 if ( m_renameTimer->IsRunning() )
2811 m_renameTimer->Stop();
2812
80d2803f
VZ
2813 m_renameTimer->Start( 100, TRUE );
2814 }
f6bcfd97 2815
80d2803f
VZ
2816 m_lastOnSame = FALSE;
2817 }
3dbeaa52 2818 }
0326c494 2819 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3dbeaa52 2820 {
f6bcfd97
BP
2821 if ( event.LeftDown() )
2822 {
2823 m_lastOnSame = item == m_current;
2824 }
2825
0326c494
VZ
2826 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
2827 {
2828 // only toggle the item for a single click, double click on
2829 // the button doesn't do anything (it toggles the item twice)
2830 if ( event.LeftDown() )
2831 {
2832 Toggle( item );
2833 }
2834
2835 // don't select the item if the button was clicked
2836 return;
2837 }
2838
3dbeaa52
VZ
2839 // how should the selection work for this event?
2840 bool is_multiple, extended_select, unselect_others;
2841 EventFlagsToSelType(GetWindowStyleFlag(),
2842 event.ShiftDown(),
2843 event.ControlDown(),
dfc6cd93 2844 is_multiple, extended_select, unselect_others);
3dbeaa52 2845
3dbeaa52
VZ
2846 SelectItem(item, unselect_others, extended_select);
2847
618a5e38
RR
2848 // For some reason, Windows isn't recognizing a left double-click,
2849 // so we need to simulate it here. Allow 200 milliseconds for now.
3dbeaa52
VZ
2850 if ( event.LeftDClick() )
2851 {
f6bcfd97
BP
2852 // double clicking should not start editing the item label
2853 m_renameTimer->Stop();
2854 m_lastOnSame = FALSE;
2855
ebb987b7
VZ
2856 // send activate event first
2857 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2858 nevent.m_item = (long) item;
ebb987b7
VZ
2859 CalcScrolledPosition(x, y,
2860 &nevent.m_pointDrag.x,
2861 &nevent.m_pointDrag.y);
2862 nevent.SetEventObject( this );
2863 if ( !GetEventHandler()->ProcessEvent( nevent ) )
618a5e38 2864 {
ebb987b7
VZ
2865 // if the user code didn't process the activate event,
2866 // handle it ourselves by toggling the item when it is
2867 // double clicked
2868 if ( item->HasPlus() )
2869 {
2870 Toggle(item);
2871 }
618a5e38 2872 }
3dbeaa52
VZ
2873 }
2874 }
bbe0af5b 2875 }
edaa81ae 2876}
c801d85f 2877
941830cb 2878void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
3db7be80 2879{
bbe0af5b
RR
2880 /* after all changes have been done to the tree control,
2881 * we actually redraw the tree when everything is over */
ef44a621 2882
618a5e38 2883 if (!m_dirty) return;
ef44a621 2884
bbe0af5b 2885 m_dirty = FALSE;
3db7be80 2886
bbe0af5b 2887 CalculatePositions();
91b8de8d 2888 Refresh();
bbe0af5b 2889 AdjustMyScrollbars();
3db7be80
RR
2890}
2891
941830cb 2892void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 2893{
e06b9569
JS
2894 wxCoord text_w = 0;
2895 wxCoord text_h = 0;
9dfbf520 2896
a62867a5 2897 if (item->IsBold())
eff869aa 2898 dc.SetFont(m_boldFont);
9dfbf520 2899
91b8de8d 2900 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 2901 text_h+=2;
91b8de8d 2902
eff869aa
RR
2903 // restore normal font
2904 dc.SetFont( m_normalFont );
9dfbf520 2905
91b8de8d
RR
2906 int image_h = 0;
2907 int image_w = 0;
8dc99046
VZ
2908 int image = item->GetCurrentImage();
2909 if ( image != NO_IMAGE )
91b8de8d 2910 {
2c8e4738
VZ
2911 if ( m_imageListNormal )
2912 {
2913 m_imageListNormal->GetSize( image, image_w, image_h );
2914 image_w += 4;
2915 }
91b8de8d
RR
2916 }
2917
2918 int total_h = (image_h > text_h) ? image_h : text_h;
2919
618a5e38 2920 if (total_h < 30)
f2593d0d 2921 total_h += 2; // at least 2 pixels
06b466c7 2922 else
f2593d0d 2923 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
2924
2925 item->SetHeight(total_h);
6cedba09
VZ
2926 if (total_h>m_lineHeight)
2927 m_lineHeight=total_h;
bbe0af5b 2928
91b8de8d
RR
2929 item->SetWidth(image_w+text_w+2);
2930}
2931
2932// -----------------------------------------------------------------------------
2933// for developper : y is now the top of the level
2934// not the middle of it !
941830cb 2935void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2936{
618a5e38
RR
2937 int x = level*m_indent;
2938 if (!HasFlag(wxTR_HIDE_ROOT))
2939 {
2940 x += m_indent;
2941 }
2942 else if (level == 0)
2943 {
2944 // a hidden root is not evaluated, but its
2945 // children are always calculated
2946 goto Recurse;
2947 }
389cdc7a 2948
91b8de8d 2949 CalculateSize( item, dc );
d3a9f4af 2950
91b8de8d 2951 // set its position
618a5e38 2952 item->SetX( x+m_spacing );
91b8de8d 2953 item->SetY( y );
618a5e38 2954 y += GetLineHeight(item);
4c681997 2955
bbe0af5b
RR
2956 if ( !item->IsExpanded() )
2957 {
618a5e38 2958 // we don't need to calculate collapsed branches
bbe0af5b
RR
2959 return;
2960 }
389cdc7a 2961
618a5e38 2962 Recurse:
91b8de8d
RR
2963 wxArrayGenericTreeItems& children = item->GetChildren();
2964 size_t n, count = children.Count();
618a5e38 2965 ++level;
91b8de8d 2966 for (n = 0; n < count; ++n )
618a5e38 2967 CalculateLevel( children[n], dc, level, y ); // recurse
edaa81ae 2968}
c801d85f 2969
941830cb 2970void wxGenericTreeCtrl::CalculatePositions()
c801d85f 2971{
bbe0af5b 2972 if ( !m_anchor ) return;
29d87bba 2973
bbe0af5b
RR
2974 wxClientDC dc(this);
2975 PrepareDC( dc );
29d87bba 2976
eff869aa 2977 dc.SetFont( m_normalFont );
29d87bba 2978
bbe0af5b 2979 dc.SetPen( m_dottedPen );
91b8de8d
RR
2980 //if(GetImageList() == NULL)
2981 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2982
8dc99046 2983 int y = 2;
f65635b5 2984 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 2985}
c801d85f 2986
941830cb 2987void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 2988{
e6527f9d
RR
2989 if (m_dirty) return;
2990
bbe0af5b
RR
2991 wxClientDC dc(this);
2992 PrepareDC(dc);
4832f7c0 2993
bbe0af5b
RR
2994 int cw = 0;
2995 int ch = 0;
2996 GetClientSize( &cw, &ch );
4832f7c0 2997
bbe0af5b
RR
2998 wxRect rect;
2999 rect.x = dc.LogicalToDeviceX( 0 );
3000 rect.width = cw;
3001 rect.y = dc.LogicalToDeviceY( item->GetY() );
3002 rect.height = ch;
f135ff73 3003
bbe0af5b 3004 Refresh( TRUE, &rect );
f135ff73 3005
bbe0af5b 3006 AdjustMyScrollbars();
edaa81ae 3007}
c801d85f 3008
941830cb 3009void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 3010{
e6527f9d
RR
3011 if (m_dirty) return;
3012
bbe0af5b
RR
3013 wxClientDC dc(this);
3014 PrepareDC( dc );
3015
5391f772
SB
3016 int cw = 0;
3017 int ch = 0;
3018 GetClientSize( &cw, &ch );
3019
bbe0af5b 3020 wxRect rect;
5391f772
SB
3021 rect.x = dc.LogicalToDeviceX( 0 );
3022 rect.y = dc.LogicalToDeviceY( item->GetY() );
3023 rect.width = cw;
91b8de8d 3024 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 3025
bbe0af5b 3026 Refresh( TRUE, &rect );
edaa81ae 3027}
c801d85f 3028
b771aa29
VZ
3029void wxGenericTreeCtrl::RefreshSelected()
3030{
3031 // TODO: this is awfully inefficient, we should keep the list of all
3032 // selected items internally, should be much faster
3033 if ( m_anchor )
3034 RefreshSelectedUnder(m_anchor);
3035}
3036
3037void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3038{
3039 if ( item->IsSelected() )
3040 RefreshLine(item);
3041
3042 const wxArrayGenericTreeItems& children = item->GetChildren();
3043 size_t count = children.GetCount();
3044 for ( size_t n = 0; n < count; n++ )
3045 {
3046 RefreshSelectedUnder(children[n]);
3047 }
3048}
3049
7009f411
VZ
3050// ----------------------------------------------------------------------------
3051// changing colours: we need to refresh the tree control
3052// ----------------------------------------------------------------------------
3053
3054bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3055{
3056 if ( !wxWindow::SetBackgroundColour(colour) )
3057 return FALSE;
3058
3059 Refresh();
3060
3061 return TRUE;
3062}
3063
0800a4ba 3064bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
7009f411
VZ
3065{
3066 if ( !wxWindow::SetForegroundColour(colour) )
3067 return FALSE;
3068
3069 Refresh();
3070
3071 return TRUE;
3072}
3073
1e6feb95 3074#endif // wxUSE_TREECTRL