]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: treectrl.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$ |
c801d85f | 8 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem |
29d87bba | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
f135ff73 VZ |
12 | // ============================================================================= |
13 | // declarations | |
14 | // ============================================================================= | |
15 | ||
16 | // ----------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ----------------------------------------------------------------------------- | |
19 | ||
c801d85f | 20 | #ifdef __GNUG__ |
f135ff73 | 21 | #pragma implementation "treectrl.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__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
f98e2558 | 31 | #include "wx/treectrl.h" |
f60d0f94 | 32 | #include "wx/generic/imaglist.h" |
c801d85f | 33 | #include "wx/settings.h" |
389cdc7a | 34 | #include "wx/log.h" |
f135ff73 VZ |
35 | #include "wx/intl.h" |
36 | #include "wx/dynarray.h" | |
91b8de8d | 37 | #include "wx/arrimpl.cpp" |
f135ff73 | 38 | #include "wx/dcclient.h" |
0659e7ee | 39 | #include "wx/msgdlg.h" |
c801d85f | 40 | |
f135ff73 VZ |
41 | // ----------------------------------------------------------------------------- |
42 | // array types | |
43 | // ----------------------------------------------------------------------------- | |
c801d85f | 44 | |
1e6d9499 JS |
45 | class WXDLLEXPORT wxGenericTreeItem; |
46 | ||
91b8de8d RR |
47 | WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems); |
48 | WX_DEFINE_OBJARRAY(wxArrayTreeItemIds); | |
c801d85f | 49 | |
8dc99046 VZ |
50 | // ---------------------------------------------------------------------------- |
51 | // constants | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | static const int NO_IMAGE = -1; | |
55 | ||
3dbeaa52 VZ |
56 | #define PIXELS_PER_UNIT 10 |
57 | ||
f135ff73 VZ |
58 | // ----------------------------------------------------------------------------- |
59 | // private classes | |
60 | // ----------------------------------------------------------------------------- | |
61 | ||
3dbeaa52 VZ |
62 | // timer used for enabling in-place edit |
63 | class WXDLLEXPORT wxTreeRenameTimer: public wxTimer | |
64 | { | |
65 | public: | |
66 | wxTreeRenameTimer( wxTreeCtrl *owner ); | |
67 | ||
68 | void Notify(); | |
69 | ||
70 | private: | |
71 | wxTreeCtrl *m_owner; | |
72 | }; | |
73 | ||
74 | // control used for in-place edit | |
75 | class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl | |
76 | { | |
77 | public: | |
78 | wxTreeTextCtrl() { } | |
79 | wxTreeTextCtrl( wxWindow *parent, | |
80 | const wxWindowID id, | |
81 | bool *accept, | |
82 | wxString *res, | |
83 | wxTreeCtrl *owner, | |
84 | const wxString &value = wxEmptyString, | |
85 | const wxPoint &pos = wxDefaultPosition, | |
86 | const wxSize &size = wxDefaultSize, | |
87 | int style = 0, | |
88 | const wxValidator& validator = wxDefaultValidator, | |
89 | const wxString &name = wxTextCtrlNameStr ); | |
90 | ||
91 | void OnChar( wxKeyEvent &event ); | |
92 | void OnKillFocus( wxFocusEvent &event ); | |
93 | ||
94 | private: | |
95 | bool *m_accept; | |
96 | wxString *m_res; | |
97 | wxTreeCtrl *m_owner; | |
98 | wxString m_startValue; | |
99 | ||
100 | DECLARE_EVENT_TABLE() | |
101 | DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl); | |
102 | }; | |
103 | ||
f135ff73 VZ |
104 | // a tree item |
105 | class WXDLLEXPORT wxGenericTreeItem | |
c801d85f | 106 | { |
f135ff73 | 107 | public: |
9ec64fa7 VZ |
108 | // ctors & dtor |
109 | wxGenericTreeItem() { m_data = NULL; } | |
110 | wxGenericTreeItem( wxGenericTreeItem *parent, | |
111 | const wxString& text, | |
112 | wxDC& dc, | |
113 | int image, int selImage, | |
114 | wxTreeItemData *data ); | |
f135ff73 | 115 | |
9ec64fa7 | 116 | ~wxGenericTreeItem(); |
f135ff73 | 117 | |
9ec64fa7 VZ |
118 | // trivial accessors |
119 | wxArrayGenericTreeItems& GetChildren() { return m_children; } | |
f135ff73 | 120 | |
9ec64fa7 VZ |
121 | const wxString& GetText() const { return m_text; } |
122 | int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const | |
3dbeaa52 | 123 | { return m_images[which]; } |
9ec64fa7 | 124 | wxTreeItemData *GetData() const { return m_data; } |
f135ff73 | 125 | |
9ec64fa7 VZ |
126 | // returns the current image for the item (depending on its |
127 | // selected/expanded/whatever state) | |
128 | int GetCurrentImage() const; | |
8dc99046 | 129 | |
9ec64fa7 VZ |
130 | void SetText( const wxString &text ); |
131 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } | |
132 | void SetData(wxTreeItemData *data) { m_data = data; } | |
f135ff73 | 133 | |
9ec64fa7 | 134 | void SetHasPlus(bool has = TRUE) { m_hasPlus = has; } |
f135ff73 | 135 | |
9ec64fa7 | 136 | void SetBold(bool bold) { m_isBold = bold; } |
ef44a621 | 137 | |
9ec64fa7 VZ |
138 | int GetX() const { return m_x; } |
139 | int GetY() const { return m_y; } | |
f135ff73 | 140 | |
9ec64fa7 VZ |
141 | void SetX(int x) { m_x = x; } |
142 | void SetY(int y) { m_y = y; } | |
f135ff73 | 143 | |
9ec64fa7 VZ |
144 | int GetHeight() const { return m_height; } |
145 | int GetWidth() const { return m_width; } | |
91b8de8d | 146 | |
9ec64fa7 VZ |
147 | void SetHeight(int h) { m_height = h; } |
148 | void SetWidth(int w) { m_width = w; } | |
91b8de8d RR |
149 | |
150 | ||
9ec64fa7 | 151 | wxGenericTreeItem *GetParent() const { return m_parent; } |
c801d85f | 152 | |
9ec64fa7 VZ |
153 | // operations |
154 | // deletes all children notifying the treectrl about it if !NULL | |
155 | // pointer given | |
156 | void DeleteChildren(wxTreeCtrl *tree = NULL); | |
157 | // FIXME don't know what is it for | |
158 | void Reset(); | |
f135ff73 | 159 | |
9ec64fa7 VZ |
160 | // get count of all children (and grand children if 'recursively') |
161 | size_t GetChildrenCount(bool recursively = TRUE) const; | |
f135ff73 | 162 | |
9ec64fa7 | 163 | void Insert(wxGenericTreeItem *child, size_t index) |
f135ff73 VZ |
164 | { m_children.Insert(child, index); } |
165 | ||
9ec64fa7 VZ |
166 | void SetCross( int x, int y ); |
167 | void GetSize( int &x, int &y, const wxTreeCtrl* ); | |
f135ff73 | 168 | |
9ec64fa7 VZ |
169 | // return the item at given position (or NULL if no item), onButton is |
170 | // TRUE if the point belongs to the item's button, otherwise it lies | |
171 | // on the button's label | |
172 | wxGenericTreeItem *HitTest( const wxPoint& point, const wxTreeCtrl *, int &flags); | |
f135ff73 | 173 | |
9ec64fa7 VZ |
174 | void Expand() { m_isCollapsed = FALSE; } |
175 | void Collapse() { m_isCollapsed = TRUE; } | |
f135ff73 | 176 | |
9ec64fa7 | 177 | void SetHilight( bool set = TRUE ) { m_hasHilight = set; } |
f135ff73 | 178 | |
9ec64fa7 VZ |
179 | // status inquiries |
180 | bool HasChildren() const { return !m_children.IsEmpty(); } | |
181 | bool IsSelected() const { return m_hasHilight; } | |
182 | bool IsExpanded() const { return !m_isCollapsed; } | |
183 | bool HasPlus() const { return m_hasPlus || HasChildren(); } | |
184 | bool IsBold() const { return m_isBold; } | |
185 | ||
186 | // attributes | |
187 | // get them - may be NULL | |
188 | wxTreeItemAttr *GetAttributes() const { return m_attr; } | |
189 | // get them ensuring that the pointer is not NULL | |
190 | wxTreeItemAttr& Attr() | |
191 | { | |
192 | if ( !m_attr ) | |
193 | m_attr = new wxTreeItemAttr; | |
194 | ||
195 | return *m_attr; | |
196 | } | |
f135ff73 VZ |
197 | |
198 | private: | |
9ec64fa7 VZ |
199 | wxString m_text; |
200 | ||
201 | // tree ctrl images for the normal, selected, expanded and | |
202 | // expanded+selected states | |
203 | int m_images[wxTreeItemIcon_Max]; | |
204 | ||
205 | wxTreeItemData *m_data; | |
206 | ||
207 | // use bitfields to save size | |
208 | int m_isCollapsed :1; | |
209 | int m_hasHilight :1; // same as focused | |
210 | int m_hasPlus :1; // used for item which doesn't have | |
211 | // children but has a [+] button | |
212 | int m_isBold :1; // render the label in bold font | |
213 | ||
6f2a55e3 VZ |
214 | wxCoord m_x, m_y; |
215 | wxCoord m_height, m_width; | |
9ec64fa7 VZ |
216 | int m_xCross, m_yCross; |
217 | int m_level; | |
218 | ||
219 | wxArrayGenericTreeItems m_children; | |
220 | wxGenericTreeItem *m_parent; | |
221 | ||
222 | wxTreeItemAttr *m_attr; | |
f135ff73 VZ |
223 | }; |
224 | ||
225 | // ============================================================================= | |
226 | // implementation | |
227 | // ============================================================================= | |
228 | ||
06b466c7 VZ |
229 | // ---------------------------------------------------------------------------- |
230 | // private functions | |
231 | // ---------------------------------------------------------------------------- | |
232 | ||
233 | // translate the key or mouse event flags to the type of selection we're | |
234 | // dealing with | |
235 | static void EventFlagsToSelType(long style, | |
236 | bool shiftDown, | |
237 | bool ctrlDown, | |
dfc6cd93 SB |
238 | bool &is_multiple, |
239 | bool &extended_select, | |
240 | bool &unselect_others) | |
06b466c7 | 241 | { |
dfc6cd93 SB |
242 | is_multiple = (style & wxTR_MULTIPLE) != 0; |
243 | extended_select = shiftDown && is_multiple; | |
244 | unselect_others = !(extended_select || (ctrlDown && is_multiple)); | |
06b466c7 | 245 | } |
e179bd65 RR |
246 | |
247 | // ----------------------------------------------------------------------------- | |
248 | // wxTreeRenameTimer (internal) | |
249 | // ----------------------------------------------------------------------------- | |
250 | ||
251 | wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl *owner ) | |
252 | { | |
253 | m_owner = owner; | |
254 | } | |
255 | ||
256 | void wxTreeRenameTimer::Notify() | |
257 | { | |
258 | m_owner->OnRenameTimer(); | |
259 | } | |
260 | ||
261 | //----------------------------------------------------------------------------- | |
262 | // wxTreeTextCtrl (internal) | |
263 | //----------------------------------------------------------------------------- | |
264 | ||
265 | IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl,wxTextCtrl); | |
266 | ||
267 | BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl) | |
268 | EVT_CHAR (wxTreeTextCtrl::OnChar) | |
269 | EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus) | |
270 | END_EVENT_TABLE() | |
271 | ||
674ac8b9 VZ |
272 | wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent, |
273 | const wxWindowID id, | |
274 | bool *accept, | |
275 | wxString *res, | |
276 | wxTreeCtrl *owner, | |
277 | const wxString &value, | |
278 | const wxPoint &pos, | |
279 | const wxSize &size, | |
280 | int style, | |
281 | const wxValidator& validator, | |
282 | const wxString &name ) | |
283 | : wxTextCtrl( parent, id, value, pos, size, style, validator, name ) | |
e179bd65 RR |
284 | { |
285 | m_res = res; | |
286 | m_accept = accept; | |
287 | m_owner = owner; | |
5f1ea0ee | 288 | (*m_accept) = FALSE; |
3dbeaa52 | 289 | (*m_res) = wxEmptyString; |
5f1ea0ee | 290 | m_startValue = value; |
e179bd65 RR |
291 | } |
292 | ||
293 | void wxTreeTextCtrl::OnChar( wxKeyEvent &event ) | |
294 | { | |
295 | if (event.m_keyCode == WXK_RETURN) | |
296 | { | |
297 | (*m_accept) = TRUE; | |
298 | (*m_res) = GetValue(); | |
bce1406b RR |
299 | |
300 | if (!wxPendingDelete.Member(this)) | |
301 | wxPendingDelete.Append(this); | |
302 | ||
303 | if ((*m_accept) && ((*m_res) != m_startValue)) | |
304 | m_owner->OnRenameAccept(); | |
305 | ||
e179bd65 RR |
306 | return; |
307 | } | |
308 | if (event.m_keyCode == WXK_ESCAPE) | |
309 | { | |
310 | (*m_accept) = FALSE; | |
311 | (*m_res) = ""; | |
bce1406b RR |
312 | |
313 | if (!wxPendingDelete.Member(this)) | |
314 | wxPendingDelete.Append(this); | |
315 | ||
e179bd65 RR |
316 | return; |
317 | } | |
318 | event.Skip(); | |
319 | } | |
320 | ||
321 | void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) ) | |
322 | { | |
bce1406b RR |
323 | if (!wxPendingDelete.Member(this)) |
324 | wxPendingDelete.Append(this); | |
9dfbf520 | 325 | |
5f1ea0ee RR |
326 | if ((*m_accept) && ((*m_res) != m_startValue)) |
327 | m_owner->OnRenameAccept(); | |
e179bd65 RR |
328 | } |
329 | ||
f135ff73 | 330 | // ----------------------------------------------------------------------------- |
c801d85f | 331 | // wxTreeEvent |
f135ff73 | 332 | // ----------------------------------------------------------------------------- |
c801d85f | 333 | |
e179bd65 | 334 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent) |
9dfbf520 | 335 | |
f135ff73 | 336 | wxTreeEvent::wxTreeEvent( wxEventType commandType, int id ) |
92976ab6 | 337 | : wxNotifyEvent( commandType, id ) |
c801d85f | 338 | { |
00e12320 RR |
339 | m_code = 0; |
340 | m_itemOld = (wxGenericTreeItem *)NULL; | |
edaa81ae | 341 | } |
c801d85f | 342 | |
f135ff73 | 343 | // ----------------------------------------------------------------------------- |
c801d85f | 344 | // wxGenericTreeItem |
f135ff73 | 345 | // ----------------------------------------------------------------------------- |
c801d85f | 346 | |
f135ff73 VZ |
347 | wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent, |
348 | const wxString& text, | |
406005d2 | 349 | wxDC& WXUNUSED(dc), |
f135ff73 VZ |
350 | int image, int selImage, |
351 | wxTreeItemData *data) | |
352 | : m_text(text) | |
c801d85f | 353 | { |
00e12320 RR |
354 | m_images[wxTreeItemIcon_Normal] = image; |
355 | m_images[wxTreeItemIcon_Selected] = selImage; | |
356 | m_images[wxTreeItemIcon_Expanded] = NO_IMAGE; | |
357 | m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE; | |
8dc99046 | 358 | |
00e12320 RR |
359 | m_data = data; |
360 | m_x = m_y = 0; | |
361 | m_xCross = m_yCross = 0; | |
f135ff73 | 362 | |
00e12320 | 363 | m_level = 0; |
f135ff73 | 364 | |
00e12320 RR |
365 | m_isCollapsed = TRUE; |
366 | m_hasHilight = FALSE; | |
367 | m_hasPlus = FALSE; | |
368 | m_isBold = FALSE; | |
c801d85f | 369 | |
00e12320 | 370 | m_parent = parent; |
f135ff73 | 371 | |
00e12320 | 372 | m_attr = (wxTreeItemAttr *)NULL; |
06b466c7 | 373 | |
00e12320 RR |
374 | // We don't know the height here yet. |
375 | m_width = 0; | |
376 | m_height = 0; | |
edaa81ae | 377 | } |
c801d85f | 378 | |
f135ff73 | 379 | wxGenericTreeItem::~wxGenericTreeItem() |
c801d85f | 380 | { |
00e12320 | 381 | delete m_data; |
4832f7c0 | 382 | |
00e12320 | 383 | delete m_attr; |
9ec64fa7 | 384 | |
00e12320 RR |
385 | wxASSERT_MSG( m_children.IsEmpty(), |
386 | wxT("please call DeleteChildren() before deleting the item") ); | |
372edb9d VZ |
387 | } |
388 | ||
a43a4f9d | 389 | void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree) |
372edb9d | 390 | { |
00e12320 RR |
391 | size_t count = m_children.Count(); |
392 | for ( size_t n = 0; n < count; n++ ) | |
a43a4f9d | 393 | { |
00e12320 RR |
394 | wxGenericTreeItem *child = m_children[n]; |
395 | if (tree) | |
396 | tree->SendDeleteEvent(child); | |
a43a4f9d | 397 | |
00e12320 RR |
398 | child->DeleteChildren(tree); |
399 | delete child; | |
400 | } | |
372edb9d | 401 | |
00e12320 | 402 | m_children.Empty(); |
edaa81ae | 403 | } |
c801d85f | 404 | |
91b8de8d | 405 | void wxGenericTreeItem::SetText( const wxString &text ) |
c801d85f | 406 | { |
00e12320 | 407 | m_text = text; |
edaa81ae | 408 | } |
c801d85f | 409 | |
74bedbeb | 410 | void wxGenericTreeItem::Reset() |
c801d85f | 411 | { |
00e12320 RR |
412 | m_text.Empty(); |
413 | for ( int i = 0; i < wxTreeItemIcon_Max; i++ ) | |
414 | { | |
415 | m_images[i] = NO_IMAGE; | |
416 | } | |
8dc99046 | 417 | |
00e12320 RR |
418 | m_data = NULL; |
419 | m_x = m_y = | |
420 | m_height = m_width = 0; | |
421 | m_xCross = | |
422 | m_yCross = 0; | |
74bedbeb | 423 | |
00e12320 | 424 | m_level = 0; |
c801d85f | 425 | |
00e12320 RR |
426 | DeleteChildren(); |
427 | m_isCollapsed = TRUE; | |
c801d85f | 428 | |
00e12320 | 429 | m_parent = (wxGenericTreeItem *)NULL; |
edaa81ae | 430 | } |
c801d85f | 431 | |
4832f7c0 | 432 | size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const |
c801d85f | 433 | { |
00e12320 RR |
434 | size_t count = m_children.Count(); |
435 | if ( !recursively ) | |
436 | return count; | |
4832f7c0 | 437 | |
00e12320 | 438 | size_t total = count; |
f2593d0d | 439 | for (size_t n = 0; n < count; ++n) |
00e12320 RR |
440 | { |
441 | total += m_children[n]->GetChildrenCount(); | |
442 | } | |
c801d85f | 443 | |
00e12320 | 444 | return total; |
edaa81ae | 445 | } |
c801d85f KB |
446 | |
447 | void wxGenericTreeItem::SetCross( int x, int y ) | |
448 | { | |
00e12320 RR |
449 | m_xCross = x; |
450 | m_yCross = y; | |
edaa81ae | 451 | } |
c801d85f | 452 | |
91b8de8d | 453 | void wxGenericTreeItem::GetSize( int &x, int &y, const wxTreeCtrl *theTree ) |
c801d85f | 454 | { |
00e12320 RR |
455 | int bottomY=m_y+theTree->GetLineHeight(this); |
456 | if ( y < bottomY ) y = bottomY; | |
457 | int width = m_x + m_width; | |
458 | if ( x < width ) x = width; | |
f135ff73 | 459 | |
00e12320 | 460 | if (IsExpanded()) |
4832f7c0 | 461 | { |
00e12320 RR |
462 | size_t count = m_children.Count(); |
463 | for ( size_t n = 0; n < count; ++n ) | |
464 | { | |
465 | m_children[n]->GetSize( x, y, theTree ); | |
466 | } | |
df875e59 | 467 | } |
edaa81ae | 468 | } |
c801d85f | 469 | |
f135ff73 | 470 | wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point, |
c193b707 VZ |
471 | const wxTreeCtrl *theTree, |
472 | int &flags) | |
c801d85f | 473 | { |
f2593d0d | 474 | if ((point.y > m_y) && (point.y < m_y + theTree->GetLineHeight(this))) |
c801d85f | 475 | { |
3dbeaa52 | 476 | if (point.y < m_y+theTree->GetLineHeight(this)/2 ) |
f2593d0d RR |
477 | flags |= wxTREE_HITTEST_ONITEMUPPERPART; |
478 | else | |
479 | flags |= wxTREE_HITTEST_ONITEMLOWERPART; | |
978f38c2 | 480 | |
f2593d0d RR |
481 | // 5 is the size of the plus sign |
482 | if ((point.x > m_xCross-5) && (point.x < m_xCross+5) && | |
483 | (point.y > m_yCross-5) && (point.y < m_yCross+5) && | |
484 | (IsExpanded() || HasPlus())) | |
485 | { | |
486 | flags|=wxTREE_HITTEST_ONITEMBUTTON; | |
487 | return this; | |
488 | } | |
d3a9f4af | 489 | |
f2593d0d RR |
490 | if ((point.x >= m_x) && (point.x <= m_x+m_width)) |
491 | { | |
492 | int image_w = -1; | |
493 | int image_h; | |
0ae7f2a2 | 494 | |
f2593d0d RR |
495 | // assuming every image (normal and selected ) has the same size ! |
496 | if ( (GetImage() != NO_IMAGE) && theTree->m_imageListNormal ) | |
497 | theTree->m_imageListNormal->GetSize(GetImage(), image_w, image_h); | |
91b8de8d | 498 | |
f2593d0d RR |
499 | if ((image_w != -1) && (point.x <= m_x + image_w + 1)) |
500 | flags |= wxTREE_HITTEST_ONITEMICON; | |
501 | else | |
502 | flags |= wxTREE_HITTEST_ONITEMLABEL; | |
91b8de8d | 503 | |
f2593d0d RR |
504 | return this; |
505 | } | |
91b8de8d | 506 | |
f2593d0d RR |
507 | if (point.x < m_x) |
508 | flags |= wxTREE_HITTEST_ONITEMINDENT; | |
509 | if (point.x > m_x+m_width) | |
510 | flags |= wxTREE_HITTEST_ONITEMRIGHT; | |
511 | ||
512 | return this; | |
513 | } | |
514 | else | |
c801d85f | 515 | { |
f2593d0d RR |
516 | if (!m_isCollapsed) |
517 | { | |
518 | size_t count = m_children.Count(); | |
519 | for ( size_t n = 0; n < count; n++ ) | |
520 | { | |
521 | wxGenericTreeItem *res = m_children[n]->HitTest( point, theTree, flags ); | |
522 | if ( res != NULL ) | |
523 | return res; | |
524 | } | |
525 | } | |
edaa81ae | 526 | } |
f135ff73 | 527 | |
f2593d0d | 528 | flags|=wxTREE_HITTEST_NOWHERE; |
06b466c7 | 529 | |
f2593d0d | 530 | return (wxGenericTreeItem*) NULL; |
edaa81ae | 531 | } |
c801d85f | 532 | |
8dc99046 VZ |
533 | int wxGenericTreeItem::GetCurrentImage() const |
534 | { | |
535 | int image = NO_IMAGE; | |
536 | if ( IsExpanded() ) | |
537 | { | |
538 | if ( IsSelected() ) | |
539 | { | |
540 | image = GetImage(wxTreeItemIcon_SelectedExpanded); | |
541 | } | |
542 | ||
543 | if ( image == NO_IMAGE ) | |
544 | { | |
545 | // we usually fall back to the normal item, but try just the | |
546 | // expanded one (and not selected) first in this case | |
547 | image = GetImage(wxTreeItemIcon_Expanded); | |
548 | } | |
549 | } | |
550 | else // not expanded | |
551 | { | |
552 | if ( IsSelected() ) | |
553 | image = GetImage(wxTreeItemIcon_Selected); | |
554 | } | |
555 | ||
556 | // may be it doesn't have the specific image we want, try the default one | |
557 | // instead | |
558 | if ( image == NO_IMAGE ) | |
559 | { | |
560 | image = GetImage(); | |
561 | } | |
562 | ||
563 | return image; | |
564 | } | |
565 | ||
f135ff73 VZ |
566 | // ----------------------------------------------------------------------------- |
567 | // wxTreeCtrl implementation | |
568 | // ----------------------------------------------------------------------------- | |
569 | ||
570 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow) | |
571 | ||
572 | BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow) | |
00e12320 RR |
573 | EVT_PAINT (wxTreeCtrl::OnPaint) |
574 | EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse) | |
575 | EVT_CHAR (wxTreeCtrl::OnChar) | |
576 | EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus) | |
577 | EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus) | |
578 | EVT_IDLE (wxTreeCtrl::OnIdle) | |
f135ff73 VZ |
579 | END_EVENT_TABLE() |
580 | ||
581 | // ----------------------------------------------------------------------------- | |
582 | // construction/destruction | |
583 | // ----------------------------------------------------------------------------- | |
91b8de8d | 584 | |
f135ff73 | 585 | void wxTreeCtrl::Init() |
c801d85f | 586 | { |
00e12320 RR |
587 | m_current = |
588 | m_key_current = | |
589 | m_anchor = (wxGenericTreeItem *) NULL; | |
590 | m_hasFocus = FALSE; | |
591 | m_dirty = FALSE; | |
f135ff73 | 592 | |
00e12320 RR |
593 | m_xScroll = 0; |
594 | m_yScroll = 0; | |
595 | m_lineHeight = 10; | |
596 | m_indent = 15; | |
597 | m_spacing = 18; | |
f135ff73 | 598 | |
00e12320 | 599 | m_hilightBrush = new wxBrush |
f135ff73 VZ |
600 | ( |
601 | wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), | |
602 | wxSOLID | |
603 | ); | |
604 | ||
00e12320 RR |
605 | m_imageListNormal = |
606 | m_imageListState = (wxImageList *) NULL; | |
978f38c2 | 607 | |
00e12320 | 608 | m_dragCount = 0; |
3dbeaa52 | 609 | m_isDragging = FALSE; |
b3a7510d VZ |
610 | m_dropTarget = |
611 | m_oldSelection = (wxGenericTreeItem *)NULL; | |
9dfbf520 | 612 | |
00e12320 | 613 | m_renameTimer = new wxTreeRenameTimer( this ); |
f38374d0 | 614 | |
00e12320 RR |
615 | m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ); |
616 | m_boldFont = wxFont( m_normalFont.GetPointSize(), | |
eff869aa RR |
617 | m_normalFont.GetFamily(), |
618 | m_normalFont.GetStyle(), | |
619 | wxBOLD, | |
620 | m_normalFont.GetUnderlined()); | |
edaa81ae | 621 | } |
c801d85f | 622 | |
f135ff73 VZ |
623 | bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, |
624 | const wxPoint& pos, const wxSize& size, | |
978f38c2 | 625 | long style, |
674ac8b9 VZ |
626 | const wxValidator &validator, |
627 | const wxString& name ) | |
c801d85f | 628 | { |
00e12320 | 629 | Init(); |
f135ff73 | 630 | |
00e12320 | 631 | wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name ); |
978f38c2 | 632 | |
ce4169a4 | 633 | #if wxUSE_VALIDATORS |
00e12320 | 634 | SetValidator( validator ); |
ce4169a4 | 635 | #endif |
f135ff73 | 636 | |
91fc2bdc | 637 | SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) ); |
00e12320 RR |
638 | // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86 |
639 | m_dottedPen = wxPen( "grey", 0, 0 ); | |
f135ff73 | 640 | |
00e12320 | 641 | return TRUE; |
edaa81ae | 642 | } |
c801d85f | 643 | |
f135ff73 | 644 | wxTreeCtrl::~wxTreeCtrl() |
c801d85f | 645 | { |
00e12320 | 646 | wxDELETE( m_hilightBrush ); |
a43a4f9d | 647 | |
00e12320 | 648 | DeleteAllItems(); |
9dfbf520 | 649 | |
00e12320 | 650 | delete m_renameTimer; |
edaa81ae | 651 | } |
c801d85f | 652 | |
f135ff73 VZ |
653 | // ----------------------------------------------------------------------------- |
654 | // accessors | |
655 | // ----------------------------------------------------------------------------- | |
656 | ||
657 | size_t wxTreeCtrl::GetCount() const | |
c801d85f | 658 | { |
f2593d0d | 659 | return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount(); |
edaa81ae | 660 | } |
c801d85f | 661 | |
f135ff73 | 662 | void wxTreeCtrl::SetIndent(unsigned int indent) |
c801d85f | 663 | { |
f2593d0d RR |
664 | m_indent = indent; |
665 | m_dirty = TRUE; | |
cf724bce RR |
666 | } |
667 | ||
668 | void wxTreeCtrl::SetSpacing(unsigned int spacing) | |
669 | { | |
f2593d0d RR |
670 | m_spacing = spacing; |
671 | m_dirty = TRUE; | |
f135ff73 | 672 | } |
74bedbeb | 673 | |
4832f7c0 VZ |
674 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively) |
675 | { | |
f2593d0d | 676 | wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") ); |
4832f7c0 | 677 | |
f2593d0d | 678 | return item.m_pItem->GetChildrenCount(recursively); |
4832f7c0 VZ |
679 | } |
680 | ||
f135ff73 VZ |
681 | // ----------------------------------------------------------------------------- |
682 | // functions to work with tree items | |
683 | // ----------------------------------------------------------------------------- | |
74bedbeb | 684 | |
f135ff73 VZ |
685 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const |
686 | { | |
f2593d0d | 687 | wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") ); |
4832f7c0 | 688 | |
f2593d0d | 689 | return item.m_pItem->GetText(); |
edaa81ae | 690 | } |
74bedbeb | 691 | |
8dc99046 VZ |
692 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item, |
693 | wxTreeItemIcon which) const | |
74bedbeb | 694 | { |
f2593d0d | 695 | wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") ); |
4832f7c0 | 696 | |
f2593d0d | 697 | return item.m_pItem->GetImage(which); |
edaa81ae | 698 | } |
c801d85f | 699 | |
f135ff73 | 700 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
c801d85f | 701 | { |
f2593d0d | 702 | wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") ); |
4832f7c0 | 703 | |
f2593d0d | 704 | return item.m_pItem->GetData(); |
edaa81ae | 705 | } |
c801d85f | 706 | |
f135ff73 VZ |
707 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
708 | { | |
f2593d0d | 709 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
4832f7c0 | 710 | |
f2593d0d RR |
711 | wxClientDC dc(this); |
712 | wxGenericTreeItem *pItem = item.m_pItem; | |
713 | pItem->SetText(text); | |
714 | CalculateSize(pItem, dc); | |
715 | RefreshLine(pItem); | |
f135ff73 | 716 | } |
c801d85f | 717 | |
8dc99046 VZ |
718 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, |
719 | int image, | |
720 | wxTreeItemIcon which) | |
f135ff73 | 721 | { |
223d09f6 | 722 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
4832f7c0 | 723 | |
8dc99046 VZ |
724 | wxGenericTreeItem *pItem = item.m_pItem; |
725 | pItem->SetImage(image, which); | |
4832f7c0 | 726 | |
8dc99046 VZ |
727 | wxClientDC dc(this); |
728 | CalculateSize(pItem, dc); | |
729 | RefreshLine(pItem); | |
edaa81ae | 730 | } |
c801d85f | 731 | |
f135ff73 | 732 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
c801d85f | 733 | { |
f2593d0d | 734 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
4832f7c0 | 735 | |
f2593d0d | 736 | item.m_pItem->SetData(data); |
edaa81ae | 737 | } |
c801d85f | 738 | |
f135ff73 | 739 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
c801d85f | 740 | { |
f2593d0d | 741 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
4832f7c0 | 742 | |
f2593d0d RR |
743 | wxGenericTreeItem *pItem = item.m_pItem; |
744 | pItem->SetHasPlus(has); | |
745 | RefreshLine(pItem); | |
edaa81ae | 746 | } |
c801d85f | 747 | |
ef44a621 VZ |
748 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
749 | { | |
9ec64fa7 | 750 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
ef44a621 | 751 | |
9ec64fa7 VZ |
752 | // avoid redrawing the tree if no real change |
753 | wxGenericTreeItem *pItem = item.m_pItem; | |
754 | if ( pItem->IsBold() != bold ) | |
755 | { | |
756 | pItem->SetBold(bold); | |
757 | RefreshLine(pItem); | |
758 | } | |
759 | } | |
760 | ||
761 | void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item, | |
762 | const wxColour& col) | |
763 | { | |
764 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
765 | ||
766 | wxGenericTreeItem *pItem = item.m_pItem; | |
767 | pItem->Attr().SetTextColour(col); | |
768 | RefreshLine(pItem); | |
769 | } | |
770 | ||
771 | void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, | |
772 | const wxColour& col) | |
773 | { | |
774 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
775 | ||
776 | wxGenericTreeItem *pItem = item.m_pItem; | |
777 | pItem->Attr().SetBackgroundColour(col); | |
778 | RefreshLine(pItem); | |
779 | } | |
780 | ||
781 | void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) | |
782 | { | |
783 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); | |
784 | ||
785 | wxGenericTreeItem *pItem = item.m_pItem; | |
786 | pItem->Attr().SetFont(font); | |
ef44a621 | 787 | RefreshLine(pItem); |
ef44a621 VZ |
788 | } |
789 | ||
f135ff73 VZ |
790 | // ----------------------------------------------------------------------------- |
791 | // item status inquiries | |
792 | // ----------------------------------------------------------------------------- | |
793 | ||
df875e59 | 794 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const |
c801d85f | 795 | { |
f2593d0d | 796 | wxFAIL_MSG(wxT("not implemented")); |
f135ff73 | 797 | |
f2593d0d | 798 | return TRUE; |
edaa81ae | 799 | } |
c801d85f | 800 | |
f135ff73 | 801 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
c801d85f | 802 | { |
f2593d0d | 803 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); |
4832f7c0 | 804 | |
f2593d0d | 805 | return !item.m_pItem->GetChildren().IsEmpty(); |
edaa81ae | 806 | } |
c801d85f | 807 | |
f135ff73 | 808 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
c801d85f | 809 | { |
f2593d0d | 810 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); |
4832f7c0 | 811 | |
f2593d0d | 812 | return item.m_pItem->IsExpanded(); |
f135ff73 | 813 | } |
29d87bba | 814 | |
f135ff73 VZ |
815 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
816 | { | |
f2593d0d | 817 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); |
4832f7c0 | 818 | |
f2593d0d | 819 | return item.m_pItem->IsSelected(); |
f135ff73 | 820 | } |
29d87bba | 821 | |
ef44a621 VZ |
822 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const |
823 | { | |
f2593d0d | 824 | wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") ); |
ef44a621 | 825 | |
f2593d0d | 826 | return item.m_pItem->IsBold(); |
ef44a621 VZ |
827 | } |
828 | ||
f135ff73 VZ |
829 | // ----------------------------------------------------------------------------- |
830 | // navigation | |
831 | // ----------------------------------------------------------------------------- | |
29d87bba | 832 | |
f135ff73 VZ |
833 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const |
834 | { | |
223d09f6 | 835 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
389cdc7a | 836 | |
f135ff73 VZ |
837 | return item.m_pItem->GetParent(); |
838 | } | |
29d87bba | 839 | |
f135ff73 VZ |
840 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const |
841 | { | |
223d09f6 | 842 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
29d87bba | 843 | |
f135ff73 VZ |
844 | cookie = 0; |
845 | return GetNextChild(item, cookie); | |
846 | } | |
29d87bba | 847 | |
f135ff73 VZ |
848 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const |
849 | { | |
223d09f6 | 850 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
29d87bba | 851 | |
91b8de8d | 852 | wxArrayGenericTreeItems& children = item.m_pItem->GetChildren(); |
4832f7c0 VZ |
853 | if ( (size_t)cookie < children.Count() ) |
854 | { | |
06b466c7 | 855 | return children.Item((size_t)cookie++); |
4832f7c0 VZ |
856 | } |
857 | else | |
858 | { | |
859 | // there are no more of them | |
1e6d9499 | 860 | return wxTreeItemId(); |
4832f7c0 | 861 | } |
f135ff73 | 862 | } |
29d87bba | 863 | |
978f38c2 VZ |
864 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
865 | { | |
223d09f6 | 866 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
978f38c2 | 867 | |
91b8de8d | 868 | wxArrayGenericTreeItems& children = item.m_pItem->GetChildren(); |
0a240683 | 869 | return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last())); |
978f38c2 VZ |
870 | } |
871 | ||
f135ff73 VZ |
872 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
873 | { | |
223d09f6 | 874 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
f135ff73 VZ |
875 | |
876 | wxGenericTreeItem *i = item.m_pItem; | |
877 | wxGenericTreeItem *parent = i->GetParent(); | |
878 | if ( parent == NULL ) | |
879 | { | |
880 | // root item doesn't have any siblings | |
1e6d9499 | 881 | return wxTreeItemId(); |
edaa81ae | 882 | } |
4832f7c0 | 883 | |
91b8de8d | 884 | wxArrayGenericTreeItems& siblings = parent->GetChildren(); |
f135ff73 | 885 | int index = siblings.Index(i); |
3c67202d | 886 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? |
29d87bba | 887 | |
f135ff73 | 888 | size_t n = (size_t)(index + 1); |
fdd8d7b5 | 889 | return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]); |
edaa81ae | 890 | } |
c801d85f | 891 | |
f135ff73 | 892 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
c801d85f | 893 | { |
223d09f6 | 894 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
f135ff73 VZ |
895 | |
896 | wxGenericTreeItem *i = item.m_pItem; | |
897 | wxGenericTreeItem *parent = i->GetParent(); | |
898 | if ( parent == NULL ) | |
c801d85f | 899 | { |
f135ff73 | 900 | // root item doesn't have any siblings |
1e6d9499 | 901 | return wxTreeItemId(); |
edaa81ae | 902 | } |
4832f7c0 | 903 | |
91b8de8d | 904 | wxArrayGenericTreeItems& siblings = parent->GetChildren(); |
f135ff73 | 905 | int index = siblings.Index(i); |
3c67202d | 906 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? |
29d87bba | 907 | |
fdd8d7b5 VZ |
908 | return index == 0 ? wxTreeItemId() |
909 | : wxTreeItemId(siblings[(size_t)(index - 1)]); | |
f135ff73 | 910 | } |
389cdc7a | 911 | |
f135ff73 VZ |
912 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
913 | { | |
223d09f6 | 914 | wxFAIL_MSG(wxT("not implemented")); |
29d87bba | 915 | |
1e6d9499 | 916 | return wxTreeItemId(); |
f135ff73 | 917 | } |
29d87bba | 918 | |
f135ff73 VZ |
919 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
920 | { | |
223d09f6 | 921 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
29d87bba | 922 | |
223d09f6 | 923 | wxFAIL_MSG(wxT("not implemented")); |
29d87bba | 924 | |
1e6d9499 | 925 | return wxTreeItemId(); |
f135ff73 | 926 | } |
29d87bba | 927 | |
f135ff73 VZ |
928 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
929 | { | |
223d09f6 | 930 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
29d87bba | 931 | |
223d09f6 | 932 | wxFAIL_MSG(wxT("not implemented")); |
29d87bba | 933 | |
1e6d9499 | 934 | return wxTreeItemId(); |
edaa81ae | 935 | } |
c801d85f | 936 | |
f135ff73 VZ |
937 | // ----------------------------------------------------------------------------- |
938 | // operations | |
939 | // ----------------------------------------------------------------------------- | |
940 | ||
941 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId, | |
942 | size_t previous, | |
943 | const wxString& text, | |
944 | int image, int selImage, | |
945 | wxTreeItemData *data) | |
c801d85f | 946 | { |
f49f2b0c RR |
947 | wxGenericTreeItem *parent = parentId.m_pItem; |
948 | if ( !parent ) | |
949 | { | |
950 | // should we give a warning here? | |
951 | return AddRoot(text, image, selImage, data); | |
952 | } | |
4832f7c0 | 953 | |
f49f2b0c | 954 | wxClientDC dc(this); |
f38374d0 | 955 | wxGenericTreeItem *item = |
f49f2b0c | 956 | new wxGenericTreeItem( parent, text, dc, image, selImage, data ); |
74bedbeb | 957 | |
f49f2b0c RR |
958 | if ( data != NULL ) |
959 | { | |
960 | data->m_pItem = item; | |
961 | } | |
74bedbeb | 962 | |
f49f2b0c | 963 | parent->Insert( item, previous ); |
ef44a621 | 964 | |
f49f2b0c | 965 | m_dirty = TRUE; |
389cdc7a | 966 | |
f49f2b0c | 967 | return item; |
4c681997 RR |
968 | } |
969 | ||
f135ff73 VZ |
970 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
971 | int image, int selImage, | |
972 | wxTreeItemData *data) | |
4c681997 | 973 | { |
f49f2b0c | 974 | wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") ); |
389cdc7a | 975 | |
f49f2b0c RR |
976 | wxClientDC dc(this); |
977 | m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc, | |
f135ff73 | 978 | image, selImage, data); |
f49f2b0c RR |
979 | if ( data != NULL ) |
980 | { | |
981 | data->m_pItem = m_anchor; | |
982 | } | |
f38374d0 | 983 | |
f49f2b0c RR |
984 | if (!HasFlag(wxTR_MULTIPLE)) |
985 | { | |
986 | m_current = m_key_current = m_anchor; | |
06b466c7 | 987 | m_current->SetHilight( TRUE ); |
f49f2b0c | 988 | } |
389cdc7a | 989 | |
f49f2b0c RR |
990 | Refresh(); |
991 | AdjustMyScrollbars(); | |
a32dd690 | 992 | |
f49f2b0c | 993 | return m_anchor; |
edaa81ae | 994 | } |
c801d85f | 995 | |
f135ff73 VZ |
996 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, |
997 | const wxString& text, | |
998 | int image, int selImage, | |
999 | wxTreeItemData *data) | |
c801d85f | 1000 | { |
f2593d0d | 1001 | return DoInsertItem(parent, 0u, text, image, selImage, data); |
edaa81ae | 1002 | } |
c801d85f | 1003 | |
f135ff73 VZ |
1004 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId, |
1005 | const wxTreeItemId& idPrevious, | |
1006 | const wxString& text, | |
1007 | int image, int selImage, | |
1008 | wxTreeItemData *data) | |
c801d85f | 1009 | { |
f2593d0d RR |
1010 | wxGenericTreeItem *parent = parentId.m_pItem; |
1011 | if ( !parent ) | |
1012 | { | |
1013 | // should we give a warning here? | |
1014 | return AddRoot(text, image, selImage, data); | |
1015 | } | |
c801d85f | 1016 | |
f2593d0d RR |
1017 | int index = parent->GetChildren().Index(idPrevious.m_pItem); |
1018 | wxASSERT_MSG( index != wxNOT_FOUND, | |
1019 | wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") ); | |
06b466c7 | 1020 | |
f2593d0d RR |
1021 | return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data); |
1022 | } | |
1023 | ||
1024 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId, | |
1025 | size_t before, | |
1026 | const wxString& text, | |
1027 | int image, int selImage, | |
1028 | wxTreeItemData *data) | |
1029 | { | |
1030 | wxGenericTreeItem *parent = parentId.m_pItem; | |
1031 | if ( !parent ) | |
1032 | { | |
1033 | // should we give a warning here? | |
1034 | return AddRoot(text, image, selImage, data); | |
1035 | } | |
1036 | ||
1037 | return DoInsertItem(parentId, before, text, image, selImage, data); | |
edaa81ae | 1038 | } |
c801d85f | 1039 | |
f135ff73 VZ |
1040 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId, |
1041 | const wxString& text, | |
1042 | int image, int selImage, | |
1043 | wxTreeItemData *data) | |
74bedbeb | 1044 | { |
f2593d0d RR |
1045 | wxGenericTreeItem *parent = parentId.m_pItem; |
1046 | if ( !parent ) | |
1047 | { | |
1048 | // should we give a warning here? | |
1049 | return AddRoot(text, image, selImage, data); | |
1050 | } | |
f135ff73 | 1051 | |
f2593d0d RR |
1052 | return DoInsertItem( parent, parent->GetChildren().Count(), text, |
1053 | image, selImage, data); | |
74bedbeb VZ |
1054 | } |
1055 | ||
a43a4f9d VZ |
1056 | void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item) |
1057 | { | |
f2593d0d RR |
1058 | wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() ); |
1059 | event.m_item = item; | |
1060 | event.SetEventObject( this ); | |
1061 | ProcessEvent( event ); | |
a43a4f9d VZ |
1062 | } |
1063 | ||
372edb9d VZ |
1064 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId) |
1065 | { | |
1066 | wxGenericTreeItem *item = itemId.m_pItem; | |
a43a4f9d | 1067 | item->DeleteChildren(this); |
372edb9d VZ |
1068 | |
1069 | m_dirty = TRUE; | |
1070 | } | |
1071 | ||
f135ff73 | 1072 | void wxTreeCtrl::Delete(const wxTreeItemId& itemId) |
c801d85f | 1073 | { |
97d7bfb8 RR |
1074 | wxGenericTreeItem *item = itemId.m_pItem; |
1075 | wxGenericTreeItem *parent = item->GetParent(); | |
ff5bf259 | 1076 | |
97d7bfb8 RR |
1077 | if ( parent ) |
1078 | { | |
1079 | parent->GetChildren().Remove( item ); // remove by value | |
1080 | } | |
f135ff73 | 1081 | |
97d7bfb8 RR |
1082 | item->DeleteChildren(this); |
1083 | SendDeleteEvent(item); | |
1084 | delete item; | |
f135ff73 | 1085 | |
97d7bfb8 | 1086 | m_dirty = TRUE; |
edaa81ae | 1087 | } |
c801d85f | 1088 | |
f135ff73 | 1089 | void wxTreeCtrl::DeleteAllItems() |
c801d85f | 1090 | { |
97d7bfb8 RR |
1091 | if ( m_anchor ) |
1092 | { | |
1093 | m_anchor->DeleteChildren(this); | |
1094 | delete m_anchor; | |
a43a4f9d | 1095 | |
97d7bfb8 | 1096 | m_anchor = NULL; |
f135ff73 | 1097 | |
97d7bfb8 RR |
1098 | m_dirty = TRUE; |
1099 | } | |
edaa81ae RR |
1100 | } |
1101 | ||
f135ff73 | 1102 | void wxTreeCtrl::Expand(const wxTreeItemId& itemId) |
edaa81ae | 1103 | { |
f2593d0d | 1104 | wxGenericTreeItem *item = itemId.m_pItem; |
f135ff73 | 1105 | |
f2593d0d RR |
1106 | if ( !item->HasPlus() ) |
1107 | return; | |
978f38c2 | 1108 | |
f2593d0d RR |
1109 | if ( item->IsExpanded() ) |
1110 | return; | |
f135ff73 | 1111 | |
f2593d0d RR |
1112 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() ); |
1113 | event.m_item = item; | |
1114 | event.SetEventObject( this ); | |
004fd0c8 | 1115 | |
d5a07b9e | 1116 | // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ? |
f2593d0d RR |
1117 | if ( ProcessEvent( event ) && !event.IsAllowed() ) |
1118 | { | |
1119 | // cancelled by program | |
1120 | return; | |
1121 | } | |
4832f7c0 | 1122 | |
f2593d0d RR |
1123 | item->Expand(); |
1124 | CalculatePositions(); | |
f135ff73 | 1125 | |
f2593d0d | 1126 | RefreshSubtree(item); |
f135ff73 | 1127 | |
f2593d0d RR |
1128 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED); |
1129 | ProcessEvent( event ); | |
edaa81ae RR |
1130 | } |
1131 | ||
f135ff73 | 1132 | void wxTreeCtrl::Collapse(const wxTreeItemId& itemId) |
edaa81ae | 1133 | { |
f2593d0d | 1134 | wxGenericTreeItem *item = itemId.m_pItem; |
f135ff73 | 1135 | |
f2593d0d RR |
1136 | if ( !item->IsExpanded() ) |
1137 | return; | |
f135ff73 | 1138 | |
f2593d0d RR |
1139 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() ); |
1140 | event.m_item = item; | |
1141 | event.SetEventObject( this ); | |
1142 | if ( ProcessEvent( event ) && !event.IsAllowed() ) | |
1143 | { | |
1144 | // cancelled by program | |
1145 | return; | |
1146 | } | |
4832f7c0 | 1147 | |
f2593d0d | 1148 | item->Collapse(); |
f135ff73 | 1149 | |
f2593d0d RR |
1150 | wxArrayGenericTreeItems& children = item->GetChildren(); |
1151 | size_t count = children.Count(); | |
1152 | for ( size_t n = 0; n < count; n++ ) | |
1153 | { | |
1154 | Collapse(children[n]); | |
1155 | } | |
f135ff73 | 1156 | |
f2593d0d | 1157 | CalculatePositions(); |
f135ff73 | 1158 | |
f2593d0d | 1159 | RefreshSubtree(item); |
f135ff73 | 1160 | |
f2593d0d RR |
1161 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED); |
1162 | ProcessEvent( event ); | |
edaa81ae | 1163 | } |
c801d85f | 1164 | |
f135ff73 | 1165 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
c801d85f | 1166 | { |
00e12320 RR |
1167 | Collapse(item); |
1168 | DeleteChildren(item); | |
edaa81ae | 1169 | } |
c801d85f | 1170 | |
f135ff73 | 1171 | void wxTreeCtrl::Toggle(const wxTreeItemId& itemId) |
c801d85f | 1172 | { |
00e12320 | 1173 | wxGenericTreeItem *item = itemId.m_pItem; |
389cdc7a | 1174 | |
00e12320 RR |
1175 | if (item->IsExpanded()) |
1176 | Collapse(itemId); | |
1177 | else | |
1178 | Expand(itemId); | |
f135ff73 | 1179 | } |
389cdc7a | 1180 | |
f135ff73 VZ |
1181 | void wxTreeCtrl::Unselect() |
1182 | { | |
00e12320 RR |
1183 | if (m_current) |
1184 | { | |
1185 | m_current->SetHilight( FALSE ); | |
1186 | RefreshLine( m_current ); | |
1187 | } | |
edaa81ae | 1188 | } |
c801d85f | 1189 | |
88ac883a | 1190 | void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item) |
389cdc7a | 1191 | { |
00e12320 RR |
1192 | if (item->IsSelected()) |
1193 | { | |
1194 | item->SetHilight(FALSE); | |
1195 | RefreshLine(item); | |
1196 | } | |
d3a9f4af | 1197 | |
00e12320 | 1198 | if (item->HasChildren()) |
88ac883a | 1199 | { |
00e12320 RR |
1200 | wxArrayGenericTreeItems& children = item->GetChildren(); |
1201 | size_t count = children.Count(); | |
1202 | for ( size_t n = 0; n < count; ++n ) | |
1203 | { | |
1204 | UnselectAllChildren(children[n]); | |
1205 | } | |
88ac883a VZ |
1206 | } |
1207 | } | |
f135ff73 | 1208 | |
88ac883a VZ |
1209 | void wxTreeCtrl::UnselectAll() |
1210 | { | |
00e12320 | 1211 | UnselectAllChildren(GetRootItem().m_pItem); |
88ac883a VZ |
1212 | } |
1213 | ||
1214 | // Recursive function ! | |
1215 | // To stop we must have crt_item<last_item | |
91b8de8d | 1216 | // Algorithm : |
88ac883a | 1217 | // Tag all next children, when no more children, |
d3a9f4af | 1218 | // Move to parent (not to tag) |
91b8de8d | 1219 | // Keep going... if we found last_item, we stop. |
88ac883a VZ |
1220 | bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select) |
1221 | { | |
1222 | wxGenericTreeItem *parent = crt_item->GetParent(); | |
1223 | ||
00e12320 RR |
1224 | if (parent == NULL) // This is root item |
1225 | return TagAllChildrenUntilLast(crt_item, last_item, select); | |
88ac883a | 1226 | |
91b8de8d | 1227 | wxArrayGenericTreeItems& children = parent->GetChildren(); |
88ac883a VZ |
1228 | int index = children.Index(crt_item); |
1229 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? | |
1230 | ||
1231 | size_t count = children.Count(); | |
1232 | for (size_t n=(size_t)(index+1); n<count; ++n) | |
00e12320 RR |
1233 | { |
1234 | if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE; | |
1235 | } | |
88ac883a VZ |
1236 | |
1237 | return TagNextChildren(parent, last_item, select); | |
1238 | } | |
1239 | ||
1240 | bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select) | |
1241 | { | |
00e12320 RR |
1242 | crt_item->SetHilight(select); |
1243 | RefreshLine(crt_item); | |
d3a9f4af | 1244 | |
06b466c7 | 1245 | if (crt_item==last_item) |
00e12320 | 1246 | return TRUE; |
88ac883a | 1247 | |
00e12320 | 1248 | if (crt_item->HasChildren()) |
88ac883a | 1249 | { |
00e12320 RR |
1250 | wxArrayGenericTreeItems& children = crt_item->GetChildren(); |
1251 | size_t count = children.Count(); | |
1252 | for ( size_t n = 0; n < count; ++n ) | |
1253 | { | |
06b466c7 | 1254 | if (TagAllChildrenUntilLast(children[n], last_item, select)) |
00e12320 | 1255 | return TRUE; |
06b466c7 | 1256 | } |
88ac883a | 1257 | } |
d3a9f4af | 1258 | |
c0de7af4 | 1259 | return FALSE; |
88ac883a VZ |
1260 | } |
1261 | ||
1262 | void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2) | |
1263 | { | |
f2593d0d RR |
1264 | // item2 is not necessary after item1 |
1265 | wxGenericTreeItem *first=NULL, *last=NULL; | |
88ac883a | 1266 | |
f2593d0d RR |
1267 | // choice first' and 'last' between item1 and item2 |
1268 | if (item1->GetY()<item2->GetY()) | |
06b466c7 | 1269 | { |
f2593d0d RR |
1270 | first=item1; |
1271 | last=item2; | |
1272 | } | |
1273 | else | |
1274 | { | |
1275 | first=item2; | |
1276 | last=item1; | |
1277 | } | |
88ac883a | 1278 | |
f2593d0d | 1279 | bool select = m_current->IsSelected(); |
d3a9f4af | 1280 | |
f2593d0d RR |
1281 | if ( TagAllChildrenUntilLast(first,last,select) ) |
1282 | return; | |
88ac883a | 1283 | |
f2593d0d | 1284 | TagNextChildren(first,last,select); |
88ac883a VZ |
1285 | } |
1286 | ||
d3a9f4af | 1287 | void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId, |
c193b707 VZ |
1288 | bool unselect_others, |
1289 | bool extended_select) | |
d3a9f4af | 1290 | { |
223d09f6 | 1291 | wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") ); |
8b04a037 | 1292 | |
88ac883a | 1293 | bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE); |
5391f772 | 1294 | wxGenericTreeItem *item = itemId.m_pItem; |
88ac883a VZ |
1295 | |
1296 | //wxCHECK_RET( ( (!unselect_others) && is_single), | |
223d09f6 | 1297 | // wxT("this is a single selection tree") ); |
88ac883a VZ |
1298 | |
1299 | // to keep going anyhow !!! | |
d3a9f4af | 1300 | if (is_single) |
8dc99046 VZ |
1301 | { |
1302 | if (item->IsSelected()) | |
1303 | return; // nothing to do | |
1304 | unselect_others = TRUE; | |
1305 | extended_select = FALSE; | |
1306 | } | |
1307 | else if ( unselect_others && item->IsSelected() ) | |
1308 | { | |
1309 | // selection change if there is more than one item currently selected | |
1310 | wxArrayTreeItemIds selected_items; | |
1311 | if ( GetSelections(selected_items) == 1 ) | |
1312 | return; | |
1313 | } | |
d3a9f4af | 1314 | |
f135ff73 VZ |
1315 | wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() ); |
1316 | event.m_item = item; | |
1317 | event.m_itemOld = m_current; | |
1318 | event.SetEventObject( this ); | |
91b8de8d | 1319 | // TODO : Here we don't send any selection mode yet ! |
d3a9f4af | 1320 | |
f98e2558 | 1321 | if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() ) |
f135ff73 VZ |
1322 | return; |
1323 | ||
88ac883a VZ |
1324 | // ctrl press |
1325 | if (unselect_others) | |
389cdc7a | 1326 | { |
88ac883a | 1327 | if (is_single) Unselect(); // to speed up thing |
c193b707 | 1328 | else UnselectAll(); |
edaa81ae | 1329 | } |
f135ff73 | 1330 | |
88ac883a | 1331 | // shift press |
d3a9f4af | 1332 | if (extended_select) |
88ac883a | 1333 | { |
91b8de8d | 1334 | if (m_current == NULL) m_current=m_key_current=GetRootItem().m_pItem; |
88ac883a VZ |
1335 | // don't change the mark (m_current) |
1336 | SelectItemRange(m_current, item); | |
1337 | } | |
1338 | else | |
1339 | { | |
c0de7af4 | 1340 | bool select=TRUE; // the default |
88ac883a | 1341 | |
c193b707 VZ |
1342 | // Check if we need to toggle hilight (ctrl mode) |
1343 | if (!unselect_others) | |
8dc99046 | 1344 | select=!item->IsSelected(); |
88ac883a | 1345 | |
91b8de8d | 1346 | m_current = m_key_current = item; |
c193b707 VZ |
1347 | m_current->SetHilight(select); |
1348 | RefreshLine( m_current ); | |
88ac883a | 1349 | } |
389cdc7a | 1350 | |
f135ff73 | 1351 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); |
6daa0637 | 1352 | GetEventHandler()->ProcessEvent( event ); |
389cdc7a VZ |
1353 | } |
1354 | ||
9dfbf520 VZ |
1355 | void wxTreeCtrl::FillArray(wxGenericTreeItem *item, |
1356 | wxArrayTreeItemIds &array) const | |
c801d85f | 1357 | { |
8dc99046 | 1358 | if ( item->IsSelected() ) |
9dfbf520 | 1359 | array.Add(wxTreeItemId(item)); |
91b8de8d | 1360 | |
9dfbf520 | 1361 | if ( item->HasChildren() ) |
91b8de8d | 1362 | { |
9dfbf520 VZ |
1363 | wxArrayGenericTreeItems& children = item->GetChildren(); |
1364 | size_t count = children.GetCount(); | |
1365 | for ( size_t n = 0; n < count; ++n ) | |
1366 | FillArray(children[n],array); | |
91b8de8d RR |
1367 | } |
1368 | } | |
1369 | ||
1370 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const | |
1371 | { | |
1372 | array.Empty(); | |
1373 | FillArray(GetRootItem().m_pItem, array); | |
1374 | ||
1375 | return array.Count(); | |
1376 | } | |
1377 | ||
1378 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) | |
d3a9f4af | 1379 | { |
91b8de8d RR |
1380 | if (!item.IsOk()) return; |
1381 | ||
0659e7ee | 1382 | wxGenericTreeItem *gitem = item.m_pItem; |
ef44a621 | 1383 | |
f65635b5 VZ |
1384 | // first expand all parent branches |
1385 | wxGenericTreeItem *parent = gitem->GetParent(); | |
5391f772 | 1386 | while ( parent ) |
f65635b5 | 1387 | { |
8dc99046 | 1388 | Expand(parent); |
f65635b5 VZ |
1389 | parent = parent->GetParent(); |
1390 | } | |
1391 | ||
5391f772 | 1392 | //if (parent) CalculatePositions(); |
91b8de8d RR |
1393 | |
1394 | ScrollTo(item); | |
1395 | } | |
1396 | ||
1397 | void wxTreeCtrl::ScrollTo(const wxTreeItemId &item) | |
1398 | { | |
1399 | if (!item.IsOk()) return; | |
1400 | ||
dc6c62a9 RR |
1401 | // We have to call this here because the label in |
1402 | // question might just have been added and no screen | |
1403 | // update taken place. | |
1404 | if (m_dirty) wxYield(); | |
1405 | ||
91b8de8d RR |
1406 | wxGenericTreeItem *gitem = item.m_pItem; |
1407 | ||
f65635b5 | 1408 | // now scroll to the item |
0659e7ee | 1409 | int item_y = gitem->GetY(); |
8dc99046 | 1410 | |
0659e7ee RR |
1411 | int start_x = 0; |
1412 | int start_y = 0; | |
1413 | ViewStart( &start_x, &start_y ); | |
91b8de8d | 1414 | start_y *= PIXELS_PER_UNIT; |
978f38c2 | 1415 | |
a93109d5 RR |
1416 | int client_h = 0; |
1417 | int client_w = 0; | |
1418 | GetClientSize( &client_w, &client_h ); | |
ef44a621 | 1419 | |
0659e7ee RR |
1420 | if (item_y < start_y+3) |
1421 | { | |
91b8de8d | 1422 | // going down |
0659e7ee RR |
1423 | int x = 0; |
1424 | int y = 0; | |
91b8de8d RR |
1425 | m_anchor->GetSize( x, y, this ); |
1426 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
c7a9fa36 | 1427 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
0659e7ee | 1428 | int x_pos = GetScrollPos( wxHORIZONTAL ); |
c193b707 | 1429 | // Item should appear at top |
91b8de8d | 1430 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT ); |
0659e7ee | 1431 | } |
91b8de8d | 1432 | else if (item_y+GetLineHeight(gitem) > start_y+client_h) |
0659e7ee | 1433 | { |
c7a9fa36 RR |
1434 | // going up |
1435 | int x = 0; | |
1436 | int y = 0; | |
1437 | m_anchor->GetSize( x, y, this ); | |
1438 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
1439 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels | |
1440 | item_y += PIXELS_PER_UNIT+2; | |
1441 | int x_pos = GetScrollPos( wxHORIZONTAL ); | |
c193b707 | 1442 | // Item should appear at bottom |
c7a9fa36 | 1443 | 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 | 1444 | } |
edaa81ae | 1445 | } |
c801d85f | 1446 | |
e1ee62bd VZ |
1447 | // FIXME: tree sorting functions are not reentrant and not MT-safe! |
1448 | static wxTreeCtrl *s_treeBeingSorted = NULL; | |
0659e7ee | 1449 | |
004fd0c8 | 1450 | static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1, |
e1ee62bd | 1451 | wxGenericTreeItem **item2) |
edaa81ae | 1452 | { |
223d09f6 | 1453 | wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxTreeCtrl::SortChildren()") ); |
e1ee62bd VZ |
1454 | |
1455 | return s_treeBeingSorted->OnCompareItems(*item1, *item2); | |
0659e7ee RR |
1456 | } |
1457 | ||
e1ee62bd VZ |
1458 | int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
1459 | const wxTreeItemId& item2) | |
0659e7ee | 1460 | { |
87138c52 | 1461 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); |
e1ee62bd VZ |
1462 | } |
1463 | ||
1464 | void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId) | |
1465 | { | |
223d09f6 | 1466 | wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") ); |
e1ee62bd VZ |
1467 | |
1468 | wxGenericTreeItem *item = itemId.m_pItem; | |
978f38c2 | 1469 | |
e1ee62bd | 1470 | wxCHECK_RET( !s_treeBeingSorted, |
223d09f6 | 1471 | wxT("wxTreeCtrl::SortChildren is not reentrant") ); |
e1ee62bd | 1472 | |
91b8de8d | 1473 | wxArrayGenericTreeItems& children = item->GetChildren(); |
e1ee62bd VZ |
1474 | if ( children.Count() > 1 ) |
1475 | { | |
1476 | s_treeBeingSorted = this; | |
1477 | children.Sort(tree_ctrl_compare_func); | |
1478 | s_treeBeingSorted = NULL; | |
978f38c2 | 1479 | |
e1ee62bd VZ |
1480 | m_dirty = TRUE; |
1481 | } | |
1482 | //else: don't make the tree dirty as nothing changed | |
edaa81ae RR |
1483 | } |
1484 | ||
f135ff73 | 1485 | wxImageList *wxTreeCtrl::GetImageList() const |
edaa81ae | 1486 | { |
f135ff73 | 1487 | return m_imageListNormal; |
edaa81ae RR |
1488 | } |
1489 | ||
f135ff73 | 1490 | wxImageList *wxTreeCtrl::GetStateImageList() const |
c801d85f | 1491 | { |
f135ff73 | 1492 | return m_imageListState; |
edaa81ae | 1493 | } |
c801d85f | 1494 | |
f135ff73 | 1495 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
e2414cbe | 1496 | { |
f2593d0d | 1497 | m_imageListNormal = imageList; |
91b8de8d | 1498 | |
f2593d0d RR |
1499 | // Calculate a m_lineHeight value from the image sizes. |
1500 | // May be toggle off. Then wxTreeCtrl will spread when | |
1501 | // necessary (which might look ugly). | |
91b8de8d | 1502 | #if 1 |
f2593d0d RR |
1503 | wxClientDC dc(this); |
1504 | m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
1505 | int width = 0, height = 0, | |
1506 | n = m_imageListNormal->GetImageCount(); | |
06b466c7 | 1507 | |
f2593d0d RR |
1508 | for (int i = 0; i < n ; i++) |
1509 | { | |
1510 | m_imageListNormal->GetSize(i, width, height); | |
1511 | if (height > m_lineHeight) m_lineHeight = height; | |
1512 | } | |
91b8de8d | 1513 | |
06b466c7 | 1514 | if (m_lineHeight < 40) |
f2593d0d | 1515 | m_lineHeight += 2; // at least 2 pixels |
06b466c7 | 1516 | else |
f2593d0d | 1517 | m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing |
91b8de8d | 1518 | #endif |
edaa81ae | 1519 | } |
e2414cbe | 1520 | |
f135ff73 | 1521 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
e2414cbe | 1522 | { |
f135ff73 | 1523 | m_imageListState = imageList; |
edaa81ae | 1524 | } |
e2414cbe | 1525 | |
f135ff73 VZ |
1526 | // ----------------------------------------------------------------------------- |
1527 | // helpers | |
1528 | // ----------------------------------------------------------------------------- | |
0659e7ee | 1529 | |
74bedbeb | 1530 | void wxTreeCtrl::AdjustMyScrollbars() |
c801d85f | 1531 | { |
0659e7ee RR |
1532 | if (m_anchor) |
1533 | { | |
1534 | int x = 0; | |
1535 | int y = 0; | |
91b8de8d | 1536 | m_anchor->GetSize( x, y, this ); |
c193b707 | 1537 | y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
c7a9fa36 | 1538 | x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels |
0659e7ee RR |
1539 | int x_pos = GetScrollPos( wxHORIZONTAL ); |
1540 | int y_pos = GetScrollPos( wxVERTICAL ); | |
91b8de8d | 1541 | SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos ); |
0659e7ee RR |
1542 | } |
1543 | else | |
1544 | { | |
1545 | SetScrollbars( 0, 0, 0, 0 ); | |
1546 | } | |
edaa81ae | 1547 | } |
c801d85f | 1548 | |
91b8de8d RR |
1549 | int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const |
1550 | { | |
dc6c62a9 RR |
1551 | if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT) |
1552 | return item->GetHeight(); | |
1553 | else | |
1554 | return m_lineHeight; | |
91b8de8d RR |
1555 | } |
1556 | ||
ef44a621 VZ |
1557 | void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc) |
1558 | { | |
9ec64fa7 VZ |
1559 | wxTreeItemAttr *attr = item->GetAttributes(); |
1560 | if ( attr && attr->HasFont() ) | |
1561 | dc.SetFont(attr->GetFont()); | |
1562 | else if (item->IsBold()) | |
eff869aa | 1563 | dc.SetFont(m_boldFont); |
ef44a621 | 1564 | |
bbe0af5b RR |
1565 | long text_w = 0; |
1566 | long text_h = 0; | |
1567 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); | |
ef44a621 | 1568 | |
bbe0af5b RR |
1569 | int image_h = 0; |
1570 | int image_w = 0; | |
8dc99046 VZ |
1571 | int image = item->GetCurrentImage(); |
1572 | if ( image != NO_IMAGE ) | |
bbe0af5b | 1573 | { |
8dc99046 | 1574 | m_imageListNormal->GetSize( image, image_w, image_h ); |
bbe0af5b RR |
1575 | image_w += 4; |
1576 | } | |
ef44a621 | 1577 | |
91b8de8d RR |
1578 | int total_h = GetLineHeight(item); |
1579 | ||
c0fba4d1 VS |
1580 | if (item->IsSelected()) |
1581 | dc.SetBrush(*m_hilightBrush); | |
afa6a1a1 | 1582 | else |
c0fba4d1 VS |
1583 | { |
1584 | wxColour colBg; | |
1585 | if ( attr && attr->HasBackgroundColour() ) | |
1586 | colBg = attr->GetBackgroundColour(); | |
1587 | else | |
1588 | colBg = m_backgroundColour; | |
1589 | dc.SetBrush(wxBrush(colBg, wxSOLID)); | |
1590 | } | |
afa6a1a1 | 1591 | |
91b8de8d | 1592 | dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h ); |
ef44a621 | 1593 | |
8dc99046 | 1594 | if ( image != NO_IMAGE ) |
bbe0af5b | 1595 | { |
d30b4d20 | 1596 | dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h ); |
8dc99046 | 1597 | m_imageListNormal->Draw( image, dc, |
49cd56ef | 1598 | item->GetX(), |
d701d432 | 1599 | item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0), |
bbe0af5b RR |
1600 | wxIMAGELIST_DRAW_TRANSPARENT ); |
1601 | dc.DestroyClippingRegion(); | |
1602 | } | |
ef44a621 | 1603 | |
afa6a1a1 | 1604 | dc.SetBackgroundMode(wxTRANSPARENT); |
6cedba09 | 1605 | dc.DrawText( item->GetText(), image_w + item->GetX(), (wxCoord)item->GetY() |
49cd56ef | 1606 | + ((total_h > text_h) ? (total_h - text_h)/2 : 0)); |
ef44a621 | 1607 | |
eff869aa RR |
1608 | // restore normal font |
1609 | dc.SetFont( m_normalFont ); | |
ef44a621 VZ |
1610 | } |
1611 | ||
91b8de8d | 1612 | // Now y stands for the top of the item, whereas it used to stand for middle ! |
16c1f7f3 | 1613 | void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) |
c801d85f | 1614 | { |
bbe0af5b | 1615 | int horizX = level*m_indent; |
389cdc7a | 1616 | |
cf724bce | 1617 | item->SetX( horizX+m_indent+m_spacing ); |
91b8de8d | 1618 | item->SetY( y ); |
4c681997 | 1619 | |
bbe0af5b | 1620 | int oldY = y; |
91b8de8d RR |
1621 | y+=GetLineHeight(item)/2; |
1622 | ||
1623 | item->SetCross( horizX+m_indent, y ); | |
389cdc7a | 1624 | |
bbe0af5b | 1625 | int exposed_x = dc.LogicalToDeviceX( 0 ); |
5391f772 | 1626 | int exposed_y = dc.LogicalToDeviceY( item->GetY() ); |
4832f7c0 | 1627 | |
5391f772 | 1628 | if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much |
bbe0af5b RR |
1629 | { |
1630 | int startX = horizX; | |
cf724bce | 1631 | int endX = horizX + (m_indent-5); |
29d87bba | 1632 | |
cf724bce | 1633 | // if (!item->HasChildren()) endX += (m_indent+5); |
bbe0af5b | 1634 | if (!item->HasChildren()) endX += 20; |
4832f7c0 | 1635 | |
bbe0af5b | 1636 | dc.DrawLine( startX, y, endX, y ); |
29d87bba | 1637 | |
bbe0af5b RR |
1638 | if (item->HasPlus()) |
1639 | { | |
cf724bce | 1640 | dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y ); |
bbe0af5b RR |
1641 | dc.SetPen( *wxGREY_PEN ); |
1642 | dc.SetBrush( *wxWHITE_BRUSH ); | |
cf724bce | 1643 | dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 ); |
9dfbf520 | 1644 | |
bbe0af5b | 1645 | dc.SetPen( *wxBLACK_PEN ); |
cf724bce | 1646 | dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y ); |
bbe0af5b | 1647 | if (!item->IsExpanded()) |
cf724bce | 1648 | dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 ); |
9dfbf520 | 1649 | |
112c5086 | 1650 | dc.SetPen( m_dottedPen ); |
bbe0af5b | 1651 | } |
c801d85f | 1652 | |
9ec64fa7 | 1653 | wxPen *pen = wxTRANSPARENT_PEN; |
9ec64fa7 | 1654 | wxColour colText; |
a367b9b3 | 1655 | |
9ec64fa7 VZ |
1656 | if ( item->IsSelected() ) |
1657 | { | |
1658 | colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ); | |
4832f7c0 | 1659 | |
9ec64fa7 VZ |
1660 | if ( m_hasFocus ) |
1661 | pen = wxBLACK_PEN; | |
f135ff73 | 1662 | |
bbe0af5b RR |
1663 | } |
1664 | else | |
1665 | { | |
9ec64fa7 VZ |
1666 | wxTreeItemAttr *attr = item->GetAttributes(); |
1667 | if ( attr && attr->HasTextColour() ) | |
1668 | colText = attr->GetTextColour(); | |
1669 | else | |
1670 | colText = *wxBLACK; | |
bbe0af5b | 1671 | } |
9ec64fa7 VZ |
1672 | |
1673 | // prepare to draw | |
1674 | dc.SetTextForeground(colText); | |
1675 | dc.SetPen(*pen); | |
9ec64fa7 VZ |
1676 | |
1677 | // draw | |
1678 | PaintItem(item, dc); | |
1679 | ||
1680 | // restore DC objects | |
1681 | dc.SetBrush( *wxWHITE_BRUSH ); | |
1682 | dc.SetPen( m_dottedPen ); | |
1683 | dc.SetTextForeground( *wxBLACK ); | |
f135ff73 | 1684 | } |
d3a9f4af | 1685 | |
91b8de8d | 1686 | y = oldY+GetLineHeight(item); |
e2414cbe | 1687 | |
bbe0af5b RR |
1688 | if (item->IsExpanded()) |
1689 | { | |
91b8de8d | 1690 | oldY+=GetLineHeight(item)/2; |
9ec64fa7 | 1691 | int semiOldY=0; |
389cdc7a | 1692 | |
91b8de8d RR |
1693 | wxArrayGenericTreeItems& children = item->GetChildren(); |
1694 | size_t n, count = children.Count(); | |
1695 | for ( n = 0; n < count; ++n ) | |
c193b707 VZ |
1696 | { |
1697 | semiOldY=y; | |
1698 | PaintLevel( children[n], dc, level+1, y ); | |
1699 | } | |
389cdc7a | 1700 | |
f65635b5 VZ |
1701 | // it may happen that the item is expanded but has no items (when you |
1702 | // delete all its children for example) - don't draw the vertical line | |
1703 | // in this case | |
1704 | if (count > 0) | |
9ec64fa7 | 1705 | { |
c193b707 | 1706 | semiOldY+=GetLineHeight(children[--n])/2; |
cf724bce | 1707 | dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY ); |
9ec64fa7 | 1708 | } |
bbe0af5b | 1709 | } |
4c681997 | 1710 | } |
c801d85f | 1711 | |
3dbeaa52 VZ |
1712 | void wxTreeCtrl::DrawDropEffect(wxGenericTreeItem *item) |
1713 | { | |
1714 | if ( item ) | |
1715 | { | |
1716 | if ( item->HasPlus() ) | |
1717 | { | |
1718 | // it's a folder, indicate it by a border | |
1719 | DrawBorder(item); | |
1720 | } | |
1721 | else | |
1722 | { | |
1723 | // draw a line under the drop target because the item will be | |
1724 | // dropped there | |
1725 | DrawLine(item, TRUE /* below */); | |
1726 | } | |
1727 | ||
1728 | SetCursor(wxCURSOR_BULLSEYE); | |
1729 | } | |
1730 | else | |
1731 | { | |
1732 | // can't drop here | |
1733 | SetCursor(wxCURSOR_NO_ENTRY); | |
1734 | } | |
1735 | } | |
1736 | ||
1737 | void wxTreeCtrl::DrawBorder(const wxTreeItemId &item) | |
91b8de8d | 1738 | { |
3dbeaa52 | 1739 | wxCHECK_RET( item.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") ); |
91b8de8d | 1740 | |
3dbeaa52 | 1741 | wxGenericTreeItem *i = item.m_pItem; |
91b8de8d | 1742 | |
1044a386 | 1743 | wxClientDC dc(this); |
91b8de8d RR |
1744 | PrepareDC( dc ); |
1745 | dc.SetLogicalFunction(wxINVERT); | |
3dbeaa52 | 1746 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
91b8de8d | 1747 | |
3dbeaa52 VZ |
1748 | int w = i->GetWidth() + 2; |
1749 | int h = GetLineHeight(i) + 2; | |
91b8de8d | 1750 | |
3dbeaa52 | 1751 | dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h); |
91b8de8d RR |
1752 | } |
1753 | ||
3dbeaa52 | 1754 | void wxTreeCtrl::DrawLine(const wxTreeItemId &item, bool below) |
91b8de8d | 1755 | { |
3dbeaa52 | 1756 | wxCHECK_RET( item.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") ); |
91b8de8d | 1757 | |
3dbeaa52 | 1758 | wxGenericTreeItem *i = item.m_pItem; |
91b8de8d | 1759 | |
1044a386 | 1760 | wxClientDC dc(this); |
91b8de8d RR |
1761 | PrepareDC( dc ); |
1762 | dc.SetLogicalFunction(wxINVERT); | |
d3a9f4af | 1763 | |
3dbeaa52 VZ |
1764 | int x = i->GetX(), |
1765 | y = i->GetY(); | |
1766 | if ( below ) | |
1767 | { | |
1768 | y += GetLineHeight(i) - 1; | |
1769 | } | |
91b8de8d | 1770 | |
3dbeaa52 | 1771 | dc.DrawLine( x, y, x + i->GetWidth(), y); |
91b8de8d RR |
1772 | } |
1773 | ||
f135ff73 VZ |
1774 | // ----------------------------------------------------------------------------- |
1775 | // wxWindows callbacks | |
1776 | // ----------------------------------------------------------------------------- | |
1777 | ||
3db7be80 | 1778 | void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
c801d85f | 1779 | { |
91b8de8d | 1780 | if ( !m_anchor) |
0659e7ee | 1781 | return; |
c801d85f | 1782 | |
0659e7ee RR |
1783 | wxPaintDC dc(this); |
1784 | PrepareDC( dc ); | |
29d87bba | 1785 | |
eff869aa | 1786 | dc.SetFont( m_normalFont ); |
0659e7ee | 1787 | dc.SetPen( m_dottedPen ); |
f38374d0 | 1788 | |
eff869aa | 1789 | // this is now done dynamically |
91b8de8d RR |
1790 | //if(GetImageList() == NULL) |
1791 | // m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
29d87bba | 1792 | |
91b8de8d | 1793 | int y = 2; |
0659e7ee | 1794 | PaintLevel( m_anchor, dc, 0, y ); |
edaa81ae | 1795 | } |
c801d85f | 1796 | |
3db7be80 | 1797 | void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) ) |
c801d85f | 1798 | { |
0659e7ee | 1799 | m_hasFocus = TRUE; |
978f38c2 | 1800 | |
bbe0af5b | 1801 | if (m_current) RefreshLine( m_current ); |
edaa81ae | 1802 | } |
c801d85f | 1803 | |
3db7be80 | 1804 | void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) ) |
c801d85f | 1805 | { |
0659e7ee | 1806 | m_hasFocus = FALSE; |
978f38c2 | 1807 | |
bbe0af5b | 1808 | if (m_current) RefreshLine( m_current ); |
edaa81ae | 1809 | } |
c801d85f KB |
1810 | |
1811 | void wxTreeCtrl::OnChar( wxKeyEvent &event ) | |
1812 | { | |
978f38c2 | 1813 | wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() ); |
06b466c7 | 1814 | te.m_code = (int)event.KeyCode(); |
978f38c2 VZ |
1815 | te.SetEventObject( this ); |
1816 | GetEventHandler()->ProcessEvent( te ); | |
435fe83e | 1817 | |
91b8de8d | 1818 | if ( (m_current == 0) || (m_key_current == 0) ) |
978f38c2 VZ |
1819 | { |
1820 | event.Skip(); | |
1821 | return; | |
1822 | } | |
ef44a621 | 1823 | |
06b466c7 VZ |
1824 | // how should the selection work for this event? |
1825 | bool is_multiple, extended_select, unselect_others; | |
1826 | EventFlagsToSelType(GetWindowStyleFlag(), | |
1827 | event.ShiftDown(), | |
1828 | event.ControlDown(), | |
dfc6cd93 SB |
1829 | is_multiple, extended_select, unselect_others); |
1830 | ||
1831 | // + : Expand | |
1832 | // - : Collaspe | |
1833 | // * : Toggle Expand/Collapse | |
1834 | // ' ' | return : activate | |
1835 | // up : go up (not last children!) | |
1836 | // down : go down | |
1837 | // left : go to parent | |
1838 | // right : open if parent and go next | |
1839 | // home : go to root | |
1840 | // end : go to last item without opening parents | |
978f38c2 VZ |
1841 | switch (event.KeyCode()) |
1842 | { | |
1843 | case '+': | |
1844 | case WXK_ADD: | |
1845 | if (m_current->HasPlus() && !IsExpanded(m_current)) | |
1846 | { | |
1847 | Expand(m_current); | |
1848 | } | |
1849 | break; | |
ef44a621 | 1850 | |
978f38c2 VZ |
1851 | case '-': |
1852 | case WXK_SUBTRACT: | |
1853 | if (IsExpanded(m_current)) | |
1854 | { | |
1855 | Collapse(m_current); | |
1856 | } | |
1857 | break; | |
ef44a621 | 1858 | |
978f38c2 VZ |
1859 | case '*': |
1860 | case WXK_MULTIPLY: | |
1861 | Toggle(m_current); | |
1862 | break; | |
ef44a621 | 1863 | |
978f38c2 VZ |
1864 | case ' ': |
1865 | case WXK_RETURN: | |
1866 | { | |
1867 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); | |
1868 | event.m_item = m_current; | |
1869 | event.m_code = 0; | |
1870 | event.SetEventObject( this ); | |
1871 | GetEventHandler()->ProcessEvent( event ); | |
1872 | } | |
1873 | break; | |
ef44a621 | 1874 | |
978f38c2 VZ |
1875 | // up goes to the previous sibling or to the last of its children if |
1876 | // it's expanded | |
1877 | case WXK_UP: | |
1878 | { | |
91b8de8d | 1879 | wxTreeItemId prev = GetPrevSibling( m_key_current ); |
978f38c2 VZ |
1880 | if (!prev) |
1881 | { | |
91b8de8d | 1882 | prev = GetParent( m_key_current ); |
c193b707 VZ |
1883 | if (prev) |
1884 | { | |
90e58684 | 1885 | long cockie = 0; |
91b8de8d | 1886 | wxTreeItemId current = m_key_current; |
90e58684 RR |
1887 | if (current == GetFirstChild( prev, cockie )) |
1888 | { | |
1889 | // otherwise we return to where we came from | |
88ac883a | 1890 | SelectItem( prev, unselect_others, extended_select ); |
c193b707 VZ |
1891 | m_key_current=prev.m_pItem; |
1892 | EnsureVisible( prev ); | |
90e58684 | 1893 | break; |
c193b707 | 1894 | } |
978f38c2 VZ |
1895 | } |
1896 | } | |
1897 | if (prev) | |
1898 | { | |
69a282d4 | 1899 | while ( IsExpanded(prev) && HasChildren(prev) ) |
978f38c2 | 1900 | { |
69a282d4 VZ |
1901 | wxTreeItemId child = GetLastChild(prev); |
1902 | if ( child ) | |
1903 | { | |
1904 | prev = child; | |
1905 | } | |
978f38c2 | 1906 | } |
69a282d4 | 1907 | |
88ac883a | 1908 | SelectItem( prev, unselect_others, extended_select ); |
c193b707 | 1909 | m_key_current=prev.m_pItem; |
978f38c2 VZ |
1910 | EnsureVisible( prev ); |
1911 | } | |
1912 | } | |
1913 | break; | |
ef44a621 | 1914 | |
978f38c2 VZ |
1915 | // left arrow goes to the parent |
1916 | case WXK_LEFT: | |
1917 | { | |
1918 | wxTreeItemId prev = GetParent( m_current ); | |
1919 | if (prev) | |
1920 | { | |
1921 | EnsureVisible( prev ); | |
88ac883a | 1922 | SelectItem( prev, unselect_others, extended_select ); |
978f38c2 VZ |
1923 | } |
1924 | } | |
1925 | break; | |
ef44a621 | 1926 | |
978f38c2 VZ |
1927 | case WXK_RIGHT: |
1928 | // this works the same as the down arrow except that we also expand the | |
1929 | // item if it wasn't expanded yet | |
1930 | Expand(m_current); | |
1931 | // fall through | |
1932 | ||
1933 | case WXK_DOWN: | |
ef44a621 | 1934 | { |
91b8de8d | 1935 | if (IsExpanded(m_key_current) && HasChildren(m_key_current)) |
978f38c2 VZ |
1936 | { |
1937 | long cookie = 0; | |
91b8de8d | 1938 | wxTreeItemId child = GetFirstChild( m_key_current, cookie ); |
88ac883a | 1939 | SelectItem( child, unselect_others, extended_select ); |
c193b707 | 1940 | m_key_current=child.m_pItem; |
978f38c2 VZ |
1941 | EnsureVisible( child ); |
1942 | } | |
1943 | else | |
1944 | { | |
91b8de8d | 1945 | wxTreeItemId next = GetNextSibling( m_key_current ); |
b62c3631 RR |
1946 | // if (next == 0) |
1947 | if (!next) | |
978f38c2 | 1948 | { |
91b8de8d | 1949 | wxTreeItemId current = m_key_current; |
978f38c2 VZ |
1950 | while (current && !next) |
1951 | { | |
1952 | current = GetParent( current ); | |
1953 | if (current) next = GetNextSibling( current ); | |
1954 | } | |
1955 | } | |
b62c3631 RR |
1956 | // if (next != 0) |
1957 | if (next) | |
978f38c2 | 1958 | { |
88ac883a | 1959 | SelectItem( next, unselect_others, extended_select ); |
c193b707 | 1960 | m_key_current=next.m_pItem; |
978f38c2 VZ |
1961 | EnsureVisible( next ); |
1962 | } | |
1963 | } | |
ef44a621 | 1964 | } |
978f38c2 | 1965 | break; |
ef44a621 | 1966 | |
978f38c2 VZ |
1967 | // <End> selects the last visible tree item |
1968 | case WXK_END: | |
1969 | { | |
1970 | wxTreeItemId last = GetRootItem(); | |
1971 | ||
1972 | while ( last.IsOk() && IsExpanded(last) ) | |
1973 | { | |
1974 | wxTreeItemId lastChild = GetLastChild(last); | |
1975 | ||
1976 | // it may happen if the item was expanded but then all of | |
1977 | // its children have been deleted - so IsExpanded() returned | |
1978 | // TRUE, but GetLastChild() returned invalid item | |
1979 | if ( !lastChild ) | |
1980 | break; | |
1981 | ||
1982 | last = lastChild; | |
1983 | } | |
1984 | ||
1985 | if ( last.IsOk() ) | |
1986 | { | |
1987 | EnsureVisible( last ); | |
88ac883a | 1988 | SelectItem( last, unselect_others, extended_select ); |
978f38c2 VZ |
1989 | } |
1990 | } | |
1991 | break; | |
1992 | ||
1993 | // <Home> selects the root item | |
1994 | case WXK_HOME: | |
1995 | { | |
1996 | wxTreeItemId prev = GetRootItem(); | |
1997 | if (prev) | |
1998 | { | |
1999 | EnsureVisible( prev ); | |
88ac883a | 2000 | SelectItem( prev, unselect_others, extended_select ); |
978f38c2 VZ |
2001 | } |
2002 | } | |
2003 | break; | |
2004 | ||
2005 | default: | |
2006 | event.Skip(); | |
2007 | } | |
edaa81ae | 2008 | } |
c801d85f | 2009 | |
91b8de8d | 2010 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) |
4f22cf8d | 2011 | { |
dc6c62a9 RR |
2012 | // We have to call this here because the label in |
2013 | // question might just have been added and no screen | |
2014 | // update taken place. | |
2015 | if (m_dirty) wxYield(); | |
2016 | ||
67a7abf7 VZ |
2017 | wxClientDC dc(this); |
2018 | PrepareDC(dc); | |
06b466c7 VZ |
2019 | wxCoord x = dc.DeviceToLogicalX( point.x ); |
2020 | wxCoord y = dc.DeviceToLogicalY( point.y ); | |
91b8de8d RR |
2021 | int w, h; |
2022 | GetSize(&w, &h); | |
2023 | ||
2024 | flags=0; | |
2025 | if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT; | |
2026 | if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT; | |
2027 | if (point.y<0) flags|=wxTREE_HITTEST_ABOVE; | |
2028 | if (point.y>h) flags|=wxTREE_HITTEST_BELOW; | |
d3a9f4af | 2029 | |
91b8de8d | 2030 | return m_anchor->HitTest( wxPoint(x, y), this, flags); |
4f22cf8d RR |
2031 | } |
2032 | ||
e179bd65 RR |
2033 | /* **** */ |
2034 | ||
2035 | void wxTreeCtrl::Edit( const wxTreeItemId& item ) | |
2036 | { | |
2037 | if (!item.IsOk()) return; | |
2038 | ||
2039 | m_currentEdit = item.m_pItem; | |
9dfbf520 | 2040 | |
e179bd65 RR |
2041 | wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() ); |
2042 | te.m_item = m_currentEdit; | |
2043 | te.SetEventObject( this ); | |
2044 | GetEventHandler()->ProcessEvent( te ); | |
2045 | ||
2046 | if (!te.IsAllowed()) return; | |
8dc99046 | 2047 | |
dc6c62a9 RR |
2048 | // We have to call this here because the label in |
2049 | // question might just have been added and no screen | |
2050 | // update taken place. | |
2051 | if (m_dirty) wxYield(); | |
9dfbf520 | 2052 | |
e179bd65 RR |
2053 | wxString s = m_currentEdit->GetText(); |
2054 | int x = m_currentEdit->GetX(); | |
2055 | int y = m_currentEdit->GetY(); | |
2056 | int w = m_currentEdit->GetWidth(); | |
2057 | int h = m_currentEdit->GetHeight(); | |
9dfbf520 | 2058 | |
5f1ea0ee RR |
2059 | int image_h = 0; |
2060 | int image_w = 0; | |
8dc99046 VZ |
2061 | |
2062 | int image = m_currentEdit->GetCurrentImage(); | |
2063 | if ( image != NO_IMAGE ) | |
5f1ea0ee | 2064 | { |
8dc99046 | 2065 | m_imageListNormal->GetSize( image, image_w, image_h ); |
5f1ea0ee RR |
2066 | image_w += 4; |
2067 | } | |
2068 | x += image_w; | |
2069 | w -= image_w + 4; // I don't know why +4 is needed | |
e179bd65 RR |
2070 | |
2071 | wxClientDC dc(this); | |
2072 | PrepareDC( dc ); | |
2073 | x = dc.LogicalToDeviceX( x ); | |
2074 | y = dc.LogicalToDeviceY( y ); | |
2075 | ||
2076 | wxTreeTextCtrl *text = new wxTreeTextCtrl( | |
2077 | this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) ); | |
2078 | text->SetFocus(); | |
2079 | } | |
2080 | ||
2081 | void wxTreeCtrl::OnRenameTimer() | |
2082 | { | |
2083 | Edit( m_current ); | |
2084 | } | |
2085 | ||
2086 | void wxTreeCtrl::OnRenameAccept() | |
2087 | { | |
2088 | wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() ); | |
2089 | le.m_item = m_currentEdit; | |
2090 | le.SetEventObject( this ); | |
2091 | le.m_label = m_renameRes; | |
2092 | GetEventHandler()->ProcessEvent( le ); | |
9dfbf520 | 2093 | |
e179bd65 | 2094 | if (!le.IsAllowed()) return; |
9dfbf520 | 2095 | |
5f1ea0ee | 2096 | SetItemText( m_currentEdit, m_renameRes ); |
e179bd65 | 2097 | } |
9dfbf520 | 2098 | |
3db7be80 | 2099 | void wxTreeCtrl::OnMouse( wxMouseEvent &event ) |
c801d85f | 2100 | { |
bbe0af5b | 2101 | if ( !m_anchor ) return; |
978f38c2 | 2102 | |
3dbeaa52 VZ |
2103 | // we process left mouse up event (enables in-place edit), right down |
2104 | // (pass to the user code), left dbl click (activate item) and | |
2105 | // dragging/moving events for items drag-and-drop | |
2106 | if ( !(event.LeftUp() || | |
2107 | event.RightDown() || | |
2108 | event.LeftDClick() || | |
2109 | event.Dragging() || | |
2110 | ((event.Moving() || event.RightUp()) && m_isDragging)) ) | |
2111 | { | |
2112 | event.Skip(); | |
2113 | ||
2114 | return; | |
2115 | } | |
2116 | ||
bbe0af5b RR |
2117 | wxClientDC dc(this); |
2118 | PrepareDC(dc); | |
06b466c7 VZ |
2119 | wxCoord x = dc.DeviceToLogicalX( event.GetX() ); |
2120 | wxCoord y = dc.DeviceToLogicalY( event.GetY() ); | |
29d87bba | 2121 | |
3dbeaa52 | 2122 | int flags = 0; |
91b8de8d | 2123 | wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags); |
3dbeaa52 | 2124 | |
91b8de8d | 2125 | bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON; |
978f38c2 | 2126 | |
3dbeaa52 | 2127 | if ( event.Dragging() && !m_isDragging ) |
bbe0af5b | 2128 | { |
fd9811b1 | 2129 | if (m_dragCount == 0) |
8dc99046 VZ |
2130 | m_dragStart = wxPoint(x,y); |
2131 | ||
fd9811b1 | 2132 | m_dragCount++; |
8dc99046 | 2133 | |
3dbeaa52 VZ |
2134 | if (m_dragCount != 3) |
2135 | { | |
2136 | // wait until user drags a bit further... | |
2137 | return; | |
2138 | } | |
8dc99046 | 2139 | |
3dbeaa52 VZ |
2140 | wxEventType command = event.RightIsDown() |
2141 | ? wxEVT_COMMAND_TREE_BEGIN_RDRAG | |
2142 | : wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
8dc99046 | 2143 | |
fd9811b1 RR |
2144 | wxTreeEvent nevent( command, GetId() ); |
2145 | nevent.m_item = m_current; | |
2146 | nevent.SetEventObject(this); | |
3dbeaa52 VZ |
2147 | |
2148 | // by default the dragging is not supported, the user code must | |
2149 | // explicitly allow the event for it to take place | |
2150 | nevent.Veto(); | |
2151 | ||
2152 | if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() ) | |
2153 | { | |
2154 | // we're going to drag this item | |
2155 | m_isDragging = TRUE; | |
2156 | ||
b3a7510d VZ |
2157 | // remember the old cursor because we will change it while |
2158 | // dragging | |
2159 | m_oldCursor = m_cursor; | |
2160 | ||
2161 | // in a single selection control, hide the selection temporarily | |
2162 | if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) ) | |
2163 | { | |
2164 | m_oldSelection = GetSelection().m_pItem; | |
2165 | ||
2166 | if ( m_oldSelection ) | |
2167 | { | |
2168 | m_oldSelection->SetHilight(FALSE); | |
2169 | RefreshLine(m_oldSelection); | |
2170 | } | |
2171 | } | |
2172 | ||
3dbeaa52 VZ |
2173 | CaptureMouse(); |
2174 | } | |
bbe0af5b | 2175 | } |
3dbeaa52 | 2176 | else if ( event.Moving() ) |
fd9811b1 | 2177 | { |
3dbeaa52 VZ |
2178 | if ( item != m_dropTarget ) |
2179 | { | |
2180 | // unhighlight the previous drop target | |
2181 | DrawDropEffect(m_dropTarget); | |
fd9811b1 | 2182 | |
3dbeaa52 | 2183 | m_dropTarget = item; |
978f38c2 | 2184 | |
3dbeaa52 VZ |
2185 | // highlight the current drop target if any |
2186 | DrawDropEffect(m_dropTarget); | |
fb882e1c | 2187 | |
3dbeaa52 VZ |
2188 | wxYield(); |
2189 | } | |
e179bd65 | 2190 | } |
3dbeaa52 VZ |
2191 | else if ( (event.LeftUp() || event.RightUp()) && m_isDragging ) |
2192 | { | |
2193 | // erase the highlighting | |
2194 | DrawDropEffect(m_dropTarget); | |
9dfbf520 | 2195 | |
3dbeaa52 VZ |
2196 | // generate the drag end event |
2197 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId()); | |
88ac883a | 2198 | |
3dbeaa52 VZ |
2199 | event.m_item = item; |
2200 | event.m_pointDrag = wxPoint(x, y); | |
2201 | event.SetEventObject(this); | |
91b8de8d | 2202 | |
3dbeaa52 | 2203 | (void)GetEventHandler()->ProcessEvent(event); |
29d87bba | 2204 | |
3dbeaa52 VZ |
2205 | m_isDragging = FALSE; |
2206 | m_dropTarget = (wxGenericTreeItem *)NULL; | |
2207 | ||
b3a7510d VZ |
2208 | if ( m_oldSelection ) |
2209 | { | |
2210 | m_oldSelection->SetHilight(TRUE); | |
2211 | RefreshLine(m_oldSelection); | |
2212 | m_oldSelection = (wxGenericTreeItem *)NULL; | |
2213 | } | |
2214 | ||
3dbeaa52 VZ |
2215 | ReleaseMouse(); |
2216 | ||
b3a7510d | 2217 | SetCursor(m_oldCursor); |
3dbeaa52 VZ |
2218 | |
2219 | wxYield(); | |
2220 | } | |
2221 | else | |
bbe0af5b | 2222 | { |
3dbeaa52 VZ |
2223 | // here we process only the messages which happen on tree items |
2224 | ||
2225 | m_dragCount = 0; | |
2226 | ||
2227 | if (item == NULL) return; /* we hit the blank area */ | |
2228 | ||
2229 | if ( event.RightDown() ) | |
2230 | { | |
2231 | wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId()); | |
2232 | nevent.m_item = item; | |
2233 | nevent.m_code = 0; | |
2234 | nevent.SetEventObject(this); | |
2235 | GetEventHandler()->ProcessEvent(nevent); | |
2236 | } | |
2237 | else if ( event.LeftUp() && (item == m_current) && | |
2238 | (flags & wxTREE_HITTEST_ONITEMLABEL) && | |
2239 | HasFlag(wxTR_EDIT_LABELS) ) | |
2240 | { | |
2241 | m_renameTimer->Start( 100, TRUE ); | |
2242 | } | |
2243 | else | |
2244 | { | |
2245 | // how should the selection work for this event? | |
2246 | bool is_multiple, extended_select, unselect_others; | |
2247 | EventFlagsToSelType(GetWindowStyleFlag(), | |
2248 | event.ShiftDown(), | |
2249 | event.ControlDown(), | |
dfc6cd93 | 2250 | is_multiple, extended_select, unselect_others); |
3dbeaa52 VZ |
2251 | |
2252 | if ( onButton ) | |
2253 | { | |
2254 | Toggle( item ); | |
2255 | if ( is_multiple ) | |
2256 | return; | |
2257 | } | |
2258 | ||
2259 | SelectItem(item, unselect_others, extended_select); | |
2260 | ||
2261 | if ( event.LeftDClick() ) | |
2262 | { | |
2263 | wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); | |
2264 | nevent.m_item = item; | |
2265 | nevent.m_code = 0; | |
2266 | nevent.SetEventObject( this ); | |
2267 | GetEventHandler()->ProcessEvent( nevent ); | |
2268 | } | |
2269 | } | |
bbe0af5b | 2270 | } |
edaa81ae | 2271 | } |
c801d85f | 2272 | |
3db7be80 RR |
2273 | void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) ) |
2274 | { | |
bbe0af5b RR |
2275 | /* after all changes have been done to the tree control, |
2276 | * we actually redraw the tree when everything is over */ | |
ef44a621 | 2277 | |
f65635b5 VZ |
2278 | if (!m_dirty) |
2279 | return; | |
ef44a621 | 2280 | |
bbe0af5b | 2281 | m_dirty = FALSE; |
3db7be80 | 2282 | |
bbe0af5b | 2283 | CalculatePositions(); |
91b8de8d | 2284 | Refresh(); |
bbe0af5b | 2285 | AdjustMyScrollbars(); |
3db7be80 RR |
2286 | } |
2287 | ||
91b8de8d | 2288 | void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc ) |
d3a9f4af | 2289 | { |
e06b9569 JS |
2290 | wxCoord text_w = 0; |
2291 | wxCoord text_h = 0; | |
9dfbf520 | 2292 | |
a62867a5 | 2293 | if (item->IsBold()) |
eff869aa | 2294 | dc.SetFont(m_boldFont); |
9dfbf520 | 2295 | |
91b8de8d | 2296 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); |
a62867a5 | 2297 | text_h+=2; |
91b8de8d | 2298 | |
eff869aa RR |
2299 | // restore normal font |
2300 | dc.SetFont( m_normalFont ); | |
9dfbf520 | 2301 | |
91b8de8d RR |
2302 | int image_h = 0; |
2303 | int image_w = 0; | |
8dc99046 VZ |
2304 | int image = item->GetCurrentImage(); |
2305 | if ( image != NO_IMAGE ) | |
91b8de8d | 2306 | { |
8dc99046 | 2307 | m_imageListNormal->GetSize( image, image_w, image_h ); |
91b8de8d RR |
2308 | image_w += 4; |
2309 | } | |
2310 | ||
2311 | int total_h = (image_h > text_h) ? image_h : text_h; | |
2312 | ||
06b466c7 | 2313 | if (total_h < 40) |
f2593d0d | 2314 | total_h += 2; // at least 2 pixels |
06b466c7 | 2315 | else |
f2593d0d | 2316 | total_h += total_h/10; // otherwise 10% extra spacing |
91b8de8d RR |
2317 | |
2318 | item->SetHeight(total_h); | |
6cedba09 VZ |
2319 | if (total_h>m_lineHeight) |
2320 | m_lineHeight=total_h; | |
bbe0af5b | 2321 | |
91b8de8d RR |
2322 | item->SetWidth(image_w+text_w+2); |
2323 | } | |
2324 | ||
2325 | // ----------------------------------------------------------------------------- | |
2326 | // for developper : y is now the top of the level | |
2327 | // not the middle of it ! | |
bbe0af5b | 2328 | void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) |
c801d85f | 2329 | { |
bbe0af5b | 2330 | int horizX = level*m_indent; |
389cdc7a | 2331 | |
91b8de8d | 2332 | CalculateSize( item, dc ); |
d3a9f4af | 2333 | |
91b8de8d | 2334 | // set its position |
cf724bce | 2335 | item->SetX( horizX+m_indent+m_spacing ); |
91b8de8d RR |
2336 | item->SetY( y ); |
2337 | y+=GetLineHeight(item); | |
4c681997 | 2338 | |
bbe0af5b RR |
2339 | if ( !item->IsExpanded() ) |
2340 | { | |
f65635b5 | 2341 | // we dont need to calculate collapsed branches |
bbe0af5b RR |
2342 | return; |
2343 | } | |
389cdc7a | 2344 | |
91b8de8d RR |
2345 | wxArrayGenericTreeItems& children = item->GetChildren(); |
2346 | size_t n, count = children.Count(); | |
2347 | for (n = 0; n < count; ++n ) | |
f2593d0d | 2348 | CalculateLevel( children[n], dc, level+1, y ); // recurse |
edaa81ae | 2349 | } |
c801d85f | 2350 | |
74bedbeb | 2351 | void wxTreeCtrl::CalculatePositions() |
c801d85f | 2352 | { |
bbe0af5b | 2353 | if ( !m_anchor ) return; |
29d87bba | 2354 | |
bbe0af5b RR |
2355 | wxClientDC dc(this); |
2356 | PrepareDC( dc ); | |
29d87bba | 2357 | |
eff869aa | 2358 | dc.SetFont( m_normalFont ); |
29d87bba | 2359 | |
bbe0af5b | 2360 | dc.SetPen( m_dottedPen ); |
91b8de8d RR |
2361 | //if(GetImageList() == NULL) |
2362 | // m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
29d87bba | 2363 | |
8dc99046 | 2364 | int y = 2; |
f65635b5 | 2365 | CalculateLevel( m_anchor, dc, 0, y ); // start recursion |
edaa81ae | 2366 | } |
c801d85f | 2367 | |
f135ff73 | 2368 | void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item) |
c801d85f | 2369 | { |
e6527f9d RR |
2370 | if (m_dirty) return; |
2371 | ||
bbe0af5b RR |
2372 | wxClientDC dc(this); |
2373 | PrepareDC(dc); | |
4832f7c0 | 2374 | |
bbe0af5b RR |
2375 | int cw = 0; |
2376 | int ch = 0; | |
2377 | GetClientSize( &cw, &ch ); | |
4832f7c0 | 2378 | |
bbe0af5b RR |
2379 | wxRect rect; |
2380 | rect.x = dc.LogicalToDeviceX( 0 ); | |
2381 | rect.width = cw; | |
2382 | rect.y = dc.LogicalToDeviceY( item->GetY() ); | |
2383 | rect.height = ch; | |
f135ff73 | 2384 | |
bbe0af5b | 2385 | Refresh( TRUE, &rect ); |
f135ff73 | 2386 | |
bbe0af5b | 2387 | AdjustMyScrollbars(); |
edaa81ae | 2388 | } |
c801d85f KB |
2389 | |
2390 | void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item ) | |
2391 | { | |
e6527f9d RR |
2392 | if (m_dirty) return; |
2393 | ||
bbe0af5b RR |
2394 | wxClientDC dc(this); |
2395 | PrepareDC( dc ); | |
2396 | ||
5391f772 SB |
2397 | int cw = 0; |
2398 | int ch = 0; | |
2399 | GetClientSize( &cw, &ch ); | |
2400 | ||
bbe0af5b | 2401 | wxRect rect; |
5391f772 SB |
2402 | rect.x = dc.LogicalToDeviceX( 0 ); |
2403 | rect.y = dc.LogicalToDeviceY( item->GetY() ); | |
2404 | rect.width = cw; | |
91b8de8d | 2405 | rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6; |
978f38c2 | 2406 | |
bbe0af5b | 2407 | Refresh( TRUE, &rect ); |
edaa81ae | 2408 | } |
c801d85f | 2409 |