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