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