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