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