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