]>
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 | #include "wx/window.h" | |
31 | #include "wx/msw/private.h" | |
32 | ||
33 | // Mingw32 is a bit mental even though this is done in winundef | |
34 | #ifdef GetFirstChild | |
35 | #undef GetFirstChild | |
36 | #endif | |
37 | ||
38 | #ifdef GetNextSibling | |
39 | #undef GetNextSibling | |
40 | #endif | |
41 | ||
42 | #if defined(__WIN95__) | |
43 | ||
44 | #include "wx/log.h" | |
45 | #include "wx/dynarray.h" | |
46 | #include "wx/imaglist.h" | |
47 | #include "wx/treectrl.h" | |
48 | ||
49 | #ifdef __GNUWIN32__ | |
50 | #ifndef wxUSE_NORLANDER_HEADERS | |
51 | #include "wx/msw/gnuwin32/extra.h" | |
52 | #endif | |
53 | #endif | |
54 | ||
55 | #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS) | |
56 | #include <commctrl.h> | |
57 | #endif | |
58 | ||
59 | // Bug in headers, sometimes | |
60 | #ifndef TVIS_FOCUSED | |
61 | #define TVIS_FOCUSED 0x0001 | |
62 | #endif | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // private classes | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | // a convenient wrapper around TV_ITEM struct which adds a ctor | |
69 | #pragma warning( disable : 4097 ) | |
70 | struct wxTreeViewItem : public TV_ITEM | |
71 | { | |
72 | wxTreeViewItem(const wxTreeItemId& item, // the item handle | |
73 | UINT mask_, // fields which are valid | |
74 | UINT stateMask_ = 0) // for TVIF_STATE only | |
75 | { | |
76 | // hItem member is always valid | |
77 | mask = mask_ | TVIF_HANDLE; | |
78 | stateMask = stateMask_; | |
79 | hItem = (HTREEITEM) (WXHTREEITEM) item; | |
80 | } | |
81 | }; | |
82 | #pragma warning( default : 4097 ) | |
83 | ||
84 | // a class which encapsulates the tree traversal logic: it vists all (unless | |
85 | // OnVisit() returns FALSE) items under the given one | |
86 | class wxTreeTraversal | |
87 | { | |
88 | public: | |
89 | wxTreeTraversal(const wxTreeCtrl *tree) | |
90 | { | |
91 | m_tree = tree; | |
92 | } | |
93 | ||
94 | // do traverse the tree: visit all items (recursively by default) under the | |
95 | // given one; return TRUE if all items were traversed or FALSE if the | |
96 | // traversal was aborted because OnVisit returned FALSE | |
97 | bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE); | |
98 | ||
99 | // override this function to do whatever is needed for each item, return | |
100 | // FALSE to stop traversing | |
101 | virtual bool OnVisit(const wxTreeItemId& item) = 0; | |
102 | ||
103 | protected: | |
104 | const wxTreeCtrl *GetTree() const { return m_tree; } | |
105 | ||
106 | private: | |
107 | bool Traverse(const wxTreeItemId& root, bool recursively); | |
108 | ||
109 | const wxTreeCtrl *m_tree; | |
110 | }; | |
111 | ||
112 | // ---------------------------------------------------------------------------- | |
113 | // macros | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
116 | #if !USE_SHARED_LIBRARY | |
117 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) | |
118 | #endif | |
119 | ||
120 | // ---------------------------------------------------------------------------- | |
121 | // variables | |
122 | // ---------------------------------------------------------------------------- | |
123 | ||
124 | // handy table for sending events | |
125 | static const wxEventType g_events[2][2] = | |
126 | { | |
127 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, | |
128 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } | |
129 | }; | |
130 | ||
131 | // ============================================================================ | |
132 | // implementation | |
133 | // ============================================================================ | |
134 | ||
135 | // ---------------------------------------------------------------------------- | |
136 | // tree traversal | |
137 | // ---------------------------------------------------------------------------- | |
138 | ||
139 | bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively) | |
140 | { | |
141 | if ( !OnVisit(root) ) | |
142 | return FALSE; | |
143 | ||
144 | return Traverse(root, recursively); | |
145 | } | |
146 | ||
147 | bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively) | |
148 | { | |
149 | long cookie; | |
150 | wxTreeItemId child = m_tree->GetFirstChild(root, cookie); | |
151 | while ( child.IsOk() ) | |
152 | { | |
153 | // depth first traversal | |
154 | if ( recursively && !Traverse(child, TRUE) ) | |
155 | return FALSE; | |
156 | ||
157 | if ( !OnVisit(child) ) | |
158 | return FALSE; | |
159 | ||
160 | child = m_tree->GetNextChild(root, cookie); | |
161 | } | |
162 | ||
163 | return TRUE; | |
164 | } | |
165 | ||
166 | // ---------------------------------------------------------------------------- | |
167 | // construction and destruction | |
168 | // ---------------------------------------------------------------------------- | |
169 | ||
170 | void wxTreeCtrl::Init() | |
171 | { | |
172 | m_imageListNormal = NULL; | |
173 | m_imageListState = NULL; | |
174 | m_textCtrl = NULL; | |
175 | } | |
176 | ||
177 | bool wxTreeCtrl::Create(wxWindow *parent, | |
178 | wxWindowID id, | |
179 | const wxPoint& pos, | |
180 | const wxSize& size, | |
181 | long style, | |
182 | const wxValidator& validator, | |
183 | const wxString& name) | |
184 | { | |
185 | Init(); | |
186 | ||
187 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
188 | return FALSE; | |
189 | ||
190 | DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | | |
191 | TVS_HASLINES | TVS_SHOWSELALWAYS; | |
192 | ||
193 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) | |
194 | wstyle |= TVS_HASBUTTONS; | |
195 | ||
196 | if ( m_windowStyle & wxTR_EDIT_LABELS ) | |
197 | wstyle |= TVS_EDITLABELS; | |
198 | ||
199 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) | |
200 | wstyle |= TVS_LINESATROOT; | |
201 | ||
202 | #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined(wxUSE_NORLANDER_HEADERS) | |
203 | // we emulate the multiple selection tree controls by using checkboxes: set | |
204 | // up the image list we need for this if we do have multiple selections | |
205 | #if !defined(__VISUALC__) || (__VISUALC__ != 1010) | |
206 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
207 | wstyle |= TVS_CHECKBOXES; | |
208 | #endif | |
209 | #endif | |
210 | ||
211 | // Create the tree control. | |
212 | if ( !MSWCreateControl(WC_TREEVIEW, wstyle) ) | |
213 | return FALSE; | |
214 | ||
215 | // the treectrl with any other background looks ugly because the items | |
216 | // background is white anyhow | |
217 | SetBackgroundColour(*wxWHITE); | |
218 | ||
219 | // VZ: this is some experimental code which may be used to get the | |
220 | // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71. | |
221 | // AFAIK, the standard DLL does about the same thing anyhow. | |
222 | #if 0 | |
223 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
224 | { | |
225 | wxBitmap bmp; | |
226 | ||
227 | // create the DC compatible with the current screen | |
228 | HDC hdcMem = CreateCompatibleDC(NULL); | |
229 | ||
230 | // create a mono bitmap of the standard size | |
231 | int x = GetSystemMetrics(SM_CXMENUCHECK); | |
232 | int y = GetSystemMetrics(SM_CYMENUCHECK); | |
233 | wxImageList imagelistCheckboxes(x, y, FALSE, 2); | |
234 | HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size | |
235 | 1, // # of color planes | |
236 | 1, // # bits needed for one pixel | |
237 | 0); // array containing colour data | |
238 | SelectObject(hdcMem, hbmpCheck); | |
239 | ||
240 | // then draw a check mark into it | |
241 | RECT rect = { 0, 0, x, y }; | |
242 | if ( !::DrawFrameControl(hdcMem, &rect, | |
243 | DFC_BUTTON, | |
244 | DFCS_BUTTONCHECK | DFCS_CHECKED) ) | |
245 | { | |
246 | wxLogLastError(_T("DrawFrameControl(check)")); | |
247 | } | |
248 | ||
249 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); | |
250 | imagelistCheckboxes.Add(bmp); | |
251 | ||
252 | if ( !::DrawFrameControl(hdcMem, &rect, | |
253 | DFC_BUTTON, | |
254 | DFCS_BUTTONCHECK) ) | |
255 | { | |
256 | wxLogLastError(_T("DrawFrameControl(uncheck)")); | |
257 | } | |
258 | ||
259 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); | |
260 | imagelistCheckboxes.Add(bmp); | |
261 | ||
262 | // clean up | |
263 | ::DeleteDC(hdcMem); | |
264 | ||
265 | // set the imagelist | |
266 | SetStateImageList(&imagelistCheckboxes); | |
267 | } | |
268 | #endif // 0 | |
269 | ||
270 | SetSize(pos.x, pos.y, size.x, size.y); | |
271 | ||
272 | return TRUE; | |
273 | } | |
274 | ||
275 | wxTreeCtrl::~wxTreeCtrl() | |
276 | { | |
277 | DeleteTextCtrl(); | |
278 | ||
279 | // delete user data to prevent memory leaks | |
280 | DeleteAllItems(); | |
281 | } | |
282 | ||
283 | // ---------------------------------------------------------------------------- | |
284 | // accessors | |
285 | // ---------------------------------------------------------------------------- | |
286 | ||
287 | // simple wrappers which add error checking in debug mode | |
288 | ||
289 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const | |
290 | { | |
291 | if ( !TreeView_GetItem(GetHwnd(), tvItem) ) | |
292 | { | |
293 | wxLogLastError("TreeView_GetItem"); | |
294 | ||
295 | return FALSE; | |
296 | } | |
297 | ||
298 | return TRUE; | |
299 | } | |
300 | ||
301 | void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem) | |
302 | { | |
303 | if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 ) | |
304 | { | |
305 | wxLogLastError("TreeView_SetItem"); | |
306 | } | |
307 | } | |
308 | ||
309 | size_t wxTreeCtrl::GetCount() const | |
310 | { | |
311 | return (size_t)TreeView_GetCount(GetHwnd()); | |
312 | } | |
313 | ||
314 | unsigned int wxTreeCtrl::GetIndent() const | |
315 | { | |
316 | return TreeView_GetIndent(GetHwnd()); | |
317 | } | |
318 | ||
319 | void wxTreeCtrl::SetIndent(unsigned int indent) | |
320 | { | |
321 | TreeView_SetIndent(GetHwnd(), indent); | |
322 | } | |
323 | ||
324 | wxImageList *wxTreeCtrl::GetImageList() const | |
325 | { | |
326 | return m_imageListNormal; | |
327 | } | |
328 | ||
329 | wxImageList *wxTreeCtrl::GetStateImageList() const | |
330 | { | |
331 | return m_imageListNormal; | |
332 | } | |
333 | ||
334 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) | |
335 | { | |
336 | // no error return | |
337 | TreeView_SetImageList(GetHwnd(), | |
338 | imageList ? imageList->GetHIMAGELIST() : 0, | |
339 | which); | |
340 | } | |
341 | ||
342 | void wxTreeCtrl::SetImageList(wxImageList *imageList) | |
343 | { | |
344 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); | |
345 | } | |
346 | ||
347 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) | |
348 | { | |
349 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); | |
350 | } | |
351 | ||
352 | // internal class for counting tree items | |
353 | ||
354 | class TraverseCounter : public wxTreeTraversal | |
355 | { | |
356 | public: | |
357 | TraverseCounter(const wxTreeCtrl *tree, | |
358 | const wxTreeItemId& root, | |
359 | bool recursively) | |
360 | : wxTreeTraversal(tree) | |
361 | { | |
362 | m_count = 0; | |
363 | ||
364 | DoTraverse(root, recursively); | |
365 | } | |
366 | ||
367 | virtual bool OnVisit(const wxTreeItemId& item) | |
368 | { | |
369 | m_count++; | |
370 | ||
371 | return TRUE; | |
372 | } | |
373 | ||
374 | size_t GetCount() const { return m_count; } | |
375 | ||
376 | private: | |
377 | size_t m_count; | |
378 | }; | |
379 | ||
380 | ||
381 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, | |
382 | bool recursively) const | |
383 | { | |
384 | TraverseCounter counter(this, item, recursively); | |
385 | ||
386 | return counter.GetCount(); | |
387 | } | |
388 | ||
389 | // ---------------------------------------------------------------------------- | |
390 | // Item access | |
391 | // ---------------------------------------------------------------------------- | |
392 | ||
393 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const | |
394 | { | |
395 | wxChar buf[512]; // the size is arbitrary... | |
396 | ||
397 | wxTreeViewItem tvItem(item, TVIF_TEXT); | |
398 | tvItem.pszText = buf; | |
399 | tvItem.cchTextMax = WXSIZEOF(buf); | |
400 | if ( !DoGetItem(&tvItem) ) | |
401 | { | |
402 | // don't return some garbage which was on stack, but an empty string | |
403 | buf[0] = _T('\0'); | |
404 | } | |
405 | ||
406 | return wxString(buf); | |
407 | } | |
408 | ||
409 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) | |
410 | { | |
411 | wxTreeViewItem tvItem(item, TVIF_TEXT); | |
412 | tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok | |
413 | DoSetItem(&tvItem); | |
414 | } | |
415 | ||
416 | void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item, | |
417 | int image, | |
418 | int imageSel) | |
419 | { | |
420 | wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE); | |
421 | tvItem.iSelectedImage = imageSel; | |
422 | tvItem.iImage = image; | |
423 | DoSetItem(&tvItem); | |
424 | } | |
425 | ||
426 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const | |
427 | { | |
428 | wxTreeViewItem tvItem(item, TVIF_IMAGE); | |
429 | DoGetItem(&tvItem); | |
430 | ||
431 | return tvItem.iImage; | |
432 | } | |
433 | ||
434 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image) | |
435 | { | |
436 | // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always | |
437 | // change both normal and selected image - otherwise the change simply | |
438 | // doesn't take place! | |
439 | DoSetItemImages(item, image, GetItemSelectedImage(item)); | |
440 | } | |
441 | ||
442 | int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const | |
443 | { | |
444 | wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE); | |
445 | DoGetItem(&tvItem); | |
446 | ||
447 | return tvItem.iSelectedImage; | |
448 | } | |
449 | ||
450 | void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image) | |
451 | { | |
452 | // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always | |
453 | // change both normal and selected image - otherwise the change simply | |
454 | // doesn't take place! | |
455 | DoSetItemImages(item, GetItemImage(item), image); | |
456 | } | |
457 | ||
458 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const | |
459 | { | |
460 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
461 | if ( !DoGetItem(&tvItem) ) | |
462 | { | |
463 | return NULL; | |
464 | } | |
465 | ||
466 | return (wxTreeItemData *)tvItem.lParam; | |
467 | } | |
468 | ||
469 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) | |
470 | { | |
471 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
472 | tvItem.lParam = (LPARAM)data; | |
473 | DoSetItem(&tvItem); | |
474 | } | |
475 | ||
476 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) | |
477 | { | |
478 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); | |
479 | tvItem.cChildren = (int)has; | |
480 | DoSetItem(&tvItem); | |
481 | } | |
482 | ||
483 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) | |
484 | { | |
485 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
486 | tvItem.state = bold ? TVIS_BOLD : 0; | |
487 | DoSetItem(&tvItem); | |
488 | } | |
489 | ||
490 | void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight) | |
491 | { | |
492 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED); | |
493 | tvItem.state = highlight ? TVIS_DROPHILITED : 0; | |
494 | DoSetItem(&tvItem); | |
495 | } | |
496 | ||
497 | // ---------------------------------------------------------------------------- | |
498 | // Item status | |
499 | // ---------------------------------------------------------------------------- | |
500 | ||
501 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const | |
502 | { | |
503 | // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect | |
504 | RECT rect; | |
505 | return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0; | |
506 | ||
507 | } | |
508 | ||
509 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const | |
510 | { | |
511 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); | |
512 | DoGetItem(&tvItem); | |
513 | ||
514 | return tvItem.cChildren != 0; | |
515 | } | |
516 | ||
517 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const | |
518 | { | |
519 | // probably not a good idea to put it here | |
520 | //wxASSERT( ItemHasChildren(item) ); | |
521 | ||
522 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); | |
523 | DoGetItem(&tvItem); | |
524 | ||
525 | return (tvItem.state & TVIS_EXPANDED) != 0; | |
526 | } | |
527 | ||
528 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const | |
529 | { | |
530 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); | |
531 | DoGetItem(&tvItem); | |
532 | ||
533 | return (tvItem.state & TVIS_SELECTED) != 0; | |
534 | } | |
535 | ||
536 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const | |
537 | { | |
538 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
539 | DoGetItem(&tvItem); | |
540 | ||
541 | return (tvItem.state & TVIS_BOLD) != 0; | |
542 | } | |
543 | ||
544 | // ---------------------------------------------------------------------------- | |
545 | // navigation | |
546 | // ---------------------------------------------------------------------------- | |
547 | ||
548 | wxTreeItemId wxTreeCtrl::GetRootItem() const | |
549 | { | |
550 | return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd())); | |
551 | } | |
552 | ||
553 | wxTreeItemId wxTreeCtrl::GetSelection() const | |
554 | { | |
555 | wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (WXHTREEITEM)0, | |
556 | _T("this only works with single selection controls") ); | |
557 | ||
558 | return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd())); | |
559 | } | |
560 | ||
561 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const | |
562 | { | |
563 | return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); | |
564 | } | |
565 | ||
566 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, | |
567 | long& _cookie) const | |
568 | { | |
569 | // remember the last child returned in 'cookie' | |
570 | _cookie = (long)TreeView_GetChild(GetHwnd(), (HTREEITEM) (WXHTREEITEM)item); | |
571 | ||
572 | return wxTreeItemId((WXHTREEITEM)_cookie); | |
573 | } | |
574 | ||
575 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), | |
576 | long& _cookie) const | |
577 | { | |
578 | wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(), | |
579 | (HTREEITEM)(WXHTREEITEM)_cookie)); | |
580 | _cookie = (long)l; | |
581 | ||
582 | return l; | |
583 | } | |
584 | ||
585 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const | |
586 | { | |
587 | // can this be done more efficiently? | |
588 | long cookie; | |
589 | ||
590 | wxTreeItemId childLast, | |
591 | child = GetFirstChild(item, cookie); | |
592 | while ( child.IsOk() ) | |
593 | { | |
594 | childLast = child; | |
595 | child = GetNextChild(item, cookie); | |
596 | } | |
597 | ||
598 | return childLast; | |
599 | } | |
600 | ||
601 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const | |
602 | { | |
603 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); | |
604 | } | |
605 | ||
606 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const | |
607 | { | |
608 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); | |
609 | } | |
610 | ||
611 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const | |
612 | { | |
613 | return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd())); | |
614 | } | |
615 | ||
616 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const | |
617 | { | |
618 | wxASSERT_MSG( IsVisible(item), _T("The item you call GetNextVisible() " | |
619 | "for must be visible itself!")); | |
620 | ||
621 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); | |
622 | } | |
623 | ||
624 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const | |
625 | { | |
626 | wxASSERT_MSG( IsVisible(item), _T("The item you call GetPrevVisible() " | |
627 | "for must be visible itself!")); | |
628 | ||
629 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); | |
630 | } | |
631 | ||
632 | // ---------------------------------------------------------------------------- | |
633 | // multiple selections emulation | |
634 | // ---------------------------------------------------------------------------- | |
635 | ||
636 | bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const | |
637 | { | |
638 | // receive the desired information. | |
639 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
640 | DoGetItem(&tvItem); | |
641 | ||
642 | // state image indices are 1 based | |
643 | return ((tvItem.state >> 12) - 1) == 1; | |
644 | } | |
645 | ||
646 | void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check) | |
647 | { | |
648 | // receive the desired information. | |
649 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
650 | ||
651 | // state images are one-based | |
652 | tvItem.state = (check ? 2 : 1) << 12; | |
653 | ||
654 | DoSetItem(&tvItem); | |
655 | } | |
656 | ||
657 | // internal class for getting the selected | |
658 | ||
659 | class TraverseSelections : public wxTreeTraversal | |
660 | { | |
661 | public: | |
662 | TraverseSelections(const wxTreeCtrl *tree, | |
663 | wxArrayTreeItemIds& selections) | |
664 | : wxTreeTraversal(tree), m_selections(selections) | |
665 | { | |
666 | m_selections.Empty(); | |
667 | ||
668 | DoTraverse(tree->GetRootItem()); | |
669 | } | |
670 | ||
671 | virtual bool OnVisit(const wxTreeItemId& item) | |
672 | { | |
673 | if ( GetTree()->IsItemChecked(item) ) | |
674 | { | |
675 | m_selections.Add(item); | |
676 | } | |
677 | ||
678 | return TRUE; | |
679 | } | |
680 | ||
681 | private: | |
682 | wxArrayTreeItemIds& m_selections; | |
683 | }; | |
684 | ||
685 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const | |
686 | { | |
687 | TraverseSelections selector(this, selections); | |
688 | ||
689 | return selections.GetCount(); | |
690 | } | |
691 | ||
692 | // ---------------------------------------------------------------------------- | |
693 | // Usual operations | |
694 | // ---------------------------------------------------------------------------- | |
695 | ||
696 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, | |
697 | wxTreeItemId hInsertAfter, | |
698 | const wxString& text, | |
699 | int image, int selectedImage, | |
700 | wxTreeItemData *data) | |
701 | { | |
702 | TV_INSERTSTRUCT tvIns; | |
703 | tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent; | |
704 | tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter; | |
705 | ||
706 | // This is how we insert the item as the first child: supply a NULL hInsertAfter | |
707 | if (tvIns.hInsertAfter == (HTREEITEM) 0) | |
708 | { | |
709 | tvIns.hInsertAfter = TVI_FIRST; | |
710 | } | |
711 | ||
712 | UINT mask = 0; | |
713 | if ( !text.IsEmpty() ) | |
714 | { | |
715 | mask |= TVIF_TEXT; | |
716 | tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok | |
717 | } | |
718 | ||
719 | if ( image != -1 ) | |
720 | { | |
721 | mask |= TVIF_IMAGE; | |
722 | tvIns.item.iImage = image; | |
723 | ||
724 | if ( selectedImage == -1 ) | |
725 | { | |
726 | // take the same image for selected icon if not specified | |
727 | selectedImage = image; | |
728 | } | |
729 | } | |
730 | ||
731 | if ( selectedImage != -1 ) | |
732 | { | |
733 | mask |= TVIF_SELECTEDIMAGE; | |
734 | tvIns.item.iSelectedImage = selectedImage; | |
735 | } | |
736 | ||
737 | if ( data != NULL ) | |
738 | { | |
739 | mask |= TVIF_PARAM; | |
740 | tvIns.item.lParam = (LPARAM)data; | |
741 | } | |
742 | ||
743 | tvIns.item.mask = mask; | |
744 | ||
745 | HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns); | |
746 | if ( id == 0 ) | |
747 | { | |
748 | wxLogLastError("TreeView_InsertItem"); | |
749 | } | |
750 | ||
751 | if ( data != NULL ) | |
752 | { | |
753 | // associate the application tree item with Win32 tree item handle | |
754 | data->SetId((WXHTREEITEM)id); | |
755 | } | |
756 | ||
757 | return wxTreeItemId((WXHTREEITEM)id); | |
758 | } | |
759 | ||
760 | // for compatibility only | |
761 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, | |
762 | const wxString& text, | |
763 | int image, int selImage, | |
764 | long insertAfter) | |
765 | { | |
766 | return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text, | |
767 | image, selImage, NULL); | |
768 | } | |
769 | ||
770 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, | |
771 | int image, int selectedImage, | |
772 | wxTreeItemData *data) | |
773 | { | |
774 | return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0, | |
775 | text, image, selectedImage, data); | |
776 | } | |
777 | ||
778 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, | |
779 | const wxString& text, | |
780 | int image, int selectedImage, | |
781 | wxTreeItemData *data) | |
782 | { | |
783 | return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST, | |
784 | text, image, selectedImage, data); | |
785 | } | |
786 | ||
787 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, | |
788 | const wxTreeItemId& idPrevious, | |
789 | const wxString& text, | |
790 | int image, int selectedImage, | |
791 | wxTreeItemData *data) | |
792 | { | |
793 | return DoInsertItem(parent, idPrevious, text, image, selectedImage, data); | |
794 | } | |
795 | ||
796 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent, | |
797 | const wxString& text, | |
798 | int image, int selectedImage, | |
799 | wxTreeItemData *data) | |
800 | { | |
801 | return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST, | |
802 | text, image, selectedImage, data); | |
803 | } | |
804 | ||
805 | void wxTreeCtrl::Delete(const wxTreeItemId& item) | |
806 | { | |
807 | if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item) ) | |
808 | { | |
809 | wxLogLastError("TreeView_DeleteItem"); | |
810 | } | |
811 | } | |
812 | ||
813 | // delete all children (but don't delete the item itself) | |
814 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item) | |
815 | { | |
816 | long cookie; | |
817 | ||
818 | wxArrayLong children; | |
819 | wxTreeItemId child = GetFirstChild(item, cookie); | |
820 | while ( child.IsOk() ) | |
821 | { | |
822 | children.Add((long)(WXHTREEITEM)child); | |
823 | ||
824 | child = GetNextChild(item, cookie); | |
825 | } | |
826 | ||
827 | size_t nCount = children.Count(); | |
828 | for ( size_t n = 0; n < nCount; n++ ) | |
829 | { | |
830 | if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) ) | |
831 | { | |
832 | wxLogLastError("TreeView_DeleteItem"); | |
833 | } | |
834 | } | |
835 | } | |
836 | ||
837 | void wxTreeCtrl::DeleteAllItems() | |
838 | { | |
839 | if ( !TreeView_DeleteAllItems(GetHwnd()) ) | |
840 | { | |
841 | wxLogLastError("TreeView_DeleteAllItems"); | |
842 | } | |
843 | } | |
844 | ||
845 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) | |
846 | { | |
847 | wxASSERT_MSG( flag == TVE_COLLAPSE || | |
848 | flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) || | |
849 | flag == TVE_EXPAND || | |
850 | flag == TVE_TOGGLE, | |
851 | _T("Unknown flag in wxTreeCtrl::DoExpand") ); | |
852 | ||
853 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must | |
854 | // emulate them. This behaviour has changed slightly with comctl32.dll | |
855 | // v 4.70 - now it does send them but only the first time. To maintain | |
856 | // compatible behaviour and also in order to not have surprises with the | |
857 | // future versions, don't rely on this and still do everything ourselves. | |
858 | // To avoid that the messages be sent twice when the item is expanded for | |
859 | // the first time we must clear TVIS_EXPANDEDONCE style manually. | |
860 | ||
861 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE); | |
862 | tvItem.state = 0; | |
863 | DoSetItem(&tvItem); | |
864 | ||
865 | if ( TreeView_Expand(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item, flag) != 0 ) | |
866 | { | |
867 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
868 | event.m_item = item; | |
869 | ||
870 | bool isExpanded = IsExpanded(item); | |
871 | ||
872 | event.SetEventObject(this); | |
873 | ||
874 | // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded | |
875 | event.SetEventType(g_events[isExpanded][TRUE]); | |
876 | GetEventHandler()->ProcessEvent(event); | |
877 | ||
878 | event.SetEventType(g_events[isExpanded][FALSE]); | |
879 | GetEventHandler()->ProcessEvent(event); | |
880 | } | |
881 | //else: change didn't took place, so do nothing at all | |
882 | } | |
883 | ||
884 | void wxTreeCtrl::Expand(const wxTreeItemId& item) | |
885 | { | |
886 | DoExpand(item, TVE_EXPAND); | |
887 | } | |
888 | ||
889 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) | |
890 | { | |
891 | DoExpand(item, TVE_COLLAPSE); | |
892 | } | |
893 | ||
894 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) | |
895 | { | |
896 | DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET); | |
897 | } | |
898 | ||
899 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) | |
900 | { | |
901 | DoExpand(item, TVE_TOGGLE); | |
902 | } | |
903 | ||
904 | void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action) | |
905 | { | |
906 | DoExpand(item, action); | |
907 | } | |
908 | ||
909 | void wxTreeCtrl::Unselect() | |
910 | { | |
911 | wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), _T("doesn't make sense") ); | |
912 | ||
913 | // just remove the selection | |
914 | SelectItem(wxTreeItemId((WXHTREEITEM) 0)); | |
915 | } | |
916 | ||
917 | void wxTreeCtrl::UnselectAll() | |
918 | { | |
919 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
920 | { | |
921 | wxArrayTreeItemIds selections; | |
922 | size_t count = GetSelections(selections); | |
923 | for ( size_t n = 0; n < count; n++ ) | |
924 | { | |
925 | SetItemCheck(selections[n], FALSE); | |
926 | } | |
927 | } | |
928 | else | |
929 | { | |
930 | // just remove the selection | |
931 | Unselect(); | |
932 | } | |
933 | } | |
934 | ||
935 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item) | |
936 | { | |
937 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
938 | { | |
939 | // selecting the item means checking it | |
940 | SetItemCheck(item); | |
941 | } | |
942 | else | |
943 | { | |
944 | // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive | |
945 | // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so | |
946 | // send them ourselves | |
947 | ||
948 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
949 | event.m_item = item; | |
950 | event.SetEventObject(this); | |
951 | ||
952 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING); | |
953 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
954 | { | |
955 | if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) ) | |
956 | { | |
957 | wxLogLastError("TreeView_SelectItem"); | |
958 | } | |
959 | else | |
960 | { | |
961 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); | |
962 | (void)GetEventHandler()->ProcessEvent(event); | |
963 | } | |
964 | } | |
965 | //else: program vetoed the change | |
966 | } | |
967 | } | |
968 | ||
969 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) | |
970 | { | |
971 | // no error return | |
972 | TreeView_EnsureVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item); | |
973 | } | |
974 | ||
975 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) | |
976 | { | |
977 | if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) ) | |
978 | { | |
979 | wxLogLastError("TreeView_SelectSetFirstVisible"); | |
980 | } | |
981 | } | |
982 | ||
983 | wxTextCtrl* wxTreeCtrl::GetEditControl() const | |
984 | { | |
985 | return m_textCtrl; | |
986 | } | |
987 | ||
988 | void wxTreeCtrl::DeleteTextCtrl() | |
989 | { | |
990 | if ( m_textCtrl ) | |
991 | { | |
992 | m_textCtrl->UnsubclassWin(); | |
993 | m_textCtrl->SetHWND(0); | |
994 | delete m_textCtrl; | |
995 | m_textCtrl = NULL; | |
996 | } | |
997 | } | |
998 | ||
999 | wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, | |
1000 | wxClassInfo* textControlClass) | |
1001 | { | |
1002 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
1003 | ||
1004 | HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item); | |
1005 | ||
1006 | // this is not an error - the TVN_BEGINLABELEDIT handler might have | |
1007 | // returned FALSE | |
1008 | if ( !hWnd ) | |
1009 | { | |
1010 | return NULL; | |
1011 | } | |
1012 | ||
1013 | DeleteTextCtrl(); | |
1014 | ||
1015 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); | |
1016 | m_textCtrl->SetHWND((WXHWND)hWnd); | |
1017 | m_textCtrl->SubclassWin((WXHWND)hWnd); | |
1018 | ||
1019 | return m_textCtrl; | |
1020 | } | |
1021 | ||
1022 | // End label editing, optionally cancelling the edit | |
1023 | void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) | |
1024 | { | |
1025 | TreeView_EndEditLabelNow(GetHwnd(), discardChanges); | |
1026 | ||
1027 | DeleteTextCtrl(); | |
1028 | } | |
1029 | ||
1030 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) | |
1031 | { | |
1032 | TV_HITTESTINFO hitTestInfo; | |
1033 | hitTestInfo.pt.x = (int)point.x; | |
1034 | hitTestInfo.pt.y = (int)point.y; | |
1035 | ||
1036 | TreeView_HitTest(GetHwnd(), &hitTestInfo); | |
1037 | ||
1038 | flags = 0; | |
1039 | ||
1040 | // avoid repetition | |
1041 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ | |
1042 | flags |= wxTREE_HITTEST_##flag | |
1043 | ||
1044 | TRANSLATE_FLAG(ABOVE); | |
1045 | TRANSLATE_FLAG(BELOW); | |
1046 | TRANSLATE_FLAG(NOWHERE); | |
1047 | TRANSLATE_FLAG(ONITEMBUTTON); | |
1048 | TRANSLATE_FLAG(ONITEMICON); | |
1049 | TRANSLATE_FLAG(ONITEMINDENT); | |
1050 | TRANSLATE_FLAG(ONITEMLABEL); | |
1051 | TRANSLATE_FLAG(ONITEMRIGHT); | |
1052 | TRANSLATE_FLAG(ONITEMSTATEICON); | |
1053 | TRANSLATE_FLAG(TOLEFT); | |
1054 | TRANSLATE_FLAG(TORIGHT); | |
1055 | ||
1056 | #undef TRANSLATE_FLAG | |
1057 | ||
1058 | return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem); | |
1059 | } | |
1060 | ||
1061 | bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item, | |
1062 | wxRect& rect, | |
1063 | bool textOnly) const | |
1064 | { | |
1065 | RECT rc; | |
1066 | if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item, | |
1067 | &rc, textOnly) ) | |
1068 | { | |
1069 | rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom)); | |
1070 | ||
1071 | return TRUE; | |
1072 | } | |
1073 | else | |
1074 | { | |
1075 | // couldn't retrieve rect: for example, item isn't visible | |
1076 | return FALSE; | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | // ---------------------------------------------------------------------------- | |
1081 | // sorting stuff | |
1082 | // ---------------------------------------------------------------------------- | |
1083 | ||
1084 | static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1, | |
1085 | wxTreeItemData *pItem2, | |
1086 | wxTreeCtrl *tree) | |
1087 | { | |
1088 | wxCHECK_MSG( pItem1 && pItem2, 0, | |
1089 | _T("sorting tree without data doesn't make sense") ); | |
1090 | ||
1091 | return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId()); | |
1092 | } | |
1093 | ||
1094 | int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1, | |
1095 | const wxTreeItemId& item2) | |
1096 | { | |
1097 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); | |
1098 | } | |
1099 | ||
1100 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item) | |
1101 | { | |
1102 | // rely on the fact that TreeView_SortChildren does the same thing as our | |
1103 | // default behaviour, i.e. sorts items alphabetically and so call it | |
1104 | // directly if we're not in derived class (much more efficient!) | |
1105 | if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) ) | |
1106 | { | |
1107 | TreeView_SortChildren(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item, 0); | |
1108 | } | |
1109 | else | |
1110 | { | |
1111 | TV_SORTCB tvSort; | |
1112 | tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item; | |
1113 | tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback; | |
1114 | tvSort.lParam = (LPARAM)this; | |
1115 | TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */); | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | // ---------------------------------------------------------------------------- | |
1120 | // implementation | |
1121 | // ---------------------------------------------------------------------------- | |
1122 | ||
1123 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id) | |
1124 | { | |
1125 | if ( cmd == EN_UPDATE ) | |
1126 | { | |
1127 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); | |
1128 | event.SetEventObject( this ); | |
1129 | ProcessCommand(event); | |
1130 | } | |
1131 | else if ( cmd == EN_KILLFOCUS ) | |
1132 | { | |
1133 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); | |
1134 | event.SetEventObject( this ); | |
1135 | ProcessCommand(event); | |
1136 | } | |
1137 | else | |
1138 | { | |
1139 | // nothing done | |
1140 | return FALSE; | |
1141 | } | |
1142 | ||
1143 | // command processed | |
1144 | return TRUE; | |
1145 | } | |
1146 | ||
1147 | // process WM_NOTIFY Windows message | |
1148 | bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) | |
1149 | { | |
1150 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1151 | wxEventType eventType = wxEVT_NULL; | |
1152 | NMHDR *hdr = (NMHDR *)lParam; | |
1153 | ||
1154 | switch ( hdr->code ) | |
1155 | { | |
1156 | case TVN_BEGINDRAG: | |
1157 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
1158 | // fall through | |
1159 | ||
1160 | case TVN_BEGINRDRAG: | |
1161 | { | |
1162 | if ( eventType == wxEVT_NULL ) | |
1163 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; | |
1164 | //else: left drag, already set above | |
1165 | ||
1166 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
1167 | ||
1168 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; | |
1169 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); | |
1170 | break; | |
1171 | } | |
1172 | ||
1173 | case TVN_BEGINLABELEDIT: | |
1174 | { | |
1175 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; | |
1176 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
1177 | ||
1178 | event.m_item = (WXHTREEITEM) info->item.hItem; | |
1179 | event.m_label = info->item.pszText; | |
1180 | break; | |
1181 | } | |
1182 | ||
1183 | case TVN_DELETEITEM: | |
1184 | { | |
1185 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; | |
1186 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
1187 | ||
1188 | event.m_item = (WXHTREEITEM) tv->itemOld.hItem; | |
1189 | break; | |
1190 | } | |
1191 | ||
1192 | case TVN_ENDLABELEDIT: | |
1193 | { | |
1194 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; | |
1195 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
1196 | ||
1197 | event.m_item = (WXHTREEITEM)info->item.hItem; | |
1198 | event.m_label = info->item.pszText; | |
1199 | break; | |
1200 | } | |
1201 | ||
1202 | case TVN_GETDISPINFO: | |
1203 | eventType = wxEVT_COMMAND_TREE_GET_INFO; | |
1204 | // fall through | |
1205 | ||
1206 | case TVN_SETDISPINFO: | |
1207 | { | |
1208 | if ( eventType == wxEVT_NULL ) | |
1209 | eventType = wxEVT_COMMAND_TREE_SET_INFO; | |
1210 | //else: get, already set above | |
1211 | ||
1212 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
1213 | ||
1214 | event.m_item = (WXHTREEITEM) info->item.hItem; | |
1215 | break; | |
1216 | } | |
1217 | ||
1218 | case TVN_ITEMEXPANDING: | |
1219 | event.m_code = FALSE; | |
1220 | // fall through | |
1221 | ||
1222 | case TVN_ITEMEXPANDED: | |
1223 | { | |
1224 | NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam; | |
1225 | ||
1226 | bool expand = FALSE; | |
1227 | switch ( tv->action ) | |
1228 | { | |
1229 | case TVE_EXPAND: | |
1230 | expand = TRUE; | |
1231 | break; | |
1232 | ||
1233 | case TVE_COLLAPSE: | |
1234 | expand = FALSE; | |
1235 | break; | |
1236 | ||
1237 | default: | |
1238 | wxLogDebug(_T("unexpected code %d in TVN_ITEMEXPAND " | |
1239 | "message"), tv->action); | |
1240 | } | |
1241 | ||
1242 | bool ing = (hdr->code == TVN_ITEMEXPANDING); | |
1243 | eventType = g_events[expand][ing]; | |
1244 | ||
1245 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; | |
1246 | break; | |
1247 | } | |
1248 | ||
1249 | case TVN_KEYDOWN: | |
1250 | { | |
1251 | eventType = wxEVT_COMMAND_TREE_KEY_DOWN; | |
1252 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; | |
1253 | ||
1254 | event.m_code = wxCharCodeMSWToWX(info->wVKey); | |
1255 | ||
1256 | // a separate event for this case | |
1257 | if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN ) | |
1258 | { | |
1259 | wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, | |
1260 | m_windowId); | |
1261 | event2.SetEventObject(this); | |
1262 | ||
1263 | GetEventHandler()->ProcessEvent(event2); | |
1264 | } | |
1265 | break; | |
1266 | } | |
1267 | ||
1268 | case TVN_SELCHANGED: | |
1269 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; | |
1270 | // fall through | |
1271 | ||
1272 | case TVN_SELCHANGING: | |
1273 | { | |
1274 | if ( eventType == wxEVT_NULL ) | |
1275 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; | |
1276 | //else: already set above | |
1277 | ||
1278 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
1279 | ||
1280 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; | |
1281 | event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem; | |
1282 | break; | |
1283 | } | |
1284 | ||
1285 | default: | |
1286 | return wxControl::MSWOnNotify(idCtrl, lParam, result); | |
1287 | } | |
1288 | ||
1289 | event.SetEventObject(this); | |
1290 | event.SetEventType(eventType); | |
1291 | ||
1292 | bool processed = GetEventHandler()->ProcessEvent(event); | |
1293 | ||
1294 | // post processing | |
1295 | switch ( hdr->code ) | |
1296 | { | |
1297 | case TVN_DELETEITEM: | |
1298 | { | |
1299 | // NB: we might process this message using wxWindows event | |
1300 | // tables, but due to overhead of wxWin event system we | |
1301 | // prefer to do it here ourself (otherwise deleting a tree | |
1302 | // with many items is just too slow) | |
1303 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
1304 | wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam; | |
1305 | delete data; // may be NULL, ok | |
1306 | ||
1307 | processed = TRUE; // Make sure we don't get called twice | |
1308 | } | |
1309 | break; | |
1310 | ||
1311 | case TVN_BEGINLABELEDIT: | |
1312 | // return TRUE to cancel label editing | |
1313 | *result = !event.IsAllowed(); | |
1314 | break; | |
1315 | ||
1316 | case TVN_ENDLABELEDIT: | |
1317 | // return TRUE to set the label to the new string | |
1318 | *result = event.IsAllowed(); | |
1319 | ||
1320 | // ensure that we don't have the text ctrl which is going to be | |
1321 | // deleted any more | |
1322 | DeleteTextCtrl(); | |
1323 | break; | |
1324 | ||
1325 | case TVN_SELCHANGING: | |
1326 | case TVN_ITEMEXPANDING: | |
1327 | // return TRUE to prevent the action from happening | |
1328 | *result = !event.IsAllowed(); | |
1329 | break; | |
1330 | ||
1331 | //default: | |
1332 | // for the other messages the return value is ignored and there is | |
1333 | // nothing special to do | |
1334 | } | |
1335 | ||
1336 | return processed; | |
1337 | } | |
1338 | ||
1339 | // ---------------------------------------------------------------------------- | |
1340 | // Tree event | |
1341 | // ---------------------------------------------------------------------------- | |
1342 | ||
1343 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent) | |
1344 | ||
1345 | wxTreeEvent::wxTreeEvent(wxEventType commandType, int id) | |
1346 | : wxNotifyEvent(commandType, id) | |
1347 | { | |
1348 | m_code = 0; | |
1349 | m_itemOld = 0; | |
1350 | } | |
1351 | ||
1352 | #endif // __WIN95__ | |
1353 |