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