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