]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectlg.cpp
Resets scroll position on load
[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
2cc78389 1861 EnsureVisible( itemId );
cdb3cffe 1862
88ac883a
VZ
1863 // ctrl press
1864 if (unselect_others)
389cdc7a 1865 {
88ac883a 1866 if (is_single) Unselect(); // to speed up thing
c193b707 1867 else UnselectAll();
edaa81ae 1868 }
f135ff73 1869
88ac883a 1870 // shift press
d3a9f4af 1871 if (extended_select)
88ac883a 1872 {
aaa2f297
VZ
1873 if ( !m_current )
1874 {
618a5e38 1875 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
aaa2f297
VZ
1876 }
1877
88ac883a
VZ
1878 // don't change the mark (m_current)
1879 SelectItemRange(m_current, item);
1880 }
1881 else
1882 {
ca65c044 1883 bool select = true; // the default
88ac883a 1884
c193b707
VZ
1885 // Check if we need to toggle hilight (ctrl mode)
1886 if (!unselect_others)
618a5e38 1887 select=!item->IsSelected();
88ac883a 1888
91b8de8d 1889 m_current = m_key_current = item;
c193b707
VZ
1890 m_current->SetHilight(select);
1891 RefreshLine( m_current );
88ac883a 1892 }
389cdc7a 1893
f135ff73 1894 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1895 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1896}
1897
78f12104
VZ
1898void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, bool select)
1899{
1900 if ( select )
1901 {
fced5066 1902 DoSelectItem(itemId, !HasFlag(wxTR_MULTIPLE));
78f12104
VZ
1903 }
1904 else // deselect
1905 {
1906 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1907 wxCHECK_RET( item, wxT("SelectItem(): invalid tree item") );
1908
ca65c044 1909 item->SetHilight(false);
78f12104
VZ
1910 RefreshLine(item);
1911 }
1912}
1913
941830cb 1914void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
cb59313c 1915 wxArrayTreeItemIds &array) const
c801d85f 1916{
8dc99046 1917 if ( item->IsSelected() )
9dfbf520 1918 array.Add(wxTreeItemId(item));
91b8de8d 1919
9dfbf520 1920 if ( item->HasChildren() )
91b8de8d 1921 {
9dfbf520
VZ
1922 wxArrayGenericTreeItems& children = item->GetChildren();
1923 size_t count = children.GetCount();
1924 for ( size_t n = 0; n < count; ++n )
f6bcfd97 1925 FillArray(children[n], array);
91b8de8d
RR
1926 }
1927}
1928
941830cb 1929size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
91b8de8d 1930{
618a5e38
RR
1931 array.Empty();
1932 wxTreeItemId idRoot = GetRootItem();
1933 if ( idRoot.IsOk() )
1934 {
1935 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1936 }
1937 //else: the tree is empty, so no selections
91b8de8d 1938
618a5e38 1939 return array.Count();
91b8de8d
RR
1940}
1941
941830cb 1942void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1943{
05b2a432
RD
1944 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1945
91b8de8d
RR
1946 if (!item.IsOk()) return;
1947
941830cb 1948 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
ef44a621 1949
f65635b5
VZ
1950 // first expand all parent branches
1951 wxGenericTreeItem *parent = gitem->GetParent();
999723f3
VS
1952
1953 if ( HasFlag(wxTR_HIDE_ROOT) )
f65635b5 1954 {
ff38281a 1955 while ( parent && parent != m_anchor )
999723f3
VS
1956 {
1957 Expand(parent);
1958 parent = parent->GetParent();
1959 }
1960 }
1961 else
1962 {
1963 while ( parent )
1964 {
1965 Expand(parent);
1966 parent = parent->GetParent();
1967 }
f65635b5
VZ
1968 }
1969
5391f772 1970 //if (parent) CalculatePositions();
91b8de8d
RR
1971
1972 ScrollTo(item);
1973}
1974
941830cb 1975void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
91b8de8d
RR
1976{
1977 if (!item.IsOk()) return;
1978
dc6c62a9
RR
1979 // We have to call this here because the label in
1980 // question might just have been added and no screen
1981 // update taken place.
ca65c044 1982 if (m_dirty)
f89d65ea
SC
1983#if defined( __WXMSW__ ) || defined(__WXMAC__)
1984 Update();
1985#else
1986 wxYieldIfNeeded();
1987#endif
941830cb 1988 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 1989
f65635b5 1990 // now scroll to the item
0659e7ee 1991 int item_y = gitem->GetY();
8dc99046 1992
0659e7ee
RR
1993 int start_x = 0;
1994 int start_y = 0;
1e6feb95 1995 GetViewStart( &start_x, &start_y );
91b8de8d 1996 start_y *= PIXELS_PER_UNIT;
978f38c2 1997
a93109d5
RR
1998 int client_h = 0;
1999 int client_w = 0;
2000 GetClientSize( &client_w, &client_h );
ef44a621 2001
0659e7ee
RR
2002 if (item_y < start_y+3)
2003 {
91b8de8d 2004 // going down
0659e7ee
RR
2005 int x = 0;
2006 int y = 0;
91b8de8d
RR
2007 m_anchor->GetSize( x, y, this );
2008 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 2009 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 2010 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 2011 // Item should appear at top
91b8de8d 2012 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 2013 }
91b8de8d 2014 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 2015 {
c7a9fa36
RR
2016 // going up
2017 int x = 0;
2018 int y = 0;
2019 m_anchor->GetSize( x, y, this );
2020 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2021 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2022 item_y += PIXELS_PER_UNIT+2;
2023 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 2024 // Item should appear at bottom
c7a9fa36 2025 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 2026 }
edaa81ae 2027}
c801d85f 2028
e1ee62bd 2029// FIXME: tree sorting functions are not reentrant and not MT-safe!
941830cb 2030static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 2031
004fd0c8 2032static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 2033 wxGenericTreeItem **item2)
edaa81ae 2034{
941830cb 2035 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
e1ee62bd
VZ
2036
2037 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
2038}
2039
941830cb 2040void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
e1ee62bd 2041{
223d09f6 2042 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd 2043
941830cb 2044 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
978f38c2 2045
e1ee62bd 2046 wxCHECK_RET( !s_treeBeingSorted,
941830cb 2047 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 2048
91b8de8d 2049 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
2050 if ( children.Count() > 1 )
2051 {
ca65c044 2052 m_dirty = true;
618a5e38 2053
e1ee62bd
VZ
2054 s_treeBeingSorted = this;
2055 children.Sort(tree_ctrl_compare_func);
2056 s_treeBeingSorted = NULL;
e1ee62bd
VZ
2057 }
2058 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
2059}
2060
618a5e38 2061void wxGenericTreeCtrl::CalculateLineHeight()
e2414cbe 2062{
f2593d0d
RR
2063 wxClientDC dc(this);
2064 m_lineHeight = (int)(dc.GetCharHeight() + 4);
06b466c7 2065
618a5e38
RR
2066 if ( m_imageListNormal )
2067 {
2068 // Calculate a m_lineHeight value from the normal Image sizes.
2069 // May be toggle off. Then wxGenericTreeCtrl will spread when
2070 // necessary (which might look ugly).
2071 int n = m_imageListNormal->GetImageCount();
2072 for (int i = 0; i < n ; i++)
2073 {
2074 int width = 0, height = 0;
2075 m_imageListNormal->GetSize(i, width, height);
2076 if (height > m_lineHeight) m_lineHeight = height;
2077 }
2078 }
2079
2080 if (m_imageListButtons)
f2593d0d 2081 {
618a5e38
RR
2082 // Calculate a m_lineHeight value from the Button image sizes.
2083 // May be toggle off. Then wxGenericTreeCtrl will spread when
2084 // necessary (which might look ugly).
2085 int n = m_imageListButtons->GetImageCount();
2086 for (int i = 0; i < n ; i++)
2087 {
2088 int width = 0, height = 0;
2089 m_imageListButtons->GetSize(i, width, height);
2090 if (height > m_lineHeight) m_lineHeight = height;
2091 }
f2593d0d 2092 }
91b8de8d 2093
618a5e38 2094 if (m_lineHeight < 30)
f2593d0d 2095 m_lineHeight += 2; // at least 2 pixels
06b466c7 2096 else
f2593d0d 2097 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
edaa81ae 2098}
e2414cbe 2099
618a5e38
RR
2100void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
2101{
2102 if (m_ownsImageListNormal) delete m_imageListNormal;
2103 m_imageListNormal = imageList;
ca65c044
WS
2104 m_ownsImageListNormal = false;
2105 m_dirty = true;
f4bd6759
JS
2106 // Don't do any drawing if we're setting the list to NULL,
2107 // since we may be in the process of deleting the tree control.
2108 if (imageList)
2109 CalculateLineHeight();
618a5e38
RR
2110}
2111
941830cb 2112void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 2113{
46cd520d 2114 if (m_ownsImageListState) delete m_imageListState;
f135ff73 2115 m_imageListState = imageList;
ca65c044 2116 m_ownsImageListState = false;
46cd520d
VS
2117}
2118
618a5e38
RR
2119void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
2120{
2121 if (m_ownsImageListButtons) delete m_imageListButtons;
2122 m_imageListButtons = imageList;
ca65c044
WS
2123 m_ownsImageListButtons = false;
2124 m_dirty = true;
618a5e38
RR
2125 CalculateLineHeight();
2126}
2127
618a5e38
RR
2128void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
2129{
2130 SetButtonsImageList(imageList);
ca65c044 2131 m_ownsImageListButtons = true;
618a5e38
RR
2132}
2133
f135ff73
VZ
2134// -----------------------------------------------------------------------------
2135// helpers
2136// -----------------------------------------------------------------------------
0659e7ee 2137
941830cb 2138void wxGenericTreeCtrl::AdjustMyScrollbars()
c801d85f 2139{
0659e7ee
RR
2140 if (m_anchor)
2141 {
618a5e38 2142 int x = 0, y = 0;
91b8de8d 2143 m_anchor->GetSize( x, y, this );
c193b707 2144 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 2145 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
2146 int x_pos = GetScrollPos( wxHORIZONTAL );
2147 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 2148 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
2149 }
2150 else
2151 {
2152 SetScrollbars( 0, 0, 0, 0 );
2153 }
edaa81ae 2154}
c801d85f 2155
941830cb 2156int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
91b8de8d 2157{
dc6c62a9
RR
2158 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2159 return item->GetHeight();
2160 else
2161 return m_lineHeight;
91b8de8d
RR
2162}
2163
941830cb 2164void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
ef44a621 2165{
618a5e38
RR
2166 // TODO implement "state" icon on items
2167
9ec64fa7
VZ
2168 wxTreeItemAttr *attr = item->GetAttributes();
2169 if ( attr && attr->HasFont() )
2170 dc.SetFont(attr->GetFont());
2171 else if (item->IsBold())
eff869aa 2172 dc.SetFont(m_boldFont);
ef44a621 2173
618a5e38 2174 long text_w = 0, text_h = 0;
bbe0af5b 2175 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 2176
618a5e38 2177 int image_h = 0, image_w = 0;
8dc99046
VZ
2178 int image = item->GetCurrentImage();
2179 if ( image != NO_IMAGE )
bbe0af5b 2180 {
2c8e4738
VZ
2181 if ( m_imageListNormal )
2182 {
2183 m_imageListNormal->GetSize( image, image_w, image_h );
3e4f8ee2 2184 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
2c8e4738
VZ
2185 }
2186 else
2187 {
2188 image = NO_IMAGE;
2189 }
bbe0af5b 2190 }
ef44a621 2191
91b8de8d 2192 int total_h = GetLineHeight(item);
6c051af4 2193 bool drawItemBackground = false;
91b8de8d 2194
b771aa29 2195 if ( item->IsSelected() )
cdb3cffe 2196 {
44c44c82
SC
2197// under mac selections are only a rectangle in case they don't have the focus
2198#ifdef __WXMAC__
2199 if ( !m_hasFocus )
2200 {
2201 dc.SetBrush( *wxTRANSPARENT_BRUSH ) ;
2202 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) , 1 , wxSOLID ) ) ;
2203 }
2204 else
2205 {
2206 dc.SetBrush( *m_hilightBrush ) ;
2207 }
2208#else
b771aa29 2209 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
44c44c82 2210#endif
6c051af4 2211 drawItemBackground = true;
cdb3cffe 2212 }
afa6a1a1 2213 else
c0fba4d1
VS
2214 {
2215 wxColour colBg;
2216 if ( attr && attr->HasBackgroundColour() )
902725ee
WS
2217 {
2218 drawItemBackground = true;
c0fba4d1 2219 colBg = attr->GetBackgroundColour();
902725ee 2220 }
c0fba4d1 2221 else
902725ee 2222 {
8cee4a30 2223 colBg = GetBackgroundColour();
902725ee 2224 }
c0fba4d1
VS
2225 dc.SetBrush(wxBrush(colBg, wxSOLID));
2226 }
afa6a1a1 2227
cdb3cffe 2228 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
b77d9650 2229
c6f4913a 2230 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
a69cb1cc 2231 {
c6f4913a
VS
2232 int x, y, w, h;
2233
2234 DoGetPosition(&x, &y);
2235 DoGetSize(&w, &h);
2236 dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset);
a69cb1cc
JS
2237 }
2238 else
b77d9650 2239 {
c6f4913a
VS
2240 if ( item->IsSelected() && image != NO_IMAGE )
2241 {
2242 // If it's selected, and there's an image, then we should
2243 // take care to leave the area under the image painted in the
2244 // background colour.
2245 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
2246 item->GetWidth() - image_w + 2, total_h-offset );
2247 }
602f0c99
JS
2248 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2249 // don't allow backgrounds to be customized. Not drawing the background,
2250 // except for custom item backgrounds, works for both kinds of theme.
6c051af4 2251 else if (drawItemBackground)
c6f4913a
VS
2252 {
2253 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
2254 item->GetWidth()+2, total_h-offset );
2255 }
b77d9650 2256 }
ef44a621 2257
8dc99046 2258 if ( image != NO_IMAGE )
bbe0af5b 2259 {
d30b4d20 2260 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 2261 m_imageListNormal->Draw( image, dc,
49cd56ef 2262 item->GetX(),
d701d432 2263 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
2264 wxIMAGELIST_DRAW_TRANSPARENT );
2265 dc.DestroyClippingRegion();
2266 }
ef44a621 2267
afa6a1a1 2268 dc.SetBackgroundMode(wxTRANSPARENT);
f6bcfd97
BP
2269 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2270 dc.DrawText( item->GetText(),
2271 (wxCoord)(image_w + item->GetX()),
2272 (wxCoord)(item->GetY() + extraH));
ef44a621 2273
eff869aa
RR
2274 // restore normal font
2275 dc.SetFont( m_normalFont );
ef44a621
VZ
2276}
2277
91b8de8d 2278// Now y stands for the top of the item, whereas it used to stand for middle !
941830cb 2279void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2280{
618a5e38
RR
2281 int x = level*m_indent;
2282 if (!HasFlag(wxTR_HIDE_ROOT))
2283 {
2284 x += m_indent;
2285 }
2286 else if (level == 0)
2287 {
2b84e565
VS
2288 // always expand hidden root
2289 int origY = y;
618a5e38 2290 wxArrayGenericTreeItems& children = item->GetChildren();
2b84e565
VS
2291 int count = children.Count();
2292 if (count > 0)
2293 {
2294 int n = 0, oldY;
2295 do {
2296 oldY = y;
2297 PaintLevel(children[n], dc, 1, y);
2298 } while (++n < count);
2299
e76520fd 2300 if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
2b84e565
VS
2301 {
2302 // draw line down to last child
2303 origY += GetLineHeight(children[0])>>1;
2304 oldY += GetLineHeight(children[n-1])>>1;
2305 dc.DrawLine(3, origY, 3, oldY);
2306 }
2307 }
618a5e38
RR
2308 return;
2309 }
91b8de8d 2310
618a5e38
RR
2311 item->SetX(x+m_spacing);
2312 item->SetY(y);
389cdc7a 2313
618a5e38
RR
2314 int h = GetLineHeight(item);
2315 int y_top = y;
2316 int y_mid = y_top + (h>>1);
2317 y += h;
4832f7c0 2318
618a5e38
RR
2319 int exposed_x = dc.LogicalToDeviceX(0);
2320 int exposed_y = dc.LogicalToDeviceY(y_top);
233058c7 2321
618a5e38 2322 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
bbe0af5b 2323 {
c6f4913a
VS
2324 wxPen *pen =
2325#ifndef __WXMAC__
2326 // don't draw rect outline if we already have the
2327 // background color under Mac
2328 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2329#endif // !__WXMAC__
2330 wxTRANSPARENT_PEN;
2331
2332 wxColour colText;
2333 if ( item->IsSelected() )
2334 {
a756f210 2335 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
c6f4913a
VS
2336 }
2337 else
2338 {
2339 wxTreeItemAttr *attr = item->GetAttributes();
2340 if (attr && attr->HasTextColour())
2341 colText = attr->GetTextColour();
2342 else
6f0dd6b2 2343 colText = GetForegroundColour();
c6f4913a
VS
2344 }
2345
2346 // prepare to draw
2347 dc.SetTextForeground(colText);
2348 dc.SetPen(*pen);
2349
2350 // draw
2351 PaintItem(item, dc);
2352
2353 if (HasFlag(wxTR_ROW_LINES))
2354 {
2355 // if the background colour is white, choose a
2356 // contrasting color for the lines
2357 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2358 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2359 dc.DrawLine(0, y_top, 10000, y_top);
2360 dc.DrawLine(0, y, 10000, y);
2361 }
2362
2363 // restore DC objects
2364 dc.SetBrush(*wxWHITE_BRUSH);
2365 dc.SetPen(m_dottedPen);
2366 dc.SetTextForeground(*wxBLACK);
2367
9c7f49f5 2368 if ( !HasFlag(wxTR_NO_LINES) )
cdb3cffe 2369 {
9c7f49f5
VZ
2370 // draw the horizontal line here
2371 int x_start = x;
2372 if (x > (signed)m_indent)
2373 x_start -= m_indent;
2374 else if (HasFlag(wxTR_LINES_AT_ROOT))
2375 x_start = 3;
2376 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2377 }
618a5e38 2378
9c7f49f5
VZ
2379 // should the item show a button?
2380 if ( item->HasPlus() && HasButtons() )
2381 {
2382 if ( m_imageListButtons )
c22886bd 2383 {
618a5e38 2384 // draw the image button here
9c7f49f5
VZ
2385 int image_h = 0,
2386 image_w = 0;
2387 int image = item->IsExpanded() ? wxTreeItemIcon_Expanded
2388 : wxTreeItemIcon_Normal;
2389 if ( item->IsSelected() )
2b84e565 2390 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
9c7f49f5 2391
618a5e38 2392 m_imageListButtons->GetSize(image, image_w, image_h);
9c7f49f5
VZ
2393 int xx = x - image_w/2;
2394 int yy = y_mid - image_h/2;
2395
2396 wxDCClipper clip(dc, xx, yy, image_w, image_h);
618a5e38
RR
2397 m_imageListButtons->Draw(image, dc, xx, yy,
2398 wxIMAGELIST_DRAW_TRANSPARENT);
c22886bd 2399 }
9c7f49f5 2400 else // no custom buttons
c22886bd 2401 {
0e7761fa
VZ
2402 static const int wImage = 9;
2403 static const int hImage = 9;
ca65c044 2404
f8b043e7
RR
2405 int flag = 0;
2406 if (item->IsExpanded())
2407 flag |= wxCONTROL_EXPANDED;
2408 if (item == m_underMouse)
2409 flag |= wxCONTROL_CURRENT;
ca65c044 2410
9c7f49f5
VZ
2411 wxRendererNative::Get().DrawTreeItemButton
2412 (
2413 this,
2414 dc,
2415 wxRect(x - wImage/2,
2416 y_mid - hImage/2,
2417 wImage, hImage),
f8b043e7 2418 flag
9c7f49f5 2419 );
c22886bd 2420 }
bbe0af5b 2421 }
f135ff73 2422 }
d3a9f4af 2423
bbe0af5b
RR
2424 if (item->IsExpanded())
2425 {
91b8de8d 2426 wxArrayGenericTreeItems& children = item->GetChildren();
618a5e38 2427 int count = children.Count();
f65635b5 2428 if (count > 0)
9ec64fa7 2429 {
618a5e38
RR
2430 int n = 0, oldY;
2431 ++level;
2432 do {
2433 oldY = y;
2434 PaintLevel(children[n], dc, level, y);
2435 } while (++n < count);
2436
e76520fd 2437 if (!HasFlag(wxTR_NO_LINES) && count > 0)
618a5e38
RR
2438 {
2439 // draw line down to last child
2b84e565 2440 oldY += GetLineHeight(children[n-1])>>1;
618a5e38 2441 if (HasButtons()) y_mid += 5;
dd360466
RD
2442
2443 // Only draw the portion of the line that is visible, in case it is huge
ca65c044 2444 wxCoord xOrigin=0, yOrigin=0, width, height;
dd360466
RD
2445 dc.GetDeviceOrigin(&xOrigin, &yOrigin);
2446 yOrigin = abs(yOrigin);
2447 GetClientSize(&width, &height);
2448
2449 // Move end points to the begining/end of the view?
2450 if (y_mid < yOrigin)
2451 y_mid = yOrigin;
2452 if (oldY > yOrigin + height)
2453 oldY = yOrigin + height;
2454
2455 // after the adjustments if y_mid is larger than oldY then the line
2456 // isn't visible at all so don't draw anything
2457 if (y_mid < oldY)
2458 dc.DrawLine(x, y_mid, x, oldY);
618a5e38 2459 }
9ec64fa7 2460 }
bbe0af5b 2461 }
4c681997 2462}
c801d85f 2463
941830cb 2464void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
3dbeaa52
VZ
2465{
2466 if ( item )
2467 {
2468 if ( item->HasPlus() )
2469 {
2470 // it's a folder, indicate it by a border
2471 DrawBorder(item);
2472 }
2473 else
2474 {
2475 // draw a line under the drop target because the item will be
2476 // dropped there
e3d64157 2477 DrawLine(item, !m_dropEffectAboveItem );
3dbeaa52
VZ
2478 }
2479
2480 SetCursor(wxCURSOR_BULLSEYE);
2481 }
2482 else
2483 {
2484 // can't drop here
2485 SetCursor(wxCURSOR_NO_ENTRY);
2486 }
2487}
2488
941830cb 2489void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
91b8de8d 2490{
941830cb 2491 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2492
941830cb 2493 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2494
1044a386 2495 wxClientDC dc(this);
91b8de8d
RR
2496 PrepareDC( dc );
2497 dc.SetLogicalFunction(wxINVERT);
3dbeaa52 2498 dc.SetBrush(*wxTRANSPARENT_BRUSH);
91b8de8d 2499
3dbeaa52
VZ
2500 int w = i->GetWidth() + 2;
2501 int h = GetLineHeight(i) + 2;
91b8de8d 2502
3dbeaa52 2503 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
91b8de8d
RR
2504}
2505
941830cb 2506void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
91b8de8d 2507{
941830cb 2508 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
91b8de8d 2509
941830cb 2510 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
91b8de8d 2511
1044a386 2512 wxClientDC dc(this);
91b8de8d
RR
2513 PrepareDC( dc );
2514 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 2515
3dbeaa52
VZ
2516 int x = i->GetX(),
2517 y = i->GetY();
2518 if ( below )
2519 {
2520 y += GetLineHeight(i) - 1;
2521 }
91b8de8d 2522
3dbeaa52 2523 dc.DrawLine( x, y, x + i->GetWidth(), y);
91b8de8d
RR
2524}
2525
f135ff73 2526// -----------------------------------------------------------------------------
77ffb593 2527// wxWidgets callbacks
f135ff73
VZ
2528// -----------------------------------------------------------------------------
2529
941830cb 2530void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 2531{
0659e7ee
RR
2532 wxPaintDC dc(this);
2533 PrepareDC( dc );
29d87bba 2534
941830cb
JS
2535 if ( !m_anchor)
2536 return;
2537
eff869aa 2538 dc.SetFont( m_normalFont );
0659e7ee 2539 dc.SetPen( m_dottedPen );
f38374d0 2540
eff869aa 2541 // this is now done dynamically
91b8de8d
RR
2542 //if(GetImageList() == NULL)
2543 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2544
91b8de8d 2545 int y = 2;
0659e7ee 2546 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 2547}
c801d85f 2548
b771aa29 2549void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
c801d85f 2550{
ca65c044 2551 m_hasFocus = true;
978f38c2 2552
b771aa29
VZ
2553 RefreshSelected();
2554
2555 event.Skip();
edaa81ae 2556}
c801d85f 2557
b771aa29 2558void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
c801d85f 2559{
ca65c044 2560 m_hasFocus = false;
978f38c2 2561
b771aa29
VZ
2562 RefreshSelected();
2563
2564 event.Skip();
edaa81ae 2565}
c801d85f 2566
941830cb 2567void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
c801d85f 2568{
978f38c2 2569 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
b09bda68 2570 te.m_evtKey = event;
978f38c2 2571 te.SetEventObject( this );
68eb5f63
VZ
2572 if ( GetEventHandler()->ProcessEvent( te ) )
2573 {
2574 // intercepted by the user code
2575 return;
2576 }
435fe83e 2577
91b8de8d 2578 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
2579 {
2580 event.Skip();
2581 return;
2582 }
ef44a621 2583
06b466c7
VZ
2584 // how should the selection work for this event?
2585 bool is_multiple, extended_select, unselect_others;
2586 EventFlagsToSelType(GetWindowStyleFlag(),
2587 event.ShiftDown(),
105fc244 2588 event.CmdDown(),
dfc6cd93
SB
2589 is_multiple, extended_select, unselect_others);
2590
2591 // + : Expand
2592 // - : Collaspe
f6bcfd97 2593 // * : Expand all/Collapse all
dfc6cd93
SB
2594 // ' ' | return : activate
2595 // up : go up (not last children!)
2596 // down : go down
2597 // left : go to parent
2598 // right : open if parent and go next
2599 // home : go to root
2600 // end : go to last item without opening parents
cb59313c 2601 // alnum : start or continue searching for the item with this prefix
12a3f227 2602 int keyCode = event.GetKeyCode();
cb59313c 2603 switch ( keyCode )
978f38c2
VZ
2604 {
2605 case '+':
2606 case WXK_ADD:
2607 if (m_current->HasPlus() && !IsExpanded(m_current))
2608 {
2609 Expand(m_current);
2610 }
2611 break;
ef44a621 2612
f6bcfd97
BP
2613 case '*':
2614 case WXK_MULTIPLY:
2615 if ( !IsExpanded(m_current) )
2616 {
2617 // expand all
2618 ExpandAll(m_current);
2619 break;
2620 }
2621 //else: fall through to Collapse() it
2622
978f38c2
VZ
2623 case '-':
2624 case WXK_SUBTRACT:
2625 if (IsExpanded(m_current))
2626 {
2627 Collapse(m_current);
2628 }
2629 break;
ef44a621 2630
f7c6f947
RR
2631 case WXK_MENU:
2632 {
39faaf39
KH
2633 // Use the item's bounding rectangle to determine position for the event
2634 wxRect ItemRect;
2635 GetBoundingRect(m_current, ItemRect, true);
2636
4e115ed2
VZ
2637 wxTreeEvent eventMenu( wxEVT_COMMAND_TREE_ITEM_MENU, GetId() );
2638 eventMenu.m_item = m_current;
39faaf39 2639 // Use the left edge, vertical middle
4e115ed2
VZ
2640 eventMenu.m_pointDrag = wxPoint(ItemRect.GetX(),
2641 ItemRect.GetY() + ItemRect.GetHeight() / 2);
2642 eventMenu.SetEventObject( this );
2643 GetEventHandler()->ProcessEvent( eventMenu );
f7c6f947
RR
2644 break;
2645 }
978f38c2
VZ
2646 case ' ':
2647 case WXK_RETURN:
6151e144 2648 if ( !event.HasModifiers() )
978f38c2 2649 {
4e115ed2
VZ
2650 wxTreeEvent eventAct( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2651 eventAct.m_item = m_current;
2652 eventAct.SetEventObject( this );
2653 GetEventHandler()->ProcessEvent( eventAct );
978f38c2 2654 }
6151e144
VZ
2655
2656 // in any case, also generate the normal key event for this key,
2657 // even if we generated the ACTIVATED event above: this is what
2658 // wxMSW does and it makes sense because you might not want to
2659 // process ACTIVATED event at all and handle Space and Return
2660 // directly (and differently) which would be impossible otherwise
2661 event.Skip();
978f38c2 2662 break;
ef44a621 2663
618a5e38
RR
2664 // up goes to the previous sibling or to the last
2665 // of its children if it's expanded
978f38c2
VZ
2666 case WXK_UP:
2667 {
91b8de8d 2668 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
2669 if (!prev)
2670 {
99006e44 2671 prev = GetItemParent( m_key_current );
618a5e38
RR
2672 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2673 {
2674 break; // don't go to root if it is hidden
2675 }
c193b707
VZ
2676 if (prev)
2677 {
2d75caaa 2678 wxTreeItemIdValue cookie;
91b8de8d 2679 wxTreeItemId current = m_key_current;
618a5e38
RR
2680 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2681 if (current == GetFirstChild( prev, cookie ))
90e58684
RR
2682 {
2683 // otherwise we return to where we came from
78f12104 2684 DoSelectItem( prev, unselect_others, extended_select );
941830cb 2685 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
90e58684 2686 break;
c193b707 2687 }
978f38c2
VZ
2688 }
2689 }
2690 if (prev)
2691 {
69a282d4 2692 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 2693 {
69a282d4
VZ
2694 wxTreeItemId child = GetLastChild(prev);
2695 if ( child )
2696 {
2697 prev = child;
2698 }
978f38c2 2699 }
69a282d4 2700
78f12104 2701 DoSelectItem( prev, unselect_others, extended_select );
941830cb 2702 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
978f38c2
VZ
2703 }
2704 }
2705 break;
ef44a621 2706
978f38c2
VZ
2707 // left arrow goes to the parent
2708 case WXK_LEFT:
2709 {
99006e44 2710 wxTreeItemId prev = GetItemParent( m_current );
618a5e38
RR
2711 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2712 {
2713 // don't go to root if it is hidden
2714 prev = GetPrevSibling( m_current );
2715 }
978f38c2
VZ
2716 if (prev)
2717 {
78f12104 2718 DoSelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2719 }
2720 }
2721 break;
ef44a621 2722
978f38c2 2723 case WXK_RIGHT:
618a5e38
RR
2724 // this works the same as the down arrow except that we
2725 // also expand the item if it wasn't expanded yet
978f38c2
VZ
2726 Expand(m_current);
2727 // fall through
2728
2729 case WXK_DOWN:
ef44a621 2730 {
91b8de8d 2731 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2 2732 {
2d75caaa 2733 wxTreeItemIdValue cookie;
91b8de8d 2734 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
78f12104 2735 DoSelectItem( child, unselect_others, extended_select );
941830cb 2736 m_key_current=(wxGenericTreeItem*) child.m_pItem;
978f38c2
VZ
2737 }
2738 else
2739 {
91b8de8d 2740 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631 2741 if (!next)
978f38c2 2742 {
91b8de8d 2743 wxTreeItemId current = m_key_current;
a0fad723 2744 while (current.IsOk() && !next)
978f38c2 2745 {
99006e44 2746 current = GetItemParent( current );
978f38c2
VZ
2747 if (current) next = GetNextSibling( current );
2748 }
2749 }
b62c3631 2750 if (next)
978f38c2 2751 {
78f12104 2752 DoSelectItem( next, unselect_others, extended_select );
941830cb 2753 m_key_current=(wxGenericTreeItem*) next.m_pItem;
978f38c2
VZ
2754 }
2755 }
ef44a621 2756 }
978f38c2 2757 break;
ef44a621 2758
978f38c2
VZ
2759 // <End> selects the last visible tree item
2760 case WXK_END:
2761 {
2762 wxTreeItemId last = GetRootItem();
2763
2764 while ( last.IsOk() && IsExpanded(last) )
2765 {
2766 wxTreeItemId lastChild = GetLastChild(last);
2767
2768 // it may happen if the item was expanded but then all of
2769 // its children have been deleted - so IsExpanded() returned
ca65c044 2770 // true, but GetLastChild() returned invalid item
978f38c2
VZ
2771 if ( !lastChild )
2772 break;
2773
2774 last = lastChild;
2775 }
2776
2777 if ( last.IsOk() )
2778 {
78f12104 2779 DoSelectItem( last, unselect_others, extended_select );
978f38c2
VZ
2780 }
2781 }
2782 break;
2783
2784 // <Home> selects the root item
2785 case WXK_HOME:
2786 {
2787 wxTreeItemId prev = GetRootItem();
cb59313c
VZ
2788 if (!prev)
2789 break;
2790
2791 if ( HasFlag(wxTR_HIDE_ROOT) )
978f38c2 2792 {
2d75caaa
VZ
2793 wxTreeItemIdValue cookie;
2794 prev = GetFirstChild(prev, cookie);
cb59313c
VZ
2795 if (!prev)
2796 break;
978f38c2 2797 }
cb59313c 2798
78f12104 2799 DoSelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
2800 }
2801 break;
2802
2803 default:
cb59313c 2804 // do not use wxIsalnum() here
6151e144 2805 if ( !event.HasModifiers() &&
1ec3a984
RR
2806 ((keyCode >= '0' && keyCode <= '9') ||
2807 (keyCode >= 'a' && keyCode <= 'z') ||
2808 (keyCode >= 'A' && keyCode <= 'Z' )))
cb59313c
VZ
2809 {
2810 // find the next item starting with the given prefix
bef7a62d 2811 wxChar ch = (wxChar)keyCode;
6151e144 2812
bef7a62d 2813 wxTreeItemId id = FindItem(m_current, m_findPrefix + ch);
cb59313c
VZ
2814 if ( !id.IsOk() )
2815 {
2816 // no such item
2817 break;
2818 }
2819
2820 SelectItem(id);
2821
2822 m_findPrefix += ch;
2823
2824 // also start the timer to reset the current prefix if the user
2825 // doesn't press any more alnum keys soon -- we wouldn't want
2826 // to use this prefix for a new item search
2827 if ( !m_findTimer )
2828 {
2829 m_findTimer = new wxTreeFindTimer(this);
2830 }
2831
2832 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
2833 }
2834 else
2835 {
2836 event.Skip();
2837 }
978f38c2 2838 }
edaa81ae 2839}
c801d85f 2840
22574b4a 2841wxTreeItemId wxGenericTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags)
4f22cf8d 2842{
618a5e38
RR
2843 // JACS: removed wxYieldIfNeeded() because it can cause the window
2844 // to be deleted from under us if a close window event is pending
dc6c62a9 2845
91b8de8d
RR
2846 int w, h;
2847 GetSize(&w, &h);
91b8de8d 2848 flags=0;
618a5e38
RR
2849 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2850 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2851 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2852 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2853 if (flags) return wxTreeItemId();
d3a9f4af 2854
618a5e38
RR
2855 if (m_anchor == NULL)
2856 {
2857 flags = wxTREE_HITTEST_NOWHERE;
2858 return wxTreeItemId();
2859 }
5716a1ab 2860
d027179b
VS
2861 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
2862 this, flags, 0);
618a5e38
RR
2863 if (hit == NULL)
2864 {
2865 flags = wxTREE_HITTEST_NOWHERE;
2866 return wxTreeItemId();
2867 }
2868 return hit;
4f22cf8d
RR
2869}
2870
8fb3bfe2
JS
2871// get the bounding rectangle of the item (or of its label only)
2872bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
edb8f298 2873 wxRect& rect,
3e4f8ee2 2874 bool textOnly) const
8fb3bfe2 2875{
ca65c044 2876 wxCHECK_MSG( item.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
8fb3bfe2
JS
2877
2878 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2879
2880 int startX, startY;
2881 GetViewStart(& startX, & startY);
2882
3e4f8ee2
VZ
2883 if ( textOnly )
2884 {
2885 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
2886 rect.width = i->GetWidth();
2887
2888 if ( m_imageListNormal )
2889 {
2890 int image_w, image_h;
2891 m_imageListNormal->GetSize( 0, image_w, image_h );
2892 rect.width += image_w + MARGIN_BETWEEN_IMAGE_AND_TEXT;
2893 }
2894 }
2895 else // the entire line
2896 {
2897 rect.x = 0;
2898 rect.width = GetClientSize().x;
2899 }
2900
c22886bd 2901 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
c22886bd 2902 rect.height = GetLineHeight(i);
8fb3bfe2 2903
ca65c044 2904 return true;
8fb3bfe2
JS
2905}
2906
8cee4a30
VZ
2907wxTextCtrl *wxGenericTreeCtrl::EditLabel(const wxTreeItemId& item,
2908 wxClassInfo * WXUNUSED(textCtrlClass))
e179bd65 2909{
8cee4a30 2910 wxCHECK_MSG( item.IsOk(), NULL, _T("can't edit an invalid item") );
e179bd65 2911
edb8f298 2912 wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem;
9dfbf520 2913
e179bd65 2914 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
ee4b2721 2915 te.m_item = itemEdit;
e179bd65 2916 te.SetEventObject( this );
edb8f298
VZ
2917 if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() )
2918 {
2919 // vetoed by user
8cee4a30 2920 return NULL;
edb8f298 2921 }
8dc99046 2922
dc6c62a9
RR
2923 // We have to call this here because the label in
2924 // question might just have been added and no screen
2925 // update taken place.
edb8f298 2926 if ( m_dirty )
f89d65ea
SC
2927#if defined( __WXMSW__ ) || defined(__WXMAC__)
2928 Update();
2929#else
edb8f298 2930 wxYieldIfNeeded();
f89d65ea 2931#endif
9dfbf520 2932
8cee4a30 2933 // TODO: use textCtrlClass here to create the control of correct class
fbb12260 2934 m_textCtrl = new wxTreeTextCtrl(this, itemEdit);
8dc99046 2935
fbb12260 2936 m_textCtrl->SetFocus();
8cee4a30
VZ
2937
2938 return m_textCtrl;
fbb12260
JS
2939}
2940
2941// returns a pointer to the text edit control if the item is being
2942// edited, NULL otherwise (it's assumed that no more than one item may
2943// be edited simultaneously)
2944wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const
2945{
2946 return m_textCtrl;
e179bd65
RR
2947}
2948
8cee4a30
VZ
2949void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item),
2950 bool discardChanges)
2951{
2952 wxCHECK_RET( m_textCtrl, _T("not editing label") );
2953
2954 m_textCtrl->EndEdit(discardChanges);
2955}
2956
edb8f298
VZ
2957bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
2958 const wxString& value)
e179bd65 2959{
e179bd65 2960 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
ee4b2721 2961 le.m_item = item;
e179bd65 2962 le.SetEventObject( this );
edb8f298 2963 le.m_label = value;
ca65c044 2964 le.m_editCancelled = false;
9dfbf520 2965
edb8f298
VZ
2966 return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2967}
9dfbf520 2968
dd23c25c
JS
2969void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item)
2970{
2971 // let owner know that the edit was cancelled
2972 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
ee4b2721 2973 le.m_item = item;
dd23c25c
JS
2974 le.SetEventObject( this );
2975 le.m_label = wxEmptyString;
ca65c044 2976 le.m_editCancelled = true;
dd23c25c
JS
2977
2978 GetEventHandler()->ProcessEvent( le );
2979}
2980
edb8f298
VZ
2981void wxGenericTreeCtrl::OnRenameTimer()
2982{
642446e3 2983 EditLabel( m_current );
e179bd65 2984}
9dfbf520 2985
941830cb 2986void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2987{
bbe0af5b 2988 if ( !m_anchor ) return;
978f38c2 2989
f8b043e7 2990 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
ca65c044 2991
f8b043e7
RR
2992 // Is the mouse over a tree item button?
2993 int flags = 0;
73bb6776
JS
2994 wxGenericTreeItem *thisItem = m_anchor->HitTest(pt, this, flags, 0);
2995 wxGenericTreeItem *underMouse = thisItem;
0efd5732 2996#if wxUSE_TOOLTIPS
73bb6776 2997 bool underMouseChanged = (underMouse != m_underMouse) ;
0efd5732 2998#endif // wxUSE_TOOLTIPS
73bb6776 2999
f8b043e7
RR
3000 if ((underMouse) &&
3001 (flags & wxTREE_HITTEST_ONITEMBUTTON) &&
3002 (!event.LeftIsDown()) &&
ca65c044 3003 (!m_isDragging) &&
f8b043e7
RR
3004 (!m_renameTimer || !m_renameTimer->IsRunning()))
3005 {
3006 }
3007 else
3008 {
3009 underMouse = NULL;
3010 }
ca65c044 3011
f8b043e7
RR
3012 if (underMouse != m_underMouse)
3013 {
3014 if (m_underMouse)
3015 {
3016 // unhighlight old item
3017 wxGenericTreeItem *tmp = m_underMouse;
3018 m_underMouse = NULL;
3019 RefreshLine( tmp );
3020 }
ca65c044 3021
f8b043e7
RR
3022 m_underMouse = underMouse;
3023 if (m_underMouse)
3024 RefreshLine( m_underMouse );
3025 }
3026
5b5ea466 3027#if wxUSE_TOOLTIPS
4a5d781c 3028 // Determines what item we are hovering over and need a tooltip for
73bb6776 3029 wxTreeItemId hoverItem = thisItem;
4a5d781c
JS
3030
3031 // We do not want a tooltip if we are dragging, or if the rename timer is running
73bb6776 3032 if (underMouseChanged && hoverItem.IsOk() && !m_isDragging && (!m_renameTimer || !m_renameTimer->IsRunning()))
4a5d781c
JS
3033 {
3034 // Ask the tree control what tooltip (if any) should be shown
3035 wxTreeEvent hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, GetId());
bc0aebab 3036 hevent.m_item = hoverItem;
4a5d781c
JS
3037 hevent.SetEventObject(this);
3038
3039 if ( GetEventHandler()->ProcessEvent(hevent) && hevent.IsAllowed() )
3040 {
3041 SetToolTip(hevent.m_label);
3042 }
3043 }
5b5ea466 3044#endif
ca65c044 3045
3dbeaa52
VZ
3046 // we process left mouse up event (enables in-place edit), right down
3047 // (pass to the user code), left dbl click (activate item) and
3048 // dragging/moving events for items drag-and-drop
f6bcfd97
BP
3049 if ( !(event.LeftDown() ||
3050 event.LeftUp() ||
3dbeaa52
VZ
3051 event.RightDown() ||
3052 event.LeftDClick() ||
3053 event.Dragging() ||
3054 ((event.Moving() || event.RightUp()) && m_isDragging)) )
3055 {
3056 event.Skip();
3057
3058 return;
3059 }
3060
29d87bba 3061
f8b043e7 3062 flags = 0;
d027179b 3063 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
3dbeaa52 3064
3dbeaa52 3065 if ( event.Dragging() && !m_isDragging )
bbe0af5b 3066 {
fd9811b1 3067 if (m_dragCount == 0)
d027179b 3068 m_dragStart = pt;
8dc99046 3069
fd9811b1 3070 m_dragCount++;
8dc99046 3071
3dbeaa52
VZ
3072 if (m_dragCount != 3)
3073 {
3074 // wait until user drags a bit further...
3075 return;
3076 }
8dc99046 3077
3dbeaa52
VZ
3078 wxEventType command = event.RightIsDown()
3079 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3080 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
8dc99046 3081
fd9811b1 3082 wxTreeEvent nevent( command, GetId() );
ee4b2721 3083 nevent.m_item = m_current;
fd9811b1 3084 nevent.SetEventObject(this);
77b7cd4f 3085 nevent.SetPoint(pt);
3dbeaa52
VZ
3086
3087 // by default the dragging is not supported, the user code must
3088 // explicitly allow the event for it to take place
3089 nevent.Veto();
3090
3091 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
3092 {
3093 // we're going to drag this item
ca65c044 3094 m_isDragging = true;
3dbeaa52 3095
b3a7510d
VZ
3096 // remember the old cursor because we will change it while
3097 // dragging
3098 m_oldCursor = m_cursor;
a6fb8636 3099
b3a7510d
VZ
3100 // in a single selection control, hide the selection temporarily
3101 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
3102 {
941830cb 3103 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
b3a7510d
VZ
3104
3105 if ( m_oldSelection )
3106 {
ca65c044 3107 m_oldSelection->SetHilight(false);
b3a7510d
VZ
3108 RefreshLine(m_oldSelection);
3109 }
3110 }
3111
3dbeaa52
VZ
3112 CaptureMouse();
3113 }
bbe0af5b 3114 }
daa7ae0c 3115 else if ( event.Dragging() )
fd9811b1 3116 {
3dbeaa52
VZ
3117 if ( item != m_dropTarget )
3118 {
3119 // unhighlight the previous drop target
3120 DrawDropEffect(m_dropTarget);
fd9811b1 3121
3dbeaa52 3122 m_dropTarget = item;
978f38c2 3123
3dbeaa52
VZ
3124 // highlight the current drop target if any
3125 DrawDropEffect(m_dropTarget);
fb882e1c 3126
daa7ae0c 3127#if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
f89d65ea
SC
3128 Update();
3129#else
cd66f45b 3130 wxYieldIfNeeded();
f89d65ea 3131#endif
3dbeaa52 3132 }
e179bd65 3133 }
3dbeaa52
VZ
3134 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
3135 {
3136 // erase the highlighting
3137 DrawDropEffect(m_dropTarget);
9dfbf520 3138
4f5fffcc
RR
3139 if ( m_oldSelection )
3140 {
ca65c044 3141 m_oldSelection->SetHilight(true);
4f5fffcc
RR
3142 RefreshLine(m_oldSelection);
3143 m_oldSelection = (wxGenericTreeItem *)NULL;
3144 }
3145
3dbeaa52 3146 // generate the drag end event
4e115ed2 3147 wxTreeEvent eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG, GetId());
88ac883a 3148
4e115ed2
VZ
3149 eventEndDrag.m_item = item;
3150 eventEndDrag.m_pointDrag = pt;
3151 eventEndDrag.SetEventObject(this);
91b8de8d 3152
4e115ed2 3153 (void)GetEventHandler()->ProcessEvent(eventEndDrag);
29d87bba 3154
ca65c044 3155 m_isDragging = false;
3dbeaa52
VZ
3156 m_dropTarget = (wxGenericTreeItem *)NULL;
3157
3158 ReleaseMouse();
3159
b3a7510d 3160 SetCursor(m_oldCursor);
3dbeaa52 3161
f89d65ea
SC
3162#if defined( __WXMSW__ ) || defined(__WXMAC__)
3163 Update();
3164#else
cd66f45b 3165 wxYieldIfNeeded();
f89d65ea 3166#endif
3dbeaa52
VZ
3167 }
3168 else
bbe0af5b 3169 {
e4899fd7
KH
3170 // If we got to this point, we are not dragging or moving the mouse.
3171 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3172 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3173 // We skip even if we didn't hit an item because we still should
3174 // restore focus to the tree control even if we didn't exactly hit an item.
3175 if ( event.LeftDown() )
3176 {
3177 event.Skip();
3178 }
3179
3dbeaa52
VZ
3180 // here we process only the messages which happen on tree items
3181
3182 m_dragCount = 0;
3183
3184 if (item == NULL) return; /* we hit the blank area */
3185
3186 if ( event.RightDown() )
3187 {
e8cf9a5f
KH
3188 // If the item is already selected, do not update the selection.
3189 // Multi-selections should not be cleared if a selected item is clicked.
3190 if (!IsSelected(item))
3191 {
3192 DoSelectItem(item, true, false);
3193 }
3194
3dbeaa52 3195 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
ee4b2721 3196 nevent.m_item = item;
d027179b 3197 nevent.m_pointDrag = CalcScrolledPosition(pt);
3dbeaa52 3198 nevent.SetEventObject(this);
ac103441 3199 event.Skip(!GetEventHandler()->ProcessEvent(nevent));
39faaf39
KH
3200
3201 // Consistent with MSW (for now), send the ITEM_MENU *after*
3202 // the RIGHT_CLICK event. TODO: This behavior may change.
3203 wxTreeEvent nevent2(wxEVT_COMMAND_TREE_ITEM_MENU, GetId());
3204 nevent2.m_item = item;
3205 nevent2.m_pointDrag = CalcScrolledPosition(pt);
3206 nevent2.SetEventObject(this);
3207 GetEventHandler()->ProcessEvent(nevent2);
3dbeaa52 3208 }
80d2803f 3209 else if ( event.LeftUp() )
f6bcfd97 3210 {
35cf1ec6
VZ
3211 // this facilitates multiple-item drag-and-drop
3212
902725ee 3213 if ( /* item && */ HasFlag(wxTR_MULTIPLE))
35cf1ec6
VZ
3214 {
3215 wxArrayTreeItemIds selections;
3216 size_t count = GetSelections(selections);
3217
3218 if (count > 1 &&
105fc244 3219 !event.CmdDown() &&
35cf1ec6
VZ
3220 !event.ShiftDown())
3221 {
78f12104 3222 DoSelectItem(item, true, false);
35cf1ec6
VZ
3223 }
3224 }
3225
80d2803f 3226 if ( m_lastOnSame )
f6bcfd97 3227 {
80d2803f
VZ
3228 if ( (item == m_current) &&
3229 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
3230 HasFlag(wxTR_EDIT_LABELS) )
3231 {
cb59313c
VZ
3232 if ( m_renameTimer )
3233 {
3234 if ( m_renameTimer->IsRunning() )
3235 m_renameTimer->Stop();
3236 }
3237 else
3238 {
3239 m_renameTimer = new wxTreeRenameTimer( this );
3240 }
bdb310a7 3241
ca65c044 3242 m_renameTimer->Start( wxTreeRenameTimer::DELAY, true );
80d2803f 3243 }
f6bcfd97 3244
ca65c044 3245 m_lastOnSame = false;
80d2803f 3246 }
3dbeaa52 3247 }
0326c494 3248 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3dbeaa52 3249 {
f6bcfd97
BP
3250 if ( event.LeftDown() )
3251 {
3252 m_lastOnSame = item == m_current;
3253 }
3254
0326c494
VZ
3255 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
3256 {
3257 // only toggle the item for a single click, double click on
3258 // the button doesn't do anything (it toggles the item twice)
3259 if ( event.LeftDown() )
3260 {
3261 Toggle( item );
3262 }
3263
3264 // don't select the item if the button was clicked
3265 return;
3266 }
3267
3dbeaa52 3268
35cf1ec6
VZ
3269 // clear the previously selected items, if the
3270 // user clicked outside of the present selection.
3271 // otherwise, perform the deselection on mouse-up.
3272 // this allows multiple drag and drop to work.
105fc244
JS
3273 // but if Cmd is down, toggle selection of the clicked item
3274 if (!IsSelected(item) || event.CmdDown())
35cf1ec6
VZ
3275 {
3276 // how should the selection work for this event?
3277 bool is_multiple, extended_select, unselect_others;
3278 EventFlagsToSelType(GetWindowStyleFlag(),
3279 event.ShiftDown(),
105fc244 3280 event.CmdDown(),
35cf1ec6
VZ
3281 is_multiple, extended_select, unselect_others);
3282
78f12104 3283 DoSelectItem(item, unselect_others, extended_select);
35cf1ec6
VZ
3284 }
3285
3dbeaa52 3286
618a5e38
RR
3287 // For some reason, Windows isn't recognizing a left double-click,
3288 // so we need to simulate it here. Allow 200 milliseconds for now.
3dbeaa52
VZ
3289 if ( event.LeftDClick() )
3290 {
f6bcfd97 3291 // double clicking should not start editing the item label
cb59313c
VZ
3292 if ( m_renameTimer )
3293 m_renameTimer->Stop();
3294
ca65c044 3295 m_lastOnSame = false;
f6bcfd97 3296
ebb987b7
VZ
3297 // send activate event first
3298 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
ee4b2721 3299 nevent.m_item = item;
d027179b 3300 nevent.m_pointDrag = CalcScrolledPosition(pt);
ebb987b7
VZ
3301 nevent.SetEventObject( this );
3302 if ( !GetEventHandler()->ProcessEvent( nevent ) )
618a5e38 3303 {
ebb987b7
VZ
3304 // if the user code didn't process the activate event,
3305 // handle it ourselves by toggling the item when it is
3306 // double clicked
3307 if ( item->HasPlus() )
3308 {
3309 Toggle(item);
3310 }
618a5e38 3311 }
3dbeaa52
VZ
3312 }
3313 }
bbe0af5b 3314 }
edaa81ae 3315}
c801d85f 3316
5180055b 3317void wxGenericTreeCtrl::OnInternalIdle()
3db7be80 3318{
5180055b 3319 wxWindow::OnInternalIdle();
ca65c044 3320
3e3a7b97
JS
3321 // Check if we need to select the root item
3322 // because nothing else has been selected.
3323 // Delaying it means that we can invoke event handlers
3324 // as required, when a first item is selected.
3325 if (!HasFlag(wxTR_MULTIPLE) && !GetSelection().IsOk())
3326 {
3327 if (m_select_me)
3328 SelectItem(m_select_me);
3329 else if (GetRootItem().IsOk())
3330 SelectItem(GetRootItem());
3331 }
3332
bbe0af5b
RR
3333 /* after all changes have been done to the tree control,
3334 * we actually redraw the tree when everything is over */
ef44a621 3335
618a5e38 3336 if (!m_dirty) return;
e1983ab5 3337 if (m_freezeCount) return;
ca65c044
WS
3338
3339 m_dirty = false;
3db7be80 3340
bbe0af5b 3341 CalculatePositions();
91b8de8d 3342 Refresh();
bbe0af5b 3343 AdjustMyScrollbars();
3db7be80
RR
3344}
3345
941830cb 3346void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 3347{
e06b9569
JS
3348 wxCoord text_w = 0;
3349 wxCoord text_h = 0;
9dfbf520 3350
40e7f56c
VZ
3351 wxTreeItemAttr *attr = item->GetAttributes();
3352 if ( attr && attr->HasFont() )
3353 dc.SetFont(attr->GetFont());
3354 else if ( item->IsBold() )
eff869aa 3355 dc.SetFont(m_boldFont);
c8fce1a4
VS
3356 else
3357 dc.SetFont(m_normalFont);
9dfbf520 3358
91b8de8d 3359 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 3360 text_h+=2;
91b8de8d 3361
eff869aa
RR
3362 // restore normal font
3363 dc.SetFont( m_normalFont );
9dfbf520 3364
91b8de8d
RR
3365 int image_h = 0;
3366 int image_w = 0;
8dc99046
VZ
3367 int image = item->GetCurrentImage();
3368 if ( image != NO_IMAGE )
91b8de8d 3369 {
2c8e4738
VZ
3370 if ( m_imageListNormal )
3371 {
3372 m_imageListNormal->GetSize( image, image_w, image_h );
3e4f8ee2 3373 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
2c8e4738 3374 }
91b8de8d
RR
3375 }
3376
3377 int total_h = (image_h > text_h) ? image_h : text_h;
3378
618a5e38 3379 if (total_h < 30)
f2593d0d 3380 total_h += 2; // at least 2 pixels
06b466c7 3381 else
f2593d0d 3382 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
3383
3384 item->SetHeight(total_h);
6cedba09
VZ
3385 if (total_h>m_lineHeight)
3386 m_lineHeight=total_h;
bbe0af5b 3387
91b8de8d
RR
3388 item->SetWidth(image_w+text_w+2);
3389}
3390
3391// -----------------------------------------------------------------------------
3392// for developper : y is now the top of the level
3393// not the middle of it !
941830cb 3394void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 3395{
618a5e38
RR
3396 int x = level*m_indent;
3397 if (!HasFlag(wxTR_HIDE_ROOT))
3398 {
3399 x += m_indent;
3400 }
3401 else if (level == 0)
3402 {
3403 // a hidden root is not evaluated, but its
3404 // children are always calculated
3405 goto Recurse;
3406 }
389cdc7a 3407
91b8de8d 3408 CalculateSize( item, dc );
d3a9f4af 3409
91b8de8d 3410 // set its position
618a5e38 3411 item->SetX( x+m_spacing );
91b8de8d 3412 item->SetY( y );
618a5e38 3413 y += GetLineHeight(item);
4c681997 3414
bbe0af5b
RR
3415 if ( !item->IsExpanded() )
3416 {
618a5e38 3417 // we don't need to calculate collapsed branches
bbe0af5b
RR
3418 return;
3419 }
389cdc7a 3420
618a5e38 3421 Recurse:
91b8de8d
RR
3422 wxArrayGenericTreeItems& children = item->GetChildren();
3423 size_t n, count = children.Count();
618a5e38 3424 ++level;
91b8de8d 3425 for (n = 0; n < count; ++n )
618a5e38 3426 CalculateLevel( children[n], dc, level, y ); // recurse
edaa81ae 3427}
c801d85f 3428
941830cb 3429void wxGenericTreeCtrl::CalculatePositions()
c801d85f 3430{
bbe0af5b 3431 if ( !m_anchor ) return;
29d87bba 3432
bbe0af5b
RR
3433 wxClientDC dc(this);
3434 PrepareDC( dc );
29d87bba 3435
eff869aa 3436 dc.SetFont( m_normalFont );
29d87bba 3437
bbe0af5b 3438 dc.SetPen( m_dottedPen );
91b8de8d
RR
3439 //if(GetImageList() == NULL)
3440 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 3441
8dc99046 3442 int y = 2;
f65635b5 3443 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 3444}
c801d85f 3445
941830cb 3446void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 3447{
e6527f9d 3448 if (m_dirty) return;
e1983ab5 3449 if (m_freezeCount) return;
e6527f9d 3450
d027179b 3451 wxSize client = GetClientSize();
4832f7c0 3452
bbe0af5b 3453 wxRect rect;
5941c5de 3454 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b
VS
3455 rect.width = client.x;
3456 rect.height = client.y;
f135ff73 3457
ca65c044 3458 Refresh(true, &rect);
f135ff73 3459
bbe0af5b 3460 AdjustMyScrollbars();
edaa81ae 3461}
c801d85f 3462
941830cb 3463void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
c801d85f 3464{
e6527f9d 3465 if (m_dirty) return;
e1983ab5 3466 if (m_freezeCount) return;
e6527f9d 3467
bbe0af5b 3468 wxRect rect;
5941c5de 3469 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
d027179b 3470 rect.width = GetClientSize().x;
91b8de8d 3471 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 3472
ca65c044 3473 Refresh(true, &rect);
edaa81ae 3474}
c801d85f 3475
b771aa29
VZ
3476void wxGenericTreeCtrl::RefreshSelected()
3477{
e1983ab5 3478 if (m_freezeCount) return;
ca65c044 3479
b771aa29
VZ
3480 // TODO: this is awfully inefficient, we should keep the list of all
3481 // selected items internally, should be much faster
3482 if ( m_anchor )
3483 RefreshSelectedUnder(m_anchor);
3484}
3485
3486void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3487{
e1983ab5 3488 if (m_freezeCount) return;
ca65c044 3489
b771aa29
VZ
3490 if ( item->IsSelected() )
3491 RefreshLine(item);
3492
3493 const wxArrayGenericTreeItems& children = item->GetChildren();
3494 size_t count = children.GetCount();
3495 for ( size_t n = 0; n < count; n++ )
3496 {
3497 RefreshSelectedUnder(children[n]);
3498 }
3499}
3500
e1983ab5
VZ
3501void wxGenericTreeCtrl::Freeze()
3502{
3503 m_freezeCount++;
3504}
3505
3506void wxGenericTreeCtrl::Thaw()
3507{
3508 wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen tree control?") );
ca65c044 3509
e1983ab5
VZ
3510 if ( !--m_freezeCount )
3511 {
3512 Refresh();
3513 }
3514}
3515
7009f411
VZ
3516// ----------------------------------------------------------------------------
3517// changing colours: we need to refresh the tree control
3518// ----------------------------------------------------------------------------
3519
3520bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3521{
3522 if ( !wxWindow::SetBackgroundColour(colour) )
ca65c044
WS
3523 return false;
3524
3525 if (m_freezeCount) return true;
7009f411
VZ
3526
3527 Refresh();
3528
ca65c044 3529 return true;
7009f411
VZ
3530}
3531
0800a4ba 3532bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
7009f411
VZ
3533{
3534 if ( !wxWindow::SetForegroundColour(colour) )
ca65c044
WS
3535 return false;
3536
3537 if (m_freezeCount) return true;
7009f411
VZ
3538
3539 Refresh();
3540
ca65c044 3541 return true;
7009f411
VZ
3542}
3543
73bb6776
JS
3544// Process the tooltip event, to speed up event processing.
3545// Doesn't actually get a tooltip.
3546void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent &event )
3547{
3548 event.Veto();
3549}
3550
35d4c967
RD
3551
3552// NOTE: If using the wxListBox visual attributes works everywhere then this can
3553// be removed, as well as the #else case below.
3554#define _USE_VISATTR 0
3555
944b1f5e 3556#if _USE_VISATTR
35d4c967 3557#include "wx/listbox.h"
944b1f5e 3558#endif
35d4c967
RD
3559
3560//static
3561wxVisualAttributes
3f927d50 3562#if _USE_VISATTR
35d4c967 3563wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
3f927d50
DS
3564#else
3565wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
3566#endif
35d4c967
RD
3567{
3568#if _USE_VISATTR
3569 // Use the same color scheme as wxListBox
3570 return wxListBox::GetClassDefaultAttributes(variant);
3571#else
3572 wxVisualAttributes attr;
3573 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
3574 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
3575 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3576 return attr;
3577#endif
3578}
3579
a6fb8636
WS
3580#if WXWIN_COMPATIBILITY_2_4
3581
3582int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
3583{
3584 return GetItemImage(item, wxTreeItemIcon_Selected);
3585}
3586
3587void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
3588{
3589 SetItemImage(item, image, wxTreeItemIcon_Selected);
3590}
3591
3592#endif // WXWIN_COMPATIBILITY_2_4
3593
1e6feb95 3594#endif // wxUSE_TREECTRL