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