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