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