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