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