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