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