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