]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
added wxUSE_FS_INET/ZIP
[wxWidgets.git] / src / generic / treectrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.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$
c801d85f 8// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
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__
f135ff73 21 #pragma implementation "treectrl.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__
28#pragma hdrstop
29#endif
30
31#include "wx/generic/treectrl.h"
f60d0f94 32#include "wx/generic/imaglist.h"
c801d85f 33#include "wx/settings.h"
389cdc7a 34#include "wx/log.h"
f135ff73
VZ
35#include "wx/intl.h"
36#include "wx/dynarray.h"
91b8de8d 37#include "wx/arrimpl.cpp"
f135ff73 38#include "wx/dcclient.h"
0659e7ee 39#include "wx/msgdlg.h"
c801d85f 40
f135ff73
VZ
41// -----------------------------------------------------------------------------
42// array types
43// -----------------------------------------------------------------------------
c801d85f 44
1e6d9499
JS
45class WXDLLEXPORT wxGenericTreeItem;
46
91b8de8d
RR
47WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
48WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
c801d85f 49
f135ff73
VZ
50// -----------------------------------------------------------------------------
51// private classes
52// -----------------------------------------------------------------------------
53
54// a tree item
55class WXDLLEXPORT wxGenericTreeItem
c801d85f 56{
f135ff73
VZ
57public:
58 // ctors & dtor
59 wxGenericTreeItem() { m_data = NULL; }
60 wxGenericTreeItem( wxGenericTreeItem *parent,
61 const wxString& text,
62 wxDC& dc,
63 int image, int selImage,
64 wxTreeItemData *data );
65
ff5bf259 66 ~wxGenericTreeItem();
f135ff73
VZ
67
68 // trivial accessors
91b8de8d 69 wxArrayGenericTreeItems& GetChildren() { return m_children; }
f135ff73
VZ
70
71 const wxString& GetText() const { return m_text; }
72 int GetImage() const { return m_image; }
73 int GetSelectedImage() const { return m_selImage; }
74 wxTreeItemData *GetData() const { return m_data; }
75
91b8de8d 76 void SetText( const wxString &text );
f135ff73
VZ
77 void SetImage(int image) { m_image = image; }
78 void SetSelectedImage(int image) { m_selImage = image; }
79 void SetData(wxTreeItemData *data) { m_data = data; }
80
81 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
82
ef44a621
VZ
83 void SetBold(bool bold) { m_isBold = bold; }
84
f135ff73
VZ
85 int GetX() const { return m_x; }
86 int GetY() const { return m_y; }
87
f135ff73
VZ
88 void SetX(int x) { m_x = x; }
89 void SetY(int y) { m_y = y; }
90
91b8de8d
RR
91 int GetHeight() const { return m_height; }
92 int GetWidth() const { return m_width; }
93
94 void SetHeight(int h) { m_height = h; }
95 void SetWidth(int w) { m_width = w; }
96
97
f135ff73 98 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 99
f135ff73 100 // operations
a43a4f9d
VZ
101 // deletes all children notifying the treectrl about it if !NULL pointer
102 // given
103 void DeleteChildren(wxTreeCtrl *tree = NULL);
104 // FIXME don't know what is it for
f135ff73
VZ
105 void Reset();
106
4832f7c0
VZ
107 // get count of all children (and grand children if 'recursively')
108 size_t GetChildrenCount(bool recursively = TRUE) const;
f135ff73
VZ
109
110 void Insert(wxGenericTreeItem *child, size_t index)
111 { m_children.Insert(child, index); }
112
113 void SetCross( int x, int y );
91b8de8d 114 void GetSize( int &x, int &y, const wxTreeCtrl* );
f135ff73
VZ
115
116 // return the item at given position (or NULL if no item), onButton is TRUE
117 // if the point belongs to the item's button, otherwise it lies on the
118 // button's label
91b8de8d 119 wxGenericTreeItem *HitTest( const wxPoint& point, const wxTreeCtrl *, int &flags);
f135ff73
VZ
120
121 void Expand() { m_isCollapsed = FALSE; }
122 void Collapse() { m_isCollapsed = TRUE; }
123
124 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
125
126 // status inquiries
127 bool HasChildren() const { return !m_children.IsEmpty(); }
128 bool HasHilight() const { return m_hasHilight; }
129 bool IsExpanded() const { return !m_isCollapsed; }
130 bool HasPlus() const { return m_hasPlus || HasChildren(); }
ef44a621 131 bool IsBold() const { return m_isBold; }
f135ff73
VZ
132
133private:
134 wxString m_text;
135
136 int m_image,
137 m_selImage;
138
139 wxTreeItemData *m_data;
4832f7c0 140
ef44a621
VZ
141 // use bitfields to save size
142 int m_isCollapsed :1;
143 int m_hasHilight :1; // same as focused
144 int m_hasPlus :1; // used for item which doesn't have
145 // children but still has a [+] button
146 int m_isBold :1; // render the label in bold font
f135ff73
VZ
147
148 int m_x, m_y;
91b8de8d 149 long m_height, m_width;
f135ff73
VZ
150 int m_xCross, m_yCross;
151 int m_level;
91b8de8d 152 wxArrayGenericTreeItems m_children;
f135ff73
VZ
153 wxGenericTreeItem *m_parent;
154};
155
156// =============================================================================
157// implementation
158// =============================================================================
159
91b8de8d 160#define PIXELS_PER_UNIT 10
f135ff73 161// -----------------------------------------------------------------------------
c801d85f 162// wxTreeEvent
f135ff73 163// -----------------------------------------------------------------------------
c801d85f 164
92976ab6 165IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
c801d85f 166
f135ff73 167wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
92976ab6 168 : wxNotifyEvent( commandType, id )
c801d85f
KB
169{
170 m_code = 0;
f135ff73 171 m_itemOld = (wxGenericTreeItem *)NULL;
edaa81ae 172}
c801d85f 173
f135ff73 174// -----------------------------------------------------------------------------
c801d85f 175// wxGenericTreeItem
f135ff73 176// -----------------------------------------------------------------------------
c801d85f 177
f135ff73
VZ
178wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
179 const wxString& text,
180 wxDC& dc,
181 int image, int selImage,
182 wxTreeItemData *data)
183 : m_text(text)
c801d85f 184{
f135ff73
VZ
185 m_image = image;
186 m_selImage = selImage;
187 m_data = data;
188 m_x = m_y = 0;
189 m_xCross = m_yCross = 0;
190
191 m_level = 0;
192
193 m_isCollapsed = TRUE;
c801d85f 194 m_hasHilight = FALSE;
df875e59 195 m_hasPlus = FALSE;
ef44a621 196 m_isBold = FALSE;
c801d85f 197
c801d85f 198 m_parent = parent;
f135ff73 199
91b8de8d
RR
200 dc.GetTextExtent( m_text, &m_width, &m_height );
201 // TODO : Add the width of the image
202 // PB : We don't know which image is shown (image, selImage)
203 // We don't even know imageList from the treectrl this item belongs to !!!
204 // At this point m_width doesn't mean much, this can be remove !
edaa81ae 205}
c801d85f 206
f135ff73 207wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 208{
f135ff73 209 delete m_data;
4832f7c0 210
a43a4f9d 211 wxASSERT_MSG( m_children.IsEmpty(),
87138c52 212 _T("please call DeleteChildren() before deleting the item") );
372edb9d
VZ
213}
214
a43a4f9d 215void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree)
372edb9d 216{
f135ff73
VZ
217 size_t count = m_children.Count();
218 for ( size_t n = 0; n < count; n++ )
a43a4f9d
VZ
219 {
220 wxGenericTreeItem *child = m_children[n];
221 if ( tree )
222 {
223 tree->SendDeleteEvent(child);
224 }
225
226 child->DeleteChildren(tree);
227 delete child;
228 }
372edb9d
VZ
229
230 m_children.Empty();
edaa81ae 231}
c801d85f 232
91b8de8d 233void wxGenericTreeItem::SetText( const wxString &text )
c801d85f
KB
234{
235 m_text = text;
edaa81ae 236}
c801d85f 237
74bedbeb 238void wxGenericTreeItem::Reset()
c801d85f 239{
f135ff73
VZ
240 m_text.Empty();
241 m_image =
242 m_selImage = -1;
243 m_data = NULL;
244 m_x = m_y =
91b8de8d 245 m_height = m_width = 0;
f135ff73 246 m_xCross =
c801d85f 247 m_yCross = 0;
74bedbeb 248
f135ff73 249 m_level = 0;
c801d85f 250
372edb9d 251 DeleteChildren();
f135ff73 252 m_isCollapsed = TRUE;
c801d85f 253
f135ff73 254 m_parent = (wxGenericTreeItem *)NULL;
edaa81ae 255}
c801d85f 256
4832f7c0 257size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 258{
f135ff73 259 size_t count = m_children.Count();
4832f7c0
VZ
260 if ( !recursively )
261 return count;
262
f135ff73 263 size_t total = count;
91b8de8d 264 for ( size_t n = 0; n < count; ++n )
c801d85f 265 {
4832f7c0 266 total += m_children[n]->GetChildrenCount();
edaa81ae 267 }
c801d85f 268
f135ff73 269 return total;
edaa81ae 270}
c801d85f
KB
271
272void wxGenericTreeItem::SetCross( int x, int y )
273{
274 m_xCross = x;
275 m_yCross = y;
edaa81ae 276}
c801d85f 277
91b8de8d 278void wxGenericTreeItem::GetSize( int &x, int &y, const wxTreeCtrl *theTree )
c801d85f 279{
91b8de8d
RR
280 int bottomY=m_y+theTree->GetLineHeight(this);
281 if ( y < bottomY ) y = bottomY;
282 int width = m_x + m_width;
283 if ( x < width ) x = width;
f135ff73 284
df875e59 285 if (IsExpanded())
c801d85f 286 {
df875e59 287 size_t count = m_children.Count();
91b8de8d 288 for ( size_t n = 0; n < count; ++n )
4832f7c0 289 {
91b8de8d 290 m_children[n]->GetSize( x, y, theTree );
df875e59 291 }
edaa81ae
RR
292 }
293}
c801d85f 294
f135ff73 295wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
91b8de8d
RR
296 const wxTreeCtrl *theTree,
297 int &flags)
c801d85f 298{
91b8de8d 299 if ((point.y > m_y) && (point.y < m_y + theTree->GetLineHeight(this)))
c801d85f 300 {
91b8de8d
RR
301 if (point.y<m_y+theTree->GetLineHeight(this)/2) flags|=wxTREE_HITTEST_ONITEMUPPERPART;
302 else flags|=wxTREE_HITTEST_ONITEMLOWERPART;
303
f135ff73 304 // FIXME why +5?
5679f335 305 // Because that is the size of the plus sign, RR
f135ff73
VZ
306 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
307 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
f9f950fc 308 (IsExpanded() || HasPlus()))
c801d85f 309 {
91b8de8d 310 flags|=wxTREE_HITTEST_ONITEMBUTTON;
f135ff73 311 return this;
edaa81ae 312 }
978f38c2 313
91b8de8d 314 if ((point.x >= m_x) && (point.x <= m_x+m_width))
c801d85f 315 {
d3a9f4af
RD
316 int image_w = -1;
317 int image_h;
318
91b8de8d 319 // assuming every image (normal and selected ) has the same size !
d3a9f4af
RD
320 if (theTree->m_imageListNormal)
321 theTree->m_imageListNormal->GetSize(m_image, image_w, image_h);
322 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
91b8de8d 323 flags|=wxTREE_HITTEST_ONITEMICON;
d3a9f4af 324 else
91b8de8d
RR
325 flags|=wxTREE_HITTEST_ONITEMLABEL;
326
f135ff73 327 return this;
edaa81ae 328 }
91b8de8d
RR
329
330 if (point.x < m_x) flags|=wxTREE_HITTEST_ONITEMIDENT;
331 if (point.x > m_x+m_width) flags|=wxTREE_HITTEST_ONITEMRIGHT;
332
333 return this;
c801d85f
KB
334 }
335 else
336 {
e2414cbe 337 if (!m_isCollapsed)
c801d85f 338 {
f135ff73
VZ
339 size_t count = m_children.Count();
340 for ( size_t n = 0; n < count; n++ )
e2414cbe 341 {
91b8de8d 342 wxGenericTreeItem *res = m_children[n]->HitTest( point, theTree, flags );
f135ff73
VZ
343 if ( res != NULL )
344 return res;
edaa81ae
RR
345 }
346 }
347 }
f135ff73 348
91b8de8d 349 flags|=wxTREE_HITTEST_NOWHERE;
f135ff73 350 return NULL;
edaa81ae 351}
c801d85f 352
f135ff73
VZ
353// -----------------------------------------------------------------------------
354// wxTreeCtrl implementation
355// -----------------------------------------------------------------------------
356
357IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow)
358
359BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
360 EVT_PAINT (wxTreeCtrl::OnPaint)
361 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
362 EVT_CHAR (wxTreeCtrl::OnChar)
363 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
364 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
3db7be80 365 EVT_IDLE (wxTreeCtrl::OnIdle)
f135ff73
VZ
366END_EVENT_TABLE()
367
368// -----------------------------------------------------------------------------
369// construction/destruction
370// -----------------------------------------------------------------------------
91b8de8d 371
f135ff73 372void wxTreeCtrl::Init()
c801d85f 373{
d3a9f4af 374 m_current =
91b8de8d 375 m_key_current =
f135ff73
VZ
376 m_anchor = (wxGenericTreeItem *) NULL;
377 m_hasFocus = FALSE;
3db7be80 378 m_dirty = FALSE;
f135ff73
VZ
379
380 m_xScroll = 0;
381 m_yScroll = 0;
382 m_lineHeight = 10;
383 m_indent = 15;
cf724bce 384 m_spacing = 18;
f135ff73
VZ
385
386 m_hilightBrush = new wxBrush
387 (
388 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
389 wxSOLID
390 );
391
392 m_imageListNormal =
393 m_imageListState = (wxImageList *) NULL;
978f38c2 394
bbe0af5b 395 m_dragCount = 0;
edaa81ae 396}
c801d85f 397
f135ff73
VZ
398bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
399 const wxPoint& pos, const wxSize& size,
978f38c2
VZ
400 long style,
401 const wxValidator &validator,
402 const wxString& name )
c801d85f 403{
f135ff73
VZ
404 Init();
405
a367b9b3 406 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
978f38c2 407
ce4169a4 408#if wxUSE_VALIDATORS
4f22cf8d 409 SetValidator( validator );
ce4169a4 410#endif
f135ff73
VZ
411
412 SetBackgroundColour( *wxWHITE );
413 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
414
415 return TRUE;
edaa81ae 416}
c801d85f 417
f135ff73 418wxTreeCtrl::~wxTreeCtrl()
c801d85f 419{
f135ff73 420 wxDELETE( m_hilightBrush );
a43a4f9d
VZ
421
422 DeleteAllItems();
edaa81ae 423}
c801d85f 424
f135ff73
VZ
425// -----------------------------------------------------------------------------
426// accessors
427// -----------------------------------------------------------------------------
428
429size_t wxTreeCtrl::GetCount() const
c801d85f 430{
4832f7c0 431 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
edaa81ae 432}
c801d85f 433
f135ff73 434void wxTreeCtrl::SetIndent(unsigned int indent)
c801d85f 435{
f135ff73 436 m_indent = indent;
cf724bce
RR
437 m_dirty = TRUE;
438 Refresh();
439}
440
441void wxTreeCtrl::SetSpacing(unsigned int spacing)
442{
443 m_spacing = spacing;
444 m_dirty = TRUE;
f135ff73
VZ
445 Refresh();
446}
74bedbeb 447
4832f7c0
VZ
448size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
449{
87138c52 450 wxCHECK_MSG( item.IsOk(), 0u, _T("invalid tree item") );
4832f7c0
VZ
451
452 return item.m_pItem->GetChildrenCount(recursively);
453}
454
f135ff73
VZ
455// -----------------------------------------------------------------------------
456// functions to work with tree items
457// -----------------------------------------------------------------------------
74bedbeb 458
f135ff73
VZ
459wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
460{
87138c52 461 wxCHECK_MSG( item.IsOk(), _T(""), _T("invalid tree item") );
4832f7c0 462
f135ff73 463 return item.m_pItem->GetText();
edaa81ae 464}
74bedbeb 465
f135ff73 466int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
74bedbeb 467{
87138c52 468 wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") );
4832f7c0 469
f135ff73 470 return item.m_pItem->GetImage();
edaa81ae 471}
c801d85f 472
f135ff73 473int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
c801d85f 474{
87138c52 475 wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") );
4832f7c0 476
f135ff73 477 return item.m_pItem->GetSelectedImage();
edaa81ae 478}
c801d85f 479
f135ff73 480wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 481{
87138c52 482 wxCHECK_MSG( item.IsOk(), NULL, _T("invalid tree item") );
4832f7c0 483
f135ff73 484 return item.m_pItem->GetData();
edaa81ae 485}
c801d85f 486
f135ff73
VZ
487void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
488{
87138c52 489 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 490
f135ff73 491 wxClientDC dc(this);
f992adf9 492 wxGenericTreeItem *pItem = item.m_pItem;
91b8de8d
RR
493 pItem->SetText(text);
494 CalculateSize(pItem, dc);
f992adf9 495 RefreshLine(pItem);
f135ff73 496}
c801d85f 497
f135ff73
VZ
498void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
499{
87138c52 500 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 501
91b8de8d 502 wxClientDC dc(this);
f992adf9
VZ
503 wxGenericTreeItem *pItem = item.m_pItem;
504 pItem->SetImage(image);
91b8de8d 505 CalculateSize(pItem, dc);
f992adf9 506 RefreshLine(pItem);
f135ff73 507}
c801d85f 508
f135ff73 509void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
c801d85f 510{
87138c52 511 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 512
91b8de8d 513 wxClientDC dc(this);
f992adf9
VZ
514 wxGenericTreeItem *pItem = item.m_pItem;
515 pItem->SetSelectedImage(image);
91b8de8d 516 CalculateSize(pItem, dc);
f992adf9 517 RefreshLine(pItem);
edaa81ae 518}
c801d85f 519
f135ff73 520void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 521{
87138c52 522 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 523
de646ed1 524 item.m_pItem->SetData(data);
edaa81ae 525}
c801d85f 526
f135ff73 527void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 528{
87138c52 529 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 530
f992adf9
VZ
531 wxGenericTreeItem *pItem = item.m_pItem;
532 pItem->SetHasPlus(has);
533 RefreshLine(pItem);
edaa81ae 534}
c801d85f 535
ef44a621
VZ
536void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
537{
87138c52 538 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
ef44a621
VZ
539
540 // avoid redrawing the tree if no real change
541 wxGenericTreeItem *pItem = item.m_pItem;
542 if ( pItem->IsBold() != bold )
543 {
544 pItem->SetBold(bold);
545 RefreshLine(pItem);
546 }
547}
548
f135ff73
VZ
549// -----------------------------------------------------------------------------
550// item status inquiries
551// -----------------------------------------------------------------------------
552
df875e59 553bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
c801d85f 554{
87138c52 555 wxFAIL_MSG(_T("not implemented"));
f135ff73 556
c801d85f 557 return TRUE;
edaa81ae 558}
c801d85f 559
f135ff73 560bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 561{
87138c52 562 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
4832f7c0 563
f135ff73 564 return !item.m_pItem->GetChildren().IsEmpty();
edaa81ae 565}
c801d85f 566
f135ff73 567bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 568{
87138c52 569 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
4832f7c0 570
f135ff73
VZ
571 return item.m_pItem->IsExpanded();
572}
29d87bba 573
f135ff73
VZ
574bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
575{
87138c52 576 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
4832f7c0 577
f135ff73
VZ
578 return item.m_pItem->HasHilight();
579}
29d87bba 580
ef44a621
VZ
581bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
582{
87138c52 583 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
ef44a621
VZ
584
585 return item.m_pItem->IsBold();
586}
587
f135ff73
VZ
588// -----------------------------------------------------------------------------
589// navigation
590// -----------------------------------------------------------------------------
29d87bba 591
f135ff73
VZ
592wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
593{
87138c52 594 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
389cdc7a 595
f135ff73
VZ
596 return item.m_pItem->GetParent();
597}
29d87bba 598
f135ff73
VZ
599wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
600{
87138c52 601 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
29d87bba 602
f135ff73
VZ
603 cookie = 0;
604 return GetNextChild(item, cookie);
605}
29d87bba 606
f135ff73
VZ
607wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
608{
87138c52 609 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
29d87bba 610
91b8de8d 611 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
4832f7c0
VZ
612 if ( (size_t)cookie < children.Count() )
613 {
978f38c2 614 return children.Item(cookie++);
4832f7c0
VZ
615 }
616 else
617 {
618 // there are no more of them
1e6d9499 619 return wxTreeItemId();
4832f7c0 620 }
f135ff73 621}
29d87bba 622
978f38c2
VZ
623wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
624{
87138c52 625 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
978f38c2 626
91b8de8d 627 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
0a240683 628 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
629}
630
f135ff73
VZ
631wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
632{
87138c52 633 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
f135ff73
VZ
634
635 wxGenericTreeItem *i = item.m_pItem;
636 wxGenericTreeItem *parent = i->GetParent();
637 if ( parent == NULL )
638 {
639 // root item doesn't have any siblings
1e6d9499 640 return wxTreeItemId();
edaa81ae 641 }
4832f7c0 642
91b8de8d 643 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 644 int index = siblings.Index(i);
3c67202d 645 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 646
f135ff73 647 size_t n = (size_t)(index + 1);
fdd8d7b5 648 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 649}
c801d85f 650
f135ff73 651wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 652{
87138c52 653 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
f135ff73
VZ
654
655 wxGenericTreeItem *i = item.m_pItem;
656 wxGenericTreeItem *parent = i->GetParent();
657 if ( parent == NULL )
c801d85f 658 {
f135ff73 659 // root item doesn't have any siblings
1e6d9499 660 return wxTreeItemId();
edaa81ae 661 }
4832f7c0 662
91b8de8d 663 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 664 int index = siblings.Index(i);
3c67202d 665 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 666
fdd8d7b5
VZ
667 return index == 0 ? wxTreeItemId()
668 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 669}
389cdc7a 670
f135ff73
VZ
671wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
672{
87138c52 673 wxFAIL_MSG(_T("not implemented"));
29d87bba 674
1e6d9499 675 return wxTreeItemId();
f135ff73 676}
29d87bba 677
f135ff73
VZ
678wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
679{
87138c52 680 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
29d87bba 681
87138c52 682 wxFAIL_MSG(_T("not implemented"));
29d87bba 683
1e6d9499 684 return wxTreeItemId();
f135ff73 685}
29d87bba 686
f135ff73
VZ
687wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
688{
87138c52 689 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
29d87bba 690
87138c52 691 wxFAIL_MSG(_T("not implemented"));
29d87bba 692
1e6d9499 693 return wxTreeItemId();
edaa81ae 694}
c801d85f 695
f135ff73
VZ
696// -----------------------------------------------------------------------------
697// operations
698// -----------------------------------------------------------------------------
699
700wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
701 size_t previous,
702 const wxString& text,
703 int image, int selImage,
704 wxTreeItemData *data)
c801d85f 705{
f135ff73
VZ
706 wxGenericTreeItem *parent = parentId.m_pItem;
707 if ( !parent )
708 {
709 // should we give a warning here?
710 return AddRoot(text, image, selImage, data);
711 }
4832f7c0 712
f135ff73
VZ
713 wxClientDC dc(this);
714 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
715 text, dc,
716 image, selImage,
717 data);
74bedbeb 718
f135ff73 719 if ( data != NULL )
c801d85f 720 {
f135ff73
VZ
721 data->m_pItem = item;
722 }
74bedbeb 723
f135ff73 724 parent->Insert( item, previous );
ef44a621 725
3db7be80 726 m_dirty = TRUE;
389cdc7a 727
f135ff73 728 return item;
4c681997
RR
729}
730
f135ff73
VZ
731wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
732 int image, int selImage,
733 wxTreeItemData *data)
4c681997 734{
87138c52 735 wxCHECK_MSG( !m_anchor, wxTreeItemId(), _T("tree can have only one root") );
389cdc7a 736
f135ff73
VZ
737 wxClientDC dc(this);
738 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
739 image, selImage, data);
740 if ( data != NULL )
741 {
742 data->m_pItem = m_anchor;
743 }
389cdc7a 744
a32dd690 745 Refresh();
91b8de8d 746 AdjustMyScrollbars();
a32dd690 747
f135ff73 748 return m_anchor;
edaa81ae 749}
c801d85f 750
f135ff73
VZ
751wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
752 const wxString& text,
753 int image, int selImage,
754 wxTreeItemData *data)
c801d85f 755{
f135ff73 756 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 757}
c801d85f 758
f135ff73
VZ
759wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
760 const wxTreeItemId& idPrevious,
761 const wxString& text,
762 int image, int selImage,
763 wxTreeItemData *data)
c801d85f 764{
f135ff73
VZ
765 wxGenericTreeItem *parent = parentId.m_pItem;
766 if ( !parent )
767 {
768 // should we give a warning here?
769 return AddRoot(text, image, selImage, data);
770 }
c801d85f 771
f135ff73 772 int index = parent->GetChildren().Index(idPrevious.m_pItem);
3c67202d 773 wxASSERT_MSG( index != wxNOT_FOUND,
87138c52 774 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
91b8de8d 775 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
edaa81ae 776}
c801d85f 777
f135ff73
VZ
778wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
779 const wxString& text,
780 int image, int selImage,
781 wxTreeItemData *data)
74bedbeb 782{
f135ff73
VZ
783 wxGenericTreeItem *parent = parentId.m_pItem;
784 if ( !parent )
785 {
786 // should we give a warning here?
787 return AddRoot(text, image, selImage, data);
788 }
789
790 return DoInsertItem(parent, parent->GetChildren().Count(), text,
791 image, selImage, data);
74bedbeb
VZ
792}
793
a43a4f9d
VZ
794void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
795{
796 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
797 event.m_item = item;
798 event.SetEventObject( this );
799 ProcessEvent( event );
800}
801
372edb9d
VZ
802void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
803{
804 wxGenericTreeItem *item = itemId.m_pItem;
a43a4f9d 805 item->DeleteChildren(this);
372edb9d
VZ
806
807 m_dirty = TRUE;
808}
809
f135ff73 810void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 811{
f135ff73 812 wxGenericTreeItem *item = itemId.m_pItem;
ff5bf259
VZ
813 wxGenericTreeItem *parent = item->GetParent();
814
815 if ( parent )
816 {
817 parent->GetChildren().Remove(item);
818 }
f135ff73 819
a43a4f9d
VZ
820 item->DeleteChildren(this);
821 SendDeleteEvent(item);
f135ff73
VZ
822 delete item;
823
4bb19cfb 824 m_dirty = TRUE;
edaa81ae 825}
c801d85f 826
f135ff73 827void wxTreeCtrl::DeleteAllItems()
c801d85f 828{
f135ff73
VZ
829 if ( m_anchor )
830 {
a43a4f9d 831 m_anchor->DeleteChildren(this);
f135ff73 832 delete m_anchor;
a43a4f9d 833
f135ff73
VZ
834 m_anchor = NULL;
835
4bb19cfb 836 m_dirty = TRUE;
f135ff73 837 }
edaa81ae
RR
838}
839
f135ff73 840void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 841{
f135ff73
VZ
842 wxGenericTreeItem *item = itemId.m_pItem;
843
978f38c2 844 if ( !item->HasPlus() )
4bb19cfb 845 return;
978f38c2 846
f135ff73
VZ
847 if ( item->IsExpanded() )
848 return;
849
850 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
851 event.m_item = item;
852 event.SetEventObject( this );
853 if ( ProcessEvent( event ) && event.m_code )
854 {
855 // cancelled by program
856 return;
857 }
4832f7c0 858
f135ff73 859 item->Expand();
28ab302b 860 CalculatePositions();
f135ff73
VZ
861
862 RefreshSubtree(item);
863
864 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
865 ProcessEvent( event );
edaa81ae
RR
866}
867
f135ff73 868void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 869{
f135ff73
VZ
870 wxGenericTreeItem *item = itemId.m_pItem;
871
872 if ( !item->IsExpanded() )
873 return;
874
875 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
876 event.m_item = item;
877 event.SetEventObject( this );
878 if ( ProcessEvent( event ) && event.m_code )
edaa81ae 879 {
f135ff73
VZ
880 // cancelled by program
881 return;
882 }
4832f7c0 883
f135ff73
VZ
884 item->Collapse();
885
91b8de8d 886 wxArrayGenericTreeItems& children = item->GetChildren();
f135ff73
VZ
887 size_t count = children.Count();
888 for ( size_t n = 0; n < count; n++ )
889 {
890 Collapse(children[n]);
edaa81ae 891 }
f135ff73
VZ
892
893 CalculatePositions();
894
895 RefreshSubtree(item);
896
897 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
898 ProcessEvent( event );
edaa81ae 899}
c801d85f 900
f135ff73 901void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 902{
f135ff73 903 Collapse(item);
372edb9d 904 DeleteChildren(item);
edaa81ae 905}
c801d85f 906
f135ff73 907void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 908{
f135ff73 909 wxGenericTreeItem *item = itemId.m_pItem;
389cdc7a 910
f135ff73
VZ
911 if ( item->IsExpanded() )
912 Collapse(itemId);
913 else
914 Expand(itemId);
915}
389cdc7a 916
f135ff73
VZ
917void wxTreeCtrl::Unselect()
918{
919 if ( m_current )
920 {
921 m_current->SetHilight( FALSE );
922 RefreshLine( m_current );
923 }
edaa81ae 924}
c801d85f 925
88ac883a 926void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 927{
88ac883a
VZ
928 item->SetHilight(FALSE);
929 RefreshLine(item);
d3a9f4af 930
88ac883a
VZ
931 if (item->HasChildren())
932 {
91b8de8d 933 wxArrayGenericTreeItems& children = item->GetChildren();
88ac883a
VZ
934 size_t count = children.Count();
935 for ( size_t n = 0; n < count; ++n )
936 UnselectAllChildren(children[n]);
937 }
938}
f135ff73 939
88ac883a
VZ
940void wxTreeCtrl::UnselectAll()
941{
942 UnselectAllChildren(GetRootItem().m_pItem);
943}
944
945// Recursive function !
946// To stop we must have crt_item<last_item
91b8de8d 947// Algorithm :
88ac883a 948// Tag all next children, when no more children,
d3a9f4af 949// Move to parent (not to tag)
91b8de8d 950// Keep going... if we found last_item, we stop.
88ac883a
VZ
951bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
952{
953 wxGenericTreeItem *parent = crt_item->GetParent();
954
955 if ( parent == NULL ) // This is root item
956 return TagAllChildrenUntilLast(crt_item, last_item, select);
957
91b8de8d 958 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
959 int index = children.Index(crt_item);
960 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
961
962 size_t count = children.Count();
963 for (size_t n=(size_t)(index+1); n<count; ++n)
c0de7af4 964 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
88ac883a
VZ
965
966 return TagNextChildren(parent, last_item, select);
967}
968
969bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
970{
971 crt_item->SetHilight(select);
972 RefreshLine(crt_item);
d3a9f4af 973
c0de7af4 974 if (crt_item==last_item) return TRUE;
88ac883a
VZ
975
976 if (crt_item->HasChildren())
977 {
91b8de8d 978 wxArrayGenericTreeItems& children = crt_item->GetChildren();
88ac883a
VZ
979 size_t count = children.Count();
980 for ( size_t n = 0; n < count; ++n )
c0de7af4 981 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
88ac883a 982 }
d3a9f4af 983
c0de7af4 984 return FALSE;
88ac883a
VZ
985}
986
987void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
988{
989 // item2 is not necessary after item1
990 wxGenericTreeItem *first=NULL, *last=NULL;
991
992 // choice first' and 'last' between item1 and item2
d3a9f4af 993 if (item1->GetY()<item2->GetY())
88ac883a
VZ
994 {
995 first=item1;
996 last=item2;
997 }
d3a9f4af 998 else
88ac883a
VZ
999 {
1000 first=item2;
1001 last=item1;
1002 }
1003
1004 bool select=m_current->HasHilight();
d3a9f4af 1005
88ac883a
VZ
1006 if (TagAllChildrenUntilLast(first,last,select)) return;
1007
d3a9f4af 1008 TagNextChildren(first,last,select);
88ac883a
VZ
1009}
1010
d3a9f4af
RD
1011void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId,
1012 bool unselect_others,
88ac883a 1013 bool extended_select)
d3a9f4af 1014{
88ac883a
VZ
1015 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1016
1017 //wxCHECK_RET( ( (!unselect_others) && is_single),
1018 // _T("this is a single selection tree") );
1019
1020 // to keep going anyhow !!!
d3a9f4af 1021 if (is_single)
88ac883a 1022 {
c0de7af4
PA
1023 unselect_others=TRUE;
1024 extended_select=FALSE;
88ac883a
VZ
1025 }
1026
1027 wxGenericTreeItem *item = itemId.m_pItem;
d3a9f4af 1028
f135ff73
VZ
1029 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1030 event.m_item = item;
1031 event.m_itemOld = m_current;
1032 event.SetEventObject( this );
91b8de8d 1033 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1034
6daa0637 1035 if ( GetEventHandler()->ProcessEvent( event ) && event.WasVetoed() )
f135ff73
VZ
1036 return;
1037
88ac883a
VZ
1038 // ctrl press
1039 if (unselect_others)
389cdc7a 1040 {
88ac883a
VZ
1041 if (is_single) Unselect(); // to speed up thing
1042 else UnselectAll();
edaa81ae 1043 }
f135ff73 1044
88ac883a 1045 // shift press
d3a9f4af 1046 if (extended_select)
88ac883a 1047 {
91b8de8d 1048 if (m_current == NULL) m_current=m_key_current=GetRootItem().m_pItem;
88ac883a
VZ
1049 // don't change the mark (m_current)
1050 SelectItemRange(m_current, item);
1051 }
1052 else
1053 {
c0de7af4 1054 bool select=TRUE; // the default
88ac883a
VZ
1055
1056 // Check if we need to toggle hilight (ctrl mode)
1057 if (!unselect_others)
1058 select=!item->HasHilight();
1059
91b8de8d 1060 m_current = m_key_current = item;
88ac883a
VZ
1061 m_current->SetHilight(select);
1062 RefreshLine( m_current );
1063 }
389cdc7a 1064
f135ff73 1065 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1066 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1067}
1068
91b8de8d 1069void wxTreeCtrl::FillArray(wxGenericTreeItem *item, wxArrayTreeItemIds &array) const
c801d85f 1070{
91b8de8d
RR
1071 if (item->HasHilight()) array.Add(wxTreeItemId(item));
1072
1073 if (item->HasChildren())
1074 {
1075 wxArrayGenericTreeItems& children = item->GetChildren();
1076 size_t count = children.Count();
1077 for ( size_t n = 0; n < count; ++n )
1078 FillArray(children[n],array);
1079 }
1080}
1081
1082size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1083{
1084 array.Empty();
1085 FillArray(GetRootItem().m_pItem, array);
1086
1087 return array.Count();
1088}
1089
1090void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1091{
91b8de8d
RR
1092 if (!item.IsOk()) return;
1093
0659e7ee 1094 wxGenericTreeItem *gitem = item.m_pItem;
ef44a621 1095
f65635b5
VZ
1096 // first expand all parent branches
1097 wxGenericTreeItem *parent = gitem->GetParent();
1098 while ( parent && !parent->IsExpanded() )
1099 {
1100 Expand(parent);
1101
1102 parent = parent->GetParent();
1103 }
1104
91b8de8d
RR
1105 if (parent) CalculatePositions();
1106
1107 ScrollTo(item);
1108}
1109
1110void wxTreeCtrl::ScrollTo(const wxTreeItemId &item)
1111{
1112 if (!item.IsOk()) return;
1113
1114 wxGenericTreeItem *gitem = item.m_pItem;
1115
f65635b5 1116 // now scroll to the item
0659e7ee 1117 int item_y = gitem->GetY();
ef44a621 1118
0659e7ee
RR
1119 int start_x = 0;
1120 int start_y = 0;
1121 ViewStart( &start_x, &start_y );
91b8de8d 1122 start_y *= PIXELS_PER_UNIT;
978f38c2 1123
a93109d5
RR
1124 int client_h = 0;
1125 int client_w = 0;
1126 GetClientSize( &client_w, &client_h );
ef44a621 1127
0659e7ee
RR
1128 if (item_y < start_y+3)
1129 {
91b8de8d 1130 // going down
0659e7ee
RR
1131 int x = 0;
1132 int y = 0;
91b8de8d
RR
1133 m_anchor->GetSize( x, y, this );
1134 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1135 int x_pos = GetScrollPos( wxHORIZONTAL );
91b8de8d
RR
1136 // Item should appear at top
1137 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1138 }
91b8de8d 1139 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1140 {
91b8de8d 1141 // going up
0659e7ee
RR
1142 int x = 0;
1143 int y = 0;
91b8de8d
RR
1144 m_anchor->GetSize( x, y, this );
1145 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1146 item_y += PIXELS_PER_UNIT+2;
0659e7ee 1147 int x_pos = GetScrollPos( wxHORIZONTAL );
91b8de8d
RR
1148 // Item should appear at bottom
1149 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 1150 }
edaa81ae 1151}
c801d85f 1152
df875e59
RR
1153wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
1154 wxClassInfo* WXUNUSED(textCtrlClass) )
c801d85f 1155{
87138c52 1156 wxFAIL_MSG(_T("not implemented"));
c801d85f 1157
bbe0af5b 1158 return (wxTextCtrl*)NULL;
edaa81ae 1159}
c801d85f 1160
f135ff73 1161wxTextCtrl *wxTreeCtrl::GetEditControl() const
c801d85f 1162{
87138c52 1163 wxFAIL_MSG(_T("not implemented"));
c801d85f 1164
0659e7ee 1165 return (wxTextCtrl*)NULL;
edaa81ae 1166}
c801d85f 1167
df875e59 1168void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool WXUNUSED(discardChanges))
74bedbeb 1169{
87138c52 1170 wxFAIL_MSG(_T("not implemented"));
74bedbeb
VZ
1171}
1172
e1ee62bd
VZ
1173// FIXME: tree sorting functions are not reentrant and not MT-safe!
1174static wxTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1175
e1ee62bd
VZ
1176static int tree_ctrl_compare_func(wxGenericTreeItem **item1,
1177 wxGenericTreeItem **item2)
edaa81ae 1178{
87138c52 1179 wxCHECK_MSG( s_treeBeingSorted, 0, _T("bug in wxTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1180
1181 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1182}
1183
e1ee62bd
VZ
1184int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1185 const wxTreeItemId& item2)
0659e7ee 1186{
87138c52 1187 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1188}
1189
1190void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1191{
87138c52 1192 wxCHECK_RET( itemId.IsOk(), _T("invalid tree item") );
e1ee62bd
VZ
1193
1194 wxGenericTreeItem *item = itemId.m_pItem;
978f38c2 1195
e1ee62bd 1196 wxCHECK_RET( !s_treeBeingSorted,
87138c52 1197 _T("wxTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1198
91b8de8d 1199 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1200 if ( children.Count() > 1 )
1201 {
1202 s_treeBeingSorted = this;
1203 children.Sort(tree_ctrl_compare_func);
1204 s_treeBeingSorted = NULL;
978f38c2 1205
e1ee62bd
VZ
1206 m_dirty = TRUE;
1207 }
1208 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1209}
1210
f135ff73 1211wxImageList *wxTreeCtrl::GetImageList() const
edaa81ae 1212{
f135ff73 1213 return m_imageListNormal;
edaa81ae
RR
1214}
1215
f135ff73 1216wxImageList *wxTreeCtrl::GetStateImageList() const
c801d85f 1217{
f135ff73 1218 return m_imageListState;
edaa81ae 1219}
c801d85f 1220
f135ff73 1221void wxTreeCtrl::SetImageList(wxImageList *imageList)
e2414cbe 1222{
d30b4d20 1223 m_imageListNormal = imageList;
91b8de8d
RR
1224
1225 // Calculate a m_lineHeight value from the image sizes.
1226 // May be toggle off. Then wxTreeCtrl will spread when
1227 // necessary (which might look ugly).
1228#if 1
d30b4d20 1229 wxPaintDC dc(this);
d30b4d20
KB
1230 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1231 int
1232 width = 0,
1233 height = 0,
1234 n = m_imageListNormal->GetImageCount();
1235 for(int i = 0; i < n ; i++)
1236 {
1237 m_imageListNormal->GetSize(i, width, height);
1238 if(height > m_lineHeight) m_lineHeight = height;
d3a9f4af 1239 }
91b8de8d
RR
1240
1241 if (m_lineHeight<40) m_lineHeight+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1242 else m_lineHeight+=m_lineHeight/10; // otherwise 10% extra spacing
1243
1244#endif
edaa81ae 1245}
e2414cbe 1246
f135ff73 1247void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1248{
f135ff73 1249 m_imageListState = imageList;
edaa81ae 1250}
e2414cbe 1251
f135ff73
VZ
1252// -----------------------------------------------------------------------------
1253// helpers
1254// -----------------------------------------------------------------------------
0659e7ee 1255
74bedbeb 1256void wxTreeCtrl::AdjustMyScrollbars()
c801d85f 1257{
0659e7ee
RR
1258 if (m_anchor)
1259 {
1260 int x = 0;
1261 int y = 0;
91b8de8d
RR
1262 m_anchor->GetSize( x, y, this );
1263 //y += GetLineHeight(m_anchor);
1264 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1265 int x_pos = GetScrollPos( wxHORIZONTAL );
1266 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1267 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1268 }
1269 else
1270 {
1271 SetScrollbars( 0, 0, 0, 0 );
1272 }
edaa81ae 1273}
c801d85f 1274
91b8de8d
RR
1275int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1276{
1277 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HIGHT)
1278 return item->GetHeight();
1279 else
d3a9f4af 1280 return m_lineHeight;
91b8de8d
RR
1281}
1282
ef44a621
VZ
1283void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1284{
f65635b5 1285 // render bold items in bold
bbe0af5b
RR
1286 wxFont fontOld;
1287 wxFont fontNew;
978f38c2 1288
bbe0af5b
RR
1289 if (item->IsBold())
1290 {
1291 fontOld = dc.GetFont();
1292 if (fontOld.Ok())
1293 {
f65635b5 1294 // VZ: is there any better way to make a bold variant of old font?
bbe0af5b
RR
1295 fontNew = wxFont( fontOld.GetPointSize(),
1296 fontOld.GetFamily(),
1297 fontOld.GetStyle(),
1298 wxBOLD,
1299 fontOld.GetUnderlined());
1300 dc.SetFont(fontNew);
1301 }
1302 else
1303 {
87138c52 1304 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
bbe0af5b
RR
1305 }
1306 }
ef44a621 1307
bbe0af5b
RR
1308 long text_w = 0;
1309 long text_h = 0;
1310 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1311
bbe0af5b
RR
1312 int image_h = 0;
1313 int image_w = 0;
1314 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1315 {
1316 m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
1317 image_w += 4;
978f38c2 1318 }
bbe0af5b
RR
1319 else if (item->GetImage() != -1)
1320 {
1321 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
1322 image_w += 4;
1323 }
ef44a621 1324
91b8de8d
RR
1325 int total_h = GetLineHeight(item);
1326
1327 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
ef44a621 1328
bbe0af5b
RR
1329 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1330 {
d30b4d20 1331 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
bbe0af5b 1332 m_imageListNormal->Draw( item->GetSelectedImage(), dc,
49cd56ef 1333 item->GetX(),
d701d432 1334 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1335 wxIMAGELIST_DRAW_TRANSPARENT );
1336 dc.DestroyClippingRegion();
1337 }
1338 else if (item->GetImage() != -1)
1339 {
d30b4d20 1340 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
bbe0af5b 1341 m_imageListNormal->Draw( item->GetImage(), dc,
49cd56ef 1342 item->GetX(),
d701d432 1343 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1344 wxIMAGELIST_DRAW_TRANSPARENT );
1345 dc.DestroyClippingRegion();
1346 }
ef44a621 1347
bbe0af5b 1348 dc.SetBackgroundMode(wxTRANSPARENT);
d30b4d20 1349 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
49cd56ef 1350 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
ef44a621 1351
f65635b5 1352 // restore normal font for bold items
bbe0af5b
RR
1353 if (fontOld.Ok())
1354 {
1355 dc.SetFont( fontOld);
1356 }
ef44a621
VZ
1357}
1358
91b8de8d 1359// Now y stands for the top of the item, whereas it used to stand for middle !
16c1f7f3 1360void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1361{
bbe0af5b 1362 int horizX = level*m_indent;
389cdc7a 1363
cf724bce 1364 item->SetX( horizX+m_indent+m_spacing );
91b8de8d 1365 item->SetY( y );
4c681997 1366
bbe0af5b 1367 int oldY = y;
91b8de8d
RR
1368 y+=GetLineHeight(item)/2;
1369
1370 item->SetCross( horizX+m_indent, y );
389cdc7a 1371
bbe0af5b
RR
1372 int exposed_x = dc.LogicalToDeviceX( 0 );
1373 int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 );
4832f7c0 1374
91b8de8d 1375 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item)+4 )) // 10000 = very much
bbe0af5b
RR
1376 {
1377 int startX = horizX;
cf724bce 1378 int endX = horizX + (m_indent-5);
29d87bba 1379
cf724bce 1380// if (!item->HasChildren()) endX += (m_indent+5);
bbe0af5b 1381 if (!item->HasChildren()) endX += 20;
4832f7c0 1382
bbe0af5b 1383 dc.DrawLine( startX, y, endX, y );
29d87bba 1384
bbe0af5b
RR
1385 if (item->HasPlus())
1386 {
cf724bce 1387 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
bbe0af5b
RR
1388 dc.SetPen( *wxGREY_PEN );
1389 dc.SetBrush( *wxWHITE_BRUSH );
cf724bce 1390 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
bbe0af5b 1391 dc.SetPen( *wxBLACK_PEN );
cf724bce 1392 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
bbe0af5b
RR
1393
1394 if (!item->IsExpanded())
d30b4d20 1395 {
cf724bce 1396 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
d30b4d20 1397 }
bbe0af5b 1398 }
c801d85f 1399
bbe0af5b
RR
1400 if (item->HasHilight())
1401 {
1402 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
a367b9b3 1403
bbe0af5b 1404 dc.SetBrush( *m_hilightBrush );
4832f7c0 1405
bbe0af5b
RR
1406 if (m_hasFocus)
1407 dc.SetPen( *wxBLACK_PEN );
1408 else
1409 dc.SetPen( *wxTRANSPARENT_PEN );
4832f7c0 1410
bbe0af5b 1411 PaintItem(item, dc);
f135ff73 1412
bbe0af5b
RR
1413 dc.SetPen( *wxBLACK_PEN );
1414 dc.SetTextForeground( *wxBLACK );
1415 dc.SetBrush( *wxWHITE_BRUSH );
1416 }
1417 else
1418 {
1419 dc.SetBrush( *wxWHITE_BRUSH );
1420 dc.SetPen( *wxTRANSPARENT_PEN );
4832f7c0 1421
bbe0af5b 1422 PaintItem(item, dc);
4832f7c0 1423
bbe0af5b
RR
1424 dc.SetPen( *wxBLACK_PEN );
1425 }
f135ff73 1426 }
d3a9f4af 1427
91b8de8d 1428 y = oldY+GetLineHeight(item);
e2414cbe 1429
bbe0af5b
RR
1430 if (item->IsExpanded())
1431 {
91b8de8d
RR
1432 oldY+=GetLineHeight(item)/2;
1433 int semiOldY=y; // (=y) for stupid compilator
389cdc7a 1434
91b8de8d
RR
1435 wxArrayGenericTreeItems& children = item->GetChildren();
1436 size_t n, count = children.Count();
1437 for ( n = 0; n < count; ++n )
1438 {
1439 semiOldY=y;
1440 PaintLevel( children[n], dc, level+1, y );
1441 }
389cdc7a 1442
f65635b5
VZ
1443 // it may happen that the item is expanded but has no items (when you
1444 // delete all its children for example) - don't draw the vertical line
1445 // in this case
1446 if (count > 0)
91b8de8d
RR
1447 {
1448 semiOldY+=GetLineHeight(children[--n])/2;
cf724bce 1449 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
91b8de8d 1450 }
bbe0af5b 1451 }
4c681997 1452}
c801d85f 1453
91b8de8d
RR
1454void wxTreeCtrl::DrawBorder(wxTreeItemId &item)
1455{
1456 if (!item) return;
1457
1458 wxGenericTreeItem *i=item.m_pItem;
1459
1460 wxPaintDC dc(this);
1461 PrepareDC( dc );
1462 dc.SetLogicalFunction(wxINVERT);
1463
1464 int w,h,x;
1465 ViewStart(&x,&h); // we only need x
1466 GetClientSize(&w,&h); // we only need w
1467
1468 h=GetLineHeight(i)+1;
1469 // 2 white column at border
1470 dc.DrawRectangle( PIXELS_PER_UNIT*x+2, i->GetY()-1, w-6, h);
1471}
1472
1473void wxTreeCtrl::DrawLine(wxTreeItemId &item, bool below)
1474{
1475 if (!item) return;
1476
1477 wxGenericTreeItem *i=item.m_pItem;
1478
1479 wxPaintDC dc(this);
1480 PrepareDC( dc );
1481 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 1482
91b8de8d
RR
1483 int w,h,y;
1484 GetSize(&w,&h);
1485
1486 if (below) y=i->GetY()+GetLineHeight(i)-1;
1487 else y=i->GetY();
1488
1489 dc.DrawLine( 0, y, w, y);
1490}
1491
f135ff73
VZ
1492// -----------------------------------------------------------------------------
1493// wxWindows callbacks
1494// -----------------------------------------------------------------------------
1495
3db7be80 1496void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 1497{
91b8de8d 1498 if ( !m_anchor)
0659e7ee 1499 return;
c801d85f 1500
0659e7ee
RR
1501 wxPaintDC dc(this);
1502 PrepareDC( dc );
29d87bba 1503
f60d0f94 1504 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
29d87bba 1505
0659e7ee 1506 dc.SetPen( m_dottedPen );
91b8de8d
RR
1507 //if(GetImageList() == NULL)
1508 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1509
91b8de8d 1510 int y = 2;
0659e7ee 1511 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 1512}
c801d85f 1513
3db7be80 1514void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1515{
0659e7ee 1516 m_hasFocus = TRUE;
978f38c2 1517
bbe0af5b 1518 if (m_current) RefreshLine( m_current );
edaa81ae 1519}
c801d85f 1520
3db7be80 1521void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1522{
0659e7ee 1523 m_hasFocus = FALSE;
978f38c2 1524
bbe0af5b 1525 if (m_current) RefreshLine( m_current );
edaa81ae 1526}
c801d85f
KB
1527
1528void wxTreeCtrl::OnChar( wxKeyEvent &event )
1529{
978f38c2
VZ
1530 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1531 te.m_code = event.KeyCode();
1532 te.SetEventObject( this );
1533 GetEventHandler()->ProcessEvent( te );
435fe83e 1534
91b8de8d 1535 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
1536 {
1537 event.Skip();
1538 return;
1539 }
ef44a621 1540
88ac883a
VZ
1541 bool is_multiple=(GetWindowStyleFlag() & wxTR_MULTIPLE);
1542 bool extended_select=(event.ShiftDown() && is_multiple);
1543 bool unselect_others=!(extended_select || (event.ControlDown() && is_multiple));
1544
978f38c2
VZ
1545 switch (event.KeyCode())
1546 {
1547 case '+':
1548 case WXK_ADD:
1549 if (m_current->HasPlus() && !IsExpanded(m_current))
1550 {
1551 Expand(m_current);
1552 }
1553 break;
ef44a621 1554
978f38c2
VZ
1555 case '-':
1556 case WXK_SUBTRACT:
1557 if (IsExpanded(m_current))
1558 {
1559 Collapse(m_current);
1560 }
1561 break;
ef44a621 1562
978f38c2
VZ
1563 case '*':
1564 case WXK_MULTIPLY:
1565 Toggle(m_current);
1566 break;
ef44a621 1567
978f38c2
VZ
1568 case ' ':
1569 case WXK_RETURN:
1570 {
1571 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1572 event.m_item = m_current;
1573 event.m_code = 0;
1574 event.SetEventObject( this );
1575 GetEventHandler()->ProcessEvent( event );
1576 }
1577 break;
ef44a621 1578
978f38c2
VZ
1579 // up goes to the previous sibling or to the last of its children if
1580 // it's expanded
1581 case WXK_UP:
1582 {
91b8de8d 1583 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
1584 if (!prev)
1585 {
91b8de8d 1586 prev = GetParent( m_key_current );
90e58684
RR
1587 if (prev)
1588 {
1589 long cockie = 0;
91b8de8d 1590 wxTreeItemId current = m_key_current;
90e58684
RR
1591 if (current == GetFirstChild( prev, cockie ))
1592 {
1593 // otherwise we return to where we came from
88ac883a 1594 SelectItem( prev, unselect_others, extended_select );
91b8de8d
RR
1595 m_key_current=prev.m_pItem;
1596 EnsureVisible( prev );
90e58684
RR
1597 break;
1598 }
978f38c2
VZ
1599 }
1600 }
1601 if (prev)
1602 {
69a282d4 1603 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 1604 {
69a282d4
VZ
1605 wxTreeItemId child = GetLastChild(prev);
1606 if ( child )
1607 {
1608 prev = child;
1609 }
978f38c2 1610 }
69a282d4 1611
88ac883a 1612 SelectItem( prev, unselect_others, extended_select );
91b8de8d 1613 m_key_current=prev.m_pItem;
978f38c2
VZ
1614 EnsureVisible( prev );
1615 }
1616 }
1617 break;
ef44a621 1618
978f38c2
VZ
1619 // left arrow goes to the parent
1620 case WXK_LEFT:
1621 {
1622 wxTreeItemId prev = GetParent( m_current );
1623 if (prev)
1624 {
1625 EnsureVisible( prev );
88ac883a 1626 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
1627 }
1628 }
1629 break;
ef44a621 1630
978f38c2
VZ
1631 case WXK_RIGHT:
1632 // this works the same as the down arrow except that we also expand the
1633 // item if it wasn't expanded yet
1634 Expand(m_current);
1635 // fall through
1636
1637 case WXK_DOWN:
ef44a621 1638 {
91b8de8d 1639 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
1640 {
1641 long cookie = 0;
91b8de8d 1642 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 1643 SelectItem( child, unselect_others, extended_select );
91b8de8d 1644 m_key_current=child.m_pItem;
978f38c2
VZ
1645 EnsureVisible( child );
1646 }
1647 else
1648 {
91b8de8d 1649 wxTreeItemId next = GetNextSibling( m_key_current );
978f38c2
VZ
1650 if (next == 0)
1651 {
91b8de8d 1652 wxTreeItemId current = m_key_current;
978f38c2
VZ
1653 while (current && !next)
1654 {
1655 current = GetParent( current );
1656 if (current) next = GetNextSibling( current );
1657 }
1658 }
1659 if (next != 0)
1660 {
88ac883a 1661 SelectItem( next, unselect_others, extended_select );
91b8de8d 1662 m_key_current=next.m_pItem;
978f38c2
VZ
1663 EnsureVisible( next );
1664 }
1665 }
ef44a621 1666 }
978f38c2 1667 break;
ef44a621 1668
978f38c2
VZ
1669 // <End> selects the last visible tree item
1670 case WXK_END:
1671 {
1672 wxTreeItemId last = GetRootItem();
1673
1674 while ( last.IsOk() && IsExpanded(last) )
1675 {
1676 wxTreeItemId lastChild = GetLastChild(last);
1677
1678 // it may happen if the item was expanded but then all of
1679 // its children have been deleted - so IsExpanded() returned
1680 // TRUE, but GetLastChild() returned invalid item
1681 if ( !lastChild )
1682 break;
1683
1684 last = lastChild;
1685 }
1686
1687 if ( last.IsOk() )
1688 {
1689 EnsureVisible( last );
88ac883a 1690 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
1691 }
1692 }
1693 break;
1694
1695 // <Home> selects the root item
1696 case WXK_HOME:
1697 {
1698 wxTreeItemId prev = GetRootItem();
1699 if (prev)
1700 {
1701 EnsureVisible( prev );
88ac883a 1702 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
1703 }
1704 }
1705 break;
1706
1707 default:
1708 event.Skip();
1709 }
edaa81ae 1710}
c801d85f 1711
91b8de8d 1712wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 1713{
67a7abf7
VZ
1714 wxClientDC dc(this);
1715 PrepareDC(dc);
1716 long x = dc.DeviceToLogicalX( (long)point.x );
1717 long y = dc.DeviceToLogicalY( (long)point.y );
91b8de8d
RR
1718 int w, h;
1719 GetSize(&w, &h);
1720
1721 flags=0;
1722 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
1723 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
1724 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
1725 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
d3a9f4af 1726
91b8de8d 1727 return m_anchor->HitTest( wxPoint(x, y), this, flags);
4f22cf8d
RR
1728}
1729
3db7be80 1730void wxTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 1731{
bbe0af5b 1732 if (!event.LeftIsDown()) m_dragCount = 0;
f135ff73 1733
91b8de8d 1734 if ( !(event.LeftUp() || event.LeftDClick() || event.Dragging()) ) return;
29d87bba 1735
bbe0af5b 1736 if ( !m_anchor ) return;
978f38c2 1737
bbe0af5b
RR
1738 wxClientDC dc(this);
1739 PrepareDC(dc);
1740 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1741 long y = dc.DeviceToLogicalY( (long)event.GetY() );
29d87bba 1742
91b8de8d
RR
1743 int flags=0;
1744 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
1745 bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON;
978f38c2 1746
bbe0af5b 1747 if (item == NULL) return; /* we hit the blank area */
29d87bba 1748
bbe0af5b
RR
1749 if (event.Dragging())
1750 {
1751 if (m_dragCount == 2) /* small drag latency (3?) */
1752 {
1753 m_dragCount = 0;
978f38c2 1754
bbe0af5b
RR
1755 wxTreeEvent nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG, GetId());
1756 nevent.m_item = m_current;
1757 nevent.SetEventObject(this);
1758 GetEventHandler()->ProcessEvent(nevent);
1759 }
1760 else
1761 {
1762 m_dragCount++;
1763 }
1764 return;
1765 }
978f38c2 1766
88ac883a
VZ
1767 bool is_multiple=(GetWindowStyleFlag() & wxTR_MULTIPLE);
1768 bool extended_select=(event.ShiftDown() && is_multiple);
1769 bool unselect_others=!(extended_select || (event.ControlDown() && is_multiple));
1770
91b8de8d
RR
1771 if (onButton)
1772 {
1773 Toggle( item );
1774 if (is_multiple) return;
1775 }
1776
88ac883a 1777 SelectItem(item, unselect_others, extended_select);
29d87bba 1778
bbe0af5b
RR
1779 if (event.LeftDClick())
1780 {
1781 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1782 event.m_item = item;
1783 event.m_code = 0;
1784 event.SetEventObject( this );
1785 GetEventHandler()->ProcessEvent( event );
1786 }
edaa81ae 1787}
c801d85f 1788
3db7be80
RR
1789void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1790{
bbe0af5b
RR
1791 /* after all changes have been done to the tree control,
1792 * we actually redraw the tree when everything is over */
ef44a621 1793
f65635b5
VZ
1794 if (!m_dirty)
1795 return;
ef44a621 1796
bbe0af5b 1797 m_dirty = FALSE;
3db7be80 1798
bbe0af5b 1799 CalculatePositions();
91b8de8d 1800 Refresh();
bbe0af5b 1801 AdjustMyScrollbars();
3db7be80
RR
1802}
1803
91b8de8d 1804void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 1805{
91b8de8d
RR
1806 long text_w = 0;
1807 long text_h = 0;
1808 // TODO : check for boldness. Here with suppose that font normal and bold
1809 // have the same height !
1810 // TODO : bug here, text_w is sometime not the correct answer !!!
1811 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1812 text_h+=4;
1813
1814 int image_h = 0;
1815 int image_w = 0;
1816 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1817 {
1818 m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
1819 image_w += 4;
1820 }
1821 else if (item->GetImage() != -1)
1822 {
1823 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
1824 image_w += 4;
1825 }
1826
1827 int total_h = (image_h > text_h) ? image_h : text_h;
1828
1829 if (total_h<40) total_h+=4; // at least 4 pixels
1830 else total_h+=total_h/10; // otherwise 10% extra spacing
1831
1832 item->SetHeight(total_h);
1833 if (total_h>m_lineHeight) m_lineHeight=total_h;
bbe0af5b 1834
91b8de8d
RR
1835 item->SetWidth(image_w+text_w+2);
1836}
1837
1838// -----------------------------------------------------------------------------
1839// for developper : y is now the top of the level
1840// not the middle of it !
bbe0af5b 1841void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1842{
bbe0af5b 1843 int horizX = level*m_indent;
389cdc7a 1844
91b8de8d 1845 CalculateSize( item, dc );
d3a9f4af 1846
91b8de8d 1847 // set its position
cf724bce 1848 item->SetX( horizX+m_indent+m_spacing );
91b8de8d
RR
1849 item->SetY( y );
1850 y+=GetLineHeight(item);
4c681997 1851
bbe0af5b
RR
1852 if ( !item->IsExpanded() )
1853 {
f65635b5 1854 // we dont need to calculate collapsed branches
bbe0af5b
RR
1855 return;
1856 }
389cdc7a 1857
91b8de8d
RR
1858 wxArrayGenericTreeItems& children = item->GetChildren();
1859 size_t n, count = children.Count();
1860 for (n = 0; n < count; ++n )
1861 CalculateLevel( children[n], dc, level+1, y ); // recurse
edaa81ae 1862}
c801d85f 1863
74bedbeb 1864void wxTreeCtrl::CalculatePositions()
c801d85f 1865{
bbe0af5b 1866 if ( !m_anchor ) return;
29d87bba 1867
bbe0af5b
RR
1868 wxClientDC dc(this);
1869 PrepareDC( dc );
29d87bba 1870
bbe0af5b 1871 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
29d87bba 1872
bbe0af5b 1873 dc.SetPen( m_dottedPen );
91b8de8d
RR
1874 //if(GetImageList() == NULL)
1875 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1876
91b8de8d 1877 int y = 2; //GetLineHeight(m_anchor) / 2 + 2;
f65635b5 1878 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 1879}
c801d85f 1880
f135ff73 1881void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 1882{
bbe0af5b
RR
1883 wxClientDC dc(this);
1884 PrepareDC(dc);
4832f7c0 1885
bbe0af5b
RR
1886 int cw = 0;
1887 int ch = 0;
1888 GetClientSize( &cw, &ch );
4832f7c0 1889
bbe0af5b
RR
1890 wxRect rect;
1891 rect.x = dc.LogicalToDeviceX( 0 );
1892 rect.width = cw;
1893 rect.y = dc.LogicalToDeviceY( item->GetY() );
1894 rect.height = ch;
f135ff73 1895
bbe0af5b 1896 Refresh( TRUE, &rect );
f135ff73 1897
bbe0af5b 1898 AdjustMyScrollbars();
edaa81ae 1899}
c801d85f
KB
1900
1901void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1902{
bbe0af5b
RR
1903 wxClientDC dc(this);
1904 PrepareDC( dc );
1905
1906 wxRect rect;
1907 rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
91b8de8d 1908 rect.y = dc.LogicalToDeviceY( item->GetY());
bbe0af5b 1909 rect.width = 1000;
91b8de8d 1910 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 1911
bbe0af5b 1912 Refresh( TRUE, &rect );
edaa81ae 1913}
c801d85f 1914