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