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