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