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