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