]>
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/msw/private.h" |
31 | ||
f6bcfd97 BP |
32 | // Set this to 1 to be _absolutely_ sure that repainting will work for all comctl32.dll versions |
33 | #define wxUSE_COMCTL32_SAFELY 0 | |
34 | ||
0c589ad0 BM |
35 | // Mingw32 is a bit mental even though this is done in winundef |
36 | #ifdef GetFirstChild | |
d220ae32 | 37 | #undef GetFirstChild |
0c589ad0 | 38 | #endif |
d220ae32 | 39 | |
0c589ad0 | 40 | #ifdef GetNextSibling |
d220ae32 | 41 | #undef GetNextSibling |
2bda0e17 KB |
42 | #endif |
43 | ||
2bda0e17 KB |
44 | #if defined(__WIN95__) |
45 | ||
08b7c251 | 46 | #include "wx/log.h" |
ce3ed50d | 47 | #include "wx/dynarray.h" |
08b7c251 | 48 | #include "wx/imaglist.h" |
77cff606 | 49 | #include "wx/settings.h" |
484523cf | 50 | #include "wx/msw/treectrl.h" |
23f681ec VZ |
51 | #include "wx/msw/dragimag.h" |
52 | ||
c42404a5 VZ |
53 | #ifdef __GNUWIN32_OLD__ |
54 | #include "wx/msw/gnuwin32/extra.h" | |
65fd5cb0 | 55 | #endif |
9a05fd8d | 56 | |
c42404a5 | 57 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) |
08b7c251 | 58 | #include <commctrl.h> |
2bda0e17 KB |
59 | #endif |
60 | ||
61 | // Bug in headers, sometimes | |
62 | #ifndef TVIS_FOCUSED | |
08b7c251 | 63 | #define TVIS_FOCUSED 0x0001 |
2bda0e17 KB |
64 | #endif |
65 | ||
bb448552 VZ |
66 | #ifndef TV_FIRST |
67 | #define TV_FIRST 0x1100 | |
68 | #endif | |
69 | ||
b94ae1ea VZ |
70 | #ifndef TVS_CHECKBOXES |
71 | #define TVS_CHECKBOXES 0x0100 | |
72 | #endif | |
73 | ||
bb448552 VZ |
74 | // old headers might miss these messages (comctl32.dll 4.71+ only) |
75 | #ifndef TVM_SETBKCOLOR | |
76 | #define TVM_SETBKCOLOR (TV_FIRST + 29) | |
77 | #define TVM_SETTEXTCOLOR (TV_FIRST + 30) | |
78 | #endif | |
79 | ||
3f7bc32b VZ |
80 | // a macro to hide the ugliness of nested casts |
81 | #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item) | |
82 | ||
83 | // the native control doesn't support multiple selections under MSW and we | |
84 | // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let | |
85 | // checkboxes be the selection status (checked == selected) or by really | |
86 | // emulating everything, i.e. intercepting mouse and key events &c. The first | |
87 | // approach is much easier but doesn't work with comctl32.dll < 4.71 and also | |
88 | // looks quite ugly. | |
89 | #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0 | |
90 | ||
2e4df4bf | 91 | |
3f7bc32b VZ |
92 | // ---------------------------------------------------------------------------- |
93 | // private functions | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | // wrapper for TreeView_HitTest | |
97 | static HTREEITEM GetItemFromPoint(HWND hwndTV, int x, int y) | |
98 | { | |
99 | TV_HITTESTINFO tvht; | |
100 | tvht.pt.x = x; | |
101 | tvht.pt.y = y; | |
102 | ||
3f7bc32b VZ |
103 | return (HTREEITEM)TreeView_HitTest(hwndTV, &tvht); |
104 | } | |
105 | ||
106 | #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
107 | ||
108 | // wrappers for TreeView_GetItem/TreeView_SetItem | |
109 | static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem) | |
110 | { | |
111 | TV_ITEM tvi; | |
112 | tvi.mask = TVIF_STATE | TVIF_HANDLE; | |
113 | tvi.stateMask = TVIS_SELECTED; | |
114 | tvi.hItem = hItem; | |
115 | ||
116 | if ( !TreeView_GetItem(hwndTV, &tvi) ) | |
117 | { | |
f6bcfd97 | 118 | wxLogLastError(wxT("TreeView_GetItem")); |
3f7bc32b VZ |
119 | } |
120 | ||
121 | return (tvi.state & TVIS_SELECTED) != 0; | |
122 | } | |
123 | ||
124 | static void SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = TRUE) | |
125 | { | |
126 | TV_ITEM tvi; | |
127 | tvi.mask = TVIF_STATE | TVIF_HANDLE; | |
128 | tvi.stateMask = TVIS_SELECTED; | |
129 | tvi.state = select ? TVIS_SELECTED : 0; | |
130 | tvi.hItem = hItem; | |
131 | ||
132 | if ( TreeView_SetItem(hwndTV, &tvi) == -1 ) | |
133 | { | |
f6bcfd97 | 134 | wxLogLastError(wxT("TreeView_SetItem")); |
3f7bc32b VZ |
135 | } |
136 | } | |
137 | ||
138 | static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem) | |
139 | { | |
140 | SelectItem(hwndTV, htItem, FALSE); | |
141 | } | |
142 | ||
143 | static inline void ToggleItemSelection(HWND hwndTV, HTREEITEM htItem) | |
144 | { | |
145 | SelectItem(hwndTV, htItem, !IsItemSelected(hwndTV, htItem)); | |
146 | } | |
147 | ||
148 | // helper function which selects all items in a range and, optionally, | |
149 | // unselects all others | |
150 | static void SelectRange(HWND hwndTV, | |
151 | HTREEITEM htFirst, | |
152 | HTREEITEM htLast, | |
153 | bool unselectOthers = TRUE) | |
154 | { | |
155 | // find the first (or last) item and select it | |
156 | bool cont = TRUE; | |
2c8e4738 | 157 | HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV); |
3f7bc32b VZ |
158 | while ( htItem && cont ) |
159 | { | |
160 | if ( (htItem == htFirst) || (htItem == htLast) ) | |
161 | { | |
162 | if ( !IsItemSelected(hwndTV, htItem) ) | |
163 | { | |
164 | SelectItem(hwndTV, htItem); | |
165 | } | |
166 | ||
167 | cont = FALSE; | |
168 | } | |
169 | else | |
170 | { | |
171 | if ( unselectOthers && IsItemSelected(hwndTV, htItem) ) | |
172 | { | |
173 | UnselectItem(hwndTV, htItem); | |
174 | } | |
175 | } | |
176 | ||
2c8e4738 | 177 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); |
3f7bc32b VZ |
178 | } |
179 | ||
180 | // select the items in range | |
181 | cont = htFirst != htLast; | |
182 | while ( htItem && cont ) | |
183 | { | |
184 | if ( !IsItemSelected(hwndTV, htItem) ) | |
185 | { | |
186 | SelectItem(hwndTV, htItem); | |
187 | } | |
188 | ||
189 | cont = (htItem != htFirst) && (htItem != htLast); | |
190 | ||
2c8e4738 | 191 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); |
3f7bc32b VZ |
192 | } |
193 | ||
194 | // unselect the rest | |
195 | if ( unselectOthers ) | |
196 | { | |
197 | while ( htItem ) | |
198 | { | |
199 | if ( IsItemSelected(hwndTV, htItem) ) | |
200 | { | |
201 | UnselectItem(hwndTV, htItem); | |
202 | } | |
203 | ||
2c8e4738 | 204 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); |
3f7bc32b VZ |
205 | } |
206 | } | |
207 | ||
208 | // seems to be necessary - otherwise the just selected items don't always | |
209 | // appear as selected | |
210 | UpdateWindow(hwndTV); | |
211 | } | |
212 | ||
213 | // helper function which tricks the standard control into changing the focused | |
214 | // item without changing anything else (if someone knows why Microsoft doesn't | |
215 | // allow to do it by just setting TVIS_FOCUSED flag, please tell me!) | |
216 | static void SetFocus(HWND hwndTV, HTREEITEM htItem) | |
217 | { | |
218 | // the current focus | |
2c8e4738 | 219 | HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV); |
3f7bc32b VZ |
220 | |
221 | if ( htItem ) | |
222 | { | |
223 | // set the focus | |
224 | if ( htItem != htFocus ) | |
225 | { | |
226 | // remember the selection state of the item | |
227 | bool wasSelected = IsItemSelected(hwndTV, htItem); | |
228 | ||
229 | if ( htFocus && IsItemSelected(hwndTV, htFocus) ) | |
230 | { | |
231 | // prevent the tree from unselecting the old focus which it | |
232 | // would do by default (TreeView_SelectItem unselects the | |
233 | // focused item) | |
234 | TreeView_SelectItem(hwndTV, 0); | |
235 | SelectItem(hwndTV, htFocus); | |
236 | } | |
237 | ||
238 | TreeView_SelectItem(hwndTV, htItem); | |
239 | ||
240 | if ( !wasSelected ) | |
241 | { | |
242 | // need to clear the selection which TreeView_SelectItem() gave | |
243 | // us | |
244 | UnselectItem(hwndTV, htItem); | |
245 | } | |
246 | //else: was selected, still selected - ok | |
247 | } | |
248 | //else: nothing to do, focus already there | |
249 | } | |
250 | else | |
251 | { | |
252 | if ( htFocus ) | |
253 | { | |
254 | bool wasFocusSelected = IsItemSelected(hwndTV, htFocus); | |
255 | ||
256 | // just clear the focus | |
257 | TreeView_SelectItem(hwndTV, 0); | |
258 | ||
259 | if ( wasFocusSelected ) | |
260 | { | |
261 | // restore the selection state | |
262 | SelectItem(hwndTV, htFocus); | |
263 | } | |
264 | } | |
265 | //else: nothing to do, no focus already | |
266 | } | |
267 | } | |
268 | ||
269 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
270 | ||
08b7c251 VZ |
271 | // ---------------------------------------------------------------------------- |
272 | // private classes | |
273 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 274 | |
08b7c251 | 275 | // a convenient wrapper around TV_ITEM struct which adds a ctor |
f3ef286f | 276 | #ifdef __VISUALC__ |
3f7bc32b | 277 | #pragma warning( disable : 4097 ) // inheriting from typedef |
f3ef286f JS |
278 | #endif |
279 | ||
08b7c251 VZ |
280 | struct wxTreeViewItem : public TV_ITEM |
281 | { | |
9dfbf520 VZ |
282 | wxTreeViewItem(const wxTreeItemId& item, // the item handle |
283 | UINT mask_, // fields which are valid | |
284 | UINT stateMask_ = 0) // for TVIF_STATE only | |
08b7c251 | 285 | { |
9dfbf520 VZ |
286 | // hItem member is always valid |
287 | mask = mask_ | TVIF_HANDLE; | |
08b7c251 | 288 | stateMask = stateMask_; |
3f7bc32b | 289 | hItem = HITEM(item); |
08b7c251 VZ |
290 | } |
291 | }; | |
f3ef286f JS |
292 | |
293 | #ifdef __VISUALC__ | |
197dd9af | 294 | #pragma warning( default : 4097 ) |
f3ef286f | 295 | #endif |
2bda0e17 | 296 | |
9dfbf520 VZ |
297 | // a class which encapsulates the tree traversal logic: it vists all (unless |
298 | // OnVisit() returns FALSE) items under the given one | |
299 | class wxTreeTraversal | |
300 | { | |
301 | public: | |
302 | wxTreeTraversal(const wxTreeCtrl *tree) | |
303 | { | |
304 | m_tree = tree; | |
305 | } | |
306 | ||
307 | // do traverse the tree: visit all items (recursively by default) under the | |
308 | // given one; return TRUE if all items were traversed or FALSE if the | |
309 | // traversal was aborted because OnVisit returned FALSE | |
310 | bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE); | |
311 | ||
312 | // override this function to do whatever is needed for each item, return | |
313 | // FALSE to stop traversing | |
314 | virtual bool OnVisit(const wxTreeItemId& item) = 0; | |
315 | ||
316 | protected: | |
317 | const wxTreeCtrl *GetTree() const { return m_tree; } | |
318 | ||
319 | private: | |
320 | bool Traverse(const wxTreeItemId& root, bool recursively); | |
321 | ||
322 | const wxTreeCtrl *m_tree; | |
323 | }; | |
324 | ||
74b31181 VZ |
325 | // internal class for getting the selected items |
326 | class TraverseSelections : public wxTreeTraversal | |
327 | { | |
328 | public: | |
329 | TraverseSelections(const wxTreeCtrl *tree, | |
330 | wxArrayTreeItemIds& selections) | |
331 | : wxTreeTraversal(tree), m_selections(selections) | |
332 | { | |
333 | m_selections.Empty(); | |
334 | ||
335 | DoTraverse(tree->GetRootItem()); | |
336 | } | |
337 | ||
338 | virtual bool OnVisit(const wxTreeItemId& item) | |
339 | { | |
3f7bc32b | 340 | #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
74b31181 | 341 | if ( GetTree()->IsItemChecked(item) ) |
3f7bc32b VZ |
342 | #else |
343 | if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item)) ) | |
344 | #endif | |
74b31181 VZ |
345 | { |
346 | m_selections.Add(item); | |
347 | } | |
348 | ||
349 | return TRUE; | |
350 | } | |
351 | ||
e47c4d48 VZ |
352 | size_t GetCount() const { return m_selections.GetCount(); } |
353 | ||
74b31181 VZ |
354 | private: |
355 | wxArrayTreeItemIds& m_selections; | |
356 | }; | |
357 | ||
358 | // internal class for counting tree items | |
359 | class TraverseCounter : public wxTreeTraversal | |
360 | { | |
361 | public: | |
362 | TraverseCounter(const wxTreeCtrl *tree, | |
363 | const wxTreeItemId& root, | |
364 | bool recursively) | |
365 | : wxTreeTraversal(tree) | |
366 | { | |
367 | m_count = 0; | |
368 | ||
369 | DoTraverse(root, recursively); | |
370 | } | |
371 | ||
33ac7e6f | 372 | virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item)) |
74b31181 VZ |
373 | { |
374 | m_count++; | |
375 | ||
376 | return TRUE; | |
377 | } | |
378 | ||
379 | size_t GetCount() const { return m_count; } | |
380 | ||
381 | private: | |
382 | size_t m_count; | |
383 | }; | |
384 | ||
385 | // ---------------------------------------------------------------------------- | |
386 | // This class is needed for support of different images: the Win32 common | |
387 | // control natively supports only 2 images (the normal one and another for the | |
388 | // selected state). We wish to provide support for 2 more of them for folder | |
389 | // items (i.e. those which have children): for expanded state and for expanded | |
390 | // selected state. For this we use this structure to store the additional items | |
391 | // images. | |
392 | // | |
393 | // There is only one problem with this: when we retrieve the item's data, we | |
394 | // don't know whether we get a pointer to wxTreeItemData or | |
395 | // wxTreeItemIndirectData. So we have to maintain a list of all items which | |
396 | // have indirect data inside the listctrl itself. | |
397 | // ---------------------------------------------------------------------------- | |
696e1ea0 | 398 | |
74b31181 VZ |
399 | class wxTreeItemIndirectData |
400 | { | |
401 | public: | |
402 | // ctor associates this data with the item and the real item data becomes | |
403 | // available through our GetData() method | |
404 | wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item) | |
405 | { | |
406 | for ( size_t n = 0; n < WXSIZEOF(m_images); n++ ) | |
407 | { | |
408 | m_images[n] = -1; | |
409 | } | |
410 | ||
411 | // save the old data | |
412 | m_data = tree->GetItemData(item); | |
413 | ||
414 | // and set ourselves as the new one | |
415 | tree->SetIndirectItemData(item, this); | |
416 | } | |
417 | ||
418 | // dtor deletes the associated data as well | |
419 | ~wxTreeItemIndirectData() { delete m_data; } | |
420 | ||
421 | // accessors | |
422 | // get the real data associated with the item | |
423 | wxTreeItemData *GetData() const { return m_data; } | |
424 | // change it | |
425 | void SetData(wxTreeItemData *data) { m_data = data; } | |
426 | ||
427 | // do we have such image? | |
428 | bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; } | |
429 | // get image | |
430 | int GetImage(wxTreeItemIcon which) const { return m_images[which]; } | |
431 | // change it | |
432 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } | |
433 | ||
434 | private: | |
435 | // all the images associated with the item | |
436 | int m_images[wxTreeItemIcon_Max]; | |
437 | ||
438 | wxTreeItemData *m_data; | |
439 | }; | |
440 | ||
23f681ec | 441 | // ---------------------------------------------------------------------------- |
3f7bc32b | 442 | // wxWin macros |
08b7c251 VZ |
443 | // ---------------------------------------------------------------------------- |
444 | ||
23f681ec | 445 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) |
2bda0e17 | 446 | |
08b7c251 VZ |
447 | // ---------------------------------------------------------------------------- |
448 | // variables | |
449 | // ---------------------------------------------------------------------------- | |
450 | ||
451 | // handy table for sending events | |
9ab5ef13 | 452 | static wxEventType g_events[2][2] = |
2bda0e17 | 453 | { |
08b7c251 VZ |
454 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, |
455 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } | |
456 | }; | |
457 | ||
458 | // ============================================================================ | |
459 | // implementation | |
460 | // ============================================================================ | |
461 | ||
9dfbf520 VZ |
462 | // ---------------------------------------------------------------------------- |
463 | // tree traversal | |
464 | // ---------------------------------------------------------------------------- | |
465 | ||
466 | bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively) | |
467 | { | |
468 | if ( !OnVisit(root) ) | |
469 | return FALSE; | |
470 | ||
471 | return Traverse(root, recursively); | |
472 | } | |
473 | ||
474 | bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively) | |
475 | { | |
476 | long cookie; | |
477 | wxTreeItemId child = m_tree->GetFirstChild(root, cookie); | |
478 | while ( child.IsOk() ) | |
479 | { | |
480 | // depth first traversal | |
481 | if ( recursively && !Traverse(child, TRUE) ) | |
482 | return FALSE; | |
483 | ||
484 | if ( !OnVisit(child) ) | |
485 | return FALSE; | |
486 | ||
487 | child = m_tree->GetNextChild(root, cookie); | |
488 | } | |
489 | ||
490 | return TRUE; | |
491 | } | |
492 | ||
08b7c251 VZ |
493 | // ---------------------------------------------------------------------------- |
494 | // construction and destruction | |
495 | // ---------------------------------------------------------------------------- | |
496 | ||
497 | void wxTreeCtrl::Init() | |
498 | { | |
499 | m_imageListNormal = NULL; | |
500 | m_imageListState = NULL; | |
0377efa2 | 501 | m_ownsImageListNormal = m_ownsImageListState = FALSE; |
08b7c251 | 502 | m_textCtrl = NULL; |
696e1ea0 | 503 | m_hasAnyAttr = FALSE; |
23f681ec | 504 | m_dragImage = NULL; |
3f7bc32b | 505 | m_htSelStart = 0; |
9ab5ef13 JS |
506 | |
507 | // Initialize static array of events, because with the new event system, | |
508 | // they may not be initialized yet. | |
509 | ||
510 | g_events[0][0] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED; | |
511 | g_events[0][1] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING; | |
512 | g_events[1][0] = wxEVT_COMMAND_TREE_ITEM_EXPANDED; | |
513 | g_events[1][1] = wxEVT_COMMAND_TREE_ITEM_EXPANDING; | |
2bda0e17 KB |
514 | } |
515 | ||
9dfbf520 VZ |
516 | bool wxTreeCtrl::Create(wxWindow *parent, |
517 | wxWindowID id, | |
518 | const wxPoint& pos, | |
519 | const wxSize& size, | |
520 | long style, | |
521 | const wxValidator& validator, | |
08b7c251 | 522 | const wxString& name) |
2bda0e17 | 523 | { |
08b7c251 | 524 | Init(); |
2bda0e17 | 525 | |
9dfbf520 VZ |
526 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
527 | return FALSE; | |
2bda0e17 | 528 | |
5ea47806 | 529 | DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | |
b0766406 JS |
530 | TVS_SHOWSELALWAYS; |
531 | ||
532 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
533 | wstyle |= WS_CLIPSIBLINGS; | |
2bda0e17 | 534 | |
63da7df7 JS |
535 | if ((m_windowStyle & wxTR_NO_LINES) == 0) |
536 | wstyle |= TVS_HASLINES; | |
08b7c251 VZ |
537 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) |
538 | wstyle |= TVS_HASBUTTONS; | |
2bda0e17 | 539 | |
08b7c251 VZ |
540 | if ( m_windowStyle & wxTR_EDIT_LABELS ) |
541 | wstyle |= TVS_EDITLABELS; | |
2bda0e17 | 542 | |
08b7c251 VZ |
543 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) |
544 | wstyle |= TVS_LINESATROOT; | |
2bda0e17 | 545 | |
3f7bc32b VZ |
546 | // using TVS_CHECKBOXES for emulation of a multiselection tree control |
547 | // doesn't work without the new enough headers | |
548 | #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \ | |
549 | !defined( __GNUWIN32_OLD__ ) && \ | |
c42404a5 VZ |
550 | !defined( __BORLANDC__ ) && \ |
551 | !defined( __WATCOMC__ ) && \ | |
552 | (!defined(__VISUALC__) || (__VISUALC__ > 1010)) | |
a20a10fe | 553 | |
9dfbf520 VZ |
554 | // we emulate the multiple selection tree controls by using checkboxes: set |
555 | // up the image list we need for this if we do have multiple selections | |
556 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
10fcf31a | 557 | wstyle |= TVS_CHECKBOXES; |
3f7bc32b | 558 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
9dfbf520 | 559 | |
08b7c251 | 560 | // Create the tree control. |
9dfbf520 VZ |
561 | if ( !MSWCreateControl(WC_TREEVIEW, wstyle) ) |
562 | return FALSE; | |
563 | ||
f6bcfd97 BP |
564 | #if wxUSE_COMCTL32_SAFELY |
565 | wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
566 | wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); | |
567 | #elif 1 | |
5aeeab14 | 568 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 | 569 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
fbdcff4a JS |
570 | #else |
571 | // This works around a bug in the Windows tree control whereby for some versions | |
572 | // of comctrl32, setting any colour actually draws the background in black. | |
573 | // This will initialise the background to the system colour. | |
f6bcfd97 BP |
574 | // THIS FIX NOW REVERTED since it caused problems on _other_ systems. |
575 | // Assume the user has an updated comctl32.dll. | |
fbdcff4a JS |
576 | ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1); |
577 | wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
f6bcfd97 | 578 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
fbdcff4a JS |
579 | #endif |
580 | ||
5aeeab14 | 581 | |
9dfbf520 VZ |
582 | // VZ: this is some experimental code which may be used to get the |
583 | // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71. | |
584 | // AFAIK, the standard DLL does about the same thing anyhow. | |
585 | #if 0 | |
586 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
587 | { | |
588 | wxBitmap bmp; | |
589 | ||
590 | // create the DC compatible with the current screen | |
591 | HDC hdcMem = CreateCompatibleDC(NULL); | |
592 | ||
593 | // create a mono bitmap of the standard size | |
594 | int x = GetSystemMetrics(SM_CXMENUCHECK); | |
595 | int y = GetSystemMetrics(SM_CYMENUCHECK); | |
596 | wxImageList imagelistCheckboxes(x, y, FALSE, 2); | |
597 | HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size | |
598 | 1, // # of color planes | |
599 | 1, // # bits needed for one pixel | |
600 | 0); // array containing colour data | |
601 | SelectObject(hdcMem, hbmpCheck); | |
602 | ||
603 | // then draw a check mark into it | |
604 | RECT rect = { 0, 0, x, y }; | |
605 | if ( !::DrawFrameControl(hdcMem, &rect, | |
606 | DFC_BUTTON, | |
607 | DFCS_BUTTONCHECK | DFCS_CHECKED) ) | |
608 | { | |
223d09f6 | 609 | wxLogLastError(wxT("DrawFrameControl(check)")); |
9dfbf520 VZ |
610 | } |
611 | ||
612 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); | |
613 | imagelistCheckboxes.Add(bmp); | |
614 | ||
615 | if ( !::DrawFrameControl(hdcMem, &rect, | |
616 | DFC_BUTTON, | |
617 | DFCS_BUTTONCHECK) ) | |
618 | { | |
223d09f6 | 619 | wxLogLastError(wxT("DrawFrameControl(uncheck)")); |
9dfbf520 VZ |
620 | } |
621 | ||
622 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); | |
623 | imagelistCheckboxes.Add(bmp); | |
624 | ||
625 | // clean up | |
626 | ::DeleteDC(hdcMem); | |
627 | ||
628 | // set the imagelist | |
629 | SetStateImageList(&imagelistCheckboxes); | |
630 | } | |
631 | #endif // 0 | |
632 | ||
633 | SetSize(pos.x, pos.y, size.x, size.y); | |
2bda0e17 | 634 | |
08b7c251 | 635 | return TRUE; |
2bda0e17 KB |
636 | } |
637 | ||
08b7c251 | 638 | wxTreeCtrl::~wxTreeCtrl() |
2bda0e17 | 639 | { |
696e1ea0 VZ |
640 | // delete any attributes |
641 | if ( m_hasAnyAttr ) | |
642 | { | |
643 | for ( wxNode *node = m_attrs.Next(); node; node = m_attrs.Next() ) | |
644 | { | |
645 | delete (wxTreeItemAttr *)node->Data(); | |
646 | } | |
647 | ||
648 | // prevent TVN_DELETEITEM handler from deleting the attributes again! | |
649 | m_hasAnyAttr = FALSE; | |
650 | } | |
651 | ||
08b7c251 | 652 | DeleteTextCtrl(); |
2bda0e17 | 653 | |
08b7c251 VZ |
654 | // delete user data to prevent memory leaks |
655 | DeleteAllItems(); | |
33ac7e6f | 656 | |
0377efa2 VS |
657 | if (m_ownsImageListNormal) delete m_imageListNormal; |
658 | if (m_ownsImageListState) delete m_imageListState; | |
2bda0e17 KB |
659 | } |
660 | ||
08b7c251 VZ |
661 | // ---------------------------------------------------------------------------- |
662 | // accessors | |
663 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 664 | |
08b7c251 | 665 | // simple wrappers which add error checking in debug mode |
2bda0e17 | 666 | |
08b7c251 | 667 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const |
2bda0e17 | 668 | { |
d220ae32 | 669 | if ( !TreeView_GetItem(GetHwnd(), tvItem) ) |
2bda0e17 | 670 | { |
f6bcfd97 | 671 | wxLogLastError(wxT("TreeView_GetItem")); |
08b7c251 VZ |
672 | |
673 | return FALSE; | |
674 | } | |
675 | ||
676 | return TRUE; | |
2bda0e17 KB |
677 | } |
678 | ||
08b7c251 | 679 | void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem) |
2bda0e17 | 680 | { |
d220ae32 | 681 | if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 ) |
2bda0e17 | 682 | { |
f6bcfd97 | 683 | wxLogLastError(wxT("TreeView_SetItem")); |
08b7c251 | 684 | } |
2bda0e17 KB |
685 | } |
686 | ||
08b7c251 | 687 | size_t wxTreeCtrl::GetCount() const |
2bda0e17 | 688 | { |
d220ae32 | 689 | return (size_t)TreeView_GetCount(GetHwnd()); |
2bda0e17 KB |
690 | } |
691 | ||
08b7c251 | 692 | unsigned int wxTreeCtrl::GetIndent() const |
2bda0e17 | 693 | { |
d220ae32 | 694 | return TreeView_GetIndent(GetHwnd()); |
2bda0e17 KB |
695 | } |
696 | ||
08b7c251 | 697 | void wxTreeCtrl::SetIndent(unsigned int indent) |
2bda0e17 | 698 | { |
d220ae32 | 699 | TreeView_SetIndent(GetHwnd(), indent); |
2bda0e17 KB |
700 | } |
701 | ||
08b7c251 | 702 | wxImageList *wxTreeCtrl::GetImageList() const |
2bda0e17 | 703 | { |
08b7c251 | 704 | return m_imageListNormal; |
2bda0e17 KB |
705 | } |
706 | ||
08b7c251 | 707 | wxImageList *wxTreeCtrl::GetStateImageList() const |
2bda0e17 | 708 | { |
08b7c251 | 709 | return m_imageListNormal; |
2bda0e17 KB |
710 | } |
711 | ||
08b7c251 | 712 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) |
2bda0e17 | 713 | { |
08b7c251 | 714 | // no error return |
d220ae32 | 715 | TreeView_SetImageList(GetHwnd(), |
08b7c251 VZ |
716 | imageList ? imageList->GetHIMAGELIST() : 0, |
717 | which); | |
2bda0e17 KB |
718 | } |
719 | ||
08b7c251 | 720 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
2bda0e17 | 721 | { |
0377efa2 | 722 | if (m_ownsImageListNormal) delete m_imageListNormal; |
08b7c251 | 723 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); |
0377efa2 | 724 | m_ownsImageListNormal = FALSE; |
2bda0e17 KB |
725 | } |
726 | ||
08b7c251 | 727 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
2bda0e17 | 728 | { |
0377efa2 | 729 | if (m_ownsImageListState) delete m_imageListState; |
08b7c251 | 730 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); |
0377efa2 VS |
731 | m_ownsImageListState = FALSE; |
732 | } | |
733 | ||
734 | void wxTreeCtrl::AssignImageList(wxImageList *imageList) | |
735 | { | |
736 | SetImageList(imageList); | |
737 | m_ownsImageListNormal = TRUE; | |
738 | } | |
739 | ||
740 | void wxTreeCtrl::AssignStateImageList(wxImageList *imageList) | |
741 | { | |
742 | SetStateImageList(imageList); | |
743 | m_ownsImageListState = TRUE; | |
2bda0e17 KB |
744 | } |
745 | ||
33961d59 RR |
746 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, |
747 | bool recursively) const | |
748 | { | |
749 | TraverseCounter counter(this, item, recursively); | |
23fd5130 | 750 | |
73974df1 | 751 | return counter.GetCount() - 1; |
23fd5130 VZ |
752 | } |
753 | ||
bb448552 VZ |
754 | // ---------------------------------------------------------------------------- |
755 | // control colours | |
756 | // ---------------------------------------------------------------------------- | |
757 | ||
758 | bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour) | |
759 | { | |
f6bcfd97 | 760 | #if !wxUSE_COMCTL32_SAFELY |
bb448552 VZ |
761 | if ( !wxWindowBase::SetBackgroundColour(colour) ) |
762 | return FALSE; | |
763 | ||
764 | SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel()); | |
f6bcfd97 | 765 | #endif |
bb448552 VZ |
766 | |
767 | return TRUE; | |
768 | } | |
769 | ||
770 | bool wxTreeCtrl::SetForegroundColour(const wxColour &colour) | |
771 | { | |
f6bcfd97 | 772 | #if !wxUSE_COMCTL32_SAFELY |
bb448552 VZ |
773 | if ( !wxWindowBase::SetForegroundColour(colour) ) |
774 | return FALSE; | |
775 | ||
776 | SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel()); | |
f6bcfd97 | 777 | #endif |
bb448552 VZ |
778 | |
779 | return TRUE; | |
780 | } | |
781 | ||
08b7c251 VZ |
782 | // ---------------------------------------------------------------------------- |
783 | // Item access | |
784 | // ---------------------------------------------------------------------------- | |
785 | ||
786 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const | |
2bda0e17 | 787 | { |
837e5743 | 788 | wxChar buf[512]; // the size is arbitrary... |
02ce7b72 | 789 | |
08b7c251 VZ |
790 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
791 | tvItem.pszText = buf; | |
792 | tvItem.cchTextMax = WXSIZEOF(buf); | |
793 | if ( !DoGetItem(&tvItem) ) | |
794 | { | |
795 | // don't return some garbage which was on stack, but an empty string | |
223d09f6 | 796 | buf[0] = wxT('\0'); |
08b7c251 | 797 | } |
2bda0e17 | 798 | |
08b7c251 VZ |
799 | return wxString(buf); |
800 | } | |
2bda0e17 | 801 | |
08b7c251 VZ |
802 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
803 | { | |
804 | wxTreeViewItem tvItem(item, TVIF_TEXT); | |
837e5743 | 805 | tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok |
08b7c251 VZ |
806 | DoSetItem(&tvItem); |
807 | } | |
2bda0e17 | 808 | |
74b31181 VZ |
809 | int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item, |
810 | wxTreeItemIcon which) const | |
811 | { | |
812 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
813 | if ( !DoGetItem(&tvItem) ) | |
814 | { | |
815 | return -1; | |
816 | } | |
817 | ||
818 | return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which); | |
819 | } | |
820 | ||
821 | void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item, | |
822 | int image, | |
823 | wxTreeItemIcon which) const | |
824 | { | |
825 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
826 | if ( !DoGetItem(&tvItem) ) | |
827 | { | |
828 | return; | |
829 | } | |
830 | ||
831 | wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam); | |
832 | ||
833 | data->SetImage(image, which); | |
834 | ||
835 | // make sure that we have selected images as well | |
836 | if ( which == wxTreeItemIcon_Normal && | |
837 | !data->HasImage(wxTreeItemIcon_Selected) ) | |
838 | { | |
839 | data->SetImage(image, wxTreeItemIcon_Selected); | |
840 | } | |
841 | ||
842 | if ( which == wxTreeItemIcon_Expanded && | |
843 | !data->HasImage(wxTreeItemIcon_SelectedExpanded) ) | |
844 | { | |
845 | data->SetImage(image, wxTreeItemIcon_SelectedExpanded); | |
846 | } | |
847 | } | |
848 | ||
9dfbf520 VZ |
849 | void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item, |
850 | int image, | |
851 | int imageSel) | |
852 | { | |
853 | wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE); | |
854 | tvItem.iSelectedImage = imageSel; | |
855 | tvItem.iImage = image; | |
856 | DoSetItem(&tvItem); | |
857 | } | |
858 | ||
74b31181 VZ |
859 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item, |
860 | wxTreeItemIcon which) const | |
08b7c251 | 861 | { |
74b31181 VZ |
862 | if ( HasIndirectData(item) ) |
863 | { | |
864 | return DoGetItemImageFromData(item, which); | |
865 | } | |
2bda0e17 | 866 | |
74b31181 VZ |
867 | UINT mask; |
868 | switch ( which ) | |
869 | { | |
870 | default: | |
223d09f6 | 871 | wxFAIL_MSG( wxT("unknown tree item image type") ); |
2bda0e17 | 872 | |
74b31181 VZ |
873 | case wxTreeItemIcon_Normal: |
874 | mask = TVIF_IMAGE; | |
875 | break; | |
2bda0e17 | 876 | |
74b31181 VZ |
877 | case wxTreeItemIcon_Selected: |
878 | mask = TVIF_SELECTEDIMAGE; | |
879 | break; | |
880 | ||
881 | case wxTreeItemIcon_Expanded: | |
882 | case wxTreeItemIcon_SelectedExpanded: | |
883 | return -1; | |
884 | } | |
885 | ||
886 | wxTreeViewItem tvItem(item, mask); | |
08b7c251 | 887 | DoGetItem(&tvItem); |
2bda0e17 | 888 | |
74b31181 | 889 | return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage; |
2bda0e17 KB |
890 | } |
891 | ||
74b31181 VZ |
892 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image, |
893 | wxTreeItemIcon which) | |
2bda0e17 | 894 | { |
74b31181 VZ |
895 | int imageNormal, imageSel; |
896 | switch ( which ) | |
897 | { | |
898 | default: | |
223d09f6 | 899 | wxFAIL_MSG( wxT("unknown tree item image type") ); |
74b31181 VZ |
900 | |
901 | case wxTreeItemIcon_Normal: | |
902 | imageNormal = image; | |
903 | imageSel = GetItemSelectedImage(item); | |
904 | break; | |
905 | ||
906 | case wxTreeItemIcon_Selected: | |
907 | imageNormal = GetItemImage(item); | |
908 | imageSel = image; | |
909 | break; | |
910 | ||
911 | case wxTreeItemIcon_Expanded: | |
912 | case wxTreeItemIcon_SelectedExpanded: | |
913 | if ( !HasIndirectData(item) ) | |
914 | { | |
915 | // we need to get the old images first, because after we create | |
916 | // the wxTreeItemIndirectData GetItemXXXImage() will use it to | |
917 | // get the images | |
918 | imageNormal = GetItemImage(item); | |
919 | imageSel = GetItemSelectedImage(item); | |
920 | ||
921 | // if it doesn't have it yet, add it | |
922 | wxTreeItemIndirectData *data = new | |
923 | wxTreeItemIndirectData(this, item); | |
924 | ||
925 | // copy the data to the new location | |
926 | data->SetImage(imageNormal, wxTreeItemIcon_Normal); | |
927 | data->SetImage(imageSel, wxTreeItemIcon_Selected); | |
928 | } | |
929 | ||
930 | DoSetItemImageFromData(item, image, which); | |
931 | ||
932 | // reset the normal/selected images because we won't use them any | |
933 | // more - now they're stored inside the indirect data | |
934 | imageNormal = | |
935 | imageSel = I_IMAGECALLBACK; | |
936 | break; | |
937 | } | |
938 | ||
9dfbf520 VZ |
939 | // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always |
940 | // change both normal and selected image - otherwise the change simply | |
941 | // doesn't take place! | |
74b31181 | 942 | DoSetItemImages(item, imageNormal, imageSel); |
2bda0e17 KB |
943 | } |
944 | ||
08b7c251 | 945 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
2bda0e17 | 946 | { |
08b7c251 VZ |
947 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
948 | if ( !DoGetItem(&tvItem) ) | |
949 | { | |
950 | return NULL; | |
951 | } | |
2bda0e17 | 952 | |
74b31181 VZ |
953 | if ( HasIndirectData(item) ) |
954 | { | |
955 | return ((wxTreeItemIndirectData *)tvItem.lParam)->GetData(); | |
956 | } | |
957 | else | |
958 | { | |
959 | return (wxTreeItemData *)tvItem.lParam; | |
960 | } | |
2bda0e17 KB |
961 | } |
962 | ||
08b7c251 | 963 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
2bda0e17 | 964 | { |
188781db VZ |
965 | // first, associate this piece of data with this item |
966 | if ( data ) | |
967 | { | |
968 | data->SetId(item); | |
969 | } | |
970 | ||
08b7c251 | 971 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
74b31181 VZ |
972 | |
973 | if ( HasIndirectData(item) ) | |
974 | { | |
975 | if ( DoGetItem(&tvItem) ) | |
976 | { | |
977 | ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data); | |
978 | } | |
979 | else | |
980 | { | |
223d09f6 | 981 | wxFAIL_MSG( wxT("failed to change tree items data") ); |
74b31181 VZ |
982 | } |
983 | } | |
984 | else | |
985 | { | |
986 | tvItem.lParam = (LPARAM)data; | |
987 | DoSetItem(&tvItem); | |
988 | } | |
989 | } | |
990 | ||
991 | void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item, | |
992 | wxTreeItemIndirectData *data) | |
993 | { | |
994 | // this should never happen because it's unnecessary and will probably lead | |
995 | // to crash too because the code elsewhere supposes that the pointer the | |
996 | // wxTreeItemIndirectData has is a real wxItemData and not | |
997 | // wxTreeItemIndirectData as well | |
223d09f6 | 998 | wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") ); |
74b31181 VZ |
999 | |
1000 | SetItemData(item, (wxTreeItemData *)data); | |
1001 | ||
1002 | m_itemsWithIndirectData.Add(item); | |
1003 | } | |
1004 | ||
1005 | bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const | |
1006 | { | |
1007 | return m_itemsWithIndirectData.Index(item) != wxNOT_FOUND; | |
08b7c251 | 1008 | } |
2bda0e17 | 1009 | |
3a5a2f56 VZ |
1010 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
1011 | { | |
1012 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); | |
1013 | tvItem.cChildren = (int)has; | |
1014 | DoSetItem(&tvItem); | |
1015 | } | |
1016 | ||
add28c55 VZ |
1017 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
1018 | { | |
1019 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
1020 | tvItem.state = bold ? TVIS_BOLD : 0; | |
1021 | DoSetItem(&tvItem); | |
1022 | } | |
1023 | ||
58a8ab88 JS |
1024 | void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight) |
1025 | { | |
1026 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED); | |
1027 | tvItem.state = highlight ? TVIS_DROPHILITED : 0; | |
1028 | DoSetItem(&tvItem); | |
1029 | } | |
1030 | ||
696e1ea0 VZ |
1031 | void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item, |
1032 | const wxColour& col) | |
1033 | { | |
1034 | m_hasAnyAttr = TRUE; | |
1035 | ||
1036 | long id = (long)(WXHTREEITEM)item; | |
1037 | wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id); | |
1038 | if ( !attr ) | |
1039 | { | |
1040 | attr = new wxTreeItemAttr; | |
1041 | m_attrs.Put(id, (wxObject *)attr); | |
1042 | } | |
1043 | ||
1044 | attr->SetTextColour(col); | |
1045 | } | |
1046 | ||
1047 | void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, | |
1048 | const wxColour& col) | |
1049 | { | |
1050 | m_hasAnyAttr = TRUE; | |
1051 | ||
1052 | long id = (long)(WXHTREEITEM)item; | |
1053 | wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id); | |
1054 | if ( !attr ) | |
1055 | { | |
1056 | attr = new wxTreeItemAttr; | |
1057 | m_attrs.Put(id, (wxObject *)attr); | |
1058 | } | |
1059 | ||
1060 | attr->SetBackgroundColour(col); | |
1061 | } | |
1062 | ||
1063 | void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) | |
1064 | { | |
1065 | m_hasAnyAttr = TRUE; | |
1066 | ||
1067 | long id = (long)(WXHTREEITEM)item; | |
1068 | wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id); | |
1069 | if ( !attr ) | |
1070 | { | |
1071 | attr = new wxTreeItemAttr; | |
1072 | m_attrs.Put(id, (wxObject *)attr); | |
1073 | } | |
1074 | ||
1075 | attr->SetFont(font); | |
1076 | } | |
1077 | ||
08b7c251 VZ |
1078 | // ---------------------------------------------------------------------------- |
1079 | // Item status | |
1080 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1081 | |
08b7c251 VZ |
1082 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const |
1083 | { | |
add28c55 | 1084 | // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect |
08b7c251 | 1085 | RECT rect; |
955be36c VZ |
1086 | |
1087 | // this ugliness comes directly from MSDN - it *is* the correct way to pass | |
1088 | // the HTREEITEM with TVM_GETITEMRECT | |
1089 | *(WXHTREEITEM *)&rect = (WXHTREEITEM)item; | |
1090 | ||
1091 | // FALSE means get item rect for the whole item, not only text | |
1c74a900 | 1092 | return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0; |
06e38c8e | 1093 | |
2bda0e17 KB |
1094 | } |
1095 | ||
08b7c251 | 1096 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
2bda0e17 | 1097 | { |
08b7c251 VZ |
1098 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
1099 | DoGetItem(&tvItem); | |
2bda0e17 | 1100 | |
08b7c251 | 1101 | return tvItem.cChildren != 0; |
2bda0e17 KB |
1102 | } |
1103 | ||
08b7c251 | 1104 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
2bda0e17 | 1105 | { |
08b7c251 VZ |
1106 | // probably not a good idea to put it here |
1107 | //wxASSERT( ItemHasChildren(item) ); | |
2bda0e17 | 1108 | |
08b7c251 VZ |
1109 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); |
1110 | DoGetItem(&tvItem); | |
2bda0e17 | 1111 | |
08b7c251 | 1112 | return (tvItem.state & TVIS_EXPANDED) != 0; |
2bda0e17 KB |
1113 | } |
1114 | ||
08b7c251 | 1115 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
2bda0e17 | 1116 | { |
08b7c251 VZ |
1117 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); |
1118 | DoGetItem(&tvItem); | |
2bda0e17 | 1119 | |
08b7c251 | 1120 | return (tvItem.state & TVIS_SELECTED) != 0; |
2bda0e17 KB |
1121 | } |
1122 | ||
add28c55 VZ |
1123 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const |
1124 | { | |
1125 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
1126 | DoGetItem(&tvItem); | |
1127 | ||
1128 | return (tvItem.state & TVIS_BOLD) != 0; | |
1129 | } | |
1130 | ||
08b7c251 VZ |
1131 | // ---------------------------------------------------------------------------- |
1132 | // navigation | |
1133 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1134 | |
08b7c251 VZ |
1135 | wxTreeItemId wxTreeCtrl::GetRootItem() const |
1136 | { | |
d220ae32 | 1137 | return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd())); |
08b7c251 | 1138 | } |
2bda0e17 | 1139 | |
08b7c251 VZ |
1140 | wxTreeItemId wxTreeCtrl::GetSelection() const |
1141 | { | |
ad90972f | 1142 | wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (long)(WXHTREEITEM)0, |
223d09f6 | 1143 | wxT("this only works with single selection controls") ); |
9dfbf520 | 1144 | |
d220ae32 | 1145 | return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd())); |
2bda0e17 KB |
1146 | } |
1147 | ||
08b7c251 | 1148 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const |
2bda0e17 | 1149 | { |
3f7bc32b | 1150 | return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), HITEM(item))); |
08b7c251 | 1151 | } |
2bda0e17 | 1152 | |
08b7c251 | 1153 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
06e38c8e | 1154 | long& _cookie) const |
08b7c251 VZ |
1155 | { |
1156 | // remember the last child returned in 'cookie' | |
3f7bc32b | 1157 | _cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item)); |
2bda0e17 | 1158 | |
06e38c8e | 1159 | return wxTreeItemId((WXHTREEITEM)_cookie); |
2bda0e17 KB |
1160 | } |
1161 | ||
08b7c251 | 1162 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), |
06e38c8e | 1163 | long& _cookie) const |
2bda0e17 | 1164 | { |
d220ae32 | 1165 | wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(), |
3f7bc32b | 1166 | HITEM(_cookie))); |
23fd5130 VZ |
1167 | _cookie = (long)l; |
1168 | ||
2e5dddb0 | 1169 | return l; |
08b7c251 | 1170 | } |
2bda0e17 | 1171 | |
978f38c2 VZ |
1172 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
1173 | { | |
1174 | // can this be done more efficiently? | |
1175 | long cookie; | |
1176 | ||
1177 | wxTreeItemId childLast, | |
2165ad93 | 1178 | child = GetFirstChild(item, cookie); |
978f38c2 VZ |
1179 | while ( child.IsOk() ) |
1180 | { | |
1181 | childLast = child; | |
2165ad93 | 1182 | child = GetNextChild(item, cookie); |
978f38c2 VZ |
1183 | } |
1184 | ||
1185 | return childLast; | |
1186 | } | |
1187 | ||
08b7c251 VZ |
1188 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
1189 | { | |
3f7bc32b | 1190 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), HITEM(item))); |
2bda0e17 KB |
1191 | } |
1192 | ||
08b7c251 | 1193 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
2bda0e17 | 1194 | { |
3f7bc32b | 1195 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), HITEM(item))); |
2bda0e17 KB |
1196 | } |
1197 | ||
08b7c251 | 1198 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
2bda0e17 | 1199 | { |
d220ae32 | 1200 | return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd())); |
2bda0e17 KB |
1201 | } |
1202 | ||
08b7c251 | 1203 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
2bda0e17 | 1204 | { |
f6bcfd97 | 1205 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!")); |
02ce7b72 | 1206 | |
3f7bc32b | 1207 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), HITEM(item))); |
08b7c251 | 1208 | } |
02ce7b72 | 1209 | |
08b7c251 VZ |
1210 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
1211 | { | |
f6bcfd97 | 1212 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!")); |
02ce7b72 | 1213 | |
3f7bc32b | 1214 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), HITEM(item))); |
08b7c251 | 1215 | } |
02ce7b72 | 1216 | |
9dfbf520 VZ |
1217 | // ---------------------------------------------------------------------------- |
1218 | // multiple selections emulation | |
1219 | // ---------------------------------------------------------------------------- | |
1220 | ||
1221 | bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const | |
1222 | { | |
1223 | // receive the desired information. | |
1224 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
1225 | DoGetItem(&tvItem); | |
1226 | ||
1227 | // state image indices are 1 based | |
1228 | return ((tvItem.state >> 12) - 1) == 1; | |
1229 | } | |
1230 | ||
1231 | void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check) | |
1232 | { | |
1233 | // receive the desired information. | |
1234 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
1235 | ||
1236 | // state images are one-based | |
1237 | tvItem.state = (check ? 2 : 1) << 12; | |
1238 | ||
1239 | DoSetItem(&tvItem); | |
1240 | } | |
1241 | ||
33961d59 RR |
1242 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const |
1243 | { | |
1244 | TraverseSelections selector(this, selections); | |
9dfbf520 | 1245 | |
e47c4d48 | 1246 | return selector.GetCount(); |
9dfbf520 VZ |
1247 | } |
1248 | ||
08b7c251 VZ |
1249 | // ---------------------------------------------------------------------------- |
1250 | // Usual operations | |
1251 | // ---------------------------------------------------------------------------- | |
02ce7b72 | 1252 | |
08b7c251 VZ |
1253 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, |
1254 | wxTreeItemId hInsertAfter, | |
1255 | const wxString& text, | |
1256 | int image, int selectedImage, | |
1257 | wxTreeItemData *data) | |
1258 | { | |
0e935169 VZ |
1259 | wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()), |
1260 | wxTreeItemId(), | |
1261 | _T("can't have more than one root in the tree") ); | |
1262 | ||
08b7c251 | 1263 | TV_INSERTSTRUCT tvIns; |
3f7bc32b VZ |
1264 | tvIns.hParent = HITEM(parent); |
1265 | tvIns.hInsertAfter = HITEM(hInsertAfter); | |
58a8ab88 | 1266 | |
74b31181 VZ |
1267 | // this is how we insert the item as the first child: supply a NULL |
1268 | // hInsertAfter | |
1269 | if ( !tvIns.hInsertAfter ) | |
58a8ab88 JS |
1270 | { |
1271 | tvIns.hInsertAfter = TVI_FIRST; | |
1272 | } | |
1273 | ||
08b7c251 VZ |
1274 | UINT mask = 0; |
1275 | if ( !text.IsEmpty() ) | |
1276 | { | |
1277 | mask |= TVIF_TEXT; | |
837e5743 | 1278 | tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok |
08b7c251 | 1279 | } |
f6bcfd97 BP |
1280 | else |
1281 | { | |
1282 | tvIns.item.pszText = NULL; | |
1283 | tvIns.item.cchTextMax = 0; | |
1284 | } | |
02ce7b72 | 1285 | |
08b7c251 VZ |
1286 | if ( image != -1 ) |
1287 | { | |
1288 | mask |= TVIF_IMAGE; | |
1289 | tvIns.item.iImage = image; | |
3a5a2f56 | 1290 | |
6b037754 | 1291 | if ( selectedImage == -1 ) |
3a5a2f56 VZ |
1292 | { |
1293 | // take the same image for selected icon if not specified | |
1294 | selectedImage = image; | |
1295 | } | |
08b7c251 | 1296 | } |
02ce7b72 | 1297 | |
08b7c251 VZ |
1298 | if ( selectedImage != -1 ) |
1299 | { | |
1300 | mask |= TVIF_SELECTEDIMAGE; | |
1301 | tvIns.item.iSelectedImage = selectedImage; | |
1302 | } | |
02ce7b72 | 1303 | |
08b7c251 VZ |
1304 | if ( data != NULL ) |
1305 | { | |
1306 | mask |= TVIF_PARAM; | |
1307 | tvIns.item.lParam = (LPARAM)data; | |
1308 | } | |
02ce7b72 | 1309 | |
08b7c251 | 1310 | tvIns.item.mask = mask; |
02ce7b72 | 1311 | |
d220ae32 | 1312 | HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns); |
08b7c251 VZ |
1313 | if ( id == 0 ) |
1314 | { | |
f6bcfd97 | 1315 | wxLogLastError(wxT("TreeView_InsertItem")); |
08b7c251 | 1316 | } |
02ce7b72 | 1317 | |
fd3f686c VZ |
1318 | if ( data != NULL ) |
1319 | { | |
1320 | // associate the application tree item with Win32 tree item handle | |
1321 | data->SetId((WXHTREEITEM)id); | |
1322 | } | |
1323 | ||
06e38c8e | 1324 | return wxTreeItemId((WXHTREEITEM)id); |
2bda0e17 KB |
1325 | } |
1326 | ||
08b7c251 VZ |
1327 | // for compatibility only |
1328 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, | |
1329 | const wxString& text, | |
1330 | int image, int selImage, | |
1331 | long insertAfter) | |
2bda0e17 | 1332 | { |
06e38c8e | 1333 | return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text, |
08b7c251 | 1334 | image, selImage, NULL); |
2bda0e17 KB |
1335 | } |
1336 | ||
08b7c251 VZ |
1337 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
1338 | int image, int selectedImage, | |
1339 | wxTreeItemData *data) | |
2bda0e17 | 1340 | { |
ad90972f | 1341 | return DoInsertItem(wxTreeItemId((long) (WXHTREEITEM) 0), (long)(WXHTREEITEM) 0, |
08b7c251 | 1342 | text, image, selectedImage, data); |
2bda0e17 KB |
1343 | } |
1344 | ||
08b7c251 VZ |
1345 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, |
1346 | const wxString& text, | |
1347 | int image, int selectedImage, | |
1348 | wxTreeItemData *data) | |
2bda0e17 | 1349 | { |
06e38c8e | 1350 | return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST, |
08b7c251 | 1351 | text, image, selectedImage, data); |
2bda0e17 KB |
1352 | } |
1353 | ||
08b7c251 VZ |
1354 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
1355 | const wxTreeItemId& idPrevious, | |
1356 | const wxString& text, | |
1357 | int image, int selectedImage, | |
1358 | wxTreeItemData *data) | |
2bda0e17 | 1359 | { |
08b7c251 | 1360 | return DoInsertItem(parent, idPrevious, text, image, selectedImage, data); |
2bda0e17 KB |
1361 | } |
1362 | ||
2ef31e80 VZ |
1363 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
1364 | size_t index, | |
1365 | const wxString& text, | |
1366 | int image, int selectedImage, | |
1367 | wxTreeItemData *data) | |
1368 | { | |
1369 | // find the item from index | |
1370 | long cookie; | |
1371 | wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie); | |
1372 | while ( index != 0 && idCur.IsOk() ) | |
1373 | { | |
1374 | index--; | |
1375 | ||
1376 | idPrev = idCur; | |
1377 | idCur = GetNextChild(parent, cookie); | |
1378 | } | |
1379 | ||
1380 | // assert, not check: if the index is invalid, we will append the item | |
1381 | // to the end | |
1382 | wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") ); | |
1383 | ||
1384 | return DoInsertItem(parent, idPrev, text, image, selectedImage, data); | |
1385 | } | |
1386 | ||
08b7c251 VZ |
1387 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent, |
1388 | const wxString& text, | |
1389 | int image, int selectedImage, | |
1390 | wxTreeItemData *data) | |
2bda0e17 | 1391 | { |
06e38c8e | 1392 | return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST, |
08b7c251 | 1393 | text, image, selectedImage, data); |
2bda0e17 KB |
1394 | } |
1395 | ||
08b7c251 | 1396 | void wxTreeCtrl::Delete(const wxTreeItemId& item) |
2bda0e17 | 1397 | { |
3f7bc32b | 1398 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) ) |
bbcdf8bc | 1399 | { |
f6bcfd97 | 1400 | wxLogLastError(wxT("TreeView_DeleteItem")); |
bbcdf8bc | 1401 | } |
bbcdf8bc JS |
1402 | } |
1403 | ||
23fd5130 VZ |
1404 | // delete all children (but don't delete the item itself) |
1405 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item) | |
1406 | { | |
1407 | long cookie; | |
1408 | ||
1409 | wxArrayLong children; | |
1410 | wxTreeItemId child = GetFirstChild(item, cookie); | |
1411 | while ( child.IsOk() ) | |
1412 | { | |
1413 | children.Add((long)(WXHTREEITEM)child); | |
1414 | ||
1415 | child = GetNextChild(item, cookie); | |
1416 | } | |
1417 | ||
1418 | size_t nCount = children.Count(); | |
1419 | for ( size_t n = 0; n < nCount; n++ ) | |
1420 | { | |
d220ae32 | 1421 | if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) ) |
23fd5130 | 1422 | { |
f6bcfd97 | 1423 | wxLogLastError(wxT("TreeView_DeleteItem")); |
23fd5130 VZ |
1424 | } |
1425 | } | |
1426 | } | |
1427 | ||
08b7c251 | 1428 | void wxTreeCtrl::DeleteAllItems() |
bbcdf8bc | 1429 | { |
d220ae32 | 1430 | if ( !TreeView_DeleteAllItems(GetHwnd()) ) |
bbcdf8bc | 1431 | { |
f6bcfd97 | 1432 | wxLogLastError(wxT("TreeView_DeleteAllItems")); |
bbcdf8bc | 1433 | } |
2bda0e17 KB |
1434 | } |
1435 | ||
08b7c251 | 1436 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) |
2bda0e17 | 1437 | { |
dd3646fd VZ |
1438 | wxASSERT_MSG( flag == TVE_COLLAPSE || |
1439 | flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) || | |
1440 | flag == TVE_EXPAND || | |
1441 | flag == TVE_TOGGLE, | |
223d09f6 | 1442 | wxT("Unknown flag in wxTreeCtrl::DoExpand") ); |
08b7c251 VZ |
1443 | |
1444 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must | |
d220ae32 VZ |
1445 | // emulate them. This behaviour has changed slightly with comctl32.dll |
1446 | // v 4.70 - now it does send them but only the first time. To maintain | |
1447 | // compatible behaviour and also in order to not have surprises with the | |
1448 | // future versions, don't rely on this and still do everything ourselves. | |
1449 | // To avoid that the messages be sent twice when the item is expanded for | |
1450 | // the first time we must clear TVIS_EXPANDEDONCE style manually. | |
1451 | ||
1452 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE); | |
1453 | tvItem.state = 0; | |
1454 | DoSetItem(&tvItem); | |
1455 | ||
3f7bc32b | 1456 | if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 ) |
08b7c251 VZ |
1457 | { |
1458 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1459 | event.m_item = item; | |
1460 | ||
1461 | bool isExpanded = IsExpanded(item); | |
2bda0e17 | 1462 | |
08b7c251 | 1463 | event.SetEventObject(this); |
2bda0e17 | 1464 | |
d220ae32 | 1465 | // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded |
08b7c251 VZ |
1466 | event.SetEventType(g_events[isExpanded][TRUE]); |
1467 | GetEventHandler()->ProcessEvent(event); | |
2bda0e17 | 1468 | |
08b7c251 VZ |
1469 | event.SetEventType(g_events[isExpanded][FALSE]); |
1470 | GetEventHandler()->ProcessEvent(event); | |
1471 | } | |
d220ae32 | 1472 | //else: change didn't took place, so do nothing at all |
2bda0e17 KB |
1473 | } |
1474 | ||
08b7c251 | 1475 | void wxTreeCtrl::Expand(const wxTreeItemId& item) |
2bda0e17 | 1476 | { |
08b7c251 | 1477 | DoExpand(item, TVE_EXPAND); |
2bda0e17 | 1478 | } |
2bda0e17 | 1479 | |
08b7c251 | 1480 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) |
2bda0e17 | 1481 | { |
08b7c251 | 1482 | DoExpand(item, TVE_COLLAPSE); |
2bda0e17 KB |
1483 | } |
1484 | ||
08b7c251 | 1485 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
2bda0e17 | 1486 | { |
dd3646fd | 1487 | DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET); |
2bda0e17 KB |
1488 | } |
1489 | ||
08b7c251 | 1490 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) |
2bda0e17 | 1491 | { |
08b7c251 | 1492 | DoExpand(item, TVE_TOGGLE); |
2bda0e17 KB |
1493 | } |
1494 | ||
42c5812d UU |
1495 | void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action) |
1496 | { | |
9dfbf520 | 1497 | DoExpand(item, action); |
42c5812d UU |
1498 | } |
1499 | ||
08b7c251 | 1500 | void wxTreeCtrl::Unselect() |
2bda0e17 | 1501 | { |
3f7bc32b VZ |
1502 | wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), |
1503 | wxT("doesn't make sense, may be you want UnselectAll()?") ); | |
9dfbf520 VZ |
1504 | |
1505 | // just remove the selection | |
ad90972f | 1506 | SelectItem(wxTreeItemId((long) (WXHTREEITEM) 0)); |
08b7c251 | 1507 | } |
02ce7b72 | 1508 | |
9dfbf520 | 1509 | void wxTreeCtrl::UnselectAll() |
08b7c251 | 1510 | { |
9dfbf520 | 1511 | if ( m_windowStyle & wxTR_MULTIPLE ) |
2bda0e17 | 1512 | { |
9dfbf520 VZ |
1513 | wxArrayTreeItemIds selections; |
1514 | size_t count = GetSelections(selections); | |
1515 | for ( size_t n = 0; n < count; n++ ) | |
d220ae32 | 1516 | { |
3f7bc32b | 1517 | #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
9dfbf520 | 1518 | SetItemCheck(selections[n], FALSE); |
3f7bc32b VZ |
1519 | #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
1520 | ::UnselectItem(GetHwnd(), HITEM(selections[n])); | |
1521 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
d220ae32 | 1522 | } |
9dfbf520 VZ |
1523 | } |
1524 | else | |
1525 | { | |
1526 | // just remove the selection | |
1527 | Unselect(); | |
1528 | } | |
1529 | } | |
1530 | ||
1531 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item) | |
1532 | { | |
1533 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
1534 | { | |
3f7bc32b | 1535 | #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
9dfbf520 VZ |
1536 | // selecting the item means checking it |
1537 | SetItemCheck(item); | |
3f7bc32b VZ |
1538 | #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
1539 | ::SelectItem(GetHwnd(), HITEM(item)); | |
1540 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
9dfbf520 VZ |
1541 | } |
1542 | else | |
1543 | { | |
1544 | // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive | |
1545 | // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so | |
1546 | // send them ourselves | |
1547 | ||
1548 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1549 | event.m_item = item; | |
1550 | event.SetEventObject(this); | |
1551 | ||
1552 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING); | |
1553 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
d220ae32 | 1554 | { |
3f7bc32b | 1555 | if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) ) |
9dfbf520 | 1556 | { |
f6bcfd97 | 1557 | wxLogLastError(wxT("TreeView_SelectItem")); |
9dfbf520 VZ |
1558 | } |
1559 | else | |
1560 | { | |
1561 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); | |
1562 | (void)GetEventHandler()->ProcessEvent(event); | |
1563 | } | |
d220ae32 | 1564 | } |
9dfbf520 | 1565 | //else: program vetoed the change |
2bda0e17 | 1566 | } |
08b7c251 | 1567 | } |
2bda0e17 | 1568 | |
08b7c251 VZ |
1569 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
1570 | { | |
1571 | // no error return | |
3f7bc32b | 1572 | TreeView_EnsureVisible(GetHwnd(), HITEM(item)); |
08b7c251 VZ |
1573 | } |
1574 | ||
1575 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) | |
1576 | { | |
3f7bc32b | 1577 | if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) ) |
2bda0e17 | 1578 | { |
f6bcfd97 | 1579 | wxLogLastError(wxT("TreeView_SelectSetFirstVisible")); |
2bda0e17 | 1580 | } |
08b7c251 VZ |
1581 | } |
1582 | ||
1583 | wxTextCtrl* wxTreeCtrl::GetEditControl() const | |
1584 | { | |
b94ae1ea VZ |
1585 | // normally, we could try to do something like this to return something |
1586 | // even when the editing was started by the user and not by calling | |
1587 | // EditLabel() - but as nobody has asked for this so far and there might be | |
1588 | // problems in the code below, I leave it disabled for now (VZ) | |
1589 | #if 0 | |
1590 | if ( !m_textCtrl ) | |
1591 | { | |
1592 | HWND hwndText = TreeView_GetEditControl(GetHwnd()); | |
1593 | if ( hwndText ) | |
1594 | { | |
1595 | m_textCtrl = new wxTextCtrl(this, -1); | |
1596 | m_textCtrl->Hide(); | |
1597 | m_textCtrl->SetHWND((WXHWND)hwndText); | |
1598 | } | |
1599 | //else: not editing label right now | |
1600 | } | |
1601 | #endif // 0 | |
1602 | ||
08b7c251 VZ |
1603 | return m_textCtrl; |
1604 | } | |
1605 | ||
1606 | void wxTreeCtrl::DeleteTextCtrl() | |
1607 | { | |
1608 | if ( m_textCtrl ) | |
2bda0e17 | 1609 | { |
b6a3d6ad VZ |
1610 | // the HWND corresponding to this control is deleted by the tree |
1611 | // control itself and we don't know when exactly this happens, so check | |
1612 | // if the window still exists before calling UnsubclassWin() | |
1613 | if ( !::IsWindow(GetHwndOf(m_textCtrl)) ) | |
1614 | { | |
1615 | m_textCtrl->SetHWND(0); | |
1616 | } | |
1617 | ||
08b7c251 VZ |
1618 | m_textCtrl->UnsubclassWin(); |
1619 | m_textCtrl->SetHWND(0); | |
1620 | delete m_textCtrl; | |
1621 | m_textCtrl = NULL; | |
2bda0e17 | 1622 | } |
08b7c251 | 1623 | } |
2bda0e17 | 1624 | |
08b7c251 VZ |
1625 | wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, |
1626 | wxClassInfo* textControlClass) | |
1627 | { | |
1628 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
1629 | ||
b94ae1ea VZ |
1630 | DeleteTextCtrl(); |
1631 | ||
3f7bc32b | 1632 | HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item)); |
2bda0e17 | 1633 | |
5ea47806 VZ |
1634 | // this is not an error - the TVN_BEGINLABELEDIT handler might have |
1635 | // returned FALSE | |
1636 | if ( !hWnd ) | |
1637 | { | |
1638 | return NULL; | |
1639 | } | |
2bda0e17 | 1640 | |
08b7c251 VZ |
1641 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); |
1642 | m_textCtrl->SetHWND((WXHWND)hWnd); | |
1643 | m_textCtrl->SubclassWin((WXHWND)hWnd); | |
2bda0e17 | 1644 | |
08b7c251 | 1645 | return m_textCtrl; |
2bda0e17 KB |
1646 | } |
1647 | ||
08b7c251 | 1648 | // End label editing, optionally cancelling the edit |
33ac7e6f | 1649 | void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges) |
2bda0e17 | 1650 | { |
d220ae32 | 1651 | TreeView_EndEditLabelNow(GetHwnd(), discardChanges); |
08b7c251 VZ |
1652 | |
1653 | DeleteTextCtrl(); | |
2bda0e17 KB |
1654 | } |
1655 | ||
08b7c251 | 1656 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) |
2bda0e17 | 1657 | { |
08b7c251 VZ |
1658 | TV_HITTESTINFO hitTestInfo; |
1659 | hitTestInfo.pt.x = (int)point.x; | |
1660 | hitTestInfo.pt.y = (int)point.y; | |
2bda0e17 | 1661 | |
d220ae32 | 1662 | TreeView_HitTest(GetHwnd(), &hitTestInfo); |
2bda0e17 | 1663 | |
08b7c251 VZ |
1664 | flags = 0; |
1665 | ||
1666 | // avoid repetition | |
1667 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ | |
1668 | flags |= wxTREE_HITTEST_##flag | |
1669 | ||
1670 | TRANSLATE_FLAG(ABOVE); | |
1671 | TRANSLATE_FLAG(BELOW); | |
1672 | TRANSLATE_FLAG(NOWHERE); | |
1673 | TRANSLATE_FLAG(ONITEMBUTTON); | |
1674 | TRANSLATE_FLAG(ONITEMICON); | |
1675 | TRANSLATE_FLAG(ONITEMINDENT); | |
1676 | TRANSLATE_FLAG(ONITEMLABEL); | |
1677 | TRANSLATE_FLAG(ONITEMRIGHT); | |
1678 | TRANSLATE_FLAG(ONITEMSTATEICON); | |
1679 | TRANSLATE_FLAG(TOLEFT); | |
1680 | TRANSLATE_FLAG(TORIGHT); | |
2bda0e17 | 1681 | |
08b7c251 VZ |
1682 | #undef TRANSLATE_FLAG |
1683 | ||
06e38c8e | 1684 | return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem); |
08b7c251 VZ |
1685 | } |
1686 | ||
f7c832a7 VZ |
1687 | bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item, |
1688 | wxRect& rect, | |
1689 | bool textOnly) const | |
1690 | { | |
1691 | RECT rc; | |
3f7bc32b | 1692 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(item), |
f7c832a7 VZ |
1693 | &rc, textOnly) ) |
1694 | { | |
1695 | rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom)); | |
1696 | ||
1697 | return TRUE; | |
1698 | } | |
1699 | else | |
1700 | { | |
1701 | // couldn't retrieve rect: for example, item isn't visible | |
1702 | return FALSE; | |
1703 | } | |
1704 | } | |
1705 | ||
23fd5130 VZ |
1706 | // ---------------------------------------------------------------------------- |
1707 | // sorting stuff | |
1708 | // ---------------------------------------------------------------------------- | |
f7c832a7 | 1709 | |
23fd5130 VZ |
1710 | static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1, |
1711 | wxTreeItemData *pItem2, | |
1712 | wxTreeCtrl *tree) | |
1713 | { | |
096c9f9b | 1714 | wxCHECK_MSG( pItem1 && pItem2, 0, |
223d09f6 | 1715 | wxT("sorting tree without data doesn't make sense") ); |
096c9f9b | 1716 | |
23fd5130 VZ |
1717 | return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId()); |
1718 | } | |
1719 | ||
95aabccc VZ |
1720 | int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
1721 | const wxTreeItemId& item2) | |
08b7c251 | 1722 | { |
837e5743 | 1723 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); |
95aabccc VZ |
1724 | } |
1725 | ||
1726 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item) | |
1727 | { | |
1728 | // rely on the fact that TreeView_SortChildren does the same thing as our | |
23fd5130 VZ |
1729 | // default behaviour, i.e. sorts items alphabetically and so call it |
1730 | // directly if we're not in derived class (much more efficient!) | |
1731 | if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) ) | |
2bda0e17 | 1732 | { |
3f7bc32b | 1733 | TreeView_SortChildren(GetHwnd(), HITEM(item), 0); |
2bda0e17 | 1734 | } |
08b7c251 | 1735 | else |
2bda0e17 | 1736 | { |
62448488 | 1737 | TV_SORTCB tvSort; |
3f7bc32b | 1738 | tvSort.hParent = HITEM(item); |
23fd5130 VZ |
1739 | tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback; |
1740 | tvSort.lParam = (LPARAM)this; | |
d220ae32 | 1741 | TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */); |
2bda0e17 | 1742 | } |
08b7c251 | 1743 | } |
2bda0e17 | 1744 | |
08b7c251 VZ |
1745 | // ---------------------------------------------------------------------------- |
1746 | // implementation | |
1747 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1748 | |
08b7c251 VZ |
1749 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id) |
1750 | { | |
1751 | if ( cmd == EN_UPDATE ) | |
2bda0e17 | 1752 | { |
08b7c251 VZ |
1753 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); |
1754 | event.SetEventObject( this ); | |
1755 | ProcessCommand(event); | |
2bda0e17 | 1756 | } |
08b7c251 | 1757 | else if ( cmd == EN_KILLFOCUS ) |
2bda0e17 | 1758 | { |
08b7c251 VZ |
1759 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); |
1760 | event.SetEventObject( this ); | |
1761 | ProcessCommand(event); | |
2bda0e17 | 1762 | } |
08b7c251 | 1763 | else |
2bda0e17 | 1764 | { |
08b7c251 VZ |
1765 | // nothing done |
1766 | return FALSE; | |
2bda0e17 | 1767 | } |
08b7c251 VZ |
1768 | |
1769 | // command processed | |
1770 | return TRUE; | |
1771 | } | |
1772 | ||
23f681ec VZ |
1773 | // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we |
1774 | // only do it during dragging, minimize wxWin overhead (this is important for | |
1775 | // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly | |
1776 | // instead of passing by wxWin events | |
1777 | long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
1778 | { | |
3f7bc32b VZ |
1779 | bool processed = FALSE; |
1780 | long rc = 0; | |
1781 | ||
1782 | bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0; | |
1783 | ||
1784 | if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) ) | |
23f681ec | 1785 | { |
3f7bc32b VZ |
1786 | // we only process mouse messages here and these parameters have the same |
1787 | // meaning for all of them | |
1788 | int x = GET_X_LPARAM(lParam), | |
1789 | y = GET_Y_LPARAM(lParam); | |
1790 | HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y); | |
1791 | ||
23f681ec VZ |
1792 | switch ( nMsg ) |
1793 | { | |
3f7bc32b VZ |
1794 | #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
1795 | case WM_LBUTTONDOWN: | |
1796 | if ( htItem && isMultiple ) | |
23f681ec | 1797 | { |
3f7bc32b VZ |
1798 | if ( wParam & MK_CONTROL ) |
1799 | { | |
1800 | SetFocus(); | |
23f681ec | 1801 | |
3f7bc32b VZ |
1802 | // toggle selected state |
1803 | ToggleItemSelection(GetHwnd(), htItem); | |
1804 | ||
1805 | ::SetFocus(GetHwnd(), htItem); | |
1806 | ||
1807 | // reset on any click without Shift | |
1808 | m_htSelStart = 0; | |
1809 | ||
1810 | processed = TRUE; | |
1811 | } | |
1812 | else if ( wParam & MK_SHIFT ) | |
1813 | { | |
1814 | // this selects all items between the starting one and | |
1815 | // the current | |
1816 | ||
1817 | if ( !m_htSelStart ) | |
1818 | { | |
1819 | // take the focused item | |
1820 | m_htSelStart = (WXHTREEITEM) | |
1821 | TreeView_GetSelection(GetHwnd()); | |
1822 | } | |
1823 | ||
1824 | SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem, | |
1825 | !(wParam & MK_CONTROL)); | |
1826 | ||
1827 | ::SetFocus(GetHwnd(), htItem); | |
23f681ec | 1828 | |
3f7bc32b VZ |
1829 | processed = TRUE; |
1830 | } | |
1831 | else // normal click | |
1832 | { | |
1833 | // clear the selection and then let the default handler | |
1834 | // do the job | |
1835 | UnselectAll(); | |
1836 | ||
1837 | // prevent the click from starting in-place editing | |
1838 | // when there was no selection in the control | |
1839 | TreeView_SelectItem(GetHwnd(), 0); | |
1840 | ||
1841 | // reset on any click without Shift | |
1842 | m_htSelStart = 0; | |
1843 | } | |
1844 | } | |
1845 | break; | |
1846 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
1847 | ||
1848 | case WM_MOUSEMOVE: | |
1849 | if ( m_dragImage ) | |
1850 | { | |
afff720b | 1851 | m_dragImage->Move(wxPoint(x, y)); |
3f7bc32b | 1852 | if ( htItem ) |
23f681ec VZ |
1853 | { |
1854 | // highlight the item as target (hiding drag image is | |
1855 | // necessary - otherwise the display will be corrupted) | |
68be9f09 | 1856 | m_dragImage->Hide(); |
3f7bc32b | 1857 | TreeView_SelectDropTarget(GetHwnd(), htItem); |
68be9f09 | 1858 | m_dragImage->Show(); |
23f681ec VZ |
1859 | } |
1860 | } | |
1861 | break; | |
1862 | ||
1863 | case WM_LBUTTONUP: | |
1864 | case WM_RBUTTONUP: | |
3f7bc32b | 1865 | if ( m_dragImage ) |
23f681ec | 1866 | { |
68be9f09 | 1867 | m_dragImage->EndDrag(); |
23f681ec VZ |
1868 | delete m_dragImage; |
1869 | m_dragImage = NULL; | |
1870 | ||
1871 | // generate the drag end event | |
1872 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId); | |
1873 | ||
3f7bc32b | 1874 | event.m_item = (WXHTREEITEM)htItem; |
23f681ec VZ |
1875 | event.m_pointDrag = wxPoint(x, y); |
1876 | event.SetEventObject(this); | |
1877 | ||
1878 | (void)GetEventHandler()->ProcessEvent(event); | |
225fe9d6 VZ |
1879 | |
1880 | // if we don't do it, the tree seems to think that 2 items | |
1881 | // are selected simultaneously which is quite weird | |
1882 | TreeView_SelectDropTarget(GetHwnd(), 0); | |
23f681ec VZ |
1883 | } |
1884 | break; | |
1885 | } | |
1886 | } | |
3f7bc32b VZ |
1887 | #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
1888 | else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple ) | |
1889 | { | |
1890 | // the tree control greys out the selected item when it loses focus and | |
1891 | // paints it as selected again when it regains it, but it won't do it | |
1892 | // for the other items itself - help it | |
1893 | wxArrayTreeItemIds selections; | |
1894 | size_t count = GetSelections(selections); | |
1895 | RECT rect; | |
1896 | for ( size_t n = 0; n < count; n++ ) | |
1897 | { | |
1898 | // TreeView_GetItemRect() will return FALSE if item is not visible, | |
1899 | // which may happen perfectly well | |
1900 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]), | |
1901 | &rect, TRUE) ) | |
1902 | { | |
1903 | ::InvalidateRect(GetHwnd(), &rect, FALSE); | |
1904 | } | |
1905 | } | |
1906 | } | |
1907 | else if ( nMsg == WM_KEYDOWN && isMultiple ) | |
1908 | { | |
1909 | bool bCtrl = wxIsCtrlDown(), | |
1910 | bShift = wxIsShiftDown(); | |
1911 | ||
1912 | // we handle.arrows and space, but not page up/down and home/end: the | |
1913 | // latter should be easy, but not the former | |
1914 | ||
2c8e4738 | 1915 | HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd()); |
3f7bc32b VZ |
1916 | if ( !m_htSelStart ) |
1917 | { | |
1918 | m_htSelStart = (WXHTREEITEM)htSel; | |
1919 | } | |
1920 | ||
1921 | if ( wParam == VK_SPACE ) | |
1922 | { | |
1923 | if ( bCtrl ) | |
1924 | { | |
1925 | ToggleItemSelection(GetHwnd(), htSel); | |
1926 | } | |
1927 | else | |
1928 | { | |
1929 | UnselectAll(); | |
1930 | ||
1931 | ::SelectItem(GetHwnd(), htSel); | |
1932 | } | |
23f681ec | 1933 | |
3f7bc32b VZ |
1934 | processed = TRUE; |
1935 | } | |
1936 | else if ( wParam == VK_UP || wParam == VK_DOWN ) | |
1937 | { | |
1938 | if ( !bCtrl && !bShift ) | |
1939 | { | |
1940 | // no modifiers, just clear selection and then let the default | |
1941 | // processing to take place | |
1942 | UnselectAll(); | |
1943 | } | |
1944 | else if ( htSel ) | |
1945 | { | |
1946 | (void)wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
1947 | ||
2c8e4738 VZ |
1948 | HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP |
1949 | ? TreeView_GetPrevVisible(GetHwnd(), htSel) | |
1950 | : TreeView_GetNextVisible(GetHwnd(), htSel)); | |
3f7bc32b VZ |
1951 | |
1952 | if ( !htNext ) | |
1953 | { | |
1954 | // at the top/bottom | |
1955 | htNext = htSel; | |
1956 | } | |
1957 | ||
1958 | if ( bShift ) | |
1959 | { | |
1960 | SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext); | |
1961 | } | |
1962 | else // bCtrl | |
1963 | { | |
1964 | // without changing selection | |
1965 | ::SetFocus(GetHwnd(), htNext); | |
1966 | } | |
1967 | ||
1968 | processed = TRUE; | |
1969 | } | |
1970 | } | |
1971 | } | |
1972 | #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
1973 | ||
1974 | if ( !processed ) | |
1975 | rc = wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
1976 | ||
1977 | return rc; | |
23f681ec VZ |
1978 | } |
1979 | ||
08b7c251 | 1980 | // process WM_NOTIFY Windows message |
a23fd0e1 | 1981 | bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
08b7c251 VZ |
1982 | { |
1983 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1984 | wxEventType eventType = wxEVT_NULL; | |
1985 | NMHDR *hdr = (NMHDR *)lParam; | |
1986 | ||
1987 | switch ( hdr->code ) | |
2bda0e17 | 1988 | { |
08b7c251 VZ |
1989 | case TVN_BEGINDRAG: |
1990 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
1991 | // fall through | |
1992 | ||
1993 | case TVN_BEGINRDRAG: | |
1994 | { | |
1995 | if ( eventType == wxEVT_NULL ) | |
1996 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; | |
1997 | //else: left drag, already set above | |
1998 | ||
1999 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
2000 | ||
06e38c8e | 2001 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
08b7c251 | 2002 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); |
23f681ec VZ |
2003 | |
2004 | // don't allow dragging by default: the user code must | |
2005 | // explicitly say that it wants to allow it to avoid breaking | |
2006 | // the old apps | |
2007 | event.Veto(); | |
08b7c251 | 2008 | } |
696e1ea0 | 2009 | break; |
08b7c251 VZ |
2010 | |
2011 | case TVN_BEGINLABELEDIT: | |
2012 | { | |
2013 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; | |
2014 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2015 | ||
06e38c8e | 2016 | event.m_item = (WXHTREEITEM) info->item.hItem; |
5ea47806 | 2017 | event.m_label = info->item.pszText; |
08b7c251 | 2018 | } |
696e1ea0 | 2019 | break; |
08b7c251 VZ |
2020 | |
2021 | case TVN_DELETEITEM: | |
2022 | { | |
2023 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; | |
2024 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
2025 | ||
696e1ea0 VZ |
2026 | event.m_item = (WXHTREEITEM)tv->itemOld.hItem; |
2027 | ||
2028 | if ( m_hasAnyAttr ) | |
2029 | { | |
2030 | delete (wxTreeItemAttr *)m_attrs. | |
2031 | Delete((long)tv->itemOld.hItem); | |
2032 | } | |
08b7c251 | 2033 | } |
696e1ea0 | 2034 | break; |
08b7c251 VZ |
2035 | |
2036 | case TVN_ENDLABELEDIT: | |
2037 | { | |
2038 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; | |
2039 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2040 | ||
5ea47806 VZ |
2041 | event.m_item = (WXHTREEITEM)info->item.hItem; |
2042 | event.m_label = info->item.pszText; | |
1ee4ead5 VZ |
2043 | if (info->item.pszText == NULL) |
2044 | return FALSE; | |
08b7c251 VZ |
2045 | break; |
2046 | } | |
2047 | ||
2048 | case TVN_GETDISPINFO: | |
2049 | eventType = wxEVT_COMMAND_TREE_GET_INFO; | |
2050 | // fall through | |
2051 | ||
2052 | case TVN_SETDISPINFO: | |
2053 | { | |
2054 | if ( eventType == wxEVT_NULL ) | |
2055 | eventType = wxEVT_COMMAND_TREE_SET_INFO; | |
2056 | //else: get, already set above | |
2057 | ||
2058 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2059 | ||
06e38c8e | 2060 | event.m_item = (WXHTREEITEM) info->item.hItem; |
08b7c251 VZ |
2061 | break; |
2062 | } | |
2063 | ||
2064 | case TVN_ITEMEXPANDING: | |
2065 | event.m_code = FALSE; | |
2066 | // fall through | |
2067 | ||
2068 | case TVN_ITEMEXPANDED: | |
2069 | { | |
2070 | NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam; | |
2071 | ||
2072 | bool expand = FALSE; | |
2073 | switch ( tv->action ) | |
2074 | { | |
2075 | case TVE_EXPAND: | |
2076 | expand = TRUE; | |
2077 | break; | |
2078 | ||
2079 | case TVE_COLLAPSE: | |
2080 | expand = FALSE; | |
2081 | break; | |
2082 | ||
2083 | default: | |
f6bcfd97 | 2084 | wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action); |
08b7c251 VZ |
2085 | } |
2086 | ||
a17e237f | 2087 | bool ing = ((int)hdr->code == TVN_ITEMEXPANDING); |
08b7c251 VZ |
2088 | eventType = g_events[expand][ing]; |
2089 | ||
06e38c8e | 2090 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
08b7c251 | 2091 | } |
696e1ea0 | 2092 | break; |
08b7c251 VZ |
2093 | |
2094 | case TVN_KEYDOWN: | |
2095 | { | |
2096 | eventType = wxEVT_COMMAND_TREE_KEY_DOWN; | |
2097 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; | |
2098 | ||
2099 | event.m_code = wxCharCodeMSWToWX(info->wVKey); | |
23fd5130 | 2100 | |
3f7bc32b VZ |
2101 | // a separate event for Space/Return |
2102 | if ( !wxIsCtrlDown() && !wxIsShiftDown() && | |
2103 | ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) ) | |
23fd5130 VZ |
2104 | { |
2105 | wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, | |
2106 | m_windowId); | |
2107 | event2.SetEventObject(this); | |
3f7bc32b VZ |
2108 | if ( !(GetWindowStyle() & wxTR_MULTIPLE) ) |
2109 | { | |
2110 | event2.m_item = GetSelection(); | |
2111 | } | |
2112 | //else: don't know how to get it | |
23fd5130 | 2113 | |
3f7bc32b | 2114 | (void)GetEventHandler()->ProcessEvent(event2); |
23fd5130 | 2115 | } |
08b7c251 | 2116 | } |
696e1ea0 | 2117 | break; |
08b7c251 VZ |
2118 | |
2119 | case TVN_SELCHANGED: | |
2120 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; | |
2121 | // fall through | |
2122 | ||
2123 | case TVN_SELCHANGING: | |
2124 | { | |
2125 | if ( eventType == wxEVT_NULL ) | |
2126 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; | |
2127 | //else: already set above | |
2128 | ||
2129 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
2130 | ||
06e38c8e JS |
2131 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
2132 | event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem; | |
08b7c251 | 2133 | } |
696e1ea0 VZ |
2134 | break; |
2135 | ||
f6bcfd97 | 2136 | #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY |
696e1ea0 VZ |
2137 | case NM_CUSTOMDRAW: |
2138 | { | |
2139 | LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam; | |
2140 | NMCUSTOMDRAW& nmcd = lptvcd->nmcd; | |
2141 | switch( nmcd.dwDrawStage ) | |
2142 | { | |
2143 | case CDDS_PREPAINT: | |
2144 | // if we've got any items with non standard attributes, | |
2145 | // notify us before painting each item | |
2146 | *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW | |
2147 | : CDRF_DODEFAULT; | |
2148 | return TRUE; | |
2149 | ||
2150 | case CDDS_ITEMPREPAINT: | |
2151 | { | |
2152 | wxTreeItemAttr *attr = | |
2153 | (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec); | |
2154 | ||
2155 | if ( !attr ) | |
2156 | { | |
2157 | // nothing to do for this item | |
2158 | return CDRF_DODEFAULT; | |
2159 | } | |
2160 | ||
2161 | HFONT hFont; | |
2162 | wxColour colText, colBack; | |
2163 | if ( attr->HasFont() ) | |
2164 | { | |
2165 | wxFont font = attr->GetFont(); | |
2166 | hFont = (HFONT)font.GetResourceHandle(); | |
2167 | } | |
2168 | else | |
2169 | { | |
2170 | hFont = 0; | |
2171 | } | |
2172 | ||
2173 | if ( attr->HasTextColour() ) | |
2174 | { | |
2175 | colText = attr->GetTextColour(); | |
2176 | } | |
2177 | else | |
2178 | { | |
2179 | colText = GetForegroundColour(); | |
2180 | } | |
2181 | ||
2182 | // selection colours should override ours | |
2183 | if ( nmcd.uItemState & CDIS_SELECTED ) | |
2184 | { | |
2185 | DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT); | |
2186 | lptvcd->clrTextBk = clrBk; | |
2187 | ||
2188 | // try to make the text visible | |
2189 | lptvcd->clrText = wxColourToRGB(colText); | |
2190 | lptvcd->clrText |= ~clrBk; | |
2191 | lptvcd->clrText &= 0x00ffffff; | |
2192 | } | |
2193 | else | |
2194 | { | |
2195 | if ( attr->HasBackgroundColour() ) | |
2196 | { | |
2197 | colBack = attr->GetBackgroundColour(); | |
2198 | } | |
2199 | else | |
2200 | { | |
2201 | colBack = GetBackgroundColour(); | |
2202 | } | |
2203 | ||
2204 | lptvcd->clrText = wxColourToRGB(colText); | |
2205 | lptvcd->clrTextBk = wxColourToRGB(colBack); | |
2206 | } | |
2207 | ||
2208 | // note that if we wanted to set colours for | |
2209 | // individual columns (subitems), we would have | |
2210 | // returned CDRF_NOTIFYSUBITEMREDRAW from here | |
2211 | if ( hFont ) | |
2212 | { | |
2213 | ::SelectObject(nmcd.hdc, hFont); | |
2214 | ||
2215 | *result = CDRF_NEWFONT; | |
2216 | } | |
2217 | else | |
2218 | { | |
2219 | *result = CDRF_DODEFAULT; | |
2220 | } | |
2221 | ||
2222 | return TRUE; | |
2223 | } | |
2224 | ||
2225 | default: | |
2226 | *result = CDRF_DODEFAULT; | |
2227 | return TRUE; | |
2228 | } | |
2229 | } | |
3897b707 | 2230 | // break; // can never be reached |
6ecfe2ac | 2231 | #endif // _WIN32_IE >= 0x300 |
08b7c251 | 2232 | |
f6bcfd97 BP |
2233 | case NM_DBLCLK: |
2234 | case NM_RCLICK: | |
2235 | { | |
2236 | TV_HITTESTINFO tvhti; | |
2237 | ::GetCursorPos(&tvhti.pt); | |
2238 | ::ScreenToClient(GetHwnd(), &tvhti.pt); | |
2239 | if ( TreeView_HitTest(GetHwnd(), &tvhti) ) | |
2240 | { | |
2241 | if ( tvhti.flags & TVHT_ONITEM ) | |
2242 | { | |
2243 | event.m_item = (WXHTREEITEM) tvhti.hItem; | |
2244 | eventType = (int)hdr->code == NM_DBLCLK | |
2245 | ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED | |
2246 | : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK; | |
8591268f VZ |
2247 | |
2248 | event.m_pointDrag.x = tvhti.pt.x; | |
2249 | event.m_pointDrag.y = tvhti.pt.y; | |
f6bcfd97 BP |
2250 | } |
2251 | ||
2252 | break; | |
2253 | } | |
2254 | } | |
2255 | // fall through | |
2256 | ||
08b7c251 | 2257 | default: |
a23fd0e1 | 2258 | return wxControl::MSWOnNotify(idCtrl, lParam, result); |
2bda0e17 | 2259 | } |
08b7c251 VZ |
2260 | |
2261 | event.SetEventObject(this); | |
2262 | event.SetEventType(eventType); | |
2263 | ||
fd3f686c | 2264 | bool processed = GetEventHandler()->ProcessEvent(event); |
08b7c251 VZ |
2265 | |
2266 | // post processing | |
5ea47806 | 2267 | switch ( hdr->code ) |
2bda0e17 | 2268 | { |
f6bcfd97 BP |
2269 | case NM_DBLCLK: |
2270 | // we translate NM_DBLCLK into ACTIVATED event, so don't interpret | |
2271 | // the return code of this event handler as the return value for | |
2272 | // NM_DBLCLK - otherwise, double clicking the item to toggle its | |
2273 | // expanded status would never work | |
2274 | *result = FALSE; | |
2275 | break; | |
2276 | ||
23f681ec VZ |
2277 | case TVN_BEGINDRAG: |
2278 | case TVN_BEGINRDRAG: | |
2279 | if ( event.IsAllowed() ) | |
2280 | { | |
2281 | // normally this is impossible because the m_dragImage is | |
2282 | // deleted once the drag operation is over | |
2283 | wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") ); | |
2284 | ||
2285 | m_dragImage = new wxDragImage(*this, event.m_item); | |
2286 | m_dragImage->BeginDrag(wxPoint(0, 0), this); | |
68be9f09 | 2287 | m_dragImage->Show(); |
23f681ec VZ |
2288 | } |
2289 | break; | |
2290 | ||
5ea47806 VZ |
2291 | case TVN_DELETEITEM: |
2292 | { | |
2293 | // NB: we might process this message using wxWindows event | |
2294 | // tables, but due to overhead of wxWin event system we | |
2295 | // prefer to do it here ourself (otherwise deleting a tree | |
2296 | // with many items is just too slow) | |
2297 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
74b31181 VZ |
2298 | |
2299 | wxTreeItemId item = event.m_item; | |
2300 | if ( HasIndirectData(item) ) | |
2301 | { | |
2302 | wxTreeItemIndirectData *data = (wxTreeItemIndirectData *) | |
2303 | tv->itemOld.lParam; | |
2304 | delete data; // can't be NULL here | |
2305 | ||
2306 | m_itemsWithIndirectData.Remove(item); | |
484523cf JS |
2307 | #if 0 |
2308 | int iIndex = m_itemsWithIndirectData.Index(item); | |
2309 | wxASSERT( iIndex != wxNOT_FOUND) ; | |
2310 | m_itemsWithIndirectData.wxBaseArray::RemoveAt((size_t)iIndex); | |
2311 | #endif | |
74b31181 VZ |
2312 | } |
2313 | else | |
2314 | { | |
2315 | wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam; | |
2316 | delete data; // may be NULL, ok | |
2317 | } | |
08b7c251 | 2318 | |
5ea47806 VZ |
2319 | processed = TRUE; // Make sure we don't get called twice |
2320 | } | |
2321 | break; | |
2322 | ||
2323 | case TVN_BEGINLABELEDIT: | |
2324 | // return TRUE to cancel label editing | |
2325 | *result = !event.IsAllowed(); | |
2326 | break; | |
2327 | ||
2328 | case TVN_ENDLABELEDIT: | |
188781db VZ |
2329 | // return TRUE to set the label to the new string: note that we |
2330 | // also must pretend that we did process the message or it is going | |
2331 | // to be passed to DefWindowProc() which will happily return FALSE | |
2332 | // cancelling the label change | |
5ea47806 | 2333 | *result = event.IsAllowed(); |
188781db | 2334 | processed = TRUE; |
5ea47806 VZ |
2335 | |
2336 | // ensure that we don't have the text ctrl which is going to be | |
2337 | // deleted any more | |
2338 | DeleteTextCtrl(); | |
2339 | break; | |
2340 | ||
2341 | case TVN_SELCHANGING: | |
2342 | case TVN_ITEMEXPANDING: | |
2343 | // return TRUE to prevent the action from happening | |
2344 | *result = !event.IsAllowed(); | |
2345 | break; | |
2346 | ||
74b31181 VZ |
2347 | case TVN_GETDISPINFO: |
2348 | // NB: so far the user can't set the image himself anyhow, so do it | |
2349 | // anyway - but this may change later | |
33ac7e6f | 2350 | // if ( /* !processed && */ 1 ) |
74b31181 VZ |
2351 | { |
2352 | wxTreeItemId item = event.m_item; | |
2353 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2354 | if ( info->item.mask & TVIF_IMAGE ) | |
2355 | { | |
2356 | info->item.iImage = | |
2357 | DoGetItemImageFromData | |
2358 | ( | |
2359 | item, | |
2360 | IsExpanded(item) ? wxTreeItemIcon_Expanded | |
2361 | : wxTreeItemIcon_Normal | |
2362 | ); | |
2363 | } | |
2364 | if ( info->item.mask & TVIF_SELECTEDIMAGE ) | |
2365 | { | |
2366 | info->item.iSelectedImage = | |
2367 | DoGetItemImageFromData | |
2368 | ( | |
2369 | item, | |
2370 | IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded | |
2371 | : wxTreeItemIcon_Selected | |
2372 | ); | |
2373 | } | |
33ac7e6f | 2374 | } |
74b31181 VZ |
2375 | break; |
2376 | ||
5ea47806 VZ |
2377 | //default: |
2378 | // for the other messages the return value is ignored and there is | |
2379 | // nothing special to do | |
2380 | } | |
fd3f686c VZ |
2381 | |
2382 | return processed; | |
2bda0e17 KB |
2383 | } |
2384 | ||
08b7c251 | 2385 | #endif // __WIN95__ |
2bda0e17 | 2386 |