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