]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: treectrl.cpp | |
3 | // Purpose: generic tree control implementation | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ) | |
7 | // Id: $Id$ | |
8 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================= | |
13 | // declarations | |
14 | // ============================================================================= | |
15 | ||
16 | // ----------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ----------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "treectrl.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #include "wx/generic/treectrl.h" | |
32 | #include "wx/generic/imaglist.h" | |
33 | #include "wx/settings.h" | |
34 | #include "wx/log.h" | |
35 | #include "wx/intl.h" | |
36 | #include "wx/dynarray.h" | |
37 | #include "wx/dcclient.h" | |
38 | #include "wx/msgdlg.h" | |
39 | ||
40 | // ----------------------------------------------------------------------------- | |
41 | // array types | |
42 | // ----------------------------------------------------------------------------- | |
43 | ||
44 | class WXDLLEXPORT wxGenericTreeItem; | |
45 | ||
46 | WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayTreeItems); | |
47 | ||
48 | // ----------------------------------------------------------------------------- | |
49 | // private classes | |
50 | // ----------------------------------------------------------------------------- | |
51 | ||
52 | // a tree item | |
53 | class WXDLLEXPORT wxGenericTreeItem | |
54 | { | |
55 | public: | |
56 | // ctors & dtor | |
57 | wxGenericTreeItem() { m_data = NULL; } | |
58 | wxGenericTreeItem( wxGenericTreeItem *parent, | |
59 | const wxString& text, | |
60 | wxDC& dc, | |
61 | int image, int selImage, | |
62 | wxTreeItemData *data ); | |
63 | ||
64 | ~wxGenericTreeItem(); | |
65 | ||
66 | // trivial accessors | |
67 | wxArrayTreeItems& GetChildren() { return m_children; } | |
68 | ||
69 | const wxString& GetText() const { return m_text; } | |
70 | int GetImage() const { return m_image; } | |
71 | int GetSelectedImage() const { return m_selImage; } | |
72 | wxTreeItemData *GetData() const { return m_data; } | |
73 | ||
74 | void SetText( const wxString &text, wxDC& dc ); | |
75 | void SetImage(int image) { m_image = image; } | |
76 | void SetSelectedImage(int image) { m_selImage = image; } | |
77 | void SetData(wxTreeItemData *data) { m_data = data; } | |
78 | ||
79 | void SetHasPlus(bool has = TRUE) { m_hasPlus = has; } | |
80 | ||
81 | void SetBold(bool bold) { m_isBold = bold; } | |
82 | ||
83 | int GetX() const { return m_x; } | |
84 | int GetY() const { return m_y; } | |
85 | ||
86 | void SetHeight(int h) { m_height = h; } | |
87 | ||
88 | void SetX(int x) { m_x = x; } | |
89 | void SetY(int y) { m_y = y; } | |
90 | ||
91 | wxGenericTreeItem *GetParent() const { return m_parent; } | |
92 | ||
93 | // operations | |
94 | // deletes all children notifying the treectrl about it if !NULL pointer | |
95 | // given | |
96 | void DeleteChildren(wxTreeCtrl *tree = NULL); | |
97 | // FIXME don't know what is it for | |
98 | void Reset(); | |
99 | ||
100 | // get count of all children (and grand children if 'recursively') | |
101 | size_t GetChildrenCount(bool recursively = TRUE) const; | |
102 | ||
103 | void Insert(wxGenericTreeItem *child, size_t index) | |
104 | { m_children.Insert(child, index); } | |
105 | ||
106 | void SetCross( int x, int y ); | |
107 | void GetSize( int &x, int &y ); | |
108 | ||
109 | // return the item at given position (or NULL if no item), onButton is TRUE | |
110 | // if the point belongs to the item's button, otherwise it lies on the | |
111 | // button's label | |
112 | wxGenericTreeItem *HitTest( const wxPoint& point, bool &onButton ); | |
113 | ||
114 | void Expand() { m_isCollapsed = FALSE; } | |
115 | void Collapse() { m_isCollapsed = TRUE; } | |
116 | ||
117 | void SetHilight( bool set = TRUE ) { m_hasHilight = set; } | |
118 | ||
119 | // status inquiries | |
120 | bool HasChildren() const { return !m_children.IsEmpty(); } | |
121 | bool HasHilight() const { return m_hasHilight; } | |
122 | bool IsExpanded() const { return !m_isCollapsed; } | |
123 | bool HasPlus() const { return m_hasPlus || HasChildren(); } | |
124 | bool IsBold() const { return m_isBold; } | |
125 | ||
126 | private: | |
127 | wxString m_text; | |
128 | ||
129 | int m_image, | |
130 | m_selImage; | |
131 | ||
132 | wxTreeItemData *m_data; | |
133 | ||
134 | // use bitfields to save size | |
135 | int m_isCollapsed :1; | |
136 | int m_hasHilight :1; // same as focused | |
137 | int m_hasPlus :1; // used for item which doesn't have | |
138 | // children but still has a [+] button | |
139 | int m_isBold :1; // render the label in bold font | |
140 | ||
141 | int m_x, m_y; | |
142 | long m_height, m_width; | |
143 | int m_xCross, m_yCross; | |
144 | int m_level; | |
145 | wxArrayTreeItems m_children; | |
146 | wxGenericTreeItem *m_parent; | |
147 | }; | |
148 | ||
149 | // ============================================================================= | |
150 | // implementation | |
151 | // ============================================================================= | |
152 | ||
153 | // ----------------------------------------------------------------------------- | |
154 | // wxTreeEvent | |
155 | // ----------------------------------------------------------------------------- | |
156 | ||
157 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent) | |
158 | ||
159 | wxTreeEvent::wxTreeEvent( wxEventType commandType, int id ) | |
160 | : wxNotifyEvent( commandType, id ) | |
161 | { | |
162 | m_code = 0; | |
163 | m_itemOld = (wxGenericTreeItem *)NULL; | |
164 | } | |
165 | ||
166 | // ----------------------------------------------------------------------------- | |
167 | // wxGenericTreeItem | |
168 | // ----------------------------------------------------------------------------- | |
169 | ||
170 | wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent, | |
171 | const wxString& text, | |
172 | wxDC& dc, | |
173 | int image, int selImage, | |
174 | wxTreeItemData *data) | |
175 | : m_text(text) | |
176 | { | |
177 | m_image = image; | |
178 | m_selImage = selImage; | |
179 | m_data = data; | |
180 | m_x = m_y = 0; | |
181 | m_xCross = m_yCross = 0; | |
182 | ||
183 | m_level = 0; | |
184 | ||
185 | m_isCollapsed = TRUE; | |
186 | m_hasHilight = FALSE; | |
187 | m_hasPlus = FALSE; | |
188 | m_isBold = FALSE; | |
189 | ||
190 | m_parent = parent; | |
191 | ||
192 | dc.GetTextExtent( m_text, &m_width, &m_height ); | |
193 | } | |
194 | ||
195 | wxGenericTreeItem::~wxGenericTreeItem() | |
196 | { | |
197 | delete m_data; | |
198 | ||
199 | wxASSERT_MSG( m_children.IsEmpty(), | |
200 | _T("please call DeleteChildren() before deleting the item") ); | |
201 | } | |
202 | ||
203 | void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree) | |
204 | { | |
205 | size_t count = m_children.Count(); | |
206 | for ( size_t n = 0; n < count; n++ ) | |
207 | { | |
208 | wxGenericTreeItem *child = m_children[n]; | |
209 | if ( tree ) | |
210 | { | |
211 | tree->SendDeleteEvent(child); | |
212 | } | |
213 | ||
214 | child->DeleteChildren(tree); | |
215 | delete child; | |
216 | } | |
217 | ||
218 | m_children.Empty(); | |
219 | } | |
220 | ||
221 | void wxGenericTreeItem::SetText( const wxString &text, wxDC& dc ) | |
222 | { | |
223 | m_text = text; | |
224 | ||
225 | dc.GetTextExtent( m_text, &m_width, &m_height ); | |
226 | } | |
227 | ||
228 | void wxGenericTreeItem::Reset() | |
229 | { | |
230 | m_text.Empty(); | |
231 | m_image = | |
232 | m_selImage = -1; | |
233 | m_data = NULL; | |
234 | m_x = m_y = | |
235 | m_height = m_width = 0; | |
236 | m_xCross = | |
237 | m_yCross = 0; | |
238 | ||
239 | m_level = 0; | |
240 | ||
241 | DeleteChildren(); | |
242 | m_isCollapsed = TRUE; | |
243 | ||
244 | m_parent = (wxGenericTreeItem *)NULL; | |
245 | } | |
246 | ||
247 | size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const | |
248 | { | |
249 | size_t count = m_children.Count(); | |
250 | if ( !recursively ) | |
251 | return count; | |
252 | ||
253 | size_t total = count; | |
254 | for ( size_t n = 0; n < count; n++ ) | |
255 | { | |
256 | total += m_children[n]->GetChildrenCount(); | |
257 | } | |
258 | ||
259 | return total; | |
260 | } | |
261 | ||
262 | void wxGenericTreeItem::SetCross( int x, int y ) | |
263 | { | |
264 | m_xCross = x; | |
265 | m_yCross = y; | |
266 | } | |
267 | ||
268 | void wxGenericTreeItem::GetSize( int &x, int &y ) | |
269 | { | |
270 | if ( y < m_y ) y = m_y; | |
271 | int width = m_x + m_width; | |
272 | if (width > x) x = width; | |
273 | ||
274 | if (IsExpanded()) | |
275 | { | |
276 | size_t count = m_children.Count(); | |
277 | for ( size_t n = 0; n < count; n++ ) | |
278 | { | |
279 | m_children[n]->GetSize( x, y ); | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point, | |
285 | bool &onButton ) | |
286 | { | |
287 | if ((point.y > m_y) && (point.y < m_y + m_height)) | |
288 | { | |
289 | // FIXME why +5? | |
290 | // Because that is the size of the plus sign, RR | |
291 | if ((point.x > m_xCross-5) && (point.x < m_xCross+5) && | |
292 | (point.y > m_yCross-5) && (point.y < m_yCross+5) && | |
293 | (IsExpanded() || HasPlus())) | |
294 | { | |
295 | onButton = TRUE; | |
296 | return this; | |
297 | } | |
298 | ||
299 | /* TODO: we should do a query here like | |
300 | m_imageListNormal->GetSize( item->GetImage(), image_w, image_h ); */ | |
301 | int w = m_width; | |
302 | if (m_image != -1) w += 24; | |
303 | ||
304 | if ((point.x > m_x) && (point.x < m_x+w)) | |
305 | { | |
306 | onButton = FALSE; | |
307 | return this; | |
308 | } | |
309 | } | |
310 | else | |
311 | { | |
312 | if (!m_isCollapsed) | |
313 | { | |
314 | size_t count = m_children.Count(); | |
315 | for ( size_t n = 0; n < count; n++ ) | |
316 | { | |
317 | wxGenericTreeItem *res = m_children[n]->HitTest( point, onButton ); | |
318 | if ( res != NULL ) | |
319 | return res; | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
324 | return NULL; | |
325 | } | |
326 | ||
327 | // ----------------------------------------------------------------------------- | |
328 | // wxTreeCtrl implementation | |
329 | // ----------------------------------------------------------------------------- | |
330 | ||
331 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow) | |
332 | ||
333 | BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow) | |
334 | EVT_PAINT (wxTreeCtrl::OnPaint) | |
335 | EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse) | |
336 | EVT_CHAR (wxTreeCtrl::OnChar) | |
337 | EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus) | |
338 | EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus) | |
339 | EVT_IDLE (wxTreeCtrl::OnIdle) | |
340 | END_EVENT_TABLE() | |
341 | ||
342 | // ----------------------------------------------------------------------------- | |
343 | // construction/destruction | |
344 | // ----------------------------------------------------------------------------- | |
345 | void wxTreeCtrl::Init() | |
346 | { | |
347 | m_current = | |
348 | m_anchor = (wxGenericTreeItem *) NULL; | |
349 | m_hasFocus = FALSE; | |
350 | m_dirty = FALSE; | |
351 | ||
352 | m_xScroll = 0; | |
353 | m_yScroll = 0; | |
354 | m_lineHeight = 10; | |
355 | m_indent = 15; | |
356 | ||
357 | m_hilightBrush = new wxBrush | |
358 | ( | |
359 | wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), | |
360 | wxSOLID | |
361 | ); | |
362 | ||
363 | m_imageListNormal = | |
364 | m_imageListState = (wxImageList *) NULL; | |
365 | ||
366 | m_dragCount = 0; | |
367 | } | |
368 | ||
369 | bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, | |
370 | const wxPoint& pos, const wxSize& size, | |
371 | long style, | |
372 | const wxValidator &validator, | |
373 | const wxString& name ) | |
374 | { | |
375 | Init(); | |
376 | ||
377 | wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name ); | |
378 | ||
379 | SetValidator( validator ); | |
380 | ||
381 | SetBackgroundColour( *wxWHITE ); | |
382 | m_dottedPen = wxPen( *wxBLACK, 0, 0 ); | |
383 | ||
384 | return TRUE; | |
385 | } | |
386 | ||
387 | wxTreeCtrl::~wxTreeCtrl() | |
388 | { | |
389 | wxDELETE( m_hilightBrush ); | |
390 | ||
391 | DeleteAllItems(); | |
392 | } | |
393 | ||
394 | // ----------------------------------------------------------------------------- | |
395 | // accessors | |
396 | // ----------------------------------------------------------------------------- | |
397 | ||
398 | size_t wxTreeCtrl::GetCount() const | |
399 | { | |
400 | return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount(); | |
401 | } | |
402 | ||
403 | void wxTreeCtrl::SetIndent(unsigned int indent) | |
404 | { | |
405 | m_indent = indent; | |
406 | Refresh(); | |
407 | } | |
408 | ||
409 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively) | |
410 | { | |
411 | wxCHECK_MSG( item.IsOk(), 0u, _T("invalid tree item") ); | |
412 | ||
413 | return item.m_pItem->GetChildrenCount(recursively); | |
414 | } | |
415 | ||
416 | // ----------------------------------------------------------------------------- | |
417 | // functions to work with tree items | |
418 | // ----------------------------------------------------------------------------- | |
419 | ||
420 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const | |
421 | { | |
422 | wxCHECK_MSG( item.IsOk(), _T(""), _T("invalid tree item") ); | |
423 | ||
424 | return item.m_pItem->GetText(); | |
425 | } | |
426 | ||
427 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const | |
428 | { | |
429 | wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") ); | |
430 | ||
431 | return item.m_pItem->GetImage(); | |
432 | } | |
433 | ||
434 | int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const | |
435 | { | |
436 | wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") ); | |
437 | ||
438 | return item.m_pItem->GetSelectedImage(); | |
439 | } | |
440 | ||
441 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const | |
442 | { | |
443 | wxCHECK_MSG( item.IsOk(), NULL, _T("invalid tree item") ); | |
444 | ||
445 | return item.m_pItem->GetData(); | |
446 | } | |
447 | ||
448 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) | |
449 | { | |
450 | wxCHECK_RET( item.IsOk(), _T("invalid tree item") ); | |
451 | ||
452 | wxClientDC dc(this); | |
453 | wxGenericTreeItem *pItem = item.m_pItem; | |
454 | pItem->SetText(text, dc); | |
455 | RefreshLine(pItem); | |
456 | } | |
457 | ||
458 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image) | |
459 | { | |
460 | wxCHECK_RET( item.IsOk(), _T("invalid tree item") ); | |
461 | ||
462 | wxGenericTreeItem *pItem = item.m_pItem; | |
463 | pItem->SetImage(image); | |
464 | RefreshLine(pItem); | |
465 | } | |
466 | ||
467 | void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image) | |
468 | { | |
469 | wxCHECK_RET( item.IsOk(), _T("invalid tree item") ); | |
470 | ||
471 | wxGenericTreeItem *pItem = item.m_pItem; | |
472 | pItem->SetSelectedImage(image); | |
473 | RefreshLine(pItem); | |
474 | } | |
475 | ||
476 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) | |
477 | { | |
478 | wxCHECK_RET( item.IsOk(), _T("invalid tree item") ); | |
479 | ||
480 | item.m_pItem->SetData(data); | |
481 | } | |
482 | ||
483 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) | |
484 | { | |
485 | wxCHECK_RET( item.IsOk(), _T("invalid tree item") ); | |
486 | ||
487 | wxGenericTreeItem *pItem = item.m_pItem; | |
488 | pItem->SetHasPlus(has); | |
489 | RefreshLine(pItem); | |
490 | } | |
491 | ||
492 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) | |
493 | { | |
494 | wxCHECK_RET( item.IsOk(), _T("invalid tree item") ); | |
495 | ||
496 | // avoid redrawing the tree if no real change | |
497 | wxGenericTreeItem *pItem = item.m_pItem; | |
498 | if ( pItem->IsBold() != bold ) | |
499 | { | |
500 | pItem->SetBold(bold); | |
501 | RefreshLine(pItem); | |
502 | } | |
503 | } | |
504 | ||
505 | // ----------------------------------------------------------------------------- | |
506 | // item status inquiries | |
507 | // ----------------------------------------------------------------------------- | |
508 | ||
509 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const | |
510 | { | |
511 | wxFAIL_MSG(_T("not implemented")); | |
512 | ||
513 | return TRUE; | |
514 | } | |
515 | ||
516 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const | |
517 | { | |
518 | wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") ); | |
519 | ||
520 | return !item.m_pItem->GetChildren().IsEmpty(); | |
521 | } | |
522 | ||
523 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const | |
524 | { | |
525 | wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") ); | |
526 | ||
527 | return item.m_pItem->IsExpanded(); | |
528 | } | |
529 | ||
530 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const | |
531 | { | |
532 | wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") ); | |
533 | ||
534 | return item.m_pItem->HasHilight(); | |
535 | } | |
536 | ||
537 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const | |
538 | { | |
539 | wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") ); | |
540 | ||
541 | return item.m_pItem->IsBold(); | |
542 | } | |
543 | ||
544 | // ----------------------------------------------------------------------------- | |
545 | // navigation | |
546 | // ----------------------------------------------------------------------------- | |
547 | ||
548 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const | |
549 | { | |
550 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") ); | |
551 | ||
552 | return item.m_pItem->GetParent(); | |
553 | } | |
554 | ||
555 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const | |
556 | { | |
557 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") ); | |
558 | ||
559 | cookie = 0; | |
560 | return GetNextChild(item, cookie); | |
561 | } | |
562 | ||
563 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const | |
564 | { | |
565 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") ); | |
566 | ||
567 | wxArrayTreeItems& children = item.m_pItem->GetChildren(); | |
568 | if ( (size_t)cookie < children.Count() ) | |
569 | { | |
570 | return children.Item(cookie++); | |
571 | } | |
572 | else | |
573 | { | |
574 | // there are no more of them | |
575 | return wxTreeItemId(); | |
576 | } | |
577 | } | |
578 | ||
579 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const | |
580 | { | |
581 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") ); | |
582 | ||
583 | wxArrayTreeItems& children = item.m_pItem->GetChildren(); | |
584 | return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last())); | |
585 | } | |
586 | ||
587 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const | |
588 | { | |
589 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") ); | |
590 | ||
591 | wxGenericTreeItem *i = item.m_pItem; | |
592 | wxGenericTreeItem *parent = i->GetParent(); | |
593 | if ( parent == NULL ) | |
594 | { | |
595 | // root item doesn't have any siblings | |
596 | return wxTreeItemId(); | |
597 | } | |
598 | ||
599 | wxArrayTreeItems& siblings = parent->GetChildren(); | |
600 | int index = siblings.Index(i); | |
601 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? | |
602 | ||
603 | size_t n = (size_t)(index + 1); | |
604 | return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]); | |
605 | } | |
606 | ||
607 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const | |
608 | { | |
609 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") ); | |
610 | ||
611 | wxGenericTreeItem *i = item.m_pItem; | |
612 | wxGenericTreeItem *parent = i->GetParent(); | |
613 | if ( parent == NULL ) | |
614 | { | |
615 | // root item doesn't have any siblings | |
616 | return wxTreeItemId(); | |
617 | } | |
618 | ||
619 | wxArrayTreeItems& siblings = parent->GetChildren(); | |
620 | int index = siblings.Index(i); | |
621 | wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent? | |
622 | ||
623 | return index == 0 ? wxTreeItemId() | |
624 | : wxTreeItemId(siblings[(size_t)(index - 1)]); | |
625 | } | |
626 | ||
627 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const | |
628 | { | |
629 | wxFAIL_MSG(_T("not implemented")); | |
630 | ||
631 | return wxTreeItemId(); | |
632 | } | |
633 | ||
634 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const | |
635 | { | |
636 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") ); | |
637 | ||
638 | wxFAIL_MSG(_T("not implemented")); | |
639 | ||
640 | return wxTreeItemId(); | |
641 | } | |
642 | ||
643 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const | |
644 | { | |
645 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") ); | |
646 | ||
647 | wxFAIL_MSG(_T("not implemented")); | |
648 | ||
649 | return wxTreeItemId(); | |
650 | } | |
651 | ||
652 | // ----------------------------------------------------------------------------- | |
653 | // operations | |
654 | // ----------------------------------------------------------------------------- | |
655 | ||
656 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId, | |
657 | size_t previous, | |
658 | const wxString& text, | |
659 | int image, int selImage, | |
660 | wxTreeItemData *data) | |
661 | { | |
662 | wxGenericTreeItem *parent = parentId.m_pItem; | |
663 | if ( !parent ) | |
664 | { | |
665 | // should we give a warning here? | |
666 | return AddRoot(text, image, selImage, data); | |
667 | } | |
668 | ||
669 | wxClientDC dc(this); | |
670 | wxGenericTreeItem *item = new wxGenericTreeItem(parent, | |
671 | text, dc, | |
672 | image, selImage, | |
673 | data); | |
674 | ||
675 | if ( data != NULL ) | |
676 | { | |
677 | data->m_pItem = item; | |
678 | } | |
679 | ||
680 | parent->Insert( item, previous ); | |
681 | ||
682 | m_dirty = TRUE; | |
683 | ||
684 | return item; | |
685 | } | |
686 | ||
687 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, | |
688 | int image, int selImage, | |
689 | wxTreeItemData *data) | |
690 | { | |
691 | wxCHECK_MSG( !m_anchor, wxTreeItemId(), _T("tree can have only one root") ); | |
692 | ||
693 | wxClientDC dc(this); | |
694 | m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc, | |
695 | image, selImage, data); | |
696 | if ( data != NULL ) | |
697 | { | |
698 | data->m_pItem = m_anchor; | |
699 | } | |
700 | ||
701 | AdjustMyScrollbars(); | |
702 | Refresh(); | |
703 | ||
704 | return m_anchor; | |
705 | } | |
706 | ||
707 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, | |
708 | const wxString& text, | |
709 | int image, int selImage, | |
710 | wxTreeItemData *data) | |
711 | { | |
712 | return DoInsertItem(parent, 0u, text, image, selImage, data); | |
713 | } | |
714 | ||
715 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId, | |
716 | const wxTreeItemId& idPrevious, | |
717 | const wxString& text, | |
718 | int image, int selImage, | |
719 | wxTreeItemData *data) | |
720 | { | |
721 | wxGenericTreeItem *parent = parentId.m_pItem; | |
722 | if ( !parent ) | |
723 | { | |
724 | // should we give a warning here? | |
725 | return AddRoot(text, image, selImage, data); | |
726 | } | |
727 | ||
728 | int index = parent->GetChildren().Index(idPrevious.m_pItem); | |
729 | wxASSERT_MSG( index != wxNOT_FOUND, | |
730 | _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") ); | |
731 | return DoInsertItem(parentId, (size_t)index, text, image, selImage, data); | |
732 | } | |
733 | ||
734 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId, | |
735 | const wxString& text, | |
736 | int image, int selImage, | |
737 | wxTreeItemData *data) | |
738 | { | |
739 | wxGenericTreeItem *parent = parentId.m_pItem; | |
740 | if ( !parent ) | |
741 | { | |
742 | // should we give a warning here? | |
743 | return AddRoot(text, image, selImage, data); | |
744 | } | |
745 | ||
746 | return DoInsertItem(parent, parent->GetChildren().Count(), text, | |
747 | image, selImage, data); | |
748 | } | |
749 | ||
750 | void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item) | |
751 | { | |
752 | wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() ); | |
753 | event.m_item = item; | |
754 | event.SetEventObject( this ); | |
755 | ProcessEvent( event ); | |
756 | } | |
757 | ||
758 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId) | |
759 | { | |
760 | wxGenericTreeItem *item = itemId.m_pItem; | |
761 | item->DeleteChildren(this); | |
762 | ||
763 | m_dirty = TRUE; | |
764 | } | |
765 | ||
766 | void wxTreeCtrl::Delete(const wxTreeItemId& itemId) | |
767 | { | |
768 | wxGenericTreeItem *item = itemId.m_pItem; | |
769 | wxGenericTreeItem *parent = item->GetParent(); | |
770 | ||
771 | if ( parent ) | |
772 | { | |
773 | parent->GetChildren().Remove(item); | |
774 | } | |
775 | ||
776 | item->DeleteChildren(this); | |
777 | SendDeleteEvent(item); | |
778 | delete item; | |
779 | ||
780 | m_dirty = TRUE; | |
781 | } | |
782 | ||
783 | void wxTreeCtrl::DeleteAllItems() | |
784 | { | |
785 | if ( m_anchor ) | |
786 | { | |
787 | m_anchor->DeleteChildren(this); | |
788 | delete m_anchor; | |
789 | ||
790 | m_anchor = NULL; | |
791 | ||
792 | m_dirty = TRUE; | |
793 | } | |
794 | } | |
795 | ||
796 | void wxTreeCtrl::Expand(const wxTreeItemId& itemId) | |
797 | { | |
798 | wxGenericTreeItem *item = itemId.m_pItem; | |
799 | ||
800 | if ( !item->HasPlus() ) | |
801 | return; | |
802 | ||
803 | if ( item->IsExpanded() ) | |
804 | return; | |
805 | ||
806 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() ); | |
807 | event.m_item = item; | |
808 | event.SetEventObject( this ); | |
809 | if ( ProcessEvent( event ) && event.m_code ) | |
810 | { | |
811 | // cancelled by program | |
812 | return; | |
813 | } | |
814 | ||
815 | item->Expand(); | |
816 | CalculatePositions(); | |
817 | ||
818 | RefreshSubtree(item); | |
819 | ||
820 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED); | |
821 | ProcessEvent( event ); | |
822 | } | |
823 | ||
824 | void wxTreeCtrl::Collapse(const wxTreeItemId& itemId) | |
825 | { | |
826 | wxGenericTreeItem *item = itemId.m_pItem; | |
827 | ||
828 | if ( !item->IsExpanded() ) | |
829 | return; | |
830 | ||
831 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() ); | |
832 | event.m_item = item; | |
833 | event.SetEventObject( this ); | |
834 | if ( ProcessEvent( event ) && event.m_code ) | |
835 | { | |
836 | // cancelled by program | |
837 | return; | |
838 | } | |
839 | ||
840 | item->Collapse(); | |
841 | ||
842 | wxArrayTreeItems& children = item->GetChildren(); | |
843 | size_t count = children.Count(); | |
844 | for ( size_t n = 0; n < count; n++ ) | |
845 | { | |
846 | Collapse(children[n]); | |
847 | } | |
848 | ||
849 | CalculatePositions(); | |
850 | ||
851 | RefreshSubtree(item); | |
852 | ||
853 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED); | |
854 | ProcessEvent( event ); | |
855 | } | |
856 | ||
857 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) | |
858 | { | |
859 | Collapse(item); | |
860 | DeleteChildren(item); | |
861 | } | |
862 | ||
863 | void wxTreeCtrl::Toggle(const wxTreeItemId& itemId) | |
864 | { | |
865 | wxGenericTreeItem *item = itemId.m_pItem; | |
866 | ||
867 | if ( item->IsExpanded() ) | |
868 | Collapse(itemId); | |
869 | else | |
870 | Expand(itemId); | |
871 | } | |
872 | ||
873 | void wxTreeCtrl::Unselect() | |
874 | { | |
875 | if ( m_current ) | |
876 | { | |
877 | m_current->SetHilight( FALSE ); | |
878 | RefreshLine( m_current ); | |
879 | } | |
880 | } | |
881 | ||
882 | void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId) | |
883 | { | |
884 | wxGenericTreeItem *item = itemId.m_pItem; | |
885 | ||
886 | if ( m_current != item ) | |
887 | { | |
888 | wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() ); | |
889 | event.m_item = item; | |
890 | event.m_itemOld = m_current; | |
891 | event.SetEventObject( this ); | |
892 | if ( GetEventHandler()->ProcessEvent( event ) && event.WasVetoed() ) | |
893 | return; | |
894 | ||
895 | if ( m_current ) | |
896 | { | |
897 | m_current->SetHilight( FALSE ); | |
898 | RefreshLine( m_current ); | |
899 | } | |
900 | ||
901 | m_current = item; | |
902 | m_current->SetHilight( TRUE ); | |
903 | RefreshLine( m_current ); | |
904 | ||
905 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); | |
906 | GetEventHandler()->ProcessEvent( event ); | |
907 | } | |
908 | } | |
909 | ||
910 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) | |
911 | { | |
912 | wxGenericTreeItem *gitem = item.m_pItem; | |
913 | ||
914 | // first expand all parent branches | |
915 | wxGenericTreeItem *parent = gitem->GetParent(); | |
916 | while ( parent && !parent->IsExpanded() ) | |
917 | { | |
918 | Expand(parent); | |
919 | ||
920 | parent = parent->GetParent(); | |
921 | } | |
922 | ||
923 | // now scroll to the item | |
924 | int item_y = gitem->GetY(); | |
925 | ||
926 | int start_x = 0; | |
927 | int start_y = 0; | |
928 | ViewStart( &start_x, &start_y ); | |
929 | start_y *= 10; | |
930 | ||
931 | int client_h = 0; | |
932 | int client_w = 0; | |
933 | GetClientSize( &client_w, &client_h ); | |
934 | ||
935 | if (item_y < start_y+3) | |
936 | { | |
937 | int x = 0; | |
938 | int y = 0; | |
939 | m_anchor->GetSize( x, y ); | |
940 | y += 2*m_lineHeight; | |
941 | int x_pos = GetScrollPos( wxHORIZONTAL ); | |
942 | SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 ); | |
943 | } | |
944 | else if (item_y > start_y+client_h-16) | |
945 | { | |
946 | int x = 0; | |
947 | int y = 0; | |
948 | m_anchor->GetSize( x, y ); | |
949 | y += 2*m_lineHeight; | |
950 | int x_pos = GetScrollPos( wxHORIZONTAL ); | |
951 | SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 ); | |
952 | } | |
953 | } | |
954 | ||
955 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& WXUNUSED(item)) | |
956 | { | |
957 | wxFAIL_MSG(_T("not implemented")); | |
958 | } | |
959 | ||
960 | wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item), | |
961 | wxClassInfo* WXUNUSED(textCtrlClass) ) | |
962 | { | |
963 | wxFAIL_MSG(_T("not implemented")); | |
964 | ||
965 | return (wxTextCtrl*)NULL; | |
966 | } | |
967 | ||
968 | wxTextCtrl *wxTreeCtrl::GetEditControl() const | |
969 | { | |
970 | wxFAIL_MSG(_T("not implemented")); | |
971 | ||
972 | return (wxTextCtrl*)NULL; | |
973 | } | |
974 | ||
975 | void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool WXUNUSED(discardChanges)) | |
976 | { | |
977 | wxFAIL_MSG(_T("not implemented")); | |
978 | } | |
979 | ||
980 | // FIXME: tree sorting functions are not reentrant and not MT-safe! | |
981 | static wxTreeCtrl *s_treeBeingSorted = NULL; | |
982 | ||
983 | static int tree_ctrl_compare_func(wxGenericTreeItem **item1, | |
984 | wxGenericTreeItem **item2) | |
985 | { | |
986 | wxCHECK_MSG( s_treeBeingSorted, 0, _T("bug in wxTreeCtrl::SortChildren()") ); | |
987 | ||
988 | return s_treeBeingSorted->OnCompareItems(*item1, *item2); | |
989 | } | |
990 | ||
991 | int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1, | |
992 | const wxTreeItemId& item2) | |
993 | { | |
994 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); | |
995 | } | |
996 | ||
997 | void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId) | |
998 | { | |
999 | wxCHECK_RET( itemId.IsOk(), _T("invalid tree item") ); | |
1000 | ||
1001 | wxGenericTreeItem *item = itemId.m_pItem; | |
1002 | ||
1003 | wxCHECK_RET( !s_treeBeingSorted, | |
1004 | _T("wxTreeCtrl::SortChildren is not reentrant") ); | |
1005 | ||
1006 | wxArrayTreeItems& children = item->GetChildren(); | |
1007 | if ( children.Count() > 1 ) | |
1008 | { | |
1009 | s_treeBeingSorted = this; | |
1010 | children.Sort(tree_ctrl_compare_func); | |
1011 | s_treeBeingSorted = NULL; | |
1012 | ||
1013 | m_dirty = TRUE; | |
1014 | } | |
1015 | //else: don't make the tree dirty as nothing changed | |
1016 | } | |
1017 | ||
1018 | wxImageList *wxTreeCtrl::GetImageList() const | |
1019 | { | |
1020 | return m_imageListNormal; | |
1021 | } | |
1022 | ||
1023 | wxImageList *wxTreeCtrl::GetStateImageList() const | |
1024 | { | |
1025 | return m_imageListState; | |
1026 | } | |
1027 | ||
1028 | void wxTreeCtrl::SetImageList(wxImageList *imageList) | |
1029 | { | |
1030 | m_imageListNormal = imageList; | |
1031 | // calculate a m_lineHeight value from the image sizes | |
1032 | wxPaintDC dc(this); | |
1033 | PrepareDC( dc ); | |
1034 | m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
1035 | int | |
1036 | width = 0, | |
1037 | height = 0, | |
1038 | n = m_imageListNormal->GetImageCount(); | |
1039 | for(int i = 0; i < n ; i++) | |
1040 | { | |
1041 | m_imageListNormal->GetSize(i, width, height); | |
1042 | height += height/5; //20% extra spacing | |
1043 | if(height > m_lineHeight) m_lineHeight = height; | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) | |
1048 | { | |
1049 | m_imageListState = imageList; | |
1050 | } | |
1051 | ||
1052 | // ----------------------------------------------------------------------------- | |
1053 | // helpers | |
1054 | // ----------------------------------------------------------------------------- | |
1055 | ||
1056 | void wxTreeCtrl::AdjustMyScrollbars() | |
1057 | { | |
1058 | if (m_anchor) | |
1059 | { | |
1060 | int x = 0; | |
1061 | int y = 0; | |
1062 | m_anchor->GetSize( x, y ); | |
1063 | y += 2*m_lineHeight; | |
1064 | int x_pos = GetScrollPos( wxHORIZONTAL ); | |
1065 | int y_pos = GetScrollPos( wxVERTICAL ); | |
1066 | SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos ); | |
1067 | } | |
1068 | else | |
1069 | { | |
1070 | SetScrollbars( 0, 0, 0, 0 ); | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc) | |
1075 | { | |
1076 | // render bold items in bold | |
1077 | wxFont fontOld; | |
1078 | wxFont fontNew; | |
1079 | ||
1080 | if (item->IsBold()) | |
1081 | { | |
1082 | fontOld = dc.GetFont(); | |
1083 | if (fontOld.Ok()) | |
1084 | { | |
1085 | // VZ: is there any better way to make a bold variant of old font? | |
1086 | fontNew = wxFont( fontOld.GetPointSize(), | |
1087 | fontOld.GetFamily(), | |
1088 | fontOld.GetStyle(), | |
1089 | wxBOLD, | |
1090 | fontOld.GetUnderlined()); | |
1091 | dc.SetFont(fontNew); | |
1092 | } | |
1093 | else | |
1094 | { | |
1095 | wxFAIL_MSG(_T("wxDC::GetFont() failed!")); | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | long text_w = 0; | |
1100 | long text_h = 0; | |
1101 | dc.GetTextExtent( item->GetText(), &text_w, &text_h ); | |
1102 | ||
1103 | int image_h = 0; | |
1104 | int image_w = 0; | |
1105 | if ((item->IsExpanded()) && (item->GetSelectedImage() != -1)) | |
1106 | { | |
1107 | m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h ); | |
1108 | image_w += 4; | |
1109 | } | |
1110 | else if (item->GetImage() != -1) | |
1111 | { | |
1112 | m_imageListNormal->GetSize( item->GetImage(), image_w, image_h ); | |
1113 | image_w += 4; | |
1114 | } | |
1115 | ||
1116 | int total_h = (image_h > text_h) ? image_h : text_h; | |
1117 | if(m_lineHeight > total_h) total_h = m_lineHeight; | |
1118 | ||
1119 | dc.DrawRectangle( item->GetX()-2, item->GetY(), image_w+text_w+4, total_h ); | |
1120 | ||
1121 | if ((item->IsExpanded()) && (item->GetSelectedImage() != -1)) | |
1122 | { | |
1123 | dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h ); | |
1124 | m_imageListNormal->Draw( item->GetSelectedImage(), dc, | |
1125 | item->GetX(), | |
1126 | item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0), | |
1127 | wxIMAGELIST_DRAW_TRANSPARENT ); | |
1128 | dc.DestroyClippingRegion(); | |
1129 | } | |
1130 | else if (item->GetImage() != -1) | |
1131 | { | |
1132 | dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h ); | |
1133 | m_imageListNormal->Draw( item->GetImage(), dc, | |
1134 | item->GetX(), | |
1135 | item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0), | |
1136 | wxIMAGELIST_DRAW_TRANSPARENT ); | |
1137 | dc.DestroyClippingRegion(); | |
1138 | } | |
1139 | ||
1140 | dc.SetBackgroundMode(wxTRANSPARENT); | |
1141 | dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY() | |
1142 | + ((total_h > text_h) ? (total_h - text_h)/2 : 0)); | |
1143 | ||
1144 | // restore normal font for bold items | |
1145 | if (fontOld.Ok()) | |
1146 | { | |
1147 | dc.SetFont( fontOld); | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) | |
1152 | { | |
1153 | int horizX = level*m_indent; | |
1154 | ||
1155 | item->SetX( horizX+33 ); | |
1156 | item->SetY( y-m_lineHeight/2 ); | |
1157 | item->SetHeight( m_lineHeight ); | |
1158 | ||
1159 | item->SetCross( horizX+15, y ); | |
1160 | ||
1161 | int oldY = y; | |
1162 | ||
1163 | int exposed_x = dc.LogicalToDeviceX( 0 ); | |
1164 | int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 ); | |
1165 | ||
1166 | if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much | |
1167 | { | |
1168 | int startX = horizX; | |
1169 | int endX = horizX + 10; | |
1170 | ||
1171 | if (!item->HasChildren()) endX += 20; | |
1172 | ||
1173 | dc.DrawLine( startX, y, endX, y ); | |
1174 | ||
1175 | if (item->HasPlus()) | |
1176 | { | |
1177 | dc.DrawLine( horizX+20, y, horizX+30, y ); | |
1178 | dc.SetPen( *wxGREY_PEN ); | |
1179 | dc.SetBrush( *wxWHITE_BRUSH ); | |
1180 | dc.DrawRectangle( horizX+10, y-4, 11, 9 ); | |
1181 | dc.SetPen( *wxBLACK_PEN ); | |
1182 | dc.DrawLine( horizX+13, y, horizX+18, y ); | |
1183 | ||
1184 | if (!item->IsExpanded()) | |
1185 | { | |
1186 | dc.DrawLine( horizX+15, y-2, horizX+15, y+3 ); | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | if (item->HasHilight()) | |
1191 | { | |
1192 | dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) ); | |
1193 | ||
1194 | dc.SetBrush( *m_hilightBrush ); | |
1195 | ||
1196 | if (m_hasFocus) | |
1197 | dc.SetPen( *wxBLACK_PEN ); | |
1198 | else | |
1199 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1200 | ||
1201 | PaintItem(item, dc); | |
1202 | ||
1203 | dc.SetPen( *wxBLACK_PEN ); | |
1204 | dc.SetTextForeground( *wxBLACK ); | |
1205 | dc.SetBrush( *wxWHITE_BRUSH ); | |
1206 | } | |
1207 | else | |
1208 | { | |
1209 | dc.SetBrush( *wxWHITE_BRUSH ); | |
1210 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1211 | ||
1212 | PaintItem(item, dc); | |
1213 | ||
1214 | dc.SetPen( *wxBLACK_PEN ); | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | if (item->IsExpanded()) | |
1219 | { | |
1220 | int semiOldY = y; | |
1221 | ||
1222 | wxArrayTreeItems& children = item->GetChildren(); | |
1223 | size_t count = children.Count(); | |
1224 | for ( size_t n = 0; n < count; n++ ) | |
1225 | { | |
1226 | y += m_lineHeight; | |
1227 | semiOldY = y; | |
1228 | PaintLevel( children[n], dc, level+1, y ); | |
1229 | } | |
1230 | ||
1231 | // it may happen that the item is expanded but has no items (when you | |
1232 | // delete all its children for example) - don't draw the vertical line | |
1233 | // in this case | |
1234 | if (count > 0) | |
1235 | dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY ); | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | // ----------------------------------------------------------------------------- | |
1240 | // wxWindows callbacks | |
1241 | // ----------------------------------------------------------------------------- | |
1242 | ||
1243 | void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
1244 | { | |
1245 | if ( !m_anchor ) | |
1246 | return; | |
1247 | ||
1248 | wxPaintDC dc(this); | |
1249 | PrepareDC( dc ); | |
1250 | ||
1251 | dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) ); | |
1252 | ||
1253 | dc.SetPen( m_dottedPen ); | |
1254 | if(GetImageList() == NULL) | |
1255 | m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
1256 | ||
1257 | int y = m_lineHeight / 2 + 2; | |
1258 | PaintLevel( m_anchor, dc, 0, y ); | |
1259 | } | |
1260 | ||
1261 | void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) ) | |
1262 | { | |
1263 | m_hasFocus = TRUE; | |
1264 | ||
1265 | if (m_current) RefreshLine( m_current ); | |
1266 | } | |
1267 | ||
1268 | void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) ) | |
1269 | { | |
1270 | m_hasFocus = FALSE; | |
1271 | ||
1272 | if (m_current) RefreshLine( m_current ); | |
1273 | } | |
1274 | ||
1275 | void wxTreeCtrl::OnChar( wxKeyEvent &event ) | |
1276 | { | |
1277 | wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() ); | |
1278 | te.m_code = event.KeyCode(); | |
1279 | te.SetEventObject( this ); | |
1280 | GetEventHandler()->ProcessEvent( te ); | |
1281 | ||
1282 | if (m_current == 0) | |
1283 | { | |
1284 | event.Skip(); | |
1285 | return; | |
1286 | } | |
1287 | ||
1288 | switch (event.KeyCode()) | |
1289 | { | |
1290 | case '+': | |
1291 | case WXK_ADD: | |
1292 | if (m_current->HasPlus() && !IsExpanded(m_current)) | |
1293 | { | |
1294 | Expand(m_current); | |
1295 | } | |
1296 | break; | |
1297 | ||
1298 | case '-': | |
1299 | case WXK_SUBTRACT: | |
1300 | if (IsExpanded(m_current)) | |
1301 | { | |
1302 | Collapse(m_current); | |
1303 | } | |
1304 | break; | |
1305 | ||
1306 | case '*': | |
1307 | case WXK_MULTIPLY: | |
1308 | Toggle(m_current); | |
1309 | break; | |
1310 | ||
1311 | case ' ': | |
1312 | case WXK_RETURN: | |
1313 | { | |
1314 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); | |
1315 | event.m_item = m_current; | |
1316 | event.m_code = 0; | |
1317 | event.SetEventObject( this ); | |
1318 | GetEventHandler()->ProcessEvent( event ); | |
1319 | } | |
1320 | break; | |
1321 | ||
1322 | // up goes to the previous sibling or to the last of its children if | |
1323 | // it's expanded | |
1324 | case WXK_UP: | |
1325 | { | |
1326 | wxTreeItemId prev = GetPrevSibling( m_current ); | |
1327 | if (!prev) | |
1328 | { | |
1329 | prev = GetParent( m_current ); | |
1330 | long cockie = 0; | |
1331 | wxTreeItemId current = m_current; | |
1332 | if (current == GetFirstChild( prev, cockie )) | |
1333 | { | |
1334 | // otherwise we return to where we came from | |
1335 | SelectItem( prev ); | |
1336 | EnsureVisible( prev ); | |
1337 | break; | |
1338 | } | |
1339 | } | |
1340 | if (prev) | |
1341 | { | |
1342 | while ( IsExpanded(prev) && HasChildren(prev) ) | |
1343 | { | |
1344 | wxTreeItemId child = GetLastChild(prev); | |
1345 | if ( child ) | |
1346 | { | |
1347 | prev = child; | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | SelectItem( prev ); | |
1352 | EnsureVisible( prev ); | |
1353 | } | |
1354 | } | |
1355 | break; | |
1356 | ||
1357 | // left arrow goes to the parent | |
1358 | case WXK_LEFT: | |
1359 | { | |
1360 | wxTreeItemId prev = GetParent( m_current ); | |
1361 | if (prev) | |
1362 | { | |
1363 | EnsureVisible( prev ); | |
1364 | SelectItem( prev ); | |
1365 | } | |
1366 | } | |
1367 | break; | |
1368 | ||
1369 | case WXK_RIGHT: | |
1370 | // this works the same as the down arrow except that we also expand the | |
1371 | // item if it wasn't expanded yet | |
1372 | Expand(m_current); | |
1373 | // fall through | |
1374 | ||
1375 | case WXK_DOWN: | |
1376 | { | |
1377 | if (IsExpanded(m_current) && HasChildren(m_current)) | |
1378 | { | |
1379 | long cookie = 0; | |
1380 | wxTreeItemId child = GetFirstChild( m_current, cookie ); | |
1381 | SelectItem( child ); | |
1382 | EnsureVisible( child ); | |
1383 | } | |
1384 | else | |
1385 | { | |
1386 | wxTreeItemId next = GetNextSibling( m_current ); | |
1387 | if (next == 0) | |
1388 | { | |
1389 | wxTreeItemId current = m_current; | |
1390 | while (current && !next) | |
1391 | { | |
1392 | current = GetParent( current ); | |
1393 | if (current) next = GetNextSibling( current ); | |
1394 | } | |
1395 | } | |
1396 | if (next != 0) | |
1397 | { | |
1398 | SelectItem( next ); | |
1399 | EnsureVisible( next ); | |
1400 | } | |
1401 | } | |
1402 | } | |
1403 | break; | |
1404 | ||
1405 | // <End> selects the last visible tree item | |
1406 | case WXK_END: | |
1407 | { | |
1408 | wxTreeItemId last = GetRootItem(); | |
1409 | ||
1410 | while ( last.IsOk() && IsExpanded(last) ) | |
1411 | { | |
1412 | wxTreeItemId lastChild = GetLastChild(last); | |
1413 | ||
1414 | // it may happen if the item was expanded but then all of | |
1415 | // its children have been deleted - so IsExpanded() returned | |
1416 | // TRUE, but GetLastChild() returned invalid item | |
1417 | if ( !lastChild ) | |
1418 | break; | |
1419 | ||
1420 | last = lastChild; | |
1421 | } | |
1422 | ||
1423 | if ( last.IsOk() ) | |
1424 | { | |
1425 | EnsureVisible( last ); | |
1426 | SelectItem( last ); | |
1427 | } | |
1428 | } | |
1429 | break; | |
1430 | ||
1431 | // <Home> selects the root item | |
1432 | case WXK_HOME: | |
1433 | { | |
1434 | wxTreeItemId prev = GetRootItem(); | |
1435 | if (prev) | |
1436 | { | |
1437 | EnsureVisible( prev ); | |
1438 | SelectItem( prev ); | |
1439 | } | |
1440 | } | |
1441 | break; | |
1442 | ||
1443 | default: | |
1444 | event.Skip(); | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags)) | |
1449 | { | |
1450 | wxClientDC dc(this); | |
1451 | PrepareDC(dc); | |
1452 | long x = dc.DeviceToLogicalX( (long)point.x ); | |
1453 | long y = dc.DeviceToLogicalY( (long)point.y ); | |
1454 | ||
1455 | bool onButton = FALSE; | |
1456 | return m_anchor->HitTest( wxPoint(x, y), onButton ); | |
1457 | } | |
1458 | ||
1459 | void wxTreeCtrl::OnMouse( wxMouseEvent &event ) | |
1460 | { | |
1461 | if (!event.LeftIsDown()) m_dragCount = 0; | |
1462 | ||
1463 | if ( !(event.LeftDown() || event.LeftDClick() || event.Dragging()) ) return; | |
1464 | ||
1465 | if ( !m_anchor ) return; | |
1466 | ||
1467 | wxClientDC dc(this); | |
1468 | PrepareDC(dc); | |
1469 | long x = dc.DeviceToLogicalX( (long)event.GetX() ); | |
1470 | long y = dc.DeviceToLogicalY( (long)event.GetY() ); | |
1471 | ||
1472 | bool onButton = FALSE; | |
1473 | wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton ); | |
1474 | ||
1475 | if (item == NULL) return; /* we hit the blank area */ | |
1476 | ||
1477 | if (event.Dragging()) | |
1478 | { | |
1479 | if (m_dragCount == 2) /* small drag latency (3?) */ | |
1480 | { | |
1481 | m_dragCount = 0; | |
1482 | ||
1483 | wxTreeEvent nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG, GetId()); | |
1484 | nevent.m_item = m_current; | |
1485 | nevent.SetEventObject(this); | |
1486 | GetEventHandler()->ProcessEvent(nevent); | |
1487 | } | |
1488 | else | |
1489 | { | |
1490 | m_dragCount++; | |
1491 | } | |
1492 | return; | |
1493 | } | |
1494 | ||
1495 | if (!IsSelected(item)) | |
1496 | SelectItem(item); /* we dont support multiple selections, BTW */ | |
1497 | ||
1498 | if (event.LeftDClick()) | |
1499 | { | |
1500 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() ); | |
1501 | event.m_item = item; | |
1502 | event.m_code = 0; | |
1503 | event.SetEventObject( this ); | |
1504 | GetEventHandler()->ProcessEvent( event ); | |
1505 | } | |
1506 | ||
1507 | if (onButton) | |
1508 | { | |
1509 | Toggle( item ); | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) ) | |
1514 | { | |
1515 | /* after all changes have been done to the tree control, | |
1516 | * we actually redraw the tree when everything is over */ | |
1517 | ||
1518 | if (!m_dirty) | |
1519 | return; | |
1520 | ||
1521 | m_dirty = FALSE; | |
1522 | ||
1523 | CalculatePositions(); | |
1524 | ||
1525 | AdjustMyScrollbars(); | |
1526 | } | |
1527 | ||
1528 | // ----------------------------------------------------------------------------- | |
1529 | ||
1530 | void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ) | |
1531 | { | |
1532 | int horizX = level*m_indent; | |
1533 | ||
1534 | item->SetX( horizX+33 ); | |
1535 | item->SetY( y-m_lineHeight/2 ); | |
1536 | item->SetHeight( m_lineHeight ); | |
1537 | ||
1538 | if ( !item->IsExpanded() ) | |
1539 | { | |
1540 | // we dont need to calculate collapsed branches | |
1541 | return; | |
1542 | } | |
1543 | ||
1544 | wxArrayTreeItems& children = item->GetChildren(); | |
1545 | size_t count = children.Count(); | |
1546 | for ( size_t n = 0; n < count; n++ ) | |
1547 | { | |
1548 | y += m_lineHeight; | |
1549 | CalculateLevel( children[n], dc, level+1, y ); // recurse | |
1550 | } | |
1551 | } | |
1552 | ||
1553 | void wxTreeCtrl::CalculatePositions() | |
1554 | { | |
1555 | if ( !m_anchor ) return; | |
1556 | ||
1557 | wxClientDC dc(this); | |
1558 | PrepareDC( dc ); | |
1559 | ||
1560 | dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) ); | |
1561 | ||
1562 | dc.SetPen( m_dottedPen ); | |
1563 | if(GetImageList() == NULL) | |
1564 | m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
1565 | ||
1566 | int y = m_lineHeight / 2 + 2; | |
1567 | CalculateLevel( m_anchor, dc, 0, y ); // start recursion | |
1568 | } | |
1569 | ||
1570 | void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item) | |
1571 | { | |
1572 | wxClientDC dc(this); | |
1573 | PrepareDC(dc); | |
1574 | ||
1575 | int cw = 0; | |
1576 | int ch = 0; | |
1577 | GetClientSize( &cw, &ch ); | |
1578 | ||
1579 | wxRect rect; | |
1580 | rect.x = dc.LogicalToDeviceX( 0 ); | |
1581 | rect.width = cw; | |
1582 | rect.y = dc.LogicalToDeviceY( item->GetY() ); | |
1583 | rect.height = ch; | |
1584 | ||
1585 | Refresh( TRUE, &rect ); | |
1586 | ||
1587 | AdjustMyScrollbars(); | |
1588 | } | |
1589 | ||
1590 | void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item ) | |
1591 | { | |
1592 | wxClientDC dc(this); | |
1593 | PrepareDC( dc ); | |
1594 | ||
1595 | wxRect rect; | |
1596 | rect.x = dc.LogicalToDeviceX( item->GetX() - 2 ); | |
1597 | rect.y = dc.LogicalToDeviceY( item->GetY() - 2 ); | |
1598 | rect.width = 1000; | |
1599 | rect.height = dc.GetCharHeight() + 6; | |
1600 | ||
1601 | Refresh( TRUE, &rect ); | |
1602 | } | |
1603 |