]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: treectrl.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: $Id$ | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "treectrl.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/treectrl.h" | |
16 | #include "wx/settings.h" | |
17 | #include "wx/log.h" | |
18 | #include <wx/intl.h> | |
19 | ||
20 | //----------------------------------------------------------------------------- | |
21 | // wxTreeItem | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | IMPLEMENT_DYNAMIC_CLASS(wxTreeItem, wxObject) | |
25 | ||
26 | wxTreeItem::wxTreeItem() | |
27 | { | |
28 | m_mask = 0; | |
29 | m_itemId = 0; | |
30 | m_state = 0; | |
31 | m_stateMask = 0; | |
32 | m_image = -1; | |
33 | m_selectedImage = -1; | |
34 | m_children = 0; | |
35 | m_data = 0; | |
36 | } | |
37 | ||
38 | //----------------------------------------------------------------------------- | |
39 | // wxTreeEvent | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent,wxCommandEvent) | |
43 | ||
44 | wxTreeEvent::wxTreeEvent( wxEventType commandType, int id ) : | |
45 | wxCommandEvent( commandType, id ) | |
46 | { | |
47 | m_code = 0; | |
48 | m_oldItem = 0; | |
49 | } | |
50 | ||
51 | //----------------------------------------------------------------------------- | |
52 | // wxGenericTreeItem | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
55 | IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeItem,wxObject) | |
56 | ||
57 | wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent ) | |
58 | { | |
59 | Reset(); | |
60 | m_parent = parent; | |
61 | m_hasHilight = FALSE; | |
62 | } | |
63 | ||
64 | wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent, const wxTreeItem& item, wxDC *dc ) | |
65 | { | |
66 | Reset(); | |
67 | SetItem( item, dc ); | |
68 | m_parent = parent; | |
69 | m_hasHilight = FALSE; | |
70 | } | |
71 | ||
72 | void wxGenericTreeItem::SetItem( const wxTreeItem &item, wxDC *dc ) | |
73 | { | |
74 | if ((item.m_mask & wxTREE_MASK_HANDLE) == wxTREE_MASK_HANDLE) | |
75 | m_itemId = item.m_itemId; | |
76 | if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE) | |
77 | m_state = item.m_state; | |
78 | if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT) | |
79 | m_text = item.m_text; | |
80 | if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE) | |
81 | m_image = item.m_image; | |
82 | if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE) | |
83 | m_selectedImage = item.m_selectedImage; | |
84 | if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN) | |
85 | m_hasChildren = (item.m_children > 0); | |
86 | if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA) | |
87 | m_data = item.m_data; | |
88 | long lw = 0; | |
89 | long lh = 0; | |
90 | dc->GetTextExtent( m_text, &lw, &lh ); | |
91 | m_width = lw; | |
92 | m_height = lh; | |
93 | } | |
94 | ||
95 | void wxGenericTreeItem::SetText( const wxString &text, wxDC *dc ) | |
96 | { | |
97 | m_text = text; | |
98 | long lw = 0; | |
99 | long lh = 0; | |
100 | dc->GetTextExtent( m_text, &lw, &lh ); | |
101 | m_width = lw; | |
102 | m_height = lh; | |
103 | } | |
104 | ||
105 | void wxGenericTreeItem::Reset() | |
106 | { | |
107 | m_itemId = -1; | |
108 | m_state = 0; | |
109 | m_text = ""; | |
110 | m_image = -1; | |
111 | m_selectedImage = -1; | |
112 | // m_children = 0; | |
113 | m_hasChildren = FALSE; | |
114 | m_data = 0; | |
115 | m_x = 0; | |
116 | m_y = 0; | |
117 | m_height = 0; | |
118 | m_width = 0; | |
119 | m_xCross = 0; | |
120 | m_yCross = 0; | |
121 | m_level = 0; | |
122 | m_children.DeleteContents( TRUE ); | |
123 | m_isCollapsed = TRUE; | |
124 | m_parent = (wxGenericTreeItem *) NULL; | |
125 | } | |
126 | ||
127 | void wxGenericTreeItem::GetItem( wxTreeItem &item ) const | |
128 | { | |
129 | if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE) | |
130 | item.m_state = m_state; | |
131 | if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT) | |
132 | item.m_text = m_text; | |
133 | if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE) | |
134 | item.m_image = m_image; | |
135 | if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE) | |
136 | item.m_selectedImage = m_selectedImage; | |
137 | if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN) | |
138 | item.m_children = (int)m_hasChildren; | |
139 | if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA) | |
140 | item.m_data = m_data; | |
141 | } | |
142 | ||
143 | bool wxGenericTreeItem::HasChildren() | |
144 | { | |
145 | return m_hasChildren; | |
146 | } | |
147 | ||
148 | bool wxGenericTreeItem::HasPlus() | |
149 | { | |
150 | if ( !HasChildren() ) | |
151 | return FALSE; | |
152 | ||
153 | return !IsExpanded(); | |
154 | } | |
155 | ||
156 | int wxGenericTreeItem::NumberOfVisibleDescendents() | |
157 | { | |
158 | int ret = m_children.Number(); | |
159 | wxNode *node = m_children.First(); | |
160 | while (node) | |
161 | { | |
162 | wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data(); | |
163 | ret += item->NumberOfVisibleDescendents(); | |
164 | node = node->Next(); | |
165 | } | |
166 | return ret; | |
167 | } | |
168 | ||
169 | int wxGenericTreeItem::NumberOfVisibleChildren() | |
170 | { | |
171 | return m_isCollapsed ? 0 : m_children.Number(); | |
172 | } | |
173 | ||
174 | wxGenericTreeItem *wxGenericTreeItem::FindItem( long itemId ) const | |
175 | { | |
176 | if (m_itemId == itemId) return (wxGenericTreeItem*)(this); | |
177 | wxNode *node = m_children.First(); | |
178 | while (node) | |
179 | { | |
180 | wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data(); | |
181 | wxGenericTreeItem *res = item->FindItem( itemId ); | |
182 | if (res) return (wxGenericTreeItem*)(res); | |
183 | node = node->Next(); | |
184 | } | |
185 | return (wxGenericTreeItem *) NULL; | |
186 | } | |
187 | ||
188 | void wxGenericTreeItem::AddChild( wxGenericTreeItem *child ) | |
189 | { | |
190 | m_children.Append( child ); | |
191 | } | |
192 | ||
193 | void wxGenericTreeItem::SetCross( int x, int y ) | |
194 | { | |
195 | m_xCross = x; | |
196 | m_yCross = y; | |
197 | } | |
198 | ||
199 | void wxGenericTreeItem::GetSize( int &x, int &y ) | |
200 | { | |
201 | if (y < m_y + 10) y = m_y +10; | |
202 | int width = m_x + m_width; | |
203 | if (width > x) x = width; | |
204 | wxNode *node = m_children.First(); | |
205 | while (node) | |
206 | { | |
207 | wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data(); | |
208 | item->GetSize( x, y ); | |
209 | node = node->Next(); | |
210 | } | |
211 | } | |
212 | ||
213 | long wxGenericTreeItem::HitTest( const wxPoint& point, int &flags ) | |
214 | { | |
215 | if ((point.y > m_y) && (point.y < m_y+m_height)) | |
216 | { | |
217 | if ((point.x > m_xCross-5) && | |
218 | (point.x < m_xCross+5) && | |
219 | (point.y > m_yCross-5) && | |
220 | (point.y < m_yCross+5) && | |
221 | (m_hasChildren)) | |
222 | { | |
223 | flags = wxTREE_HITTEST_ONITEMBUTTON; | |
224 | return m_itemId; | |
225 | } | |
226 | if ((point.x > m_x) && (point.x < m_x+m_width)) | |
227 | { | |
228 | flags = wxTREE_HITTEST_ONITEMLABEL; | |
229 | return m_itemId; | |
230 | } | |
231 | if (point.x > m_x) | |
232 | { | |
233 | flags = wxTREE_HITTEST_ONITEMRIGHT; | |
234 | return m_itemId; | |
235 | } | |
236 | flags = wxTREE_HITTEST_ONITEMINDENT; | |
237 | return m_itemId; | |
238 | } | |
239 | else | |
240 | { | |
241 | if (!m_isCollapsed) | |
242 | { | |
243 | wxNode *node = m_children.First(); | |
244 | while (node) | |
245 | { | |
246 | wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data(); | |
247 | long res = child->HitTest( point, flags ); | |
248 | if (res != -1) return res; | |
249 | node = node->Next(); | |
250 | } | |
251 | } | |
252 | } | |
253 | return -1; | |
254 | } | |
255 | ||
256 | void wxGenericTreeItem::PrepareEvent( wxTreeEvent &event ) | |
257 | { | |
258 | event.m_item.m_itemId = m_itemId; | |
259 | event.m_item.m_state = m_state; | |
260 | event.m_item.m_text = m_text; | |
261 | event.m_item.m_image = m_image; | |
262 | event.m_item.m_selectedImage = m_selectedImage; | |
263 | event.m_item.m_children = (int)m_hasChildren; | |
264 | event.m_item.m_data = m_data; | |
265 | event.m_oldItem = 0; | |
266 | event.m_code = 0; | |
267 | event.m_pointDrag.x = 0; | |
268 | event.m_pointDrag.y = 0; | |
269 | } | |
270 | ||
271 | void wxGenericTreeItem::SendKeyDown( wxWindow *target ) | |
272 | { | |
273 | wxTreeEvent event( wxEVT_COMMAND_TREE_KEY_DOWN, target->GetId() ); | |
274 | PrepareEvent( event ); | |
275 | event.SetEventObject( target ); | |
276 | target->ProcessEvent( event ); | |
277 | } | |
278 | ||
279 | void wxGenericTreeItem::SendSelected( wxWindow *target ) | |
280 | { | |
281 | wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGED, target->GetId() ); | |
282 | PrepareEvent( event ); | |
283 | event.SetEventObject( target ); | |
284 | target->ProcessEvent( event ); | |
285 | } | |
286 | ||
287 | void wxGenericTreeItem::SendDelete( wxWindow *target ) | |
288 | { | |
289 | wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, target->GetId() ); | |
290 | PrepareEvent( event ); | |
291 | event.SetEventObject( target ); | |
292 | target->ProcessEvent( event ); | |
293 | } | |
294 | ||
295 | void wxGenericTreeItem::SendExpand( wxWindow *target ) | |
296 | { | |
297 | m_isCollapsed = FALSE; | |
298 | ||
299 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, target->GetId() ); | |
300 | event.SetEventObject( target ); | |
301 | PrepareEvent( event ); | |
302 | target->ProcessEvent( event ); | |
303 | ||
304 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED); | |
305 | PrepareEvent( event ); | |
306 | target->ProcessEvent( event ); | |
307 | } | |
308 | ||
309 | void wxGenericTreeItem::SendCollapse( wxWindow *target ) | |
310 | { | |
311 | m_isCollapsed = TRUE; | |
312 | ||
313 | wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, target->GetId() ); | |
314 | event.SetEventObject( target ); | |
315 | PrepareEvent( event ); | |
316 | target->ProcessEvent( event ); | |
317 | ||
318 | event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED); | |
319 | PrepareEvent( event ); | |
320 | target->ProcessEvent( event ); | |
321 | } | |
322 | ||
323 | void wxGenericTreeItem::SetHilight( bool set ) | |
324 | { | |
325 | m_hasHilight = set; | |
326 | } | |
327 | ||
328 | bool wxGenericTreeItem::HasHilight() | |
329 | { | |
330 | return m_hasHilight; | |
331 | } | |
332 | ||
333 | //----------------------------------------------------------------------------- | |
334 | // wxTreeCtrl | |
335 | //----------------------------------------------------------------------------- | |
336 | ||
337 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl,wxScrolledWindow) | |
338 | ||
339 | BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow) | |
340 | EVT_PAINT (wxTreeCtrl::OnPaint) | |
341 | EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse) | |
342 | EVT_CHAR (wxTreeCtrl::OnChar) | |
343 | EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus) | |
344 | EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus) | |
345 | END_EVENT_TABLE() | |
346 | ||
347 | wxTreeCtrl::wxTreeCtrl() | |
348 | { | |
349 | m_current = (wxGenericTreeItem *) NULL; | |
350 | m_anchor = (wxGenericTreeItem *) NULL; | |
351 | m_hasFocus = FALSE; | |
352 | m_xScroll = 0; | |
353 | m_yScroll = 0; | |
354 | m_lastId = 0; | |
355 | m_lineHeight = 10; | |
356 | m_indent = 15; | |
357 | m_isCreated = FALSE; | |
358 | m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID ); | |
359 | m_imageList = (wxImageList *) NULL; | |
360 | m_smallImageList = (wxImageList *) NULL; | |
361 | } | |
362 | ||
363 | wxTreeCtrl::wxTreeCtrl(wxWindow *parent, wxWindowID id, | |
364 | const wxPoint& pos, const wxSize& size, | |
365 | long style, const wxString& name ) | |
366 | { | |
367 | m_current = (wxGenericTreeItem *) NULL; | |
368 | m_anchor = (wxGenericTreeItem *) NULL; | |
369 | m_hasFocus = FALSE; | |
370 | m_xScroll = 0; | |
371 | m_yScroll = 0; | |
372 | m_lastId = 0; | |
373 | m_lineHeight = 10; | |
374 | m_indent = 15; | |
375 | m_isCreated = FALSE; | |
376 | m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID ); | |
377 | m_imageList = (wxImageList *) NULL; | |
378 | m_smallImageList = (wxImageList *) NULL; | |
379 | Create( parent, id, pos, size, style, name ); | |
380 | } | |
381 | ||
382 | wxTreeCtrl::~wxTreeCtrl() | |
383 | { | |
384 | } | |
385 | ||
386 | bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, | |
387 | const wxPoint& pos, const wxSize& size, | |
388 | long style, const wxString& name ) | |
389 | { | |
390 | wxScrolledWindow::Create( parent, id, pos, size, style, name ); | |
391 | SetBackgroundColour( *wxWHITE ); | |
392 | m_dottedPen = wxPen( *wxBLACK, 0, 0 ); | |
393 | return TRUE; | |
394 | } | |
395 | ||
396 | int wxTreeCtrl::GetCount() const | |
397 | { | |
398 | if (!m_anchor) return 0; | |
399 | return m_anchor->NumberOfVisibleDescendents(); | |
400 | } | |
401 | ||
402 | long wxTreeCtrl::InsertItem( long parent, const wxString& label, int image, | |
403 | int selImage, long WXUNUSED(insertAfter) ) | |
404 | { | |
405 | wxGenericTreeItem *p = (wxGenericTreeItem *) NULL; | |
406 | if (parent == 0) | |
407 | { | |
408 | if (m_anchor) return -1; | |
409 | } | |
410 | else | |
411 | { | |
412 | p = FindItem( parent ); | |
413 | if (!p) return -1; | |
414 | } | |
415 | wxTreeItem item; | |
416 | m_lastId++; | |
417 | item.m_mask = wxTREE_MASK_HANDLE; | |
418 | item.m_itemId = m_lastId; | |
419 | if (!label.IsNull() || (label == "")) | |
420 | { | |
421 | item.m_text = label; | |
422 | item.m_mask |= wxTREE_MASK_TEXT; | |
423 | } | |
424 | if (image >= 0) | |
425 | { | |
426 | item.m_image = image; | |
427 | item.m_mask |= wxTREE_MASK_IMAGE; | |
428 | } | |
429 | if (selImage >= 0) | |
430 | { | |
431 | item.m_selectedImage = selImage; | |
432 | item.m_mask |= wxTREE_MASK_SELECTED_IMAGE; | |
433 | } | |
434 | ||
435 | wxClientDC dc(this); | |
436 | wxGenericTreeItem *new_child = new wxGenericTreeItem( p, item, &dc ); | |
437 | if (p) | |
438 | p->AddChild( new_child ); | |
439 | else | |
440 | m_anchor = new_child; | |
441 | ||
442 | if (p) | |
443 | { | |
444 | CalculatePositions(); | |
445 | ||
446 | if (!p->HasChildren()) p->m_hasChildren = TRUE; | |
447 | ||
448 | int ch = 0; | |
449 | GetClientSize( (int *) NULL, &ch ); | |
450 | ||
451 | PrepareDC( dc ); | |
452 | ||
453 | wxRectangle rect; | |
454 | rect.x = dc.LogicalToDeviceX( 0 ); | |
455 | rect.y = 0; | |
456 | rect.width = 10000; | |
457 | rect.height = ch; | |
458 | ||
459 | if (p->m_children.Number() == 1) | |
460 | { | |
461 | rect.y = dc.LogicalToDeviceY( p->m_y ); | |
462 | } | |
463 | else | |
464 | { | |
465 | wxNode *node = p->m_children.Member( new_child )->Previous(); | |
466 | wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data(); | |
467 | rect.y = dc.LogicalToDeviceY( last_child->m_y ); | |
468 | } | |
469 | ||
470 | AdjustMyScrollbars(); | |
471 | ||
472 | if (rect.height > 0) Refresh( FALSE, &rect); | |
473 | } | |
474 | else | |
475 | { | |
476 | AdjustMyScrollbars(); | |
477 | ||
478 | Refresh(); | |
479 | } | |
480 | ||
481 | return m_lastId; | |
482 | } | |
483 | ||
484 | long wxTreeCtrl::InsertItem( long parent, wxTreeItem &info, long WXUNUSED(insertAfter) ) | |
485 | { | |
486 | int oldMask = info.m_mask; | |
487 | wxGenericTreeItem *p = (wxGenericTreeItem *) NULL; | |
488 | if (parent == 0) | |
489 | { | |
490 | if (m_anchor) return -1; | |
491 | } | |
492 | else | |
493 | { | |
494 | p = FindItem( parent ); | |
495 | if (!p) | |
496 | { | |
497 | printf( "TreeItem not found.\n" ); | |
498 | return -1; | |
499 | } | |
500 | } | |
501 | long ret = 0; | |
502 | if ((info.m_mask & wxTREE_MASK_HANDLE) == 0) | |
503 | { | |
504 | m_lastId++; | |
505 | info.m_itemId = m_lastId; | |
506 | info.m_mask |= wxTREE_MASK_HANDLE; | |
507 | ret = m_lastId; | |
508 | } | |
509 | else | |
510 | { | |
511 | ret = info.m_itemId; | |
512 | } | |
513 | ||
514 | wxClientDC dc(this); | |
515 | wxGenericTreeItem *new_child = new wxGenericTreeItem( p, info, &dc ); | |
516 | if (p) | |
517 | p->AddChild( new_child ); | |
518 | else | |
519 | m_anchor = new_child; | |
520 | ||
521 | if (p) | |
522 | { | |
523 | CalculatePositions(); | |
524 | ||
525 | if (!p->HasChildren()) p->m_hasChildren = TRUE; | |
526 | ||
527 | int ch = 0; | |
528 | GetClientSize( (int *) NULL, &ch ); | |
529 | ||
530 | PrepareDC( dc ); | |
531 | ||
532 | wxRectangle rect; | |
533 | rect.x = dc.LogicalToDeviceX( 0 ); | |
534 | rect.y = 0; | |
535 | rect.width = 10000; | |
536 | rect.height = ch; | |
537 | ||
538 | if (p->m_children.Number() == 1) | |
539 | { | |
540 | rect.y = dc.LogicalToDeviceY( p->m_y ); | |
541 | } | |
542 | else | |
543 | { | |
544 | wxNode *node = p->m_children.Member( new_child )->Previous(); | |
545 | wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data(); | |
546 | rect.y = dc.LogicalToDeviceY( last_child->m_y ); | |
547 | } | |
548 | ||
549 | AdjustMyScrollbars(); | |
550 | ||
551 | if (rect.height > 0) Refresh( FALSE, &rect); | |
552 | } | |
553 | else | |
554 | { | |
555 | AdjustMyScrollbars(); | |
556 | ||
557 | Refresh(); | |
558 | } | |
559 | ||
560 | info.m_mask = oldMask; | |
561 | return ret; | |
562 | } | |
563 | ||
564 | bool wxTreeCtrl::ExpandItem( long item, int action ) | |
565 | { | |
566 | wxGenericTreeItem *i = FindItem( item ); | |
567 | if (!i) | |
568 | return FALSE; | |
569 | ||
570 | switch (action) | |
571 | { | |
572 | case wxTREE_EXPAND_EXPAND: | |
573 | { | |
574 | i->SendExpand( this ); | |
575 | break; | |
576 | } | |
577 | ||
578 | case wxTREE_EXPAND_COLLAPSE_RESET: | |
579 | case wxTREE_EXPAND_COLLAPSE: | |
580 | { | |
581 | wxNode *node = i->m_children.First(); | |
582 | while (node) | |
583 | { | |
584 | wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data(); | |
585 | if ( child->IsExpanded() ) | |
586 | ExpandItem( child->m_itemId, wxTREE_EXPAND_COLLAPSE ); | |
587 | node = node->Next(); | |
588 | } | |
589 | ||
590 | CalculatePositions(); | |
591 | ||
592 | i->SendCollapse( this ); | |
593 | break; | |
594 | } | |
595 | ||
596 | case wxTREE_EXPAND_TOGGLE: | |
597 | { | |
598 | if ( i->IsExpanded() ) | |
599 | ExpandItem( item, wxTREE_EXPAND_COLLAPSE ); | |
600 | else | |
601 | ExpandItem( item, wxTREE_EXPAND_EXPAND ); | |
602 | return TRUE; | |
603 | } | |
604 | } | |
605 | ||
606 | wxClientDC dc(this); | |
607 | PrepareDC(dc); | |
608 | ||
609 | int cw = 0; | |
610 | int ch = 0; | |
611 | GetClientSize( &cw, &ch ); | |
612 | ||
613 | wxRect rect; | |
614 | rect.x = dc.LogicalToDeviceX( 0 ); | |
615 | rect.width = cw; | |
616 | rect.y = dc.LogicalToDeviceY( i->m_y ); | |
617 | rect.height = ch; | |
618 | Refresh( TRUE, &rect ); | |
619 | ||
620 | AdjustMyScrollbars(); | |
621 | ||
622 | return TRUE; | |
623 | } | |
624 | ||
625 | void wxTreeCtrl::DeleteItem( long item ) | |
626 | { | |
627 | wxGenericTreeItem *pItem = FindItem( item ); | |
628 | wxCHECK_RET( pItem != NULL, _("wxTreeCtrl::DeleteItem: no such pItem.") ); | |
629 | ||
630 | pItem->m_parent->m_children.DeleteObject(pItem); | |
631 | ||
632 | Refresh(); | |
633 | } | |
634 | ||
635 | void wxTreeCtrl::DeleteChildren( long item ) | |
636 | { | |
637 | wxGenericTreeItem *pItem = FindItem( item ); | |
638 | wxCHECK_RET( pItem != NULL, _("wxTreeCtrl::DeleteChildren: no such pItem.") ); | |
639 | ||
640 | pItem->m_children.Clear(); | |
641 | ||
642 | Refresh(); | |
643 | } | |
644 | ||
645 | bool wxTreeCtrl::DeleteAllItems() | |
646 | { | |
647 | delete m_anchor; | |
648 | m_anchor = (wxGenericTreeItem *) NULL; | |
649 | Refresh(); | |
650 | return TRUE; | |
651 | } | |
652 | ||
653 | bool wxTreeCtrl::GetItem( wxTreeItem &info ) const | |
654 | { | |
655 | wxGenericTreeItem *i = FindItem( info.m_itemId ); | |
656 | if (!i) return FALSE; | |
657 | i->GetItem( info ); | |
658 | return TRUE; | |
659 | } | |
660 | ||
661 | long wxTreeCtrl::GetItemData( long item ) const | |
662 | { | |
663 | wxGenericTreeItem *i = FindItem( item ); | |
664 | if (!i) return 0; | |
665 | return i->m_data; | |
666 | } | |
667 | ||
668 | wxString wxTreeCtrl::GetItemText( long item ) const | |
669 | { | |
670 | wxGenericTreeItem *i = FindItem( item ); | |
671 | if (!i) return ""; | |
672 | return i->m_text; | |
673 | } | |
674 | ||
675 | int wxTreeCtrl::GetItemImage(long item) const | |
676 | { | |
677 | wxGenericTreeItem *i = FindItem( item ); | |
678 | return i == 0 ? -1 : i->GetImage(); | |
679 | } | |
680 | ||
681 | long wxTreeCtrl::GetParent( long item ) const | |
682 | { | |
683 | wxGenericTreeItem *i = FindItem( item ); | |
684 | if (!i) return -1; | |
685 | i = i->m_parent; | |
686 | if (!i) return -1; | |
687 | return i->m_parent->m_itemId; | |
688 | } | |
689 | ||
690 | long wxTreeCtrl::GetRootItem() const | |
691 | { | |
692 | if (m_anchor) return m_anchor->m_itemId; | |
693 | return -1; | |
694 | } | |
695 | ||
696 | long wxTreeCtrl::GetChild( long item ) const | |
697 | { | |
698 | wxGenericTreeItem *i = FindItem( item ); | |
699 | if (!i) return -1; | |
700 | if (!i->HasChildren()) return -1; | |
701 | wxNode *node = i->m_children.First(); | |
702 | i = (wxGenericTreeItem *)node->Data(); | |
703 | return i->m_itemId; | |
704 | } | |
705 | ||
706 | long wxTreeCtrl::GetNextItem( long item, int code ) const | |
707 | { | |
708 | switch (code) | |
709 | { | |
710 | case wxTREE_NEXT_CARET: return GetSelection(); | |
711 | case wxTREE_NEXT_CHILD: return GetChild( item ); | |
712 | case wxTREE_NEXT_ROOT: return GetRootItem(); | |
713 | case wxTREE_NEXT_PARENT: return GetParent( item ); | |
714 | case wxTREE_NEXT_NEXT: | |
715 | { | |
716 | wxGenericTreeItem *i = FindItem( item ); | |
717 | if (!i) return -1; | |
718 | if (i->m_parent) | |
719 | { | |
720 | wxNode *node = i->m_parent->m_children.Member( i ); | |
721 | if (!node) return -1; | |
722 | node = node->Next(); | |
723 | if (!node) return -1; | |
724 | i = (wxGenericTreeItem *)node->Data(); | |
725 | return i->m_itemId; | |
726 | } | |
727 | break; | |
728 | } | |
729 | case wxTREE_NEXT_PREVIOUS: | |
730 | { | |
731 | wxGenericTreeItem *i = FindItem( item ); | |
732 | if (!i) return -1; | |
733 | if (i->m_parent) | |
734 | { | |
735 | wxNode *node = i->m_parent->m_children.Member( i ); | |
736 | if (!node) return -1; | |
737 | node = node->Previous(); | |
738 | if (!node) return -1; | |
739 | i = (wxGenericTreeItem *)node->Data(); | |
740 | return i->m_itemId; | |
741 | } | |
742 | break; | |
743 | } | |
744 | } | |
745 | return -1; | |
746 | } | |
747 | ||
748 | long wxTreeCtrl::GetSelection() const | |
749 | { | |
750 | return m_current ? m_current->GetItemId() : -1; | |
751 | } | |
752 | ||
753 | bool wxTreeCtrl::SelectItem( long itemId ) | |
754 | { | |
755 | wxGenericTreeItem *pItem = FindItem(itemId); | |
756 | if ( !pItem ) { | |
757 | wxLogDebug(_("Can't select an item %d which doesn't exist."), itemId); | |
758 | ||
759 | return FALSE; | |
760 | } | |
761 | ||
762 | SelectItem(pItem); | |
763 | ||
764 | return TRUE; | |
765 | } | |
766 | ||
767 | void wxTreeCtrl::SelectItem(wxGenericTreeItem *item) | |
768 | { | |
769 | if (m_current != item) | |
770 | { | |
771 | if (m_current) | |
772 | { | |
773 | m_current->SetHilight( FALSE ); | |
774 | RefreshLine( m_current ); | |
775 | } | |
776 | m_current = item; | |
777 | m_current->SetHilight( TRUE ); | |
778 | RefreshLine( m_current ); | |
779 | ||
780 | m_current->SendSelected( this ); | |
781 | } | |
782 | } | |
783 | ||
784 | bool wxTreeCtrl::ItemHasChildren( long item ) const | |
785 | { | |
786 | wxGenericTreeItem *i = FindItem( item ); | |
787 | if (!i) return FALSE; | |
788 | return i->m_hasChildren; | |
789 | } | |
790 | ||
791 | void wxTreeCtrl::SetIndent( int indent ) | |
792 | { | |
793 | m_indent = indent; | |
794 | Refresh(); | |
795 | } | |
796 | ||
797 | int wxTreeCtrl::GetIndent() const | |
798 | { | |
799 | return m_indent; | |
800 | } | |
801 | ||
802 | bool wxTreeCtrl::SetItem( wxTreeItem &info ) | |
803 | { | |
804 | wxGenericTreeItem *i = FindItem( info.m_itemId ); | |
805 | if (!i) return FALSE; | |
806 | wxClientDC dc(this); | |
807 | i->SetItem( info, &dc ); | |
808 | Refresh(); | |
809 | return TRUE; | |
810 | } | |
811 | ||
812 | bool wxTreeCtrl::SetItemData( long item, long data ) | |
813 | { | |
814 | wxGenericTreeItem *i = FindItem( item ); | |
815 | if (!i) return FALSE; | |
816 | i->m_data = data; | |
817 | return TRUE; | |
818 | } | |
819 | ||
820 | bool wxTreeCtrl::SetItemText( long item, const wxString &text ) | |
821 | { | |
822 | wxGenericTreeItem *i = FindItem( item ); | |
823 | if (!i) return FALSE; | |
824 | wxClientDC dc(this); | |
825 | i->SetText( text, &dc ); | |
826 | return TRUE; | |
827 | } | |
828 | ||
829 | void wxTreeCtrl::SetItemImage(long item, int image, int imageSel) const | |
830 | { | |
831 | wxGenericTreeItem *i = FindItem( item ); | |
832 | if ( i != 0 ) | |
833 | { | |
834 | i->SetImage(image); | |
835 | i->SetSelectedImage(imageSel); | |
836 | } | |
837 | } | |
838 | ||
839 | int wxTreeCtrl::GetItemState( long item, long stateMask ) const | |
840 | { | |
841 | wxGenericTreeItem *i = FindItem( item ); | |
842 | if (!i) return 0; | |
843 | int ret = 0; | |
844 | ||
845 | // Currently, an item is always selected _and_ focussed in wxGTK | |
846 | ||
847 | if (stateMask & wxTREE_STATE_FOCUSED) | |
848 | if (m_current == i) | |
849 | ret += wxTREE_STATE_FOCUSED; | |
850 | ||
851 | if (stateMask & wxTREE_STATE_SELECTED) | |
852 | if (m_current == i) | |
853 | ret += wxTREE_STATE_SELECTED; | |
854 | ||
855 | if (stateMask & wxTREE_STATE_EXPANDED) | |
856 | if (i->IsExpanded()) | |
857 | ret += wxTREE_STATE_EXPANDED; | |
858 | ||
859 | return ret; | |
860 | } | |
861 | ||
862 | bool wxTreeCtrl::SetItemState( long item, long state, long stateMask ) | |
863 | { | |
864 | wxGenericTreeItem *i = FindItem( item ); | |
865 | if (!i) return FALSE; | |
866 | ||
867 | if (stateMask & wxTREE_STATE_FOCUSED) | |
868 | { | |
869 | if (state & wxTREE_STATE_FOCUSED) | |
870 | SelectItem( i ); | |
871 | } | |
872 | ||
873 | if (stateMask & wxTREE_STATE_SELECTED) | |
874 | { | |
875 | if (state & wxTREE_STATE_SELECTED) | |
876 | SelectItem( i ); | |
877 | } | |
878 | ||
879 | if (stateMask & wxTREE_STATE_EXPANDED) | |
880 | { | |
881 | if (state & wxTREE_STATE_EXPANDED) | |
882 | ExpandItem( i->m_itemId, wxTREE_EXPAND_EXPAND ); | |
883 | else | |
884 | ExpandItem( i->m_itemId, wxTREE_EXPAND_COLLAPSE ); | |
885 | } | |
886 | ||
887 | return TRUE; | |
888 | } | |
889 | ||
890 | long wxTreeCtrl::HitTest( const wxPoint& point, int &flags ) | |
891 | { | |
892 | flags = 0; | |
893 | if (!m_anchor) return -1; | |
894 | return m_anchor->HitTest( point, flags ); | |
895 | } | |
896 | ||
897 | wxImageList *wxTreeCtrl::GetImageList( int which ) const | |
898 | { | |
899 | if (which == wxIMAGE_LIST_NORMAL) return m_imageList; | |
900 | return m_smallImageList; | |
901 | } | |
902 | ||
903 | void wxTreeCtrl::SetImageList( wxImageList *imageList, int which ) | |
904 | { | |
905 | if (which == wxIMAGE_LIST_NORMAL) | |
906 | { | |
907 | if (m_imageList) delete m_imageList; | |
908 | m_imageList = imageList; | |
909 | } | |
910 | else | |
911 | { | |
912 | if (m_smallImageList) delete m_smallImageList; | |
913 | m_smallImageList = imageList; | |
914 | } | |
915 | } | |
916 | ||
917 | void wxTreeCtrl::AdjustMyScrollbars() | |
918 | { | |
919 | if (m_anchor) | |
920 | { | |
921 | int x = 0; | |
922 | int y = 0; | |
923 | m_anchor->GetSize( x, y ); | |
924 | y += 2*m_lineHeight; | |
925 | int x_pos = GetScrollPos( wxHORIZONTAL ); | |
926 | int y_pos = GetScrollPos( wxVERTICAL ); | |
927 | SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos ); | |
928 | } | |
929 | else | |
930 | { | |
931 | SetScrollbars( 0, 0, 0, 0 ); | |
932 | } | |
933 | } | |
934 | ||
935 | void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y ) | |
936 | { | |
937 | int horizX = level*m_indent; | |
938 | ||
939 | item->m_x = horizX+33; | |
940 | item->m_y = y-m_lineHeight/3; | |
941 | item->m_height = m_lineHeight; | |
942 | ||
943 | item->SetCross( horizX+15, y ); | |
944 | ||
945 | int oldY = y; | |
946 | ||
947 | int exposed_x = dc.LogicalToDeviceX( 0 ); | |
948 | int exposed_y = dc.LogicalToDeviceY( item->m_y-2 ); | |
949 | ||
950 | if (IsExposed( exposed_x, exposed_y, 1000, m_lineHeight+4 )) | |
951 | { | |
952 | int startX = horizX; | |
953 | int endX = horizX + 10; | |
954 | ||
955 | if (!item->HasChildren()) endX += 20; | |
956 | ||
957 | dc.DrawLine( startX, y, endX, y ); | |
958 | ||
959 | if (item->HasChildren()) | |
960 | { | |
961 | dc.DrawLine( horizX+20, y, horizX+30, y ); | |
962 | dc.SetPen( *wxGREY_PEN ); | |
963 | dc.DrawRectangle( horizX+10, y-4, 11, 9 ); | |
964 | dc.SetPen( *wxBLACK_PEN ); | |
965 | dc.DrawLine( horizX+13, y, horizX+18, y ); | |
966 | if (item->HasPlus()) | |
967 | dc.DrawLine( horizX+15, y-2, horizX+15, y+3 ); | |
968 | } | |
969 | ||
970 | if (item->HasHilight()) | |
971 | { | |
972 | dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) ); | |
973 | dc.SetBrush( *m_hilightBrush ); | |
974 | long tw, th; | |
975 | dc.GetTextExtent( item->m_text, &tw, &th ); | |
976 | if (m_hasFocus) | |
977 | { | |
978 | dc.SetPen( *wxBLACK_PEN ); | |
979 | dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 ); | |
980 | } | |
981 | else | |
982 | { | |
983 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
984 | dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 ); | |
985 | } | |
986 | dc.DrawText( item->m_text, item->m_x, item->m_y ); | |
987 | ||
988 | dc.SetPen( *wxBLACK_PEN ); | |
989 | dc.SetTextForeground( *wxBLACK ); | |
990 | dc.SetBrush( *wxWHITE_BRUSH ); | |
991 | } | |
992 | else | |
993 | { | |
994 | dc.SetBrush( *wxWHITE_BRUSH ); | |
995 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
996 | long tw, th; | |
997 | dc.GetTextExtent( item->m_text, &tw, &th ); | |
998 | dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 ); | |
999 | dc.DrawText( item->m_text, item->m_x, item->m_y ); | |
1000 | dc.SetPen( *wxBLACK_PEN ); | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | if (item->NumberOfVisibleChildren() == 0) return; | |
1005 | ||
1006 | int semiOldY = y; | |
1007 | ||
1008 | wxNode *node = item->m_children.First(); | |
1009 | while (node) | |
1010 | { | |
1011 | wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data(); | |
1012 | ||
1013 | y += m_lineHeight; | |
1014 | semiOldY = y; | |
1015 | ||
1016 | PaintLevel( child, dc, level+1, y ); | |
1017 | ||
1018 | node = node->Next(); | |
1019 | } | |
1020 | ||
1021 | dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY ); | |
1022 | } | |
1023 | ||
1024 | void wxTreeCtrl::OnPaint( const wxPaintEvent &WXUNUSED(event) ) | |
1025 | { | |
1026 | if (!m_anchor) return; | |
1027 | ||
1028 | wxPaintDC dc(this); | |
1029 | PrepareDC( dc ); | |
1030 | ||
1031 | dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) ); | |
1032 | ||
1033 | dc.SetPen( m_dottedPen ); | |
1034 | m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
1035 | ||
1036 | int y = m_lineHeight / 2 + 2; | |
1037 | PaintLevel( m_anchor, dc, 0, y ); | |
1038 | } | |
1039 | ||
1040 | void wxTreeCtrl::OnSetFocus( const wxFocusEvent &WXUNUSED(event) ) | |
1041 | { | |
1042 | m_hasFocus = TRUE; | |
1043 | if (m_current) RefreshLine( m_current ); | |
1044 | } | |
1045 | ||
1046 | void wxTreeCtrl::OnKillFocus( const wxFocusEvent &WXUNUSED(event) ) | |
1047 | { | |
1048 | m_hasFocus = FALSE; | |
1049 | if (m_current) RefreshLine( m_current ); | |
1050 | } | |
1051 | ||
1052 | void wxTreeCtrl::OnChar( wxKeyEvent &event ) | |
1053 | { | |
1054 | event.Skip(); | |
1055 | } | |
1056 | ||
1057 | void wxTreeCtrl::OnMouse( const wxMouseEvent &event ) | |
1058 | { | |
1059 | if (!event.LeftDown() && | |
1060 | !event.LeftDClick()) return; | |
1061 | ||
1062 | wxClientDC dc(this); | |
1063 | PrepareDC(dc); | |
1064 | long x = dc.DeviceToLogicalX( (long)event.GetX() ); | |
1065 | long y = dc.DeviceToLogicalY( (long)event.GetY() ); | |
1066 | ||
1067 | int flag = 0; | |
1068 | long id = HitTest( wxPoint(x,y), flag ); | |
1069 | if (id == -1) | |
1070 | return; | |
1071 | wxGenericTreeItem *item = FindItem( id ); | |
1072 | ||
1073 | if (!item) return; | |
1074 | if ((flag != wxTREE_HITTEST_ONITEMBUTTON) && | |
1075 | (flag != wxTREE_HITTEST_ONITEMLABEL)) return; | |
1076 | ||
1077 | SelectItem(item); | |
1078 | ||
1079 | if (event.LeftDClick()) | |
1080 | m_current->SendKeyDown( this ); | |
1081 | ||
1082 | if (flag == wxTREE_HITTEST_ONITEMBUTTON) | |
1083 | { | |
1084 | ExpandItem( item->m_itemId, wxTREE_EXPAND_TOGGLE ); | |
1085 | return; | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y ) | |
1090 | { | |
1091 | int horizX = level*m_indent; | |
1092 | ||
1093 | item->m_x = horizX+33; | |
1094 | item->m_y = y-m_lineHeight/3-2; | |
1095 | item->m_height = m_lineHeight; | |
1096 | ||
1097 | if (item->NumberOfVisibleChildren() == 0) return; | |
1098 | ||
1099 | wxNode *node = item->m_children.First(); | |
1100 | while (node) | |
1101 | { | |
1102 | wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data(); | |
1103 | ||
1104 | y += m_lineHeight; | |
1105 | CalculateLevel( child, dc, level+1, y ); | |
1106 | ||
1107 | node = node->Next(); | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | void wxTreeCtrl::CalculatePositions() | |
1112 | { | |
1113 | if (!m_anchor) | |
1114 | return; | |
1115 | ||
1116 | wxClientDC dc(this); | |
1117 | PrepareDC( dc ); | |
1118 | ||
1119 | dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) ); | |
1120 | ||
1121 | dc.SetPen( m_dottedPen ); | |
1122 | m_lineHeight = (int)(dc.GetCharHeight() + 4); | |
1123 | ||
1124 | int y = m_lineHeight / 2 + 2; | |
1125 | CalculateLevel( m_anchor, dc, 0, y ); | |
1126 | } | |
1127 | ||
1128 | wxGenericTreeItem *wxTreeCtrl::FindItem( long itemId ) const | |
1129 | { | |
1130 | if (!m_anchor) return (wxGenericTreeItem *) NULL; | |
1131 | return m_anchor->FindItem( itemId ); | |
1132 | } | |
1133 | ||
1134 | void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item ) | |
1135 | { | |
1136 | if (!item) return; | |
1137 | wxClientDC dc(this); | |
1138 | PrepareDC( dc ); | |
1139 | wxRect rect; | |
1140 | rect.x = dc.LogicalToDeviceX( item->m_x-2 ); | |
1141 | rect.y = dc.LogicalToDeviceY( item->m_y-2 ); | |
1142 | rect.width = 1000; | |
1143 | rect.height = dc.GetCharHeight()+6; | |
1144 | Refresh( TRUE, &rect ); | |
1145 | } | |
1146 | ||
1147 | ||
1148 |