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