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