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