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