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