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