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