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