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