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