]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
Link against import libraries of DLLs when building DLLs.
[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$
6aa89a22 8// Copyright: (c) 1998 Robert Roebling and Julian Smart
29d87bba 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
f135ff73
VZ
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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__
1e6feb95 28 #pragma hdrstop
1e6d9499
JS
29#endif
30
1e6feb95
VZ
31#if wxUSE_TREECTRL
32
618a5e38 33#include "wx/treebase.h"
941830cb 34#include "wx/generic/treectlg.h"
618a5e38
RR
35#include "wx/timer.h"
36#include "wx/textctrl.h"
941830cb 37#include "wx/imaglist.h"
c801d85f 38#include "wx/settings.h"
f135ff73 39#include "wx/dcclient.h"
c801d85f 40
9c7f49f5 41#include "wx/renderer.h"
44c44c82 42
f135ff73
VZ
43// -----------------------------------------------------------------------------
44// array types
45// -----------------------------------------------------------------------------
c801d85f 46
1e6d9499
JS
47class WXDLLEXPORT wxGenericTreeItem;
48
d5d29b8a 49WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem *, wxArrayGenericTreeItems);
c801d85f 50
8dc99046
VZ
51// ----------------------------------------------------------------------------
52// constants
53// ----------------------------------------------------------------------------
54
55static const int NO_IMAGE = -1;
56
cb59313c 57static const int PIXELS_PER_UNIT = 10;
3dbeaa52 58
f135ff73
VZ
59// -----------------------------------------------------------------------------
60// private classes
61// -----------------------------------------------------------------------------
62
3dbeaa52
VZ
63// timer used for enabling in-place edit
64class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
65{
66public:
cb59313c
VZ
67 // start editing the current item after half a second (if the mouse hasn't
68 // been clicked/moved)
7a944d2f 69 enum { DELAY = 500 };
cb59313c 70
941830cb 71 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
3dbeaa52 72
cb59313c 73 virtual void Notify();
3dbeaa52
VZ
74
75private:
cb59313c 76 wxGenericTreeCtrl *m_owner;
22f3361e
VZ
77
78 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer)
3dbeaa52
VZ
79};
80
81// control used for in-place edit
82class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
83{
84public:
edb8f298 85 wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item);
3dbeaa52 86
edb8f298 87protected:
3dbeaa52 88 void OnChar( wxKeyEvent &event );
c13cace1 89 void OnKeyUp( wxKeyEvent &event );
3dbeaa52
VZ
90 void OnKillFocus( wxFocusEvent &event );
91
edb8f298
VZ
92 bool AcceptChanges();
93 void Finish();
94
3dbeaa52 95private:
618a5e38 96 wxGenericTreeCtrl *m_owner;
edb8f298 97 wxGenericTreeItem *m_itemEdited;
3dbeaa52 98 wxString m_startValue;
dd5a32cc 99 bool m_finished;
3dbeaa52
VZ
100
101 DECLARE_EVENT_TABLE()
22f3361e 102 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl)
3dbeaa52
VZ
103};
104
cb59313c
VZ
105// timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
106// for a sufficiently long time
107class WXDLLEXPORT wxTreeFindTimer : public wxTimer
108{
109public:
110 // reset the current prefix after half a second of inactivity
7a944d2f 111 enum { DELAY = 500 };
cb59313c
VZ
112
113 wxTreeFindTimer( wxGenericTreeCtrl *owner ) { m_owner = owner; }
114
115 virtual void Notify() { m_owner->m_findPrefix.clear(); }
116
117private:
118 wxGenericTreeCtrl *m_owner;
22f3361e
VZ
119
120 DECLARE_NO_COPY_CLASS(wxTreeFindTimer)
cb59313c
VZ
121};
122
f135ff73
VZ
123// a tree item
124class WXDLLEXPORT wxGenericTreeItem
c801d85f 125{
f135ff73 126public:
9ec64fa7
VZ
127 // ctors & dtor
128 wxGenericTreeItem() { m_data = NULL; }
129 wxGenericTreeItem( wxGenericTreeItem *parent,
618a5e38 130 const wxString& text,
618a5e38
RR
131 int image,
132 int selImage,
133 wxTreeItemData *data );
f135ff73 134
9ec64fa7 135 ~wxGenericTreeItem();
f135ff73 136
9ec64fa7
VZ
137 // trivial accessors
138 wxArrayGenericTreeItems& GetChildren() { return m_children; }
f135ff73 139
9ec64fa7
VZ
140 const wxString& GetText() const { return m_text; }
141 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
3dbeaa52 142 { return m_images[which]; }
9ec64fa7 143 wxTreeItemData *GetData() const { return m_data; }
f135ff73 144
9ec64fa7
VZ
145 // returns the current image for the item (depending on its
146 // selected/expanded/whatever state)
147 int GetCurrentImage() const;
8dc99046 148
9ec64fa7
VZ
149 void SetText( const wxString &text );
150 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
151 void SetData(wxTreeItemData *data) { m_data = data; }
f135ff73 152
9ec64fa7 153 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
f135ff73 154
9ec64fa7 155 void SetBold(bool bold) { m_isBold = bold; }
ef44a621 156
9ec64fa7
VZ
157 int GetX() const { return m_x; }
158 int GetY() const { return m_y; }
f135ff73 159
9ec64fa7
VZ
160 void SetX(int x) { m_x = x; }
161 void SetY(int y) { m_y = y; }
f135ff73 162
9ec64fa7
VZ
163 int GetHeight() const { return m_height; }
164 int GetWidth() const { return m_width; }
91b8de8d 165
9ec64fa7
VZ
166 void SetHeight(int h) { m_height = h; }
167 void SetWidth(int w) { m_width = w; }
91b8de8d 168
9ec64fa7 169 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 170
9ec64fa7
VZ
171 // operations
172 // deletes all children notifying the treectrl about it if !NULL
173 // pointer given
941830cb 174 void DeleteChildren(wxGenericTreeCtrl *tree = NULL);
f135ff73 175
9ec64fa7
VZ
176 // get count of all children (and grand children if 'recursively')
177 size_t GetChildrenCount(bool recursively = TRUE) const;
f135ff73 178
9ec64fa7 179 void Insert(wxGenericTreeItem *child, size_t index)
f135ff73
VZ
180 { m_children.Insert(child, index); }
181
941830cb 182 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
f135ff73 183
9ec64fa7
VZ
184 // return the item at given position (or NULL if no item), onButton is
185 // TRUE if the point belongs to the item's button, otherwise it lies
f8b043e7 186 // on the item's label
618a5e38
RR
187 wxGenericTreeItem *HitTest( const wxPoint& point,
188 const wxGenericTreeCtrl *,
189 int &flags,
190 int level );
f135ff73 191
9ec64fa7
VZ
192 void Expand() { m_isCollapsed = FALSE; }
193 void Collapse() { m_isCollapsed = TRUE; }
f135ff73 194
9ec64fa7 195 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
f135ff73 196
9ec64fa7
VZ
197 // status inquiries
198 bool HasChildren() const { return !m_children.IsEmpty(); }
ef8698d6 199 bool IsSelected() const { return m_hasHilight != 0; }
9ec64fa7
VZ
200 bool IsExpanded() const { return !m_isCollapsed; }
201 bool HasPlus() const { return m_hasPlus || HasChildren(); }
ef8698d6 202 bool IsBold() const { return m_isBold != 0; }
9ec64fa7
VZ
203
204 // attributes
205 // get them - may be NULL
206 wxTreeItemAttr *GetAttributes() const { return m_attr; }
207 // get them ensuring that the pointer is not NULL
208 wxTreeItemAttr& Attr()
209 {
210 if ( !m_attr )
618a5e38 211 {
9ec64fa7 212 m_attr = new wxTreeItemAttr;
618a5e38
RR
213 m_ownsAttr = TRUE;
214 }
9ec64fa7
VZ
215 return *m_attr;
216 }
618a5e38
RR
217 // set them
218 void SetAttributes(wxTreeItemAttr *attr)
219 {
220 if ( m_ownsAttr ) delete m_attr;
221 m_attr = attr;
222 m_ownsAttr = FALSE;
223 }
224 // set them and delete when done
225 void AssignAttributes(wxTreeItemAttr *attr)
226 {
227 SetAttributes(attr);
228 m_ownsAttr = TRUE;
229 }
f135ff73
VZ
230
231private:
618a5e38
RR
232 // since there can be very many of these, we save size by chosing
233 // the smallest representation for the elements and by ordering
234 // the members to avoid padding.
235 wxString m_text; // label to be rendered for item
236
237 wxTreeItemData *m_data; // user-provided data
238
239 wxArrayGenericTreeItems m_children; // list of children
240 wxGenericTreeItem *m_parent; // parent of this item
241
242 wxTreeItemAttr *m_attr; // attributes???
9ec64fa7
VZ
243
244 // tree ctrl images for the normal, selected, expanded and
245 // expanded+selected states
618a5e38 246 short m_images[wxTreeItemIcon_Max];
9ec64fa7 247
618a5e38 248 wxCoord m_x; // (virtual) offset from top
e549bec4 249 wxCoord m_y; // (virtual) offset from left
618a5e38
RR
250 short m_width; // width of this item
251 unsigned char m_height; // height of this item
9ec64fa7
VZ
252
253 // use bitfields to save size
254 int m_isCollapsed :1;
255 int m_hasHilight :1; // same as focused
256 int m_hasPlus :1; // used for item which doesn't have
257 // children but has a [+] button
258 int m_isBold :1; // render the label in bold font
618a5e38 259 int m_ownsAttr :1; // delete attribute when done
22f3361e
VZ
260
261 DECLARE_NO_COPY_CLASS(wxGenericTreeItem)
f135ff73
VZ
262};
263
264// =============================================================================
265// implementation
266// =============================================================================
267
06b466c7
VZ
268// ----------------------------------------------------------------------------
269// private functions
270// ----------------------------------------------------------------------------
271
272// translate the key or mouse event flags to the type of selection we're
273// dealing with
274static void EventFlagsToSelType(long style,
275 bool shiftDown,
276 bool ctrlDown,
dfc6cd93
SB
277 bool &is_multiple,
278 bool &extended_select,
279 bool &unselect_others)
06b466c7 280{
dfc6cd93
SB
281 is_multiple = (style & wxTR_MULTIPLE) != 0;
282 extended_select = shiftDown && is_multiple;
283 unselect_others = !(extended_select || (ctrlDown && is_multiple));
06b466c7 284}
e179bd65 285
7475e8a3
VZ
286// check if the given item is under another one
287static bool IsDescendantOf(wxGenericTreeItem *parent, wxGenericTreeItem *item)
288{
289 while ( item )
290 {
291 if ( item == parent )
292 {
293 // item is a descendant of parent
294 return TRUE;
295 }
296
297 item = item->GetParent();
298 }
299
300 return FALSE;
301}
302
e179bd65
RR
303// -----------------------------------------------------------------------------
304// wxTreeRenameTimer (internal)
305// -----------------------------------------------------------------------------
306
941830cb 307wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
e179bd65
RR
308{
309 m_owner = owner;
310}
311
312void wxTreeRenameTimer::Notify()
313{
314 m_owner->OnRenameTimer();
315}
316
317//-----------------------------------------------------------------------------
318// wxTreeTextCtrl (internal)
319//-----------------------------------------------------------------------------
320
e179bd65
RR
321BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
322 EVT_CHAR (wxTreeTextCtrl::OnChar)
c13cace1 323 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
e179bd65
RR
324 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
325END_EVENT_TABLE()
326
edb8f298
VZ
327wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
328 wxGenericTreeItem *item)
329 : m_itemEdited(item), m_startValue(item->GetText())
330{
e179bd65 331 m_owner = owner;
dd5a32cc 332 m_finished = FALSE;
e179bd65 333
edb8f298
VZ
334 int w = m_itemEdited->GetWidth(),
335 h = m_itemEdited->GetHeight();
336
337 int x, y;
338 m_owner->CalcScrolledPosition(item->GetX(), item->GetY(), &x, &y);
339
340 int image_h = 0,
341 image_w = 0;
342
343 int image = item->GetCurrentImage();
344 if ( image != NO_IMAGE )
e179bd65 345 {
edb8f298
VZ
346 if ( m_owner->m_imageListNormal )
347 {
348 m_owner->m_imageListNormal->GetSize( image, image_w, image_h );
349 image_w += 4;
350 }
351 else
352 {
353 wxFAIL_MSG(_T("you must create an image list to use images!"));
354 }
355 }
33ac7e6f 356
edb8f298
VZ
357 // FIXME: what are all these hardcoded 4, 8 and 11s really?
358 x += image_w;
359 w -= image_w + 4;
33ac7e6f 360
edb8f298
VZ
361 (void)Create(m_owner, wxID_ANY, m_startValue,
362 wxPoint(x - 4, y - 4), wxSize(w + 11, h + 8));
363}
574c939e 364
edb8f298
VZ
365bool wxTreeTextCtrl::AcceptChanges()
366{
367 const wxString value = GetValue();
618a5e38 368
edb8f298
VZ
369 if ( value == m_startValue )
370 {
371 // nothing changed, always accept
372 return TRUE;
e179bd65 373 }
edb8f298
VZ
374
375 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
e179bd65 376 {
edb8f298
VZ
377 // vetoed by the user
378 return FALSE;
379 }
380
381 // accepted, do rename the item
382 m_owner->SetItemText(m_itemEdited, value);
33ac7e6f 383
edb8f298
VZ
384 return TRUE;
385}
386
387void wxTreeTextCtrl::Finish()
388{
389 if ( !m_finished )
390 {
fbb12260
JS
391 m_owner->ResetTextControl();
392
edb8f298 393 wxPendingDelete.Append(this);
33ac7e6f 394
dd5a32cc 395 m_finished = TRUE;
edb8f298 396
dd5a32cc 397 m_owner->SetFocus(); // This doesn't work. TODO.
edb8f298
VZ
398 }
399}
dd5a32cc 400
edb8f298
VZ
401void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
402{
403 switch ( event.m_keyCode )
404 {
405 case WXK_RETURN:
406 if ( !AcceptChanges() )
407 {
408 // vetoed by the user, don't disappear
409 break;
410 }
411 //else: fall through
412
413 case WXK_ESCAPE:
414 Finish();
dd23c25c 415 m_owner->OnRenameCancelled(m_itemEdited);
edb8f298
VZ
416 break;
417
418 default:
419 event.Skip();
e179bd65 420 }
c13cace1
VS
421}
422
423void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
424{
edb8f298 425 if ( !m_finished )
dd5a32cc 426 {
edb8f298
VZ
427 // auto-grow the textctrl:
428 wxSize parentSize = m_owner->GetSize();
429 wxPoint myPos = GetPosition();
430 wxSize mySize = GetSize();
431 int sx, sy;
432 GetTextExtent(GetValue() + _T("M"), &sx, &sy);
433 if (myPos.x + sx > parentSize.x)
434 sx = parentSize.x - myPos.x;
435 if (mySize.x > sx)
436 sx = mySize.x;
437 SetSize(sx, -1);
dd5a32cc
RR
438 }
439
c13cace1 440 event.Skip();
e179bd65
RR
441}
442
dd5a32cc 443void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
e179bd65 444{
edb8f298 445 if ( m_finished )
dd5a32cc
RR
446 {
447 event.Skip();
448 return;
449 }
450
edb8f298
VZ
451 if ( AcceptChanges() )
452 {
453 Finish();
454 }
e179bd65
RR
455}
456
f135ff73 457// -----------------------------------------------------------------------------
c801d85f 458// wxGenericTreeItem
f135ff73 459// -----------------------------------------------------------------------------
c801d85f 460
f135ff73
VZ
461wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
462 const wxString& text,
f135ff73
VZ
463 int image, int selImage,
464 wxTreeItemData *data)
465 : m_text(text)
c801d85f 466{
00e12320
RR
467 m_images[wxTreeItemIcon_Normal] = image;
468 m_images[wxTreeItemIcon_Selected] = selImage;
469 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
470 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
8dc99046 471
00e12320
RR
472 m_data = data;
473 m_x = m_y = 0;
f135ff73 474
00e12320
RR
475 m_isCollapsed = TRUE;
476 m_hasHilight = FALSE;
477 m_hasPlus = FALSE;
478 m_isBold = FALSE;
c801d85f 479
00e12320 480 m_parent = parent;
f135ff73 481
00e12320 482 m_attr = (wxTreeItemAttr *)NULL;
618a5e38 483 m_ownsAttr = FALSE;
06b466c7 484
00e12320
RR
485 // We don't know the height here yet.
486 m_width = 0;
487 m_height = 0;
edaa81ae 488}
c801d85f 489
f135ff73 490wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 491{
00e12320 492 delete m_data;
4832f7c0 493
618a5e38 494 if (m_ownsAttr) delete m_attr;
9ec64fa7 495
00e12320
RR
496 wxASSERT_MSG( m_children.IsEmpty(),
497 wxT("please call DeleteChildren() before deleting the item") );
372edb9d
VZ
498}
499
941830cb 500void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
372edb9d 501{
00e12320
RR
502 size_t count = m_children.Count();
503 for ( size_t n = 0; n < count; n++ )
a43a4f9d 504 {
00e12320
RR
505 wxGenericTreeItem *child = m_children[n];
506 if (tree)
507 tree->SendDeleteEvent(child);
a43a4f9d 508
00e12320
RR
509 child->DeleteChildren(tree);
510 delete child;
511 }
372edb9d 512
00e12320 513 m_children.Empty();
edaa81ae 514}
c801d85f 515
91b8de8d 516void wxGenericTreeItem::SetText( const wxString &text )
c801d85f 517{
00e12320 518 m_text = text;
edaa81ae 519}
c801d85f 520
4832f7c0 521size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 522{
00e12320
RR
523 size_t count = m_children.Count();
524 if ( !recursively )
525 return count;
4832f7c0 526
00e12320 527 size_t total = count;
f2593d0d 528 for (size_t n = 0; n < count; ++n)
00e12320
RR
529 {
530 total += m_children[n]->GetChildrenCount();
531 }
c801d85f 532
00e12320 533 return total;
edaa81ae 534}
c801d85f 535
618a5e38
RR
536void wxGenericTreeItem::GetSize( int &x, int &y,
537 const wxGenericTreeCtrl *theButton )
c801d85f 538{
618a5e38 539 int bottomY=m_y+theButton->GetLineHeight(this);
00e12320
RR
540 if ( y < bottomY ) y = bottomY;
541 int width = m_x + m_width;
542 if ( x < width ) x = width;
f135ff73 543
00e12320 544 if (IsExpanded())
4832f7c0 545 {
00e12320
RR
546 size_t count = m_children.Count();
547 for ( size_t n = 0; n < count; ++n )
548 {
618a5e38 549 m_children[n]->GetSize( x, y, theButton );
00e12320 550 }
df875e59 551 }
edaa81ae 552}
c801d85f 553
618a5e38
RR
554wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point,
555 const wxGenericTreeCtrl *theCtrl,
556 int &flags,
557 int level)
c801d85f 558{
618a5e38
RR
559 // for a hidden root node, don't evaluate it, but do evaluate children
560 if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) )
c801d85f 561 {
618a5e38
RR
562 // evaluate the item
563 int h = theCtrl->GetLineHeight(this);
564 if ((point.y > m_y) && (point.y < m_y + h))
f2593d0d 565 {
618a5e38
RR
566 int y_mid = m_y + h/2;
567 if (point.y < y_mid )
568 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
569 else
570 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
d3a9f4af 571
618a5e38 572 int xCross = m_x - theCtrl->GetSpacing();
a6b0ca75
SC
573#ifdef __WXMAC__
574 // according to the drawing code the triangels are drawn
575 // at -4 , -4 from the position up to +10/+10 max
576 if ((point.x > xCross-4) && (point.x < xCross+10) &&
577 (point.y > y_mid-4) && (point.y < y_mid+10) &&
578 HasPlus() && theCtrl->HasButtons() )
579#else
580 // 5 is the size of the plus sign
f8b043e7
RR
581 if ((point.x > xCross-6) && (point.x < xCross+6) &&
582 (point.y > y_mid-6) && (point.y < y_mid+6) &&
618a5e38 583 HasPlus() && theCtrl->HasButtons() )
a6b0ca75 584#endif
618a5e38
RR
585 {
586 flags |= wxTREE_HITTEST_ONITEMBUTTON;
587 return this;
588 }
0ae7f2a2 589
618a5e38
RR
590 if ((point.x >= m_x) && (point.x <= m_x+m_width))
591 {
592 int image_w = -1;
593 int image_h;
91b8de8d 594
618a5e38
RR
595 // assuming every image (normal and selected) has the same size!
596 if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal )
597 theCtrl->m_imageListNormal->GetSize(GetImage(),
598 image_w, image_h);
599
600 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
601 flags |= wxTREE_HITTEST_ONITEMICON;
602 else
603 flags |= wxTREE_HITTEST_ONITEMLABEL;
604
605 return this;
606 }
607
608 if (point.x < m_x)
609 flags |= wxTREE_HITTEST_ONITEMINDENT;
610 if (point.x > m_x+m_width)
611 flags |= wxTREE_HITTEST_ONITEMRIGHT;
91b8de8d 612
f2593d0d
RR
613 return this;
614 }
91b8de8d 615
618a5e38
RR
616 // if children are expanded, fall through to evaluate them
617 if (m_isCollapsed) return (wxGenericTreeItem*) NULL;
f2593d0d 618 }
618a5e38
RR
619
620 // evaluate children
621 size_t count = m_children.Count();
622 for ( size_t n = 0; n < count; n++ )
c801d85f 623 {
618a5e38
RR
624 wxGenericTreeItem *res = m_children[n]->HitTest( point,
625 theCtrl,
626 flags,
627 level + 1 );
628 if ( res != NULL )
629 return res;
edaa81ae 630 }
f135ff73 631
f2593d0d 632 return (wxGenericTreeItem*) NULL;
edaa81ae 633}
c801d85f 634
8dc99046
VZ
635int wxGenericTreeItem::GetCurrentImage() const
636{
637 int image = NO_IMAGE;
638 if ( IsExpanded() )
639 {
640 if ( IsSelected() )
641 {
642 image = GetImage(wxTreeItemIcon_SelectedExpanded);
643 }
644
645 if ( image == NO_IMAGE )
646 {
647 // we usually fall back to the normal item, but try just the
648 // expanded one (and not selected) first in this case
649 image = GetImage(wxTreeItemIcon_Expanded);
650 }
651 }
652 else // not expanded
653 {
654 if ( IsSelected() )
655 image = GetImage(wxTreeItemIcon_Selected);
656 }
657
618a5e38
RR
658 // maybe it doesn't have the specific image we want,
659 // try the default one instead
660 if ( image == NO_IMAGE ) image = GetImage();
8dc99046
VZ
661
662 return image;
663}
664
f135ff73 665// -----------------------------------------------------------------------------
941830cb 666// wxGenericTreeCtrl implementation
f135ff73
VZ
667// -----------------------------------------------------------------------------
668
941830cb 669IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow)
f135ff73 670
941830cb
JS
671BEGIN_EVENT_TABLE(wxGenericTreeCtrl,wxScrolledWindow)
672 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
673 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
674 EVT_CHAR (wxGenericTreeCtrl::OnChar)
675 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
676 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
73bb6776 677 EVT_TREE_ITEM_GETTOOLTIP(-1, wxGenericTreeCtrl::OnGetToolTip)
f135ff73
VZ
678END_EVENT_TABLE()
679
618a5e38 680#if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
233058c7
JS
681/*
682 * wxTreeCtrl has to be a real class or we have problems with
683 * the run-time information.
684 */
685
686IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
687#endif
688
f135ff73
VZ
689// -----------------------------------------------------------------------------
690// construction/destruction
691// -----------------------------------------------------------------------------
91b8de8d 692
941830cb 693void wxGenericTreeCtrl::Init()
c801d85f 694{
3e3a7b97 695 m_current = m_key_current = m_anchor = m_select_me = (wxGenericTreeItem *) NULL;
00e12320
RR
696 m_hasFocus = FALSE;
697 m_dirty = FALSE;
f135ff73 698
00e12320
RR
699 m_lineHeight = 10;
700 m_indent = 15;
701 m_spacing = 18;
f135ff73 702
b771aa29
VZ
703 m_hilightBrush = new wxBrush
704 (
a756f210 705 wxSystemSettings::GetColour
b771aa29
VZ
706 (
707 wxSYS_COLOUR_HIGHLIGHT
708 ),
709 wxSOLID
710 );
711
712 m_hilightUnfocusedBrush = new wxBrush
713 (
a756f210 714 wxSystemSettings::GetColour
b771aa29
VZ
715 (
716 wxSYS_COLOUR_BTNSHADOW
717 ),
718 wxSOLID
719 );
f135ff73 720
618a5e38 721 m_imageListNormal = m_imageListButtons =
00e12320 722 m_imageListState = (wxImageList *) NULL;
618a5e38 723 m_ownsImageListNormal = m_ownsImageListButtons =
46cd520d 724 m_ownsImageListState = FALSE;
978f38c2 725
00e12320 726 m_dragCount = 0;
3dbeaa52 727 m_isDragging = FALSE;
f8b043e7
RR
728 m_dropTarget = m_oldSelection = NULL;
729 m_underMouse = NULL;
fbb12260 730 m_textCtrl = NULL;
9dfbf520 731
cb59313c
VZ
732 m_renameTimer = NULL;
733 m_findTimer = NULL;
734
f6bcfd97 735 m_lastOnSame = FALSE;
f38374d0 736
a756f210 737 m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
2b5f62a0
VZ
738 m_boldFont = wxFont(m_normalFont.GetPointSize(),
739 m_normalFont.GetFamily(),
740 m_normalFont.GetStyle(),
741 wxBOLD,
742 m_normalFont.GetUnderlined(),
743 m_normalFont.GetFaceName(),
744 m_normalFont.GetEncoding());
edaa81ae 745}
c801d85f 746
618a5e38
RR
747bool wxGenericTreeCtrl::Create(wxWindow *parent,
748 wxWindowID id,
749 const wxPoint& pos,
750 const wxSize& size,
751 long style,
ac8d0c11 752 const wxValidator& wxVALIDATOR_PARAM(validator),
618a5e38 753 const wxString& name )
c801d85f 754{
2593f033
RR
755#ifdef __WXMAC__
756 int major,minor;
757 wxGetOsVersion( &major, &minor );
574c939e 758
2593f033
RR
759 style &= ~wxTR_LINES_AT_ROOT;
760 style |= wxTR_NO_LINES;
761 if (major < 10)
762 style |= wxTR_ROW_LINES;
9c7f49f5 763#endif // __WXMAC__
17d5bdf9 764
618a5e38
RR
765 wxScrolledWindow::Create( parent, id, pos, size,
766 style|wxHSCROLL|wxVSCROLL, name );
767
6f0dd6b2
VZ
768 // If the tree display has no buttons, but does have
769 // connecting lines, we can use a narrower layout.
770 // It may not be a good idea to force this...
618a5e38
RR
771 if (!HasButtons() && !HasFlag(wxTR_NO_LINES))
772 {
773 m_indent= 10;
774 m_spacing = 10;
775 }
978f38c2 776
ce4169a4 777#if wxUSE_VALIDATORS
00e12320 778 SetValidator( validator );
ce4169a4 779#endif
f135ff73 780
6f0dd6b2
VZ
781 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
782 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX) );
c22886bd 783
00e12320 784// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
9e0a12c9 785 m_dottedPen = wxPen( wxT("grey"), 0, 0 );
f135ff73 786
00e12320 787 return TRUE;
edaa81ae 788}
c801d85f 789
941830cb 790wxGenericTreeCtrl::~wxGenericTreeCtrl()
c801d85f 791{
b771aa29
VZ
792 delete m_hilightBrush;
793 delete m_hilightUnfocusedBrush;
574c939e 794
00e12320 795 DeleteAllItems();
9dfbf520 796
00e12320 797 delete m_renameTimer;
cb59313c
VZ
798 delete m_findTimer;
799
800 if (m_ownsImageListNormal)
801 delete m_imageListNormal;
802 if (m_ownsImageListState)
803 delete m_imageListState;
804 if (m_ownsImageListButtons)
805 delete m_imageListButtons;
edaa81ae 806}
c801d85f 807
f135ff73
VZ
808// -----------------------------------------------------------------------------
809// accessors
810// -----------------------------------------------------------------------------
811
941830cb 812size_t wxGenericTreeCtrl::GetCount() const
c801d85f 813{
f2593d0d 814 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
edaa81ae 815}
c801d85f 816
941830cb 817void wxGenericTreeCtrl::SetIndent(unsigned int indent)
c801d85f 818{
574c939e 819 m_indent = (unsigned short) indent;
f2593d0d 820 m_dirty = TRUE;
cf724bce
RR
821}
822
941830cb 823void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
cf724bce 824{
574c939e 825 m_spacing = (unsigned short) spacing;
f2593d0d 826 m_dirty = TRUE;
f135ff73 827}
74bedbeb 828
941830cb 829size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
4832f7c0 830{
f2593d0d 831 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
4832f7c0 832
941830cb 833 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
4832f7c0
VZ
834}
835
618a5e38
RR
836void wxGenericTreeCtrl::SetWindowStyle(const long styles)
837{
374a8e5c
VS
838 if (!HasFlag(wxTR_HIDE_ROOT) && (styles & wxTR_HIDE_ROOT))
839 {
840 // if we will hide the root, make sure children are visible
841 m_anchor->SetHasPlus();
842 m_anchor->Expand();
843 CalculatePositions();
844 }
845
846 // right now, just sets the styles. Eventually, we may
847 // want to update the inherited styles, but right now
848 // none of the parents has updatable styles
618a5e38
RR
849 m_windowStyle = styles;
850 m_dirty = TRUE;
851}
852
f135ff73
VZ
853// -----------------------------------------------------------------------------
854// functions to work with tree items
855// -----------------------------------------------------------------------------
74bedbeb 856
941830cb 857wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
f135ff73 858{
f2593d0d 859 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
4832f7c0 860
941830cb 861 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
edaa81ae 862}
74bedbeb 863
941830cb 864int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
8dc99046 865 wxTreeItemIcon which) const
74bedbeb 866{
f2593d0d 867 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 868
941830cb 869 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
edaa81ae 870}
c801d85f 871
941830cb 872wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 873{
f2593d0d 874 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 875
941830cb 876 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
edaa81ae 877}
c801d85f 878
2b5f62a0
VZ
879wxColour wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
880{
881 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
882
883 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
884 return pItem->Attr().GetTextColour();
885}
886
887wxColour wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
888{
889 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
890
891 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
892 return pItem->Attr().GetBackgroundColour();
893}
894
895wxFont wxGenericTreeCtrl::GetItemFont(const wxTreeItemId& item) const
896{
897 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
898
899 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
900 return pItem->Attr().GetFont();
901}
902
941830cb 903void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
f135ff73 904{
f2593d0d 905 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 906
f2593d0d 907 wxClientDC dc(this);
941830cb 908 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
909 pItem->SetText(text);
910 CalculateSize(pItem, dc);
911 RefreshLine(pItem);
f135ff73 912}
c801d85f 913
941830cb 914void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
8dc99046
VZ
915 int image,
916 wxTreeItemIcon which)
f135ff73 917{
223d09f6 918 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 919
941830cb 920 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
8dc99046 921 pItem->SetImage(image, which);
4832f7c0 922
8dc99046
VZ
923 wxClientDC dc(this);
924 CalculateSize(pItem, dc);
925 RefreshLine(pItem);
edaa81ae 926}
c801d85f 927
941830cb 928void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 929{
f2593d0d 930 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 931
941830cb 932 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
edaa81ae 933}
c801d85f 934
941830cb 935void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 936{
f2593d0d 937 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 938
941830cb 939 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
f2593d0d
RR
940 pItem->SetHasPlus(has);
941 RefreshLine(pItem);
edaa81ae 942}
c801d85f 943
941830cb 944void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
ef44a621 945{
9ec64fa7 946 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 947
9ec64fa7 948 // avoid redrawing the tree if no real change
941830cb 949 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
950 if ( pItem->IsBold() != bold )
951 {
952 pItem->SetBold(bold);
953 RefreshLine(pItem);
954 }
955}
956
941830cb 957void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
9ec64fa7
VZ
958 const wxColour& col)
959{
960 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
961
941830cb 962 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
963 pItem->Attr().SetTextColour(col);
964 RefreshLine(pItem);
965}
966
941830cb 967void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
9ec64fa7
VZ
968 const wxColour& col)
969{
970 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
971
941830cb 972 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7
VZ
973 pItem->Attr().SetBackgroundColour(col);
974 RefreshLine(pItem);
975}
976
941830cb 977void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
9ec64fa7
VZ
978{
979 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
980
941830cb 981 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
9ec64fa7 982 pItem->Attr().SetFont(font);
ef44a621 983 RefreshLine(pItem);
ef44a621
VZ
984}
985
3da2715f
JS
986bool wxGenericTreeCtrl::SetFont( const wxFont &font )
987{
988 wxScrolledWindow::SetFont(font);
989
990 m_normalFont = font ;
2b5f62a0
VZ
991 m_boldFont = wxFont(m_normalFont.GetPointSize(),
992 m_normalFont.GetFamily(),
993 m_normalFont.GetStyle(),
994 wxBOLD,
995 m_normalFont.GetUnderlined(),
996 m_normalFont.GetFaceName(),
997 m_normalFont.GetEncoding());
3da2715f
JS
998
999 return TRUE;
1000}
1001
1002
f135ff73
VZ
1003// -----------------------------------------------------------------------------
1004// item status inquiries
1005// -----------------------------------------------------------------------------
1006
1ed01484 1007bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
c801d85f 1008{
1ed01484
JS
1009 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
1010
c22886bd 1011 // An item is only visible if it's not a descendant of a collapsed item
1ed01484 1012 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
c22886bd
RR
1013 wxGenericTreeItem* parent = pItem->GetParent();
1014 while (parent)
1015 {
1016 if (!parent->IsExpanded())
1017 return FALSE;
1018 parent = parent->GetParent();
1019 }
1ed01484
JS
1020
1021 int startX, startY;
1022 GetViewStart(& startX, & startY);
1023
1024 wxSize clientSize = GetClientSize();
1025
1026 wxRect rect;
1027 if (!GetBoundingRect(item, rect))
1028 return FALSE;
c22886bd
RR
1029 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
1030 return FALSE;
1ed01484
JS
1031 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
1032 return FALSE;
1033 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
1034 return FALSE;
f135ff73 1035
f2593d0d 1036 return TRUE;
edaa81ae 1037}
c801d85f 1038
941830cb 1039bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 1040{
f2593d0d 1041 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 1042
59a2e635
VZ
1043 // consider that the item does have children if it has the "+" button: it
1044 // might not have them (if it had never been expanded yet) but then it
1045 // could have them as well and it's better to err on this side rather than
1046 // disabling some operations which are restricted to the items with
1047 // children for an item which does have them
607d9cfc 1048 return ((wxGenericTreeItem*) item.m_pItem)->HasPlus();
edaa81ae 1049}
c801d85f 1050
941830cb 1051bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 1052{
f2593d0d 1053 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 1054
941830cb 1055 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
f135ff73 1056}
29d87bba 1057
941830cb 1058bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
f135ff73 1059{
f2593d0d 1060 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 1061
941830cb 1062 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
f135ff73 1063}
29d87bba 1064
941830cb 1065bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
ef44a621 1066{
f2593d0d 1067 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
ef44a621 1068
941830cb 1069 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
ef44a621
VZ
1070}
1071
f135ff73
VZ
1072// -----------------------------------------------------------------------------
1073// navigation
1074// -----------------------------------------------------------------------------
29d87bba 1075
99006e44 1076wxTreeItemId wxGenericTreeCtrl::GetItemParent(const wxTreeItemId& item) const
f135ff73 1077{
618a5e38 1078 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 1079
618a5e38 1080 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
f135ff73 1081}
29d87bba 1082
ee4b2721
VZ
1083wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1084 wxTreeItemIdValue& cookie) const
f135ff73 1085{
618a5e38 1086 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1087
618a5e38
RR
1088 cookie = 0;
1089 return GetNextChild(item, cookie);
f135ff73 1090}
29d87bba 1091
ee4b2721
VZ
1092wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1093 wxTreeItemIdValue& cookie) const
1094{
1095 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1096
1097 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1098
1099 // it's ok to cast cookie to size_t, we never have indices big enough to
1100 // overflow "void *"
1101 size_t *pIndex = (size_t *)&cookie;
1102 if ( *pIndex < children.Count() )
1103 {
836543b8 1104 return children.Item((*pIndex)++);
ee4b2721
VZ
1105 }
1106 else
1107 {
1108 // there are no more of them
1109 return wxTreeItemId();
1110 }
1111}
1112
1113#if WXWIN_COMPATIBILITY_2_4
1114
1115wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1116 long& cookie) const
1117{
1118 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1119
1120 cookie = 0;
1121 return GetNextChild(item, cookie);
1122}
1123
1124wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1125 long& cookie) const
f135ff73 1126{
618a5e38 1127 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1128
618a5e38
RR
1129 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1130 if ( (size_t)cookie < children.Count() )
1131 {
1132 return children.Item((size_t)cookie++);
1133 }
1134 else
1135 {
1136 // there are no more of them
1137 return wxTreeItemId();
1138 }
f135ff73 1139}
29d87bba 1140
ee4b2721
VZ
1141#endif // WXWIN_COMPATIBILITY_2_4
1142
941830cb 1143wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
978f38c2 1144{
618a5e38 1145 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 1146
618a5e38
RR
1147 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1148 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
1149}
1150
941830cb 1151wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
f135ff73 1152{
618a5e38 1153 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1154
618a5e38
RR
1155 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1156 wxGenericTreeItem *parent = i->GetParent();
1157 if ( parent == NULL )
1158 {
1159 // root item doesn't have any siblings
1160 return wxTreeItemId();
1161 }
4832f7c0 1162
618a5e38
RR
1163 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1164 int index = siblings.Index(i);
1165 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1166
618a5e38
RR
1167 size_t n = (size_t)(index + 1);
1168 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 1169}
c801d85f 1170
941830cb 1171wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 1172{
618a5e38 1173 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73 1174
618a5e38
RR
1175 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1176 wxGenericTreeItem *parent = i->GetParent();
1177 if ( parent == NULL )
1178 {
1179 // root item doesn't have any siblings
1180 return wxTreeItemId();
1181 }
4832f7c0 1182
618a5e38
RR
1183 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1184 int index = siblings.Index(i);
1185 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 1186
618a5e38
RR
1187 return index == 0 ? wxTreeItemId()
1188 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 1189}
389cdc7a 1190
1ed01484
JS
1191// Only for internal use right now, but should probably be public
1192wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
f135ff73 1193{
618a5e38
RR
1194 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1195
1196 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1197
1198 // First see if there are any children.
1199 wxArrayGenericTreeItems& children = i->GetChildren();
1200 if (children.GetCount() > 0)
1201 {
1202 return children.Item(0);
1203 }
1204 else
1205 {
1206 // Try a sibling of this or ancestor instead
1207 wxTreeItemId p = item;
1208 wxTreeItemId toFind;
1209 do
1210 {
1211 toFind = GetNextSibling(p);
99006e44 1212 p = GetItemParent(p);
618a5e38
RR
1213 } while (p.IsOk() && !toFind.IsOk());
1214 return toFind;
1215 }
1ed01484
JS
1216}
1217
1ed01484
JS
1218wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1219{
618a5e38
RR
1220 wxTreeItemId id = GetRootItem();
1221 if (!id.IsOk())
1ed01484 1222 return id;
1ed01484 1223
618a5e38
RR
1224 do
1225 {
1226 if (IsVisible(id))
1227 return id;
1228 id = GetNext(id);
1229 } while (id.IsOk());
1230
1231 return wxTreeItemId();
1ed01484
JS
1232}
1233
941830cb 1234wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
f135ff73 1235{
618a5e38 1236 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1237
618a5e38
RR
1238 wxTreeItemId id = item;
1239 if (id.IsOk())
1240 {
1241 while (id = GetNext(id), id.IsOk())
1242 {
1243 if (IsVisible(id))
1244 return id;
1245 }
1246 }
1247 return wxTreeItemId();
f135ff73 1248}
29d87bba 1249
941830cb 1250wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
f135ff73 1251{
618a5e38 1252 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 1253
618a5e38 1254 wxFAIL_MSG(wxT("not implemented"));
29d87bba 1255
618a5e38 1256 return wxTreeItemId();
edaa81ae 1257}
c801d85f 1258
fbb12260
JS
1259// called by wxTextTreeCtrl when it marks itself for deletion
1260void wxGenericTreeCtrl::ResetTextControl()
1261{
1262 m_textCtrl = NULL;
1263}
1264
cb59313c
VZ
1265// find the first item starting with the given prefix after the given item
1266wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent,
1267 const wxString& prefixOrig) const
1268{
1269 // match is case insensitive as this is more convenient to the user: having
1270 // to press Shift-letter to go to the item starting with a capital letter
1271 // would be too bothersome
1272 wxString prefix = prefixOrig.Lower();
1273
526b8142
VZ
1274 // determine the starting point: we shouldn't take the current item (this
1275 // allows to switch between two items starting with the same letter just by
1276 // pressing it) but we shouldn't jump to the next one if the user is
1277 // continuing to type as otherwise he might easily skip the item he wanted
cb59313c 1278 wxTreeItemId id = idParent;
526b8142
VZ
1279 if ( prefix.length() == 1 )
1280 {
1281 id = GetNext(id);
1282 }
cb59313c 1283
526b8142 1284 // look for the item starting with the given prefix after it
cb59313c
VZ
1285 while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) )
1286 {
1287 id = GetNext(id);
1288 }
1289
526b8142
VZ
1290 // if we haven't found anything...
1291 if ( !id.IsOk() )
1292 {
1293 // ... wrap to the beginning
1294 id = GetRootItem();
1295 if ( HasFlag(wxTR_HIDE_ROOT) )
1296 {
1297 // can't select virtual root
1298 id = GetNext(id);
1299 }
1300
1301 // and try all the items (stop when we get to the one we started from)
1302 while ( id != idParent && !GetItemText(id).Lower().StartsWith(prefix) )
1303 {
1304 id = GetNext(id);
1305 }
1306 }
1307
cb59313c
VZ
1308 return id;
1309}
1310
f135ff73
VZ
1311// -----------------------------------------------------------------------------
1312// operations
1313// -----------------------------------------------------------------------------
1314
941830cb 1315wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1316 size_t previous,
1317 const wxString& text,
1318 int image, int selImage,
1319 wxTreeItemData *data)
c801d85f 1320{
941830cb 1321 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f49f2b0c
RR
1322 if ( !parent )
1323 {
1324 // should we give a warning here?
1325 return AddRoot(text, image, selImage, data);
1326 }
4832f7c0 1327
618a5e38
RR
1328 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1329
f38374d0 1330 wxGenericTreeItem *item =
2b84e565 1331 new wxGenericTreeItem( parent, text, image, selImage, data );
74bedbeb 1332
f49f2b0c
RR
1333 if ( data != NULL )
1334 {
ee4b2721 1335 data->m_pItem = item;
f49f2b0c 1336 }
74bedbeb 1337
f49f2b0c 1338 parent->Insert( item, previous );
ef44a621 1339
f49f2b0c 1340 return item;
4c681997
RR
1341}
1342
941830cb 1343wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
f135ff73
VZ
1344 int image, int selImage,
1345 wxTreeItemData *data)
4c681997 1346{
f49f2b0c 1347 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 1348
618a5e38
RR
1349 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1350
2b84e565 1351 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text,
f135ff73 1352 image, selImage, data);
cf31a1d7
VS
1353 if ( data != NULL )
1354 {
ee4b2721 1355 data->m_pItem = m_anchor;
cf31a1d7
VS
1356 }
1357
618a5e38
RR
1358 if (HasFlag(wxTR_HIDE_ROOT))
1359 {
1360 // if root is hidden, make sure we can navigate
1361 // into children
1362 m_anchor->SetHasPlus();
999723f3
VS
1363 m_anchor->Expand();
1364 CalculatePositions();
618a5e38 1365 }
f38374d0 1366
f49f2b0c
RR
1367 if (!HasFlag(wxTR_MULTIPLE))
1368 {
1369 m_current = m_key_current = m_anchor;
06b466c7 1370 m_current->SetHilight( TRUE );
f49f2b0c 1371 }
389cdc7a 1372
f49f2b0c 1373 return m_anchor;
edaa81ae 1374}
c801d85f 1375
941830cb 1376wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
f135ff73
VZ
1377 const wxString& text,
1378 int image, int selImage,
1379 wxTreeItemData *data)
c801d85f 1380{
f2593d0d 1381 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 1382}
c801d85f 1383
941830cb 1384wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f135ff73
VZ
1385 const wxTreeItemId& idPrevious,
1386 const wxString& text,
1387 int image, int selImage,
1388 wxTreeItemData *data)
c801d85f 1389{
941830cb 1390 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1391 if ( !parent )
1392 {
1393 // should we give a warning here?
1394 return AddRoot(text, image, selImage, data);
1395 }
c801d85f 1396
4855a477
JS
1397 int index = -1;
1398 if (idPrevious.IsOk())
1399 {
1400 index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1401 wxASSERT_MSG( index != wxNOT_FOUND,
1402 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1403 }
06b466c7 1404
f2593d0d
RR
1405 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1406}
1407
941830cb 1408wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
f2593d0d
RR
1409 size_t before,
1410 const wxString& text,
1411 int image, int selImage,
1412 wxTreeItemData *data)
1413{
941830cb 1414 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1415 if ( !parent )
1416 {
1417 // should we give a warning here?
1418 return AddRoot(text, image, selImage, data);
1419 }
1420
1421 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 1422}
c801d85f 1423
941830cb 1424wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
f135ff73
VZ
1425 const wxString& text,
1426 int image, int selImage,
1427 wxTreeItemData *data)
74bedbeb 1428{
941830cb 1429 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
f2593d0d
RR
1430 if ( !parent )
1431 {
1432 // should we give a warning here?
1433 return AddRoot(text, image, selImage, data);
1434 }
f135ff73 1435
f2593d0d
RR
1436 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1437 image, selImage, data);
74bedbeb
VZ
1438}
1439
941830cb 1440void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
a43a4f9d 1441{
f2593d0d 1442 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
ee4b2721 1443 event.m_item = item;
f2593d0d
RR
1444 event.SetEventObject( this );
1445 ProcessEvent( event );
a43a4f9d
VZ
1446}
1447
941830cb 1448void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
372edb9d 1449{
618a5e38
RR
1450 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1451
941830cb 1452 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
a43a4f9d 1453 item->DeleteChildren(this);
372edb9d
VZ
1454}
1455
941830cb 1456void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1457{
618a5e38
RR
1458 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1459
941830cb 1460 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
ff5bf259 1461
7475e8a3
VZ
1462 wxGenericTreeItem *parent = item->GetParent();
1463
1464 // don't keep stale pointers around!
1465 if ( IsDescendantOf(item, m_key_current) )
aaa2f297 1466 {
3e3a7b97
JS
1467 // Don't silently change the selection:
1468 // do it properly in idle time, so event
1469 // handlers get called.
1470
1471 // m_key_current = parent;
1472 m_key_current = NULL;
1473 }
1474
1475 // m_select_me records whether we need to select
1476 // a different item, in idle time.
1477 if ( m_select_me && IsDescendantOf(item, m_select_me) )
1478 {
1479 m_select_me = parent;
aaa2f297
VZ
1480 }
1481
7475e8a3
VZ
1482 if ( IsDescendantOf(item, m_current) )
1483 {
3e3a7b97
JS
1484 // Don't silently change the selection:
1485 // do it properly in idle time, so event
1486 // handlers get called.
1487
1488 // m_current = parent;
1489 m_current = NULL;
1490 m_select_me = parent;
7475e8a3
VZ
1491 }
1492
1493 // remove the item from the tree
97d7bfb8
RR
1494 if ( parent )
1495 {
1496 parent->GetChildren().Remove( item ); // remove by value
1497 }
7475e8a3 1498 else // deleting the root
aaa2f297 1499 {
7475e8a3
VZ
1500 // nothing will be left in the tree
1501 m_anchor = NULL;
aaa2f297
VZ
1502 }
1503
7475e8a3 1504 // and delete all of its children and the item itself now
97d7bfb8
RR
1505 item->DeleteChildren(this);
1506 SendDeleteEvent(item);
1507 delete item;
edaa81ae 1508}
c801d85f 1509
941830cb 1510void wxGenericTreeCtrl::DeleteAllItems()
c801d85f 1511{
97d7bfb8
RR
1512 if ( m_anchor )
1513 {
7475e8a3 1514 Delete(m_anchor);
97d7bfb8 1515 }
edaa81ae
RR
1516}
1517
941830cb 1518void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1519{
941830cb 1520 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1521
941830cb 1522 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
7a944d2f 1523 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
999723f3 1524 _T("can't expand hidden root") );
f6bcfd97 1525
f2593d0d
RR
1526 if ( !item->HasPlus() )
1527 return;
978f38c2 1528
f2593d0d
RR
1529 if ( item->IsExpanded() )
1530 return;
f135ff73 1531
f2593d0d 1532 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
ee4b2721 1533 event.m_item = item;
f2593d0d 1534 event.SetEventObject( this );
004fd0c8 1535
f2593d0d
RR
1536 if ( ProcessEvent( event ) && !event.IsAllowed() )
1537 {
1538 // cancelled by program
1539 return;
1540 }
4832f7c0 1541
f2593d0d
RR
1542 item->Expand();
1543 CalculatePositions();
f135ff73 1544
f2593d0d 1545 RefreshSubtree(item);
f135ff73 1546
f2593d0d
RR
1547 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1548 ProcessEvent( event );
edaa81ae
RR
1549}
1550
941830cb 1551void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
f6bcfd97 1552{
1fc32b12 1553 if ( !HasFlag(wxTR_HIDE_ROOT) || item != GetRootItem())
f6bcfd97 1554 {
1fc32b12
JS
1555 Expand(item);
1556 if ( !IsExpanded(item) )
1557 return;
1558 }
6151e144 1559
2d75caaa 1560 wxTreeItemIdValue cookie;
1fc32b12
JS
1561 wxTreeItemId child = GetFirstChild(item, cookie);
1562 while ( child.IsOk() )
1563 {
1564 ExpandAll(child);
6151e144 1565
1fc32b12 1566 child = GetNextChild(item, cookie);
f6bcfd97
BP
1567 }
1568}
1569
941830cb 1570void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1571{
7a944d2f 1572 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
999723f3
VS
1573 _T("can't collapse hidden root") );
1574
941830cb 1575 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
f135ff73 1576
f2593d0d
RR
1577 if ( !item->IsExpanded() )
1578 return;
f135ff73 1579
f2593d0d 1580 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
ee4b2721 1581 event.m_item = item;
f2593d0d
RR
1582 event.SetEventObject( this );
1583 if ( ProcessEvent( event ) && !event.IsAllowed() )
1584 {
1585 // cancelled by program
1586 return;
1587 }
4832f7c0 1588
f2593d0d 1589 item->Collapse();
f135ff73 1590
618a5e38 1591#if 0 // TODO why should items be collapsed recursively?
f2593d0d
RR
1592 wxArrayGenericTreeItems& children = item->GetChildren();
1593 size_t count = children.Count();
1594 for ( size_t n = 0; n < count; n++ )
1595 {
1596 Collapse(children[n]);
1597 }
618a5e38 1598#endif
f135ff73 1599
f2593d0d 1600 CalculatePositions();
f135ff73 1601
f2593d0d 1602 RefreshSubtree(item);
f135ff73 1603
f2593d0d
RR
1604 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1605 ProcessEvent( event );
edaa81ae 1606}
c801d85f 1607
941830cb 1608void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1609{
00e12320
RR
1610 Collapse(item);
1611 DeleteChildren(item);
edaa81ae 1612}
c801d85f 1613
941830cb 1614void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1615{
941830cb 1616 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
389cdc7a 1617
00e12320
RR
1618 if (item->IsExpanded())
1619 Collapse(itemId);
1620 else
1621 Expand(itemId);
f135ff73 1622}
389cdc7a 1623
941830cb 1624void wxGenericTreeCtrl::Unselect()
f135ff73 1625{
00e12320
RR
1626 if (m_current)
1627 {
1628 m_current->SetHilight( FALSE );
1629 RefreshLine( m_current );
02fe25c2
VZ
1630
1631 m_current = NULL;
3e3a7b97 1632 m_select_me = NULL;
00e12320 1633 }
edaa81ae 1634}
c801d85f 1635
941830cb 1636void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1637{
00e12320
RR
1638 if (item->IsSelected())
1639 {
1640 item->SetHilight(FALSE);
1641 RefreshLine(item);
1642 }
d3a9f4af 1643
00e12320 1644 if (item->HasChildren())
88ac883a 1645 {
00e12320
RR
1646 wxArrayGenericTreeItems& children = item->GetChildren();
1647 size_t count = children.Count();
1648 for ( size_t n = 0; n < count; ++n )
1649 {
1650 UnselectAllChildren(children[n]);
1651 }
88ac883a
VZ
1652 }
1653}
f135ff73 1654
941830cb 1655void wxGenericTreeCtrl::UnselectAll()
88ac883a 1656{
0a33446c
VZ
1657 wxTreeItemId rootItem = GetRootItem();
1658
1659 // the tree might not have the root item at all
1660 if ( rootItem )
1661 {
1662 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1663 }
88ac883a
VZ
1664}
1665
1666// Recursive function !
1667// To stop we must have crt_item<last_item
91b8de8d 1668// Algorithm :
88ac883a 1669// Tag all next children, when no more children,
d3a9f4af 1670// Move to parent (not to tag)
91b8de8d 1671// Keep going... if we found last_item, we stop.
941830cb 1672bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a
VZ
1673{
1674 wxGenericTreeItem *parent = crt_item->GetParent();
1675
00e12320
RR
1676 if (parent == NULL) // This is root item
1677 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1678
91b8de8d 1679 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1680 int index = children.Index(crt_item);
1681 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1682
1683 size_t count = children.Count();
1684 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1685 {
1686 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1687 }
88ac883a
VZ
1688
1689 return TagNextChildren(parent, last_item, select);
1690}
1691
941830cb 1692bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
88ac883a 1693{
00e12320
RR
1694 crt_item->SetHilight(select);
1695 RefreshLine(crt_item);
d3a9f4af 1696
06b466c7 1697 if (crt_item==last_item)
00e12320 1698 return TRUE;
88ac883a 1699
00e12320 1700 if (crt_item->HasChildren())
88ac883a 1701 {
00e12320
RR
1702 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1703 size_t count = children.Count();
1704 for ( size_t n = 0; n < count; ++n )
1705 {
06b466c7 1706 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1707 return TRUE;
06b466c7 1708 }
88ac883a 1709 }
d3a9f4af 1710
c0de7af4 1711 return FALSE;
88ac883a
VZ
1712}
1713
941830cb 1714void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
88ac883a 1715{
3e3a7b97 1716 m_select_me = NULL;
88ac883a 1717
999836aa 1718 // item2 is not necessary after item1
f2593d0d 1719 // choice first' and 'last' between item1 and item2
999836aa
VZ
1720 wxGenericTreeItem *first= (item1->GetY()<item2->GetY()) ? item1 : item2;
1721 wxGenericTreeItem *last = (item1->GetY()<item2->GetY()) ? item2 : item1;
88ac883a 1722
f2593d0d 1723 bool select = m_current->IsSelected();
d3a9f4af 1724
f2593d0d
RR
1725 if ( TagAllChildrenUntilLast(first,last,select) )
1726 return;
88ac883a 1727
f2593d0d 1728 TagNextChildren(first,last,select);
88ac883a
VZ
1729}
1730
78f12104
VZ
1731void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId& itemId,
1732 bool unselect_others,
1733 bool extended_select)
d3a9f4af 1734{
223d09f6 1735 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1736
3e3a7b97
JS
1737 m_select_me = NULL;
1738
88ac883a 1739 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
941830cb 1740 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
88ac883a
VZ
1741
1742 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1743 // wxT("this is a single selection tree") );
88ac883a
VZ
1744
1745 // to keep going anyhow !!!
d3a9f4af 1746 if (is_single)
8dc99046
VZ
1747 {
1748 if (item->IsSelected())
1749 return; // nothing to do
1750 unselect_others = TRUE;
1751 extended_select = FALSE;
1752 }
1753 else if ( unselect_others && item->IsSelected() )
1754 {
1755 // selection change if there is more than one item currently selected
1756 wxArrayTreeItemIds selected_items;
1757 if ( GetSelections(selected_items) == 1 )
1758 return;
1759 }
d3a9f4af 1760
f135ff73 1761 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
ee4b2721
VZ
1762 event.m_item = item;
1763 event.m_itemOld = m_current;
f135ff73 1764 event.SetEventObject( this );
91b8de8d 1765 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1766
f98e2558 1767 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
618a5e38 1768 return;
f135ff73 1769
99006e44 1770 wxTreeItemId parent = GetItemParent( itemId );
2cc78389
RR
1771 while (parent.IsOk())
1772 {
1773 if (!IsExpanded(parent))
1774 Expand( parent );
cdb3cffe 1775
99006e44 1776 parent = GetItemParent( parent );
2cc78389 1777 }
cdb3cffe 1778
2cc78389 1779 EnsureVisible( itemId );
cdb3cffe 1780
88ac883a
VZ
1781 // ctrl press
1782 if (unselect_others)
389cdc7a 1783 {
88ac883a 1784 if (is_single) Unselect(); // to speed up thing
c193b707 1785 else UnselectAll();
edaa81ae 1786 }
f135ff73 1787
88ac883a 1788 // shift press
d3a9f4af 1789 if (extended_select)
88ac883a 1790 {
aaa2f297
VZ
1791 if ( !m_current )
1792 {
618a5e38 1793 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
1794 }
1795
88ac883a
VZ
1796 // don't change the mark (m_current)
1797 SelectItemRange(m_current, item);
1798 }
1799 else
1800 {
c0de7af4 1801 bool select=TRUE; // the default
88ac883a 1802
c193b707
VZ
1803 // Check if we need to toggle hilight (ctrl mode)
1804 if (!unselect_others)
618a5e38 1805 select=!item->IsSelected();
88ac883a 1806
91b8de8d 1807 m_current = m_key_current = item;
c193b707
VZ
1808 m_current->SetHilight(select);
1809 RefreshLine( m_current );
88ac883a 1810 }
389cdc7a 1811
f135ff73 1812 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1813 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1814}
1815
78f12104
VZ
1816void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, bool select)
1817{
1818 if ( select )
1819 {
1820 DoSelectItem(itemId);
1821 }
1822 else // deselect
1823 {
1824 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1825 wxCHECK_RET( item, wxT("SelectItem(): invalid tree item") );
1826
1827 item->SetHilight(FALSE);
1828 RefreshLine(item);
1829 }
1830}
1831
941830cb 1832void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
cb59313c 1833 wxArrayTreeItemIds &array) const
c801d85f 1834{
8dc99046 1835 if ( item->IsSelected() )
9dfbf520 1836 array.Add(wxTreeItemId(item));
91b8de8d 1837
9dfbf520 1838 if ( item->HasChildren() )
91b8de8d 1839 {
9dfbf520
VZ
1840 wxArrayGenericTreeItems& children = item->GetChildren();
1841 size_t count = children.GetCount();
1842 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1843 FillArray(children[n], array);
91b8de8d
RR
1844 }
1845}
1846
941830cb 1847size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d 1848{
618a5e38
RR
1849 array.Empty();
1850 wxTreeItemId idRoot = GetRootItem();
1851 if ( idRoot.IsOk() )
1852 {
1853 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1854 }
1855 //else: the tree is empty, so no selections
91b8de8d 1856
618a5e38 1857 return array.Count();
91b8de8d
RR
1858}
1859
941830cb 1860void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1861{
91b8de8d
RR
1862 if (!item.IsOk()) return;
1863
941830cb 1864 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 1865
f65635b5
VZ
1866 // first expand all parent branches
1867 wxGenericTreeItem *parent = gitem->GetParent();
999723f3
VS
1868
1869 if ( HasFlag(wxTR_HIDE_ROOT) )
f65635b5 1870 {
ff38281a 1871 while ( parent && parent != m_anchor )
999723f3
VS
1872 {
1873 Expand(parent);
1874 parent = parent->GetParent();
1875 }
1876 }
1877 else
1878 {
1879 while ( parent )
1880 {
1881 Expand(parent);
1882 parent = parent->GetParent();
1883 }
f65635b5
VZ
1884 }
1885
5391f772 1886 //if (parent) CalculatePositions();
91b8de8d
RR
1887
1888 ScrollTo(item);
1889}
1890
941830cb 1891void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d
RR
1892{
1893 if (!item.IsOk()) return;
1894
dc6c62a9
RR
1895 // We have to call this here because the label in
1896 // question might just have been added and no screen
1897 // update taken place.
cd66f45b 1898 if (m_dirty) wxYieldIfNeeded();
dc6c62a9 1899
941830cb 1900 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1901
f65635b5 1902 // now scroll to the item
0659e7ee 1903 int item_y = gitem->GetY();
8dc99046 1904
0659e7ee
RR
1905 int start_x = 0;
1906 int start_y = 0;
1e6feb95 1907 GetViewStart( &start_x, &start_y );
91b8de8d 1908 start_y *= PIXELS_PER_UNIT;
978f38c2 1909
a93109d5
RR
1910 int client_h = 0;
1911 int client_w = 0;
1912 GetClientSize( &client_w, &client_h );
ef44a621 1913
0659e7ee
RR
1914 if (item_y < start_y+3)
1915 {
91b8de8d 1916 // going down
0659e7ee
RR
1917 int x = 0;
1918 int y = 0;
91b8de8d
RR
1919 m_anchor->GetSize( x, y, this );
1920 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1921 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1922 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1923 // Item should appear at top
91b8de8d 1924 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1925 }
91b8de8d 1926 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1927 {
c7a9fa36
RR
1928 // going up
1929 int x = 0;
1930 int y = 0;
1931 m_anchor->GetSize( x, y, this );
1932 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1933 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1934 item_y += PIXELS_PER_UNIT+2;
1935 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1936 // Item should appear at bottom
c7a9fa36 1937 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 1938 }
edaa81ae 1939}
c801d85f 1940
e1ee62bd 1941// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 1942static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1943
004fd0c8 1944static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1945 wxGenericTreeItem **item2)
edaa81ae 1946{
941830cb 1947 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1948
1949 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1950}
1951
941830cb 1952int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
e1ee62bd 1953 const wxTreeItemId& item2)
0659e7ee 1954{
87138c52 1955 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1956}
1957
941830cb 1958void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 1959{
223d09f6 1960 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 1961
941830cb 1962 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 1963
e1ee62bd 1964 wxCHECK_RET( !s_treeBeingSorted,
941830cb 1965 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1966
91b8de8d 1967 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1968 if ( children.Count() > 1 )
1969 {
618a5e38
RR
1970 m_dirty = TRUE;
1971
e1ee62bd
VZ
1972 s_treeBeingSorted = this;
1973 children.Sort(tree_ctrl_compare_func);
1974 s_treeBeingSorted = NULL;
e1ee62bd
VZ
1975 }
1976 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1977}
1978
941830cb 1979wxImageList *wxGenericTreeCtrl::GetImageList() const
edaa81ae 1980{
f135ff73 1981 return m_imageListNormal;
edaa81ae
RR
1982}
1983
618a5e38
RR
1984wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
1985{
1986 return m_imageListButtons;
1987}
1988
941830cb 1989wxImageList *wxGenericTreeCtrl::GetStateImageList() const
c801d85f 1990{
f135ff73 1991 return m_imageListState;
edaa81ae 1992}
c801d85f 1993
618a5e38 1994void wxGenericTreeCtrl::CalculateLineHeight()
e2414cbe 1995{
f2593d0d
RR
1996 wxClientDC dc(this);
1997 m_lineHeight = (int)(dc.GetCharHeight() + 4);
06b466c7 1998
618a5e38
RR
1999 if ( m_imageListNormal )
2000 {
2001 // Calculate a m_lineHeight value from the normal Image sizes.
2002 // May be toggle off. Then wxGenericTreeCtrl will spread when
2003 // necessary (which might look ugly).
2004 int n = m_imageListNormal->GetImageCount();
2005 for (int i = 0; i < n ; i++)
2006 {
2007 int width = 0, height = 0;
2008 m_imageListNormal->GetSize(i, width, height);
2009 if (height > m_lineHeight) m_lineHeight = height;
2010 }
2011 }
2012
2013 if (m_imageListButtons)
f2593d0d 2014 {
618a5e38
RR
2015 // Calculate a m_lineHeight value from the Button image sizes.
2016 // May be toggle off. Then wxGenericTreeCtrl will spread when
2017 // necessary (which might look ugly).
2018 int n = m_imageListButtons->GetImageCount();
2019 for (int i = 0; i < n ; i++)
2020 {
2021 int width = 0, height = 0;
2022 m_imageListButtons->GetSize(i, width, height);
2023 if (height > m_lineHeight) m_lineHeight = height;
2024 }
f2593d0d 2025 }
91b8de8d 2026
618a5e38 2027 if (m_lineHeight < 30)
f2593d0d 2028 m_lineHeight += 2; // at least 2 pixels
06b466c7 2029 else
f2593d0d 2030 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 2031}
e2414cbe 2032
618a5e38
RR
2033void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
2034{
2035 if (m_ownsImageListNormal) delete m_imageListNormal;
2036 m_imageListNormal = imageList;
2037 m_ownsImageListNormal = FALSE;
2038 m_dirty = TRUE;
f4bd6759
JS
2039 // Don't do any drawing if we're setting the list to NULL,
2040 // since we may be in the process of deleting the tree control.
2041 if (imageList)
2042 CalculateLineHeight();
618a5e38
RR
2043}
2044
941830cb 2045void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 2046{
46cd520d 2047 if (m_ownsImageListState) delete m_imageListState;
f135ff73 2048 m_imageListState = imageList;
46cd520d
VS
2049 m_ownsImageListState = FALSE;
2050}
2051
618a5e38
RR
2052void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
2053{
2054 if (m_ownsImageListButtons) delete m_imageListButtons;
2055 m_imageListButtons = imageList;
2056 m_ownsImageListButtons = FALSE;
2057 m_dirty = TRUE;
2058 CalculateLineHeight();
2059}
2060
46cd520d
VS
2061void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
2062{
2063 SetImageList(imageList);
2064 m_ownsImageListNormal = TRUE;
2065}
2066
2067void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
2068{
2069 SetStateImageList(imageList);
2070 m_ownsImageListState = TRUE;
edaa81ae 2071}
e2414cbe 2072
618a5e38
RR
2073void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
2074{
2075 SetButtonsImageList(imageList);
2076 m_ownsImageListButtons = TRUE;
2077}
2078
f135ff73
VZ
2079// -----------------------------------------------------------------------------
2080// helpers
2081// -----------------------------------------------------------------------------
0659e7ee 2082
941830cb 2083void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 2084{
0659e7ee
RR
2085 if (m_anchor)
2086 {
618a5e38 2087 int x = 0, y = 0;
91b8de8d 2088 m_anchor->GetSize( x, y, this );
c193b707 2089 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 2090 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
2091 int x_pos = GetScrollPos( wxHORIZONTAL );
2092 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 2093 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
2094 }
2095 else
2096 {
2097 SetScrollbars( 0, 0, 0, 0 );
2098 }
edaa81ae 2099}
c801d85f 2100
941830cb 2101int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 2102{
dc6c62a9
RR
2103 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2104 return item->GetHeight();
2105 else
2106 return m_lineHeight;
91b8de8d
RR
2107}
2108
941830cb 2109void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 2110{
618a5e38
RR
2111 // TODO implement "state" icon on items
2112
9ec64fa7
VZ
2113 wxTreeItemAttr *attr = item->GetAttributes();
2114 if ( attr && attr->HasFont() )
2115 dc.SetFont(attr->GetFont());
2116 else if (item->IsBold())
eff869aa 2117 dc.SetFont(m_boldFont);
ef44a621 2118
618a5e38 2119 long text_w = 0, text_h = 0;
bbe0af5b 2120 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 2121
618a5e38 2122 int image_h = 0, image_w = 0;
8dc99046
VZ
2123 int image = item->GetCurrentImage();
2124 if ( image != NO_IMAGE )
bbe0af5b 2125 {
2c8e4738
VZ
2126 if ( m_imageListNormal )
2127 {
2128 m_imageListNormal->GetSize( image, image_w, image_h );
2129 image_w += 4;
2130 }
2131 else
2132 {
2133 image = NO_IMAGE;
2134 }
bbe0af5b 2135 }
ef44a621 2136
91b8de8d
RR
2137 int total_h = GetLineHeight(item);
2138
b771aa29 2139 if ( item->IsSelected() )
cdb3cffe 2140 {
44c44c82
SC
2141// under mac selections are only a rectangle in case they don't have the focus
2142#ifdef __WXMAC__
2143 if ( !m_hasFocus )
2144 {
2145 dc.SetBrush( *wxTRANSPARENT_BRUSH ) ;
2146 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) , 1 , wxSOLID ) ) ;
2147 }
2148 else
2149 {
2150 dc.SetBrush( *m_hilightBrush ) ;
2151 }
2152#else
b771aa29 2153 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
44c44c82 2154#endif
cdb3cffe 2155 }
afa6a1a1 2156 else
c0fba4d1
VS
2157 {
2158 wxColour colBg;
2159 if ( attr && attr->HasBackgroundColour() )
2160 colBg = attr->GetBackgroundColour();
2161 else
2162 colBg = m_backgroundColour;
2163 dc.SetBrush(wxBrush(colBg, wxSOLID));
2164 }
afa6a1a1 2165
cdb3cffe 2166 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
b77d9650 2167
c6f4913a 2168 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
a69cb1cc 2169 {
c6f4913a
VS
2170 int x, y, w, h;
2171
2172 DoGetPosition(&x, &y);
2173 DoGetSize(&w, &h);
2174 dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset);
a69cb1cc
JS
2175 }
2176 else
b77d9650 2177 {
c6f4913a
VS
2178 if ( item->IsSelected() && image != NO_IMAGE )
2179 {
2180 // If it's selected, and there's an image, then we should
2181 // take care to leave the area under the image painted in the
2182 // background colour.
2183 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
2184 item->GetWidth() - image_w + 2, total_h-offset );
2185 }
2186 else
2187 {
2188 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
2189 item->GetWidth()+2, total_h-offset );
2190 }
b77d9650 2191 }
ef44a621 2192
8dc99046 2193 if ( image != NO_IMAGE )
bbe0af5b 2194 {
d30b4d20 2195 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 2196 m_imageListNormal->Draw( image, dc,
49cd56ef 2197 item->GetX(),
d701d432 2198 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
2199 wxIMAGELIST_DRAW_TRANSPARENT );
2200 dc.DestroyClippingRegion();
2201 }
ef44a621 2202
afa6a1a1 2203 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
2204 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2205 dc.DrawText( item->GetText(),
2206 (wxCoord)(image_w + item->GetX()),
2207 (wxCoord)(item->GetY() + extraH));
ef44a621 2208
eff869aa
RR
2209 // restore normal font
2210 dc.SetFont( m_normalFont );
ef44a621
VZ
2211}
2212
91b8de8d 2213// Now y stands for the top of the item, whereas it used to stand for middle !
941830cb 2214void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2215{
618a5e38
RR
2216 int x = level*m_indent;
2217 if (!HasFlag(wxTR_HIDE_ROOT))
2218 {
2219 x += m_indent;
2220 }
2221 else if (level == 0)
2222 {
2b84e565
VS
2223 // always expand hidden root
2224 int origY = y;
618a5e38 2225 wxArrayGenericTreeItems& children = item->GetChildren();
2b84e565
VS
2226 int count = children.Count();
2227 if (count > 0)
2228 {
2229 int n = 0, oldY;
2230 do {
2231 oldY = y;
2232 PaintLevel(children[n], dc, 1, y);
2233 } while (++n < count);
2234
e76520fd 2235 if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
2b84e565
VS
2236 {
2237 // draw line down to last child
2238 origY += GetLineHeight(children[0])>>1;
2239 oldY += GetLineHeight(children[n-1])>>1;
2240 dc.DrawLine(3, origY, 3, oldY);
2241 }
2242 }
618a5e38
RR
2243 return;
2244 }
91b8de8d 2245
618a5e38
RR
2246 item->SetX(x+m_spacing);
2247 item->SetY(y);
389cdc7a 2248
618a5e38
RR
2249 int h = GetLineHeight(item);
2250 int y_top = y;
2251 int y_mid = y_top + (h>>1);
2252 y += h;
4832f7c0 2253
618a5e38
RR
2254 int exposed_x = dc.LogicalToDeviceX(0);
2255 int exposed_y = dc.LogicalToDeviceY(y_top);
233058c7 2256
618a5e38 2257 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
bbe0af5b 2258 {
c6f4913a
VS
2259 wxPen *pen =
2260#ifndef __WXMAC__
2261 // don't draw rect outline if we already have the
2262 // background color under Mac
2263 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2264#endif // !__WXMAC__
2265 wxTRANSPARENT_PEN;
2266
2267 wxColour colText;
2268 if ( item->IsSelected() )
2269 {
a756f210 2270 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
c6f4913a
VS
2271 }
2272 else
2273 {
2274 wxTreeItemAttr *attr = item->GetAttributes();
2275 if (attr && attr->HasTextColour())
2276 colText = attr->GetTextColour();
2277 else
6f0dd6b2 2278 colText = GetForegroundColour();
c6f4913a
VS
2279 }
2280
2281 // prepare to draw
2282 dc.SetTextForeground(colText);
2283 dc.SetPen(*pen);
2284
2285 // draw
2286 PaintItem(item, dc);
2287
2288 if (HasFlag(wxTR_ROW_LINES))
2289 {
2290 // if the background colour is white, choose a
2291 // contrasting color for the lines
2292 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2293 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2294 dc.DrawLine(0, y_top, 10000, y_top);
2295 dc.DrawLine(0, y, 10000, y);
2296 }
2297
2298 // restore DC objects
2299 dc.SetBrush(*wxWHITE_BRUSH);
2300 dc.SetPen(m_dottedPen);
2301 dc.SetTextForeground(*wxBLACK);
2302
9c7f49f5 2303 if ( !HasFlag(wxTR_NO_LINES) )
cdb3cffe 2304 {
9c7f49f5
VZ
2305 // draw the horizontal line here
2306 int x_start = x;
2307 if (x > (signed)m_indent)
2308 x_start -= m_indent;
2309 else if (HasFlag(wxTR_LINES_AT_ROOT))
2310 x_start = 3;
2311 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2312 }
618a5e38 2313
9c7f49f5
VZ
2314 // should the item show a button?
2315 if ( item->HasPlus() && HasButtons() )
2316 {
2317 if ( m_imageListButtons )
c22886bd 2318 {
618a5e38 2319 // draw the image button here
9c7f49f5
VZ
2320 int image_h = 0,
2321 image_w = 0;
2322 int image = item->IsExpanded() ? wxTreeItemIcon_Expanded
2323 : wxTreeItemIcon_Normal;
2324 if ( item->IsSelected() )
2b84e565 2325 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
9c7f49f5 2326
618a5e38 2327 m_imageListButtons->GetSize(image, image_w, image_h);
9c7f49f5
VZ
2328 int xx = x - image_w/2;
2329 int yy = y_mid - image_h/2;
2330
2331 wxDCClipper clip(dc, xx, yy, image_w, image_h);
618a5e38
RR
2332 m_imageListButtons->Draw(image, dc, xx, yy,
2333 wxIMAGELIST_DRAW_TRANSPARENT);
c22886bd 2334 }
9c7f49f5 2335 else // no custom buttons
c22886bd 2336 {
0e7761fa
VZ
2337 static const int wImage = 9;
2338 static const int hImage = 9;
f8b043e7
RR
2339
2340 int flag = 0;
2341 if (item->IsExpanded())
2342 flag |= wxCONTROL_EXPANDED;
2343 if (item == m_underMouse)
2344 flag |= wxCONTROL_CURRENT;
2345
9c7f49f5
VZ
2346 wxRendererNative::Get().DrawTreeItemButton
2347 (
2348 this,
2349 dc,
2350 wxRect(x - wImage/2,
2351 y_mid - hImage/2,
2352 wImage, hImage),
f8b043e7 2353 flag
9c7f49f5 2354 );
c22886bd 2355 }
bbe0af5b 2356 }
f135ff73 2357 }
d3a9f4af 2358
bbe0af5b
RR
2359 if (item->IsExpanded())
2360 {
91b8de8d 2361 wxArrayGenericTreeItems& children = item->GetChildren();
618a5e38 2362 int count = children.Count();
f65635b5 2363 if (count > 0)
9ec64fa7 2364 {
618a5e38
RR
2365 int n = 0, oldY;
2366 ++level;
2367 do {
2368 oldY = y;
2369 PaintLevel(children[n], dc, level, y);
2370 } while (++n < count);
2371
e76520fd 2372 if (!HasFlag(wxTR_NO_LINES) && count > 0)
618a5e38
RR
2373 {
2374 // draw line down to last child
2b84e565 2375 oldY += GetLineHeight(children[n-1])>>1;
618a5e38 2376 if (HasButtons()) y_mid += 5;
dd360466
RD
2377
2378 // Only draw the portion of the line that is visible, in case it is huge
2379 wxCoord xOrigin=0, yOrigin=0, width, height;
2380 dc.GetDeviceOrigin(&xOrigin, &yOrigin);
2381 yOrigin = abs(yOrigin);
2382 GetClientSize(&width, &height);
2383
2384 // Move end points to the begining/end of the view?
2385 if (y_mid < yOrigin)
2386 y_mid = yOrigin;
2387 if (oldY > yOrigin + height)
2388 oldY = yOrigin + height;
2389
2390 // after the adjustments if y_mid is larger than oldY then the line
2391 // isn't visible at all so don't draw anything
2392 if (y_mid < oldY)
2393 dc.DrawLine(x, y_mid, x, oldY);
618a5e38 2394 }
9ec64fa7 2395 }
bbe0af5b 2396 }
4c681997 2397}
c801d85f 2398
941830cb 2399void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
2400{
2401 if ( item )
2402 {
2403 if ( item->HasPlus() )
2404 {
2405 // it's a folder, indicate it by a border
2406 DrawBorder(item);
2407 }
2408 else
2409 {
2410 // draw a line under the drop target because the item will be
2411 // dropped there
2412 DrawLine(item, TRUE /* below */);
2413 }
2414
2415 SetCursor(wxCURSOR_BULLSEYE);
2416 }
2417 else
2418 {
2419 // can't drop here
2420 SetCursor(wxCURSOR_NO_ENTRY);
2421 }
2422}
2423
941830cb 2424void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 2425{
941830cb 2426 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2427
941830cb 2428 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2429
1044a386 2430 wxClientDC dc(this);
91b8de8d
RR
2431 PrepareDC( dc );
2432 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 2433 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 2434
3dbeaa52
VZ
2435 int w = i->GetWidth() + 2;
2436 int h = GetLineHeight(i) + 2;
91b8de8d 2437
3dbeaa52 2438 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
2439}
2440
941830cb 2441void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 2442{
941830cb 2443 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2444
941830cb 2445 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2446
1044a386 2447 wxClientDC dc(this);
91b8de8d
RR
2448 PrepareDC( dc );
2449 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 2450
3dbeaa52
VZ
2451 int x = i->GetX(),
2452 y = i->GetY();
2453 if ( below )
2454 {
2455 y += GetLineHeight(i) - 1;
2456 }
91b8de8d 2457
3dbeaa52 2458 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
2459}
2460
f135ff73
VZ
2461// -----------------------------------------------------------------------------
2462// wxWindows callbacks
2463// -----------------------------------------------------------------------------
2464
941830cb 2465void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 2466{
0659e7ee
RR
2467 wxPaintDC dc(this);
2468 PrepareDC( dc );
29d87bba 2469
941830cb
JS
2470 if ( !m_anchor)
2471 return;
2472
eff869aa 2473 dc.SetFont( m_normalFont );
0659e7ee 2474 dc.SetPen( m_dottedPen );
f38374d0 2475
eff869aa 2476 // this is now done dynamically
91b8de8d
RR
2477 //if(GetImageList() == NULL)
2478 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2479
91b8de8d 2480 int y = 2;
0659e7ee 2481 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 2482}
c801d85f 2483
b771aa29 2484void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
c801d85f 2485{
0659e7ee 2486 m_hasFocus = TRUE;
978f38c2 2487
b771aa29
VZ
2488 RefreshSelected();
2489
2490 event.Skip();
edaa81ae 2491}
c801d85f 2492
b771aa29 2493void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
c801d85f 2494{
0659e7ee 2495 m_hasFocus = FALSE;
978f38c2 2496
b771aa29
VZ
2497 RefreshSelected();
2498
2499 event.Skip();
edaa81ae 2500}
c801d85f 2501
941830cb 2502void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
c801d85f 2503{
978f38c2 2504 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
b09bda68 2505 te.m_evtKey = event;
978f38c2 2506 te.SetEventObject( this );
68eb5f63
VZ
2507 if ( GetEventHandler()->ProcessEvent( te ) )
2508 {
2509 // intercepted by the user code
2510 return;
2511 }
435fe83e 2512
91b8de8d 2513 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
2514 {
2515 event.Skip();
2516 return;
2517 }
ef44a621 2518
06b466c7
VZ
2519 // how should the selection work for this event?
2520 bool is_multiple, extended_select, unselect_others;
2521 EventFlagsToSelType(GetWindowStyleFlag(),
2522 event.ShiftDown(),
2523 event.ControlDown(),
dfc6cd93
SB
2524 is_multiple, extended_select, unselect_others);
2525
2526 // + : Expand
2527 // - : Collaspe
f6bcfd97 2528 // * : Expand all/Collapse all
dfc6cd93
SB
2529 // ' ' | return : activate
2530 // up : go up (not last children!)
2531 // down : go down
2532 // left : go to parent
2533 // right : open if parent and go next
2534 // home : go to root
2535 // end : go to last item without opening parents
cb59313c 2536 // alnum : start or continue searching for the item with this prefix
12a3f227 2537 int keyCode = event.GetKeyCode();
cb59313c 2538 switch ( keyCode )
978f38c2
VZ
2539 {
2540 case '+':
2541 case WXK_ADD:
2542 if (m_current->HasPlus() && !IsExpanded(m_current))
2543 {
2544 Expand(m_current);
2545 }
2546 break;
ef44a621 2547
f6bcfd97
BP
2548 case '*':
2549 case WXK_MULTIPLY:
2550 if ( !IsExpanded(m_current) )
2551 {
2552 // expand all
2553 ExpandAll(m_current);
2554 break;
2555 }
2556 //else: fall through to Collapse() it
2557
978f38c2
VZ
2558 case '-':
2559 case WXK_SUBTRACT:
2560 if (IsExpanded(m_current))
2561 {
2562 Collapse(m_current);
2563 }
2564 break;
ef44a621 2565
978f38c2
VZ
2566 case ' ':
2567 case WXK_RETURN:
6151e144 2568 if ( !event.HasModifiers() )
978f38c2
VZ
2569 {
2570 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
ee4b2721 2571 event.m_item = m_current;
978f38c2
VZ
2572 event.SetEventObject( this );
2573 GetEventHandler()->ProcessEvent( event );
2574 }
6151e144
VZ
2575
2576 // in any case, also generate the normal key event for this key,
2577 // even if we generated the ACTIVATED event above: this is what
2578 // wxMSW does and it makes sense because you might not want to
2579 // process ACTIVATED event at all and handle Space and Return
2580 // directly (and differently) which would be impossible otherwise
2581 event.Skip();
978f38c2 2582 break;
ef44a621 2583
618a5e38
RR
2584 // up goes to the previous sibling or to the last
2585 // of its children if it's expanded
978f38c2
VZ
2586 case WXK_UP:
2587 {
91b8de8d 2588 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
2589 if (!prev)
2590 {
99006e44 2591 prev = GetItemParent( m_key_current );
618a5e38
RR
2592 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2593 {
2594 break; // don't go to root if it is hidden
2595 }
c193b707
VZ
2596 if (prev)
2597 {
2d75caaa 2598 wxTreeItemIdValue cookie;
91b8de8d 2599 wxTreeItemId current = m_key_current;
618a5e38
RR
2600 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2601 if (current == GetFirstChild( prev, cookie ))
90e58684
RR
2602 {
2603 // otherwise we return to where we came from
78f12104 2604 DoSelectItem( prev, unselect_others, extended_select );
941830cb 2605 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
90e58684 2606 break;
c193b707 2607 }
978f38c2
VZ
2608 }
2609 }
2610 if (prev)
2611 {
69a282d4 2612 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 2613 {
69a282d4
VZ
2614 wxTreeItemId child = GetLastChild(prev);
2615 if ( child )
2616 {
2617 prev = child;
2618 }
978f38c2 2619 }
69a282d4 2620
78f12104 2621 DoSelectItem( prev, unselect_others, extended_select );
941830cb 2622 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
2623 }
2624 }
2625 break;
ef44a621 2626
978f38c2
VZ
2627 // left arrow goes to the parent
2628 case WXK_LEFT:
2629 {
99006e44 2630 wxTreeItemId prev = GetItemParent( m_current );
618a5e38
RR
2631 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2632 {
2633 // don't go to root if it is hidden
2634 prev = GetPrevSibling( m_current );
2635 }
978f38c2
VZ
2636 if (prev)
2637 {
78f12104 2638 DoSelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2639 }
2640 }
2641 break;
ef44a621 2642
978f38c2 2643 case WXK_RIGHT:
618a5e38
RR
2644 // this works the same as the down arrow except that we
2645 // also expand the item if it wasn't expanded yet
978f38c2
VZ
2646 Expand(m_current);
2647 // fall through
2648
2649 case WXK_DOWN:
ef44a621 2650 {
91b8de8d 2651 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2 2652 {
2d75caaa 2653 wxTreeItemIdValue cookie;
91b8de8d 2654 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
78f12104 2655 DoSelectItem( child, unselect_others, extended_select );
941830cb 2656 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
2657 }
2658 else
2659 {
91b8de8d 2660 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2661 if (!next)
978f38c2 2662 {
91b8de8d 2663 wxTreeItemId current = m_key_current;
a0fad723 2664 while (current.IsOk() && !next)
978f38c2 2665 {
99006e44 2666 current = GetItemParent( current );
978f38c2
VZ
2667 if (current) next = GetNextSibling( current );
2668 }
2669 }
b62c3631 2670 if (next)
978f38c2 2671 {
78f12104 2672 DoSelectItem( next, unselect_others, extended_select );
941830cb 2673 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
2674 }
2675 }
ef44a621 2676 }
978f38c2 2677 break;
ef44a621 2678
978f38c2
VZ
2679 // <End> selects the last visible tree item
2680 case WXK_END:
2681 {
2682 wxTreeItemId last = GetRootItem();
2683
2684 while ( last.IsOk() && IsExpanded(last) )
2685 {
2686 wxTreeItemId lastChild = GetLastChild(last);
2687
2688 // it may happen if the item was expanded but then all of
2689 // its children have been deleted - so IsExpanded() returned
2690 // TRUE, but GetLastChild() returned invalid item
2691 if ( !lastChild )
2692 break;
2693
2694 last = lastChild;
2695 }
2696
2697 if ( last.IsOk() )
2698 {
78f12104 2699 DoSelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2700 }
2701 }
2702 break;
2703
2704 // <Home> selects the root item
2705 case WXK_HOME:
2706 {
2707 wxTreeItemId prev = GetRootItem();
cb59313c
VZ
2708 if (!prev)
2709 break;
2710
2711 if ( HasFlag(wxTR_HIDE_ROOT) )
978f38c2 2712 {
2d75caaa
VZ
2713 wxTreeItemIdValue cookie;
2714 prev = GetFirstChild(prev, cookie);
cb59313c
VZ
2715 if (!prev)
2716 break;
978f38c2 2717 }
cb59313c 2718
78f12104 2719 DoSelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2720 }
2721 break;
2722
2723 default:
cb59313c 2724 // do not use wxIsalnum() here
6151e144 2725 if ( !event.HasModifiers() &&
1ec3a984
RR
2726 ((keyCode >= '0' && keyCode <= '9') ||
2727 (keyCode >= 'a' && keyCode <= 'z') ||
2728 (keyCode >= 'A' && keyCode <= 'Z' )))
cb59313c
VZ
2729 {
2730 // find the next item starting with the given prefix
2731 char ch = (char)keyCode;
6151e144 2732
b3887715 2733 wxTreeItemId id = FindItem(m_current, m_findPrefix + (wxChar)ch);
cb59313c
VZ
2734 if ( !id.IsOk() )
2735 {
2736 // no such item
2737 break;
2738 }
2739
2740 SelectItem(id);
2741
2742 m_findPrefix += ch;
2743
2744 // also start the timer to reset the current prefix if the user
2745 // doesn't press any more alnum keys soon -- we wouldn't want
2746 // to use this prefix for a new item search
2747 if ( !m_findTimer )
2748 {
2749 m_findTimer = new wxTreeFindTimer(this);
2750 }
2751
2752 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
2753 }
2754 else
2755 {
2756 event.Skip();
2757 }
978f38c2 2758 }
edaa81ae 2759}
c801d85f 2760
941830cb 2761wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 2762{
618a5e38
RR
2763 // JACS: removed wxYieldIfNeeded() because it can cause the window
2764 // to be deleted from under us if a close window event is pending
dc6c62a9 2765
91b8de8d
RR
2766 int w, h;
2767 GetSize(&w, &h);
91b8de8d 2768 flags=0;
618a5e38
RR
2769 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2770 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2771 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2772 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2773 if (flags) return wxTreeItemId();
d3a9f4af 2774
618a5e38
RR
2775 if (m_anchor == NULL)
2776 {
2777 flags = wxTREE_HITTEST_NOWHERE;
2778 return wxTreeItemId();
2779 }
5716a1ab 2780
d027179b
VS
2781 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
2782 this, flags, 0);
618a5e38
RR
2783 if (hit == NULL)
2784 {
2785 flags = wxTREE_HITTEST_NOWHERE;
2786 return wxTreeItemId();
2787 }
2788 return hit;
4f22cf8d
RR
2789}
2790
8fb3bfe2
JS
2791// get the bounding rectangle of the item (or of its label only)
2792bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
edb8f298
VZ
2793 wxRect& rect,
2794 bool WXUNUSED(textOnly)) const
8fb3bfe2 2795{
601c163b 2796 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
8fb3bfe2
JS
2797
2798 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2799
2800 int startX, startY;
2801 GetViewStart(& startX, & startY);
2802
1ed01484 2803 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
c22886bd 2804 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
1ed01484 2805 rect.width = i->GetWidth();
c22886bd
RR
2806 //rect.height = i->GetHeight();
2807 rect.height = GetLineHeight(i);
8fb3bfe2
JS
2808
2809 return TRUE;
8fb3bfe2
JS
2810}
2811
941830cb 2812void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
e179bd65 2813{
edb8f298 2814 wxCHECK_RET( item.IsOk(), _T("can't edit an invalid item") );
e179bd65 2815
edb8f298 2816 wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem;
9dfbf520 2817
e179bd65 2818 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
ee4b2721 2819 te.m_item = itemEdit;
e179bd65 2820 te.SetEventObject( this );
edb8f298
VZ
2821 if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() )
2822 {
2823 // vetoed by user
2824 return;
2825 }
8dc99046 2826
dc6c62a9
RR
2827 // We have to call this here because the label in
2828 // question might just have been added and no screen
2829 // update taken place.
edb8f298
VZ
2830 if ( m_dirty )
2831 wxYieldIfNeeded();
9dfbf520 2832
fbb12260 2833 m_textCtrl = new wxTreeTextCtrl(this, itemEdit);
8dc99046 2834
fbb12260
JS
2835 m_textCtrl->SetFocus();
2836}
2837
2838// returns a pointer to the text edit control if the item is being
2839// edited, NULL otherwise (it's assumed that no more than one item may
2840// be edited simultaneously)
2841wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const
2842{
2843 return m_textCtrl;
e179bd65
RR
2844}
2845
edb8f298
VZ
2846bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
2847 const wxString& value)
e179bd65 2848{
e179bd65 2849 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
ee4b2721 2850 le.m_item = item;
e179bd65 2851 le.SetEventObject( this );
edb8f298 2852 le.m_label = value;
dd23c25c 2853 le.m_editCancelled = FALSE;
9dfbf520 2854
edb8f298
VZ
2855 return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2856}
9dfbf520 2857
dd23c25c
JS
2858void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item)
2859{
2860 // let owner know that the edit was cancelled
2861 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
ee4b2721 2862 le.m_item = item;
dd23c25c
JS
2863 le.SetEventObject( this );
2864 le.m_label = wxEmptyString;
86586459 2865 le.m_editCancelled = TRUE;
dd23c25c
JS
2866
2867 GetEventHandler()->ProcessEvent( le );
2868}
2869
2870
2871
2872
edb8f298
VZ
2873void wxGenericTreeCtrl::OnRenameTimer()
2874{
2875 Edit( m_current );
e179bd65 2876}
9dfbf520 2877
941830cb 2878void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2879{
bbe0af5b 2880 if ( !m_anchor ) return;
978f38c2 2881
f8b043e7
RR
2882 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
2883
2884 // Is the mouse over a tree item button?
2885 int flags = 0;
73bb6776
JS
2886 wxGenericTreeItem *thisItem = m_anchor->HitTest(pt, this, flags, 0);
2887 wxGenericTreeItem *underMouse = thisItem;
2888 bool underMouseChanged = (underMouse != m_underMouse) ;
2889
f8b043e7
RR
2890 if ((underMouse) &&
2891 (flags & wxTREE_HITTEST_ONITEMBUTTON) &&
2892 (!event.LeftIsDown()) &&
2893 (!m_isDragging) &&
2894 (!m_renameTimer || !m_renameTimer->IsRunning()))
2895 {
2896 }
2897 else
2898 {
2899 underMouse = NULL;
2900 }
2901
2902 if (underMouse != m_underMouse)
2903 {
2904 if (m_underMouse)
2905 {
2906 // unhighlight old item
2907 wxGenericTreeItem *tmp = m_underMouse;
2908 m_underMouse = NULL;
2909 RefreshLine( tmp );
2910 }
2911
2912 m_underMouse = underMouse;
2913 if (m_underMouse)
2914 RefreshLine( m_underMouse );
2915 }
2916
5b5ea466 2917#if wxUSE_TOOLTIPS
4a5d781c 2918 // Determines what item we are hovering over and need a tooltip for
73bb6776 2919 wxTreeItemId hoverItem = thisItem;
4a5d781c
JS
2920
2921 // We do not want a tooltip if we are dragging, or if the rename timer is running
73bb6776 2922 if (underMouseChanged && hoverItem.IsOk() && !m_isDragging && (!m_renameTimer || !m_renameTimer->IsRunning()))
4a5d781c
JS
2923 {
2924 // Ask the tree control what tooltip (if any) should be shown
2925 wxTreeEvent hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, GetId());
bc0aebab 2926 hevent.m_item = hoverItem;
4a5d781c
JS
2927 hevent.SetEventObject(this);
2928
2929 if ( GetEventHandler()->ProcessEvent(hevent) && hevent.IsAllowed() )
2930 {
2931 SetToolTip(hevent.m_label);
2932 }
2933 }
5b5ea466 2934#endif
4a5d781c 2935
3dbeaa52
VZ
2936 // we process left mouse up event (enables in-place edit), right down
2937 // (pass to the user code), left dbl click (activate item) and
2938 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
2939 if ( !(event.LeftDown() ||
2940 event.LeftUp() ||
3dbeaa52
VZ
2941 event.RightDown() ||
2942 event.LeftDClick() ||
2943 event.Dragging() ||
2944 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2945 {
2946 event.Skip();
2947
2948 return;
2949 }
2950
29d87bba 2951
f8b043e7 2952 flags = 0;
d027179b 2953 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
3dbeaa52 2954
3dbeaa52 2955 if ( event.Dragging() && !m_isDragging )
bbe0af5b 2956 {
fd9811b1 2957 if (m_dragCount == 0)
d027179b 2958 m_dragStart = pt;
8dc99046 2959
fd9811b1 2960 m_dragCount++;
8dc99046 2961
3dbeaa52
VZ
2962 if (m_dragCount != 3)
2963 {
2964 // wait until user drags a bit further...
2965 return;
2966 }
8dc99046 2967
3dbeaa52
VZ
2968 wxEventType command = event.RightIsDown()
2969 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2970 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 2971
fd9811b1 2972 wxTreeEvent nevent( command, GetId() );
ee4b2721 2973 nevent.m_item = m_current;
fd9811b1 2974 nevent.SetEventObject(this);
3dbeaa52
VZ
2975
2976 // by default the dragging is not supported, the user code must
2977 // explicitly allow the event for it to take place
2978 nevent.Veto();
2979
2980 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2981 {
2982 // we're going to drag this item
2983 m_isDragging = TRUE;
2984
b3a7510d
VZ
2985 // remember the old cursor because we will change it while
2986 // dragging
2987 m_oldCursor = m_cursor;
2988
2989 // in a single selection control, hide the selection temporarily
2990 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2991 {
941830cb 2992 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
2993
2994 if ( m_oldSelection )
2995 {
2996 m_oldSelection->SetHilight(FALSE);
2997 RefreshLine(m_oldSelection);
2998 }
2999 }
3000
3dbeaa52
VZ
3001 CaptureMouse();
3002 }
bbe0af5b 3003 }
3dbeaa52 3004 else if ( event.Moving() )
fd9811b1 3005 {
3dbeaa52
VZ
3006 if ( item != m_dropTarget )
3007 {
3008 // unhighlight the previous drop target
3009 DrawDropEffect(m_dropTarget);
fd9811b1 3010
3dbeaa52 3011 m_dropTarget = item;
978f38c2 3012
3dbeaa52
VZ
3013 // highlight the current drop target if any
3014 DrawDropEffect(m_dropTarget);
fb882e1c 3015
cd66f45b 3016 wxYieldIfNeeded();
3dbeaa52 3017 }
e179bd65 3018 }
3dbeaa52
VZ
3019 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
3020 {
3021 // erase the highlighting
3022 DrawDropEffect(m_dropTarget);
9dfbf520 3023
4f5fffcc
RR
3024 if ( m_oldSelection )
3025 {
3026 m_oldSelection->SetHilight(TRUE);
3027 RefreshLine(m_oldSelection);
3028 m_oldSelection = (wxGenericTreeItem *)NULL;
3029 }
3030
3dbeaa52
VZ
3031 // generate the drag end event
3032 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 3033
ee4b2721 3034 event.m_item = item;
d027179b 3035 event.m_pointDrag = pt;
3dbeaa52 3036 event.SetEventObject(this);
91b8de8d 3037
3dbeaa52 3038 (void)GetEventHandler()->ProcessEvent(event);
29d87bba 3039
3dbeaa52
VZ
3040 m_isDragging = FALSE;
3041 m_dropTarget = (wxGenericTreeItem *)NULL;
3042
3043 ReleaseMouse();
3044
b3a7510d 3045 SetCursor(m_oldCursor);
3dbeaa52 3046
cd66f45b 3047 wxYieldIfNeeded();
3dbeaa52
VZ
3048 }
3049 else
bbe0af5b 3050 {
3dbeaa52
VZ
3051 // here we process only the messages which happen on tree items
3052
3053 m_dragCount = 0;
3054
3055 if (item == NULL) return; /* we hit the blank area */
3056
3057 if ( event.RightDown() )
3058 {
3059 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
ee4b2721 3060 nevent.m_item = item;
d027179b 3061 nevent.m_pointDrag = CalcScrolledPosition(pt);
3dbeaa52
VZ
3062 nevent.SetEventObject(this);
3063 GetEventHandler()->ProcessEvent(nevent);
3064 }
80d2803f 3065 else if ( event.LeftUp() )
f6bcfd97 3066 {
35cf1ec6
VZ
3067 // this facilitates multiple-item drag-and-drop
3068
3069 if (item && HasFlag(wxTR_MULTIPLE))
3070 {
3071 wxArrayTreeItemIds selections;
3072 size_t count = GetSelections(selections);
3073
3074 if (count > 1 &&
3075 !event.ControlDown() &&
3076 !event.ShiftDown())
3077 {
78f12104 3078 DoSelectItem(item, true, false);
35cf1ec6
VZ
3079 }
3080 }
3081
80d2803f 3082 if ( m_lastOnSame )
f6bcfd97 3083 {
80d2803f
VZ
3084 if ( (item == m_current) &&
3085 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
3086 HasFlag(wxTR_EDIT_LABELS) )
3087 {
cb59313c
VZ
3088 if ( m_renameTimer )
3089 {
3090 if ( m_renameTimer->IsRunning() )
3091 m_renameTimer->Stop();
3092 }
3093 else
3094 {
3095 m_renameTimer = new wxTreeRenameTimer( this );
3096 }
bdb310a7 3097
cb59313c 3098 m_renameTimer->Start( wxTreeRenameTimer::DELAY, TRUE );
80d2803f 3099 }
f6bcfd97 3100
80d2803f
VZ
3101 m_lastOnSame = FALSE;
3102 }
3dbeaa52 3103 }
0326c494 3104 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3dbeaa52 3105 {
f6bcfd97
BP
3106 if ( event.LeftDown() )
3107 {
3108 m_lastOnSame = item == m_current;
3109 }
3110
0326c494
VZ
3111 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
3112 {
3113 // only toggle the item for a single click, double click on
3114 // the button doesn't do anything (it toggles the item twice)
3115 if ( event.LeftDown() )
3116 {
3117 Toggle( item );
3118 }
3119
3120 // don't select the item if the button was clicked
3121 return;
3122 }
3123
3dbeaa52 3124
35cf1ec6
VZ
3125 // clear the previously selected items, if the
3126 // user clicked outside of the present selection.
3127 // otherwise, perform the deselection on mouse-up.
3128 // this allows multiple drag and drop to work.
3129
3130 if (!IsSelected(item))
3131 {
3132 // how should the selection work for this event?
3133 bool is_multiple, extended_select, unselect_others;
3134 EventFlagsToSelType(GetWindowStyleFlag(),
3135 event.ShiftDown(),
3136 event.ControlDown(),
3137 is_multiple, extended_select, unselect_others);
3138
78f12104 3139 DoSelectItem(item, unselect_others, extended_select);
35cf1ec6
VZ
3140 }
3141
3dbeaa52 3142
618a5e38
RR
3143 // For some reason, Windows isn't recognizing a left double-click,
3144 // so we need to simulate it here. Allow 200 milliseconds for now.
3dbeaa52
VZ
3145 if ( event.LeftDClick() )
3146 {
f6bcfd97 3147 // double clicking should not start editing the item label
cb59313c
VZ
3148 if ( m_renameTimer )
3149 m_renameTimer->Stop();
3150
f6bcfd97
BP
3151 m_lastOnSame = FALSE;
3152
ebb987b7
VZ
3153 // send activate event first
3154 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
ee4b2721 3155 nevent.m_item = item;
d027179b 3156 nevent.m_pointDrag = CalcScrolledPosition(pt);
ebb987b7
VZ
3157 nevent.SetEventObject( this );
3158 if ( !GetEventHandler()->ProcessEvent( nevent ) )
618a5e38 3159 {
ebb987b7
VZ
3160 // if the user code didn't process the activate event,
3161 // handle it ourselves by toggling the item when it is
3162 // double clicked
3163 if ( item->HasPlus() )
3164 {
3165 Toggle(item);
3166 }
618a5e38 3167 }
3dbeaa52
VZ
3168 }
3169 }
bbe0af5b 3170 }
edaa81ae 3171}
c801d85f 3172
5180055b 3173void wxGenericTreeCtrl::OnInternalIdle()
3db7be80 3174{
5180055b
JS
3175 wxWindow::OnInternalIdle();
3176
3e3a7b97
JS
3177 // Check if we need to select the root item
3178 // because nothing else has been selected.
3179 // Delaying it means that we can invoke event handlers
3180 // as required, when a first item is selected.
3181 if (!HasFlag(wxTR_MULTIPLE) && !GetSelection().IsOk())
3182 {
3183 if (m_select_me)
3184 SelectItem(m_select_me);
3185 else if (GetRootItem().IsOk())
3186 SelectItem(GetRootItem());
3187 }
3188
bbe0af5b
RR
3189 /* after all changes have been done to the tree control,
3190 * we actually redraw the tree when everything is over */
ef44a621 3191
618a5e38 3192 if (!m_dirty) return;
ef44a621 3193
bbe0af5b 3194 m_dirty = FALSE;
3db7be80 3195
bbe0af5b 3196 CalculatePositions();
91b8de8d 3197 Refresh();
bbe0af5b 3198 AdjustMyScrollbars();
3db7be80
RR
3199}
3200
941830cb 3201void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 3202{
e06b9569
JS
3203 wxCoord text_w = 0;
3204 wxCoord text_h = 0;
9dfbf520 3205
40e7f56c
VZ
3206 wxTreeItemAttr *attr = item->GetAttributes();
3207 if ( attr && attr->HasFont() )
3208 dc.SetFont(attr->GetFont());
3209 else if ( item->IsBold() )
eff869aa 3210 dc.SetFont(m_boldFont);
9dfbf520 3211
91b8de8d 3212 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 3213 text_h+=2;
91b8de8d 3214
eff869aa
RR
3215 // restore normal font
3216 dc.SetFont( m_normalFont );
9dfbf520 3217
91b8de8d
RR
3218 int image_h = 0;
3219 int image_w = 0;
8dc99046
VZ
3220 int image = item->GetCurrentImage();
3221 if ( image != NO_IMAGE )
91b8de8d 3222 {
2c8e4738
VZ
3223 if ( m_imageListNormal )
3224 {
3225 m_imageListNormal->GetSize( image, image_w, image_h );
3226 image_w += 4;
3227 }
91b8de8d
RR
3228 }
3229
3230 int total_h = (image_h > text_h) ? image_h : text_h;
3231
618a5e38 3232 if (total_h < 30)
f2593d0d 3233 total_h += 2; // at least 2 pixels
06b466c7 3234 else
f2593d0d 3235 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
3236
3237 item->SetHeight(total_h);
6cedba09
VZ
3238 if (total_h>m_lineHeight)
3239 m_lineHeight=total_h;
bbe0af5b 3240
91b8de8d
RR
3241 item->SetWidth(image_w+text_w+2);
3242}
3243
3244// -----------------------------------------------------------------------------
3245// for developper : y is now the top of the level
3246// not the middle of it !
941830cb 3247void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 3248{
618a5e38
RR
3249 int x = level*m_indent;
3250 if (!HasFlag(wxTR_HIDE_ROOT))
3251 {
3252 x += m_indent;
3253 }
3254 else if (level == 0)
3255 {
3256 // a hidden root is not evaluated, but its
3257 // children are always calculated
3258 goto Recurse;
3259 }
389cdc7a 3260
91b8de8d 3261 CalculateSize( item, dc );
d3a9f4af 3262
91b8de8d 3263 // set its position
618a5e38 3264 item->SetX( x+m_spacing );
91b8de8d 3265 item->SetY( y );
618a5e38 3266 y += GetLineHeight(item);
4c681997 3267
bbe0af5b
RR
3268 if ( !item->IsExpanded() )
3269 {
618a5e38 3270 // we don't need to calculate collapsed branches
bbe0af5b
RR
3271 return;
3272 }
389cdc7a 3273
618a5e38 3274 Recurse:
91b8de8d
RR
3275 wxArrayGenericTreeItems& children = item->GetChildren();
3276 size_t n, count = children.Count();
618a5e38 3277 ++level;
91b8de8d 3278 for (n = 0; n < count; ++n )
618a5e38 3279 CalculateLevel( children[n], dc, level, y ); // recurse
edaa81ae 3280}
c801d85f 3281
941830cb 3282void wxGenericTreeCtrl::CalculatePositions()
c801d85f 3283{
bbe0af5b 3284 if ( !m_anchor ) return;
29d87bba 3285
bbe0af5b
RR
3286 wxClientDC dc(this);
3287 PrepareDC( dc );
29d87bba 3288
eff869aa 3289 dc.SetFont( m_normalFont );
29d87bba 3290
bbe0af5b 3291 dc.SetPen( m_dottedPen );
91b8de8d
RR
3292 //if(GetImageList() == NULL)
3293 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 3294
8dc99046 3295 int y = 2;
f65635b5 3296 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 3297}
c801d85f 3298
941830cb 3299void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 3300{
e6527f9d
RR
3301 if (m_dirty) return;
3302
d027179b 3303 wxSize client = GetClientSize();
4832f7c0 3304
bbe0af5b 3305 wxRect rect;
5941c5de 3306 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b
VS
3307 rect.width = client.x;
3308 rect.height = client.y;
f135ff73 3309
cb9965c3 3310 Refresh(TRUE, &rect);
f135ff73 3311
bbe0af5b 3312 AdjustMyScrollbars();
edaa81ae 3313}
c801d85f 3314
941830cb 3315void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 3316{
e6527f9d
RR
3317 if (m_dirty) return;
3318
bbe0af5b 3319 wxRect rect;
5941c5de 3320 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b 3321 rect.width = GetClientSize().x;
91b8de8d 3322 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 3323
cb9965c3 3324 Refresh(TRUE, &rect);
edaa81ae 3325}
c801d85f 3326
b771aa29
VZ
3327void wxGenericTreeCtrl::RefreshSelected()
3328{
3329 // TODO: this is awfully inefficient, we should keep the list of all
3330 // selected items internally, should be much faster
3331 if ( m_anchor )
3332 RefreshSelectedUnder(m_anchor);
3333}
3334
3335void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3336{
3337 if ( item->IsSelected() )
3338 RefreshLine(item);
3339
3340 const wxArrayGenericTreeItems& children = item->GetChildren();
3341 size_t count = children.GetCount();
3342 for ( size_t n = 0; n < count; n++ )
3343 {
3344 RefreshSelectedUnder(children[n]);
3345 }
3346}
3347
7009f411
VZ
3348// ----------------------------------------------------------------------------
3349// changing colours: we need to refresh the tree control
3350// ----------------------------------------------------------------------------
3351
3352bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3353{
3354 if ( !wxWindow::SetBackgroundColour(colour) )
3355 return FALSE;
3356
3357 Refresh();
3358
3359 return TRUE;
3360}
3361
0800a4ba 3362bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
7009f411
VZ
3363{
3364 if ( !wxWindow::SetForegroundColour(colour) )
3365 return FALSE;
3366
3367 Refresh();
3368
3369 return TRUE;
3370}
3371
73bb6776
JS
3372// Process the tooltip event, to speed up event processing.
3373// Doesn't actually get a tooltip.
3374void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent &event )
3375{
3376 event.Veto();
3377}
3378
1e6feb95 3379#endif // wxUSE_TREECTRL