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