]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: treectrl.cpp | |
3 | // Purpose: wxTreeCtrl | |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98 | |
6 | // Created: 1997 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | #ifdef __GNUG__ | |
20 | #pragma implementation "treectrl.h" | |
21 | #endif | |
22 | ||
23 | // For compilers that support precompilation, includes "wx.h". | |
24 | #include "wx/wxprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
27 | #pragma hdrstop | |
28 | #endif | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/wx.h" | |
32 | #endif | |
33 | ||
34 | #if defined(__WIN95__) | |
35 | ||
36 | #include "wx/log.h" | |
37 | #include "wx/dynarray.h" | |
38 | #include "wx/imaglist.h" | |
39 | #include "wx/msw/treectrl.h" | |
40 | ||
41 | #include "wx/msw/private.h" | |
42 | ||
43 | #ifdef __GNUWIN32__ | |
44 | #include "wx/msw/gnuwin32/extra.h" | |
45 | #endif | |
46 | ||
47 | #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) | |
48 | #include <commctrl.h> | |
49 | #endif | |
50 | ||
51 | #ifdef GetFirstChild | |
52 | #undef GetFirstChild | |
53 | #endif | |
54 | ||
55 | #ifdef GetNextChild | |
56 | #undef GetNextChild | |
57 | #endif | |
58 | ||
59 | #ifdef GetNextSibling | |
60 | #undef GetNextSibling | |
61 | #endif | |
62 | ||
63 | #ifdef GetClassInfo | |
64 | #undef GetClassInfo | |
65 | #endif | |
66 | ||
67 | // Bug in headers, sometimes | |
68 | #ifndef TVIS_FOCUSED | |
69 | #define TVIS_FOCUSED 0x0001 | |
70 | #endif | |
71 | ||
72 | // ---------------------------------------------------------------------------- | |
73 | // private classes | |
74 | // ---------------------------------------------------------------------------- | |
75 | ||
76 | // a convenient wrapper around TV_ITEM struct which adds a ctor | |
77 | struct wxTreeViewItem : public TV_ITEM | |
78 | { | |
79 | wxTreeViewItem(const wxTreeItemId& item, | |
80 | UINT mask_, UINT stateMask_ = 0) | |
81 | { | |
82 | mask = mask_; | |
83 | stateMask = stateMask_; | |
84 | hItem = (HTREEITEM) (WXHTREEITEM) item; | |
85 | } | |
86 | }; | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // macros | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | #if !USE_SHARED_LIBRARY | |
93 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) | |
94 | #endif | |
95 | ||
96 | // hide the ugly cast (of course, the macro is _quite_ ugly too...) | |
97 | #define wxhWnd ((HWND)m_hWnd) | |
98 | ||
99 | // ---------------------------------------------------------------------------- | |
100 | // variables | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
103 | // handy table for sending events | |
104 | static const wxEventType g_events[2][2] = | |
105 | { | |
106 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, | |
107 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } | |
108 | }; | |
109 | ||
110 | // ============================================================================ | |
111 | // implementation | |
112 | // ============================================================================ | |
113 | ||
114 | // ---------------------------------------------------------------------------- | |
115 | // construction and destruction | |
116 | // ---------------------------------------------------------------------------- | |
117 | ||
118 | void wxTreeCtrl::Init() | |
119 | { | |
120 | m_imageListNormal = NULL; | |
121 | m_imageListState = NULL; | |
122 | m_textCtrl = NULL; | |
123 | } | |
124 | ||
125 | bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, | |
126 | const wxPoint& pos, const wxSize& size, | |
127 | long style, const wxValidator& validator, | |
128 | const wxString& name) | |
129 | { | |
130 | Init(); | |
131 | ||
132 | wxSystemSettings settings; | |
133 | ||
134 | SetName(name); | |
135 | SetValidator(validator); | |
136 | ||
137 | m_windowStyle = style; | |
138 | ||
139 | SetParent(parent); | |
140 | ||
141 | m_windowId = (id == -1) ? NewControlId() : id; | |
142 | ||
143 | DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_SHOWSELALWAYS ; | |
144 | ||
145 | ||
146 | bool want3D; | |
147 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; | |
148 | ||
149 | // Even with extended styles, need to combine with WS_BORDER | |
150 | // for them to look right. | |
151 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) | |
152 | { | |
153 | wstyle |= WS_BORDER; | |
154 | } | |
155 | ||
156 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) | |
157 | wstyle |= TVS_HASBUTTONS; | |
158 | ||
159 | if ( m_windowStyle & wxTR_EDIT_LABELS ) | |
160 | wstyle |= TVS_EDITLABELS; | |
161 | ||
162 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) | |
163 | wstyle |= TVS_LINESATROOT; | |
164 | ||
165 | // Create the tree control. | |
166 | m_hWnd = (WXHWND)::CreateWindowEx | |
167 | ( | |
168 | exStyle, | |
169 | WC_TREEVIEW, | |
170 | "", | |
171 | wstyle, | |
172 | pos.x, pos.y, size.x, size.y, | |
173 | (HWND)parent->GetHWND(), | |
174 | (HMENU)m_windowId, | |
175 | wxGetInstance(), | |
176 | NULL | |
177 | ); | |
178 | ||
179 | wxCHECK_MSG( m_hWnd, FALSE, "Failed to create tree ctrl" ); | |
180 | ||
181 | if ( parent ) | |
182 | parent->AddChild(this); | |
183 | ||
184 | SubclassWin(m_hWnd); | |
185 | ||
186 | return TRUE; | |
187 | } | |
188 | ||
189 | wxTreeCtrl::~wxTreeCtrl() | |
190 | { | |
191 | DeleteTextCtrl(); | |
192 | ||
193 | // delete user data to prevent memory leaks | |
194 | DeleteAllItems(); | |
195 | } | |
196 | ||
197 | // ---------------------------------------------------------------------------- | |
198 | // accessors | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
201 | // simple wrappers which add error checking in debug mode | |
202 | ||
203 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const | |
204 | { | |
205 | if ( !TreeView_GetItem(wxhWnd, tvItem) ) | |
206 | { | |
207 | wxLogLastError("TreeView_GetItem"); | |
208 | ||
209 | return FALSE; | |
210 | } | |
211 | ||
212 | return TRUE; | |
213 | } | |
214 | ||
215 | void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem) | |
216 | { | |
217 | if ( TreeView_SetItem(wxhWnd, tvItem) == -1 ) | |
218 | { | |
219 | wxLogLastError("TreeView_SetItem"); | |
220 | } | |
221 | } | |
222 | ||
223 | size_t wxTreeCtrl::GetCount() const | |
224 | { | |
225 | return (size_t)TreeView_GetCount(wxhWnd); | |
226 | } | |
227 | ||
228 | unsigned int wxTreeCtrl::GetIndent() const | |
229 | { | |
230 | return TreeView_GetIndent(wxhWnd); | |
231 | } | |
232 | ||
233 | void wxTreeCtrl::SetIndent(unsigned int indent) | |
234 | { | |
235 | TreeView_SetIndent(wxhWnd, indent); | |
236 | } | |
237 | ||
238 | wxImageList *wxTreeCtrl::GetImageList() const | |
239 | { | |
240 | return m_imageListNormal; | |
241 | } | |
242 | ||
243 | wxImageList *wxTreeCtrl::GetStateImageList() const | |
244 | { | |
245 | return m_imageListNormal; | |
246 | } | |
247 | ||
248 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) | |
249 | { | |
250 | // no error return | |
251 | TreeView_SetImageList(wxhWnd, | |
252 | imageList ? imageList->GetHIMAGELIST() : 0, | |
253 | which); | |
254 | } | |
255 | ||
256 | void wxTreeCtrl::SetImageList(wxImageList *imageList) | |
257 | { | |
258 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); | |
259 | } | |
260 | ||
261 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) | |
262 | { | |
263 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); | |
264 | } | |
265 | ||
266 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively) | |
267 | { | |
268 | long cookie; | |
269 | ||
270 | size_t result = 0; | |
271 | ||
272 | wxArrayLong children; | |
273 | wxTreeItemId child = GetFirstChild(item, cookie); | |
274 | while ( child.IsOk() ) | |
275 | { | |
276 | if ( recursively ) | |
277 | { | |
278 | // recursive call | |
279 | result += GetChildrenCount(child, TRUE); | |
280 | } | |
281 | ||
282 | // add the child to the result in any case | |
283 | result++; | |
284 | ||
285 | child = GetNextChild(item, cookie); | |
286 | } | |
287 | ||
288 | return result; | |
289 | } | |
290 | ||
291 | // ---------------------------------------------------------------------------- | |
292 | // Item access | |
293 | // ---------------------------------------------------------------------------- | |
294 | ||
295 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const | |
296 | { | |
297 | char buf[512]; // the size is arbitrary... | |
298 | ||
299 | wxTreeViewItem tvItem(item, TVIF_TEXT); | |
300 | tvItem.pszText = buf; | |
301 | tvItem.cchTextMax = WXSIZEOF(buf); | |
302 | if ( !DoGetItem(&tvItem) ) | |
303 | { | |
304 | // don't return some garbage which was on stack, but an empty string | |
305 | buf[0] = '\0'; | |
306 | } | |
307 | ||
308 | return wxString(buf); | |
309 | } | |
310 | ||
311 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) | |
312 | { | |
313 | wxTreeViewItem tvItem(item, TVIF_TEXT); | |
314 | tvItem.pszText = (char *)text.c_str(); // conversion is ok | |
315 | DoSetItem(&tvItem); | |
316 | } | |
317 | ||
318 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const | |
319 | { | |
320 | wxTreeViewItem tvItem(item, TVIF_IMAGE); | |
321 | DoGetItem(&tvItem); | |
322 | ||
323 | return tvItem.iImage; | |
324 | } | |
325 | ||
326 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image) | |
327 | { | |
328 | wxTreeViewItem tvItem(item, TVIF_IMAGE); | |
329 | tvItem.iImage = image; | |
330 | DoSetItem(&tvItem); | |
331 | } | |
332 | ||
333 | int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const | |
334 | { | |
335 | wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE); | |
336 | DoGetItem(&tvItem); | |
337 | ||
338 | return tvItem.iSelectedImage; | |
339 | } | |
340 | ||
341 | void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image) | |
342 | { | |
343 | wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE); | |
344 | tvItem.iSelectedImage = image; | |
345 | DoSetItem(&tvItem); | |
346 | } | |
347 | ||
348 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const | |
349 | { | |
350 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
351 | if ( !DoGetItem(&tvItem) ) | |
352 | { | |
353 | return NULL; | |
354 | } | |
355 | ||
356 | return (wxTreeItemData *)tvItem.lParam; | |
357 | } | |
358 | ||
359 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) | |
360 | { | |
361 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
362 | tvItem.lParam = (LPARAM)data; | |
363 | DoSetItem(&tvItem); | |
364 | } | |
365 | ||
366 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) | |
367 | { | |
368 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); | |
369 | tvItem.cChildren = (int)has; | |
370 | DoSetItem(&tvItem); | |
371 | } | |
372 | ||
373 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) | |
374 | { | |
375 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
376 | tvItem.state = bold ? TVIS_BOLD : 0; | |
377 | DoSetItem(&tvItem); | |
378 | } | |
379 | ||
380 | // ---------------------------------------------------------------------------- | |
381 | // Item status | |
382 | // ---------------------------------------------------------------------------- | |
383 | ||
384 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const | |
385 | { | |
386 | // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect | |
387 | RECT rect; | |
388 | return SendMessage(wxhWnd, TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0; | |
389 | ||
390 | } | |
391 | ||
392 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const | |
393 | { | |
394 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); | |
395 | DoGetItem(&tvItem); | |
396 | ||
397 | return tvItem.cChildren != 0; | |
398 | } | |
399 | ||
400 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const | |
401 | { | |
402 | // probably not a good idea to put it here | |
403 | //wxASSERT( ItemHasChildren(item) ); | |
404 | ||
405 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); | |
406 | DoGetItem(&tvItem); | |
407 | ||
408 | return (tvItem.state & TVIS_EXPANDED) != 0; | |
409 | } | |
410 | ||
411 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const | |
412 | { | |
413 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); | |
414 | DoGetItem(&tvItem); | |
415 | ||
416 | return (tvItem.state & TVIS_SELECTED) != 0; | |
417 | } | |
418 | ||
419 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const | |
420 | { | |
421 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
422 | DoGetItem(&tvItem); | |
423 | ||
424 | return (tvItem.state & TVIS_BOLD) != 0; | |
425 | } | |
426 | ||
427 | // ---------------------------------------------------------------------------- | |
428 | // navigation | |
429 | // ---------------------------------------------------------------------------- | |
430 | ||
431 | wxTreeItemId wxTreeCtrl::GetRootItem() const | |
432 | { | |
433 | return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(wxhWnd)); | |
434 | } | |
435 | ||
436 | wxTreeItemId wxTreeCtrl::GetSelection() const | |
437 | { | |
438 | return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(wxhWnd)); | |
439 | } | |
440 | ||
441 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const | |
442 | { | |
443 | return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); | |
444 | } | |
445 | ||
446 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, | |
447 | long& _cookie) const | |
448 | { | |
449 | // remember the last child returned in 'cookie' | |
450 | _cookie = (long)TreeView_GetChild(wxhWnd, (HTREEITEM) (WXHTREEITEM)item); | |
451 | ||
452 | return wxTreeItemId((WXHTREEITEM)_cookie); | |
453 | } | |
454 | ||
455 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), | |
456 | long& _cookie) const | |
457 | { | |
458 | wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(wxhWnd, | |
459 | (HTREEITEM)(WXHTREEITEM)_cookie)); | |
460 | _cookie = (long)l; | |
461 | ||
462 | return l; | |
463 | } | |
464 | ||
465 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const | |
466 | { | |
467 | // can this be done more efficiently? | |
468 | long cookie; | |
469 | ||
470 | wxTreeItemId childLast, | |
471 | child = GetFirstChild(item, cookie); | |
472 | while ( child.IsOk() ) | |
473 | { | |
474 | childLast = child; | |
475 | child = GetNextChild(item, cookie); | |
476 | } | |
477 | ||
478 | return childLast; | |
479 | } | |
480 | ||
481 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const | |
482 | { | |
483 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); | |
484 | } | |
485 | ||
486 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const | |
487 | { | |
488 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); | |
489 | } | |
490 | ||
491 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const | |
492 | { | |
493 | return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(wxhWnd)); | |
494 | } | |
495 | ||
496 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const | |
497 | { | |
498 | wxASSERT_MSG( IsVisible(item), "The item you call GetNextVisible() " | |
499 | "for must be visible itself!"); | |
500 | ||
501 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); | |
502 | } | |
503 | ||
504 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const | |
505 | { | |
506 | wxASSERT_MSG( IsVisible(item), "The item you call GetPrevVisible() " | |
507 | "for must be visible itself!"); | |
508 | ||
509 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); | |
510 | } | |
511 | ||
512 | // ---------------------------------------------------------------------------- | |
513 | // Usual operations | |
514 | // ---------------------------------------------------------------------------- | |
515 | ||
516 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, | |
517 | wxTreeItemId hInsertAfter, | |
518 | const wxString& text, | |
519 | int image, int selectedImage, | |
520 | wxTreeItemData *data) | |
521 | { | |
522 | TV_INSERTSTRUCT tvIns; | |
523 | tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent; | |
524 | tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter; | |
525 | UINT mask = 0; | |
526 | if ( !text.IsEmpty() ) | |
527 | { | |
528 | mask |= TVIF_TEXT; | |
529 | tvIns.item.pszText = (char *)text.c_str(); // cast is ok | |
530 | } | |
531 | ||
532 | if ( image != -1 ) | |
533 | { | |
534 | mask |= TVIF_IMAGE; | |
535 | tvIns.item.iImage = image; | |
536 | ||
537 | if ( selectedImage == -1 ) | |
538 | { | |
539 | // take the same image for selected icon if not specified | |
540 | selectedImage = image; | |
541 | } | |
542 | } | |
543 | ||
544 | if ( selectedImage != -1 ) | |
545 | { | |
546 | mask |= TVIF_SELECTEDIMAGE; | |
547 | tvIns.item.iSelectedImage = selectedImage; | |
548 | } | |
549 | ||
550 | if ( data != NULL ) | |
551 | { | |
552 | mask |= TVIF_PARAM; | |
553 | tvIns.item.lParam = (LPARAM)data; | |
554 | } | |
555 | ||
556 | tvIns.item.mask = mask; | |
557 | ||
558 | HTREEITEM id = (HTREEITEM) TreeView_InsertItem(wxhWnd, &tvIns); | |
559 | if ( id == 0 ) | |
560 | { | |
561 | wxLogLastError("TreeView_InsertItem"); | |
562 | } | |
563 | ||
564 | if ( data != NULL ) | |
565 | { | |
566 | // associate the application tree item with Win32 tree item handle | |
567 | data->SetId((WXHTREEITEM)id); | |
568 | } | |
569 | ||
570 | return wxTreeItemId((WXHTREEITEM)id); | |
571 | } | |
572 | ||
573 | // for compatibility only | |
574 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, | |
575 | const wxString& text, | |
576 | int image, int selImage, | |
577 | long insertAfter) | |
578 | { | |
579 | return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text, | |
580 | image, selImage, NULL); | |
581 | } | |
582 | ||
583 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, | |
584 | int image, int selectedImage, | |
585 | wxTreeItemData *data) | |
586 | { | |
587 | return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0, | |
588 | text, image, selectedImage, data); | |
589 | } | |
590 | ||
591 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, | |
592 | const wxString& text, | |
593 | int image, int selectedImage, | |
594 | wxTreeItemData *data) | |
595 | { | |
596 | return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST, | |
597 | text, image, selectedImage, data); | |
598 | } | |
599 | ||
600 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, | |
601 | const wxTreeItemId& idPrevious, | |
602 | const wxString& text, | |
603 | int image, int selectedImage, | |
604 | wxTreeItemData *data) | |
605 | { | |
606 | return DoInsertItem(parent, idPrevious, text, image, selectedImage, data); | |
607 | } | |
608 | ||
609 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent, | |
610 | const wxString& text, | |
611 | int image, int selectedImage, | |
612 | wxTreeItemData *data) | |
613 | { | |
614 | return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST, | |
615 | text, image, selectedImage, data); | |
616 | } | |
617 | ||
618 | void wxTreeCtrl::Delete(const wxTreeItemId& item) | |
619 | { | |
620 | if ( !TreeView_DeleteItem(wxhWnd, (HTREEITEM)(WXHTREEITEM)item) ) | |
621 | { | |
622 | wxLogLastError("TreeView_DeleteItem"); | |
623 | } | |
624 | } | |
625 | ||
626 | // delete all children (but don't delete the item itself) | |
627 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item) | |
628 | { | |
629 | long cookie; | |
630 | ||
631 | wxArrayLong children; | |
632 | wxTreeItemId child = GetFirstChild(item, cookie); | |
633 | while ( child.IsOk() ) | |
634 | { | |
635 | children.Add((long)(WXHTREEITEM)child); | |
636 | ||
637 | child = GetNextChild(item, cookie); | |
638 | } | |
639 | ||
640 | size_t nCount = children.Count(); | |
641 | for ( size_t n = 0; n < nCount; n++ ) | |
642 | { | |
643 | if ( !TreeView_DeleteItem(wxhWnd, (HTREEITEM)children[n]) ) | |
644 | { | |
645 | wxLogLastError("TreeView_DeleteItem"); | |
646 | } | |
647 | } | |
648 | } | |
649 | ||
650 | void wxTreeCtrl::DeleteAllItems() | |
651 | { | |
652 | if ( !TreeView_DeleteAllItems(wxhWnd) ) | |
653 | { | |
654 | wxLogLastError("TreeView_DeleteAllItems"); | |
655 | } | |
656 | } | |
657 | ||
658 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) | |
659 | { | |
660 | wxASSERT_MSG( flag == TVE_COLLAPSE || flag == TVE_COLLAPSERESET || | |
661 | flag == TVE_EXPAND || flag == TVE_TOGGLE, | |
662 | "Unknown flag in wxTreeCtrl::DoExpand" ); | |
663 | ||
664 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must | |
665 | // emulate them | |
666 | if ( TreeView_Expand(wxhWnd, (HTREEITEM) (WXHTREEITEM) item, flag) != 0 ) | |
667 | { | |
668 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
669 | event.m_item = item; | |
670 | ||
671 | bool isExpanded = IsExpanded(item); | |
672 | ||
673 | event.SetEventObject(this); | |
674 | ||
675 | // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded | |
676 | event.SetEventType(g_events[isExpanded][TRUE]); | |
677 | GetEventHandler()->ProcessEvent(event); | |
678 | ||
679 | event.SetEventType(g_events[isExpanded][FALSE]); | |
680 | GetEventHandler()->ProcessEvent(event); | |
681 | } | |
682 | else | |
683 | { | |
684 | // I wonder if it really ever happens... | |
685 | wxLogDebug("TreeView_Expand: change didn't took place."); | |
686 | } | |
687 | } | |
688 | ||
689 | void wxTreeCtrl::Expand(const wxTreeItemId& item) | |
690 | { | |
691 | DoExpand(item, TVE_EXPAND); | |
692 | } | |
693 | ||
694 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) | |
695 | { | |
696 | DoExpand(item, TVE_COLLAPSE); | |
697 | } | |
698 | ||
699 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) | |
700 | { | |
701 | DoExpand(item, TVE_COLLAPSERESET); | |
702 | } | |
703 | ||
704 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) | |
705 | { | |
706 | DoExpand(item, TVE_TOGGLE); | |
707 | } | |
708 | ||
709 | void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action) | |
710 | { | |
711 | DoExpand(item, action); | |
712 | } | |
713 | ||
714 | void wxTreeCtrl::Unselect() | |
715 | { | |
716 | SelectItem(wxTreeItemId((WXHTREEITEM) 0)); | |
717 | } | |
718 | ||
719 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item) | |
720 | { | |
721 | if ( !TreeView_SelectItem(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) ) | |
722 | { | |
723 | wxLogLastError("TreeView_SelectItem"); | |
724 | } | |
725 | } | |
726 | ||
727 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) | |
728 | { | |
729 | // no error return | |
730 | TreeView_EnsureVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item); | |
731 | } | |
732 | ||
733 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) | |
734 | { | |
735 | if ( !TreeView_SelectSetFirstVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) ) | |
736 | { | |
737 | wxLogLastError("TreeView_SelectSetFirstVisible"); | |
738 | } | |
739 | } | |
740 | ||
741 | wxTextCtrl* wxTreeCtrl::GetEditControl() const | |
742 | { | |
743 | return m_textCtrl; | |
744 | } | |
745 | ||
746 | void wxTreeCtrl::DeleteTextCtrl() | |
747 | { | |
748 | if ( m_textCtrl ) | |
749 | { | |
750 | m_textCtrl->UnsubclassWin(); | |
751 | m_textCtrl->SetHWND(0); | |
752 | delete m_textCtrl; | |
753 | m_textCtrl = NULL; | |
754 | } | |
755 | } | |
756 | ||
757 | wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, | |
758 | wxClassInfo* textControlClass) | |
759 | { | |
760 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
761 | ||
762 | HWND hWnd = (HWND) TreeView_EditLabel(wxhWnd, (HTREEITEM) (WXHTREEITEM) item); | |
763 | ||
764 | wxCHECK_MSG( hWnd, NULL, "Can't edit tree ctrl label" ); | |
765 | ||
766 | DeleteTextCtrl(); | |
767 | ||
768 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); | |
769 | m_textCtrl->SetHWND((WXHWND)hWnd); | |
770 | m_textCtrl->SubclassWin((WXHWND)hWnd); | |
771 | ||
772 | return m_textCtrl; | |
773 | } | |
774 | ||
775 | // End label editing, optionally cancelling the edit | |
776 | void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) | |
777 | { | |
778 | TreeView_EndEditLabelNow(wxhWnd, discardChanges); | |
779 | ||
780 | DeleteTextCtrl(); | |
781 | } | |
782 | ||
783 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) | |
784 | { | |
785 | TV_HITTESTINFO hitTestInfo; | |
786 | hitTestInfo.pt.x = (int)point.x; | |
787 | hitTestInfo.pt.y = (int)point.y; | |
788 | ||
789 | TreeView_HitTest(wxhWnd, &hitTestInfo); | |
790 | ||
791 | flags = 0; | |
792 | ||
793 | // avoid repetition | |
794 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ | |
795 | flags |= wxTREE_HITTEST_##flag | |
796 | ||
797 | TRANSLATE_FLAG(ABOVE); | |
798 | TRANSLATE_FLAG(BELOW); | |
799 | TRANSLATE_FLAG(NOWHERE); | |
800 | TRANSLATE_FLAG(ONITEMBUTTON); | |
801 | TRANSLATE_FLAG(ONITEMICON); | |
802 | TRANSLATE_FLAG(ONITEMINDENT); | |
803 | TRANSLATE_FLAG(ONITEMLABEL); | |
804 | TRANSLATE_FLAG(ONITEMRIGHT); | |
805 | TRANSLATE_FLAG(ONITEMSTATEICON); | |
806 | TRANSLATE_FLAG(TOLEFT); | |
807 | TRANSLATE_FLAG(TORIGHT); | |
808 | ||
809 | #undef TRANSLATE_FLAG | |
810 | ||
811 | return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem); | |
812 | } | |
813 | ||
814 | bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item, | |
815 | wxRect& rect, | |
816 | bool textOnly) const | |
817 | { | |
818 | RECT rc; | |
819 | if ( TreeView_GetItemRect(wxhWnd, (HTREEITEM)(WXHTREEITEM)item, | |
820 | &rc, textOnly) ) | |
821 | { | |
822 | rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom)); | |
823 | ||
824 | return TRUE; | |
825 | } | |
826 | else | |
827 | { | |
828 | // couldn't retrieve rect: for example, item isn't visible | |
829 | return FALSE; | |
830 | } | |
831 | } | |
832 | ||
833 | // ---------------------------------------------------------------------------- | |
834 | // sorting stuff | |
835 | // ---------------------------------------------------------------------------- | |
836 | ||
837 | static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1, | |
838 | wxTreeItemData *pItem2, | |
839 | wxTreeCtrl *tree) | |
840 | { | |
841 | return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId()); | |
842 | } | |
843 | ||
844 | int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1, | |
845 | const wxTreeItemId& item2) | |
846 | { | |
847 | return strcmp(GetItemText(item1), GetItemText(item2)); | |
848 | } | |
849 | ||
850 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item) | |
851 | { | |
852 | // rely on the fact that TreeView_SortChildren does the same thing as our | |
853 | // default behaviour, i.e. sorts items alphabetically and so call it | |
854 | // directly if we're not in derived class (much more efficient!) | |
855 | if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) ) | |
856 | { | |
857 | TreeView_SortChildren(wxhWnd, (HTREEITEM)(WXHTREEITEM)item, 0); | |
858 | } | |
859 | else | |
860 | { | |
861 | TV_SORTCB tvSort; | |
862 | tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item; | |
863 | tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback; | |
864 | tvSort.lParam = (LPARAM)this; | |
865 | TreeView_SortChildrenCB(wxhWnd, &tvSort, 0 /* reserved */); | |
866 | } | |
867 | } | |
868 | ||
869 | // ---------------------------------------------------------------------------- | |
870 | // implementation | |
871 | // ---------------------------------------------------------------------------- | |
872 | ||
873 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id) | |
874 | { | |
875 | if ( cmd == EN_UPDATE ) | |
876 | { | |
877 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); | |
878 | event.SetEventObject( this ); | |
879 | ProcessCommand(event); | |
880 | } | |
881 | else if ( cmd == EN_KILLFOCUS ) | |
882 | { | |
883 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); | |
884 | event.SetEventObject( this ); | |
885 | ProcessCommand(event); | |
886 | } | |
887 | else | |
888 | { | |
889 | // nothing done | |
890 | return FALSE; | |
891 | } | |
892 | ||
893 | // command processed | |
894 | return TRUE; | |
895 | } | |
896 | ||
897 | // process WM_NOTIFY Windows message | |
898 | bool wxTreeCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam, WXLPARAM *result) | |
899 | { | |
900 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
901 | wxEventType eventType = wxEVT_NULL; | |
902 | NMHDR *hdr = (NMHDR *)lParam; | |
903 | ||
904 | switch ( hdr->code ) | |
905 | { | |
906 | case TVN_BEGINDRAG: | |
907 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
908 | // fall through | |
909 | ||
910 | case TVN_BEGINRDRAG: | |
911 | { | |
912 | if ( eventType == wxEVT_NULL ) | |
913 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; | |
914 | //else: left drag, already set above | |
915 | ||
916 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
917 | ||
918 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; | |
919 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); | |
920 | break; | |
921 | } | |
922 | ||
923 | case TVN_BEGINLABELEDIT: | |
924 | { | |
925 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; | |
926 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
927 | ||
928 | event.m_item = (WXHTREEITEM) info->item.hItem; | |
929 | break; | |
930 | } | |
931 | ||
932 | case TVN_DELETEITEM: | |
933 | { | |
934 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; | |
935 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
936 | ||
937 | event.m_item = (WXHTREEITEM) tv->itemOld.hItem; | |
938 | break; | |
939 | } | |
940 | ||
941 | case TVN_ENDLABELEDIT: | |
942 | { | |
943 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; | |
944 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
945 | ||
946 | event.m_item = (WXHTREEITEM) info->item.hItem; | |
947 | break; | |
948 | } | |
949 | ||
950 | case TVN_GETDISPINFO: | |
951 | eventType = wxEVT_COMMAND_TREE_GET_INFO; | |
952 | // fall through | |
953 | ||
954 | case TVN_SETDISPINFO: | |
955 | { | |
956 | if ( eventType == wxEVT_NULL ) | |
957 | eventType = wxEVT_COMMAND_TREE_SET_INFO; | |
958 | //else: get, already set above | |
959 | ||
960 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
961 | ||
962 | event.m_item = (WXHTREEITEM) info->item.hItem; | |
963 | break; | |
964 | } | |
965 | ||
966 | case TVN_ITEMEXPANDING: | |
967 | event.m_code = FALSE; | |
968 | // fall through | |
969 | ||
970 | case TVN_ITEMEXPANDED: | |
971 | { | |
972 | NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam; | |
973 | ||
974 | bool expand = FALSE; | |
975 | switch ( tv->action ) | |
976 | { | |
977 | case TVE_EXPAND: | |
978 | expand = TRUE; | |
979 | break; | |
980 | ||
981 | case TVE_COLLAPSE: | |
982 | expand = FALSE; | |
983 | break; | |
984 | ||
985 | default: | |
986 | wxLogDebug("unexpected code %d in TVN_ITEMEXPAND " | |
987 | "message", tv->action); | |
988 | } | |
989 | ||
990 | bool ing = (hdr->code == TVN_ITEMEXPANDING); | |
991 | eventType = g_events[expand][ing]; | |
992 | ||
993 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; | |
994 | break; | |
995 | } | |
996 | ||
997 | case TVN_KEYDOWN: | |
998 | { | |
999 | eventType = wxEVT_COMMAND_TREE_KEY_DOWN; | |
1000 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; | |
1001 | ||
1002 | event.m_code = wxCharCodeMSWToWX(info->wVKey); | |
1003 | ||
1004 | // a separate event for this case | |
1005 | if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN ) | |
1006 | { | |
1007 | wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, | |
1008 | m_windowId); | |
1009 | event2.SetEventObject(this); | |
1010 | ||
1011 | GetEventHandler()->ProcessEvent(event2); | |
1012 | } | |
1013 | break; | |
1014 | } | |
1015 | ||
1016 | case TVN_SELCHANGED: | |
1017 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; | |
1018 | // fall through | |
1019 | ||
1020 | case TVN_SELCHANGING: | |
1021 | { | |
1022 | if ( eventType == wxEVT_NULL ) | |
1023 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; | |
1024 | //else: already set above | |
1025 | ||
1026 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
1027 | ||
1028 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; | |
1029 | event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem; | |
1030 | break; | |
1031 | } | |
1032 | ||
1033 | default: | |
1034 | return wxControl::MSWNotify(wParam, lParam, result); | |
1035 | } | |
1036 | ||
1037 | event.SetEventObject(this); | |
1038 | event.SetEventType(eventType); | |
1039 | ||
1040 | bool processed = GetEventHandler()->ProcessEvent(event); | |
1041 | ||
1042 | // post processing | |
1043 | if ( hdr->code == TVN_DELETEITEM ) | |
1044 | { | |
1045 | // NB: we might process this message using wxWindows event tables, but | |
1046 | // due to overhead of wxWin event system we prefer to do it here | |
1047 | // (otherwise deleting a tree with many items is just too slow) | |
1048 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
1049 | wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam; | |
1050 | delete data; // may be NULL, ok | |
1051 | } | |
1052 | ||
1053 | *result = !event.IsAllowed(); | |
1054 | ||
1055 | return processed; | |
1056 | } | |
1057 | ||
1058 | // ---------------------------------------------------------------------------- | |
1059 | // Tree event | |
1060 | // ---------------------------------------------------------------------------- | |
1061 | ||
1062 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent) | |
1063 | ||
1064 | wxTreeEvent::wxTreeEvent(wxEventType commandType, int id) | |
1065 | : wxNotifyEvent(commandType, id) | |
1066 | { | |
1067 | m_code = 0; | |
1068 | m_itemOld = 0; | |
1069 | } | |
1070 | ||
1071 | #endif // __WIN95__ | |
1072 |