]>
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 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 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::GetSystemColour | |
673 | ( | |
674 | wxSYS_COLOUR_HIGHLIGHT | |
675 | ), | |
676 | wxSOLID | |
677 | ); | |
678 | ||
679 | m_hilightUnfocusedBrush = new wxBrush | |
680 | ( | |
681 | wxSystemSettings::GetSystemColour | |
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::GetSystemFont( 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 | SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) ); | |
758 | ||
759 | // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86 | |
760 | m_dottedPen = wxPen( wxT("grey"), 0, 0 ); | |
761 | ||
762 | return TRUE; | |
763 | } | |
764 | ||
765 | wxGenericTreeCtrl::~wxGenericTreeCtrl() | |
766 | { | |
767 | delete m_hilightBrush; | |
768 | delete m_hilightUnfocusedBrush; | |
769 | ||
770 | if (m_arrowRight) delete m_arrowRight; | |
771 | if (m_arrowDown) delete m_arrowDown; | |
772 | ||
773 | DeleteAllItems(); | |
774 | ||
775 | delete m_renameTimer; | |
776 | if (m_ownsImageListNormal) delete m_imageListNormal; | |
777 | if (m_ownsImageListState) delete m_imageListState; | |
778 | if (m_ownsImageListButtons) delete m_imageListButtons; | |
779 | } | |
780 | ||
781 | // ----------------------------------------------------------------------------- | |
782 | // accessors | |
783 | // ----------------------------------------------------------------------------- | |
784 | ||
785 | size_t wxGenericTreeCtrl::GetCount() const | |
786 | { | |
787 | return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount(); | |
788 | } | |
789 | ||
790 | void wxGenericTreeCtrl::SetIndent(unsigned int indent) | |
791 | { | |
792 | m_indent = indent; | |
793 | m_dirty = TRUE; | |
794 | } | |
795 | ||
796 | void wxGenericTreeCtrl::SetSpacing(unsigned int spacing) | |
797 | { | |
798 | m_spacing = spacing; | |
799 | m_dirty = TRUE; | |
800 | } | |
801 | ||
802 | size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively) | |
803 | { | |
804 | wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") ); | |
805 | ||
806 | return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively); | |
807 | } | |
808 | ||
809 | void wxGenericTreeCtrl::SetWindowStyle(const long styles) | |
810 | { | |
811 | // right now, just sets the styles. Eventually, we may | |
812 | // want to update the inherited styles, but right now | |
813 | // none of the parents has updatable styles | |
814 | m_windowStyle = styles; | |
815 | m_dirty = TRUE; | |
816 | } | |
817 | ||
818 | // ----------------------------------------------------------------------------- | |
819 | // functions to work with tree items | |
820 | // ----------------------------------------------------------------------------- | |
821 | ||
822 | wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const | |
823 | { | |
824 | wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") ); | |
825 | ||
826 | return ((wxGenericTreeItem*) item.m_pItem)->GetText(); | |
827 | } | |
828 | ||
829 | int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item, | |
830 | wxTreeItemIcon which) const | |
831 | { | |
832 | wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") ); | |
833 | ||
834 | return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which); | |
835 | } | |
836 | ||
837 | wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const | |
838 | { | |
839 | wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") ); | |
840 | ||
841 | return ((wxGenericTreeItem*) item.m_pItem)->GetData(); | |
842 | } | |
843 | ||
844 | void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) | |
845 | { | |
846 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
847 | ||
848 | wxClientDC dc(this); | |
849 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; | |
850 | pItem->SetText(text); | |
851 | CalculateSize(pItem, dc); | |
852 | RefreshLine(pItem); | |
853 | } | |
854 | ||
855 | void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item, | |
856 | int image, | |
857 | wxTreeItemIcon which) | |
858 | { | |
859 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
860 | ||
861 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; | |
862 | pItem->SetImage(image, which); | |
863 | ||
864 | wxClientDC dc(this); | |
865 | CalculateSize(pItem, dc); | |
866 | RefreshLine(pItem); | |
867 | } | |
868 | ||
869 | void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) | |
870 | { | |
871 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
872 | ||
873 | ((wxGenericTreeItem*) item.m_pItem)->SetData(data); | |
874 | } | |
875 | ||
876 | void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) | |
877 | { | |
878 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
879 | ||
880 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; | |
881 | pItem->SetHasPlus(has); | |
882 | RefreshLine(pItem); | |
883 | } | |
884 | ||
885 | void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) | |
886 | { | |
887 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
888 | ||
889 | // avoid redrawing the tree if no real change | |
890 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; | |
891 | if ( pItem->IsBold() != bold ) | |
892 | { | |
893 | pItem->SetBold(bold); | |
894 | RefreshLine(pItem); | |
895 | } | |
896 | } | |
897 | ||
898 | void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item, | |
899 | const wxColour& col) | |
900 | { | |
901 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
902 | ||
903 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; | |
904 | pItem->Attr().SetTextColour(col); | |
905 | RefreshLine(pItem); | |
906 | } | |
907 | ||
908 | void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, | |
909 | const wxColour& col) | |
910 | { | |
911 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
912 | ||
913 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; | |
914 | pItem->Attr().SetBackgroundColour(col); | |
915 | RefreshLine(pItem); | |
916 | } | |
917 | ||
918 | void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) | |
919 | { | |
920 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
921 | ||
922 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; | |
923 | pItem->Attr().SetFont(font); | |
924 | RefreshLine(pItem); | |
925 | } | |
926 | ||
927 | bool wxGenericTreeCtrl::SetFont( const wxFont &font ) | |
928 | { | |
929 | wxScrolledWindow::SetFont(font); | |
930 | ||
931 | m_normalFont = font ; | |
932 | m_boldFont = wxFont( m_normalFont.GetPointSize(), | |
933 | m_normalFont.GetFamily(), | |
934 | m_normalFont.GetStyle(), | |
935 | wxBOLD, | |
936 | m_normalFont.GetUnderlined()); | |
937 | ||
938 | return TRUE; | |
939 | } | |
940 | ||
941 | ||
942 | // ----------------------------------------------------------------------------- | |
943 | // item status inquiries | |
944 | // ----------------------------------------------------------------------------- | |
945 | ||
946 | bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const | |
947 | { | |
948 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); | |
949 | ||
950 | // An item is only visible if it's not a descendant of a collapsed item | |
951 | wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem; | |
952 | wxGenericTreeItem* parent = pItem->GetParent(); | |
953 | while (parent) | |
954 | { | |
955 | if (!parent->IsExpanded()) | |
956 | return FALSE; | |
957 | parent = parent->GetParent(); | |
958 | } | |
959 | ||
960 | int startX, startY; | |
961 | GetViewStart(& startX, & startY); | |
962 | ||
963 | wxSize clientSize = GetClientSize(); | |
964 | ||
965 | wxRect rect; | |
966 | if (!GetBoundingRect(item, rect)) | |
967 | return FALSE; | |
968 | if (rect.GetWidth() == 0 || rect.GetHeight() == 0) | |
969 | return FALSE; | |
970 | if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y) | |
971 | return FALSE; | |
972 | if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x) | |
973 | return FALSE; | |
974 | ||
975 | return TRUE; | |
976 | } | |
977 | ||
978 | bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const | |
979 | { | |
980 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); | |
981 | ||
982 | // consider that the item does have children if it has the "+" button: it | |
983 | // might not have them (if it had never been expanded yet) but then it | |
984 | // could have them as well and it's better to err on this side rather than | |
985 | // disabling some operations which are restricted to the items with | |
986 | // children for an item which does have them | |
987 | return ((wxGenericTreeItem*) item.m_pItem)->HasPlus(); | |
988 | } | |
989 | ||
990 | bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const | |
991 | { | |
992 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); | |
993 | ||
994 | return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded(); | |
995 | } | |
996 | ||
997 | bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const | |
998 | { | |
999 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); | |
1000 | ||
1001 | return ((wxGenericTreeItem*) item.m_pItem)->IsSelected(); | |
1002 | } | |
1003 | ||
1004 | bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const | |
1005 | { | |
1006 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); | |
1007 | ||
1008 | return ((wxGenericTreeItem*) item.m_pItem)->IsBold(); | |
1009 | } | |
1010 | ||
1011 | // ----------------------------------------------------------------------------- | |
1012 | // navigation | |
1013 | // ----------------------------------------------------------------------------- | |
1014 | ||
1015 | wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const | |
1016 | { | |
1017 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1018 | ||
1019 | return ((wxGenericTreeItem*) item.m_pItem)->GetParent(); | |
1020 | } | |
1021 | ||
1022 | wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const | |
1023 | { | |
1024 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1025 | ||
1026 | cookie = 0; | |
1027 | return GetNextChild(item, cookie); | |
1028 | } | |
1029 | ||
1030 | wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const | |
1031 | { | |
1032 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1033 | ||
1034 | wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren(); | |
1035 | if ( (size_t)cookie < children.Count() ) | |
1036 | { | |
1037 | return children.Item((size_t)cookie++); | |
1038 | } | |
1039 | else | |
1040 | { | |
1041 | // there are no more of them | |
1042 | return wxTreeItemId(); | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const | |
1047 | { | |
1048 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1049 | ||
1050 | wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren(); | |
1051 | return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last())); | |
1052 | } | |
1053 | ||
1054 | wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const | |
1055 | { | |
1056 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1057 | ||
1058 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; | |
1059 | wxGenericTreeItem *parent = i->GetParent(); | |
1060 | if ( parent == NULL ) | |
1061 | { | |
1062 | // root item doesn't have any siblings | |
1063 | return wxTreeItemId(); | |
1064 | } | |
1065 | ||
1066 | wxArrayGenericTreeItems& siblings = parent->GetChildren(); | |
1067 | int index = siblings.Index(i); | |
1068 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? | |
1069 | ||
1070 | size_t n = (size_t)(index + 1); | |
1071 | return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]); | |
1072 | } | |
1073 | ||
1074 | wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const | |
1075 | { | |
1076 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1077 | ||
1078 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; | |
1079 | wxGenericTreeItem *parent = i->GetParent(); | |
1080 | if ( parent == NULL ) | |
1081 | { | |
1082 | // root item doesn't have any siblings | |
1083 | return wxTreeItemId(); | |
1084 | } | |
1085 | ||
1086 | wxArrayGenericTreeItems& siblings = parent->GetChildren(); | |
1087 | int index = siblings.Index(i); | |
1088 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? | |
1089 | ||
1090 | return index == 0 ? wxTreeItemId() | |
1091 | : wxTreeItemId(siblings[(size_t)(index - 1)]); | |
1092 | } | |
1093 | ||
1094 | // Only for internal use right now, but should probably be public | |
1095 | wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const | |
1096 | { | |
1097 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1098 | ||
1099 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; | |
1100 | ||
1101 | // First see if there are any children. | |
1102 | wxArrayGenericTreeItems& children = i->GetChildren(); | |
1103 | if (children.GetCount() > 0) | |
1104 | { | |
1105 | return children.Item(0); | |
1106 | } | |
1107 | else | |
1108 | { | |
1109 | // Try a sibling of this or ancestor instead | |
1110 | wxTreeItemId p = item; | |
1111 | wxTreeItemId toFind; | |
1112 | do | |
1113 | { | |
1114 | toFind = GetNextSibling(p); | |
1115 | p = GetParent(p); | |
1116 | } while (p.IsOk() && !toFind.IsOk()); | |
1117 | return toFind; | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const | |
1122 | { | |
1123 | wxTreeItemId id = GetRootItem(); | |
1124 | if (!id.IsOk()) | |
1125 | return id; | |
1126 | ||
1127 | do | |
1128 | { | |
1129 | if (IsVisible(id)) | |
1130 | return id; | |
1131 | id = GetNext(id); | |
1132 | } while (id.IsOk()); | |
1133 | ||
1134 | return wxTreeItemId(); | |
1135 | } | |
1136 | ||
1137 | wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const | |
1138 | { | |
1139 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1140 | ||
1141 | wxTreeItemId id = item; | |
1142 | if (id.IsOk()) | |
1143 | { | |
1144 | while (id = GetNext(id), id.IsOk()) | |
1145 | { | |
1146 | if (IsVisible(id)) | |
1147 | return id; | |
1148 | } | |
1149 | } | |
1150 | return wxTreeItemId(); | |
1151 | } | |
1152 | ||
1153 | wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const | |
1154 | { | |
1155 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); | |
1156 | ||
1157 | wxFAIL_MSG(wxT("not implemented")); | |
1158 | ||
1159 | return wxTreeItemId(); | |
1160 | } | |
1161 | ||
1162 | // ----------------------------------------------------------------------------- | |
1163 | // operations | |
1164 | // ----------------------------------------------------------------------------- | |
1165 | ||
1166 | wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId, | |
1167 | size_t previous, | |
1168 | const wxString& text, | |
1169 | int image, int selImage, | |
1170 | wxTreeItemData *data) | |
1171 | { | |
1172 | wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem; | |
1173 | if ( !parent ) | |
1174 | { | |
1175 | // should we give a warning here? | |
1176 | return AddRoot(text, image, selImage, data); | |
1177 | } | |
1178 | ||
1179 | m_dirty = TRUE; // do this first so stuff below doesn't cause flicker | |
1180 | ||
1181 | wxGenericTreeItem *item = | |
1182 | new wxGenericTreeItem( parent, text, image, selImage, data ); | |
1183 | ||
1184 | if ( data != NULL ) | |
1185 | { | |
1186 | data->m_pItem = (long) item; | |
1187 | } | |
1188 | ||
1189 | parent->Insert( item, previous ); | |
1190 | ||
1191 | return item; | |
1192 | } | |
1193 | ||
1194 | wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text, | |
1195 | int image, int selImage, | |
1196 | wxTreeItemData *data) | |
1197 | { | |
1198 | wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") ); | |
1199 | ||
1200 | m_dirty = TRUE; // do this first so stuff below doesn't cause flicker | |
1201 | ||
1202 | m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, | |
1203 | image, selImage, data); | |
1204 | if (HasFlag(wxTR_HIDE_ROOT)) | |
1205 | { | |
1206 | // if root is hidden, make sure we can navigate | |
1207 | // into children | |
1208 | m_anchor->SetHasPlus(); | |
1209 | Expand(m_anchor); | |
1210 | } | |
1211 | if ( data != NULL ) | |
1212 | { | |
1213 | data->m_pItem = (long) m_anchor; | |
1214 | } | |
1215 | ||
1216 | if (!HasFlag(wxTR_MULTIPLE)) | |
1217 | { | |
1218 | m_current = m_key_current = m_anchor; | |
1219 | m_current->SetHilight( TRUE ); | |
1220 | } | |
1221 | ||
1222 | return m_anchor; | |
1223 | } | |
1224 | ||
1225 | wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent, | |
1226 | const wxString& text, | |
1227 | int image, int selImage, | |
1228 | wxTreeItemData *data) | |
1229 | { | |
1230 | return DoInsertItem(parent, 0u, text, image, selImage, data); | |
1231 | } | |
1232 | ||
1233 | wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId, | |
1234 | const wxTreeItemId& idPrevious, | |
1235 | const wxString& text, | |
1236 | int image, int selImage, | |
1237 | wxTreeItemData *data) | |
1238 | { | |
1239 | wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem; | |
1240 | if ( !parent ) | |
1241 | { | |
1242 | // should we give a warning here? | |
1243 | return AddRoot(text, image, selImage, data); | |
1244 | } | |
1245 | ||
1246 | int index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem); | |
1247 | wxASSERT_MSG( index != wxNOT_FOUND, | |
1248 | wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") ); | |
1249 | ||
1250 | return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data); | |
1251 | } | |
1252 | ||
1253 | wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId, | |
1254 | size_t before, | |
1255 | const wxString& text, | |
1256 | int image, int selImage, | |
1257 | wxTreeItemData *data) | |
1258 | { | |
1259 | wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem; | |
1260 | if ( !parent ) | |
1261 | { | |
1262 | // should we give a warning here? | |
1263 | return AddRoot(text, image, selImage, data); | |
1264 | } | |
1265 | ||
1266 | return DoInsertItem(parentId, before, text, image, selImage, data); | |
1267 | } | |
1268 | ||
1269 | wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId, | |
1270 | const wxString& text, | |
1271 | int image, int selImage, | |
1272 | wxTreeItemData *data) | |
1273 | { | |
1274 | wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem; | |
1275 | if ( !parent ) | |
1276 | { | |
1277 | // should we give a warning here? | |
1278 | return AddRoot(text, image, selImage, data); | |
1279 | } | |
1280 | ||
1281 | return DoInsertItem( parent, parent->GetChildren().Count(), text, | |
1282 | image, selImage, data); | |
1283 | } | |
1284 | ||
1285 | void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item) | |
1286 | { | |
1287 | wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() ); | |
1288 | event.m_item = (long) item; | |
1289 | event.SetEventObject( this ); | |
1290 | ProcessEvent( event ); | |
1291 | } | |
1292 | ||
1293 | void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId) | |
1294 | { | |
1295 | m_dirty = TRUE; // do this first so stuff below doesn't cause flicker | |
1296 | ||
1297 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; | |
1298 | item->DeleteChildren(this); | |
1299 | } | |
1300 | ||
1301 | void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId) | |
1302 | { | |
1303 | m_dirty = TRUE; // do this first so stuff below doesn't cause flicker | |
1304 | ||
1305 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; | |
1306 | ||
1307 | // don't stay with invalid m_key_current or we will crash in | |
1308 | // the next call to OnChar() | |
1309 | bool changeKeyCurrent = FALSE; | |
1310 | wxGenericTreeItem *itemKey = m_key_current; | |
1311 | while ( itemKey ) | |
1312 | { | |
1313 | if ( itemKey == item ) | |
1314 | { | |
1315 | // m_key_current is a descendant of the item being deleted | |
1316 | changeKeyCurrent = TRUE; | |
1317 | break; | |
1318 | } | |
1319 | itemKey = itemKey->GetParent(); | |
1320 | } | |
1321 | ||
1322 | wxGenericTreeItem *parent = item->GetParent(); | |
1323 | if ( parent ) | |
1324 | { | |
1325 | parent->GetChildren().Remove( item ); // remove by value | |
1326 | } | |
1327 | ||
1328 | if ( changeKeyCurrent ) | |
1329 | { | |
1330 | // may be NULL or not | |
1331 | m_key_current = parent; | |
1332 | } | |
1333 | ||
1334 | item->DeleteChildren(this); | |
1335 | SendDeleteEvent(item); | |
1336 | delete item; | |
1337 | } | |
1338 | ||
1339 | void wxGenericTreeCtrl::DeleteAllItems() | |
1340 | { | |
1341 | if ( m_anchor ) | |
1342 | { | |
1343 | m_dirty = TRUE; | |
1344 | ||
1345 | m_anchor->DeleteChildren(this); | |
1346 | delete m_anchor; | |
1347 | ||
1348 | m_anchor = NULL; | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId) | |
1353 | { | |
1354 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; | |
1355 | ||
1356 | wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") ); | |
1357 | ||
1358 | if ( !item->HasPlus() ) | |
1359 | return; | |
1360 | ||
1361 | if ( item->IsExpanded() ) | |
1362 | return; | |
1363 | ||
1364 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() ); | |
1365 | event.m_item = (long) item; | |
1366 | event.SetEventObject( this ); | |
1367 | ||
1368 | if ( ProcessEvent( event ) && !event.IsAllowed() ) | |
1369 | { | |
1370 | // cancelled by program | |
1371 | return; | |
1372 | } | |
1373 | ||
1374 | item->Expand(); | |
1375 | CalculatePositions(); | |
1376 | ||
1377 | RefreshSubtree(item); | |
1378 | ||
1379 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED); | |
1380 | ProcessEvent( event ); | |
1381 | } | |
1382 | ||
1383 | void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item) | |
1384 | { | |
1385 | Expand(item); | |
1386 | if ( IsExpanded(item) ) | |
1387 | { | |
1388 | long cookie; | |
1389 | wxTreeItemId child = GetFirstChild(item, cookie); | |
1390 | while ( child.IsOk() ) | |
1391 | { | |
1392 | ExpandAll(child); | |
1393 | ||
1394 | child = GetNextChild(item, cookie); | |
1395 | } | |
1396 | } | |
1397 | } | |
1398 | ||
1399 | void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId) | |
1400 | { | |
1401 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; | |
1402 | ||
1403 | if ( !item->IsExpanded() ) | |
1404 | return; | |
1405 | ||
1406 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() ); | |
1407 | event.m_item = (long) item; | |
1408 | event.SetEventObject( this ); | |
1409 | if ( ProcessEvent( event ) && !event.IsAllowed() ) | |
1410 | { | |
1411 | // cancelled by program | |
1412 | return; | |
1413 | } | |
1414 | ||
1415 | item->Collapse(); | |
1416 | ||
1417 | #if 0 // TODO why should items be collapsed recursively? | |
1418 | wxArrayGenericTreeItems& children = item->GetChildren(); | |
1419 | size_t count = children.Count(); | |
1420 | for ( size_t n = 0; n < count; n++ ) | |
1421 | { | |
1422 | Collapse(children[n]); | |
1423 | } | |
1424 | #endif | |
1425 | ||
1426 | CalculatePositions(); | |
1427 | ||
1428 | RefreshSubtree(item); | |
1429 | ||
1430 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED); | |
1431 | ProcessEvent( event ); | |
1432 | } | |
1433 | ||
1434 | void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item) | |
1435 | { | |
1436 | Collapse(item); | |
1437 | DeleteChildren(item); | |
1438 | } | |
1439 | ||
1440 | void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId) | |
1441 | { | |
1442 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; | |
1443 | ||
1444 | if (item->IsExpanded()) | |
1445 | Collapse(itemId); | |
1446 | else | |
1447 | Expand(itemId); | |
1448 | } | |
1449 | ||
1450 | void wxGenericTreeCtrl::Unselect() | |
1451 | { | |
1452 | if (m_current) | |
1453 | { | |
1454 | m_current->SetHilight( FALSE ); | |
1455 | RefreshLine( m_current ); | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item) | |
1460 | { | |
1461 | if (item->IsSelected()) | |
1462 | { | |
1463 | item->SetHilight(FALSE); | |
1464 | RefreshLine(item); | |
1465 | } | |
1466 | ||
1467 | if (item->HasChildren()) | |
1468 | { | |
1469 | wxArrayGenericTreeItems& children = item->GetChildren(); | |
1470 | size_t count = children.Count(); | |
1471 | for ( size_t n = 0; n < count; ++n ) | |
1472 | { | |
1473 | UnselectAllChildren(children[n]); | |
1474 | } | |
1475 | } | |
1476 | } | |
1477 | ||
1478 | void wxGenericTreeCtrl::UnselectAll() | |
1479 | { | |
1480 | UnselectAllChildren((wxGenericTreeItem*) GetRootItem().m_pItem); | |
1481 | } | |
1482 | ||
1483 | // Recursive function ! | |
1484 | // To stop we must have crt_item<last_item | |
1485 | // Algorithm : | |
1486 | // Tag all next children, when no more children, | |
1487 | // Move to parent (not to tag) | |
1488 | // Keep going... if we found last_item, we stop. | |
1489 | bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select) | |
1490 | { | |
1491 | wxGenericTreeItem *parent = crt_item->GetParent(); | |
1492 | ||
1493 | if (parent == NULL) // This is root item | |
1494 | return TagAllChildrenUntilLast(crt_item, last_item, select); | |
1495 | ||
1496 | wxArrayGenericTreeItems& children = parent->GetChildren(); | |
1497 | int index = children.Index(crt_item); | |
1498 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? | |
1499 | ||
1500 | size_t count = children.Count(); | |
1501 | for (size_t n=(size_t)(index+1); n<count; ++n) | |
1502 | { | |
1503 | if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE; | |
1504 | } | |
1505 | ||
1506 | return TagNextChildren(parent, last_item, select); | |
1507 | } | |
1508 | ||
1509 | bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select) | |
1510 | { | |
1511 | crt_item->SetHilight(select); | |
1512 | RefreshLine(crt_item); | |
1513 | ||
1514 | if (crt_item==last_item) | |
1515 | return TRUE; | |
1516 | ||
1517 | if (crt_item->HasChildren()) | |
1518 | { | |
1519 | wxArrayGenericTreeItems& children = crt_item->GetChildren(); | |
1520 | size_t count = children.Count(); | |
1521 | for ( size_t n = 0; n < count; ++n ) | |
1522 | { | |
1523 | if (TagAllChildrenUntilLast(children[n], last_item, select)) | |
1524 | return TRUE; | |
1525 | } | |
1526 | } | |
1527 | ||
1528 | return FALSE; | |
1529 | } | |
1530 | ||
1531 | void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2) | |
1532 | { | |
1533 | // item2 is not necessary after item1 | |
1534 | wxGenericTreeItem *first=NULL, *last=NULL; | |
1535 | ||
1536 | // choice first' and 'last' between item1 and item2 | |
1537 | if (item1->GetY()<item2->GetY()) | |
1538 | { | |
1539 | first=item1; | |
1540 | last=item2; | |
1541 | } | |
1542 | else | |
1543 | { | |
1544 | first=item2; | |
1545 | last=item1; | |
1546 | } | |
1547 | ||
1548 | bool select = m_current->IsSelected(); | |
1549 | ||
1550 | if ( TagAllChildrenUntilLast(first,last,select) ) | |
1551 | return; | |
1552 | ||
1553 | TagNextChildren(first,last,select); | |
1554 | } | |
1555 | ||
1556 | void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, | |
1557 | bool unselect_others, | |
1558 | bool extended_select) | |
1559 | { | |
1560 | wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") ); | |
1561 | ||
1562 | bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE); | |
1563 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; | |
1564 | ||
1565 | //wxCHECK_RET( ( (!unselect_others) && is_single), | |
1566 | // wxT("this is a single selection tree") ); | |
1567 | ||
1568 | // to keep going anyhow !!! | |
1569 | if (is_single) | |
1570 | { | |
1571 | if (item->IsSelected()) | |
1572 | return; // nothing to do | |
1573 | unselect_others = TRUE; | |
1574 | extended_select = FALSE; | |
1575 | } | |
1576 | else if ( unselect_others && item->IsSelected() ) | |
1577 | { | |
1578 | // selection change if there is more than one item currently selected | |
1579 | wxArrayTreeItemIds selected_items; | |
1580 | if ( GetSelections(selected_items) == 1 ) | |
1581 | return; | |
1582 | } | |
1583 | ||
1584 | wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() ); | |
1585 | event.m_item = (long) item; | |
1586 | event.m_itemOld = (long) m_current; | |
1587 | event.SetEventObject( this ); | |
1588 | // TODO : Here we don't send any selection mode yet ! | |
1589 | ||
1590 | if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() ) | |
1591 | return; | |
1592 | ||
1593 | wxTreeItemId parent = GetParent( itemId ); | |
1594 | while (parent.IsOk()) | |
1595 | { | |
1596 | if (!IsExpanded(parent)) | |
1597 | Expand( parent ); | |
1598 | ||
1599 | parent = GetParent( parent ); | |
1600 | } | |
1601 | ||
1602 | EnsureVisible( itemId ); | |
1603 | ||
1604 | // ctrl press | |
1605 | if (unselect_others) | |
1606 | { | |
1607 | if (is_single) Unselect(); // to speed up thing | |
1608 | else UnselectAll(); | |
1609 | } | |
1610 | ||
1611 | // shift press | |
1612 | if (extended_select) | |
1613 | { | |
1614 | if ( !m_current ) | |
1615 | { | |
1616 | m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem; | |
1617 | } | |
1618 | ||
1619 | // don't change the mark (m_current) | |
1620 | SelectItemRange(m_current, item); | |
1621 | } | |
1622 | else | |
1623 | { | |
1624 | bool select=TRUE; // the default | |
1625 | ||
1626 | // Check if we need to toggle hilight (ctrl mode) | |
1627 | if (!unselect_others) | |
1628 | select=!item->IsSelected(); | |
1629 | ||
1630 | m_current = m_key_current = item; | |
1631 | m_current->SetHilight(select); | |
1632 | RefreshLine( m_current ); | |
1633 | } | |
1634 | ||
1635 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); | |
1636 | GetEventHandler()->ProcessEvent( event ); | |
1637 | } | |
1638 | ||
1639 | void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item, | |
1640 | wxArrayTreeItemIds &array) const | |
1641 | { | |
1642 | if ( item->IsSelected() ) | |
1643 | array.Add(wxTreeItemId(item)); | |
1644 | ||
1645 | if ( item->HasChildren() ) | |
1646 | { | |
1647 | wxArrayGenericTreeItems& children = item->GetChildren(); | |
1648 | size_t count = children.GetCount(); | |
1649 | for ( size_t n = 0; n < count; ++n ) | |
1650 | FillArray(children[n], array); | |
1651 | } | |
1652 | } | |
1653 | ||
1654 | size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const | |
1655 | { | |
1656 | array.Empty(); | |
1657 | wxTreeItemId idRoot = GetRootItem(); | |
1658 | if ( idRoot.IsOk() ) | |
1659 | { | |
1660 | FillArray((wxGenericTreeItem*) idRoot.m_pItem, array); | |
1661 | } | |
1662 | //else: the tree is empty, so no selections | |
1663 | ||
1664 | return array.Count(); | |
1665 | } | |
1666 | ||
1667 | void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item) | |
1668 | { | |
1669 | if (!item.IsOk()) return; | |
1670 | ||
1671 | wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem; | |
1672 | ||
1673 | // first expand all parent branches | |
1674 | wxGenericTreeItem *parent = gitem->GetParent(); | |
1675 | while ( parent ) | |
1676 | { | |
1677 | Expand(parent); | |
1678 | parent = parent->GetParent(); | |
1679 | } | |
1680 | ||
1681 | //if (parent) CalculatePositions(); | |
1682 | ||
1683 | ScrollTo(item); | |
1684 | } | |
1685 | ||
1686 | void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item) | |
1687 | { | |
1688 | if (!item.IsOk()) return; | |
1689 | ||
1690 | // We have to call this here because the label in | |
1691 | // question might just have been added and no screen | |
1692 | // update taken place. | |
1693 | if (m_dirty) wxYieldIfNeeded(); | |
1694 | ||
1695 | wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem; | |
1696 | ||
1697 | // now scroll to the item | |
1698 | int item_y = gitem->GetY(); | |
1699 | ||
1700 | int start_x = 0; | |
1701 | int start_y = 0; | |
1702 | GetViewStart( &start_x, &start_y ); | |
1703 | start_y *= PIXELS_PER_UNIT; | |
1704 | ||
1705 | int client_h = 0; | |
1706 | int client_w = 0; | |
1707 | GetClientSize( &client_w, &client_h ); | |
1708 | ||
1709 | if (item_y < start_y+3) | |
1710 | { | |
1711 | // going down | |
1712 | int x = 0; | |
1713 | int y = 0; | |
1714 | m_anchor->GetSize( x, y, this ); | |
1715 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
1716 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
1717 | int x_pos = GetScrollPos( wxHORIZONTAL ); | |
1718 | // Item should appear at top | |
1719 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT ); | |
1720 | } | |
1721 | else if (item_y+GetLineHeight(gitem) > start_y+client_h) | |
1722 | { | |
1723 | // going up | |
1724 | int x = 0; | |
1725 | int y = 0; | |
1726 | m_anchor->GetSize( x, y, this ); | |
1727 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
1728 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
1729 | item_y += PIXELS_PER_UNIT+2; | |
1730 | int x_pos = GetScrollPos( wxHORIZONTAL ); | |
1731 | // Item should appear at bottom | |
1732 | 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 ); | |
1733 | } | |
1734 | } | |
1735 | ||
1736 | // FIXME: tree sorting functions are not reentrant and not MT-safe! | |
1737 | static wxGenericTreeCtrl *s_treeBeingSorted = NULL; | |
1738 | ||
1739 | static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1, | |
1740 | wxGenericTreeItem **item2) | |
1741 | { | |
1742 | wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") ); | |
1743 | ||
1744 | return s_treeBeingSorted->OnCompareItems(*item1, *item2); | |
1745 | } | |
1746 | ||
1747 | int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1, | |
1748 | const wxTreeItemId& item2) | |
1749 | { | |
1750 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); | |
1751 | } | |
1752 | ||
1753 | void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId) | |
1754 | { | |
1755 | wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") ); | |
1756 | ||
1757 | wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; | |
1758 | ||
1759 | wxCHECK_RET( !s_treeBeingSorted, | |
1760 | wxT("wxGenericTreeCtrl::SortChildren is not reentrant") ); | |
1761 | ||
1762 | wxArrayGenericTreeItems& children = item->GetChildren(); | |
1763 | if ( children.Count() > 1 ) | |
1764 | { | |
1765 | m_dirty = TRUE; | |
1766 | ||
1767 | s_treeBeingSorted = this; | |
1768 | children.Sort(tree_ctrl_compare_func); | |
1769 | s_treeBeingSorted = NULL; | |
1770 | } | |
1771 | //else: don't make the tree dirty as nothing changed | |
1772 | } | |
1773 | ||
1774 | wxImageList *wxGenericTreeCtrl::GetImageList() const | |
1775 | { | |
1776 | return m_imageListNormal; | |
1777 | } | |
1778 | ||
1779 | wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const | |
1780 | { | |
1781 | return m_imageListButtons; | |
1782 | } | |
1783 | ||
1784 | wxImageList *wxGenericTreeCtrl::GetStateImageList() const | |
1785 | { | |
1786 | return m_imageListState; | |
1787 | } | |
1788 | ||
1789 | void wxGenericTreeCtrl::CalculateLineHeight() | |
1790 | { | |
1791 | wxClientDC dc(this); | |
1792 | m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
1793 | ||
1794 | if ( m_imageListNormal ) | |
1795 | { | |
1796 | // Calculate a m_lineHeight value from the normal Image sizes. | |
1797 | // May be toggle off. Then wxGenericTreeCtrl will spread when | |
1798 | // necessary (which might look ugly). | |
1799 | int n = m_imageListNormal->GetImageCount(); | |
1800 | for (int i = 0; i < n ; i++) | |
1801 | { | |
1802 | int width = 0, height = 0; | |
1803 | m_imageListNormal->GetSize(i, width, height); | |
1804 | if (height > m_lineHeight) m_lineHeight = height; | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | if (m_imageListButtons) | |
1809 | { | |
1810 | // Calculate a m_lineHeight value from the Button image sizes. | |
1811 | // May be toggle off. Then wxGenericTreeCtrl will spread when | |
1812 | // necessary (which might look ugly). | |
1813 | int n = m_imageListButtons->GetImageCount(); | |
1814 | for (int i = 0; i < n ; i++) | |
1815 | { | |
1816 | int width = 0, height = 0; | |
1817 | m_imageListButtons->GetSize(i, width, height); | |
1818 | if (height > m_lineHeight) m_lineHeight = height; | |
1819 | } | |
1820 | } | |
1821 | ||
1822 | if (m_lineHeight < 30) | |
1823 | m_lineHeight += 2; // at least 2 pixels | |
1824 | else | |
1825 | m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing | |
1826 | } | |
1827 | ||
1828 | void wxGenericTreeCtrl::SetImageList(wxImageList *imageList) | |
1829 | { | |
1830 | if (m_ownsImageListNormal) delete m_imageListNormal; | |
1831 | m_imageListNormal = imageList; | |
1832 | m_ownsImageListNormal = FALSE; | |
1833 | m_dirty = TRUE; | |
1834 | CalculateLineHeight(); | |
1835 | } | |
1836 | ||
1837 | void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList) | |
1838 | { | |
1839 | if (m_ownsImageListState) delete m_imageListState; | |
1840 | m_imageListState = imageList; | |
1841 | m_ownsImageListState = FALSE; | |
1842 | } | |
1843 | ||
1844 | void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList) | |
1845 | { | |
1846 | if (m_ownsImageListButtons) delete m_imageListButtons; | |
1847 | m_imageListButtons = imageList; | |
1848 | m_ownsImageListButtons = FALSE; | |
1849 | m_dirty = TRUE; | |
1850 | CalculateLineHeight(); | |
1851 | } | |
1852 | ||
1853 | void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList) | |
1854 | { | |
1855 | SetImageList(imageList); | |
1856 | m_ownsImageListNormal = TRUE; | |
1857 | } | |
1858 | ||
1859 | void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList) | |
1860 | { | |
1861 | SetStateImageList(imageList); | |
1862 | m_ownsImageListState = TRUE; | |
1863 | } | |
1864 | ||
1865 | void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList) | |
1866 | { | |
1867 | SetButtonsImageList(imageList); | |
1868 | m_ownsImageListButtons = TRUE; | |
1869 | } | |
1870 | ||
1871 | // ----------------------------------------------------------------------------- | |
1872 | // helpers | |
1873 | // ----------------------------------------------------------------------------- | |
1874 | ||
1875 | void wxGenericTreeCtrl::AdjustMyScrollbars() | |
1876 | { | |
1877 | if (m_anchor) | |
1878 | { | |
1879 | int x = 0, y = 0; | |
1880 | m_anchor->GetSize( x, y, this ); | |
1881 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
1882 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
1883 | int x_pos = GetScrollPos( wxHORIZONTAL ); | |
1884 | int y_pos = GetScrollPos( wxVERTICAL ); | |
1885 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos ); | |
1886 | } | |
1887 | else | |
1888 | { | |
1889 | SetScrollbars( 0, 0, 0, 0 ); | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const | |
1894 | { | |
1895 | if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT) | |
1896 | return item->GetHeight(); | |
1897 | else | |
1898 | return m_lineHeight; | |
1899 | } | |
1900 | ||
1901 | void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc) | |
1902 | { | |
1903 | // TODO implement "state" icon on items | |
1904 | ||
1905 | wxTreeItemAttr *attr = item->GetAttributes(); | |
1906 | if ( attr && attr->HasFont() ) | |
1907 | dc.SetFont(attr->GetFont()); | |
1908 | else if (item->IsBold()) | |
1909 | dc.SetFont(m_boldFont); | |
1910 | ||
1911 | long text_w = 0, text_h = 0; | |
1912 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); | |
1913 | ||
1914 | int image_h = 0, image_w = 0; | |
1915 | int image = item->GetCurrentImage(); | |
1916 | if ( image != NO_IMAGE ) | |
1917 | { | |
1918 | if ( m_imageListNormal ) | |
1919 | { | |
1920 | m_imageListNormal->GetSize( image, image_w, image_h ); | |
1921 | image_w += 4; | |
1922 | } | |
1923 | else | |
1924 | { | |
1925 | image = NO_IMAGE; | |
1926 | } | |
1927 | } | |
1928 | ||
1929 | int total_h = GetLineHeight(item); | |
1930 | ||
1931 | if ( item->IsSelected() ) | |
1932 | { | |
1933 | dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush)); | |
1934 | } | |
1935 | else | |
1936 | { | |
1937 | wxColour colBg; | |
1938 | if ( attr && attr->HasBackgroundColour() ) | |
1939 | colBg = attr->GetBackgroundColour(); | |
1940 | else | |
1941 | colBg = m_backgroundColour; | |
1942 | dc.SetBrush(wxBrush(colBg, wxSOLID)); | |
1943 | } | |
1944 | ||
1945 | int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0; | |
1946 | ||
1947 | if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) ) | |
1948 | { | |
1949 | int x, y, w, h; | |
1950 | ||
1951 | DoGetPosition(&x, &y); | |
1952 | DoGetSize(&w, &h); | |
1953 | dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset); | |
1954 | } | |
1955 | else | |
1956 | { | |
1957 | if ( item->IsSelected() && image != NO_IMAGE ) | |
1958 | { | |
1959 | // If it's selected, and there's an image, then we should | |
1960 | // take care to leave the area under the image painted in the | |
1961 | // background colour. | |
1962 | dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset, | |
1963 | item->GetWidth() - image_w + 2, total_h-offset ); | |
1964 | } | |
1965 | else | |
1966 | { | |
1967 | dc.DrawRectangle( item->GetX()-2, item->GetY()+offset, | |
1968 | item->GetWidth()+2, total_h-offset ); | |
1969 | } | |
1970 | } | |
1971 | ||
1972 | if ( image != NO_IMAGE ) | |
1973 | { | |
1974 | dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h ); | |
1975 | m_imageListNormal->Draw( image, dc, | |
1976 | item->GetX(), | |
1977 | item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0), | |
1978 | wxIMAGELIST_DRAW_TRANSPARENT ); | |
1979 | dc.DestroyClippingRegion(); | |
1980 | } | |
1981 | ||
1982 | dc.SetBackgroundMode(wxTRANSPARENT); | |
1983 | int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0; | |
1984 | dc.DrawText( item->GetText(), | |
1985 | (wxCoord)(image_w + item->GetX()), | |
1986 | (wxCoord)(item->GetY() + extraH)); | |
1987 | ||
1988 | // restore normal font | |
1989 | dc.SetFont( m_normalFont ); | |
1990 | } | |
1991 | ||
1992 | // Now y stands for the top of the item, whereas it used to stand for middle ! | |
1993 | void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) | |
1994 | { | |
1995 | int x = level*m_indent; | |
1996 | if (!HasFlag(wxTR_HIDE_ROOT)) | |
1997 | { | |
1998 | x += m_indent; | |
1999 | } | |
2000 | else if (level == 0) | |
2001 | { | |
2002 | // always expand hidden root | |
2003 | int origY = y; | |
2004 | wxArrayGenericTreeItems& children = item->GetChildren(); | |
2005 | int count = children.Count(); | |
2006 | if (count > 0) | |
2007 | { | |
2008 | int n = 0, oldY; | |
2009 | do { | |
2010 | oldY = y; | |
2011 | PaintLevel(children[n], dc, 1, y); | |
2012 | } while (++n < count); | |
2013 | ||
2014 | if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0) | |
2015 | { | |
2016 | // draw line down to last child | |
2017 | origY += GetLineHeight(children[0])>>1; | |
2018 | oldY += GetLineHeight(children[n-1])>>1; | |
2019 | dc.DrawLine(3, origY, 3, oldY); | |
2020 | } | |
2021 | } | |
2022 | return; | |
2023 | } | |
2024 | ||
2025 | item->SetX(x+m_spacing); | |
2026 | item->SetY(y); | |
2027 | ||
2028 | int h = GetLineHeight(item); | |
2029 | int y_top = y; | |
2030 | int y_mid = y_top + (h>>1); | |
2031 | y += h; | |
2032 | ||
2033 | int exposed_x = dc.LogicalToDeviceX(0); | |
2034 | int exposed_y = dc.LogicalToDeviceY(y_top); | |
2035 | ||
2036 | if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much | |
2037 | { | |
2038 | wxPen *pen = | |
2039 | #ifndef __WXMAC__ | |
2040 | // don't draw rect outline if we already have the | |
2041 | // background color under Mac | |
2042 | (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN : | |
2043 | #endif // !__WXMAC__ | |
2044 | wxTRANSPARENT_PEN; | |
2045 | ||
2046 | wxColour colText; | |
2047 | if ( item->IsSelected() ) | |
2048 | { | |
2049 | colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
2050 | } | |
2051 | else | |
2052 | { | |
2053 | wxTreeItemAttr *attr = item->GetAttributes(); | |
2054 | if (attr && attr->HasTextColour()) | |
2055 | colText = attr->GetTextColour(); | |
2056 | else | |
2057 | colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT); | |
2058 | } | |
2059 | ||
2060 | // prepare to draw | |
2061 | dc.SetTextForeground(colText); | |
2062 | dc.SetPen(*pen); | |
2063 | ||
2064 | // draw | |
2065 | PaintItem(item, dc); | |
2066 | ||
2067 | if (HasFlag(wxTR_ROW_LINES)) | |
2068 | { | |
2069 | // if the background colour is white, choose a | |
2070 | // contrasting color for the lines | |
2071 | dc.SetPen(*((GetBackgroundColour() == *wxWHITE) | |
2072 | ? wxMEDIUM_GREY_PEN : wxWHITE_PEN)); | |
2073 | dc.DrawLine(0, y_top, 10000, y_top); | |
2074 | dc.DrawLine(0, y, 10000, y); | |
2075 | } | |
2076 | ||
2077 | // restore DC objects | |
2078 | dc.SetBrush(*wxWHITE_BRUSH); | |
2079 | dc.SetPen(m_dottedPen); | |
2080 | dc.SetTextForeground(*wxBLACK); | |
2081 | ||
2082 | if (item->HasPlus() && HasButtons()) // should the item show a button? | |
2083 | { | |
2084 | if (!HasFlag(wxTR_NO_LINES)) | |
2085 | { | |
2086 | if (x > (signed)m_indent) | |
2087 | dc.DrawLine(x - m_indent, y_mid, x - 5, y_mid); | |
2088 | else if (HasFlag(wxTR_LINES_AT_ROOT)) | |
2089 | dc.DrawLine(3, y_mid, x - 5, y_mid); | |
2090 | dc.DrawLine(x + 5, y_mid, x + m_spacing, y_mid); | |
2091 | } | |
2092 | ||
2093 | if (m_imageListButtons != NULL) | |
2094 | { | |
2095 | // draw the image button here | |
2096 | int image_h = 0, image_w = 0, image = wxTreeItemIcon_Normal; | |
2097 | if (item->IsExpanded()) image = wxTreeItemIcon_Expanded; | |
2098 | if (item->IsSelected()) | |
2099 | image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal; | |
2100 | m_imageListButtons->GetSize(image, image_w, image_h); | |
2101 | int xx = x - (image_w>>1); | |
2102 | int yy = y_mid - (image_h>>1); | |
2103 | dc.SetClippingRegion(xx, yy, image_w, image_h); | |
2104 | m_imageListButtons->Draw(image, dc, xx, yy, | |
2105 | wxIMAGELIST_DRAW_TRANSPARENT); | |
2106 | dc.DestroyClippingRegion(); | |
2107 | } | |
2108 | else if (HasFlag(wxTR_TWIST_BUTTONS)) | |
2109 | { | |
2110 | // draw the twisty button here | |
2111 | ||
2112 | if (HasFlag(wxTR_AQUA_BUTTONS)) | |
2113 | { | |
2114 | if (item->IsExpanded()) | |
2115 | dc.DrawBitmap( *m_arrowDown, x-5, y_mid-6, TRUE ); | |
2116 | else | |
2117 | dc.DrawBitmap( *m_arrowRight, x-5, y_mid-6, TRUE ); | |
2118 | } | |
2119 | else | |
2120 | { | |
2121 | dc.SetBrush(*m_hilightBrush); | |
2122 | dc.SetPen(*wxBLACK_PEN); | |
2123 | wxPoint button[3]; | |
2124 | ||
2125 | if (item->IsExpanded()) | |
2126 | { | |
2127 | button[0].x = x-5; | |
2128 | button[0].y = y_mid-2; | |
2129 | button[1].x = x+5; | |
2130 | button[1].y = y_mid-2; | |
2131 | button[2].x = x; | |
2132 | button[2].y = y_mid+3; | |
2133 | } | |
2134 | else | |
2135 | { | |
2136 | button[0].y = y_mid-5; | |
2137 | button[0].x = x-2; | |
2138 | button[1].y = y_mid+5; | |
2139 | button[1].x = x-2; | |
2140 | button[2].y = y_mid; | |
2141 | button[2].x = x+3; | |
2142 | } | |
2143 | dc.DrawPolygon(3, button); | |
2144 | dc.SetPen(m_dottedPen); | |
2145 | } | |
2146 | } | |
2147 | else // if (HasFlag(wxTR_HAS_BUTTONS)) | |
2148 | { | |
2149 | // draw the plus sign here | |
2150 | dc.SetPen(*wxGREY_PEN); | |
2151 | dc.SetBrush(*wxWHITE_BRUSH); | |
2152 | dc.DrawRectangle(x-5, y_mid-4, 11, 9); | |
2153 | dc.SetPen(*wxBLACK_PEN); | |
2154 | dc.DrawLine(x-2, y_mid, x+3, y_mid); | |
2155 | if (!item->IsExpanded()) | |
2156 | dc.DrawLine(x, y_mid-2, x, y_mid+3); | |
2157 | dc.SetPen(m_dottedPen); | |
2158 | } | |
2159 | } | |
2160 | else if (!HasFlag(wxTR_NO_LINES)) // no button; maybe a line? | |
2161 | { | |
2162 | // draw the horizontal line here | |
2163 | int x_start = x; | |
2164 | if (x > (signed)m_indent) | |
2165 | x_start -= m_indent; | |
2166 | else if (HasFlag(wxTR_LINES_AT_ROOT)) | |
2167 | x_start = 3; | |
2168 | dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid); | |
2169 | } | |
2170 | } | |
2171 | ||
2172 | if (item->IsExpanded()) | |
2173 | { | |
2174 | wxArrayGenericTreeItems& children = item->GetChildren(); | |
2175 | int count = children.Count(); | |
2176 | if (count > 0) | |
2177 | { | |
2178 | int n = 0, oldY; | |
2179 | ++level; | |
2180 | do { | |
2181 | oldY = y; | |
2182 | PaintLevel(children[n], dc, level, y); | |
2183 | } while (++n < count); | |
2184 | ||
2185 | if (!HasFlag(wxTR_NO_LINES) && count > 0) | |
2186 | { | |
2187 | // draw line down to last child | |
2188 | oldY += GetLineHeight(children[n-1])>>1; | |
2189 | if (HasButtons()) y_mid += 5; | |
2190 | dc.DrawLine(x, y_mid, x, oldY); | |
2191 | } | |
2192 | } | |
2193 | } | |
2194 | } | |
2195 | ||
2196 | void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item) | |
2197 | { | |
2198 | if ( item ) | |
2199 | { | |
2200 | if ( item->HasPlus() ) | |
2201 | { | |
2202 | // it's a folder, indicate it by a border | |
2203 | DrawBorder(item); | |
2204 | } | |
2205 | else | |
2206 | { | |
2207 | // draw a line under the drop target because the item will be | |
2208 | // dropped there | |
2209 | DrawLine(item, TRUE /* below */); | |
2210 | } | |
2211 | ||
2212 | SetCursor(wxCURSOR_BULLSEYE); | |
2213 | } | |
2214 | else | |
2215 | { | |
2216 | // can't drop here | |
2217 | SetCursor(wxCURSOR_NO_ENTRY); | |
2218 | } | |
2219 | } | |
2220 | ||
2221 | void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item) | |
2222 | { | |
2223 | wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") ); | |
2224 | ||
2225 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; | |
2226 | ||
2227 | wxClientDC dc(this); | |
2228 | PrepareDC( dc ); | |
2229 | dc.SetLogicalFunction(wxINVERT); | |
2230 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2231 | ||
2232 | int w = i->GetWidth() + 2; | |
2233 | int h = GetLineHeight(i) + 2; | |
2234 | ||
2235 | dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h); | |
2236 | } | |
2237 | ||
2238 | void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below) | |
2239 | { | |
2240 | wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") ); | |
2241 | ||
2242 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; | |
2243 | ||
2244 | wxClientDC dc(this); | |
2245 | PrepareDC( dc ); | |
2246 | dc.SetLogicalFunction(wxINVERT); | |
2247 | ||
2248 | int x = i->GetX(), | |
2249 | y = i->GetY(); | |
2250 | if ( below ) | |
2251 | { | |
2252 | y += GetLineHeight(i) - 1; | |
2253 | } | |
2254 | ||
2255 | dc.DrawLine( x, y, x + i->GetWidth(), y); | |
2256 | } | |
2257 | ||
2258 | // ----------------------------------------------------------------------------- | |
2259 | // wxWindows callbacks | |
2260 | // ----------------------------------------------------------------------------- | |
2261 | ||
2262 | void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
2263 | { | |
2264 | wxPaintDC dc(this); | |
2265 | PrepareDC( dc ); | |
2266 | ||
2267 | if ( !m_anchor) | |
2268 | return; | |
2269 | ||
2270 | dc.SetFont( m_normalFont ); | |
2271 | dc.SetPen( m_dottedPen ); | |
2272 | ||
2273 | // this is now done dynamically | |
2274 | //if(GetImageList() == NULL) | |
2275 | // m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
2276 | ||
2277 | int y = 2; | |
2278 | PaintLevel( m_anchor, dc, 0, y ); | |
2279 | } | |
2280 | ||
2281 | void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event ) | |
2282 | { | |
2283 | m_hasFocus = TRUE; | |
2284 | ||
2285 | RefreshSelected(); | |
2286 | ||
2287 | event.Skip(); | |
2288 | } | |
2289 | ||
2290 | void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event ) | |
2291 | { | |
2292 | m_hasFocus = FALSE; | |
2293 | ||
2294 | RefreshSelected(); | |
2295 | ||
2296 | event.Skip(); | |
2297 | } | |
2298 | ||
2299 | void wxGenericTreeCtrl::OnChar( wxKeyEvent &event ) | |
2300 | { | |
2301 | wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() ); | |
2302 | te.m_evtKey = event; | |
2303 | te.SetEventObject( this ); | |
2304 | if ( GetEventHandler()->ProcessEvent( te ) ) | |
2305 | { | |
2306 | // intercepted by the user code | |
2307 | return; | |
2308 | } | |
2309 | ||
2310 | if ( (m_current == 0) || (m_key_current == 0) ) | |
2311 | { | |
2312 | event.Skip(); | |
2313 | return; | |
2314 | } | |
2315 | ||
2316 | // how should the selection work for this event? | |
2317 | bool is_multiple, extended_select, unselect_others; | |
2318 | EventFlagsToSelType(GetWindowStyleFlag(), | |
2319 | event.ShiftDown(), | |
2320 | event.ControlDown(), | |
2321 | is_multiple, extended_select, unselect_others); | |
2322 | ||
2323 | // + : Expand | |
2324 | // - : Collaspe | |
2325 | // * : Expand all/Collapse all | |
2326 | // ' ' | return : activate | |
2327 | // up : go up (not last children!) | |
2328 | // down : go down | |
2329 | // left : go to parent | |
2330 | // right : open if parent and go next | |
2331 | // home : go to root | |
2332 | // end : go to last item without opening parents | |
2333 | switch (event.KeyCode()) | |
2334 | { | |
2335 | case '+': | |
2336 | case WXK_ADD: | |
2337 | if (m_current->HasPlus() && !IsExpanded(m_current)) | |
2338 | { | |
2339 | Expand(m_current); | |
2340 | } | |
2341 | break; | |
2342 | ||
2343 | case '*': | |
2344 | case WXK_MULTIPLY: | |
2345 | if ( !IsExpanded(m_current) ) | |
2346 | { | |
2347 | // expand all | |
2348 | ExpandAll(m_current); | |
2349 | break; | |
2350 | } | |
2351 | //else: fall through to Collapse() it | |
2352 | ||
2353 | case '-': | |
2354 | case WXK_SUBTRACT: | |
2355 | if (IsExpanded(m_current)) | |
2356 | { | |
2357 | Collapse(m_current); | |
2358 | } | |
2359 | break; | |
2360 | ||
2361 | case ' ': | |
2362 | case WXK_RETURN: | |
2363 | { | |
2364 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); | |
2365 | event.m_item = (long) m_current; | |
2366 | event.SetEventObject( this ); | |
2367 | GetEventHandler()->ProcessEvent( event ); | |
2368 | } | |
2369 | break; | |
2370 | ||
2371 | // up goes to the previous sibling or to the last | |
2372 | // of its children if it's expanded | |
2373 | case WXK_UP: | |
2374 | { | |
2375 | wxTreeItemId prev = GetPrevSibling( m_key_current ); | |
2376 | if (!prev) | |
2377 | { | |
2378 | prev = GetParent( m_key_current ); | |
2379 | if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT)) | |
2380 | { | |
2381 | break; // don't go to root if it is hidden | |
2382 | } | |
2383 | if (prev) | |
2384 | { | |
2385 | long cookie = 0; | |
2386 | wxTreeItemId current = m_key_current; | |
2387 | // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be? | |
2388 | if (current == GetFirstChild( prev, cookie )) | |
2389 | { | |
2390 | // otherwise we return to where we came from | |
2391 | SelectItem( prev, unselect_others, extended_select ); | |
2392 | m_key_current= (wxGenericTreeItem*) prev.m_pItem; | |
2393 | EnsureVisible( prev ); | |
2394 | break; | |
2395 | } | |
2396 | } | |
2397 | } | |
2398 | if (prev) | |
2399 | { | |
2400 | while ( IsExpanded(prev) && HasChildren(prev) ) | |
2401 | { | |
2402 | wxTreeItemId child = GetLastChild(prev); | |
2403 | if ( child ) | |
2404 | { | |
2405 | prev = child; | |
2406 | } | |
2407 | } | |
2408 | ||
2409 | SelectItem( prev, unselect_others, extended_select ); | |
2410 | m_key_current=(wxGenericTreeItem*) prev.m_pItem; | |
2411 | EnsureVisible( prev ); | |
2412 | } | |
2413 | } | |
2414 | break; | |
2415 | ||
2416 | // left arrow goes to the parent | |
2417 | case WXK_LEFT: | |
2418 | { | |
2419 | wxTreeItemId prev = GetParent( m_current ); | |
2420 | if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT)) | |
2421 | { | |
2422 | // don't go to root if it is hidden | |
2423 | prev = GetPrevSibling( m_current ); | |
2424 | } | |
2425 | if (prev) | |
2426 | { | |
2427 | EnsureVisible( prev ); | |
2428 | SelectItem( prev, unselect_others, extended_select ); | |
2429 | } | |
2430 | } | |
2431 | break; | |
2432 | ||
2433 | case WXK_RIGHT: | |
2434 | // this works the same as the down arrow except that we | |
2435 | // also expand the item if it wasn't expanded yet | |
2436 | Expand(m_current); | |
2437 | // fall through | |
2438 | ||
2439 | case WXK_DOWN: | |
2440 | { | |
2441 | if (IsExpanded(m_key_current) && HasChildren(m_key_current)) | |
2442 | { | |
2443 | long cookie = 0; | |
2444 | wxTreeItemId child = GetFirstChild( m_key_current, cookie ); | |
2445 | SelectItem( child, unselect_others, extended_select ); | |
2446 | m_key_current=(wxGenericTreeItem*) child.m_pItem; | |
2447 | EnsureVisible( child ); | |
2448 | } | |
2449 | else | |
2450 | { | |
2451 | wxTreeItemId next = GetNextSibling( m_key_current ); | |
2452 | if (!next) | |
2453 | { | |
2454 | wxTreeItemId current = m_key_current; | |
2455 | while (current && !next) | |
2456 | { | |
2457 | current = GetParent( current ); | |
2458 | if (current) next = GetNextSibling( current ); | |
2459 | } | |
2460 | } | |
2461 | if (next) | |
2462 | { | |
2463 | SelectItem( next, unselect_others, extended_select ); | |
2464 | m_key_current=(wxGenericTreeItem*) next.m_pItem; | |
2465 | EnsureVisible( next ); | |
2466 | } | |
2467 | } | |
2468 | } | |
2469 | break; | |
2470 | ||
2471 | // <End> selects the last visible tree item | |
2472 | case WXK_END: | |
2473 | { | |
2474 | wxTreeItemId last = GetRootItem(); | |
2475 | ||
2476 | while ( last.IsOk() && IsExpanded(last) ) | |
2477 | { | |
2478 | wxTreeItemId lastChild = GetLastChild(last); | |
2479 | ||
2480 | // it may happen if the item was expanded but then all of | |
2481 | // its children have been deleted - so IsExpanded() returned | |
2482 | // TRUE, but GetLastChild() returned invalid item | |
2483 | if ( !lastChild ) | |
2484 | break; | |
2485 | ||
2486 | last = lastChild; | |
2487 | } | |
2488 | ||
2489 | if ( last.IsOk() ) | |
2490 | { | |
2491 | EnsureVisible( last ); | |
2492 | SelectItem( last, unselect_others, extended_select ); | |
2493 | } | |
2494 | } | |
2495 | break; | |
2496 | ||
2497 | // <Home> selects the root item | |
2498 | case WXK_HOME: | |
2499 | { | |
2500 | wxTreeItemId prev = GetRootItem(); | |
2501 | if (!prev) break; | |
2502 | if (HasFlag(wxTR_HIDE_ROOT)) | |
2503 | { | |
2504 | long dummy; | |
2505 | prev = GetFirstChild(prev, dummy); | |
2506 | if (!prev) break; | |
2507 | } | |
2508 | EnsureVisible( prev ); | |
2509 | SelectItem( prev, unselect_others, extended_select ); | |
2510 | } | |
2511 | break; | |
2512 | ||
2513 | default: | |
2514 | event.Skip(); | |
2515 | } | |
2516 | } | |
2517 | ||
2518 | wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags) | |
2519 | { | |
2520 | // JACS: removed wxYieldIfNeeded() because it can cause the window | |
2521 | // to be deleted from under us if a close window event is pending | |
2522 | ||
2523 | int w, h; | |
2524 | GetSize(&w, &h); | |
2525 | flags=0; | |
2526 | if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT; | |
2527 | if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT; | |
2528 | if (point.y<0) flags |= wxTREE_HITTEST_ABOVE; | |
2529 | if (point.y>h) flags |= wxTREE_HITTEST_BELOW; | |
2530 | if (flags) return wxTreeItemId(); | |
2531 | ||
2532 | if (m_anchor == NULL) | |
2533 | { | |
2534 | flags = wxTREE_HITTEST_NOWHERE; | |
2535 | return wxTreeItemId(); | |
2536 | } | |
2537 | ||
2538 | wxClientDC dc(this); | |
2539 | PrepareDC(dc); | |
2540 | wxCoord x = dc.DeviceToLogicalX( point.x ); | |
2541 | wxCoord y = dc.DeviceToLogicalY( point.y ); | |
2542 | wxGenericTreeItem *hit = m_anchor->HitTest(wxPoint(x, y), | |
2543 | this, | |
2544 | flags, | |
2545 | 0 ); | |
2546 | if (hit == NULL) | |
2547 | { | |
2548 | flags = wxTREE_HITTEST_NOWHERE; | |
2549 | return wxTreeItemId(); | |
2550 | } | |
2551 | return hit; | |
2552 | } | |
2553 | ||
2554 | // get the bounding rectangle of the item (or of its label only) | |
2555 | bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item, | |
2556 | wxRect& rect, | |
2557 | bool WXUNUSED(textOnly)) const | |
2558 | { | |
2559 | wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") ); | |
2560 | ||
2561 | wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem; | |
2562 | ||
2563 | int startX, startY; | |
2564 | GetViewStart(& startX, & startY); | |
2565 | ||
2566 | rect.x = i->GetX() - startX*PIXELS_PER_UNIT; | |
2567 | rect.y = i->GetY() - startY*PIXELS_PER_UNIT; | |
2568 | rect.width = i->GetWidth(); | |
2569 | //rect.height = i->GetHeight(); | |
2570 | rect.height = GetLineHeight(i); | |
2571 | ||
2572 | return TRUE; | |
2573 | } | |
2574 | ||
2575 | /* **** */ | |
2576 | ||
2577 | void wxGenericTreeCtrl::Edit( const wxTreeItemId& item ) | |
2578 | { | |
2579 | if (!item.IsOk()) return; | |
2580 | ||
2581 | m_currentEdit = (wxGenericTreeItem*) item.m_pItem; | |
2582 | ||
2583 | wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() ); | |
2584 | te.m_item = (long) m_currentEdit; | |
2585 | te.SetEventObject( this ); | |
2586 | GetEventHandler()->ProcessEvent( te ); | |
2587 | ||
2588 | if (!te.IsAllowed()) return; | |
2589 | ||
2590 | // We have to call this here because the label in | |
2591 | // question might just have been added and no screen | |
2592 | // update taken place. | |
2593 | if (m_dirty) wxYieldIfNeeded(); | |
2594 | ||
2595 | wxString s = m_currentEdit->GetText(); | |
2596 | int x = m_currentEdit->GetX(); | |
2597 | int y = m_currentEdit->GetY(); | |
2598 | int w = m_currentEdit->GetWidth(); | |
2599 | int h = m_currentEdit->GetHeight(); | |
2600 | ||
2601 | int image_h = 0; | |
2602 | int image_w = 0; | |
2603 | ||
2604 | int image = m_currentEdit->GetCurrentImage(); | |
2605 | if ( image != NO_IMAGE ) | |
2606 | { | |
2607 | if ( m_imageListNormal ) | |
2608 | { | |
2609 | m_imageListNormal->GetSize( image, image_w, image_h ); | |
2610 | image_w += 4; | |
2611 | } | |
2612 | else | |
2613 | { | |
2614 | wxFAIL_MSG(_T("you must create an image list to use images!")); | |
2615 | } | |
2616 | } | |
2617 | x += image_w; | |
2618 | w -= image_w + 4; // I don't know why +4 is needed | |
2619 | ||
2620 | wxClientDC dc(this); | |
2621 | PrepareDC( dc ); | |
2622 | x = dc.LogicalToDeviceX( x ); | |
2623 | y = dc.LogicalToDeviceY( y ); | |
2624 | ||
2625 | wxTreeTextCtrl *text = new wxTreeTextCtrl(this, -1, | |
2626 | &m_renameAccept, | |
2627 | &m_renameRes, | |
2628 | this, | |
2629 | s, | |
2630 | wxPoint(x-4,y-4), | |
2631 | wxSize(w+11,h+8)); | |
2632 | text->SetFocus(); | |
2633 | } | |
2634 | ||
2635 | void wxGenericTreeCtrl::OnRenameTimer() | |
2636 | { | |
2637 | Edit( m_current ); | |
2638 | } | |
2639 | ||
2640 | void wxGenericTreeCtrl::OnRenameAccept() | |
2641 | { | |
2642 | // TODO if the validator fails this causes a crash | |
2643 | wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() ); | |
2644 | le.m_item = (long) m_currentEdit; | |
2645 | le.SetEventObject( this ); | |
2646 | le.m_label = m_renameRes; | |
2647 | GetEventHandler()->ProcessEvent( le ); | |
2648 | ||
2649 | if (!le.IsAllowed()) return; | |
2650 | ||
2651 | SetItemText( m_currentEdit, m_renameRes ); | |
2652 | } | |
2653 | ||
2654 | void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event ) | |
2655 | { | |
2656 | if ( !m_anchor ) return; | |
2657 | ||
2658 | // we process left mouse up event (enables in-place edit), right down | |
2659 | // (pass to the user code), left dbl click (activate item) and | |
2660 | // dragging/moving events for items drag-and-drop | |
2661 | if ( !(event.LeftDown() || | |
2662 | event.LeftUp() || | |
2663 | event.RightDown() || | |
2664 | event.LeftDClick() || | |
2665 | event.Dragging() || | |
2666 | ((event.Moving() || event.RightUp()) && m_isDragging)) ) | |
2667 | { | |
2668 | event.Skip(); | |
2669 | ||
2670 | return; | |
2671 | } | |
2672 | ||
2673 | wxClientDC dc(this); | |
2674 | PrepareDC(dc); | |
2675 | wxCoord x = dc.DeviceToLogicalX( event.GetX() ); | |
2676 | wxCoord y = dc.DeviceToLogicalY( event.GetY() ); | |
2677 | ||
2678 | int flags = 0; | |
2679 | wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), | |
2680 | this, | |
2681 | flags, | |
2682 | 0 ); | |
2683 | ||
2684 | if ( event.Dragging() && !m_isDragging ) | |
2685 | { | |
2686 | if (m_dragCount == 0) | |
2687 | m_dragStart = wxPoint(x,y); | |
2688 | ||
2689 | m_dragCount++; | |
2690 | ||
2691 | if (m_dragCount != 3) | |
2692 | { | |
2693 | // wait until user drags a bit further... | |
2694 | return; | |
2695 | } | |
2696 | ||
2697 | wxEventType command = event.RightIsDown() | |
2698 | ? wxEVT_COMMAND_TREE_BEGIN_RDRAG | |
2699 | : wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
2700 | ||
2701 | wxTreeEvent nevent( command, GetId() ); | |
2702 | nevent.m_item = (long) m_current; | |
2703 | nevent.SetEventObject(this); | |
2704 | ||
2705 | // by default the dragging is not supported, the user code must | |
2706 | // explicitly allow the event for it to take place | |
2707 | nevent.Veto(); | |
2708 | ||
2709 | if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() ) | |
2710 | { | |
2711 | // we're going to drag this item | |
2712 | m_isDragging = TRUE; | |
2713 | ||
2714 | // remember the old cursor because we will change it while | |
2715 | // dragging | |
2716 | m_oldCursor = m_cursor; | |
2717 | ||
2718 | // in a single selection control, hide the selection temporarily | |
2719 | if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) ) | |
2720 | { | |
2721 | m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem; | |
2722 | ||
2723 | if ( m_oldSelection ) | |
2724 | { | |
2725 | m_oldSelection->SetHilight(FALSE); | |
2726 | RefreshLine(m_oldSelection); | |
2727 | } | |
2728 | } | |
2729 | ||
2730 | CaptureMouse(); | |
2731 | } | |
2732 | } | |
2733 | else if ( event.Moving() ) | |
2734 | { | |
2735 | if ( item != m_dropTarget ) | |
2736 | { | |
2737 | // unhighlight the previous drop target | |
2738 | DrawDropEffect(m_dropTarget); | |
2739 | ||
2740 | m_dropTarget = item; | |
2741 | ||
2742 | // highlight the current drop target if any | |
2743 | DrawDropEffect(m_dropTarget); | |
2744 | ||
2745 | wxYieldIfNeeded(); | |
2746 | } | |
2747 | } | |
2748 | else if ( (event.LeftUp() || event.RightUp()) && m_isDragging ) | |
2749 | { | |
2750 | // erase the highlighting | |
2751 | DrawDropEffect(m_dropTarget); | |
2752 | ||
2753 | if ( m_oldSelection ) | |
2754 | { | |
2755 | m_oldSelection->SetHilight(TRUE); | |
2756 | RefreshLine(m_oldSelection); | |
2757 | m_oldSelection = (wxGenericTreeItem *)NULL; | |
2758 | } | |
2759 | ||
2760 | // generate the drag end event | |
2761 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId()); | |
2762 | ||
2763 | event.m_item = (long) item; | |
2764 | event.m_pointDrag = wxPoint(x, y); | |
2765 | event.SetEventObject(this); | |
2766 | ||
2767 | (void)GetEventHandler()->ProcessEvent(event); | |
2768 | ||
2769 | m_isDragging = FALSE; | |
2770 | m_dropTarget = (wxGenericTreeItem *)NULL; | |
2771 | ||
2772 | ReleaseMouse(); | |
2773 | ||
2774 | SetCursor(m_oldCursor); | |
2775 | ||
2776 | wxYieldIfNeeded(); | |
2777 | } | |
2778 | else | |
2779 | { | |
2780 | // here we process only the messages which happen on tree items | |
2781 | ||
2782 | m_dragCount = 0; | |
2783 | ||
2784 | if (item == NULL) return; /* we hit the blank area */ | |
2785 | ||
2786 | if ( event.RightDown() ) | |
2787 | { | |
2788 | wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId()); | |
2789 | nevent.m_item = (long) item; | |
2790 | CalcScrolledPosition(x, y, | |
2791 | &nevent.m_pointDrag.x, | |
2792 | &nevent.m_pointDrag.y); | |
2793 | nevent.SetEventObject(this); | |
2794 | GetEventHandler()->ProcessEvent(nevent); | |
2795 | } | |
2796 | else if ( event.LeftUp() ) | |
2797 | { | |
2798 | if ( m_lastOnSame ) | |
2799 | { | |
2800 | if ( (item == m_current) && | |
2801 | (flags & wxTREE_HITTEST_ONITEMLABEL) && | |
2802 | HasFlag(wxTR_EDIT_LABELS) ) | |
2803 | { | |
2804 | if ( m_renameTimer->IsRunning() ) | |
2805 | m_renameTimer->Stop(); | |
2806 | ||
2807 | m_renameTimer->Start( 100, TRUE ); | |
2808 | } | |
2809 | ||
2810 | m_lastOnSame = FALSE; | |
2811 | } | |
2812 | } | |
2813 | else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick() | |
2814 | { | |
2815 | if ( event.LeftDown() ) | |
2816 | { | |
2817 | m_lastOnSame = item == m_current; | |
2818 | } | |
2819 | ||
2820 | if ( flags & wxTREE_HITTEST_ONITEMBUTTON ) | |
2821 | { | |
2822 | // only toggle the item for a single click, double click on | |
2823 | // the button doesn't do anything (it toggles the item twice) | |
2824 | if ( event.LeftDown() ) | |
2825 | { | |
2826 | Toggle( item ); | |
2827 | } | |
2828 | ||
2829 | // don't select the item if the button was clicked | |
2830 | return; | |
2831 | } | |
2832 | ||
2833 | // how should the selection work for this event? | |
2834 | bool is_multiple, extended_select, unselect_others; | |
2835 | EventFlagsToSelType(GetWindowStyleFlag(), | |
2836 | event.ShiftDown(), | |
2837 | event.ControlDown(), | |
2838 | is_multiple, extended_select, unselect_others); | |
2839 | ||
2840 | SelectItem(item, unselect_others, extended_select); | |
2841 | ||
2842 | // For some reason, Windows isn't recognizing a left double-click, | |
2843 | // so we need to simulate it here. Allow 200 milliseconds for now. | |
2844 | if ( event.LeftDClick() ) | |
2845 | { | |
2846 | // double clicking should not start editing the item label | |
2847 | m_renameTimer->Stop(); | |
2848 | m_lastOnSame = FALSE; | |
2849 | ||
2850 | // send activate event first | |
2851 | wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); | |
2852 | nevent.m_item = (long) item; | |
2853 | CalcScrolledPosition(x, y, | |
2854 | &nevent.m_pointDrag.x, | |
2855 | &nevent.m_pointDrag.y); | |
2856 | nevent.SetEventObject( this ); | |
2857 | if ( !GetEventHandler()->ProcessEvent( nevent ) ) | |
2858 | { | |
2859 | // if the user code didn't process the activate event, | |
2860 | // handle it ourselves by toggling the item when it is | |
2861 | // double clicked | |
2862 | if ( item->HasPlus() ) | |
2863 | { | |
2864 | Toggle(item); | |
2865 | } | |
2866 | } | |
2867 | } | |
2868 | } | |
2869 | } | |
2870 | } | |
2871 | ||
2872 | void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) ) | |
2873 | { | |
2874 | /* after all changes have been done to the tree control, | |
2875 | * we actually redraw the tree when everything is over */ | |
2876 | ||
2877 | if (!m_dirty) return; | |
2878 | ||
2879 | m_dirty = FALSE; | |
2880 | ||
2881 | CalculatePositions(); | |
2882 | Refresh(); | |
2883 | AdjustMyScrollbars(); | |
2884 | } | |
2885 | ||
2886 | void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc ) | |
2887 | { | |
2888 | wxCoord text_w = 0; | |
2889 | wxCoord text_h = 0; | |
2890 | ||
2891 | if (item->IsBold()) | |
2892 | dc.SetFont(m_boldFont); | |
2893 | ||
2894 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); | |
2895 | text_h+=2; | |
2896 | ||
2897 | // restore normal font | |
2898 | dc.SetFont( m_normalFont ); | |
2899 | ||
2900 | int image_h = 0; | |
2901 | int image_w = 0; | |
2902 | int image = item->GetCurrentImage(); | |
2903 | if ( image != NO_IMAGE ) | |
2904 | { | |
2905 | if ( m_imageListNormal ) | |
2906 | { | |
2907 | m_imageListNormal->GetSize( image, image_w, image_h ); | |
2908 | image_w += 4; | |
2909 | } | |
2910 | } | |
2911 | ||
2912 | int total_h = (image_h > text_h) ? image_h : text_h; | |
2913 | ||
2914 | if (total_h < 30) | |
2915 | total_h += 2; // at least 2 pixels | |
2916 | else | |
2917 | total_h += total_h/10; // otherwise 10% extra spacing | |
2918 | ||
2919 | item->SetHeight(total_h); | |
2920 | if (total_h>m_lineHeight) | |
2921 | m_lineHeight=total_h; | |
2922 | ||
2923 | item->SetWidth(image_w+text_w+2); | |
2924 | } | |
2925 | ||
2926 | // ----------------------------------------------------------------------------- | |
2927 | // for developper : y is now the top of the level | |
2928 | // not the middle of it ! | |
2929 | void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) | |
2930 | { | |
2931 | int x = level*m_indent; | |
2932 | if (!HasFlag(wxTR_HIDE_ROOT)) | |
2933 | { | |
2934 | x += m_indent; | |
2935 | } | |
2936 | else if (level == 0) | |
2937 | { | |
2938 | // a hidden root is not evaluated, but its | |
2939 | // children are always calculated | |
2940 | goto Recurse; | |
2941 | } | |
2942 | ||
2943 | CalculateSize( item, dc ); | |
2944 | ||
2945 | // set its position | |
2946 | item->SetX( x+m_spacing ); | |
2947 | item->SetY( y ); | |
2948 | y += GetLineHeight(item); | |
2949 | ||
2950 | if ( !item->IsExpanded() ) | |
2951 | { | |
2952 | // we don't need to calculate collapsed branches | |
2953 | return; | |
2954 | } | |
2955 | ||
2956 | Recurse: | |
2957 | wxArrayGenericTreeItems& children = item->GetChildren(); | |
2958 | size_t n, count = children.Count(); | |
2959 | ++level; | |
2960 | for (n = 0; n < count; ++n ) | |
2961 | CalculateLevel( children[n], dc, level, y ); // recurse | |
2962 | } | |
2963 | ||
2964 | void wxGenericTreeCtrl::CalculatePositions() | |
2965 | { | |
2966 | if ( !m_anchor ) return; | |
2967 | ||
2968 | wxClientDC dc(this); | |
2969 | PrepareDC( dc ); | |
2970 | ||
2971 | dc.SetFont( m_normalFont ); | |
2972 | ||
2973 | dc.SetPen( m_dottedPen ); | |
2974 | //if(GetImageList() == NULL) | |
2975 | // m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
2976 | ||
2977 | int y = 2; | |
2978 | CalculateLevel( m_anchor, dc, 0, y ); // start recursion | |
2979 | } | |
2980 | ||
2981 | void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item) | |
2982 | { | |
2983 | if (m_dirty) return; | |
2984 | ||
2985 | wxClientDC dc(this); | |
2986 | PrepareDC(dc); | |
2987 | ||
2988 | int cw = 0; | |
2989 | int ch = 0; | |
2990 | GetClientSize( &cw, &ch ); | |
2991 | ||
2992 | wxRect rect; | |
2993 | rect.x = dc.LogicalToDeviceX( 0 ); | |
2994 | rect.width = cw; | |
2995 | rect.y = dc.LogicalToDeviceY( item->GetY() ); | |
2996 | rect.height = ch; | |
2997 | ||
2998 | Refresh( TRUE, &rect ); | |
2999 | ||
3000 | AdjustMyScrollbars(); | |
3001 | } | |
3002 | ||
3003 | void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item ) | |
3004 | { | |
3005 | if (m_dirty) return; | |
3006 | ||
3007 | wxClientDC dc(this); | |
3008 | PrepareDC( dc ); | |
3009 | ||
3010 | int cw = 0; | |
3011 | int ch = 0; | |
3012 | GetClientSize( &cw, &ch ); | |
3013 | ||
3014 | wxRect rect; | |
3015 | rect.x = dc.LogicalToDeviceX( 0 ); | |
3016 | rect.y = dc.LogicalToDeviceY( item->GetY() ); | |
3017 | rect.width = cw; | |
3018 | rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6; | |
3019 | ||
3020 | Refresh( TRUE, &rect ); | |
3021 | } | |
3022 | ||
3023 | void wxGenericTreeCtrl::RefreshSelected() | |
3024 | { | |
3025 | // TODO: this is awfully inefficient, we should keep the list of all | |
3026 | // selected items internally, should be much faster | |
3027 | if ( m_anchor ) | |
3028 | RefreshSelectedUnder(m_anchor); | |
3029 | } | |
3030 | ||
3031 | void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item) | |
3032 | { | |
3033 | if ( item->IsSelected() ) | |
3034 | RefreshLine(item); | |
3035 | ||
3036 | const wxArrayGenericTreeItems& children = item->GetChildren(); | |
3037 | size_t count = children.GetCount(); | |
3038 | for ( size_t n = 0; n < count; n++ ) | |
3039 | { | |
3040 | RefreshSelectedUnder(children[n]); | |
3041 | } | |
3042 | } | |
3043 | ||
3044 | // ---------------------------------------------------------------------------- | |
3045 | // changing colours: we need to refresh the tree control | |
3046 | // ---------------------------------------------------------------------------- | |
3047 | ||
3048 | bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour) | |
3049 | { | |
3050 | if ( !wxWindow::SetBackgroundColour(colour) ) | |
3051 | return FALSE; | |
3052 | ||
3053 | Refresh(); | |
3054 | ||
3055 | return TRUE; | |
3056 | } | |
3057 | ||
3058 | bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour) | |
3059 | { | |
3060 | if ( !wxWindow::SetForegroundColour(colour) ) | |
3061 | return FALSE; | |
3062 | ||
3063 | Refresh(); | |
3064 | ||
3065 | return TRUE; | |
3066 | } | |
3067 | ||
3068 | #endif // wxUSE_TREECTRL |