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