]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
fixed a compilation warning about signed/unsigned comparison
[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{
374a8e5c
VS
838 if (!HasFlag(wxTR_HIDE_ROOT) && (styles & wxTR_HIDE_ROOT))
839 {
840 // if we will hide the root, make sure children are visible
841 m_anchor->SetHasPlus();
842 m_anchor->Expand();
843 CalculatePositions();
844 }
845
846 // right now, just sets the styles. Eventually, we may
847 // want to update the inherited styles, but right now
848 // none of the parents has updatable styles
618a5e38
RR
849 m_windowStyle = styles;
850 m_dirty = TRUE;
851}
852
f135ff73
VZ
853// -----------------------------------------------------------------------------
854// functions to work with tree items
855// -----------------------------------------------------------------------------
74bedbeb 856
941830cb 857wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
f135ff73 858{
f2593d0d 859 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
4832f7c0 860
941830cb 861 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
edaa81ae 862}
74bedbeb 863
941830cb 864int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
8dc99046 865 wxTreeItemIcon which) const
74bedbeb 866{
f2593d0d 867 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 868
941830cb 869 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
edaa81ae 870}
c801d85f 871
941830cb 872wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 873{
f2593d0d 874 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 875
941830cb 876 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
edaa81ae 877}
c801d85f 878
941830cb 879void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
f135ff73 880{
f2593d0d 881 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 882
f2593d0d 883 wxClientDC dc(this);
941830cb 884 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
885 pItem->SetText(text);
886 CalculateSize(pItem, dc);
887 RefreshLine(pItem);
f135ff73 888}
c801d85f 889
941830cb 890void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
8dc99046
VZ
891 int image,
892 wxTreeItemIcon which)
f135ff73 893{
223d09f6 894 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 895
941830cb 896 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
8dc99046 897 pItem->SetImage(image, which);
4832f7c0 898
8dc99046
VZ
899 wxClientDC dc(this);
900 CalculateSize(pItem, dc);
901 RefreshLine(pItem);
edaa81ae 902}
c801d85f 903
941830cb 904void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 905{
f2593d0d 906 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 907
941830cb 908 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
edaa81ae 909}
c801d85f 910
941830cb 911void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 912{
f2593d0d 913 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 914
941830cb 915 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
916 pItem->SetHasPlus(has);
917 RefreshLine(pItem);
edaa81ae 918}
c801d85f 919
941830cb 920void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
ef44a621 921{
9ec64fa7 922 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 923
9ec64fa7 924 // avoid redrawing the tree if no real change
941830cb 925 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
926 if ( pItem->IsBold() != bold )
927 {
928 pItem->SetBold(bold);
929 RefreshLine(pItem);
930 }
931}
932
941830cb 933void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
9ec64fa7
VZ
934 const wxColour& col)
935{
936 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
937
941830cb 938 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
939 pItem->Attr().SetTextColour(col);
940 RefreshLine(pItem);
941}
942
941830cb 943void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
9ec64fa7
VZ
944 const wxColour& col)
945{
946 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
947
941830cb 948 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
949 pItem->Attr().SetBackgroundColour(col);
950 RefreshLine(pItem);
951}
952
941830cb 953void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
9ec64fa7
VZ
954{
955 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
956
941830cb 957 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7 958 pItem->Attr().SetFont(font);
ef44a621 959 RefreshLine(pItem);
ef44a621
VZ
960}
961
3da2715f
JS
962bool wxGenericTreeCtrl::SetFont( const wxFont &font )
963{
964 wxScrolledWindow::SetFont(font);
965
966 m_normalFont = font ;
967 m_boldFont = wxFont( m_normalFont.GetPointSize(),
968 m_normalFont.GetFamily(),
969 m_normalFont.GetStyle(),
970 wxBOLD,
971 m_normalFont.GetUnderlined());
972
973 return TRUE;
974}
975
976
f135ff73
VZ
977// -----------------------------------------------------------------------------
978// item status inquiries
979// -----------------------------------------------------------------------------
980
1ed01484 981bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
c801d85f 982{
1ed01484
JS
983 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
984
c22886bd 985 // An item is only visible if it's not a descendant of a collapsed item
1ed01484 986 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
c22886bd
RR
987 wxGenericTreeItem* parent = pItem->GetParent();
988 while (parent)
989 {
990 if (!parent->IsExpanded())
991 return FALSE;
992 parent = parent->GetParent();
993 }
1ed01484
JS
994
995 int startX, startY;
996 GetViewStart(& startX, & startY);
997
998 wxSize clientSize = GetClientSize();
999
1000 wxRect rect;
1001 if (!GetBoundingRect(item, rect))
1002 return FALSE;
c22886bd
RR
1003 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
1004 return FALSE;
1ed01484
JS
1005 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
1006 return FALSE;
1007 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
1008 return FALSE;
f135ff73 1009
f2593d0d 1010 return TRUE;
edaa81ae 1011}
c801d85f 1012
941830cb 1013bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 1014{
f2593d0d 1015 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 1016
59a2e635
VZ
1017 // consider that the item does have children if it has the "+" button: it
1018 // might not have them (if it had never been expanded yet) but then it
1019 // could have them as well and it's better to err on this side rather than
1020 // disabling some operations which are restricted to the items with
1021 // children for an item which does have them
607d9cfc 1022 return ((wxGenericTreeItem*) item.m_pItem)->HasPlus();
edaa81ae 1023}
c801d85f 1024
941830cb 1025bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 1026{
f2593d0d 1027 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 1028
941830cb 1029 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
f135ff73 1030}
29d87bba 1031
941830cb 1032bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
f135ff73 1033{
f2593d0d 1034 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 1035
941830cb 1036 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
f135ff73 1037}
29d87bba 1038
941830cb 1039bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
ef44a621 1040{
f2593d0d 1041 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
ef44a621 1042
941830cb 1043 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
ef44a621
VZ
1044}
1045
f135ff73
VZ
1046// -----------------------------------------------------------------------------
1047// navigation
1048// -----------------------------------------------------------------------------
29d87bba 1049
941830cb 1050wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const
f135ff73 1051{
618a5e38 1052 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 1053
618a5e38 1054 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
f135ff73 1055}
29d87bba 1056
941830cb 1057wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
f135ff73 1058{
618a5e38 1059 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1060
618a5e38
RR
1061 cookie = 0;
1062 return GetNextChild(item, cookie);
f135ff73 1063}
29d87bba 1064
941830cb 1065wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
f135ff73 1066{
618a5e38 1067 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1068
618a5e38
RR
1069 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1070 if ( (size_t)cookie < children.Count() )
1071 {
1072 return children.Item((size_t)cookie++);
1073 }
1074 else
1075 {
1076 // there are no more of them
1077 return wxTreeItemId();
1078 }
f135ff73 1079}
29d87bba 1080
941830cb 1081wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
978f38c2 1082{
618a5e38 1083 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 1084
618a5e38
RR
1085 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1086 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
1087}
1088
941830cb 1089wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
f135ff73 1090{
618a5e38 1091 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1092
618a5e38
RR
1093 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1094 wxGenericTreeItem *parent = i->GetParent();
1095 if ( parent == NULL )
1096 {
1097 // root item doesn't have any siblings
1098 return wxTreeItemId();
1099 }
4832f7c0 1100
618a5e38
RR
1101 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1102 int index = siblings.Index(i);
1103 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1104
618a5e38
RR
1105 size_t n = (size_t)(index + 1);
1106 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 1107}
c801d85f 1108
941830cb 1109wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 1110{
618a5e38 1111 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1112
618a5e38
RR
1113 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1114 wxGenericTreeItem *parent = i->GetParent();
1115 if ( parent == NULL )
1116 {
1117 // root item doesn't have any siblings
1118 return wxTreeItemId();
1119 }
4832f7c0 1120
618a5e38
RR
1121 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1122 int index = siblings.Index(i);
1123 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1124
618a5e38
RR
1125 return index == 0 ? wxTreeItemId()
1126 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 1127}
389cdc7a 1128
1ed01484
JS
1129// Only for internal use right now, but should probably be public
1130wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
f135ff73 1131{
618a5e38
RR
1132 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1133
1134 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1135
1136 // First see if there are any children.
1137 wxArrayGenericTreeItems& children = i->GetChildren();
1138 if (children.GetCount() > 0)
1139 {
1140 return children.Item(0);
1141 }
1142 else
1143 {
1144 // Try a sibling of this or ancestor instead
1145 wxTreeItemId p = item;
1146 wxTreeItemId toFind;
1147 do
1148 {
1149 toFind = GetNextSibling(p);
1150 p = GetParent(p);
1151 } while (p.IsOk() && !toFind.IsOk());
1152 return toFind;
1153 }
1ed01484
JS
1154}
1155
1ed01484
JS
1156wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1157{
618a5e38
RR
1158 wxTreeItemId id = GetRootItem();
1159 if (!id.IsOk())
1ed01484 1160 return id;
1ed01484 1161
618a5e38
RR
1162 do
1163 {
1164 if (IsVisible(id))
1165 return id;
1166 id = GetNext(id);
1167 } while (id.IsOk());
1168
1169 return wxTreeItemId();
1ed01484
JS
1170}
1171
941830cb 1172wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
f135ff73 1173{
618a5e38 1174 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1175
618a5e38
RR
1176 wxTreeItemId id = item;
1177 if (id.IsOk())
1178 {
1179 while (id = GetNext(id), id.IsOk())
1180 {
1181 if (IsVisible(id))
1182 return id;
1183 }
1184 }
1185 return wxTreeItemId();
f135ff73 1186}
29d87bba 1187
941830cb 1188wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
f135ff73 1189{
618a5e38 1190 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1191
618a5e38 1192 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1193
618a5e38 1194 return wxTreeItemId();
edaa81ae 1195}
c801d85f 1196
cb59313c
VZ
1197// find the first item starting with the given prefix after the given item
1198wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent,
1199 const wxString& prefixOrig) const
1200{
1201 // match is case insensitive as this is more convenient to the user: having
1202 // to press Shift-letter to go to the item starting with a capital letter
1203 // would be too bothersome
1204 wxString prefix = prefixOrig.Lower();
1205
526b8142
VZ
1206 // determine the starting point: we shouldn't take the current item (this
1207 // allows to switch between two items starting with the same letter just by
1208 // pressing it) but we shouldn't jump to the next one if the user is
1209 // continuing to type as otherwise he might easily skip the item he wanted
cb59313c 1210 wxTreeItemId id = idParent;
526b8142
VZ
1211 if ( prefix.length() == 1 )
1212 {
1213 id = GetNext(id);
1214 }
cb59313c 1215
526b8142 1216 // look for the item starting with the given prefix after it
cb59313c
VZ
1217 while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) )
1218 {
1219 id = GetNext(id);
1220 }
1221
526b8142
VZ
1222 // if we haven't found anything...
1223 if ( !id.IsOk() )
1224 {
1225 // ... wrap to the beginning
1226 id = GetRootItem();
1227 if ( HasFlag(wxTR_HIDE_ROOT) )
1228 {
1229 // can't select virtual root
1230 id = GetNext(id);
1231 }
1232
1233 // and try all the items (stop when we get to the one we started from)
1234 while ( id != idParent && !GetItemText(id).Lower().StartsWith(prefix) )
1235 {
1236 id = GetNext(id);
1237 }
1238 }
1239
cb59313c
VZ
1240 return id;
1241}
1242
f135ff73
VZ
1243// -----------------------------------------------------------------------------
1244// operations
1245// -----------------------------------------------------------------------------
1246
941830cb 1247wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1248 size_t previous,
1249 const wxString& text,
1250 int image, int selImage,
1251 wxTreeItemData *data)
c801d85f 1252{
941830cb 1253 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f49f2b0c
RR
1254 if ( !parent )
1255 {
1256 // should we give a warning here?
1257 return AddRoot(text, image, selImage, data);
1258 }
4832f7c0 1259
618a5e38
RR
1260 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1261
f38374d0 1262 wxGenericTreeItem *item =
2b84e565 1263 new wxGenericTreeItem( parent, text, image, selImage, data );
74bedbeb 1264
f49f2b0c
RR
1265 if ( data != NULL )
1266 {
40fab78b 1267 data->m_pItem = (long) item;
f49f2b0c 1268 }
74bedbeb 1269
f49f2b0c 1270 parent->Insert( item, previous );
ef44a621 1271
f49f2b0c 1272 return item;
4c681997
RR
1273}
1274
941830cb 1275wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
f135ff73
VZ
1276 int image, int selImage,
1277 wxTreeItemData *data)
4c681997 1278{
f49f2b0c 1279 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 1280
618a5e38
RR
1281 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1282
2b84e565 1283 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text,
f135ff73 1284 image, selImage, data);
cf31a1d7
VS
1285 if ( data != NULL )
1286 {
1287 data->m_pItem = (long) m_anchor;
1288 }
1289
618a5e38
RR
1290 if (HasFlag(wxTR_HIDE_ROOT))
1291 {
1292 // if root is hidden, make sure we can navigate
1293 // into children
1294 m_anchor->SetHasPlus();
999723f3
VS
1295 m_anchor->Expand();
1296 CalculatePositions();
618a5e38 1297 }
f38374d0 1298
f49f2b0c
RR
1299 if (!HasFlag(wxTR_MULTIPLE))
1300 {
1301 m_current = m_key_current = m_anchor;
06b466c7 1302 m_current->SetHilight( TRUE );
f49f2b0c 1303 }
389cdc7a 1304
f49f2b0c 1305 return m_anchor;
edaa81ae 1306}
c801d85f 1307
941830cb 1308wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
f135ff73
VZ
1309 const wxString& text,
1310 int image, int selImage,
1311 wxTreeItemData *data)
c801d85f 1312{
f2593d0d 1313 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 1314}
c801d85f 1315
941830cb 1316wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1317 const wxTreeItemId& idPrevious,
1318 const wxString& text,
1319 int image, int selImage,
1320 wxTreeItemData *data)
c801d85f 1321{
941830cb 1322 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1323 if ( !parent )
1324 {
1325 // should we give a warning here?
1326 return AddRoot(text, image, selImage, data);
1327 }
c801d85f 1328
4855a477
JS
1329 int index = -1;
1330 if (idPrevious.IsOk())
1331 {
1332 index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1333 wxASSERT_MSG( index != wxNOT_FOUND,
1334 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1335 }
06b466c7 1336
f2593d0d
RR
1337 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1338}
1339
941830cb 1340wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f2593d0d
RR
1341 size_t before,
1342 const wxString& text,
1343 int image, int selImage,
1344 wxTreeItemData *data)
1345{
941830cb 1346 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1347 if ( !parent )
1348 {
1349 // should we give a warning here?
1350 return AddRoot(text, image, selImage, data);
1351 }
1352
1353 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 1354}
c801d85f 1355
941830cb 1356wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
f135ff73
VZ
1357 const wxString& text,
1358 int image, int selImage,
1359 wxTreeItemData *data)
74bedbeb 1360{
941830cb 1361 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1362 if ( !parent )
1363 {
1364 // should we give a warning here?
1365 return AddRoot(text, image, selImage, data);
1366 }
f135ff73 1367
f2593d0d
RR
1368 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1369 image, selImage, data);
74bedbeb
VZ
1370}
1371
941830cb 1372void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
a43a4f9d 1373{
f2593d0d 1374 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
40fab78b 1375 event.m_item = (long) item;
f2593d0d
RR
1376 event.SetEventObject( this );
1377 ProcessEvent( event );
a43a4f9d
VZ
1378}
1379
941830cb 1380void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
372edb9d 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;
a43a4f9d 1385 item->DeleteChildren(this);
372edb9d
VZ
1386}
1387
941830cb 1388void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1389{
618a5e38
RR
1390 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1391
941830cb 1392 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
ff5bf259 1393
618a5e38
RR
1394 // don't stay with invalid m_key_current or we will crash in
1395 // the next call to OnChar()
aaa2f297
VZ
1396 bool changeKeyCurrent = FALSE;
1397 wxGenericTreeItem *itemKey = m_key_current;
618a5e38 1398 while ( itemKey )
aaa2f297
VZ
1399 {
1400 if ( itemKey == item )
1401 {
1402 // m_key_current is a descendant of the item being deleted
1403 changeKeyCurrent = TRUE;
618a5e38 1404 break;
aaa2f297 1405 }
618a5e38 1406 itemKey = itemKey->GetParent();
aaa2f297
VZ
1407 }
1408
1409 wxGenericTreeItem *parent = item->GetParent();
97d7bfb8
RR
1410 if ( parent )
1411 {
1412 parent->GetChildren().Remove( item ); // remove by value
1413 }
f135ff73 1414
aaa2f297
VZ
1415 if ( changeKeyCurrent )
1416 {
1417 // may be NULL or not
1418 m_key_current = parent;
1419 }
1420
97d7bfb8
RR
1421 item->DeleteChildren(this);
1422 SendDeleteEvent(item);
1423 delete item;
edaa81ae 1424}
c801d85f 1425
941830cb 1426void wxGenericTreeCtrl::DeleteAllItems()
c801d85f 1427{
97d7bfb8
RR
1428 if ( m_anchor )
1429 {
618a5e38
RR
1430 m_dirty = TRUE;
1431
97d7bfb8
RR
1432 m_anchor->DeleteChildren(this);
1433 delete m_anchor;
a43a4f9d 1434
97d7bfb8 1435 m_anchor = NULL;
97d7bfb8 1436 }
edaa81ae
RR
1437}
1438
941830cb 1439void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1440{
941830cb 1441 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1442
941830cb 1443 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
7a944d2f 1444 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
999723f3 1445 _T("can't expand hidden root") );
f6bcfd97 1446
f2593d0d
RR
1447 if ( !item->HasPlus() )
1448 return;
978f38c2 1449
f2593d0d
RR
1450 if ( item->IsExpanded() )
1451 return;
f135ff73 1452
f2593d0d 1453 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
40fab78b 1454 event.m_item = (long) item;
f2593d0d 1455 event.SetEventObject( this );
004fd0c8 1456
f2593d0d
RR
1457 if ( ProcessEvent( event ) && !event.IsAllowed() )
1458 {
1459 // cancelled by program
1460 return;
1461 }
4832f7c0 1462
f2593d0d
RR
1463 item->Expand();
1464 CalculatePositions();
f135ff73 1465
f2593d0d 1466 RefreshSubtree(item);
f135ff73 1467
f2593d0d
RR
1468 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1469 ProcessEvent( event );
edaa81ae
RR
1470}
1471
941830cb 1472void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
f6bcfd97
BP
1473{
1474 Expand(item);
1475 if ( IsExpanded(item) )
1476 {
1477 long cookie;
1478 wxTreeItemId child = GetFirstChild(item, cookie);
1479 while ( child.IsOk() )
1480 {
1481 ExpandAll(child);
1482
1483 child = GetNextChild(item, cookie);
1484 }
1485 }
1486}
1487
941830cb 1488void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1489{
7a944d2f 1490 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
999723f3
VS
1491 _T("can't collapse hidden root") );
1492
941830cb 1493 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1494
f2593d0d
RR
1495 if ( !item->IsExpanded() )
1496 return;
f135ff73 1497
f2593d0d 1498 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
40fab78b 1499 event.m_item = (long) item;
f2593d0d
RR
1500 event.SetEventObject( this );
1501 if ( ProcessEvent( event ) && !event.IsAllowed() )
1502 {
1503 // cancelled by program
1504 return;
1505 }
4832f7c0 1506
f2593d0d 1507 item->Collapse();
f135ff73 1508
618a5e38 1509#if 0 // TODO why should items be collapsed recursively?
f2593d0d
RR
1510 wxArrayGenericTreeItems& children = item->GetChildren();
1511 size_t count = children.Count();
1512 for ( size_t n = 0; n < count; n++ )
1513 {
1514 Collapse(children[n]);
1515 }
618a5e38 1516#endif
f135ff73 1517
f2593d0d 1518 CalculatePositions();
f135ff73 1519
f2593d0d 1520 RefreshSubtree(item);
f135ff73 1521
f2593d0d
RR
1522 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1523 ProcessEvent( event );
edaa81ae 1524}
c801d85f 1525
941830cb 1526void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1527{
00e12320
RR
1528 Collapse(item);
1529 DeleteChildren(item);
edaa81ae 1530}
c801d85f 1531
941830cb 1532void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1533{
941830cb 1534 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
389cdc7a 1535
00e12320
RR
1536 if (item->IsExpanded())
1537 Collapse(itemId);
1538 else
1539 Expand(itemId);
f135ff73 1540}
389cdc7a 1541
941830cb 1542void wxGenericTreeCtrl::Unselect()
f135ff73 1543{
00e12320
RR
1544 if (m_current)
1545 {
1546 m_current->SetHilight( FALSE );
1547 RefreshLine( m_current );
1548 }
edaa81ae 1549}
c801d85f 1550
941830cb 1551void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1552{
00e12320
RR
1553 if (item->IsSelected())
1554 {
1555 item->SetHilight(FALSE);
1556 RefreshLine(item);
1557 }
d3a9f4af 1558
00e12320 1559 if (item->HasChildren())
88ac883a 1560 {
00e12320
RR
1561 wxArrayGenericTreeItems& children = item->GetChildren();
1562 size_t count = children.Count();
1563 for ( size_t n = 0; n < count; ++n )
1564 {
1565 UnselectAllChildren(children[n]);
1566 }
88ac883a
VZ
1567 }
1568}
f135ff73 1569
941830cb 1570void wxGenericTreeCtrl::UnselectAll()
88ac883a 1571{
0a33446c
VZ
1572 wxTreeItemId rootItem = GetRootItem();
1573
1574 // the tree might not have the root item at all
1575 if ( rootItem )
1576 {
1577 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1578 }
88ac883a
VZ
1579}
1580
1581// Recursive function !
1582// To stop we must have crt_item<last_item
91b8de8d 1583// Algorithm :
88ac883a 1584// Tag all next children, when no more children,
d3a9f4af 1585// Move to parent (not to tag)
91b8de8d 1586// Keep going... if we found last_item, we stop.
941830cb 1587bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a
VZ
1588{
1589 wxGenericTreeItem *parent = crt_item->GetParent();
1590
00e12320
RR
1591 if (parent == NULL) // This is root item
1592 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1593
91b8de8d 1594 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1595 int index = children.Index(crt_item);
1596 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1597
1598 size_t count = children.Count();
1599 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1600 {
1601 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1602 }
88ac883a
VZ
1603
1604 return TagNextChildren(parent, last_item, select);
1605}
1606
941830cb 1607bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a 1608{
00e12320
RR
1609 crt_item->SetHilight(select);
1610 RefreshLine(crt_item);
d3a9f4af 1611
06b466c7 1612 if (crt_item==last_item)
00e12320 1613 return TRUE;
88ac883a 1614
00e12320 1615 if (crt_item->HasChildren())
88ac883a 1616 {
00e12320
RR
1617 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1618 size_t count = children.Count();
1619 for ( size_t n = 0; n < count; ++n )
1620 {
06b466c7 1621 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1622 return TRUE;
06b466c7 1623 }
88ac883a 1624 }
d3a9f4af 1625
c0de7af4 1626 return FALSE;
88ac883a
VZ
1627}
1628
941830cb 1629void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
88ac883a 1630{
f2593d0d
RR
1631 // item2 is not necessary after item1
1632 wxGenericTreeItem *first=NULL, *last=NULL;
88ac883a 1633
f2593d0d
RR
1634 // choice first' and 'last' between item1 and item2
1635 if (item1->GetY()<item2->GetY())
06b466c7 1636 {
f2593d0d
RR
1637 first=item1;
1638 last=item2;
1639 }
1640 else
1641 {
1642 first=item2;
1643 last=item1;
1644 }
88ac883a 1645
f2593d0d 1646 bool select = m_current->IsSelected();
d3a9f4af 1647
f2593d0d
RR
1648 if ( TagAllChildrenUntilLast(first,last,select) )
1649 return;
88ac883a 1650
f2593d0d 1651 TagNextChildren(first,last,select);
88ac883a
VZ
1652}
1653
941830cb 1654void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
cb59313c
VZ
1655 bool unselect_others,
1656 bool extended_select)
d3a9f4af 1657{
223d09f6 1658 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1659
88ac883a 1660 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
941830cb 1661 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
88ac883a
VZ
1662
1663 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1664 // wxT("this is a single selection tree") );
88ac883a
VZ
1665
1666 // to keep going anyhow !!!
d3a9f4af 1667 if (is_single)
8dc99046
VZ
1668 {
1669 if (item->IsSelected())
1670 return; // nothing to do
1671 unselect_others = TRUE;
1672 extended_select = FALSE;
1673 }
1674 else if ( unselect_others && item->IsSelected() )
1675 {
1676 // selection change if there is more than one item currently selected
1677 wxArrayTreeItemIds selected_items;
1678 if ( GetSelections(selected_items) == 1 )
1679 return;
1680 }
d3a9f4af 1681
f135ff73 1682 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
40fab78b
JS
1683 event.m_item = (long) item;
1684 event.m_itemOld = (long) m_current;
f135ff73 1685 event.SetEventObject( this );
91b8de8d 1686 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1687
f98e2558 1688 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
618a5e38 1689 return;
f135ff73 1690
2cc78389
RR
1691 wxTreeItemId parent = GetParent( itemId );
1692 while (parent.IsOk())
1693 {
1694 if (!IsExpanded(parent))
1695 Expand( parent );
cdb3cffe 1696
2cc78389
RR
1697 parent = GetParent( parent );
1698 }
cdb3cffe 1699
2cc78389 1700 EnsureVisible( itemId );
cdb3cffe 1701
88ac883a
VZ
1702 // ctrl press
1703 if (unselect_others)
389cdc7a 1704 {
88ac883a 1705 if (is_single) Unselect(); // to speed up thing
c193b707 1706 else UnselectAll();
edaa81ae 1707 }
f135ff73 1708
88ac883a 1709 // shift press
d3a9f4af 1710 if (extended_select)
88ac883a 1711 {
aaa2f297
VZ
1712 if ( !m_current )
1713 {
618a5e38 1714 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
1715 }
1716
88ac883a
VZ
1717 // don't change the mark (m_current)
1718 SelectItemRange(m_current, item);
1719 }
1720 else
1721 {
c0de7af4 1722 bool select=TRUE; // the default
88ac883a 1723
c193b707
VZ
1724 // Check if we need to toggle hilight (ctrl mode)
1725 if (!unselect_others)
618a5e38 1726 select=!item->IsSelected();
88ac883a 1727
91b8de8d 1728 m_current = m_key_current = item;
c193b707
VZ
1729 m_current->SetHilight(select);
1730 RefreshLine( m_current );
88ac883a 1731 }
389cdc7a 1732
f135ff73 1733 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1734 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1735}
1736
941830cb 1737void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
cb59313c 1738 wxArrayTreeItemIds &array) const
c801d85f 1739{
8dc99046 1740 if ( item->IsSelected() )
9dfbf520 1741 array.Add(wxTreeItemId(item));
91b8de8d 1742
9dfbf520 1743 if ( item->HasChildren() )
91b8de8d 1744 {
9dfbf520
VZ
1745 wxArrayGenericTreeItems& children = item->GetChildren();
1746 size_t count = children.GetCount();
1747 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1748 FillArray(children[n], array);
91b8de8d
RR
1749 }
1750}
1751
941830cb 1752size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d 1753{
618a5e38
RR
1754 array.Empty();
1755 wxTreeItemId idRoot = GetRootItem();
1756 if ( idRoot.IsOk() )
1757 {
1758 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1759 }
1760 //else: the tree is empty, so no selections
91b8de8d 1761
618a5e38 1762 return array.Count();
91b8de8d
RR
1763}
1764
941830cb 1765void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1766{
91b8de8d
RR
1767 if (!item.IsOk()) return;
1768
941830cb 1769 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 1770
f65635b5
VZ
1771 // first expand all parent branches
1772 wxGenericTreeItem *parent = gitem->GetParent();
999723f3
VS
1773
1774 if ( HasFlag(wxTR_HIDE_ROOT) )
f65635b5 1775 {
999723f3
VS
1776 while ( parent != m_anchor )
1777 {
1778 Expand(parent);
1779 parent = parent->GetParent();
1780 }
1781 }
1782 else
1783 {
1784 while ( parent )
1785 {
1786 Expand(parent);
1787 parent = parent->GetParent();
1788 }
f65635b5
VZ
1789 }
1790
5391f772 1791 //if (parent) CalculatePositions();
91b8de8d
RR
1792
1793 ScrollTo(item);
1794}
1795
941830cb 1796void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d
RR
1797{
1798 if (!item.IsOk()) return;
1799
dc6c62a9
RR
1800 // We have to call this here because the label in
1801 // question might just have been added and no screen
1802 // update taken place.
cd66f45b 1803 if (m_dirty) wxYieldIfNeeded();
dc6c62a9 1804
941830cb 1805 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1806
f65635b5 1807 // now scroll to the item
0659e7ee 1808 int item_y = gitem->GetY();
8dc99046 1809
0659e7ee
RR
1810 int start_x = 0;
1811 int start_y = 0;
1e6feb95 1812 GetViewStart( &start_x, &start_y );
91b8de8d 1813 start_y *= PIXELS_PER_UNIT;
978f38c2 1814
a93109d5
RR
1815 int client_h = 0;
1816 int client_w = 0;
1817 GetClientSize( &client_w, &client_h );
ef44a621 1818
0659e7ee
RR
1819 if (item_y < start_y+3)
1820 {
91b8de8d 1821 // going down
0659e7ee
RR
1822 int x = 0;
1823 int y = 0;
91b8de8d
RR
1824 m_anchor->GetSize( x, y, this );
1825 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1826 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1827 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1828 // Item should appear at top
91b8de8d 1829 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1830 }
91b8de8d 1831 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1832 {
c7a9fa36
RR
1833 // going up
1834 int x = 0;
1835 int y = 0;
1836 m_anchor->GetSize( x, y, this );
1837 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1838 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1839 item_y += PIXELS_PER_UNIT+2;
1840 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1841 // Item should appear at bottom
c7a9fa36 1842 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 1843 }
edaa81ae 1844}
c801d85f 1845
e1ee62bd 1846// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 1847static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1848
004fd0c8 1849static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1850 wxGenericTreeItem **item2)
edaa81ae 1851{
941830cb 1852 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1853
1854 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1855}
1856
941830cb 1857int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
e1ee62bd 1858 const wxTreeItemId& item2)
0659e7ee 1859{
87138c52 1860 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1861}
1862
941830cb 1863void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 1864{
223d09f6 1865 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 1866
941830cb 1867 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 1868
e1ee62bd 1869 wxCHECK_RET( !s_treeBeingSorted,
941830cb 1870 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1871
91b8de8d 1872 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1873 if ( children.Count() > 1 )
1874 {
618a5e38
RR
1875 m_dirty = TRUE;
1876
e1ee62bd
VZ
1877 s_treeBeingSorted = this;
1878 children.Sort(tree_ctrl_compare_func);
1879 s_treeBeingSorted = NULL;
e1ee62bd
VZ
1880 }
1881 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1882}
1883
941830cb 1884wxImageList *wxGenericTreeCtrl::GetImageList() const
edaa81ae 1885{
f135ff73 1886 return m_imageListNormal;
edaa81ae
RR
1887}
1888
618a5e38
RR
1889wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
1890{
1891 return m_imageListButtons;
1892}
1893
941830cb 1894wxImageList *wxGenericTreeCtrl::GetStateImageList() const
c801d85f 1895{
f135ff73 1896 return m_imageListState;
edaa81ae 1897}
c801d85f 1898
618a5e38 1899void wxGenericTreeCtrl::CalculateLineHeight()
e2414cbe 1900{
f2593d0d
RR
1901 wxClientDC dc(this);
1902 m_lineHeight = (int)(dc.GetCharHeight() + 4);
06b466c7 1903
618a5e38
RR
1904 if ( m_imageListNormal )
1905 {
1906 // Calculate a m_lineHeight value from the normal Image sizes.
1907 // May be toggle off. Then wxGenericTreeCtrl will spread when
1908 // necessary (which might look ugly).
1909 int n = m_imageListNormal->GetImageCount();
1910 for (int i = 0; i < n ; i++)
1911 {
1912 int width = 0, height = 0;
1913 m_imageListNormal->GetSize(i, width, height);
1914 if (height > m_lineHeight) m_lineHeight = height;
1915 }
1916 }
1917
1918 if (m_imageListButtons)
f2593d0d 1919 {
618a5e38
RR
1920 // Calculate a m_lineHeight value from the Button image sizes.
1921 // May be toggle off. Then wxGenericTreeCtrl will spread when
1922 // necessary (which might look ugly).
1923 int n = m_imageListButtons->GetImageCount();
1924 for (int i = 0; i < n ; i++)
1925 {
1926 int width = 0, height = 0;
1927 m_imageListButtons->GetSize(i, width, height);
1928 if (height > m_lineHeight) m_lineHeight = height;
1929 }
f2593d0d 1930 }
91b8de8d 1931
618a5e38 1932 if (m_lineHeight < 30)
f2593d0d 1933 m_lineHeight += 2; // at least 2 pixels
06b466c7 1934 else
f2593d0d 1935 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 1936}
e2414cbe 1937
618a5e38
RR
1938void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
1939{
1940 if (m_ownsImageListNormal) delete m_imageListNormal;
1941 m_imageListNormal = imageList;
1942 m_ownsImageListNormal = FALSE;
1943 m_dirty = TRUE;
f4bd6759
JS
1944 // Don't do any drawing if we're setting the list to NULL,
1945 // since we may be in the process of deleting the tree control.
1946 if (imageList)
1947 CalculateLineHeight();
618a5e38
RR
1948}
1949
941830cb 1950void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1951{
46cd520d 1952 if (m_ownsImageListState) delete m_imageListState;
f135ff73 1953 m_imageListState = imageList;
46cd520d
VS
1954 m_ownsImageListState = FALSE;
1955}
1956
618a5e38
RR
1957void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
1958{
1959 if (m_ownsImageListButtons) delete m_imageListButtons;
1960 m_imageListButtons = imageList;
1961 m_ownsImageListButtons = FALSE;
1962 m_dirty = TRUE;
1963 CalculateLineHeight();
1964}
1965
46cd520d
VS
1966void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
1967{
1968 SetImageList(imageList);
1969 m_ownsImageListNormal = TRUE;
1970}
1971
1972void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
1973{
1974 SetStateImageList(imageList);
1975 m_ownsImageListState = TRUE;
edaa81ae 1976}
e2414cbe 1977
618a5e38
RR
1978void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
1979{
1980 SetButtonsImageList(imageList);
1981 m_ownsImageListButtons = TRUE;
1982}
1983
f135ff73
VZ
1984// -----------------------------------------------------------------------------
1985// helpers
1986// -----------------------------------------------------------------------------
0659e7ee 1987
941830cb 1988void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 1989{
0659e7ee
RR
1990 if (m_anchor)
1991 {
618a5e38 1992 int x = 0, y = 0;
91b8de8d 1993 m_anchor->GetSize( x, y, this );
c193b707 1994 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1995 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1996 int x_pos = GetScrollPos( wxHORIZONTAL );
1997 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1998 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1999 }
2000 else
2001 {
2002 SetScrollbars( 0, 0, 0, 0 );
2003 }
edaa81ae 2004}
c801d85f 2005
941830cb 2006int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 2007{
dc6c62a9
RR
2008 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2009 return item->GetHeight();
2010 else
2011 return m_lineHeight;
91b8de8d
RR
2012}
2013
941830cb 2014void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 2015{
618a5e38
RR
2016 // TODO implement "state" icon on items
2017
9ec64fa7
VZ
2018 wxTreeItemAttr *attr = item->GetAttributes();
2019 if ( attr && attr->HasFont() )
2020 dc.SetFont(attr->GetFont());
2021 else if (item->IsBold())
eff869aa 2022 dc.SetFont(m_boldFont);
ef44a621 2023
618a5e38 2024 long text_w = 0, text_h = 0;
bbe0af5b 2025 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 2026
618a5e38 2027 int image_h = 0, image_w = 0;
8dc99046
VZ
2028 int image = item->GetCurrentImage();
2029 if ( image != NO_IMAGE )
bbe0af5b 2030 {
2c8e4738
VZ
2031 if ( m_imageListNormal )
2032 {
2033 m_imageListNormal->GetSize( image, image_w, image_h );
2034 image_w += 4;
2035 }
2036 else
2037 {
2038 image = NO_IMAGE;
2039 }
bbe0af5b 2040 }
ef44a621 2041
91b8de8d
RR
2042 int total_h = GetLineHeight(item);
2043
b771aa29 2044 if ( item->IsSelected() )
cdb3cffe 2045 {
b771aa29 2046 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
cdb3cffe 2047 }
afa6a1a1 2048 else
c0fba4d1
VS
2049 {
2050 wxColour colBg;
2051 if ( attr && attr->HasBackgroundColour() )
2052 colBg = attr->GetBackgroundColour();
2053 else
2054 colBg = m_backgroundColour;
2055 dc.SetBrush(wxBrush(colBg, wxSOLID));
2056 }
afa6a1a1 2057
cdb3cffe 2058 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
b77d9650 2059
c6f4913a 2060 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
a69cb1cc 2061 {
c6f4913a
VS
2062 int x, y, w, h;
2063
2064 DoGetPosition(&x, &y);
2065 DoGetSize(&w, &h);
2066 dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset);
a69cb1cc
JS
2067 }
2068 else
b77d9650 2069 {
c6f4913a
VS
2070 if ( item->IsSelected() && image != NO_IMAGE )
2071 {
2072 // If it's selected, and there's an image, then we should
2073 // take care to leave the area under the image painted in the
2074 // background colour.
2075 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
2076 item->GetWidth() - image_w + 2, total_h-offset );
2077 }
2078 else
2079 {
2080 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
2081 item->GetWidth()+2, total_h-offset );
2082 }
b77d9650 2083 }
ef44a621 2084
8dc99046 2085 if ( image != NO_IMAGE )
bbe0af5b 2086 {
d30b4d20 2087 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 2088 m_imageListNormal->Draw( image, dc,
49cd56ef 2089 item->GetX(),
d701d432 2090 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
2091 wxIMAGELIST_DRAW_TRANSPARENT );
2092 dc.DestroyClippingRegion();
2093 }
ef44a621 2094
afa6a1a1 2095 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
2096 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2097 dc.DrawText( item->GetText(),
2098 (wxCoord)(image_w + item->GetX()),
2099 (wxCoord)(item->GetY() + extraH));
ef44a621 2100
eff869aa
RR
2101 // restore normal font
2102 dc.SetFont( m_normalFont );
ef44a621
VZ
2103}
2104
91b8de8d 2105// Now y stands for the top of the item, whereas it used to stand for middle !
941830cb 2106void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2107{
618a5e38
RR
2108 int x = level*m_indent;
2109 if (!HasFlag(wxTR_HIDE_ROOT))
2110 {
2111 x += m_indent;
2112 }
2113 else if (level == 0)
2114 {
2b84e565
VS
2115 // always expand hidden root
2116 int origY = y;
618a5e38 2117 wxArrayGenericTreeItems& children = item->GetChildren();
2b84e565
VS
2118 int count = children.Count();
2119 if (count > 0)
2120 {
2121 int n = 0, oldY;
2122 do {
2123 oldY = y;
2124 PaintLevel(children[n], dc, 1, y);
2125 } while (++n < count);
2126
e76520fd 2127 if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
2b84e565
VS
2128 {
2129 // draw line down to last child
2130 origY += GetLineHeight(children[0])>>1;
2131 oldY += GetLineHeight(children[n-1])>>1;
2132 dc.DrawLine(3, origY, 3, oldY);
2133 }
2134 }
618a5e38
RR
2135 return;
2136 }
91b8de8d 2137
618a5e38
RR
2138 item->SetX(x+m_spacing);
2139 item->SetY(y);
389cdc7a 2140
618a5e38
RR
2141 int h = GetLineHeight(item);
2142 int y_top = y;
2143 int y_mid = y_top + (h>>1);
2144 y += h;
4832f7c0 2145
618a5e38
RR
2146 int exposed_x = dc.LogicalToDeviceX(0);
2147 int exposed_y = dc.LogicalToDeviceY(y_top);
233058c7 2148
618a5e38 2149 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
bbe0af5b 2150 {
c6f4913a
VS
2151 wxPen *pen =
2152#ifndef __WXMAC__
2153 // don't draw rect outline if we already have the
2154 // background color under Mac
2155 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2156#endif // !__WXMAC__
2157 wxTRANSPARENT_PEN;
2158
2159 wxColour colText;
2160 if ( item->IsSelected() )
2161 {
a756f210 2162 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
c6f4913a
VS
2163 }
2164 else
2165 {
2166 wxTreeItemAttr *attr = item->GetAttributes();
2167 if (attr && attr->HasTextColour())
2168 colText = attr->GetTextColour();
2169 else
6f0dd6b2 2170 colText = GetForegroundColour();
c6f4913a
VS
2171 }
2172
2173 // prepare to draw
2174 dc.SetTextForeground(colText);
2175 dc.SetPen(*pen);
2176
2177 // draw
2178 PaintItem(item, dc);
2179
2180 if (HasFlag(wxTR_ROW_LINES))
2181 {
2182 // if the background colour is white, choose a
2183 // contrasting color for the lines
2184 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2185 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2186 dc.DrawLine(0, y_top, 10000, y_top);
2187 dc.DrawLine(0, y, 10000, y);
2188 }
2189
2190 // restore DC objects
2191 dc.SetBrush(*wxWHITE_BRUSH);
2192 dc.SetPen(m_dottedPen);
2193 dc.SetTextForeground(*wxBLACK);
2194
618a5e38 2195 if (item->HasPlus() && HasButtons()) // should the item show a button?
cdb3cffe 2196 {
618a5e38
RR
2197 if (!HasFlag(wxTR_NO_LINES))
2198 {
2b84e565 2199 if (x > (signed)m_indent)
618a5e38 2200 dc.DrawLine(x - m_indent, y_mid, x - 5, y_mid);
2b84e565
VS
2201 else if (HasFlag(wxTR_LINES_AT_ROOT))
2202 dc.DrawLine(3, y_mid, x - 5, y_mid);
618a5e38
RR
2203 dc.DrawLine(x + 5, y_mid, x + m_spacing, y_mid);
2204 }
2205
2206 if (m_imageListButtons != NULL)
c22886bd 2207 {
618a5e38 2208 // draw the image button here
2b84e565
VS
2209 int image_h = 0, image_w = 0, image = wxTreeItemIcon_Normal;
2210 if (item->IsExpanded()) image = wxTreeItemIcon_Expanded;
618a5e38 2211 if (item->IsSelected())
2b84e565 2212 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
618a5e38
RR
2213 m_imageListButtons->GetSize(image, image_w, image_h);
2214 int xx = x - (image_w>>1);
2215 int yy = y_mid - (image_h>>1);
2216 dc.SetClippingRegion(xx, yy, image_w, image_h);
2217 m_imageListButtons->Draw(image, dc, xx, yy,
2218 wxIMAGELIST_DRAW_TRANSPARENT);
2219 dc.DestroyClippingRegion();
2220 }
2221 else if (HasFlag(wxTR_TWIST_BUTTONS))
2222 {
2223 // draw the twisty button here
574c939e 2224
17d5bdf9 2225 if (HasFlag(wxTR_AQUA_BUTTONS))
c22886bd 2226 {
17d5bdf9 2227 if (item->IsExpanded())
d21d2e5a 2228 dc.DrawBitmap( *m_arrowDown, x-5, y_mid-6, TRUE );
17d5bdf9 2229 else
d21d2e5a 2230 dc.DrawBitmap( *m_arrowRight, x-5, y_mid-6, TRUE );
c22886bd
RR
2231 }
2232 else
2233 {
d21d2e5a 2234 dc.SetBrush(*m_hilightBrush);
17d5bdf9 2235 dc.SetPen(*wxBLACK_PEN);
d21d2e5a 2236 wxPoint button[3];
17d5bdf9
RR
2237
2238 if (item->IsExpanded())
2239 {
2240 button[0].x = x-5;
2241 button[0].y = y_mid-2;
2242 button[1].x = x+5;
2243 button[1].y = y_mid-2;
2244 button[2].x = x;
2245 button[2].y = y_mid+3;
2246 }
2247 else
2248 {
2249 button[0].y = y_mid-5;
2250 button[0].x = x-2;
2251 button[1].y = y_mid+5;
2252 button[1].x = x-2;
2253 button[2].y = y_mid;
2254 button[2].x = x+3;
2255 }
d21d2e5a
RR
2256 dc.DrawPolygon(3, button);
2257 dc.SetPen(m_dottedPen);
c22886bd 2258 }
c22886bd 2259 }
618a5e38 2260 else // if (HasFlag(wxTR_HAS_BUTTONS))
c22886bd 2261 {
618a5e38
RR
2262 // draw the plus sign here
2263 dc.SetPen(*wxGREY_PEN);
2264 dc.SetBrush(*wxWHITE_BRUSH);
2265 dc.DrawRectangle(x-5, y_mid-4, 11, 9);
2266 dc.SetPen(*wxBLACK_PEN);
2267 dc.DrawLine(x-2, y_mid, x+3, y_mid);
c22886bd 2268 if (!item->IsExpanded())
618a5e38
RR
2269 dc.DrawLine(x, y_mid-2, x, y_mid+3);
2270 dc.SetPen(m_dottedPen);
c22886bd 2271 }
bbe0af5b 2272 }
618a5e38
RR
2273 else if (!HasFlag(wxTR_NO_LINES)) // no button; maybe a line?
2274 {
2275 // draw the horizontal line here
2276 int x_start = x;
2b84e565 2277 if (x > (signed)m_indent)
618a5e38 2278 x_start -= m_indent;
2b84e565
VS
2279 else if (HasFlag(wxTR_LINES_AT_ROOT))
2280 x_start = 3;
618a5e38
RR
2281 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2282 }
f135ff73 2283 }
d3a9f4af 2284
bbe0af5b
RR
2285 if (item->IsExpanded())
2286 {
91b8de8d 2287 wxArrayGenericTreeItems& children = item->GetChildren();
618a5e38 2288 int count = children.Count();
f65635b5 2289 if (count > 0)
9ec64fa7 2290 {
618a5e38
RR
2291 int n = 0, oldY;
2292 ++level;
2293 do {
2294 oldY = y;
2295 PaintLevel(children[n], dc, level, y);
2296 } while (++n < count);
2297
e76520fd 2298 if (!HasFlag(wxTR_NO_LINES) && count > 0)
618a5e38
RR
2299 {
2300 // draw line down to last child
2b84e565 2301 oldY += GetLineHeight(children[n-1])>>1;
618a5e38
RR
2302 if (HasButtons()) y_mid += 5;
2303 dc.DrawLine(x, y_mid, x, oldY);
2304 }
9ec64fa7 2305 }
bbe0af5b 2306 }
4c681997 2307}
c801d85f 2308
941830cb 2309void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
2310{
2311 if ( item )
2312 {
2313 if ( item->HasPlus() )
2314 {
2315 // it's a folder, indicate it by a border
2316 DrawBorder(item);
2317 }
2318 else
2319 {
2320 // draw a line under the drop target because the item will be
2321 // dropped there
2322 DrawLine(item, TRUE /* below */);
2323 }
2324
2325 SetCursor(wxCURSOR_BULLSEYE);
2326 }
2327 else
2328 {
2329 // can't drop here
2330 SetCursor(wxCURSOR_NO_ENTRY);
2331 }
2332}
2333
941830cb 2334void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 2335{
941830cb 2336 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2337
941830cb 2338 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2339
1044a386 2340 wxClientDC dc(this);
91b8de8d
RR
2341 PrepareDC( dc );
2342 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 2343 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 2344
3dbeaa52
VZ
2345 int w = i->GetWidth() + 2;
2346 int h = GetLineHeight(i) + 2;
91b8de8d 2347
3dbeaa52 2348 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
2349}
2350
941830cb 2351void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 2352{
941830cb 2353 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2354
941830cb 2355 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2356
1044a386 2357 wxClientDC dc(this);
91b8de8d
RR
2358 PrepareDC( dc );
2359 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 2360
3dbeaa52
VZ
2361 int x = i->GetX(),
2362 y = i->GetY();
2363 if ( below )
2364 {
2365 y += GetLineHeight(i) - 1;
2366 }
91b8de8d 2367
3dbeaa52 2368 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
2369}
2370
f135ff73
VZ
2371// -----------------------------------------------------------------------------
2372// wxWindows callbacks
2373// -----------------------------------------------------------------------------
2374
941830cb 2375void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 2376{
0659e7ee
RR
2377 wxPaintDC dc(this);
2378 PrepareDC( dc );
29d87bba 2379
941830cb
JS
2380 if ( !m_anchor)
2381 return;
2382
eff869aa 2383 dc.SetFont( m_normalFont );
0659e7ee 2384 dc.SetPen( m_dottedPen );
f38374d0 2385
eff869aa 2386 // this is now done dynamically
91b8de8d
RR
2387 //if(GetImageList() == NULL)
2388 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2389
91b8de8d 2390 int y = 2;
0659e7ee 2391 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 2392}
c801d85f 2393
b771aa29 2394void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
c801d85f 2395{
0659e7ee 2396 m_hasFocus = TRUE;
978f38c2 2397
b771aa29
VZ
2398 RefreshSelected();
2399
2400 event.Skip();
edaa81ae 2401}
c801d85f 2402
b771aa29 2403void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
c801d85f 2404{
0659e7ee 2405 m_hasFocus = FALSE;
978f38c2 2406
b771aa29
VZ
2407 RefreshSelected();
2408
2409 event.Skip();
edaa81ae 2410}
c801d85f 2411
941830cb 2412void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
c801d85f 2413{
978f38c2 2414 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
b09bda68 2415 te.m_evtKey = event;
978f38c2 2416 te.SetEventObject( this );
68eb5f63
VZ
2417 if ( GetEventHandler()->ProcessEvent( te ) )
2418 {
2419 // intercepted by the user code
2420 return;
2421 }
435fe83e 2422
91b8de8d 2423 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
2424 {
2425 event.Skip();
2426 return;
2427 }
ef44a621 2428
06b466c7
VZ
2429 // how should the selection work for this event?
2430 bool is_multiple, extended_select, unselect_others;
2431 EventFlagsToSelType(GetWindowStyleFlag(),
2432 event.ShiftDown(),
2433 event.ControlDown(),
dfc6cd93
SB
2434 is_multiple, extended_select, unselect_others);
2435
2436 // + : Expand
2437 // - : Collaspe
f6bcfd97 2438 // * : Expand all/Collapse all
dfc6cd93
SB
2439 // ' ' | return : activate
2440 // up : go up (not last children!)
2441 // down : go down
2442 // left : go to parent
2443 // right : open if parent and go next
2444 // home : go to root
2445 // end : go to last item without opening parents
cb59313c
VZ
2446 // alnum : start or continue searching for the item with this prefix
2447 int keyCode = event.KeyCode();
2448 switch ( keyCode )
978f38c2
VZ
2449 {
2450 case '+':
2451 case WXK_ADD:
2452 if (m_current->HasPlus() && !IsExpanded(m_current))
2453 {
2454 Expand(m_current);
2455 }
2456 break;
ef44a621 2457
f6bcfd97
BP
2458 case '*':
2459 case WXK_MULTIPLY:
2460 if ( !IsExpanded(m_current) )
2461 {
2462 // expand all
2463 ExpandAll(m_current);
2464 break;
2465 }
2466 //else: fall through to Collapse() it
2467
978f38c2
VZ
2468 case '-':
2469 case WXK_SUBTRACT:
2470 if (IsExpanded(m_current))
2471 {
2472 Collapse(m_current);
2473 }
2474 break;
ef44a621 2475
978f38c2
VZ
2476 case ' ':
2477 case WXK_RETURN:
2478 {
2479 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
40fab78b 2480 event.m_item = (long) m_current;
978f38c2
VZ
2481 event.SetEventObject( this );
2482 GetEventHandler()->ProcessEvent( event );
2483 }
2484 break;
ef44a621 2485
618a5e38
RR
2486 // up goes to the previous sibling or to the last
2487 // of its children if it's expanded
978f38c2
VZ
2488 case WXK_UP:
2489 {
91b8de8d 2490 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
2491 if (!prev)
2492 {
91b8de8d 2493 prev = GetParent( m_key_current );
618a5e38
RR
2494 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2495 {
2496 break; // don't go to root if it is hidden
2497 }
c193b707
VZ
2498 if (prev)
2499 {
618a5e38 2500 long cookie = 0;
91b8de8d 2501 wxTreeItemId current = m_key_current;
618a5e38
RR
2502 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2503 if (current == GetFirstChild( prev, cookie ))
90e58684
RR
2504 {
2505 // otherwise we return to where we came from
88ac883a 2506 SelectItem( prev, unselect_others, extended_select );
941830cb 2507 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
90e58684 2508 break;
c193b707 2509 }
978f38c2
VZ
2510 }
2511 }
2512 if (prev)
2513 {
69a282d4 2514 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 2515 {
69a282d4
VZ
2516 wxTreeItemId child = GetLastChild(prev);
2517 if ( child )
2518 {
2519 prev = child;
2520 }
978f38c2 2521 }
69a282d4 2522
88ac883a 2523 SelectItem( prev, unselect_others, extended_select );
941830cb 2524 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
2525 }
2526 }
2527 break;
ef44a621 2528
978f38c2
VZ
2529 // left arrow goes to the parent
2530 case WXK_LEFT:
2531 {
2532 wxTreeItemId prev = GetParent( m_current );
618a5e38
RR
2533 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2534 {
2535 // don't go to root if it is hidden
2536 prev = GetPrevSibling( m_current );
2537 }
978f38c2
VZ
2538 if (prev)
2539 {
88ac883a 2540 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2541 }
2542 }
2543 break;
ef44a621 2544
978f38c2 2545 case WXK_RIGHT:
618a5e38
RR
2546 // this works the same as the down arrow except that we
2547 // also expand the item if it wasn't expanded yet
978f38c2
VZ
2548 Expand(m_current);
2549 // fall through
2550
2551 case WXK_DOWN:
ef44a621 2552 {
91b8de8d 2553 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
2554 {
2555 long cookie = 0;
91b8de8d 2556 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 2557 SelectItem( child, unselect_others, extended_select );
941830cb 2558 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
2559 }
2560 else
2561 {
91b8de8d 2562 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2563 if (!next)
978f38c2 2564 {
91b8de8d 2565 wxTreeItemId current = m_key_current;
978f38c2
VZ
2566 while (current && !next)
2567 {
2568 current = GetParent( current );
2569 if (current) next = GetNextSibling( current );
2570 }
2571 }
b62c3631 2572 if (next)
978f38c2 2573 {
88ac883a 2574 SelectItem( next, unselect_others, extended_select );
941830cb 2575 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
2576 }
2577 }
ef44a621 2578 }
978f38c2 2579 break;
ef44a621 2580
978f38c2
VZ
2581 // <End> selects the last visible tree item
2582 case WXK_END:
2583 {
2584 wxTreeItemId last = GetRootItem();
2585
2586 while ( last.IsOk() && IsExpanded(last) )
2587 {
2588 wxTreeItemId lastChild = GetLastChild(last);
2589
2590 // it may happen if the item was expanded but then all of
2591 // its children have been deleted - so IsExpanded() returned
2592 // TRUE, but GetLastChild() returned invalid item
2593 if ( !lastChild )
2594 break;
2595
2596 last = lastChild;
2597 }
2598
2599 if ( last.IsOk() )
2600 {
88ac883a 2601 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2602 }
2603 }
2604 break;
2605
2606 // <Home> selects the root item
2607 case WXK_HOME:
2608 {
2609 wxTreeItemId prev = GetRootItem();
cb59313c
VZ
2610 if (!prev)
2611 break;
2612
2613 if ( HasFlag(wxTR_HIDE_ROOT) )
978f38c2 2614 {
618a5e38
RR
2615 long dummy;
2616 prev = GetFirstChild(prev, dummy);
cb59313c
VZ
2617 if (!prev)
2618 break;
978f38c2 2619 }
cb59313c 2620
618a5e38 2621 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2622 }
2623 break;
2624
2625 default:
cb59313c 2626 // do not use wxIsalnum() here
1ec3a984
RR
2627 if ( !event.HasModifiers() &&
2628 ((keyCode >= '0' && keyCode <= '9') ||
2629 (keyCode >= 'a' && keyCode <= 'z') ||
2630 (keyCode >= 'A' && keyCode <= 'Z' )))
cb59313c
VZ
2631 {
2632 // find the next item starting with the given prefix
2633 char ch = (char)keyCode;
1ec3a984 2634
b3887715 2635 wxTreeItemId id = FindItem(m_current, m_findPrefix + (wxChar)ch);
cb59313c
VZ
2636 if ( !id.IsOk() )
2637 {
2638 // no such item
2639 break;
2640 }
2641
2642 SelectItem(id);
2643
2644 m_findPrefix += ch;
2645
2646 // also start the timer to reset the current prefix if the user
2647 // doesn't press any more alnum keys soon -- we wouldn't want
2648 // to use this prefix for a new item search
2649 if ( !m_findTimer )
2650 {
2651 m_findTimer = new wxTreeFindTimer(this);
2652 }
2653
2654 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
2655 }
2656 else
2657 {
2658 event.Skip();
2659 }
978f38c2 2660 }
edaa81ae 2661}
c801d85f 2662
941830cb 2663wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2664{
618a5e38
RR
2665 // JACS: removed wxYieldIfNeeded() because it can cause the window
2666 // to be deleted from under us if a close window event is pending
dc6c62a9 2667
91b8de8d
RR
2668 int w, h;
2669 GetSize(&w, &h);
91b8de8d 2670 flags=0;
618a5e38
RR
2671 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2672 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2673 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2674 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2675 if (flags) return wxTreeItemId();
d3a9f4af 2676
618a5e38
RR
2677 if (m_anchor == NULL)
2678 {
2679 flags = wxTREE_HITTEST_NOWHERE;
2680 return wxTreeItemId();
2681 }
5716a1ab 2682
d027179b
VS
2683 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
2684 this, flags, 0);
618a5e38
RR
2685 if (hit == NULL)
2686 {
2687 flags = wxTREE_HITTEST_NOWHERE;
2688 return wxTreeItemId();
2689 }
2690 return hit;
4f22cf8d
RR
2691}
2692
8fb3bfe2
JS
2693// get the bounding rectangle of the item (or of its label only)
2694bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2695 wxRect& rect,
33ac7e6f 2696 bool WXUNUSED(textOnly)) const
8fb3bfe2 2697{
601c163b 2698 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
8fb3bfe2
JS
2699
2700 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2701
2702 int startX, startY;
2703 GetViewStart(& startX, & startY);
2704
1ed01484 2705 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
c22886bd 2706 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
1ed01484 2707 rect.width = i->GetWidth();
c22886bd
RR
2708 //rect.height = i->GetHeight();
2709 rect.height = GetLineHeight(i);
8fb3bfe2
JS
2710
2711 return TRUE;
8fb3bfe2
JS
2712}
2713
e179bd65
RR
2714/* **** */
2715
941830cb 2716void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
e179bd65
RR
2717{
2718 if (!item.IsOk()) return;
2719
941830cb 2720 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
9dfbf520 2721
e179bd65 2722 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
40fab78b 2723 te.m_item = (long) m_currentEdit;
e179bd65
RR
2724 te.SetEventObject( this );
2725 GetEventHandler()->ProcessEvent( te );
2726
2727 if (!te.IsAllowed()) return;
8dc99046 2728
dc6c62a9
RR
2729 // We have to call this here because the label in
2730 // question might just have been added and no screen
2731 // update taken place.
cd66f45b 2732 if (m_dirty) wxYieldIfNeeded();
9dfbf520 2733
e179bd65 2734 wxString s = m_currentEdit->GetText();
e179bd65
RR
2735 int w = m_currentEdit->GetWidth();
2736 int h = m_currentEdit->GetHeight();
d027179b
VS
2737 int x, y;
2738 CalcScrolledPosition(m_currentEdit->GetX(), m_currentEdit->GetY(), &x, &y);
9dfbf520 2739
5f1ea0ee
RR
2740 int image_h = 0;
2741 int image_w = 0;
8dc99046
VZ
2742
2743 int image = m_currentEdit->GetCurrentImage();
2744 if ( image != NO_IMAGE )
5f1ea0ee 2745 {
2c8e4738
VZ
2746 if ( m_imageListNormal )
2747 {
2748 m_imageListNormal->GetSize( image, image_w, image_h );
2749 image_w += 4;
2750 }
6359fa0e 2751 else
2c8e4738
VZ
2752 {
2753 wxFAIL_MSG(_T("you must create an image list to use images!"));
2754 }
5f1ea0ee
RR
2755 }
2756 x += image_w;
2757 w -= image_w + 4; // I don't know why +4 is needed
e179bd65 2758
6359fa0e
VZ
2759 wxTreeTextCtrl *text = new wxTreeTextCtrl(this, -1,
2760 &m_renameAccept,
2761 &m_renameRes,
2762 this,
2763 s,
2764 wxPoint(x-4,y-4),
2765 wxSize(w+11,h+8));
e179bd65
RR
2766 text->SetFocus();
2767}
2768
941830cb 2769void wxGenericTreeCtrl::OnRenameTimer()
e179bd65
RR
2770{
2771 Edit( m_current );
2772}
2773
941830cb 2774void wxGenericTreeCtrl::OnRenameAccept()
e179bd65 2775{
618a5e38 2776 // TODO if the validator fails this causes a crash
e179bd65 2777 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
40fab78b 2778 le.m_item = (long) m_currentEdit;
e179bd65
RR
2779 le.SetEventObject( this );
2780 le.m_label = m_renameRes;
2781 GetEventHandler()->ProcessEvent( le );
9dfbf520 2782
e179bd65 2783 if (!le.IsAllowed()) return;
9dfbf520 2784
5f1ea0ee 2785 SetItemText( m_currentEdit, m_renameRes );
e179bd65 2786}
9dfbf520 2787
941830cb 2788void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2789{
bbe0af5b 2790 if ( !m_anchor ) return;
978f38c2 2791
3dbeaa52
VZ
2792 // we process left mouse up event (enables in-place edit), right down
2793 // (pass to the user code), left dbl click (activate item) and
2794 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
2795 if ( !(event.LeftDown() ||
2796 event.LeftUp() ||
3dbeaa52
VZ
2797 event.RightDown() ||
2798 event.LeftDClick() ||
2799 event.Dragging() ||
2800 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2801 {
2802 event.Skip();
2803
2804 return;
2805 }
2806
d027179b 2807 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
29d87bba 2808
3dbeaa52 2809 int flags = 0;
d027179b 2810 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
3dbeaa52 2811
3dbeaa52 2812 if ( event.Dragging() && !m_isDragging )
bbe0af5b 2813 {
fd9811b1 2814 if (m_dragCount == 0)
d027179b 2815 m_dragStart = pt;
8dc99046 2816
fd9811b1 2817 m_dragCount++;
8dc99046 2818
3dbeaa52
VZ
2819 if (m_dragCount != 3)
2820 {
2821 // wait until user drags a bit further...
2822 return;
2823 }
8dc99046 2824
3dbeaa52
VZ
2825 wxEventType command = event.RightIsDown()
2826 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2827 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 2828
fd9811b1 2829 wxTreeEvent nevent( command, GetId() );
40fab78b 2830 nevent.m_item = (long) m_current;
fd9811b1 2831 nevent.SetEventObject(this);
3dbeaa52
VZ
2832
2833 // by default the dragging is not supported, the user code must
2834 // explicitly allow the event for it to take place
2835 nevent.Veto();
2836
2837 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2838 {
2839 // we're going to drag this item
2840 m_isDragging = TRUE;
2841
b3a7510d
VZ
2842 // remember the old cursor because we will change it while
2843 // dragging
2844 m_oldCursor = m_cursor;
2845
2846 // in a single selection control, hide the selection temporarily
2847 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2848 {
941830cb 2849 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
2850
2851 if ( m_oldSelection )
2852 {
2853 m_oldSelection->SetHilight(FALSE);
2854 RefreshLine(m_oldSelection);
2855 }
2856 }
2857
3dbeaa52
VZ
2858 CaptureMouse();
2859 }
bbe0af5b 2860 }
3dbeaa52 2861 else if ( event.Moving() )
fd9811b1 2862 {
3dbeaa52
VZ
2863 if ( item != m_dropTarget )
2864 {
2865 // unhighlight the previous drop target
2866 DrawDropEffect(m_dropTarget);
fd9811b1 2867
3dbeaa52 2868 m_dropTarget = item;
978f38c2 2869
3dbeaa52
VZ
2870 // highlight the current drop target if any
2871 DrawDropEffect(m_dropTarget);
fb882e1c 2872
cd66f45b 2873 wxYieldIfNeeded();
3dbeaa52 2874 }
e179bd65 2875 }
3dbeaa52
VZ
2876 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2877 {
2878 // erase the highlighting
2879 DrawDropEffect(m_dropTarget);
9dfbf520 2880
4f5fffcc
RR
2881 if ( m_oldSelection )
2882 {
2883 m_oldSelection->SetHilight(TRUE);
2884 RefreshLine(m_oldSelection);
2885 m_oldSelection = (wxGenericTreeItem *)NULL;
2886 }
2887
3dbeaa52
VZ
2888 // generate the drag end event
2889 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 2890
40fab78b 2891 event.m_item = (long) item;
d027179b 2892 event.m_pointDrag = pt;
3dbeaa52 2893 event.SetEventObject(this);
91b8de8d 2894
3dbeaa52 2895 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 2896
3dbeaa52
VZ
2897 m_isDragging = FALSE;
2898 m_dropTarget = (wxGenericTreeItem *)NULL;
2899
2900 ReleaseMouse();
2901
b3a7510d 2902 SetCursor(m_oldCursor);
3dbeaa52 2903
cd66f45b 2904 wxYieldIfNeeded();
3dbeaa52
VZ
2905 }
2906 else
bbe0af5b 2907 {
3dbeaa52
VZ
2908 // here we process only the messages which happen on tree items
2909
2910 m_dragCount = 0;
2911
2912 if (item == NULL) return; /* we hit the blank area */
2913
2914 if ( event.RightDown() )
2915 {
2916 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
40fab78b 2917 nevent.m_item = (long) item;
d027179b 2918 nevent.m_pointDrag = CalcScrolledPosition(pt);
3dbeaa52
VZ
2919 nevent.SetEventObject(this);
2920 GetEventHandler()->ProcessEvent(nevent);
2921 }
80d2803f 2922 else if ( event.LeftUp() )
f6bcfd97 2923 {
80d2803f 2924 if ( m_lastOnSame )
f6bcfd97 2925 {
80d2803f
VZ
2926 if ( (item == m_current) &&
2927 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2928 HasFlag(wxTR_EDIT_LABELS) )
2929 {
cb59313c
VZ
2930 if ( m_renameTimer )
2931 {
2932 if ( m_renameTimer->IsRunning() )
2933 m_renameTimer->Stop();
2934 }
2935 else
2936 {
2937 m_renameTimer = new wxTreeRenameTimer( this );
2938 }
bdb310a7 2939
cb59313c 2940 m_renameTimer->Start( wxTreeRenameTimer::DELAY, TRUE );
80d2803f 2941 }
f6bcfd97 2942
80d2803f
VZ
2943 m_lastOnSame = FALSE;
2944 }
3dbeaa52 2945 }
0326c494 2946 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3dbeaa52 2947 {
f6bcfd97
BP
2948 if ( event.LeftDown() )
2949 {
2950 m_lastOnSame = item == m_current;
2951 }
2952
0326c494
VZ
2953 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
2954 {
2955 // only toggle the item for a single click, double click on
2956 // the button doesn't do anything (it toggles the item twice)
2957 if ( event.LeftDown() )
2958 {
2959 Toggle( item );
2960 }
2961
2962 // don't select the item if the button was clicked
2963 return;
2964 }
2965
3dbeaa52
VZ
2966 // how should the selection work for this event?
2967 bool is_multiple, extended_select, unselect_others;
2968 EventFlagsToSelType(GetWindowStyleFlag(),
2969 event.ShiftDown(),
2970 event.ControlDown(),
dfc6cd93 2971 is_multiple, extended_select, unselect_others);
3dbeaa52 2972
3dbeaa52
VZ
2973 SelectItem(item, unselect_others, extended_select);
2974
618a5e38
RR
2975 // For some reason, Windows isn't recognizing a left double-click,
2976 // so we need to simulate it here. Allow 200 milliseconds for now.
3dbeaa52
VZ
2977 if ( event.LeftDClick() )
2978 {
f6bcfd97 2979 // double clicking should not start editing the item label
cb59313c
VZ
2980 if ( m_renameTimer )
2981 m_renameTimer->Stop();
2982
f6bcfd97
BP
2983 m_lastOnSame = FALSE;
2984
ebb987b7
VZ
2985 // send activate event first
2986 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2987 nevent.m_item = (long) item;
d027179b 2988 nevent.m_pointDrag = CalcScrolledPosition(pt);
ebb987b7
VZ
2989 nevent.SetEventObject( this );
2990 if ( !GetEventHandler()->ProcessEvent( nevent ) )
618a5e38 2991 {
ebb987b7
VZ
2992 // if the user code didn't process the activate event,
2993 // handle it ourselves by toggling the item when it is
2994 // double clicked
2995 if ( item->HasPlus() )
2996 {
2997 Toggle(item);
2998 }
618a5e38 2999 }
3dbeaa52
VZ
3000 }
3001 }
bbe0af5b 3002 }
edaa81ae 3003}
c801d85f 3004
941830cb 3005void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
3db7be80 3006{
bbe0af5b
RR
3007 /* after all changes have been done to the tree control,
3008 * we actually redraw the tree when everything is over */
ef44a621 3009
618a5e38 3010 if (!m_dirty) return;
ef44a621 3011
bbe0af5b 3012 m_dirty = FALSE;
3db7be80 3013
bbe0af5b 3014 CalculatePositions();
91b8de8d 3015 Refresh();
bbe0af5b 3016 AdjustMyScrollbars();
3db7be80
RR
3017}
3018
941830cb 3019void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 3020{
e06b9569
JS
3021 wxCoord text_w = 0;
3022 wxCoord text_h = 0;
9dfbf520 3023
40e7f56c
VZ
3024 wxTreeItemAttr *attr = item->GetAttributes();
3025 if ( attr && attr->HasFont() )
3026 dc.SetFont(attr->GetFont());
3027 else if ( item->IsBold() )
eff869aa 3028 dc.SetFont(m_boldFont);
9dfbf520 3029
91b8de8d 3030 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 3031 text_h+=2;
91b8de8d 3032
eff869aa
RR
3033 // restore normal font
3034 dc.SetFont( m_normalFont );
9dfbf520 3035
91b8de8d
RR
3036 int image_h = 0;
3037 int image_w = 0;
8dc99046
VZ
3038 int image = item->GetCurrentImage();
3039 if ( image != NO_IMAGE )
91b8de8d 3040 {
2c8e4738
VZ
3041 if ( m_imageListNormal )
3042 {
3043 m_imageListNormal->GetSize( image, image_w, image_h );
3044 image_w += 4;
3045 }
91b8de8d
RR
3046 }
3047
3048 int total_h = (image_h > text_h) ? image_h : text_h;
3049
618a5e38 3050 if (total_h < 30)
f2593d0d 3051 total_h += 2; // at least 2 pixels
06b466c7 3052 else
f2593d0d 3053 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
3054
3055 item->SetHeight(total_h);
6cedba09
VZ
3056 if (total_h>m_lineHeight)
3057 m_lineHeight=total_h;
bbe0af5b 3058
91b8de8d
RR
3059 item->SetWidth(image_w+text_w+2);
3060}
3061
3062// -----------------------------------------------------------------------------
3063// for developper : y is now the top of the level
3064// not the middle of it !
941830cb 3065void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 3066{
618a5e38
RR
3067 int x = level*m_indent;
3068 if (!HasFlag(wxTR_HIDE_ROOT))
3069 {
3070 x += m_indent;
3071 }
3072 else if (level == 0)
3073 {
3074 // a hidden root is not evaluated, but its
3075 // children are always calculated
3076 goto Recurse;
3077 }
389cdc7a 3078
91b8de8d 3079 CalculateSize( item, dc );
d3a9f4af 3080
91b8de8d 3081 // set its position
618a5e38 3082 item->SetX( x+m_spacing );
91b8de8d 3083 item->SetY( y );
618a5e38 3084 y += GetLineHeight(item);
4c681997 3085
bbe0af5b
RR
3086 if ( !item->IsExpanded() )
3087 {
618a5e38 3088 // we don't need to calculate collapsed branches
bbe0af5b
RR
3089 return;
3090 }
389cdc7a 3091
618a5e38 3092 Recurse:
91b8de8d
RR
3093 wxArrayGenericTreeItems& children = item->GetChildren();
3094 size_t n, count = children.Count();
618a5e38 3095 ++level;
91b8de8d 3096 for (n = 0; n < count; ++n )
618a5e38 3097 CalculateLevel( children[n], dc, level, y ); // recurse
edaa81ae 3098}
c801d85f 3099
941830cb 3100void wxGenericTreeCtrl::CalculatePositions()
c801d85f 3101{
bbe0af5b 3102 if ( !m_anchor ) return;
29d87bba 3103
bbe0af5b
RR
3104 wxClientDC dc(this);
3105 PrepareDC( dc );
29d87bba 3106
eff869aa 3107 dc.SetFont( m_normalFont );
29d87bba 3108
bbe0af5b 3109 dc.SetPen( m_dottedPen );
91b8de8d
RR
3110 //if(GetImageList() == NULL)
3111 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 3112
8dc99046 3113 int y = 2;
f65635b5 3114 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 3115}
c801d85f 3116
941830cb 3117void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 3118{
e6527f9d
RR
3119 if (m_dirty) return;
3120
d027179b 3121 wxSize client = GetClientSize();
4832f7c0 3122
bbe0af5b 3123 wxRect rect;
5941c5de 3124 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b
VS
3125 rect.width = client.x;
3126 rect.height = client.y;
f135ff73 3127
cb9965c3 3128 Refresh(TRUE, &rect);
f135ff73 3129
bbe0af5b 3130 AdjustMyScrollbars();
edaa81ae 3131}
c801d85f 3132
941830cb 3133void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 3134{
e6527f9d
RR
3135 if (m_dirty) return;
3136
bbe0af5b 3137 wxRect rect;
5941c5de 3138 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b 3139 rect.width = GetClientSize().x;
91b8de8d 3140 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 3141
cb9965c3 3142 Refresh(TRUE, &rect);
edaa81ae 3143}
c801d85f 3144
b771aa29
VZ
3145void wxGenericTreeCtrl::RefreshSelected()
3146{
3147 // TODO: this is awfully inefficient, we should keep the list of all
3148 // selected items internally, should be much faster
3149 if ( m_anchor )
3150 RefreshSelectedUnder(m_anchor);
3151}
3152
3153void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3154{
3155 if ( item->IsSelected() )
3156 RefreshLine(item);
3157
3158 const wxArrayGenericTreeItems& children = item->GetChildren();
3159 size_t count = children.GetCount();
3160 for ( size_t n = 0; n < count; n++ )
3161 {
3162 RefreshSelectedUnder(children[n]);
3163 }
3164}
3165
7009f411
VZ
3166// ----------------------------------------------------------------------------
3167// changing colours: we need to refresh the tree control
3168// ----------------------------------------------------------------------------
3169
3170bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3171{
3172 if ( !wxWindow::SetBackgroundColour(colour) )
3173 return FALSE;
3174
3175 Refresh();
3176
3177 return TRUE;
3178}
3179
0800a4ba 3180bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
7009f411
VZ
3181{
3182 if ( !wxWindow::SetForegroundColour(colour) )
3183 return FALSE;
3184
3185 Refresh();
3186
3187 return TRUE;
3188}
3189
1e6feb95 3190#endif // wxUSE_TREECTRL