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