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