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