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