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