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