]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
regenerated
[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();
bce1406b
RR
301
302 if (!wxPendingDelete.Member(this))
303 wxPendingDelete.Append(this);
304
305 if ((*m_accept) && ((*m_res) != m_startValue))
306 m_owner->OnRenameAccept();
307
e179bd65
RR
308 return;
309 }
310 if (event.m_keyCode == WXK_ESCAPE)
311 {
312 (*m_accept) = FALSE;
313 (*m_res) = "";
bce1406b
RR
314
315 if (!wxPendingDelete.Member(this))
316 wxPendingDelete.Append(this);
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);
334
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;
46cd520d
VS
635 m_ownsImageListNormal =
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
f135ff73
VZ
821// -----------------------------------------------------------------------------
822// item status inquiries
823// -----------------------------------------------------------------------------
824
1ed01484 825bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
c801d85f 826{
1ed01484
JS
827 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
828
829 // An item is only visible if it's not a descendant of a collapsed item
830 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
831 wxGenericTreeItem* parent = pItem->GetParent();
832 while (parent)
833 {
834 if (!parent->IsExpanded())
835 return FALSE;
836 parent = parent->GetParent();
837 }
838
839 int startX, startY;
840 GetViewStart(& startX, & startY);
841
842 wxSize clientSize = GetClientSize();
843
844 wxRect rect;
845 if (!GetBoundingRect(item, rect))
846 return FALSE;
847 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
848 return FALSE;
849 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
850 return FALSE;
851 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
852 return FALSE;
f135ff73 853
f2593d0d 854 return TRUE;
edaa81ae 855}
c801d85f 856
941830cb 857bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 858{
f2593d0d 859 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 860
941830cb 861 return !((wxGenericTreeItem*) item.m_pItem)->GetChildren().IsEmpty();
edaa81ae 862}
c801d85f 863
941830cb 864bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 865{
f2593d0d 866 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 867
941830cb 868 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
f135ff73 869}
29d87bba 870
941830cb 871bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
f135ff73 872{
f2593d0d 873 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 874
941830cb 875 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
f135ff73 876}
29d87bba 877
941830cb 878bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
ef44a621 879{
f2593d0d 880 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
ef44a621 881
941830cb 882 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
ef44a621
VZ
883}
884
f135ff73
VZ
885// -----------------------------------------------------------------------------
886// navigation
887// -----------------------------------------------------------------------------
29d87bba 888
941830cb 889wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const
f135ff73 890{
223d09f6 891 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 892
941830cb 893 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
f135ff73 894}
29d87bba 895
941830cb 896wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
f135ff73 897{
223d09f6 898 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 899
f135ff73
VZ
900 cookie = 0;
901 return GetNextChild(item, cookie);
902}
29d87bba 903
941830cb 904wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
f135ff73 905{
223d09f6 906 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 907
941830cb 908 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
4832f7c0
VZ
909 if ( (size_t)cookie < children.Count() )
910 {
06b466c7 911 return children.Item((size_t)cookie++);
4832f7c0
VZ
912 }
913 else
914 {
915 // there are no more of them
1e6d9499 916 return wxTreeItemId();
4832f7c0 917 }
f135ff73 918}
29d87bba 919
941830cb 920wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
978f38c2 921{
223d09f6 922 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 923
941830cb 924 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
0a240683 925 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
926}
927
941830cb 928wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
f135ff73 929{
223d09f6 930 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 931
941830cb 932 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
f135ff73
VZ
933 wxGenericTreeItem *parent = i->GetParent();
934 if ( parent == NULL )
935 {
936 // root item doesn't have any siblings
1e6d9499 937 return wxTreeItemId();
edaa81ae 938 }
4832f7c0 939
91b8de8d 940 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 941 int index = siblings.Index(i);
3c67202d 942 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 943
f135ff73 944 size_t n = (size_t)(index + 1);
fdd8d7b5 945 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 946}
c801d85f 947
941830cb 948wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 949{
223d09f6 950 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 951
941830cb 952 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
f135ff73
VZ
953 wxGenericTreeItem *parent = i->GetParent();
954 if ( parent == NULL )
c801d85f 955 {
f135ff73 956 // root item doesn't have any siblings
1e6d9499 957 return wxTreeItemId();
edaa81ae 958 }
4832f7c0 959
91b8de8d 960 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 961 int index = siblings.Index(i);
3c67202d 962 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 963
fdd8d7b5
VZ
964 return index == 0 ? wxTreeItemId()
965 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 966}
389cdc7a 967
1ed01484
JS
968// Only for internal use right now, but should probably be public
969wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
f135ff73 970{
1ed01484
JS
971 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
972
973 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
974
975 // First see if there are any children.
976 wxArrayGenericTreeItems& children = i->GetChildren();
977 if (children.GetCount() > 0)
978 {
979 return children.Item(0);
980 }
981 else
982 {
983 // Try a sibling of this or ancestor instead
984 wxTreeItemId p = item;
985 wxTreeItemId toFind;
986 do
987 {
988 toFind = GetNextSibling(p);
989 p = GetParent(p);
990 } while (p.IsOk() && !toFind.IsOk());
991 return toFind;
992 }
993}
994
995wxTreeItemId wxGenericTreeCtrl::GetPrev(const wxTreeItemId& item) const
996{
997 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
998
223d09f6 999 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1000
1e6d9499 1001 return wxTreeItemId();
f135ff73 1002}
29d87bba 1003
1ed01484
JS
1004wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1005{
1006 wxTreeItemId id = GetRootItem();
1007 if (!id.IsOk())
1008 return id;
1009
1010 do
1011 {
1012 if (IsVisible(id))
1013 return id;
1014 id = GetNext(id);
1015 } while (id.IsOk());
1016
1017 return wxTreeItemId();
1018}
1019
941830cb 1020wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
f135ff73 1021{
223d09f6 1022 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1023
1ed01484
JS
1024 wxTreeItemId id = item;
1025 while (id.IsOk())
1026 {
1027 id = GetNext(id);
29d87bba 1028
1ed01484
JS
1029 if (id.IsOk() && IsVisible(id))
1030 return id;
1031 }
1e6d9499 1032 return wxTreeItemId();
f135ff73 1033}
29d87bba 1034
941830cb 1035wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
f135ff73 1036{
223d09f6 1037 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1038
223d09f6 1039 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1040
1e6d9499 1041 return wxTreeItemId();
edaa81ae 1042}
c801d85f 1043
f135ff73
VZ
1044// -----------------------------------------------------------------------------
1045// operations
1046// -----------------------------------------------------------------------------
1047
941830cb 1048wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1049 size_t previous,
1050 const wxString& text,
1051 int image, int selImage,
1052 wxTreeItemData *data)
c801d85f 1053{
941830cb 1054 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f49f2b0c
RR
1055 if ( !parent )
1056 {
1057 // should we give a warning here?
1058 return AddRoot(text, image, selImage, data);
1059 }
4832f7c0 1060
f49f2b0c 1061 wxClientDC dc(this);
f38374d0 1062 wxGenericTreeItem *item =
f49f2b0c 1063 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
74bedbeb 1064
f49f2b0c
RR
1065 if ( data != NULL )
1066 {
40fab78b 1067 data->m_pItem = (long) item;
f49f2b0c 1068 }
74bedbeb 1069
f49f2b0c 1070 parent->Insert( item, previous );
ef44a621 1071
f49f2b0c 1072 m_dirty = TRUE;
389cdc7a 1073
f49f2b0c 1074 return item;
4c681997
RR
1075}
1076
941830cb 1077wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
f135ff73
VZ
1078 int image, int selImage,
1079 wxTreeItemData *data)
4c681997 1080{
f49f2b0c 1081 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 1082
f49f2b0c
RR
1083 wxClientDC dc(this);
1084 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
f135ff73 1085 image, selImage, data);
f49f2b0c
RR
1086 if ( data != NULL )
1087 {
40fab78b 1088 data->m_pItem = (long) m_anchor;
f49f2b0c 1089 }
f38374d0 1090
f49f2b0c
RR
1091 if (!HasFlag(wxTR_MULTIPLE))
1092 {
1093 m_current = m_key_current = m_anchor;
06b466c7 1094 m_current->SetHilight( TRUE );
f49f2b0c 1095 }
389cdc7a 1096
f6bcfd97 1097 m_dirty = TRUE;
a32dd690 1098
f49f2b0c 1099 return m_anchor;
edaa81ae 1100}
c801d85f 1101
941830cb 1102wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
f135ff73
VZ
1103 const wxString& text,
1104 int image, int selImage,
1105 wxTreeItemData *data)
c801d85f 1106{
f2593d0d 1107 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 1108}
c801d85f 1109
941830cb 1110wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1111 const wxTreeItemId& idPrevious,
1112 const wxString& text,
1113 int image, int selImage,
1114 wxTreeItemData *data)
c801d85f 1115{
941830cb 1116 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1117 if ( !parent )
1118 {
1119 // should we give a warning here?
1120 return AddRoot(text, image, selImage, data);
1121 }
c801d85f 1122
941830cb 1123 int index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
f2593d0d 1124 wxASSERT_MSG( index != wxNOT_FOUND,
941830cb 1125 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
06b466c7 1126
f2593d0d
RR
1127 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1128}
1129
941830cb 1130wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f2593d0d
RR
1131 size_t before,
1132 const wxString& text,
1133 int image, int selImage,
1134 wxTreeItemData *data)
1135{
941830cb 1136 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1137 if ( !parent )
1138 {
1139 // should we give a warning here?
1140 return AddRoot(text, image, selImage, data);
1141 }
1142
1143 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 1144}
c801d85f 1145
941830cb 1146wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
f135ff73
VZ
1147 const wxString& text,
1148 int image, int selImage,
1149 wxTreeItemData *data)
74bedbeb 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 }
f135ff73 1157
f2593d0d
RR
1158 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1159 image, selImage, data);
74bedbeb
VZ
1160}
1161
941830cb 1162void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
a43a4f9d 1163{
f2593d0d 1164 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
40fab78b 1165 event.m_item = (long) item;
f2593d0d
RR
1166 event.SetEventObject( this );
1167 ProcessEvent( event );
a43a4f9d
VZ
1168}
1169
941830cb 1170void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
372edb9d 1171{
941830cb 1172 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
a43a4f9d 1173 item->DeleteChildren(this);
372edb9d
VZ
1174
1175 m_dirty = TRUE;
1176}
1177
941830cb 1178void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1179{
941830cb 1180 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
ff5bf259 1181
aaa2f297
VZ
1182 // don't stay with invalid m_key_current or we will crash in the next call
1183 // to OnChar()
1184 bool changeKeyCurrent = FALSE;
1185 wxGenericTreeItem *itemKey = m_key_current;
1186 while ( itemKey && !changeKeyCurrent )
1187 {
1188 if ( itemKey == item )
1189 {
1190 // m_key_current is a descendant of the item being deleted
1191 changeKeyCurrent = TRUE;
1192 }
1193 else
1194 {
1195 itemKey = itemKey->GetParent();
1196 }
1197 }
1198
1199 wxGenericTreeItem *parent = item->GetParent();
97d7bfb8
RR
1200 if ( parent )
1201 {
1202 parent->GetChildren().Remove( item ); // remove by value
1203 }
f135ff73 1204
aaa2f297
VZ
1205 if ( changeKeyCurrent )
1206 {
1207 // may be NULL or not
1208 m_key_current = parent;
1209 }
1210
97d7bfb8
RR
1211 item->DeleteChildren(this);
1212 SendDeleteEvent(item);
1213 delete item;
f135ff73 1214
97d7bfb8 1215 m_dirty = TRUE;
edaa81ae 1216}
c801d85f 1217
941830cb 1218void wxGenericTreeCtrl::DeleteAllItems()
c801d85f 1219{
97d7bfb8
RR
1220 if ( m_anchor )
1221 {
1222 m_anchor->DeleteChildren(this);
1223 delete m_anchor;
a43a4f9d 1224
97d7bfb8 1225 m_anchor = NULL;
f135ff73 1226
97d7bfb8
RR
1227 m_dirty = TRUE;
1228 }
edaa81ae
RR
1229}
1230
941830cb 1231void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1232{
941830cb 1233 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1234
941830cb 1235 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
f6bcfd97 1236
f2593d0d
RR
1237 if ( !item->HasPlus() )
1238 return;
978f38c2 1239
f2593d0d
RR
1240 if ( item->IsExpanded() )
1241 return;
f135ff73 1242
f2593d0d 1243 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
40fab78b 1244 event.m_item = (long) item;
f2593d0d 1245 event.SetEventObject( this );
004fd0c8 1246
f2593d0d
RR
1247 if ( ProcessEvent( event ) && !event.IsAllowed() )
1248 {
1249 // cancelled by program
1250 return;
1251 }
4832f7c0 1252
f2593d0d
RR
1253 item->Expand();
1254 CalculatePositions();
f135ff73 1255
f2593d0d 1256 RefreshSubtree(item);
f135ff73 1257
f2593d0d
RR
1258 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1259 ProcessEvent( event );
edaa81ae
RR
1260}
1261
941830cb 1262void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
f6bcfd97
BP
1263{
1264 Expand(item);
1265 if ( IsExpanded(item) )
1266 {
1267 long cookie;
1268 wxTreeItemId child = GetFirstChild(item, cookie);
1269 while ( child.IsOk() )
1270 {
1271 ExpandAll(child);
1272
1273 child = GetNextChild(item, cookie);
1274 }
1275 }
1276}
1277
941830cb 1278void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1279{
941830cb 1280 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1281
f2593d0d
RR
1282 if ( !item->IsExpanded() )
1283 return;
f135ff73 1284
f2593d0d 1285 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
40fab78b 1286 event.m_item = (long) item;
f2593d0d
RR
1287 event.SetEventObject( this );
1288 if ( ProcessEvent( event ) && !event.IsAllowed() )
1289 {
1290 // cancelled by program
1291 return;
1292 }
4832f7c0 1293
f2593d0d 1294 item->Collapse();
f135ff73 1295
f2593d0d
RR
1296 wxArrayGenericTreeItems& children = item->GetChildren();
1297 size_t count = children.Count();
1298 for ( size_t n = 0; n < count; n++ )
1299 {
1300 Collapse(children[n]);
1301 }
f135ff73 1302
f2593d0d 1303 CalculatePositions();
f135ff73 1304
f2593d0d 1305 RefreshSubtree(item);
f135ff73 1306
f2593d0d
RR
1307 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1308 ProcessEvent( event );
edaa81ae 1309}
c801d85f 1310
941830cb 1311void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1312{
00e12320
RR
1313 Collapse(item);
1314 DeleteChildren(item);
edaa81ae 1315}
c801d85f 1316
941830cb 1317void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1318{
941830cb 1319 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
389cdc7a 1320
00e12320
RR
1321 if (item->IsExpanded())
1322 Collapse(itemId);
1323 else
1324 Expand(itemId);
f135ff73 1325}
389cdc7a 1326
941830cb 1327void wxGenericTreeCtrl::Unselect()
f135ff73 1328{
00e12320
RR
1329 if (m_current)
1330 {
1331 m_current->SetHilight( FALSE );
1332 RefreshLine( m_current );
1333 }
edaa81ae 1334}
c801d85f 1335
941830cb 1336void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1337{
00e12320
RR
1338 if (item->IsSelected())
1339 {
1340 item->SetHilight(FALSE);
1341 RefreshLine(item);
1342 }
d3a9f4af 1343
00e12320 1344 if (item->HasChildren())
88ac883a 1345 {
00e12320
RR
1346 wxArrayGenericTreeItems& children = item->GetChildren();
1347 size_t count = children.Count();
1348 for ( size_t n = 0; n < count; ++n )
1349 {
1350 UnselectAllChildren(children[n]);
1351 }
88ac883a
VZ
1352 }
1353}
f135ff73 1354
941830cb 1355void wxGenericTreeCtrl::UnselectAll()
88ac883a 1356{
941830cb 1357 UnselectAllChildren((wxGenericTreeItem*) GetRootItem().m_pItem);
88ac883a
VZ
1358}
1359
1360// Recursive function !
1361// To stop we must have crt_item<last_item
91b8de8d 1362// Algorithm :
88ac883a 1363// Tag all next children, when no more children,
d3a9f4af 1364// Move to parent (not to tag)
91b8de8d 1365// Keep going... if we found last_item, we stop.
941830cb 1366bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a
VZ
1367{
1368 wxGenericTreeItem *parent = crt_item->GetParent();
1369
00e12320
RR
1370 if (parent == NULL) // This is root item
1371 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1372
91b8de8d 1373 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1374 int index = children.Index(crt_item);
1375 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1376
1377 size_t count = children.Count();
1378 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1379 {
1380 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1381 }
88ac883a
VZ
1382
1383 return TagNextChildren(parent, last_item, select);
1384}
1385
941830cb 1386bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a 1387{
00e12320
RR
1388 crt_item->SetHilight(select);
1389 RefreshLine(crt_item);
d3a9f4af 1390
06b466c7 1391 if (crt_item==last_item)
00e12320 1392 return TRUE;
88ac883a 1393
00e12320 1394 if (crt_item->HasChildren())
88ac883a 1395 {
00e12320
RR
1396 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1397 size_t count = children.Count();
1398 for ( size_t n = 0; n < count; ++n )
1399 {
06b466c7 1400 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1401 return TRUE;
06b466c7 1402 }
88ac883a 1403 }
d3a9f4af 1404
c0de7af4 1405 return FALSE;
88ac883a
VZ
1406}
1407
941830cb 1408void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
88ac883a 1409{
f2593d0d
RR
1410 // item2 is not necessary after item1
1411 wxGenericTreeItem *first=NULL, *last=NULL;
88ac883a 1412
f2593d0d
RR
1413 // choice first' and 'last' between item1 and item2
1414 if (item1->GetY()<item2->GetY())
06b466c7 1415 {
f2593d0d
RR
1416 first=item1;
1417 last=item2;
1418 }
1419 else
1420 {
1421 first=item2;
1422 last=item1;
1423 }
88ac883a 1424
f2593d0d 1425 bool select = m_current->IsSelected();
d3a9f4af 1426
f2593d0d
RR
1427 if ( TagAllChildrenUntilLast(first,last,select) )
1428 return;
88ac883a 1429
f2593d0d 1430 TagNextChildren(first,last,select);
88ac883a
VZ
1431}
1432
941830cb 1433void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
c193b707
VZ
1434 bool unselect_others,
1435 bool extended_select)
d3a9f4af 1436{
223d09f6 1437 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1438
88ac883a 1439 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
941830cb 1440 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
88ac883a
VZ
1441
1442 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1443 // wxT("this is a single selection tree") );
88ac883a
VZ
1444
1445 // to keep going anyhow !!!
d3a9f4af 1446 if (is_single)
8dc99046
VZ
1447 {
1448 if (item->IsSelected())
1449 return; // nothing to do
1450 unselect_others = TRUE;
1451 extended_select = FALSE;
1452 }
1453 else if ( unselect_others && item->IsSelected() )
1454 {
1455 // selection change if there is more than one item currently selected
1456 wxArrayTreeItemIds selected_items;
1457 if ( GetSelections(selected_items) == 1 )
1458 return;
1459 }
d3a9f4af 1460
f135ff73 1461 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
40fab78b
JS
1462 event.m_item = (long) item;
1463 event.m_itemOld = (long) m_current;
f135ff73 1464 event.SetEventObject( this );
91b8de8d 1465 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1466
f98e2558 1467 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
f135ff73
VZ
1468 return;
1469
88ac883a
VZ
1470 // ctrl press
1471 if (unselect_others)
389cdc7a 1472 {
88ac883a 1473 if (is_single) Unselect(); // to speed up thing
c193b707 1474 else UnselectAll();
edaa81ae 1475 }
f135ff73 1476
88ac883a 1477 // shift press
d3a9f4af 1478 if (extended_select)
88ac883a 1479 {
aaa2f297
VZ
1480 if ( !m_current )
1481 {
1482 m_current =
941830cb 1483 m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
1484 }
1485
88ac883a
VZ
1486 // don't change the mark (m_current)
1487 SelectItemRange(m_current, item);
1488 }
1489 else
1490 {
c0de7af4 1491 bool select=TRUE; // the default
88ac883a 1492
c193b707
VZ
1493 // Check if we need to toggle hilight (ctrl mode)
1494 if (!unselect_others)
8dc99046 1495 select=!item->IsSelected();
88ac883a 1496
91b8de8d 1497 m_current = m_key_current = item;
c193b707
VZ
1498 m_current->SetHilight(select);
1499 RefreshLine( m_current );
88ac883a 1500 }
389cdc7a 1501
f135ff73 1502 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1503 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1504}
1505
941830cb 1506void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
9dfbf520 1507 wxArrayTreeItemIds &array) const
c801d85f 1508{
8dc99046 1509 if ( item->IsSelected() )
9dfbf520 1510 array.Add(wxTreeItemId(item));
91b8de8d 1511
9dfbf520 1512 if ( item->HasChildren() )
91b8de8d 1513 {
9dfbf520
VZ
1514 wxArrayGenericTreeItems& children = item->GetChildren();
1515 size_t count = children.GetCount();
1516 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1517 FillArray(children[n], array);
91b8de8d
RR
1518 }
1519}
1520
941830cb 1521size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d
RR
1522{
1523 array.Empty();
f6bcfd97
BP
1524 wxTreeItemId idRoot = GetRootItem();
1525 if ( idRoot.IsOk() )
1526 {
941830cb 1527 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
f6bcfd97
BP
1528 }
1529 //else: the tree is empty, so no selections
91b8de8d
RR
1530
1531 return array.Count();
1532}
1533
941830cb 1534void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1535{
91b8de8d
RR
1536 if (!item.IsOk()) return;
1537
941830cb 1538 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 1539
f65635b5
VZ
1540 // first expand all parent branches
1541 wxGenericTreeItem *parent = gitem->GetParent();
5391f772 1542 while ( parent )
f65635b5 1543 {
8dc99046 1544 Expand(parent);
f65635b5
VZ
1545 parent = parent->GetParent();
1546 }
1547
5391f772 1548 //if (parent) CalculatePositions();
91b8de8d
RR
1549
1550 ScrollTo(item);
1551}
1552
941830cb 1553void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d
RR
1554{
1555 if (!item.IsOk()) return;
1556
dc6c62a9
RR
1557 // We have to call this here because the label in
1558 // question might just have been added and no screen
1559 // update taken place.
cd66f45b 1560 if (m_dirty) wxYieldIfNeeded();
dc6c62a9 1561
941830cb 1562 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1563
f65635b5 1564 // now scroll to the item
0659e7ee 1565 int item_y = gitem->GetY();
8dc99046 1566
0659e7ee
RR
1567 int start_x = 0;
1568 int start_y = 0;
1569 ViewStart( &start_x, &start_y );
91b8de8d 1570 start_y *= PIXELS_PER_UNIT;
978f38c2 1571
a93109d5
RR
1572 int client_h = 0;
1573 int client_w = 0;
1574 GetClientSize( &client_w, &client_h );
ef44a621 1575
0659e7ee
RR
1576 if (item_y < start_y+3)
1577 {
91b8de8d 1578 // going down
0659e7ee
RR
1579 int x = 0;
1580 int y = 0;
91b8de8d
RR
1581 m_anchor->GetSize( x, y, this );
1582 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1583 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1584 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1585 // Item should appear at top
91b8de8d 1586 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1587 }
91b8de8d 1588 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1589 {
c7a9fa36
RR
1590 // going up
1591 int x = 0;
1592 int y = 0;
1593 m_anchor->GetSize( x, y, this );
1594 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1595 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1596 item_y += PIXELS_PER_UNIT+2;
1597 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1598 // Item should appear at bottom
c7a9fa36 1599 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 1600 }
edaa81ae 1601}
c801d85f 1602
e1ee62bd 1603// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 1604static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1605
004fd0c8 1606static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1607 wxGenericTreeItem **item2)
edaa81ae 1608{
941830cb 1609 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1610
1611 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1612}
1613
941830cb 1614int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
e1ee62bd 1615 const wxTreeItemId& item2)
0659e7ee 1616{
87138c52 1617 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1618}
1619
941830cb 1620void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 1621{
223d09f6 1622 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 1623
941830cb 1624 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 1625
e1ee62bd 1626 wxCHECK_RET( !s_treeBeingSorted,
941830cb 1627 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1628
91b8de8d 1629 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1630 if ( children.Count() > 1 )
1631 {
1632 s_treeBeingSorted = this;
1633 children.Sort(tree_ctrl_compare_func);
1634 s_treeBeingSorted = NULL;
978f38c2 1635
e1ee62bd
VZ
1636 m_dirty = TRUE;
1637 }
1638 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1639}
1640
941830cb 1641wxImageList *wxGenericTreeCtrl::GetImageList() const
edaa81ae 1642{
f135ff73 1643 return m_imageListNormal;
edaa81ae
RR
1644}
1645
941830cb 1646wxImageList *wxGenericTreeCtrl::GetStateImageList() const
c801d85f 1647{
f135ff73 1648 return m_imageListState;
edaa81ae 1649}
c801d85f 1650
941830cb 1651void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
e2414cbe 1652{
46cd520d
VS
1653 if (m_ownsImageListNormal) delete m_imageListNormal;
1654
f2593d0d 1655 m_imageListNormal = imageList;
46cd520d 1656 m_ownsImageListNormal = FALSE;
91b8de8d 1657
2c8e4738
VZ
1658 if ( !m_imageListNormal )
1659 return;
1660
f2593d0d 1661 // Calculate a m_lineHeight value from the image sizes.
941830cb 1662 // May be toggle off. Then wxGenericTreeCtrl will spread when
f2593d0d 1663 // necessary (which might look ugly).
f2593d0d
RR
1664 wxClientDC dc(this);
1665 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1666 int width = 0, height = 0,
1667 n = m_imageListNormal->GetImageCount();
06b466c7 1668
f2593d0d
RR
1669 for (int i = 0; i < n ; i++)
1670 {
1671 m_imageListNormal->GetSize(i, width, height);
1672 if (height > m_lineHeight) m_lineHeight = height;
1673 }
91b8de8d 1674
06b466c7 1675 if (m_lineHeight < 40)
f2593d0d 1676 m_lineHeight += 2; // at least 2 pixels
06b466c7 1677 else
f2593d0d 1678 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 1679}
e2414cbe 1680
941830cb 1681void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1682{
46cd520d 1683 if (m_ownsImageListState) delete m_imageListState;
f135ff73 1684 m_imageListState = imageList;
46cd520d
VS
1685 m_ownsImageListState = FALSE;
1686}
1687
1688void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
1689{
1690 SetImageList(imageList);
1691 m_ownsImageListNormal = TRUE;
1692}
1693
1694void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
1695{
1696 SetStateImageList(imageList);
1697 m_ownsImageListState = TRUE;
edaa81ae 1698}
e2414cbe 1699
f135ff73
VZ
1700// -----------------------------------------------------------------------------
1701// helpers
1702// -----------------------------------------------------------------------------
0659e7ee 1703
941830cb 1704void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 1705{
0659e7ee
RR
1706 if (m_anchor)
1707 {
1708 int x = 0;
1709 int y = 0;
91b8de8d 1710 m_anchor->GetSize( x, y, this );
c193b707 1711 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1712 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1713 int x_pos = GetScrollPos( wxHORIZONTAL );
1714 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1715 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1716 }
1717 else
1718 {
1719 SetScrollbars( 0, 0, 0, 0 );
1720 }
edaa81ae 1721}
c801d85f 1722
941830cb 1723int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 1724{
dc6c62a9
RR
1725 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1726 return item->GetHeight();
1727 else
1728 return m_lineHeight;
91b8de8d
RR
1729}
1730
941830cb 1731void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 1732{
9ec64fa7
VZ
1733 wxTreeItemAttr *attr = item->GetAttributes();
1734 if ( attr && attr->HasFont() )
1735 dc.SetFont(attr->GetFont());
1736 else if (item->IsBold())
eff869aa 1737 dc.SetFont(m_boldFont);
ef44a621 1738
bbe0af5b
RR
1739 long text_w = 0;
1740 long text_h = 0;
1741 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1742
bbe0af5b
RR
1743 int image_h = 0;
1744 int image_w = 0;
8dc99046
VZ
1745 int image = item->GetCurrentImage();
1746 if ( image != NO_IMAGE )
bbe0af5b 1747 {
2c8e4738
VZ
1748 if ( m_imageListNormal )
1749 {
1750 m_imageListNormal->GetSize( image, image_w, image_h );
1751 image_w += 4;
1752 }
1753 else
1754 {
1755 image = NO_IMAGE;
1756 }
bbe0af5b 1757 }
ef44a621 1758
91b8de8d
RR
1759 int total_h = GetLineHeight(item);
1760
c0fba4d1
VS
1761 if (item->IsSelected())
1762 dc.SetBrush(*m_hilightBrush);
afa6a1a1 1763 else
c0fba4d1
VS
1764 {
1765 wxColour colBg;
1766 if ( attr && attr->HasBackgroundColour() )
1767 colBg = attr->GetBackgroundColour();
1768 else
1769 colBg = m_backgroundColour;
1770 dc.SetBrush(wxBrush(colBg, wxSOLID));
1771 }
afa6a1a1 1772
a69cb1cc
JS
1773 if (item->IsSelected() && image != NO_IMAGE)
1774 {
1775 // If it's selected, and there's an image, then we should
1776 // take care to leave the area under the image painted in the
1777 // background colour.
1778 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY(), item->GetWidth() - image_w + 2, total_h );
1779 }
1780 else
1781 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
ef44a621 1782
8dc99046 1783 if ( image != NO_IMAGE )
bbe0af5b 1784 {
d30b4d20 1785 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 1786 m_imageListNormal->Draw( image, dc,
49cd56ef 1787 item->GetX(),
d701d432 1788 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1789 wxIMAGELIST_DRAW_TRANSPARENT );
1790 dc.DestroyClippingRegion();
1791 }
ef44a621 1792
afa6a1a1 1793 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
1794 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
1795 dc.DrawText( item->GetText(),
1796 (wxCoord)(image_w + item->GetX()),
1797 (wxCoord)(item->GetY() + extraH));
ef44a621 1798
eff869aa
RR
1799 // restore normal font
1800 dc.SetFont( m_normalFont );
ef44a621
VZ
1801}
1802
91b8de8d 1803// Now y stands for the top of the item, whereas it used to stand for middle !
941830cb 1804void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1805{
bbe0af5b 1806 int horizX = level*m_indent;
389cdc7a 1807
cf724bce 1808 item->SetX( horizX+m_indent+m_spacing );
91b8de8d 1809 item->SetY( y );
4c681997 1810
bbe0af5b 1811 int oldY = y;
91b8de8d
RR
1812 y+=GetLineHeight(item)/2;
1813
1814 item->SetCross( horizX+m_indent, y );
389cdc7a 1815
bbe0af5b 1816 int exposed_x = dc.LogicalToDeviceX( 0 );
5391f772 1817 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
4832f7c0 1818
233058c7
JS
1819 bool drawLines = ((GetWindowStyle() & wxTR_NO_LINES) == 0);
1820
5391f772 1821 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
bbe0af5b
RR
1822 {
1823 int startX = horizX;
cf724bce 1824 int endX = horizX + (m_indent-5);
29d87bba 1825
cf724bce 1826// if (!item->HasChildren()) endX += (m_indent+5);
bbe0af5b 1827 if (!item->HasChildren()) endX += 20;
4832f7c0 1828
233058c7
JS
1829 if (drawLines)
1830 dc.DrawLine( startX, y, endX, y );
29d87bba 1831
bbe0af5b
RR
1832 if (item->HasPlus())
1833 {
233058c7
JS
1834 if (drawLines)
1835 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
bbe0af5b
RR
1836 dc.SetPen( *wxGREY_PEN );
1837 dc.SetBrush( *wxWHITE_BRUSH );
cf724bce 1838 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
9dfbf520 1839
bbe0af5b 1840 dc.SetPen( *wxBLACK_PEN );
cf724bce 1841 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
bbe0af5b 1842 if (!item->IsExpanded())
cf724bce 1843 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
9dfbf520 1844
112c5086 1845 dc.SetPen( m_dottedPen );
bbe0af5b 1846 }
c801d85f 1847
9ec64fa7 1848 wxPen *pen = wxTRANSPARENT_PEN;
9ec64fa7 1849 wxColour colText;
a367b9b3 1850
9ec64fa7
VZ
1851 if ( item->IsSelected() )
1852 {
1853 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
4832f7c0 1854
9ec64fa7
VZ
1855 if ( m_hasFocus )
1856 pen = wxBLACK_PEN;
f135ff73 1857
bbe0af5b
RR
1858 }
1859 else
1860 {
9ec64fa7
VZ
1861 wxTreeItemAttr *attr = item->GetAttributes();
1862 if ( attr && attr->HasTextColour() )
1863 colText = attr->GetTextColour();
1864 else
37d403aa 1865 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT );
bbe0af5b 1866 }
9ec64fa7
VZ
1867
1868 // prepare to draw
1869 dc.SetTextForeground(colText);
1870 dc.SetPen(*pen);
9ec64fa7
VZ
1871
1872 // draw
1873 PaintItem(item, dc);
1874
1875 // restore DC objects
1876 dc.SetBrush( *wxWHITE_BRUSH );
1877 dc.SetPen( m_dottedPen );
1878 dc.SetTextForeground( *wxBLACK );
f135ff73 1879 }
d3a9f4af 1880
91b8de8d 1881 y = oldY+GetLineHeight(item);
e2414cbe 1882
bbe0af5b
RR
1883 if (item->IsExpanded())
1884 {
91b8de8d 1885 oldY+=GetLineHeight(item)/2;
9ec64fa7 1886 int semiOldY=0;
389cdc7a 1887
91b8de8d
RR
1888 wxArrayGenericTreeItems& children = item->GetChildren();
1889 size_t n, count = children.Count();
1890 for ( n = 0; n < count; ++n )
c193b707
VZ
1891 {
1892 semiOldY=y;
1893 PaintLevel( children[n], dc, level+1, y );
1894 }
389cdc7a 1895
f65635b5
VZ
1896 // it may happen that the item is expanded but has no items (when you
1897 // delete all its children for example) - don't draw the vertical line
1898 // in this case
1899 if (count > 0)
9ec64fa7 1900 {
c193b707 1901 semiOldY+=GetLineHeight(children[--n])/2;
233058c7
JS
1902 if (drawLines)
1903 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
9ec64fa7 1904 }
bbe0af5b 1905 }
4c681997 1906}
c801d85f 1907
941830cb 1908void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
1909{
1910 if ( item )
1911 {
1912 if ( item->HasPlus() )
1913 {
1914 // it's a folder, indicate it by a border
1915 DrawBorder(item);
1916 }
1917 else
1918 {
1919 // draw a line under the drop target because the item will be
1920 // dropped there
1921 DrawLine(item, TRUE /* below */);
1922 }
1923
1924 SetCursor(wxCURSOR_BULLSEYE);
1925 }
1926 else
1927 {
1928 // can't drop here
1929 SetCursor(wxCURSOR_NO_ENTRY);
1930 }
1931}
1932
941830cb 1933void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 1934{
941830cb 1935 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 1936
941830cb 1937 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1938
1044a386 1939 wxClientDC dc(this);
91b8de8d
RR
1940 PrepareDC( dc );
1941 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 1942 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 1943
3dbeaa52
VZ
1944 int w = i->GetWidth() + 2;
1945 int h = GetLineHeight(i) + 2;
91b8de8d 1946
3dbeaa52 1947 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
1948}
1949
941830cb 1950void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 1951{
941830cb 1952 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 1953
941830cb 1954 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1955
1044a386 1956 wxClientDC dc(this);
91b8de8d
RR
1957 PrepareDC( dc );
1958 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 1959
3dbeaa52
VZ
1960 int x = i->GetX(),
1961 y = i->GetY();
1962 if ( below )
1963 {
1964 y += GetLineHeight(i) - 1;
1965 }
91b8de8d 1966
3dbeaa52 1967 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
1968}
1969
f135ff73
VZ
1970// -----------------------------------------------------------------------------
1971// wxWindows callbacks
1972// -----------------------------------------------------------------------------
1973
941830cb 1974void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 1975{
0659e7ee
RR
1976 wxPaintDC dc(this);
1977 PrepareDC( dc );
29d87bba 1978
941830cb
JS
1979 if ( !m_anchor)
1980 return;
1981
eff869aa 1982 dc.SetFont( m_normalFont );
0659e7ee 1983 dc.SetPen( m_dottedPen );
f38374d0 1984
eff869aa 1985 // this is now done dynamically
91b8de8d
RR
1986 //if(GetImageList() == NULL)
1987 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1988
91b8de8d 1989 int y = 2;
0659e7ee 1990 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 1991}
c801d85f 1992
941830cb 1993void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1994{
0659e7ee 1995 m_hasFocus = TRUE;
978f38c2 1996
bbe0af5b 1997 if (m_current) RefreshLine( m_current );
edaa81ae 1998}
c801d85f 1999
941830cb 2000void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 2001{
0659e7ee 2002 m_hasFocus = FALSE;
978f38c2 2003
bbe0af5b 2004 if (m_current) RefreshLine( m_current );
edaa81ae 2005}
c801d85f 2006
941830cb 2007void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
c801d85f 2008{
978f38c2 2009 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
06b466c7 2010 te.m_code = (int)event.KeyCode();
978f38c2
VZ
2011 te.SetEventObject( this );
2012 GetEventHandler()->ProcessEvent( te );
435fe83e 2013
91b8de8d 2014 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
2015 {
2016 event.Skip();
2017 return;
2018 }
ef44a621 2019
06b466c7
VZ
2020 // how should the selection work for this event?
2021 bool is_multiple, extended_select, unselect_others;
2022 EventFlagsToSelType(GetWindowStyleFlag(),
2023 event.ShiftDown(),
2024 event.ControlDown(),
dfc6cd93
SB
2025 is_multiple, extended_select, unselect_others);
2026
2027 // + : Expand
2028 // - : Collaspe
f6bcfd97 2029 // * : Expand all/Collapse all
dfc6cd93
SB
2030 // ' ' | return : activate
2031 // up : go up (not last children!)
2032 // down : go down
2033 // left : go to parent
2034 // right : open if parent and go next
2035 // home : go to root
2036 // end : go to last item without opening parents
978f38c2
VZ
2037 switch (event.KeyCode())
2038 {
2039 case '+':
2040 case WXK_ADD:
2041 if (m_current->HasPlus() && !IsExpanded(m_current))
2042 {
2043 Expand(m_current);
2044 }
2045 break;
ef44a621 2046
f6bcfd97
BP
2047 case '*':
2048 case WXK_MULTIPLY:
2049 if ( !IsExpanded(m_current) )
2050 {
2051 // expand all
2052 ExpandAll(m_current);
2053 break;
2054 }
2055 //else: fall through to Collapse() it
2056
978f38c2
VZ
2057 case '-':
2058 case WXK_SUBTRACT:
2059 if (IsExpanded(m_current))
2060 {
2061 Collapse(m_current);
2062 }
2063 break;
ef44a621 2064
978f38c2
VZ
2065 case ' ':
2066 case WXK_RETURN:
2067 {
2068 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
40fab78b 2069 event.m_item = (long) m_current;
978f38c2
VZ
2070 event.m_code = 0;
2071 event.SetEventObject( this );
2072 GetEventHandler()->ProcessEvent( event );
2073 }
2074 break;
ef44a621 2075
978f38c2
VZ
2076 // up goes to the previous sibling or to the last of its children if
2077 // it's expanded
2078 case WXK_UP:
2079 {
91b8de8d 2080 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
2081 if (!prev)
2082 {
91b8de8d 2083 prev = GetParent( m_key_current );
c193b707
VZ
2084 if (prev)
2085 {
90e58684 2086 long cockie = 0;
91b8de8d 2087 wxTreeItemId current = m_key_current;
90e58684
RR
2088 if (current == GetFirstChild( prev, cockie ))
2089 {
2090 // otherwise we return to where we came from
88ac883a 2091 SelectItem( prev, unselect_others, extended_select );
941830cb 2092 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
c193b707 2093 EnsureVisible( prev );
90e58684 2094 break;
c193b707 2095 }
978f38c2
VZ
2096 }
2097 }
2098 if (prev)
2099 {
69a282d4 2100 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 2101 {
69a282d4
VZ
2102 wxTreeItemId child = GetLastChild(prev);
2103 if ( child )
2104 {
2105 prev = child;
2106 }
978f38c2 2107 }
69a282d4 2108
88ac883a 2109 SelectItem( prev, unselect_others, extended_select );
941830cb 2110 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
2111 EnsureVisible( prev );
2112 }
2113 }
2114 break;
ef44a621 2115
978f38c2
VZ
2116 // left arrow goes to the parent
2117 case WXK_LEFT:
2118 {
2119 wxTreeItemId prev = GetParent( m_current );
2120 if (prev)
2121 {
2122 EnsureVisible( prev );
88ac883a 2123 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2124 }
2125 }
2126 break;
ef44a621 2127
978f38c2
VZ
2128 case WXK_RIGHT:
2129 // this works the same as the down arrow except that we also expand the
2130 // item if it wasn't expanded yet
2131 Expand(m_current);
2132 // fall through
2133
2134 case WXK_DOWN:
ef44a621 2135 {
91b8de8d 2136 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
2137 {
2138 long cookie = 0;
91b8de8d 2139 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 2140 SelectItem( child, unselect_others, extended_select );
941830cb 2141 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
2142 EnsureVisible( child );
2143 }
2144 else
2145 {
91b8de8d 2146 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2147 if (!next)
978f38c2 2148 {
91b8de8d 2149 wxTreeItemId current = m_key_current;
978f38c2
VZ
2150 while (current && !next)
2151 {
2152 current = GetParent( current );
2153 if (current) next = GetNextSibling( current );
2154 }
2155 }
b62c3631 2156 if (next)
978f38c2 2157 {
88ac883a 2158 SelectItem( next, unselect_others, extended_select );
941830cb 2159 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
2160 EnsureVisible( next );
2161 }
2162 }
ef44a621 2163 }
978f38c2 2164 break;
ef44a621 2165
978f38c2
VZ
2166 // <End> selects the last visible tree item
2167 case WXK_END:
2168 {
2169 wxTreeItemId last = GetRootItem();
2170
2171 while ( last.IsOk() && IsExpanded(last) )
2172 {
2173 wxTreeItemId lastChild = GetLastChild(last);
2174
2175 // it may happen if the item was expanded but then all of
2176 // its children have been deleted - so IsExpanded() returned
2177 // TRUE, but GetLastChild() returned invalid item
2178 if ( !lastChild )
2179 break;
2180
2181 last = lastChild;
2182 }
2183
2184 if ( last.IsOk() )
2185 {
2186 EnsureVisible( last );
88ac883a 2187 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2188 }
2189 }
2190 break;
2191
2192 // <Home> selects the root item
2193 case WXK_HOME:
2194 {
2195 wxTreeItemId prev = GetRootItem();
2196 if (prev)
2197 {
2198 EnsureVisible( prev );
88ac883a 2199 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2200 }
2201 }
2202 break;
2203
2204 default:
2205 event.Skip();
2206 }
edaa81ae 2207}
c801d85f 2208
941830cb 2209wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2210{
dc6c62a9
RR
2211 // We have to call this here because the label in
2212 // question might just have been added and no screen
2213 // update taken place.
cd66f45b 2214 if (m_dirty) wxYieldIfNeeded();
dc6c62a9 2215
67a7abf7
VZ
2216 wxClientDC dc(this);
2217 PrepareDC(dc);
06b466c7
VZ
2218 wxCoord x = dc.DeviceToLogicalX( point.x );
2219 wxCoord y = dc.DeviceToLogicalY( point.y );
91b8de8d
RR
2220 int w, h;
2221 GetSize(&w, &h);
2222
2223 flags=0;
2224 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
2225 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
2226 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
2227 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
d3a9f4af 2228
c3c979c7
JS
2229 if (m_anchor)
2230 return m_anchor->HitTest( wxPoint(x, y), this, flags);
2231 else
2232 return wxTreeItemId();
4f22cf8d
RR
2233}
2234
8fb3bfe2
JS
2235// get the bounding rectangle of the item (or of its label only)
2236bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2237 wxRect& rect,
2238 bool textOnly) const
2239{
601c163b 2240 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
8fb3bfe2
JS
2241
2242 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2243
2244 int startX, startY;
2245 GetViewStart(& startX, & startY);
2246
1ed01484
JS
2247 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
2248 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
2249 rect.width = i->GetWidth();
2250 //rect.height = i->GetHeight();
2251 rect.height = GetLineHeight(i);
8fb3bfe2
JS
2252
2253 return TRUE;
8fb3bfe2
JS
2254}
2255
e179bd65
RR
2256/* **** */
2257
941830cb 2258void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
e179bd65
RR
2259{
2260 if (!item.IsOk()) return;
2261
941830cb 2262 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
9dfbf520 2263
e179bd65 2264 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
40fab78b 2265 te.m_item = (long) m_currentEdit;
e179bd65
RR
2266 te.SetEventObject( this );
2267 GetEventHandler()->ProcessEvent( te );
2268
2269 if (!te.IsAllowed()) return;
8dc99046 2270
dc6c62a9
RR
2271 // We have to call this here because the label in
2272 // question might just have been added and no screen
2273 // update taken place.
cd66f45b 2274 if (m_dirty) wxYieldIfNeeded();
9dfbf520 2275
e179bd65
RR
2276 wxString s = m_currentEdit->GetText();
2277 int x = m_currentEdit->GetX();
2278 int y = m_currentEdit->GetY();
2279 int w = m_currentEdit->GetWidth();
2280 int h = m_currentEdit->GetHeight();
9dfbf520 2281
5f1ea0ee
RR
2282 int image_h = 0;
2283 int image_w = 0;
8dc99046
VZ
2284
2285 int image = m_currentEdit->GetCurrentImage();
2286 if ( image != NO_IMAGE )
5f1ea0ee 2287 {
2c8e4738
VZ
2288 if ( m_imageListNormal )
2289 {
2290 m_imageListNormal->GetSize( image, image_w, image_h );
2291 image_w += 4;
2292 }
6359fa0e 2293 else
2c8e4738
VZ
2294 {
2295 wxFAIL_MSG(_T("you must create an image list to use images!"));
2296 }
5f1ea0ee
RR
2297 }
2298 x += image_w;
2299 w -= image_w + 4; // I don't know why +4 is needed
e179bd65
RR
2300
2301 wxClientDC dc(this);
2302 PrepareDC( dc );
2303 x = dc.LogicalToDeviceX( x );
2304 y = dc.LogicalToDeviceY( y );
2305
6359fa0e
VZ
2306 wxTreeTextCtrl *text = new wxTreeTextCtrl(this, -1,
2307 &m_renameAccept,
2308 &m_renameRes,
2309 this,
2310 s,
2311 wxPoint(x-4,y-4),
2312 wxSize(w+11,h+8));
e179bd65
RR
2313 text->SetFocus();
2314}
2315
941830cb 2316void wxGenericTreeCtrl::OnRenameTimer()
e179bd65
RR
2317{
2318 Edit( m_current );
2319}
2320
941830cb 2321void wxGenericTreeCtrl::OnRenameAccept()
e179bd65
RR
2322{
2323 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
40fab78b 2324 le.m_item = (long) m_currentEdit;
e179bd65
RR
2325 le.SetEventObject( this );
2326 le.m_label = m_renameRes;
2327 GetEventHandler()->ProcessEvent( le );
9dfbf520 2328
e179bd65 2329 if (!le.IsAllowed()) return;
9dfbf520 2330
5f1ea0ee 2331 SetItemText( m_currentEdit, m_renameRes );
e179bd65 2332}
9dfbf520 2333
941830cb 2334void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2335{
bbe0af5b 2336 if ( !m_anchor ) return;
978f38c2 2337
3dbeaa52
VZ
2338 // we process left mouse up event (enables in-place edit), right down
2339 // (pass to the user code), left dbl click (activate item) and
2340 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
2341 if ( !(event.LeftDown() ||
2342 event.LeftUp() ||
3dbeaa52
VZ
2343 event.RightDown() ||
2344 event.LeftDClick() ||
2345 event.Dragging() ||
2346 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2347 {
2348 event.Skip();
2349
2350 return;
2351 }
2352
bbe0af5b
RR
2353 wxClientDC dc(this);
2354 PrepareDC(dc);
06b466c7
VZ
2355 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2356 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
29d87bba 2357
3dbeaa52 2358 int flags = 0;
91b8de8d 2359 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
3dbeaa52 2360
3dbeaa52 2361 if ( event.Dragging() && !m_isDragging )
bbe0af5b 2362 {
fd9811b1 2363 if (m_dragCount == 0)
8dc99046
VZ
2364 m_dragStart = wxPoint(x,y);
2365
fd9811b1 2366 m_dragCount++;
8dc99046 2367
3dbeaa52
VZ
2368 if (m_dragCount != 3)
2369 {
2370 // wait until user drags a bit further...
2371 return;
2372 }
8dc99046 2373
3dbeaa52
VZ
2374 wxEventType command = event.RightIsDown()
2375 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2376 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 2377
fd9811b1 2378 wxTreeEvent nevent( command, GetId() );
40fab78b 2379 nevent.m_item = (long) m_current;
fd9811b1 2380 nevent.SetEventObject(this);
3dbeaa52
VZ
2381
2382 // by default the dragging is not supported, the user code must
2383 // explicitly allow the event for it to take place
2384 nevent.Veto();
2385
2386 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2387 {
2388 // we're going to drag this item
2389 m_isDragging = TRUE;
2390
b3a7510d
VZ
2391 // remember the old cursor because we will change it while
2392 // dragging
2393 m_oldCursor = m_cursor;
2394
2395 // in a single selection control, hide the selection temporarily
2396 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2397 {
941830cb 2398 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
2399
2400 if ( m_oldSelection )
2401 {
2402 m_oldSelection->SetHilight(FALSE);
2403 RefreshLine(m_oldSelection);
2404 }
2405 }
2406
3dbeaa52
VZ
2407 CaptureMouse();
2408 }
bbe0af5b 2409 }
3dbeaa52 2410 else if ( event.Moving() )
fd9811b1 2411 {
3dbeaa52
VZ
2412 if ( item != m_dropTarget )
2413 {
2414 // unhighlight the previous drop target
2415 DrawDropEffect(m_dropTarget);
fd9811b1 2416
3dbeaa52 2417 m_dropTarget = item;
978f38c2 2418
3dbeaa52
VZ
2419 // highlight the current drop target if any
2420 DrawDropEffect(m_dropTarget);
fb882e1c 2421
cd66f45b 2422 wxYieldIfNeeded();
3dbeaa52 2423 }
e179bd65 2424 }
3dbeaa52
VZ
2425 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2426 {
2427 // erase the highlighting
2428 DrawDropEffect(m_dropTarget);
9dfbf520 2429
3dbeaa52
VZ
2430 // generate the drag end event
2431 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 2432
40fab78b 2433 event.m_item = (long) item;
3dbeaa52
VZ
2434 event.m_pointDrag = wxPoint(x, y);
2435 event.SetEventObject(this);
91b8de8d 2436
3dbeaa52 2437 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 2438
3dbeaa52
VZ
2439 m_isDragging = FALSE;
2440 m_dropTarget = (wxGenericTreeItem *)NULL;
2441
b3a7510d
VZ
2442 if ( m_oldSelection )
2443 {
2444 m_oldSelection->SetHilight(TRUE);
2445 RefreshLine(m_oldSelection);
2446 m_oldSelection = (wxGenericTreeItem *)NULL;
2447 }
2448
3dbeaa52
VZ
2449 ReleaseMouse();
2450
b3a7510d 2451 SetCursor(m_oldCursor);
3dbeaa52 2452
cd66f45b 2453 wxYieldIfNeeded();
3dbeaa52
VZ
2454 }
2455 else
bbe0af5b 2456 {
3dbeaa52
VZ
2457 // here we process only the messages which happen on tree items
2458
2459 m_dragCount = 0;
2460
2461 if (item == NULL) return; /* we hit the blank area */
2462
2463 if ( event.RightDown() )
2464 {
2465 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
40fab78b 2466 nevent.m_item = (long) item;
3dbeaa52 2467 nevent.m_code = 0;
05a7f61d
VZ
2468 CalcScrolledPosition(x, y,
2469 &nevent.m_pointDrag.x,
2470 &nevent.m_pointDrag.y);
3dbeaa52
VZ
2471 nevent.SetEventObject(this);
2472 GetEventHandler()->ProcessEvent(nevent);
2473 }
80d2803f 2474 else if ( event.LeftUp() )
f6bcfd97 2475 {
80d2803f 2476 if ( m_lastOnSame )
f6bcfd97 2477 {
80d2803f
VZ
2478 if ( (item == m_current) &&
2479 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2480 HasFlag(wxTR_EDIT_LABELS) )
2481 {
bdb310a7
VZ
2482 if ( m_renameTimer->IsRunning() )
2483 m_renameTimer->Stop();
2484
80d2803f
VZ
2485 m_renameTimer->Start( 100, TRUE );
2486 }
f6bcfd97 2487
80d2803f
VZ
2488 m_lastOnSame = FALSE;
2489 }
3dbeaa52 2490 }
0326c494 2491 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3dbeaa52 2492 {
f6bcfd97
BP
2493 if ( event.LeftDown() )
2494 {
2495 m_lastOnSame = item == m_current;
2496 }
2497
0326c494
VZ
2498 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
2499 {
2500 // only toggle the item for a single click, double click on
2501 // the button doesn't do anything (it toggles the item twice)
2502 if ( event.LeftDown() )
2503 {
2504 Toggle( item );
2505 }
2506
2507 // don't select the item if the button was clicked
2508 return;
2509 }
2510
3dbeaa52
VZ
2511 // how should the selection work for this event?
2512 bool is_multiple, extended_select, unselect_others;
2513 EventFlagsToSelType(GetWindowStyleFlag(),
2514 event.ShiftDown(),
2515 event.ControlDown(),
dfc6cd93 2516 is_multiple, extended_select, unselect_others);
3dbeaa52 2517
3dbeaa52
VZ
2518 SelectItem(item, unselect_others, extended_select);
2519
2520 if ( event.LeftDClick() )
2521 {
f6bcfd97
BP
2522 // double clicking should not start editing the item label
2523 m_renameTimer->Stop();
2524 m_lastOnSame = FALSE;
2525
3dbeaa52 2526 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
40fab78b 2527 nevent.m_item = (long) item;
3dbeaa52 2528 nevent.m_code = 0;
05a7f61d
VZ
2529 CalcScrolledPosition(x, y,
2530 &nevent.m_pointDrag.x,
2531 &nevent.m_pointDrag.y);
3dbeaa52
VZ
2532 nevent.SetEventObject( this );
2533 GetEventHandler()->ProcessEvent( nevent );
2534 }
2535 }
bbe0af5b 2536 }
edaa81ae 2537}
c801d85f 2538
941830cb 2539void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
3db7be80 2540{
bbe0af5b
RR
2541 /* after all changes have been done to the tree control,
2542 * we actually redraw the tree when everything is over */
ef44a621 2543
f65635b5
VZ
2544 if (!m_dirty)
2545 return;
ef44a621 2546
bbe0af5b 2547 m_dirty = FALSE;
3db7be80 2548
bbe0af5b 2549 CalculatePositions();
91b8de8d 2550 Refresh();
bbe0af5b 2551 AdjustMyScrollbars();
3db7be80
RR
2552}
2553
941830cb 2554void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 2555{
e06b9569
JS
2556 wxCoord text_w = 0;
2557 wxCoord text_h = 0;
9dfbf520 2558
a62867a5 2559 if (item->IsBold())
eff869aa 2560 dc.SetFont(m_boldFont);
9dfbf520 2561
91b8de8d 2562 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 2563 text_h+=2;
91b8de8d 2564
eff869aa
RR
2565 // restore normal font
2566 dc.SetFont( m_normalFont );
9dfbf520 2567
91b8de8d
RR
2568 int image_h = 0;
2569 int image_w = 0;
8dc99046
VZ
2570 int image = item->GetCurrentImage();
2571 if ( image != NO_IMAGE )
91b8de8d 2572 {
2c8e4738
VZ
2573 if ( m_imageListNormal )
2574 {
2575 m_imageListNormal->GetSize( image, image_w, image_h );
2576 image_w += 4;
2577 }
91b8de8d
RR
2578 }
2579
2580 int total_h = (image_h > text_h) ? image_h : text_h;
2581
06b466c7 2582 if (total_h < 40)
f2593d0d 2583 total_h += 2; // at least 2 pixels
06b466c7 2584 else
f2593d0d 2585 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
2586
2587 item->SetHeight(total_h);
6cedba09
VZ
2588 if (total_h>m_lineHeight)
2589 m_lineHeight=total_h;
bbe0af5b 2590
91b8de8d
RR
2591 item->SetWidth(image_w+text_w+2);
2592}
2593
2594// -----------------------------------------------------------------------------
2595// for developper : y is now the top of the level
2596// not the middle of it !
941830cb 2597void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2598{
bbe0af5b 2599 int horizX = level*m_indent;
389cdc7a 2600
91b8de8d 2601 CalculateSize( item, dc );
d3a9f4af 2602
91b8de8d 2603 // set its position
cf724bce 2604 item->SetX( horizX+m_indent+m_spacing );
91b8de8d
RR
2605 item->SetY( y );
2606 y+=GetLineHeight(item);
4c681997 2607
bbe0af5b
RR
2608 if ( !item->IsExpanded() )
2609 {
f65635b5 2610 // we dont need to calculate collapsed branches
bbe0af5b
RR
2611 return;
2612 }
389cdc7a 2613
91b8de8d
RR
2614 wxArrayGenericTreeItems& children = item->GetChildren();
2615 size_t n, count = children.Count();
2616 for (n = 0; n < count; ++n )
f2593d0d 2617 CalculateLevel( children[n], dc, level+1, y ); // recurse
edaa81ae 2618}
c801d85f 2619
941830cb 2620void wxGenericTreeCtrl::CalculatePositions()
c801d85f 2621{
bbe0af5b 2622 if ( !m_anchor ) return;
29d87bba 2623
bbe0af5b
RR
2624 wxClientDC dc(this);
2625 PrepareDC( dc );
29d87bba 2626
eff869aa 2627 dc.SetFont( m_normalFont );
29d87bba 2628
bbe0af5b 2629 dc.SetPen( m_dottedPen );
91b8de8d
RR
2630 //if(GetImageList() == NULL)
2631 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2632
8dc99046 2633 int y = 2;
f65635b5 2634 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 2635}
c801d85f 2636
941830cb 2637void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 2638{
e6527f9d
RR
2639 if (m_dirty) return;
2640
bbe0af5b
RR
2641 wxClientDC dc(this);
2642 PrepareDC(dc);
4832f7c0 2643
bbe0af5b
RR
2644 int cw = 0;
2645 int ch = 0;
2646 GetClientSize( &cw, &ch );
4832f7c0 2647
bbe0af5b
RR
2648 wxRect rect;
2649 rect.x = dc.LogicalToDeviceX( 0 );
2650 rect.width = cw;
2651 rect.y = dc.LogicalToDeviceY( item->GetY() );
2652 rect.height = ch;
f135ff73 2653
bbe0af5b 2654 Refresh( TRUE, &rect );
f135ff73 2655
bbe0af5b 2656 AdjustMyScrollbars();
edaa81ae 2657}
c801d85f 2658
941830cb 2659void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 2660{
e6527f9d
RR
2661 if (m_dirty) return;
2662
bbe0af5b
RR
2663 wxClientDC dc(this);
2664 PrepareDC( dc );
2665
5391f772
SB
2666 int cw = 0;
2667 int ch = 0;
2668 GetClientSize( &cw, &ch );
2669
bbe0af5b 2670 wxRect rect;
5391f772
SB
2671 rect.x = dc.LogicalToDeviceX( 0 );
2672 rect.y = dc.LogicalToDeviceY( item->GetY() );
2673 rect.width = cw;
91b8de8d 2674 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 2675
bbe0af5b 2676 Refresh( TRUE, &rect );
edaa81ae 2677}
c801d85f 2678