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