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