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