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