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