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