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