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