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