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