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