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