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