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