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