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