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