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