]>
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 | ||
c6f4913a | 39 | #include "wx/app.h" |
08b7c251 | 40 | #include "wx/log.h" |
ce3ed50d | 41 | #include "wx/dynarray.h" |
08b7c251 | 42 | #include "wx/imaglist.h" |
77cff606 | 43 | #include "wx/settings.h" |
484523cf | 44 | #include "wx/msw/treectrl.h" |
23f681ec VZ |
45 | #include "wx/msw/dragimag.h" |
46 | ||
c42404a5 VZ |
47 | #ifdef __GNUWIN32_OLD__ |
48 | #include "wx/msw/gnuwin32/extra.h" | |
65fd5cb0 | 49 | #endif |
9a05fd8d | 50 | |
b39dbf34 | 51 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)) |
08b7c251 | 52 | #include <commctrl.h> |
2bda0e17 KB |
53 | #endif |
54 | ||
55 | // Bug in headers, sometimes | |
56 | #ifndef TVIS_FOCUSED | |
08b7c251 | 57 | #define TVIS_FOCUSED 0x0001 |
2bda0e17 KB |
58 | #endif |
59 | ||
bb448552 VZ |
60 | #ifndef TV_FIRST |
61 | #define TV_FIRST 0x1100 | |
62 | #endif | |
63 | ||
b94ae1ea VZ |
64 | #ifndef TVS_CHECKBOXES |
65 | #define TVS_CHECKBOXES 0x0100 | |
66 | #endif | |
67 | ||
5ece068d VZ |
68 | #ifndef TVS_FULLROWSELECT |
69 | #define TVS_FULLROWSELECT 0x1000 | |
70 | #endif | |
71 | ||
bb448552 VZ |
72 | // old headers might miss these messages (comctl32.dll 4.71+ only) |
73 | #ifndef TVM_SETBKCOLOR | |
74 | #define TVM_SETBKCOLOR (TV_FIRST + 29) | |
75 | #define TVM_SETTEXTCOLOR (TV_FIRST + 30) | |
76 | #endif | |
77 | ||
f888d614 VZ |
78 | // macros to hide the cast ugliness |
79 | // -------------------------------- | |
80 | ||
81 | // ptr is the real item id, i.e. wxTreeItemId::m_pItem | |
82 | #define HITEM_PTR(ptr) (HTREEITEM)(ptr) | |
83 | ||
84 | // item here is a wxTreeItemId | |
85 | #define HITEM(item) HITEM_PTR((item).m_pItem) | |
3f7bc32b VZ |
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 | ||
3f7bc32b VZ |
95 | // ---------------------------------------------------------------------------- |
96 | // private functions | |
97 | // ---------------------------------------------------------------------------- | |
98 | ||
99 | // wrapper for TreeView_HitTest | |
100 | static HTREEITEM GetItemFromPoint(HWND hwndTV, int x, int y) | |
101 | { | |
102 | TV_HITTESTINFO tvht; | |
103 | tvht.pt.x = x; | |
104 | tvht.pt.y = y; | |
105 | ||
3f7bc32b VZ |
106 | return (HTREEITEM)TreeView_HitTest(hwndTV, &tvht); |
107 | } | |
108 | ||
109 | #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
110 | ||
111 | // wrappers for TreeView_GetItem/TreeView_SetItem | |
112 | static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem) | |
113 | { | |
a9c1265f | 114 | |
3f7bc32b VZ |
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 | ||
04cd30de | 128 | static void SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = true) |
3f7bc32b VZ |
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 | { | |
04cd30de | 144 | SelectItem(hwndTV, htItem, false); |
3f7bc32b VZ |
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, | |
04cd30de | 157 | bool unselectOthers = true) |
3f7bc32b VZ |
158 | { |
159 | // find the first (or last) item and select it | |
04cd30de | 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 | ||
04cd30de | 171 | cont = false; |
3f7bc32b VZ |
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 | { |
a9c1265f VZ |
290 | wxZeroMemory(*this); |
291 | ||
9dfbf520 VZ |
292 | // hItem member is always valid |
293 | mask = mask_ | TVIF_HANDLE; | |
08b7c251 | 294 | stateMask = stateMask_; |
3f7bc32b | 295 | hItem = HITEM(item); |
08b7c251 VZ |
296 | } |
297 | }; | |
f3ef286f | 298 | |
a9c1265f VZ |
299 | // wxVirutalNode is used in place of a single root when 'hidden' root is |
300 | // specified. | |
efa38350 | 301 | class wxVirtualNode : public wxTreeViewItem |
a9c1265f VZ |
302 | { |
303 | public: | |
304 | wxVirtualNode(wxTreeItemData *data) | |
efa38350 | 305 | : wxTreeViewItem(TVI_ROOT, 0) |
a9c1265f VZ |
306 | { |
307 | m_data = data; | |
a9c1265f VZ |
308 | } |
309 | ||
310 | ~wxVirtualNode() | |
311 | { | |
312 | delete m_data; | |
a9c1265f VZ |
313 | } |
314 | ||
315 | wxTreeItemData *GetData() const { return m_data; } | |
316 | void SetData(wxTreeItemData *data) { delete m_data; m_data = data; } | |
317 | ||
318 | private: | |
a9c1265f | 319 | wxTreeItemData *m_data; |
22f3361e VZ |
320 | |
321 | DECLARE_NO_COPY_CLASS(wxVirtualNode) | |
a9c1265f VZ |
322 | }; |
323 | ||
f3ef286f | 324 | #ifdef __VISUALC__ |
197dd9af | 325 | #pragma warning( default : 4097 ) |
f3ef286f | 326 | #endif |
2bda0e17 | 327 | |
a9c1265f VZ |
328 | // a macro to get the virtual root, returns NULL if none |
329 | #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot) | |
330 | ||
04cd30de | 331 | // returns true if the item is the virtual root |
a9c1265f VZ |
332 | #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT) |
333 | ||
9dfbf520 | 334 | // a class which encapsulates the tree traversal logic: it vists all (unless |
04cd30de | 335 | // OnVisit() returns false) items under the given one |
9dfbf520 VZ |
336 | class wxTreeTraversal |
337 | { | |
338 | public: | |
339 | wxTreeTraversal(const wxTreeCtrl *tree) | |
340 | { | |
341 | m_tree = tree; | |
342 | } | |
343 | ||
344 | // do traverse the tree: visit all items (recursively by default) under the | |
04cd30de RL |
345 | // given one; return true if all items were traversed or false if the |
346 | // traversal was aborted because OnVisit returned false | |
347 | bool DoTraverse(const wxTreeItemId& root, bool recursively = true); | |
9dfbf520 VZ |
348 | |
349 | // override this function to do whatever is needed for each item, return | |
04cd30de | 350 | // false to stop traversing |
9dfbf520 VZ |
351 | virtual bool OnVisit(const wxTreeItemId& item) = 0; |
352 | ||
353 | protected: | |
354 | const wxTreeCtrl *GetTree() const { return m_tree; } | |
355 | ||
356 | private: | |
357 | bool Traverse(const wxTreeItemId& root, bool recursively); | |
358 | ||
359 | const wxTreeCtrl *m_tree; | |
22f3361e VZ |
360 | |
361 | DECLARE_NO_COPY_CLASS(wxTreeTraversal) | |
9dfbf520 VZ |
362 | }; |
363 | ||
74b31181 VZ |
364 | // internal class for getting the selected items |
365 | class TraverseSelections : public wxTreeTraversal | |
366 | { | |
367 | public: | |
368 | TraverseSelections(const wxTreeCtrl *tree, | |
369 | wxArrayTreeItemIds& selections) | |
370 | : wxTreeTraversal(tree), m_selections(selections) | |
371 | { | |
372 | m_selections.Empty(); | |
373 | ||
374 | DoTraverse(tree->GetRootItem()); | |
375 | } | |
376 | ||
377 | virtual bool OnVisit(const wxTreeItemId& item) | |
378 | { | |
a9c1265f VZ |
379 | // can't visit a virtual node. |
380 | if ( (GetTree()->GetRootItem() == item) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT)) | |
381 | { | |
04cd30de | 382 | return true; |
a9c1265f VZ |
383 | } |
384 | ||
3f7bc32b | 385 | #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
74b31181 | 386 | if ( GetTree()->IsItemChecked(item) ) |
3f7bc32b VZ |
387 | #else |
388 | if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item)) ) | |
389 | #endif | |
74b31181 VZ |
390 | { |
391 | m_selections.Add(item); | |
392 | } | |
393 | ||
04cd30de | 394 | return true; |
74b31181 VZ |
395 | } |
396 | ||
e47c4d48 VZ |
397 | size_t GetCount() const { return m_selections.GetCount(); } |
398 | ||
74b31181 VZ |
399 | private: |
400 | wxArrayTreeItemIds& m_selections; | |
401 | }; | |
402 | ||
403 | // internal class for counting tree items | |
404 | class TraverseCounter : public wxTreeTraversal | |
405 | { | |
406 | public: | |
407 | TraverseCounter(const wxTreeCtrl *tree, | |
408 | const wxTreeItemId& root, | |
409 | bool recursively) | |
410 | : wxTreeTraversal(tree) | |
411 | { | |
412 | m_count = 0; | |
413 | ||
414 | DoTraverse(root, recursively); | |
415 | } | |
416 | ||
33ac7e6f | 417 | virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item)) |
74b31181 VZ |
418 | { |
419 | m_count++; | |
420 | ||
04cd30de | 421 | return true; |
74b31181 VZ |
422 | } |
423 | ||
424 | size_t GetCount() const { return m_count; } | |
425 | ||
426 | private: | |
427 | size_t m_count; | |
428 | }; | |
429 | ||
430 | // ---------------------------------------------------------------------------- | |
431 | // This class is needed for support of different images: the Win32 common | |
432 | // control natively supports only 2 images (the normal one and another for the | |
433 | // selected state). We wish to provide support for 2 more of them for folder | |
434 | // items (i.e. those which have children): for expanded state and for expanded | |
435 | // selected state. For this we use this structure to store the additional items | |
436 | // images. | |
437 | // | |
438 | // There is only one problem with this: when we retrieve the item's data, we | |
439 | // don't know whether we get a pointer to wxTreeItemData or | |
502a2b18 VZ |
440 | // wxTreeItemIndirectData. So we always set the item id to an invalid value |
441 | // in this class and the code using the client data checks for it and retrieves | |
442 | // the real client data in this case. | |
74b31181 | 443 | // ---------------------------------------------------------------------------- |
696e1ea0 | 444 | |
502a2b18 | 445 | class wxTreeItemIndirectData : public wxTreeItemData |
74b31181 VZ |
446 | { |
447 | public: | |
448 | // ctor associates this data with the item and the real item data becomes | |
449 | // available through our GetData() method | |
450 | wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item) | |
451 | { | |
452 | for ( size_t n = 0; n < WXSIZEOF(m_images); n++ ) | |
453 | { | |
454 | m_images[n] = -1; | |
455 | } | |
456 | ||
457 | // save the old data | |
458 | m_data = tree->GetItemData(item); | |
459 | ||
460 | // and set ourselves as the new one | |
461 | tree->SetIndirectItemData(item, this); | |
cf48ccc0 VZ |
462 | |
463 | // we must have the invalid value for the item | |
464 | m_pItem = 0l; | |
74b31181 VZ |
465 | } |
466 | ||
467 | // dtor deletes the associated data as well | |
502a2b18 | 468 | virtual ~wxTreeItemIndirectData() { delete m_data; } |
74b31181 VZ |
469 | |
470 | // accessors | |
471 | // get the real data associated with the item | |
472 | wxTreeItemData *GetData() const { return m_data; } | |
473 | // change it | |
474 | void SetData(wxTreeItemData *data) { m_data = data; } | |
475 | ||
476 | // do we have such image? | |
477 | bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; } | |
478 | // get image | |
479 | int GetImage(wxTreeItemIcon which) const { return m_images[which]; } | |
480 | // change it | |
481 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } | |
482 | ||
483 | private: | |
484 | // all the images associated with the item | |
485 | int m_images[wxTreeItemIcon_Max]; | |
486 | ||
502a2b18 | 487 | // the real client data |
74b31181 | 488 | wxTreeItemData *m_data; |
22f3361e VZ |
489 | |
490 | DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData) | |
74b31181 VZ |
491 | }; |
492 | ||
23f681ec | 493 | // ---------------------------------------------------------------------------- |
3f7bc32b | 494 | // wxWin macros |
08b7c251 VZ |
495 | // ---------------------------------------------------------------------------- |
496 | ||
23f681ec | 497 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) |
2bda0e17 | 498 | |
08b7c251 | 499 | // ---------------------------------------------------------------------------- |
deb1de30 | 500 | // constants |
08b7c251 VZ |
501 | // ---------------------------------------------------------------------------- |
502 | ||
deb1de30 VZ |
503 | // indices in gs_expandEvents table below |
504 | enum | |
505 | { | |
506 | IDX_COLLAPSE, | |
507 | IDX_EXPAND, | |
508 | IDX_WHAT_MAX | |
509 | }; | |
510 | ||
511 | enum | |
512 | { | |
513 | IDX_DONE, | |
514 | IDX_DOING, | |
515 | IDX_HOW_MAX | |
516 | }; | |
517 | ||
518 | // handy table for sending events - it has to be initialized during run-time | |
519 | // now so can't be const any more | |
520 | static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX]; | |
521 | ||
522 | /* | |
523 | but logically it's a const table with the following entries: | |
524 | = | |
2bda0e17 | 525 | { |
08b7c251 VZ |
526 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, |
527 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } | |
528 | }; | |
deb1de30 | 529 | */ |
08b7c251 VZ |
530 | |
531 | // ============================================================================ | |
532 | // implementation | |
533 | // ============================================================================ | |
534 | ||
9dfbf520 VZ |
535 | // ---------------------------------------------------------------------------- |
536 | // tree traversal | |
537 | // ---------------------------------------------------------------------------- | |
538 | ||
539 | bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively) | |
540 | { | |
541 | if ( !OnVisit(root) ) | |
04cd30de | 542 | return false; |
9dfbf520 VZ |
543 | |
544 | return Traverse(root, recursively); | |
545 | } | |
546 | ||
547 | bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively) | |
548 | { | |
ee4b2721 | 549 | wxTreeItemIdValue cookie; |
9dfbf520 VZ |
550 | wxTreeItemId child = m_tree->GetFirstChild(root, cookie); |
551 | while ( child.IsOk() ) | |
552 | { | |
553 | // depth first traversal | |
04cd30de RL |
554 | if ( recursively && !Traverse(child, true) ) |
555 | return false; | |
9dfbf520 VZ |
556 | |
557 | if ( !OnVisit(child) ) | |
04cd30de | 558 | return false; |
9dfbf520 VZ |
559 | |
560 | child = m_tree->GetNextChild(root, cookie); | |
561 | } | |
562 | ||
04cd30de | 563 | return true; |
9dfbf520 VZ |
564 | } |
565 | ||
08b7c251 VZ |
566 | // ---------------------------------------------------------------------------- |
567 | // construction and destruction | |
568 | // ---------------------------------------------------------------------------- | |
569 | ||
570 | void wxTreeCtrl::Init() | |
571 | { | |
572 | m_imageListNormal = NULL; | |
573 | m_imageListState = NULL; | |
04cd30de | 574 | m_ownsImageListNormal = m_ownsImageListState = false; |
08b7c251 | 575 | m_textCtrl = NULL; |
04cd30de | 576 | m_hasAnyAttr = false; |
23f681ec | 577 | m_dragImage = NULL; |
a9c1265f | 578 | m_pVirtualRoot = NULL; |
9ab5ef13 | 579 | |
deb1de30 VZ |
580 | // initialize the global array of events now as it can't be done statically |
581 | // with the wxEVT_XXX values being allocated during run-time only | |
582 | gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED; | |
583 | gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING; | |
584 | gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED; | |
585 | gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING; | |
2bda0e17 KB |
586 | } |
587 | ||
9dfbf520 VZ |
588 | bool wxTreeCtrl::Create(wxWindow *parent, |
589 | wxWindowID id, | |
590 | const wxPoint& pos, | |
591 | const wxSize& size, | |
592 | long style, | |
593 | const wxValidator& validator, | |
08b7c251 | 594 | const wxString& name) |
2bda0e17 | 595 | { |
08b7c251 | 596 | Init(); |
2bda0e17 | 597 | |
7699361c JS |
598 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) |
599 | style |= wxBORDER_SUNKEN; | |
600 | ||
9dfbf520 | 601 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
04cd30de | 602 | return false; |
2bda0e17 | 603 | |
7699361c JS |
604 | DWORD exStyle = 0; |
605 | DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle); | |
606 | wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS; | |
2bda0e17 | 607 | |
63da7df7 JS |
608 | if ((m_windowStyle & wxTR_NO_LINES) == 0) |
609 | wstyle |= TVS_HASLINES; | |
08b7c251 VZ |
610 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) |
611 | wstyle |= TVS_HASBUTTONS; | |
2bda0e17 | 612 | |
08b7c251 VZ |
613 | if ( m_windowStyle & wxTR_EDIT_LABELS ) |
614 | wstyle |= TVS_EDITLABELS; | |
2bda0e17 | 615 | |
08b7c251 VZ |
616 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) |
617 | wstyle |= TVS_LINESATROOT; | |
deb1de30 | 618 | |
c6f4913a | 619 | if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT ) |
deb1de30 | 620 | { |
c6f4913a VS |
621 | if ( wxTheApp->GetComCtl32Version() >= 471 ) |
622 | wstyle |= TVS_FULLROWSELECT; | |
623 | } | |
624 | ||
3f7bc32b VZ |
625 | // using TVS_CHECKBOXES for emulation of a multiselection tree control |
626 | // doesn't work without the new enough headers | |
627 | #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \ | |
628 | !defined( __GNUWIN32_OLD__ ) && \ | |
c42404a5 VZ |
629 | !defined( __BORLANDC__ ) && \ |
630 | !defined( __WATCOMC__ ) && \ | |
631 | (!defined(__VISUALC__) || (__VISUALC__ > 1010)) | |
a20a10fe | 632 | |
9dfbf520 VZ |
633 | // we emulate the multiple selection tree controls by using checkboxes: set |
634 | // up the image list we need for this if we do have multiple selections | |
635 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
10fcf31a | 636 | wstyle |= TVS_CHECKBOXES; |
3f7bc32b | 637 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
9dfbf520 | 638 | |
08b7c251 | 639 | // Create the tree control. |
9dfbf520 | 640 | if ( !MSWCreateControl(WC_TREEVIEW, wstyle) ) |
04cd30de | 641 | return false; |
9dfbf520 | 642 | |
f6bcfd97 | 643 | #if wxUSE_COMCTL32_SAFELY |
a756f210 | 644 | wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 BP |
645 | wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
646 | #elif 1 | |
a756f210 | 647 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 | 648 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
fbdcff4a JS |
649 | #else |
650 | // This works around a bug in the Windows tree control whereby for some versions | |
651 | // of comctrl32, setting any colour actually draws the background in black. | |
652 | // This will initialise the background to the system colour. | |
f6bcfd97 BP |
653 | // THIS FIX NOW REVERTED since it caused problems on _other_ systems. |
654 | // Assume the user has an updated comctl32.dll. | |
fbdcff4a | 655 | ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1); |
a756f210 | 656 | wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 | 657 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
fbdcff4a JS |
658 | #endif |
659 | ||
5aeeab14 | 660 | |
9dfbf520 VZ |
661 | // VZ: this is some experimental code which may be used to get the |
662 | // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71. | |
663 | // AFAIK, the standard DLL does about the same thing anyhow. | |
664 | #if 0 | |
665 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
666 | { | |
667 | wxBitmap bmp; | |
668 | ||
669 | // create the DC compatible with the current screen | |
670 | HDC hdcMem = CreateCompatibleDC(NULL); | |
671 | ||
672 | // create a mono bitmap of the standard size | |
673 | int x = GetSystemMetrics(SM_CXMENUCHECK); | |
674 | int y = GetSystemMetrics(SM_CYMENUCHECK); | |
04cd30de | 675 | wxImageList imagelistCheckboxes(x, y, false, 2); |
9dfbf520 VZ |
676 | HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size |
677 | 1, // # of color planes | |
678 | 1, // # bits needed for one pixel | |
679 | 0); // array containing colour data | |
680 | SelectObject(hdcMem, hbmpCheck); | |
681 | ||
682 | // then draw a check mark into it | |
683 | RECT rect = { 0, 0, x, y }; | |
684 | if ( !::DrawFrameControl(hdcMem, &rect, | |
685 | DFC_BUTTON, | |
686 | DFCS_BUTTONCHECK | DFCS_CHECKED) ) | |
687 | { | |
223d09f6 | 688 | wxLogLastError(wxT("DrawFrameControl(check)")); |
9dfbf520 VZ |
689 | } |
690 | ||
691 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); | |
692 | imagelistCheckboxes.Add(bmp); | |
693 | ||
694 | if ( !::DrawFrameControl(hdcMem, &rect, | |
695 | DFC_BUTTON, | |
696 | DFCS_BUTTONCHECK) ) | |
697 | { | |
223d09f6 | 698 | wxLogLastError(wxT("DrawFrameControl(uncheck)")); |
9dfbf520 VZ |
699 | } |
700 | ||
701 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); | |
702 | imagelistCheckboxes.Add(bmp); | |
703 | ||
704 | // clean up | |
705 | ::DeleteDC(hdcMem); | |
706 | ||
707 | // set the imagelist | |
708 | SetStateImageList(&imagelistCheckboxes); | |
709 | } | |
710 | #endif // 0 | |
711 | ||
712 | SetSize(pos.x, pos.y, size.x, size.y); | |
2bda0e17 | 713 | |
04cd30de | 714 | return true; |
2bda0e17 KB |
715 | } |
716 | ||
08b7c251 | 717 | wxTreeCtrl::~wxTreeCtrl() |
2bda0e17 | 718 | { |
696e1ea0 VZ |
719 | // delete any attributes |
720 | if ( m_hasAnyAttr ) | |
721 | { | |
ee4b2721 | 722 | WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs); |
696e1ea0 VZ |
723 | |
724 | // prevent TVN_DELETEITEM handler from deleting the attributes again! | |
04cd30de | 725 | m_hasAnyAttr = false; |
696e1ea0 VZ |
726 | } |
727 | ||
08b7c251 | 728 | DeleteTextCtrl(); |
2bda0e17 | 729 | |
08b7c251 | 730 | // delete user data to prevent memory leaks |
a9c1265f | 731 | // also deletes hidden root node storage. |
08b7c251 | 732 | DeleteAllItems(); |
33ac7e6f | 733 | |
0377efa2 VS |
734 | if (m_ownsImageListNormal) delete m_imageListNormal; |
735 | if (m_ownsImageListState) delete m_imageListState; | |
2bda0e17 KB |
736 | } |
737 | ||
08b7c251 VZ |
738 | // ---------------------------------------------------------------------------- |
739 | // accessors | |
740 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 741 | |
08b7c251 | 742 | // simple wrappers which add error checking in debug mode |
2bda0e17 | 743 | |
08b7c251 | 744 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const |
2bda0e17 | 745 | { |
04cd30de | 746 | wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false, |
a9c1265f VZ |
747 | _T("can't retrieve virtual root item") ); |
748 | ||
d220ae32 | 749 | if ( !TreeView_GetItem(GetHwnd(), tvItem) ) |
2bda0e17 | 750 | { |
f6bcfd97 | 751 | wxLogLastError(wxT("TreeView_GetItem")); |
08b7c251 | 752 | |
04cd30de | 753 | return false; |
08b7c251 VZ |
754 | } |
755 | ||
04cd30de | 756 | return true; |
2bda0e17 KB |
757 | } |
758 | ||
08b7c251 | 759 | void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem) |
2bda0e17 | 760 | { |
d220ae32 | 761 | if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 ) |
2bda0e17 | 762 | { |
f6bcfd97 | 763 | wxLogLastError(wxT("TreeView_SetItem")); |
08b7c251 | 764 | } |
2bda0e17 KB |
765 | } |
766 | ||
08b7c251 | 767 | size_t wxTreeCtrl::GetCount() const |
2bda0e17 | 768 | { |
d220ae32 | 769 | return (size_t)TreeView_GetCount(GetHwnd()); |
2bda0e17 KB |
770 | } |
771 | ||
08b7c251 | 772 | unsigned int wxTreeCtrl::GetIndent() const |
2bda0e17 | 773 | { |
d220ae32 | 774 | return TreeView_GetIndent(GetHwnd()); |
2bda0e17 KB |
775 | } |
776 | ||
08b7c251 | 777 | void wxTreeCtrl::SetIndent(unsigned int indent) |
2bda0e17 | 778 | { |
d220ae32 | 779 | TreeView_SetIndent(GetHwnd(), indent); |
2bda0e17 KB |
780 | } |
781 | ||
08b7c251 | 782 | wxImageList *wxTreeCtrl::GetImageList() const |
2bda0e17 | 783 | { |
08b7c251 | 784 | return m_imageListNormal; |
2bda0e17 KB |
785 | } |
786 | ||
08b7c251 | 787 | wxImageList *wxTreeCtrl::GetStateImageList() const |
2bda0e17 | 788 | { |
8a000b6b | 789 | return m_imageListState; |
2bda0e17 KB |
790 | } |
791 | ||
08b7c251 | 792 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) |
2bda0e17 | 793 | { |
08b7c251 | 794 | // no error return |
d220ae32 | 795 | TreeView_SetImageList(GetHwnd(), |
08b7c251 VZ |
796 | imageList ? imageList->GetHIMAGELIST() : 0, |
797 | which); | |
2bda0e17 KB |
798 | } |
799 | ||
08b7c251 | 800 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
2bda0e17 | 801 | { |
a9c1265f VZ |
802 | if (m_ownsImageListNormal) |
803 | delete m_imageListNormal; | |
804 | ||
08b7c251 | 805 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); |
04cd30de | 806 | m_ownsImageListNormal = false; |
2bda0e17 KB |
807 | } |
808 | ||
08b7c251 | 809 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
2bda0e17 | 810 | { |
0377efa2 | 811 | if (m_ownsImageListState) delete m_imageListState; |
08b7c251 | 812 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); |
04cd30de | 813 | m_ownsImageListState = false; |
0377efa2 VS |
814 | } |
815 | ||
816 | void wxTreeCtrl::AssignImageList(wxImageList *imageList) | |
817 | { | |
818 | SetImageList(imageList); | |
04cd30de | 819 | m_ownsImageListNormal = true; |
0377efa2 VS |
820 | } |
821 | ||
822 | void wxTreeCtrl::AssignStateImageList(wxImageList *imageList) | |
823 | { | |
824 | SetStateImageList(imageList); | |
04cd30de | 825 | m_ownsImageListState = true; |
2bda0e17 KB |
826 | } |
827 | ||
33961d59 RR |
828 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, |
829 | bool recursively) const | |
830 | { | |
831 | TraverseCounter counter(this, item, recursively); | |
23fd5130 | 832 | |
73974df1 | 833 | return counter.GetCount() - 1; |
23fd5130 VZ |
834 | } |
835 | ||
bb448552 VZ |
836 | // ---------------------------------------------------------------------------- |
837 | // control colours | |
838 | // ---------------------------------------------------------------------------- | |
839 | ||
840 | bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour) | |
841 | { | |
f6bcfd97 | 842 | #if !wxUSE_COMCTL32_SAFELY |
bb448552 | 843 | if ( !wxWindowBase::SetBackgroundColour(colour) ) |
04cd30de | 844 | return false; |
bb448552 VZ |
845 | |
846 | SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel()); | |
f6bcfd97 | 847 | #endif |
bb448552 | 848 | |
04cd30de | 849 | return true; |
bb448552 VZ |
850 | } |
851 | ||
852 | bool wxTreeCtrl::SetForegroundColour(const wxColour &colour) | |
853 | { | |
f6bcfd97 | 854 | #if !wxUSE_COMCTL32_SAFELY |
bb448552 | 855 | if ( !wxWindowBase::SetForegroundColour(colour) ) |
04cd30de | 856 | return false; |
bb448552 VZ |
857 | |
858 | SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel()); | |
f6bcfd97 | 859 | #endif |
bb448552 | 860 | |
04cd30de | 861 | return true; |
bb448552 VZ |
862 | } |
863 | ||
08b7c251 VZ |
864 | // ---------------------------------------------------------------------------- |
865 | // Item access | |
866 | // ---------------------------------------------------------------------------- | |
867 | ||
868 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const | |
2bda0e17 | 869 | { |
837e5743 | 870 | wxChar buf[512]; // the size is arbitrary... |
02ce7b72 | 871 | |
08b7c251 VZ |
872 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
873 | tvItem.pszText = buf; | |
874 | tvItem.cchTextMax = WXSIZEOF(buf); | |
875 | if ( !DoGetItem(&tvItem) ) | |
876 | { | |
877 | // don't return some garbage which was on stack, but an empty string | |
223d09f6 | 878 | buf[0] = wxT('\0'); |
08b7c251 | 879 | } |
2bda0e17 | 880 | |
08b7c251 VZ |
881 | return wxString(buf); |
882 | } | |
2bda0e17 | 883 | |
08b7c251 VZ |
884 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
885 | { | |
a9c1265f VZ |
886 | if ( IS_VIRTUAL_ROOT(item) ) |
887 | return; | |
888 | ||
08b7c251 | 889 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
837e5743 | 890 | tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok |
08b7c251 | 891 | DoSetItem(&tvItem); |
44fbc477 VZ |
892 | |
893 | // when setting the text of the item being edited, the text control should | |
894 | // be updated to reflect the new text as well, otherwise calling | |
895 | // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect | |
896 | // | |
897 | // don't use GetEditControl() here because m_textCtrl is not set yet | |
898 | HWND hwndEdit = TreeView_GetEditControl(GetHwnd()); | |
899 | if ( hwndEdit ) | |
900 | { | |
901 | if ( item == GetSelection() ) | |
902 | { | |
903 | ::SetWindowText(hwndEdit, text); | |
904 | } | |
905 | } | |
08b7c251 | 906 | } |
2bda0e17 | 907 | |
74b31181 VZ |
908 | int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item, |
909 | wxTreeItemIcon which) const | |
910 | { | |
911 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
912 | if ( !DoGetItem(&tvItem) ) | |
913 | { | |
914 | return -1; | |
915 | } | |
916 | ||
917 | return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which); | |
918 | } | |
919 | ||
920 | void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item, | |
921 | int image, | |
922 | wxTreeItemIcon which) const | |
923 | { | |
924 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
925 | if ( !DoGetItem(&tvItem) ) | |
926 | { | |
927 | return; | |
928 | } | |
929 | ||
930 | wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam); | |
931 | ||
932 | data->SetImage(image, which); | |
933 | ||
934 | // make sure that we have selected images as well | |
935 | if ( which == wxTreeItemIcon_Normal && | |
936 | !data->HasImage(wxTreeItemIcon_Selected) ) | |
937 | { | |
938 | data->SetImage(image, wxTreeItemIcon_Selected); | |
939 | } | |
940 | ||
941 | if ( which == wxTreeItemIcon_Expanded && | |
942 | !data->HasImage(wxTreeItemIcon_SelectedExpanded) ) | |
943 | { | |
944 | data->SetImage(image, wxTreeItemIcon_SelectedExpanded); | |
945 | } | |
946 | } | |
947 | ||
9dfbf520 VZ |
948 | void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item, |
949 | int image, | |
950 | int imageSel) | |
951 | { | |
952 | wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE); | |
953 | tvItem.iSelectedImage = imageSel; | |
954 | tvItem.iImage = image; | |
955 | DoSetItem(&tvItem); | |
956 | } | |
957 | ||
74b31181 VZ |
958 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item, |
959 | wxTreeItemIcon which) const | |
08b7c251 | 960 | { |
a9c1265f VZ |
961 | if ( (HITEM(item) == TVI_ROOT) && (m_windowStyle & wxTR_HIDE_ROOT) ) |
962 | { | |
963 | // TODO: Maybe a hidden root can still provide images? | |
964 | return -1; | |
965 | } | |
966 | ||
74b31181 VZ |
967 | if ( HasIndirectData(item) ) |
968 | { | |
969 | return DoGetItemImageFromData(item, which); | |
970 | } | |
2bda0e17 | 971 | |
74b31181 VZ |
972 | UINT mask; |
973 | switch ( which ) | |
974 | { | |
975 | default: | |
223d09f6 | 976 | wxFAIL_MSG( wxT("unknown tree item image type") ); |
2bda0e17 | 977 | |
74b31181 VZ |
978 | case wxTreeItemIcon_Normal: |
979 | mask = TVIF_IMAGE; | |
980 | break; | |
2bda0e17 | 981 | |
74b31181 VZ |
982 | case wxTreeItemIcon_Selected: |
983 | mask = TVIF_SELECTEDIMAGE; | |
984 | break; | |
985 | ||
986 | case wxTreeItemIcon_Expanded: | |
987 | case wxTreeItemIcon_SelectedExpanded: | |
988 | return -1; | |
989 | } | |
990 | ||
991 | wxTreeViewItem tvItem(item, mask); | |
08b7c251 | 992 | DoGetItem(&tvItem); |
2bda0e17 | 993 | |
74b31181 | 994 | return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage; |
2bda0e17 KB |
995 | } |
996 | ||
74b31181 VZ |
997 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image, |
998 | wxTreeItemIcon which) | |
2bda0e17 | 999 | { |
a9c1265f VZ |
1000 | if ( IS_VIRTUAL_ROOT(item) ) |
1001 | { | |
1002 | // TODO: Maybe a hidden root can still store images? | |
1003 | return; | |
1004 | } | |
1005 | ||
7ab0c3ad VZ |
1006 | int imageNormal, |
1007 | imageSel; | |
1008 | ||
74b31181 VZ |
1009 | switch ( which ) |
1010 | { | |
1011 | default: | |
223d09f6 | 1012 | wxFAIL_MSG( wxT("unknown tree item image type") ); |
7ab0c3ad | 1013 | // fall through |
74b31181 VZ |
1014 | |
1015 | case wxTreeItemIcon_Normal: | |
7ab0c3ad VZ |
1016 | { |
1017 | const int imageNormalOld = GetItemImage(item); | |
b5f6b52a MB |
1018 | const int imageSelOld = |
1019 | GetItemImage(item, wxTreeItemIcon_Selected); | |
7ab0c3ad VZ |
1020 | |
1021 | // always set the normal image | |
1022 | imageNormal = image; | |
1023 | ||
1024 | // if the selected and normal images were the same, they should | |
1025 | // be the same after the update, otherwise leave the selected | |
1026 | // image as it was | |
1027 | imageSel = imageNormalOld == imageSelOld ? image : imageSelOld; | |
1028 | } | |
74b31181 VZ |
1029 | break; |
1030 | ||
1031 | case wxTreeItemIcon_Selected: | |
1032 | imageNormal = GetItemImage(item); | |
1033 | imageSel = image; | |
1034 | break; | |
1035 | ||
1036 | case wxTreeItemIcon_Expanded: | |
1037 | case wxTreeItemIcon_SelectedExpanded: | |
1038 | if ( !HasIndirectData(item) ) | |
1039 | { | |
1040 | // we need to get the old images first, because after we create | |
1041 | // the wxTreeItemIndirectData GetItemXXXImage() will use it to | |
1042 | // get the images | |
1043 | imageNormal = GetItemImage(item); | |
b5f6b52a | 1044 | imageSel = GetItemImage(item, wxTreeItemIcon_Selected); |
74b31181 VZ |
1045 | |
1046 | // if it doesn't have it yet, add it | |
1047 | wxTreeItemIndirectData *data = new | |
1048 | wxTreeItemIndirectData(this, item); | |
1049 | ||
1050 | // copy the data to the new location | |
1051 | data->SetImage(imageNormal, wxTreeItemIcon_Normal); | |
1052 | data->SetImage(imageSel, wxTreeItemIcon_Selected); | |
1053 | } | |
1054 | ||
1055 | DoSetItemImageFromData(item, image, which); | |
1056 | ||
1057 | // reset the normal/selected images because we won't use them any | |
1058 | // more - now they're stored inside the indirect data | |
1059 | imageNormal = | |
1060 | imageSel = I_IMAGECALLBACK; | |
1061 | break; | |
1062 | } | |
1063 | ||
9dfbf520 VZ |
1064 | // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always |
1065 | // change both normal and selected image - otherwise the change simply | |
1066 | // doesn't take place! | |
74b31181 | 1067 | DoSetItemImages(item, imageNormal, imageSel); |
2bda0e17 KB |
1068 | } |
1069 | ||
08b7c251 | 1070 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
2bda0e17 | 1071 | { |
08b7c251 | 1072 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
a9c1265f VZ |
1073 | |
1074 | // Hidden root may have data. | |
1075 | if ( IS_VIRTUAL_ROOT(item) ) | |
1076 | { | |
1077 | return GET_VIRTUAL_ROOT()->GetData(); | |
1078 | } | |
1079 | ||
1080 | // Visible node. | |
08b7c251 VZ |
1081 | if ( !DoGetItem(&tvItem) ) |
1082 | { | |
1083 | return NULL; | |
1084 | } | |
2bda0e17 | 1085 | |
502a2b18 VZ |
1086 | wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam; |
1087 | if ( IsDataIndirect(data) ) | |
74b31181 | 1088 | { |
502a2b18 | 1089 | data = ((wxTreeItemIndirectData *)data)->GetData(); |
74b31181 | 1090 | } |
502a2b18 VZ |
1091 | |
1092 | return data; | |
2bda0e17 KB |
1093 | } |
1094 | ||
08b7c251 | 1095 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
2bda0e17 | 1096 | { |
a9c1265f VZ |
1097 | if ( IS_VIRTUAL_ROOT(item) ) |
1098 | { | |
1099 | GET_VIRTUAL_ROOT()->SetData(data); | |
1100 | } | |
1101 | ||
188781db VZ |
1102 | // first, associate this piece of data with this item |
1103 | if ( data ) | |
1104 | { | |
1105 | data->SetId(item); | |
1106 | } | |
1107 | ||
08b7c251 | 1108 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
74b31181 VZ |
1109 | |
1110 | if ( HasIndirectData(item) ) | |
1111 | { | |
1112 | if ( DoGetItem(&tvItem) ) | |
1113 | { | |
1114 | ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data); | |
1115 | } | |
1116 | else | |
1117 | { | |
223d09f6 | 1118 | wxFAIL_MSG( wxT("failed to change tree items data") ); |
74b31181 VZ |
1119 | } |
1120 | } | |
1121 | else | |
1122 | { | |
1123 | tvItem.lParam = (LPARAM)data; | |
1124 | DoSetItem(&tvItem); | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item, | |
1129 | wxTreeItemIndirectData *data) | |
1130 | { | |
1131 | // this should never happen because it's unnecessary and will probably lead | |
1132 | // to crash too because the code elsewhere supposes that the pointer the | |
1133 | // wxTreeItemIndirectData has is a real wxItemData and not | |
1134 | // wxTreeItemIndirectData as well | |
223d09f6 | 1135 | wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") ); |
74b31181 | 1136 | |
502a2b18 | 1137 | SetItemData(item, data); |
74b31181 VZ |
1138 | } |
1139 | ||
1140 | bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const | |
1141 | { | |
502a2b18 VZ |
1142 | // query the item itself |
1143 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
1144 | if ( !DoGetItem(&tvItem) ) | |
1145 | { | |
04cd30de | 1146 | return false; |
502a2b18 VZ |
1147 | } |
1148 | ||
1149 | wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam; | |
1150 | ||
1151 | return data && IsDataIndirect(data); | |
08b7c251 | 1152 | } |
2bda0e17 | 1153 | |
3a5a2f56 VZ |
1154 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
1155 | { | |
a9c1265f VZ |
1156 | if ( IS_VIRTUAL_ROOT(item) ) |
1157 | return; | |
1158 | ||
3a5a2f56 VZ |
1159 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
1160 | tvItem.cChildren = (int)has; | |
1161 | DoSetItem(&tvItem); | |
1162 | } | |
1163 | ||
add28c55 VZ |
1164 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
1165 | { | |
a9c1265f VZ |
1166 | if ( IS_VIRTUAL_ROOT(item) ) |
1167 | return; | |
1168 | ||
add28c55 VZ |
1169 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); |
1170 | tvItem.state = bold ? TVIS_BOLD : 0; | |
1171 | DoSetItem(&tvItem); | |
1172 | } | |
1173 | ||
58a8ab88 JS |
1174 | void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight) |
1175 | { | |
a9c1265f VZ |
1176 | if ( IS_VIRTUAL_ROOT(item) ) |
1177 | return; | |
1178 | ||
58a8ab88 JS |
1179 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED); |
1180 | tvItem.state = highlight ? TVIS_DROPHILITED : 0; | |
1181 | DoSetItem(&tvItem); | |
1182 | } | |
1183 | ||
d00407b2 VZ |
1184 | void wxTreeCtrl::RefreshItem(const wxTreeItemId& item) |
1185 | { | |
a9c1265f VZ |
1186 | if ( IS_VIRTUAL_ROOT(item) ) |
1187 | return; | |
1188 | ||
d00407b2 VZ |
1189 | wxRect rect; |
1190 | if ( GetBoundingRect(item, rect) ) | |
1191 | { | |
1192 | RefreshRect(rect); | |
1193 | } | |
1194 | } | |
1195 | ||
2b5f62a0 VZ |
1196 | wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const |
1197 | { | |
f888d614 | 1198 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
2b5f62a0 | 1199 | |
ee4b2721 | 1200 | return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour(); |
2b5f62a0 VZ |
1201 | } |
1202 | ||
1203 | wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const | |
1204 | { | |
f888d614 | 1205 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
2b5f62a0 | 1206 | |
ee4b2721 | 1207 | return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour(); |
2b5f62a0 VZ |
1208 | } |
1209 | ||
1210 | wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const | |
1211 | { | |
f888d614 | 1212 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
2b5f62a0 | 1213 | |
ee4b2721 | 1214 | return it == m_attrs.end() ? wxNullFont : it->second->GetFont(); |
2b5f62a0 VZ |
1215 | } |
1216 | ||
696e1ea0 VZ |
1217 | void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item, |
1218 | const wxColour& col) | |
1219 | { | |
ee4b2721 | 1220 | wxTreeItemAttr *attr; |
f888d614 | 1221 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1222 | if ( it == m_attrs.end() ) |
696e1ea0 | 1223 | { |
ee4b2721 VZ |
1224 | m_hasAnyAttr = true; |
1225 | ||
f888d614 | 1226 | m_attrs[item.m_pItem] = |
696e1ea0 | 1227 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1228 | } |
1229 | else | |
1230 | { | |
1231 | attr = it->second; | |
696e1ea0 VZ |
1232 | } |
1233 | ||
1234 | attr->SetTextColour(col); | |
d00407b2 VZ |
1235 | |
1236 | RefreshItem(item); | |
696e1ea0 VZ |
1237 | } |
1238 | ||
1239 | void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, | |
1240 | const wxColour& col) | |
1241 | { | |
ee4b2721 | 1242 | wxTreeItemAttr *attr; |
f888d614 | 1243 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1244 | if ( it == m_attrs.end() ) |
696e1ea0 | 1245 | { |
ee4b2721 VZ |
1246 | m_hasAnyAttr = true; |
1247 | ||
f888d614 | 1248 | m_attrs[item.m_pItem] = |
696e1ea0 | 1249 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1250 | } |
1251 | else // already in the hash | |
1252 | { | |
1253 | attr = it->second; | |
696e1ea0 VZ |
1254 | } |
1255 | ||
1256 | attr->SetBackgroundColour(col); | |
d00407b2 VZ |
1257 | |
1258 | RefreshItem(item); | |
696e1ea0 VZ |
1259 | } |
1260 | ||
1261 | void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) | |
1262 | { | |
ee4b2721 | 1263 | wxTreeItemAttr *attr; |
f888d614 | 1264 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1265 | if ( it == m_attrs.end() ) |
696e1ea0 | 1266 | { |
ee4b2721 VZ |
1267 | m_hasAnyAttr = true; |
1268 | ||
f888d614 | 1269 | m_attrs[item.m_pItem] = |
696e1ea0 | 1270 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1271 | } |
1272 | else // already in the hash | |
1273 | { | |
1274 | attr = it->second; | |
696e1ea0 VZ |
1275 | } |
1276 | ||
1277 | attr->SetFont(font); | |
d00407b2 VZ |
1278 | |
1279 | RefreshItem(item); | |
696e1ea0 VZ |
1280 | } |
1281 | ||
08b7c251 VZ |
1282 | // ---------------------------------------------------------------------------- |
1283 | // Item status | |
1284 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1285 | |
08b7c251 VZ |
1286 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const |
1287 | { | |
2b5f62a0 VZ |
1288 | if ( item == wxTreeItemId(TVI_ROOT) ) |
1289 | { | |
1290 | // virtual (hidden) root is never visible | |
04cd30de | 1291 | return false; |
2b5f62a0 VZ |
1292 | } |
1293 | ||
add28c55 | 1294 | // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect |
08b7c251 | 1295 | RECT rect; |
955be36c VZ |
1296 | |
1297 | // this ugliness comes directly from MSDN - it *is* the correct way to pass | |
1298 | // the HTREEITEM with TVM_GETITEMRECT | |
ee4b2721 | 1299 | *(HTREEITEM *)&rect = HITEM(item); |
955be36c | 1300 | |
04cd30de RL |
1301 | // false means get item rect for the whole item, not only text |
1302 | return SendMessage(GetHwnd(), TVM_GETITEMRECT, false, (LPARAM)&rect) != 0; | |
2bda0e17 KB |
1303 | } |
1304 | ||
08b7c251 | 1305 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
2bda0e17 | 1306 | { |
08b7c251 VZ |
1307 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
1308 | DoGetItem(&tvItem); | |
2bda0e17 | 1309 | |
08b7c251 | 1310 | return tvItem.cChildren != 0; |
2bda0e17 KB |
1311 | } |
1312 | ||
08b7c251 | 1313 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
2bda0e17 | 1314 | { |
08b7c251 VZ |
1315 | // probably not a good idea to put it here |
1316 | //wxASSERT( ItemHasChildren(item) ); | |
2bda0e17 | 1317 | |
08b7c251 VZ |
1318 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); |
1319 | DoGetItem(&tvItem); | |
2bda0e17 | 1320 | |
08b7c251 | 1321 | return (tvItem.state & TVIS_EXPANDED) != 0; |
2bda0e17 KB |
1322 | } |
1323 | ||
08b7c251 | 1324 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
2bda0e17 | 1325 | { |
08b7c251 VZ |
1326 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); |
1327 | DoGetItem(&tvItem); | |
2bda0e17 | 1328 | |
08b7c251 | 1329 | return (tvItem.state & TVIS_SELECTED) != 0; |
2bda0e17 KB |
1330 | } |
1331 | ||
add28c55 VZ |
1332 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const |
1333 | { | |
1334 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
1335 | DoGetItem(&tvItem); | |
1336 | ||
1337 | return (tvItem.state & TVIS_BOLD) != 0; | |
1338 | } | |
1339 | ||
08b7c251 VZ |
1340 | // ---------------------------------------------------------------------------- |
1341 | // navigation | |
1342 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1343 | |
08b7c251 VZ |
1344 | wxTreeItemId wxTreeCtrl::GetRootItem() const |
1345 | { | |
a9c1265f VZ |
1346 | // Root may be real (visible) or virtual (hidden). |
1347 | if ( GET_VIRTUAL_ROOT() ) | |
1348 | return TVI_ROOT; | |
1349 | ||
ee4b2721 | 1350 | return wxTreeItemId(TreeView_GetRoot(GetHwnd())); |
08b7c251 | 1351 | } |
2bda0e17 | 1352 | |
08b7c251 VZ |
1353 | wxTreeItemId wxTreeCtrl::GetSelection() const |
1354 | { | |
f888d614 | 1355 | wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxTreeItemId(), |
223d09f6 | 1356 | wxT("this only works with single selection controls") ); |
9dfbf520 | 1357 | |
ee4b2721 | 1358 | return wxTreeItemId(TreeView_GetSelection(GetHwnd())); |
2bda0e17 KB |
1359 | } |
1360 | ||
99006e44 | 1361 | wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const |
2bda0e17 | 1362 | { |
c9b142c9 VZ |
1363 | HTREEITEM hItem; |
1364 | ||
1365 | if ( IS_VIRTUAL_ROOT(item) ) | |
1366 | { | |
1367 | // no parent for the virtual root | |
1368 | hItem = 0; | |
1369 | } | |
1370 | else // normal item | |
a9c1265f | 1371 | { |
c9b142c9 VZ |
1372 | hItem = TreeView_GetParent(GetHwnd(), HITEM(item)); |
1373 | if ( !hItem && HasFlag(wxTR_HIDE_ROOT) ) | |
1374 | { | |
1375 | // the top level items should have the virtual root as their parent | |
1376 | hItem = TVI_ROOT; | |
1377 | } | |
a9c1265f VZ |
1378 | } |
1379 | ||
ee4b2721 | 1380 | return wxTreeItemId(hItem); |
08b7c251 | 1381 | } |
2bda0e17 | 1382 | |
08b7c251 | 1383 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
ee4b2721 | 1384 | wxTreeItemIdValue& cookie) const |
08b7c251 VZ |
1385 | { |
1386 | // remember the last child returned in 'cookie' | |
ee4b2721 VZ |
1387 | cookie = TreeView_GetChild(GetHwnd(), HITEM(item)); |
1388 | ||
1389 | return wxTreeItemId(cookie); | |
1390 | } | |
1391 | ||
1392 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), | |
1393 | wxTreeItemIdValue& cookie) const | |
1394 | { | |
1395 | wxTreeItemId item(TreeView_GetNextSibling(GetHwnd(), | |
1396 | HITEM(wxTreeItemId(cookie)))); | |
1397 | cookie = item.m_pItem; | |
1398 | ||
1399 | return item; | |
1400 | } | |
2bda0e17 | 1401 | |
ee4b2721 VZ |
1402 | #if WXWIN_COMPATIBILITY_2_4 |
1403 | ||
1404 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, | |
1405 | long& cookie) const | |
1406 | { | |
1407 | cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item)); | |
1408 | ||
1409 | return wxTreeItemId((void *)cookie); | |
2bda0e17 KB |
1410 | } |
1411 | ||
08b7c251 | 1412 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), |
ee4b2721 | 1413 | long& cookie) const |
2bda0e17 | 1414 | { |
ee4b2721 VZ |
1415 | wxTreeItemId item(TreeView_GetNextSibling |
1416 | ( | |
1417 | GetHwnd(), | |
1418 | HITEM(wxTreeItemId((void *)cookie) | |
1419 | ))); | |
1420 | cookie = (long)item.m_pItem; | |
23fd5130 | 1421 | |
ee4b2721 | 1422 | return item; |
08b7c251 | 1423 | } |
2bda0e17 | 1424 | |
ee4b2721 VZ |
1425 | #endif // WXWIN_COMPATIBILITY_2_4 |
1426 | ||
978f38c2 VZ |
1427 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
1428 | { | |
1429 | // can this be done more efficiently? | |
ee4b2721 | 1430 | wxTreeItemIdValue cookie; |
978f38c2 VZ |
1431 | |
1432 | wxTreeItemId childLast, | |
2165ad93 | 1433 | child = GetFirstChild(item, cookie); |
978f38c2 VZ |
1434 | while ( child.IsOk() ) |
1435 | { | |
1436 | childLast = child; | |
2165ad93 | 1437 | child = GetNextChild(item, cookie); |
978f38c2 VZ |
1438 | } |
1439 | ||
1440 | return childLast; | |
1441 | } | |
1442 | ||
08b7c251 VZ |
1443 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
1444 | { | |
ee4b2721 | 1445 | return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item))); |
2bda0e17 KB |
1446 | } |
1447 | ||
08b7c251 | 1448 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
2bda0e17 | 1449 | { |
ee4b2721 | 1450 | return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item))); |
2bda0e17 KB |
1451 | } |
1452 | ||
08b7c251 | 1453 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
2bda0e17 | 1454 | { |
ee4b2721 | 1455 | return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd())); |
2bda0e17 KB |
1456 | } |
1457 | ||
08b7c251 | 1458 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
2bda0e17 | 1459 | { |
f6bcfd97 | 1460 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!")); |
02ce7b72 | 1461 | |
ee4b2721 | 1462 | return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item))); |
08b7c251 | 1463 | } |
02ce7b72 | 1464 | |
08b7c251 VZ |
1465 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
1466 | { | |
f6bcfd97 | 1467 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!")); |
02ce7b72 | 1468 | |
ee4b2721 | 1469 | return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item))); |
08b7c251 | 1470 | } |
02ce7b72 | 1471 | |
9dfbf520 VZ |
1472 | // ---------------------------------------------------------------------------- |
1473 | // multiple selections emulation | |
1474 | // ---------------------------------------------------------------------------- | |
1475 | ||
1476 | bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const | |
1477 | { | |
1478 | // receive the desired information. | |
1479 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
1480 | DoGetItem(&tvItem); | |
1481 | ||
1482 | // state image indices are 1 based | |
1483 | return ((tvItem.state >> 12) - 1) == 1; | |
1484 | } | |
1485 | ||
1486 | void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check) | |
1487 | { | |
1488 | // receive the desired information. | |
1489 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
1490 | ||
a9c1265f VZ |
1491 | DoGetItem(&tvItem); |
1492 | ||
9dfbf520 VZ |
1493 | // state images are one-based |
1494 | tvItem.state = (check ? 2 : 1) << 12; | |
1495 | ||
1496 | DoSetItem(&tvItem); | |
1497 | } | |
1498 | ||
33961d59 RR |
1499 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const |
1500 | { | |
1501 | TraverseSelections selector(this, selections); | |
9dfbf520 | 1502 | |
e47c4d48 | 1503 | return selector.GetCount(); |
9dfbf520 VZ |
1504 | } |
1505 | ||
08b7c251 VZ |
1506 | // ---------------------------------------------------------------------------- |
1507 | // Usual operations | |
1508 | // ---------------------------------------------------------------------------- | |
02ce7b72 | 1509 | |
08b7c251 VZ |
1510 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, |
1511 | wxTreeItemId hInsertAfter, | |
1512 | const wxString& text, | |
1513 | int image, int selectedImage, | |
1514 | wxTreeItemData *data) | |
1515 | { | |
0e935169 VZ |
1516 | wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()), |
1517 | wxTreeItemId(), | |
1518 | _T("can't have more than one root in the tree") ); | |
1519 | ||
08b7c251 | 1520 | TV_INSERTSTRUCT tvIns; |
3f7bc32b VZ |
1521 | tvIns.hParent = HITEM(parent); |
1522 | tvIns.hInsertAfter = HITEM(hInsertAfter); | |
58a8ab88 | 1523 | |
74b31181 VZ |
1524 | // this is how we insert the item as the first child: supply a NULL |
1525 | // hInsertAfter | |
1526 | if ( !tvIns.hInsertAfter ) | |
58a8ab88 JS |
1527 | { |
1528 | tvIns.hInsertAfter = TVI_FIRST; | |
1529 | } | |
1530 | ||
08b7c251 VZ |
1531 | UINT mask = 0; |
1532 | if ( !text.IsEmpty() ) | |
1533 | { | |
1534 | mask |= TVIF_TEXT; | |
837e5743 | 1535 | tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok |
08b7c251 | 1536 | } |
f6bcfd97 BP |
1537 | else |
1538 | { | |
1539 | tvIns.item.pszText = NULL; | |
1540 | tvIns.item.cchTextMax = 0; | |
1541 | } | |
02ce7b72 | 1542 | |
08b7c251 VZ |
1543 | if ( image != -1 ) |
1544 | { | |
1545 | mask |= TVIF_IMAGE; | |
1546 | tvIns.item.iImage = image; | |
3a5a2f56 | 1547 | |
6b037754 | 1548 | if ( selectedImage == -1 ) |
3a5a2f56 VZ |
1549 | { |
1550 | // take the same image for selected icon if not specified | |
1551 | selectedImage = image; | |
1552 | } | |
08b7c251 | 1553 | } |
02ce7b72 | 1554 | |
08b7c251 VZ |
1555 | if ( selectedImage != -1 ) |
1556 | { | |
1557 | mask |= TVIF_SELECTEDIMAGE; | |
1558 | tvIns.item.iSelectedImage = selectedImage; | |
1559 | } | |
02ce7b72 | 1560 | |
08b7c251 VZ |
1561 | if ( data != NULL ) |
1562 | { | |
1563 | mask |= TVIF_PARAM; | |
1564 | tvIns.item.lParam = (LPARAM)data; | |
1565 | } | |
02ce7b72 | 1566 | |
08b7c251 | 1567 | tvIns.item.mask = mask; |
02ce7b72 | 1568 | |
d220ae32 | 1569 | HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns); |
08b7c251 VZ |
1570 | if ( id == 0 ) |
1571 | { | |
f6bcfd97 | 1572 | wxLogLastError(wxT("TreeView_InsertItem")); |
08b7c251 | 1573 | } |
02ce7b72 | 1574 | |
fd3f686c VZ |
1575 | if ( data != NULL ) |
1576 | { | |
1577 | // associate the application tree item with Win32 tree item handle | |
ee4b2721 | 1578 | data->SetId(id); |
fd3f686c VZ |
1579 | } |
1580 | ||
ee4b2721 | 1581 | return wxTreeItemId(id); |
2bda0e17 KB |
1582 | } |
1583 | ||
08b7c251 | 1584 | // for compatibility only |
ee4b2721 VZ |
1585 | #if WXWIN_COMPATIBILITY_2_4 |
1586 | ||
08b7c251 VZ |
1587 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
1588 | const wxString& text, | |
1589 | int image, int selImage, | |
1590 | long insertAfter) | |
2bda0e17 | 1591 | { |
ee4b2721 | 1592 | return DoInsertItem(parent, wxTreeItemId((void *)insertAfter), text, |
08b7c251 | 1593 | image, selImage, NULL); |
2bda0e17 KB |
1594 | } |
1595 | ||
ee4b2721 VZ |
1596 | #endif // WXWIN_COMPATIBILITY_2_4 |
1597 | ||
08b7c251 VZ |
1598 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
1599 | int image, int selectedImage, | |
1600 | wxTreeItemData *data) | |
2bda0e17 | 1601 | { |
a9c1265f VZ |
1602 | |
1603 | if ( m_windowStyle & wxTR_HIDE_ROOT ) | |
1604 | { | |
1605 | // create a virtual root item, the parent for all the others | |
1606 | m_pVirtualRoot = new wxVirtualNode(data); | |
1607 | ||
1608 | return TVI_ROOT; | |
1609 | } | |
1610 | ||
f888d614 | 1611 | return DoInsertItem(wxTreeItemId(), wxTreeItemId(), |
08b7c251 | 1612 | text, image, selectedImage, data); |
2bda0e17 KB |
1613 | } |
1614 | ||
08b7c251 VZ |
1615 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, |
1616 | const wxString& text, | |
1617 | int image, int selectedImage, | |
1618 | wxTreeItemData *data) | |
2bda0e17 | 1619 | { |
ee4b2721 | 1620 | return DoInsertItem(parent, TVI_FIRST, |
08b7c251 | 1621 | text, image, selectedImage, data); |
2bda0e17 KB |
1622 | } |
1623 | ||
08b7c251 VZ |
1624 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
1625 | const wxTreeItemId& idPrevious, | |
1626 | const wxString& text, | |
1627 | int image, int selectedImage, | |
1628 | wxTreeItemData *data) | |
2bda0e17 | 1629 | { |
08b7c251 | 1630 | return DoInsertItem(parent, idPrevious, text, image, selectedImage, data); |
2bda0e17 KB |
1631 | } |
1632 | ||
2ef31e80 VZ |
1633 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
1634 | size_t index, | |
1635 | const wxString& text, | |
1636 | int image, int selectedImage, | |
1637 | wxTreeItemData *data) | |
1638 | { | |
1639 | // find the item from index | |
ee4b2721 | 1640 | wxTreeItemIdValue cookie; |
2ef31e80 VZ |
1641 | wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie); |
1642 | while ( index != 0 && idCur.IsOk() ) | |
1643 | { | |
1644 | index--; | |
1645 | ||
1646 | idPrev = idCur; | |
1647 | idCur = GetNextChild(parent, cookie); | |
1648 | } | |
1649 | ||
1650 | // assert, not check: if the index is invalid, we will append the item | |
1651 | // to the end | |
1652 | wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") ); | |
1653 | ||
1654 | return DoInsertItem(parent, idPrev, text, image, selectedImage, data); | |
1655 | } | |
1656 | ||
08b7c251 VZ |
1657 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent, |
1658 | const wxString& text, | |
1659 | int image, int selectedImage, | |
1660 | wxTreeItemData *data) | |
2bda0e17 | 1661 | { |
ee4b2721 | 1662 | return DoInsertItem(parent, TVI_LAST, |
08b7c251 | 1663 | text, image, selectedImage, data); |
2bda0e17 KB |
1664 | } |
1665 | ||
08b7c251 | 1666 | void wxTreeCtrl::Delete(const wxTreeItemId& item) |
2bda0e17 | 1667 | { |
3f7bc32b | 1668 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) ) |
bbcdf8bc | 1669 | { |
f6bcfd97 | 1670 | wxLogLastError(wxT("TreeView_DeleteItem")); |
bbcdf8bc | 1671 | } |
bbcdf8bc JS |
1672 | } |
1673 | ||
23fd5130 VZ |
1674 | // delete all children (but don't delete the item itself) |
1675 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item) | |
1676 | { | |
ee4b2721 | 1677 | wxTreeItemIdValue cookie; |
23fd5130 | 1678 | |
ee4b2721 | 1679 | wxArrayTreeItemIds children; |
23fd5130 VZ |
1680 | wxTreeItemId child = GetFirstChild(item, cookie); |
1681 | while ( child.IsOk() ) | |
1682 | { | |
ee4b2721 | 1683 | children.Add(child); |
23fd5130 VZ |
1684 | |
1685 | child = GetNextChild(item, cookie); | |
1686 | } | |
1687 | ||
1688 | size_t nCount = children.Count(); | |
1689 | for ( size_t n = 0; n < nCount; n++ ) | |
1690 | { | |
f888d614 | 1691 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children[n])) ) |
23fd5130 | 1692 | { |
f6bcfd97 | 1693 | wxLogLastError(wxT("TreeView_DeleteItem")); |
23fd5130 VZ |
1694 | } |
1695 | } | |
1696 | } | |
1697 | ||
08b7c251 | 1698 | void wxTreeCtrl::DeleteAllItems() |
bbcdf8bc | 1699 | { |
bfedf621 VZ |
1700 | // delete the "virtual" root item. |
1701 | if ( GET_VIRTUAL_ROOT() ) | |
1702 | { | |
1703 | delete GET_VIRTUAL_ROOT(); | |
1704 | m_pVirtualRoot = NULL; | |
1705 | } | |
1706 | ||
1707 | // and all the real items | |
a9c1265f | 1708 | |
d220ae32 | 1709 | if ( !TreeView_DeleteAllItems(GetHwnd()) ) |
bbcdf8bc | 1710 | { |
f6bcfd97 | 1711 | wxLogLastError(wxT("TreeView_DeleteAllItems")); |
bbcdf8bc | 1712 | } |
2bda0e17 KB |
1713 | } |
1714 | ||
08b7c251 | 1715 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) |
2bda0e17 | 1716 | { |
dd3646fd VZ |
1717 | wxASSERT_MSG( flag == TVE_COLLAPSE || |
1718 | flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) || | |
1719 | flag == TVE_EXPAND || | |
1720 | flag == TVE_TOGGLE, | |
223d09f6 | 1721 | wxT("Unknown flag in wxTreeCtrl::DoExpand") ); |
08b7c251 | 1722 | |
a9c1265f | 1723 | // A hidden root can be neither expanded nor collapsed. |
e9616381 | 1724 | wxCHECK_RET( !(m_windowStyle & wxTR_HIDE_ROOT) || (HITEM(item) != TVI_ROOT), |
0d6a0101 | 1725 | wxT("Can't expand/collapse hidden root node!") ) |
a9c1265f | 1726 | |
08b7c251 | 1727 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must |
d220ae32 VZ |
1728 | // emulate them. This behaviour has changed slightly with comctl32.dll |
1729 | // v 4.70 - now it does send them but only the first time. To maintain | |
1730 | // compatible behaviour and also in order to not have surprises with the | |
1731 | // future versions, don't rely on this and still do everything ourselves. | |
1732 | // To avoid that the messages be sent twice when the item is expanded for | |
1733 | // the first time we must clear TVIS_EXPANDEDONCE style manually. | |
1734 | ||
1735 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE); | |
1736 | tvItem.state = 0; | |
1737 | DoSetItem(&tvItem); | |
1738 | ||
3f7bc32b | 1739 | if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 ) |
08b7c251 VZ |
1740 | { |
1741 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1742 | event.m_item = item; | |
08b7c251 | 1743 | event.SetEventObject(this); |
2bda0e17 | 1744 | |
deb1de30 VZ |
1745 | // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand() |
1746 | // itself | |
1747 | event.SetEventType(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND | |
1748 | : IDX_COLLAPSE] | |
1749 | [IDX_DONE]); | |
2bda0e17 | 1750 | |
deb1de30 | 1751 | (void)GetEventHandler()->ProcessEvent(event); |
08b7c251 | 1752 | } |
d220ae32 | 1753 | //else: change didn't took place, so do nothing at all |
2bda0e17 KB |
1754 | } |
1755 | ||
08b7c251 | 1756 | void wxTreeCtrl::Expand(const wxTreeItemId& item) |
2bda0e17 | 1757 | { |
08b7c251 | 1758 | DoExpand(item, TVE_EXPAND); |
2bda0e17 | 1759 | } |
2bda0e17 | 1760 | |
08b7c251 | 1761 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) |
2bda0e17 | 1762 | { |
08b7c251 | 1763 | DoExpand(item, TVE_COLLAPSE); |
2bda0e17 KB |
1764 | } |
1765 | ||
08b7c251 | 1766 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
2bda0e17 | 1767 | { |
dd3646fd | 1768 | DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET); |
2bda0e17 KB |
1769 | } |
1770 | ||
08b7c251 | 1771 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) |
2bda0e17 | 1772 | { |
08b7c251 | 1773 | DoExpand(item, TVE_TOGGLE); |
2bda0e17 KB |
1774 | } |
1775 | ||
b5f6b52a | 1776 | #if WXWIN_COMPATIBILITY_2_4 |
42c5812d UU |
1777 | void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action) |
1778 | { | |
9dfbf520 | 1779 | DoExpand(item, action); |
42c5812d | 1780 | } |
b5f6b52a | 1781 | #endif |
42c5812d | 1782 | |
08b7c251 | 1783 | void wxTreeCtrl::Unselect() |
2bda0e17 | 1784 | { |
3f7bc32b VZ |
1785 | wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), |
1786 | wxT("doesn't make sense, may be you want UnselectAll()?") ); | |
9dfbf520 VZ |
1787 | |
1788 | // just remove the selection | |
ee4b2721 | 1789 | SelectItem(wxTreeItemId()); |
08b7c251 | 1790 | } |
02ce7b72 | 1791 | |
9dfbf520 | 1792 | void wxTreeCtrl::UnselectAll() |
08b7c251 | 1793 | { |
9dfbf520 | 1794 | if ( m_windowStyle & wxTR_MULTIPLE ) |
2bda0e17 | 1795 | { |
9dfbf520 VZ |
1796 | wxArrayTreeItemIds selections; |
1797 | size_t count = GetSelections(selections); | |
1798 | for ( size_t n = 0; n < count; n++ ) | |
d220ae32 | 1799 | { |
3f7bc32b | 1800 | #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
f888d614 | 1801 | SetItemCheck(HITEM_PTR(selections[n]), false); |
3f7bc32b | 1802 | #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
f888d614 | 1803 | ::UnselectItem(GetHwnd(), HITEM_PTR(selections[n])); |
3f7bc32b | 1804 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
d220ae32 | 1805 | } |
9dfbf520 VZ |
1806 | } |
1807 | else | |
1808 | { | |
1809 | // just remove the selection | |
1810 | Unselect(); | |
1811 | } | |
1812 | } | |
1813 | ||
1814 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item) | |
1815 | { | |
1816 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
1817 | { | |
3f7bc32b | 1818 | #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
9dfbf520 VZ |
1819 | // selecting the item means checking it |
1820 | SetItemCheck(item); | |
3f7bc32b VZ |
1821 | #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
1822 | ::SelectItem(GetHwnd(), HITEM(item)); | |
1823 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
9dfbf520 VZ |
1824 | } |
1825 | else | |
1826 | { | |
1827 | // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive | |
1828 | // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so | |
1829 | // send them ourselves | |
1830 | ||
1831 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1832 | event.m_item = item; | |
1833 | event.SetEventObject(this); | |
1834 | ||
1835 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING); | |
1836 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
d220ae32 | 1837 | { |
3f7bc32b | 1838 | if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) ) |
9dfbf520 | 1839 | { |
f6bcfd97 | 1840 | wxLogLastError(wxT("TreeView_SelectItem")); |
9dfbf520 VZ |
1841 | } |
1842 | else | |
1843 | { | |
1844 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); | |
1845 | (void)GetEventHandler()->ProcessEvent(event); | |
1846 | } | |
d220ae32 | 1847 | } |
9dfbf520 | 1848 | //else: program vetoed the change |
2bda0e17 | 1849 | } |
08b7c251 | 1850 | } |
2bda0e17 | 1851 | |
08b7c251 VZ |
1852 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
1853 | { | |
1854 | // no error return | |
3f7bc32b | 1855 | TreeView_EnsureVisible(GetHwnd(), HITEM(item)); |
08b7c251 VZ |
1856 | } |
1857 | ||
1858 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) | |
1859 | { | |
3f7bc32b | 1860 | if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) ) |
2bda0e17 | 1861 | { |
f6bcfd97 | 1862 | wxLogLastError(wxT("TreeView_SelectSetFirstVisible")); |
2bda0e17 | 1863 | } |
08b7c251 VZ |
1864 | } |
1865 | ||
44fbc477 | 1866 | wxTextCtrl *wxTreeCtrl::GetEditControl() const |
08b7c251 VZ |
1867 | { |
1868 | return m_textCtrl; | |
1869 | } | |
1870 | ||
1871 | void wxTreeCtrl::DeleteTextCtrl() | |
1872 | { | |
1873 | if ( m_textCtrl ) | |
2bda0e17 | 1874 | { |
b6a3d6ad VZ |
1875 | // the HWND corresponding to this control is deleted by the tree |
1876 | // control itself and we don't know when exactly this happens, so check | |
1877 | // if the window still exists before calling UnsubclassWin() | |
1878 | if ( !::IsWindow(GetHwndOf(m_textCtrl)) ) | |
1879 | { | |
1880 | m_textCtrl->SetHWND(0); | |
1881 | } | |
1882 | ||
08b7c251 VZ |
1883 | m_textCtrl->UnsubclassWin(); |
1884 | m_textCtrl->SetHWND(0); | |
1885 | delete m_textCtrl; | |
1886 | m_textCtrl = NULL; | |
2bda0e17 | 1887 | } |
08b7c251 | 1888 | } |
2bda0e17 | 1889 | |
08b7c251 VZ |
1890 | wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, |
1891 | wxClassInfo* textControlClass) | |
1892 | { | |
1893 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
1894 | ||
b94ae1ea VZ |
1895 | DeleteTextCtrl(); |
1896 | ||
cd6bd270 | 1897 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); |
3f7bc32b | 1898 | HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item)); |
2bda0e17 | 1899 | |
5ea47806 | 1900 | // this is not an error - the TVN_BEGINLABELEDIT handler might have |
04cd30de | 1901 | // returned false |
5ea47806 VZ |
1902 | if ( !hWnd ) |
1903 | { | |
cd6bd270 MB |
1904 | delete m_textCtrl; |
1905 | m_textCtrl = NULL; | |
5ea47806 VZ |
1906 | return NULL; |
1907 | } | |
2bda0e17 | 1908 | |
cd6bd270 | 1909 | // textctrl is subclassed in MSWOnNotify |
08b7c251 | 1910 | return m_textCtrl; |
2bda0e17 KB |
1911 | } |
1912 | ||
08b7c251 | 1913 | // End label editing, optionally cancelling the edit |
33ac7e6f | 1914 | void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges) |
2bda0e17 | 1915 | { |
d220ae32 | 1916 | TreeView_EndEditLabelNow(GetHwnd(), discardChanges); |
08b7c251 VZ |
1917 | |
1918 | DeleteTextCtrl(); | |
2bda0e17 KB |
1919 | } |
1920 | ||
08b7c251 | 1921 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) |
2bda0e17 | 1922 | { |
08b7c251 VZ |
1923 | TV_HITTESTINFO hitTestInfo; |
1924 | hitTestInfo.pt.x = (int)point.x; | |
1925 | hitTestInfo.pt.y = (int)point.y; | |
2bda0e17 | 1926 | |
d220ae32 | 1927 | TreeView_HitTest(GetHwnd(), &hitTestInfo); |
2bda0e17 | 1928 | |
08b7c251 VZ |
1929 | flags = 0; |
1930 | ||
1931 | // avoid repetition | |
1932 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ | |
1933 | flags |= wxTREE_HITTEST_##flag | |
1934 | ||
1935 | TRANSLATE_FLAG(ABOVE); | |
1936 | TRANSLATE_FLAG(BELOW); | |
1937 | TRANSLATE_FLAG(NOWHERE); | |
1938 | TRANSLATE_FLAG(ONITEMBUTTON); | |
1939 | TRANSLATE_FLAG(ONITEMICON); | |
1940 | TRANSLATE_FLAG(ONITEMINDENT); | |
1941 | TRANSLATE_FLAG(ONITEMLABEL); | |
1942 | TRANSLATE_FLAG(ONITEMRIGHT); | |
1943 | TRANSLATE_FLAG(ONITEMSTATEICON); | |
1944 | TRANSLATE_FLAG(TOLEFT); | |
1945 | TRANSLATE_FLAG(TORIGHT); | |
2bda0e17 | 1946 | |
08b7c251 VZ |
1947 | #undef TRANSLATE_FLAG |
1948 | ||
ee4b2721 | 1949 | return wxTreeItemId(hitTestInfo.hItem); |
08b7c251 VZ |
1950 | } |
1951 | ||
f7c832a7 VZ |
1952 | bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item, |
1953 | wxRect& rect, | |
1954 | bool textOnly) const | |
1955 | { | |
1956 | RECT rc; | |
f2c3db9d RD |
1957 | |
1958 | // Virtual root items have no bounding rectangle | |
1959 | if ( IS_VIRTUAL_ROOT(item) ) | |
1960 | { | |
1961 | return false; | |
1962 | } | |
1963 | ||
3f7bc32b | 1964 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(item), |
f7c832a7 VZ |
1965 | &rc, textOnly) ) |
1966 | { | |
1967 | rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom)); | |
1968 | ||
04cd30de | 1969 | return true; |
f7c832a7 VZ |
1970 | } |
1971 | else | |
1972 | { | |
1973 | // couldn't retrieve rect: for example, item isn't visible | |
04cd30de | 1974 | return false; |
f7c832a7 VZ |
1975 | } |
1976 | } | |
1977 | ||
23fd5130 VZ |
1978 | // ---------------------------------------------------------------------------- |
1979 | // sorting stuff | |
1980 | // ---------------------------------------------------------------------------- | |
f7c832a7 | 1981 | |
502a2b18 VZ |
1982 | // this is just a tiny namespace which is friend to wxTreeCtrl and so can use |
1983 | // functions such as IsDataIndirect() | |
1984 | class wxTreeSortHelper | |
1985 | { | |
1986 | public: | |
1987 | static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree); | |
1988 | ||
1989 | private: | |
1990 | static wxTreeItemId GetIdFromData(wxTreeCtrl *tree, LPARAM item) | |
1991 | { | |
1992 | wxTreeItemData *data = (wxTreeItemData *)item; | |
1993 | if ( tree->IsDataIndirect(data) ) | |
1994 | { | |
1995 | data = ((wxTreeItemIndirectData *)data)->GetData(); | |
1996 | } | |
1997 | ||
1998 | return data->GetId(); | |
1999 | } | |
2000 | }; | |
2001 | ||
2002 | int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1, | |
2003 | LPARAM pItem2, | |
2004 | LPARAM htree) | |
23fd5130 | 2005 | { |
096c9f9b | 2006 | wxCHECK_MSG( pItem1 && pItem2, 0, |
223d09f6 | 2007 | wxT("sorting tree without data doesn't make sense") ); |
096c9f9b | 2008 | |
502a2b18 VZ |
2009 | wxTreeCtrl *tree = (wxTreeCtrl *)htree; |
2010 | ||
2011 | return tree->OnCompareItems(GetIdFromData(tree, pItem1), | |
2012 | GetIdFromData(tree, pItem2)); | |
23fd5130 VZ |
2013 | } |
2014 | ||
95aabccc VZ |
2015 | int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
2016 | const wxTreeItemId& item2) | |
08b7c251 | 2017 | { |
837e5743 | 2018 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); |
95aabccc VZ |
2019 | } |
2020 | ||
2021 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item) | |
2022 | { | |
2023 | // rely on the fact that TreeView_SortChildren does the same thing as our | |
23fd5130 VZ |
2024 | // default behaviour, i.e. sorts items alphabetically and so call it |
2025 | // directly if we're not in derived class (much more efficient!) | |
2026 | if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) ) | |
2bda0e17 | 2027 | { |
3f7bc32b | 2028 | TreeView_SortChildren(GetHwnd(), HITEM(item), 0); |
2bda0e17 | 2029 | } |
08b7c251 | 2030 | else |
2bda0e17 | 2031 | { |
62448488 | 2032 | TV_SORTCB tvSort; |
3f7bc32b | 2033 | tvSort.hParent = HITEM(item); |
502a2b18 | 2034 | tvSort.lpfnCompare = wxTreeSortHelper::Compare; |
23fd5130 | 2035 | tvSort.lParam = (LPARAM)this; |
d220ae32 | 2036 | TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */); |
2bda0e17 | 2037 | } |
08b7c251 | 2038 | } |
2bda0e17 | 2039 | |
08b7c251 VZ |
2040 | // ---------------------------------------------------------------------------- |
2041 | // implementation | |
2042 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 2043 | |
08b7c251 VZ |
2044 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id) |
2045 | { | |
2046 | if ( cmd == EN_UPDATE ) | |
2bda0e17 | 2047 | { |
08b7c251 VZ |
2048 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); |
2049 | event.SetEventObject( this ); | |
2050 | ProcessCommand(event); | |
2bda0e17 | 2051 | } |
08b7c251 | 2052 | else if ( cmd == EN_KILLFOCUS ) |
2bda0e17 | 2053 | { |
08b7c251 VZ |
2054 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); |
2055 | event.SetEventObject( this ); | |
2056 | ProcessCommand(event); | |
2bda0e17 | 2057 | } |
08b7c251 | 2058 | else |
2bda0e17 | 2059 | { |
08b7c251 | 2060 | // nothing done |
04cd30de | 2061 | return false; |
2bda0e17 | 2062 | } |
08b7c251 VZ |
2063 | |
2064 | // command processed | |
04cd30de | 2065 | return true; |
08b7c251 VZ |
2066 | } |
2067 | ||
23f681ec VZ |
2068 | // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we |
2069 | // only do it during dragging, minimize wxWin overhead (this is important for | |
2070 | // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly | |
2071 | // instead of passing by wxWin events | |
2072 | long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
2073 | { | |
04cd30de | 2074 | bool processed = false; |
3f7bc32b | 2075 | long rc = 0; |
3f7bc32b VZ |
2076 | bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0; |
2077 | ||
2078 | if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) ) | |
23f681ec | 2079 | { |
7bb8798c VZ |
2080 | // we only process mouse messages here and these parameters have the |
2081 | // same meaning for all of them | |
3f7bc32b VZ |
2082 | int x = GET_X_LPARAM(lParam), |
2083 | y = GET_Y_LPARAM(lParam); | |
2084 | HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y); | |
2085 | ||
23f681ec VZ |
2086 | switch ( nMsg ) |
2087 | { | |
de6ac339 JS |
2088 | case WM_RBUTTONDOWN: |
2089 | // if the item we are about to right click on | |
2090 | // is not already select, remove the entire | |
2091 | // previous selection | |
2092 | if (!::IsItemSelected(GetHwnd(), htItem)) | |
2093 | { | |
2094 | UnselectAll(); | |
2095 | } | |
2096 | ||
2097 | // select item and set the focus to the | |
2098 | // newly selected item | |
2099 | ::SelectItem(GetHwnd(), htItem); | |
2100 | ::SetFocus(GetHwnd(), htItem); | |
2101 | break; | |
2102 | ||
3f7bc32b VZ |
2103 | #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
2104 | case WM_LBUTTONDOWN: | |
2105 | if ( htItem && isMultiple ) | |
23f681ec | 2106 | { |
3f7bc32b VZ |
2107 | if ( wParam & MK_CONTROL ) |
2108 | { | |
2109 | SetFocus(); | |
23f681ec | 2110 | |
3f7bc32b VZ |
2111 | // toggle selected state |
2112 | ToggleItemSelection(GetHwnd(), htItem); | |
2113 | ||
2114 | ::SetFocus(GetHwnd(), htItem); | |
2115 | ||
2116 | // reset on any click without Shift | |
f888d614 | 2117 | m_htSelStart.Unset(); |
3f7bc32b | 2118 | |
04cd30de | 2119 | processed = true; |
3f7bc32b VZ |
2120 | } |
2121 | else if ( wParam & MK_SHIFT ) | |
2122 | { | |
2123 | // this selects all items between the starting one and | |
2124 | // the current | |
2125 | ||
2126 | if ( !m_htSelStart ) | |
2127 | { | |
2128 | // take the focused item | |
ee4b2721 | 2129 | m_htSelStart = TreeView_GetSelection(GetHwnd()); |
3f7bc32b VZ |
2130 | } |
2131 | ||
2132 | SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem, | |
2133 | !(wParam & MK_CONTROL)); | |
2134 | ||
2135 | ::SetFocus(GetHwnd(), htItem); | |
23f681ec | 2136 | |
04cd30de | 2137 | processed = true; |
3f7bc32b VZ |
2138 | } |
2139 | else // normal click | |
2140 | { | |
df4ac4c7 VZ |
2141 | // avoid doing anything if we click on the only |
2142 | // currently selected item | |
35cf1ec6 | 2143 | |
df4ac4c7 VZ |
2144 | wxArrayTreeItemIds selections; |
2145 | size_t count = GetSelections(selections); | |
2146 | if ( count == 0 || | |
35cf1ec6 | 2147 | count > 1 || |
f888d614 | 2148 | HITEM_PTR(selections[0]) != htItem ) |
df4ac4c7 | 2149 | { |
35cf1ec6 VZ |
2150 | // clear the previously selected items, if the |
2151 | // user clicked outside of the present selection. | |
2152 | // otherwise, perform the deselection on mouse-up. | |
2153 | // this allows multiple drag and drop to work. | |
2154 | ||
2155 | if (IsItemSelected(GetHwnd(), htItem)) | |
2156 | { | |
2157 | ::SetFocus(GetHwnd(), htItem); | |
2158 | } | |
2159 | else | |
2160 | { | |
2161 | UnselectAll(); | |
2162 | ||
2163 | // prevent the click from starting in-place editing | |
2164 | // which should only happen if we click on the | |
2165 | // already selected item (and nothing else is | |
2166 | // selected) | |
2167 | ||
2168 | TreeView_SelectItem(GetHwnd(), 0); | |
2169 | ::SelectItem(GetHwnd(), htItem); | |
2170 | } | |
df4ac4c7 | 2171 | } |
3f7bc32b VZ |
2172 | |
2173 | // reset on any click without Shift | |
f888d614 | 2174 | m_htSelStart.Unset(); |
3f7bc32b VZ |
2175 | } |
2176 | } | |
2177 | break; | |
2178 | #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
2179 | ||
2180 | case WM_MOUSEMOVE: | |
2181 | if ( m_dragImage ) | |
2182 | { | |
afff720b | 2183 | m_dragImage->Move(wxPoint(x, y)); |
3f7bc32b | 2184 | if ( htItem ) |
23f681ec VZ |
2185 | { |
2186 | // highlight the item as target (hiding drag image is | |
2187 | // necessary - otherwise the display will be corrupted) | |
68be9f09 | 2188 | m_dragImage->Hide(); |
3f7bc32b | 2189 | TreeView_SelectDropTarget(GetHwnd(), htItem); |
68be9f09 | 2190 | m_dragImage->Show(); |
23f681ec VZ |
2191 | } |
2192 | } | |
2193 | break; | |
2194 | ||
2195 | case WM_LBUTTONUP: | |
35cf1ec6 VZ |
2196 | |
2197 | // facilitates multiple drag-and-drop | |
2198 | if (htItem && isMultiple) | |
2199 | { | |
2200 | wxArrayTreeItemIds selections; | |
2201 | size_t count = GetSelections(selections); | |
2202 | ||
2203 | if (count > 1 && | |
2204 | !(wParam & MK_CONTROL) && | |
2205 | !(wParam & MK_SHIFT)) | |
2206 | { | |
2207 | UnselectAll(); | |
2208 | TreeView_SelectItem(GetHwnd(), htItem); | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | // fall through | |
2213 | ||
23f681ec | 2214 | case WM_RBUTTONUP: |
3f7bc32b | 2215 | if ( m_dragImage ) |
23f681ec | 2216 | { |
68be9f09 | 2217 | m_dragImage->EndDrag(); |
23f681ec VZ |
2218 | delete m_dragImage; |
2219 | m_dragImage = NULL; | |
2220 | ||
2221 | // generate the drag end event | |
2222 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId); | |
2223 | ||
ee4b2721 | 2224 | event.m_item = htItem; |
23f681ec VZ |
2225 | event.m_pointDrag = wxPoint(x, y); |
2226 | event.SetEventObject(this); | |
2227 | ||
2228 | (void)GetEventHandler()->ProcessEvent(event); | |
225fe9d6 VZ |
2229 | |
2230 | // if we don't do it, the tree seems to think that 2 items | |
2231 | // are selected simultaneously which is quite weird | |
2232 | TreeView_SelectDropTarget(GetHwnd(), 0); | |
23f681ec VZ |
2233 | } |
2234 | break; | |
2235 | } | |
2236 | } | |
3f7bc32b VZ |
2237 | #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE |
2238 | else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple ) | |
2239 | { | |
2240 | // the tree control greys out the selected item when it loses focus and | |
2241 | // paints it as selected again when it regains it, but it won't do it | |
2242 | // for the other items itself - help it | |
2243 | wxArrayTreeItemIds selections; | |
2244 | size_t count = GetSelections(selections); | |
2245 | RECT rect; | |
2246 | for ( size_t n = 0; n < count; n++ ) | |
2247 | { | |
04cd30de | 2248 | // TreeView_GetItemRect() will return false if item is not visible, |
3f7bc32b | 2249 | // which may happen perfectly well |
f888d614 | 2250 | if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections[n]), |
04cd30de | 2251 | &rect, true) ) |
3f7bc32b | 2252 | { |
04cd30de | 2253 | ::InvalidateRect(GetHwnd(), &rect, false); |
3f7bc32b VZ |
2254 | } |
2255 | } | |
2256 | } | |
2257 | else if ( nMsg == WM_KEYDOWN && isMultiple ) | |
2258 | { | |
2259 | bool bCtrl = wxIsCtrlDown(), | |
2260 | bShift = wxIsShiftDown(); | |
2261 | ||
2262 | // we handle.arrows and space, but not page up/down and home/end: the | |
2263 | // latter should be easy, but not the former | |
2264 | ||
2c8e4738 | 2265 | HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd()); |
3f7bc32b VZ |
2266 | if ( !m_htSelStart ) |
2267 | { | |
ee4b2721 | 2268 | m_htSelStart = htSel; |
3f7bc32b VZ |
2269 | } |
2270 | ||
2271 | if ( wParam == VK_SPACE ) | |
2272 | { | |
2273 | if ( bCtrl ) | |
2274 | { | |
2275 | ToggleItemSelection(GetHwnd(), htSel); | |
2276 | } | |
2277 | else | |
2278 | { | |
2279 | UnselectAll(); | |
2280 | ||
2281 | ::SelectItem(GetHwnd(), htSel); | |
2282 | } | |
23f681ec | 2283 | |
04cd30de | 2284 | processed = true; |
3f7bc32b VZ |
2285 | } |
2286 | else if ( wParam == VK_UP || wParam == VK_DOWN ) | |
2287 | { | |
2288 | if ( !bCtrl && !bShift ) | |
2289 | { | |
2290 | // no modifiers, just clear selection and then let the default | |
2291 | // processing to take place | |
2292 | UnselectAll(); | |
2293 | } | |
2294 | else if ( htSel ) | |
2295 | { | |
2296 | (void)wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
2297 | ||
2c8e4738 VZ |
2298 | HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP |
2299 | ? TreeView_GetPrevVisible(GetHwnd(), htSel) | |
2300 | : TreeView_GetNextVisible(GetHwnd(), htSel)); | |
3f7bc32b VZ |
2301 | |
2302 | if ( !htNext ) | |
2303 | { | |
2304 | // at the top/bottom | |
2305 | htNext = htSel; | |
2306 | } | |
2307 | ||
2308 | if ( bShift ) | |
2309 | { | |
2310 | SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext); | |
2311 | } | |
2312 | else // bCtrl | |
2313 | { | |
2314 | // without changing selection | |
2315 | ::SetFocus(GetHwnd(), htNext); | |
2316 | } | |
2317 | ||
04cd30de | 2318 | processed = true; |
3f7bc32b VZ |
2319 | } |
2320 | } | |
2321 | } | |
2322 | #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE | |
7bb8798c VZ |
2323 | else if ( nMsg == WM_CHAR ) |
2324 | { | |
2325 | // don't let the control process Space and Return keys because it | |
2326 | // doesn't do anything useful with them anyhow but always beeps | |
2327 | // annoyingly when it receives them and there is no way to turn it off | |
2328 | // simply if you just process TREEITEM_ACTIVATED event to which Space | |
2329 | // and Enter presses are mapped in your code | |
2330 | if ( wParam == VK_SPACE || wParam == VK_RETURN ) | |
2331 | { | |
2332 | processed = true; | |
2333 | } | |
2334 | } | |
2335 | ||
3f7bc32b VZ |
2336 | if ( !processed ) |
2337 | rc = wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
2338 | ||
2339 | return rc; | |
23f681ec VZ |
2340 | } |
2341 | ||
08b7c251 | 2342 | // process WM_NOTIFY Windows message |
a23fd0e1 | 2343 | bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
08b7c251 VZ |
2344 | { |
2345 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
2346 | wxEventType eventType = wxEVT_NULL; | |
2347 | NMHDR *hdr = (NMHDR *)lParam; | |
2348 | ||
2349 | switch ( hdr->code ) | |
2bda0e17 | 2350 | { |
08b7c251 VZ |
2351 | case TVN_BEGINDRAG: |
2352 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
2353 | // fall through | |
2354 | ||
2355 | case TVN_BEGINRDRAG: | |
2356 | { | |
2357 | if ( eventType == wxEVT_NULL ) | |
2358 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; | |
2359 | //else: left drag, already set above | |
2360 | ||
2361 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
2362 | ||
ee4b2721 | 2363 | event.m_item = tv->itemNew.hItem; |
08b7c251 | 2364 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); |
23f681ec VZ |
2365 | |
2366 | // don't allow dragging by default: the user code must | |
2367 | // explicitly say that it wants to allow it to avoid breaking | |
2368 | // the old apps | |
2369 | event.Veto(); | |
08b7c251 | 2370 | } |
696e1ea0 | 2371 | break; |
08b7c251 VZ |
2372 | |
2373 | case TVN_BEGINLABELEDIT: | |
2374 | { | |
2375 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; | |
2376 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2377 | ||
ee4b2721 | 2378 | event.m_item = info->item.hItem; |
5ea47806 | 2379 | event.m_label = info->item.pszText; |
04cd30de | 2380 | event.m_editCancelled = false; |
08b7c251 | 2381 | } |
696e1ea0 | 2382 | break; |
08b7c251 VZ |
2383 | |
2384 | case TVN_DELETEITEM: | |
2385 | { | |
2386 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; | |
2387 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
2388 | ||
ee4b2721 | 2389 | event.m_item = tv->itemOld.hItem; |
696e1ea0 VZ |
2390 | |
2391 | if ( m_hasAnyAttr ) | |
2392 | { | |
ee4b2721 VZ |
2393 | wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem); |
2394 | if ( it != m_attrs.end() ) | |
2395 | { | |
2396 | delete it->second; | |
2397 | m_attrs.erase(it); | |
2398 | } | |
696e1ea0 | 2399 | } |
08b7c251 | 2400 | } |
696e1ea0 | 2401 | break; |
08b7c251 VZ |
2402 | |
2403 | case TVN_ENDLABELEDIT: | |
2404 | { | |
2405 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; | |
2406 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2407 | ||
ee4b2721 | 2408 | event.m_item = info->item.hItem; |
5ea47806 | 2409 | event.m_label = info->item.pszText; |
1ee4ead5 | 2410 | if (info->item.pszText == NULL) |
dd23c25c | 2411 | { |
04cd30de | 2412 | event.m_editCancelled = true; |
dd23c25c JS |
2413 | } |
2414 | else | |
2415 | { | |
04cd30de | 2416 | event.m_editCancelled = false; |
dd23c25c | 2417 | } |
08b7c251 VZ |
2418 | break; |
2419 | } | |
2420 | ||
2421 | case TVN_GETDISPINFO: | |
2422 | eventType = wxEVT_COMMAND_TREE_GET_INFO; | |
2423 | // fall through | |
2424 | ||
2425 | case TVN_SETDISPINFO: | |
2426 | { | |
2427 | if ( eventType == wxEVT_NULL ) | |
2428 | eventType = wxEVT_COMMAND_TREE_SET_INFO; | |
2429 | //else: get, already set above | |
2430 | ||
2431 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2432 | ||
ee4b2721 | 2433 | event.m_item = info->item.hItem; |
08b7c251 VZ |
2434 | break; |
2435 | } | |
2436 | ||
2437 | case TVN_ITEMEXPANDING: | |
08b7c251 VZ |
2438 | case TVN_ITEMEXPANDED: |
2439 | { | |
2440 | NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam; | |
2441 | ||
deb1de30 | 2442 | int what; |
08b7c251 VZ |
2443 | switch ( tv->action ) |
2444 | { | |
deb1de30 VZ |
2445 | default: |
2446 | wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action); | |
2447 | // fall through | |
2448 | ||
08b7c251 | 2449 | case TVE_EXPAND: |
deb1de30 | 2450 | what = IDX_EXPAND; |
08b7c251 VZ |
2451 | break; |
2452 | ||
2453 | case TVE_COLLAPSE: | |
deb1de30 | 2454 | what = IDX_COLLAPSE; |
08b7c251 | 2455 | break; |
08b7c251 VZ |
2456 | } |
2457 | ||
2b5f62a0 VZ |
2458 | int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING |
2459 | : IDX_DONE; | |
deb1de30 VZ |
2460 | |
2461 | eventType = gs_expandEvents[what][how]; | |
08b7c251 | 2462 | |
ee4b2721 | 2463 | event.m_item = tv->itemNew.hItem; |
08b7c251 | 2464 | } |
696e1ea0 | 2465 | break; |
08b7c251 VZ |
2466 | |
2467 | case TVN_KEYDOWN: | |
2468 | { | |
2469 | eventType = wxEVT_COMMAND_TREE_KEY_DOWN; | |
2470 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; | |
2471 | ||
1944ad76 VZ |
2472 | // fabricate the lParam and wParam parameters sufficiently |
2473 | // similar to the ones from a "real" WM_KEYDOWN so that | |
2474 | // CreateKeyEvent() works correctly | |
2475 | WXLPARAM lParam = | |
1fdf858b | 2476 | (::GetKeyState(VK_MENU) < 0 ? KF_ALTDOWN : 0) << 16; |
1944ad76 VZ |
2477 | |
2478 | WXWPARAM wParam = info->wVKey; | |
2479 | ||
2480 | int keyCode = wxCharCodeMSWToWX(info->wVKey); | |
2481 | if ( !keyCode ) | |
2482 | { | |
2483 | // wxCharCodeMSWToWX() returns 0 to indicate that this is a | |
2484 | // simple ASCII key | |
2485 | keyCode = wParam; | |
2486 | } | |
2487 | ||
8191741f | 2488 | event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, |
1944ad76 VZ |
2489 | keyCode, |
2490 | lParam, | |
2491 | wParam); | |
23fd5130 | 2492 | |
3f7bc32b VZ |
2493 | // a separate event for Space/Return |
2494 | if ( !wxIsCtrlDown() && !wxIsShiftDown() && | |
2495 | ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) ) | |
23fd5130 VZ |
2496 | { |
2497 | wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, | |
2498 | m_windowId); | |
2499 | event2.SetEventObject(this); | |
3f7bc32b VZ |
2500 | if ( !(GetWindowStyle() & wxTR_MULTIPLE) ) |
2501 | { | |
2502 | event2.m_item = GetSelection(); | |
2503 | } | |
2504 | //else: don't know how to get it | |
23fd5130 | 2505 | |
3f7bc32b | 2506 | (void)GetEventHandler()->ProcessEvent(event2); |
23fd5130 | 2507 | } |
08b7c251 | 2508 | } |
696e1ea0 | 2509 | break; |
08b7c251 | 2510 | |
69a8b5b4 VS |
2511 | // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of |
2512 | // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore | |
2513 | // we have to handle both messages: | |
2514 | case TVN_SELCHANGEDA: | |
2515 | case TVN_SELCHANGEDW: | |
08b7c251 VZ |
2516 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; |
2517 | // fall through | |
2518 | ||
69a8b5b4 VS |
2519 | case TVN_SELCHANGINGA: |
2520 | case TVN_SELCHANGINGW: | |
08b7c251 VZ |
2521 | { |
2522 | if ( eventType == wxEVT_NULL ) | |
2523 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; | |
2524 | //else: already set above | |
2525 | ||
69a8b5b4 VS |
2526 | if (hdr->code == TVN_SELCHANGINGW || |
2527 | hdr->code == TVN_SELCHANGEDW) | |
2528 | { | |
2529 | NM_TREEVIEWW* tv = (NM_TREEVIEWW *)lParam; | |
ee4b2721 VZ |
2530 | event.m_item = tv->itemNew.hItem; |
2531 | event.m_itemOld = tv->itemOld.hItem; | |
69a8b5b4 VS |
2532 | } |
2533 | else | |
2534 | { | |
2535 | NM_TREEVIEWA* tv = (NM_TREEVIEWA *)lParam; | |
ee4b2721 VZ |
2536 | event.m_item = tv->itemNew.hItem; |
2537 | event.m_itemOld = tv->itemOld.hItem; | |
69a8b5b4 | 2538 | } |
08b7c251 | 2539 | } |
696e1ea0 VZ |
2540 | break; |
2541 | ||
bf9b6266 | 2542 | #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) |
696e1ea0 VZ |
2543 | case NM_CUSTOMDRAW: |
2544 | { | |
2545 | LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam; | |
2546 | NMCUSTOMDRAW& nmcd = lptvcd->nmcd; | |
d7926e0b | 2547 | switch ( nmcd.dwDrawStage ) |
696e1ea0 VZ |
2548 | { |
2549 | case CDDS_PREPAINT: | |
2550 | // if we've got any items with non standard attributes, | |
2551 | // notify us before painting each item | |
2552 | *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW | |
2553 | : CDRF_DODEFAULT; | |
d7926e0b | 2554 | break; |
696e1ea0 VZ |
2555 | |
2556 | case CDDS_ITEMPREPAINT: | |
2557 | { | |
ee4b2721 VZ |
2558 | wxMapTreeAttr::iterator |
2559 | it = m_attrs.find((void *)nmcd.dwItemSpec); | |
696e1ea0 | 2560 | |
ee4b2721 | 2561 | if ( it == m_attrs.end() ) |
696e1ea0 VZ |
2562 | { |
2563 | // nothing to do for this item | |
d7926e0b VZ |
2564 | *result = CDRF_DODEFAULT; |
2565 | break; | |
696e1ea0 VZ |
2566 | } |
2567 | ||
ee4b2721 VZ |
2568 | wxTreeItemAttr * const attr = it->second; |
2569 | ||
696e1ea0 | 2570 | HFONT hFont; |
696e1ea0 VZ |
2571 | if ( attr->HasFont() ) |
2572 | { | |
2076893b | 2573 | hFont = GetHfontOf(attr->GetFont()); |
696e1ea0 VZ |
2574 | } |
2575 | else | |
2576 | { | |
2577 | hFont = 0; | |
2578 | } | |
2579 | ||
2076893b | 2580 | wxColour colText; |
696e1ea0 VZ |
2581 | if ( attr->HasTextColour() ) |
2582 | { | |
2583 | colText = attr->GetTextColour(); | |
2584 | } | |
2585 | else | |
2586 | { | |
2587 | colText = GetForegroundColour(); | |
2588 | } | |
2589 | ||
2590 | // selection colours should override ours | |
2591 | if ( nmcd.uItemState & CDIS_SELECTED ) | |
2592 | { | |
2076893b VZ |
2593 | lptvcd->clrTextBk = |
2594 | ::GetSysColor(COLOR_HIGHLIGHT); | |
2595 | lptvcd->clrText = | |
2596 | ::GetSysColor(COLOR_HIGHLIGHTTEXT); | |
696e1ea0 | 2597 | } |
2076893b | 2598 | else // !selected |
696e1ea0 | 2599 | { |
2076893b | 2600 | wxColour colBack; |
696e1ea0 VZ |
2601 | if ( attr->HasBackgroundColour() ) |
2602 | { | |
2603 | colBack = attr->GetBackgroundColour(); | |
2604 | } | |
2605 | else | |
2606 | { | |
2607 | colBack = GetBackgroundColour(); | |
2608 | } | |
2609 | ||
2610 | lptvcd->clrText = wxColourToRGB(colText); | |
2611 | lptvcd->clrTextBk = wxColourToRGB(colBack); | |
2612 | } | |
2613 | ||
2614 | // note that if we wanted to set colours for | |
2615 | // individual columns (subitems), we would have | |
2616 | // returned CDRF_NOTIFYSUBITEMREDRAW from here | |
2617 | if ( hFont ) | |
2618 | { | |
2619 | ::SelectObject(nmcd.hdc, hFont); | |
2620 | ||
2621 | *result = CDRF_NEWFONT; | |
2622 | } | |
2623 | else | |
2624 | { | |
2625 | *result = CDRF_DODEFAULT; | |
2626 | } | |
696e1ea0 | 2627 | } |
d7926e0b | 2628 | break; |
696e1ea0 VZ |
2629 | |
2630 | default: | |
2631 | *result = CDRF_DODEFAULT; | |
696e1ea0 VZ |
2632 | } |
2633 | } | |
d7926e0b VZ |
2634 | |
2635 | // we always process it | |
04cd30de | 2636 | return true; |
6ecfe2ac | 2637 | #endif // _WIN32_IE >= 0x300 |
08b7c251 | 2638 | |
8a000b6b VZ |
2639 | case NM_CLICK: |
2640 | { | |
2641 | DWORD pos = GetMessagePos(); | |
2642 | POINT point; | |
2643 | point.x = LOWORD(pos); | |
2644 | point.y = HIWORD(pos); | |
2645 | ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1); | |
2646 | int flags = 0; | |
2647 | wxTreeItemId item = HitTest(wxPoint(point.x, point.y), flags); | |
2648 | if (flags & wxTREE_HITTEST_ONITEMSTATEICON) | |
2649 | { | |
2650 | event.m_item = item; | |
2651 | eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK; | |
2652 | } | |
2653 | break; | |
2654 | } | |
2655 | ||
f6bcfd97 BP |
2656 | case NM_DBLCLK: |
2657 | case NM_RCLICK: | |
2658 | { | |
2659 | TV_HITTESTINFO tvhti; | |
2660 | ::GetCursorPos(&tvhti.pt); | |
2661 | ::ScreenToClient(GetHwnd(), &tvhti.pt); | |
2662 | if ( TreeView_HitTest(GetHwnd(), &tvhti) ) | |
2663 | { | |
2664 | if ( tvhti.flags & TVHT_ONITEM ) | |
2665 | { | |
ee4b2721 | 2666 | event.m_item = tvhti.hItem; |
f6bcfd97 BP |
2667 | eventType = (int)hdr->code == NM_DBLCLK |
2668 | ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED | |
2669 | : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK; | |
8591268f VZ |
2670 | |
2671 | event.m_pointDrag.x = tvhti.pt.x; | |
2672 | event.m_pointDrag.y = tvhti.pt.y; | |
f6bcfd97 BP |
2673 | } |
2674 | ||
2675 | break; | |
2676 | } | |
2677 | } | |
2678 | // fall through | |
2679 | ||
08b7c251 | 2680 | default: |
a23fd0e1 | 2681 | return wxControl::MSWOnNotify(idCtrl, lParam, result); |
2bda0e17 | 2682 | } |
08b7c251 VZ |
2683 | |
2684 | event.SetEventObject(this); | |
2685 | event.SetEventType(eventType); | |
2686 | ||
fd3f686c | 2687 | bool processed = GetEventHandler()->ProcessEvent(event); |
08b7c251 VZ |
2688 | |
2689 | // post processing | |
5ea47806 | 2690 | switch ( hdr->code ) |
2bda0e17 | 2691 | { |
f6bcfd97 BP |
2692 | case NM_DBLCLK: |
2693 | // we translate NM_DBLCLK into ACTIVATED event, so don't interpret | |
2694 | // the return code of this event handler as the return value for | |
2695 | // NM_DBLCLK - otherwise, double clicking the item to toggle its | |
2696 | // expanded status would never work | |
04cd30de | 2697 | *result = false; |
f6bcfd97 BP |
2698 | break; |
2699 | ||
23f681ec VZ |
2700 | case TVN_BEGINDRAG: |
2701 | case TVN_BEGINRDRAG: | |
2702 | if ( event.IsAllowed() ) | |
2703 | { | |
2704 | // normally this is impossible because the m_dragImage is | |
2705 | // deleted once the drag operation is over | |
2706 | wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") ); | |
2707 | ||
2708 | m_dragImage = new wxDragImage(*this, event.m_item); | |
2709 | m_dragImage->BeginDrag(wxPoint(0, 0), this); | |
68be9f09 | 2710 | m_dragImage->Show(); |
23f681ec VZ |
2711 | } |
2712 | break; | |
2713 | ||
5ea47806 VZ |
2714 | case TVN_DELETEITEM: |
2715 | { | |
2716 | // NB: we might process this message using wxWindows event | |
2717 | // tables, but due to overhead of wxWin event system we | |
2718 | // prefer to do it here ourself (otherwise deleting a tree | |
2719 | // with many items is just too slow) | |
2720 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
74b31181 VZ |
2721 | |
2722 | wxTreeItemId item = event.m_item; | |
2723 | if ( HasIndirectData(item) ) | |
2724 | { | |
2725 | wxTreeItemIndirectData *data = (wxTreeItemIndirectData *) | |
2726 | tv->itemOld.lParam; | |
2727 | delete data; // can't be NULL here | |
74b31181 VZ |
2728 | } |
2729 | else | |
2730 | { | |
2731 | wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam; | |
2732 | delete data; // may be NULL, ok | |
2733 | } | |
08b7c251 | 2734 | |
04cd30de | 2735 | processed = true; // Make sure we don't get called twice |
5ea47806 VZ |
2736 | } |
2737 | break; | |
2738 | ||
2739 | case TVN_BEGINLABELEDIT: | |
04cd30de | 2740 | // return true to cancel label editing |
5ea47806 | 2741 | *result = !event.IsAllowed(); |
cd6bd270 MB |
2742 | // set ES_WANTRETURN ( like we do in BeginLabelEdit ) |
2743 | if(event.IsAllowed()) | |
2744 | { | |
2745 | HWND hText = TreeView_GetEditControl(GetHwnd()); | |
2746 | if(hText != NULL) | |
2747 | { | |
2748 | // MBN: if m_textCtrl already has an HWND, it is a stale | |
2749 | // pointer from a previous edit (because the user | |
2750 | // didn't modify the label before dismissing the control, | |
2751 | // and TVN_ENDLABELEDIT was not sent), so delete it | |
2752 | if(m_textCtrl && m_textCtrl->GetHWND() != 0) | |
2753 | DeleteTextCtrl(); | |
2754 | if(!m_textCtrl) | |
2755 | m_textCtrl = new wxTextCtrl(); | |
2756 | m_textCtrl->SetParent(this); | |
2757 | m_textCtrl->SetHWND((WXHWND)hText); | |
2758 | m_textCtrl->SubclassWin((WXHWND)hText); | |
2759 | ||
2760 | // set wxTE_PROCESS_ENTER style for the text control to | |
2761 | // force it to process the Enter presses itself, otherwise | |
2762 | // they could be stolen from it by the dialog | |
2763 | // navigation code | |
2764 | m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | |
2765 | | wxTE_PROCESS_ENTER); | |
2766 | } | |
2767 | } | |
5ea47806 VZ |
2768 | break; |
2769 | ||
2770 | case TVN_ENDLABELEDIT: | |
04cd30de | 2771 | // return true to set the label to the new string: note that we |
188781db | 2772 | // also must pretend that we did process the message or it is going |
04cd30de | 2773 | // to be passed to DefWindowProc() which will happily return false |
188781db | 2774 | // cancelling the label change |
5ea47806 | 2775 | *result = event.IsAllowed(); |
04cd30de | 2776 | processed = true; |
5ea47806 VZ |
2777 | |
2778 | // ensure that we don't have the text ctrl which is going to be | |
2779 | // deleted any more | |
2780 | DeleteTextCtrl(); | |
2781 | break; | |
2782 | ||
2783 | case TVN_SELCHANGING: | |
2784 | case TVN_ITEMEXPANDING: | |
04cd30de | 2785 | // return true to prevent the action from happening |
5ea47806 VZ |
2786 | *result = !event.IsAllowed(); |
2787 | break; | |
2788 | ||
deb1de30 VZ |
2789 | case TVN_ITEMEXPANDED: |
2790 | // the item is not refreshed properly after expansion when it has | |
2791 | // an image depending on the expanded/collapsed state - bug in | |
2792 | // comctl32.dll or our code? | |
2793 | { | |
bf43d750 | 2794 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; |
ee4b2721 | 2795 | wxTreeItemId id(tv->itemNew.hItem); |
deb1de30 | 2796 | |
bf43d750 VZ |
2797 | int image = GetItemImage(id, wxTreeItemIcon_Expanded); |
2798 | if ( image != -1 ) | |
2799 | { | |
2800 | RefreshItem(id); | |
deb1de30 VZ |
2801 | } |
2802 | } | |
2803 | break; | |
2804 | ||
74b31181 VZ |
2805 | case TVN_GETDISPINFO: |
2806 | // NB: so far the user can't set the image himself anyhow, so do it | |
2807 | // anyway - but this may change later | |
bf43d750 | 2808 | //if ( /* !processed && */ 1 ) |
74b31181 VZ |
2809 | { |
2810 | wxTreeItemId item = event.m_item; | |
2811 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2812 | if ( info->item.mask & TVIF_IMAGE ) | |
2813 | { | |
2814 | info->item.iImage = | |
2815 | DoGetItemImageFromData | |
2816 | ( | |
2817 | item, | |
2818 | IsExpanded(item) ? wxTreeItemIcon_Expanded | |
2819 | : wxTreeItemIcon_Normal | |
2820 | ); | |
2821 | } | |
2822 | if ( info->item.mask & TVIF_SELECTEDIMAGE ) | |
2823 | { | |
2824 | info->item.iSelectedImage = | |
2825 | DoGetItemImageFromData | |
2826 | ( | |
2827 | item, | |
2828 | IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded | |
2829 | : wxTreeItemIcon_Selected | |
2830 | ); | |
2831 | } | |
deb1de30 | 2832 | } |
74b31181 VZ |
2833 | break; |
2834 | ||
5ea47806 VZ |
2835 | //default: |
2836 | // for the other messages the return value is ignored and there is | |
2837 | // nothing special to do | |
2838 | } | |
fd3f686c | 2839 | return processed; |
2bda0e17 KB |
2840 | } |
2841 | ||
8a000b6b VZ |
2842 | // ---------------------------------------------------------------------------- |
2843 | // State control. | |
2844 | // ---------------------------------------------------------------------------- | |
2845 | ||
2846 | // why do they define INDEXTOSTATEIMAGEMASK but not the inverse? | |
2847 | #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12) | |
2848 | ||
2849 | void wxTreeCtrl::SetState(const wxTreeItemId& node, int state) | |
2850 | { | |
2851 | TV_ITEM tvi; | |
2852 | tvi.hItem = (HTREEITEM)node.m_pItem; | |
2853 | tvi.mask = TVIF_STATE; | |
2854 | tvi.stateMask = TVIS_STATEIMAGEMASK; | |
2855 | ||
2856 | // Select the specified state, or -1 == cycle to the next one. | |
2857 | if ( state == -1 ) | |
2858 | { | |
2859 | TreeView_GetItem(GetHwnd(), &tvi); | |
2860 | ||
2861 | state = STATEIMAGEMASKTOINDEX(tvi.state) + 1; | |
2862 | if ( state == m_imageListState->GetImageCount() ) | |
2863 | state = 1; | |
2864 | } | |
2865 | ||
2866 | wxCHECK_RET( state < m_imageListState->GetImageCount(), | |
2867 | _T("wxTreeCtrl::SetState(): item index out of bounds") ); | |
2868 | ||
2869 | tvi.state = INDEXTOSTATEIMAGEMASK(state); | |
2870 | ||
2871 | TreeView_SetItem(GetHwnd(), &tvi); | |
2872 | } | |
2873 | ||
2874 | int wxTreeCtrl::GetState(const wxTreeItemId& node) | |
2875 | { | |
2876 | TV_ITEM tvi; | |
2877 | tvi.hItem = (HTREEITEM)node.m_pItem; | |
2878 | tvi.mask = TVIF_STATE; | |
2879 | tvi.stateMask = TVIS_STATEIMAGEMASK; | |
2880 | TreeView_GetItem(GetHwnd(), &tvi); | |
2881 | ||
2882 | return STATEIMAGEMASKTOINDEX(tvi.state); | |
2883 | } | |
2884 | ||
1e6feb95 | 2885 | #endif // wxUSE_TREECTRL |
8a000b6b | 2886 |