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