]>
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 | |
65571936 | 9 | // Licence: wxWindows licence |
b823f5a1 | 10 | ///////////////////////////////////////////////////////////////////////////// |
2bda0e17 | 11 | |
08b7c251 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
1e6feb95 | 19 | |
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
08b7c251 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_TREECTRL |
28 | ||
e4db172a WS |
29 | #include "wx/treectrl.h" |
30 | ||
ad9835c9 | 31 | #ifndef WX_PRECOMP |
57bd4c60 WS |
32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
33 | #include "wx/msw/missing.h" | |
ad9835c9 | 34 | #include "wx/dynarray.h" |
e4db172a | 35 | #include "wx/log.h" |
670f9935 | 36 | #include "wx/app.h" |
9eddec69 | 37 | #include "wx/settings.h" |
ad9835c9 WS |
38 | #endif |
39 | ||
c27dce18 | 40 | #include "wx/dynlib.h" |
2f17da8b WS |
41 | #include "wx/msw/private.h" |
42 | ||
1e6feb95 VZ |
43 | // Set this to 1 to be _absolutely_ sure that repainting will work for all |
44 | // comctl32.dll versions | |
f6bcfd97 BP |
45 | #define wxUSE_COMCTL32_SAFELY 0 |
46 | ||
08b7c251 | 47 | #include "wx/imaglist.h" |
23f681ec VZ |
48 | #include "wx/msw/dragimag.h" |
49 | ||
f888d614 VZ |
50 | // macros to hide the cast ugliness |
51 | // -------------------------------- | |
52 | ||
14ba002a VZ |
53 | // get HTREEITEM from wxTreeItemId |
54 | #define HITEM(item) ((HTREEITEM)(((item).m_pItem))) | |
3f7bc32b | 55 | |
5fe302ef BW |
56 | |
57 | // older SDKs are missing these | |
58 | #ifndef TVN_ITEMCHANGINGA | |
59 | ||
60 | #define TVN_ITEMCHANGINGA (TVN_FIRST-16) | |
61 | #define TVN_ITEMCHANGINGW (TVN_FIRST-17) | |
62 | ||
63 | typedef struct tagNMTVITEMCHANGE | |
64 | { | |
65 | NMHDR hdr; | |
66 | UINT uChanged; | |
67 | HTREEITEM hItem; | |
68 | UINT uStateNew; | |
69 | UINT uStateOld; | |
70 | LPARAM lParam; | |
71 | } NMTVITEMCHANGE; | |
72 | ||
73 | #endif | |
74 | ||
75 | ||
396f85ba BW |
76 | // this helper class is used on vista systems for preventing unwanted |
77 | // item state changes in the vista tree control. It is only effective in | |
5fe302ef BW |
78 | // multi-select mode on vista systems. |
79 | ||
396f85ba BW |
80 | // The vista tree control includes some new code that originally broke the |
81 | // multi-selection tree, causing seemingly spurious item selection state changes | |
82 | // during Shift or Ctrl-click item selection. (To witness the original broken | |
83 | // behavior, simply make IsLocked() below always return false). This problem was | |
84 | // solved by using the following class to 'unlock' an item's selection state. | |
5fe302ef BW |
85 | |
86 | class TreeItemUnlocker | |
87 | { | |
88 | public: | |
396f85ba BW |
89 | // unlock a single item |
90 | TreeItemUnlocker(HTREEITEM item) { ms_unlockedItem = item; } | |
91 | ||
92 | // unlock all items, don't use unless absolutely necessary | |
93 | TreeItemUnlocker() { ms_unlockedItem = (HTREEITEM)-1; } | |
94 | ||
95 | // lock everything back | |
96 | ~TreeItemUnlocker() { ms_unlockedItem = NULL; } | |
97 | ||
98 | ||
99 | // check if the item state is currently locked | |
100 | static bool IsLocked(HTREEITEM item) | |
101 | { return ms_unlockedItem != (HTREEITEM)-1 && item != ms_unlockedItem; } | |
102 | ||
103 | private: | |
104 | static HTREEITEM ms_unlockedItem; | |
5fe302ef BW |
105 | }; |
106 | ||
396f85ba | 107 | HTREEITEM TreeItemUnlocker::ms_unlockedItem = NULL; |
5fe302ef | 108 | |
7dac12ef VZ |
109 | // another helper class: set the variable to true during its lifetime and reset |
110 | // it to false when it is destroyed | |
111 | // | |
112 | // it is currently always used with wxTreeCtrl::m_changingSelection | |
113 | class TempSetter | |
114 | { | |
115 | public: | |
116 | TempSetter(bool& var) : m_var(var) | |
117 | { | |
118 | wxASSERT_MSG( !m_var, "variable shouldn't be already set" ); | |
119 | m_var = true; | |
120 | } | |
121 | ||
122 | ~TempSetter() | |
123 | { | |
124 | m_var = false; | |
125 | } | |
126 | ||
127 | private: | |
128 | bool& m_var; | |
129 | ||
130 | wxDECLARE_NO_COPY_CLASS(TempSetter); | |
131 | }; | |
132 | ||
3f7bc32b VZ |
133 | // ---------------------------------------------------------------------------- |
134 | // private functions | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
75afdc2d VZ |
137 | // wrappers for TreeView_GetItem/TreeView_SetItem |
138 | static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem) | |
139 | { | |
75afdc2d VZ |
140 | TV_ITEM tvi; |
141 | tvi.mask = TVIF_STATE | TVIF_HANDLE; | |
142 | tvi.stateMask = TVIS_SELECTED; | |
143 | tvi.hItem = hItem; | |
144 | ||
5fe302ef BW |
145 | TreeItemUnlocker unlocker(hItem); |
146 | ||
75afdc2d VZ |
147 | if ( !TreeView_GetItem(hwndTV, &tvi) ) |
148 | { | |
149 | wxLogLastError(wxT("TreeView_GetItem")); | |
150 | } | |
151 | ||
152 | return (tvi.state & TVIS_SELECTED) != 0; | |
153 | } | |
154 | ||
155 | static bool SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = true) | |
156 | { | |
157 | TV_ITEM tvi; | |
158 | tvi.mask = TVIF_STATE | TVIF_HANDLE; | |
159 | tvi.stateMask = TVIS_SELECTED; | |
160 | tvi.state = select ? TVIS_SELECTED : 0; | |
161 | tvi.hItem = hItem; | |
162 | ||
5fe302ef BW |
163 | TreeItemUnlocker unlocker(hItem); |
164 | ||
75afdc2d VZ |
165 | if ( TreeView_SetItem(hwndTV, &tvi) == -1 ) |
166 | { | |
167 | wxLogLastError(wxT("TreeView_SetItem")); | |
168 | return false; | |
169 | } | |
170 | ||
171 | return true; | |
172 | } | |
173 | ||
174 | static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem) | |
175 | { | |
176 | SelectItem(hwndTV, htItem, false); | |
177 | } | |
178 | ||
c7d9c476 VZ |
179 | static inline void ToggleItemSelection(HWND hwndTV, HTREEITEM htItem) |
180 | { | |
181 | SelectItem(hwndTV, htItem, !IsItemSelected(hwndTV, htItem)); | |
182 | } | |
183 | ||
75afdc2d | 184 | // helper function which selects all items in a range and, optionally, |
c7d9c476 VZ |
185 | // deselects all the other ones |
186 | // | |
187 | // returns true if the selection changed at all or false if nothing changed | |
188 | ||
189 | // flags for SelectRange() | |
190 | enum | |
191 | { | |
192 | SR_SIMULATE = 1, // don't do anything, just return true or false | |
193 | SR_UNSELECT_OTHERS = 2 // deselect the items not in range | |
194 | }; | |
195 | ||
196 | static bool SelectRange(HWND hwndTV, | |
75afdc2d VZ |
197 | HTREEITEM htFirst, |
198 | HTREEITEM htLast, | |
c7d9c476 | 199 | int flags) |
75afdc2d VZ |
200 | { |
201 | // find the first (or last) item and select it | |
c7d9c476 | 202 | bool changed = false; |
75afdc2d VZ |
203 | bool cont = true; |
204 | HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV); | |
c7d9c476 | 205 | |
75afdc2d VZ |
206 | while ( htItem && cont ) |
207 | { | |
208 | if ( (htItem == htFirst) || (htItem == htLast) ) | |
209 | { | |
210 | if ( !IsItemSelected(hwndTV, htItem) ) | |
211 | { | |
c7d9c476 VZ |
212 | if ( !(flags & SR_SIMULATE) ) |
213 | { | |
214 | SelectItem(hwndTV, htItem); | |
215 | } | |
216 | ||
217 | changed = true; | |
75afdc2d VZ |
218 | } |
219 | ||
220 | cont = false; | |
221 | } | |
c7d9c476 | 222 | else // not first or last |
75afdc2d | 223 | { |
c7d9c476 | 224 | if ( flags & SR_UNSELECT_OTHERS ) |
75afdc2d | 225 | { |
c7d9c476 VZ |
226 | if ( IsItemSelected(hwndTV, htItem) ) |
227 | { | |
228 | if ( !(flags & SR_SIMULATE) ) | |
229 | UnselectItem(hwndTV, htItem); | |
230 | ||
231 | changed = true; | |
232 | } | |
75afdc2d VZ |
233 | } |
234 | } | |
235 | ||
236 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); | |
237 | } | |
238 | ||
239 | // select the items in range | |
240 | cont = htFirst != htLast; | |
241 | while ( htItem && cont ) | |
242 | { | |
243 | if ( !IsItemSelected(hwndTV, htItem) ) | |
244 | { | |
c7d9c476 VZ |
245 | if ( !(flags & SR_SIMULATE) ) |
246 | { | |
247 | SelectItem(hwndTV, htItem); | |
248 | } | |
249 | ||
250 | changed = true; | |
75afdc2d VZ |
251 | } |
252 | ||
253 | cont = (htItem != htFirst) && (htItem != htLast); | |
254 | ||
255 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); | |
256 | } | |
257 | ||
c7d9c476 VZ |
258 | // optionally deselect the rest |
259 | if ( flags & SR_UNSELECT_OTHERS ) | |
75afdc2d VZ |
260 | { |
261 | while ( htItem ) | |
262 | { | |
263 | if ( IsItemSelected(hwndTV, htItem) ) | |
264 | { | |
c7d9c476 VZ |
265 | if ( !(flags & SR_SIMULATE) ) |
266 | { | |
267 | UnselectItem(hwndTV, htItem); | |
268 | } | |
269 | ||
270 | changed = true; | |
75afdc2d VZ |
271 | } |
272 | ||
273 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); | |
274 | } | |
275 | } | |
276 | ||
277 | // seems to be necessary - otherwise the just selected items don't always | |
278 | // appear as selected | |
c7d9c476 VZ |
279 | if ( !(flags & SR_SIMULATE) ) |
280 | { | |
281 | UpdateWindow(hwndTV); | |
282 | } | |
283 | ||
284 | return changed; | |
75afdc2d VZ |
285 | } |
286 | ||
287 | // helper function which tricks the standard control into changing the focused | |
288 | // item without changing anything else (if someone knows why Microsoft doesn't | |
289 | // allow to do it by just setting TVIS_FOCUSED flag, please tell me!) | |
cc87a04c VZ |
290 | // |
291 | // returns true if the focus was changed, false if the given item was already | |
292 | // the focused one | |
293 | static bool SetFocus(HWND hwndTV, HTREEITEM htItem) | |
75afdc2d VZ |
294 | { |
295 | // the current focus | |
296 | HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV); | |
297 | ||
cc87a04c VZ |
298 | if ( htItem == htFocus ) |
299 | return false; | |
300 | ||
75afdc2d VZ |
301 | if ( htItem ) |
302 | { | |
cc87a04c VZ |
303 | // remember the selection state of the item |
304 | bool wasSelected = IsItemSelected(hwndTV, htItem); | |
75afdc2d | 305 | |
cc87a04c VZ |
306 | if ( htFocus && IsItemSelected(hwndTV, htFocus) ) |
307 | { | |
308 | // prevent the tree from unselecting the old focus which it | |
309 | // would do by default (TreeView_SelectItem unselects the | |
310 | // focused item) | |
311 | TreeView_SelectItem(hwndTV, 0); | |
312 | SelectItem(hwndTV, htFocus); | |
313 | } | |
75afdc2d | 314 | |
cc87a04c | 315 | TreeView_SelectItem(hwndTV, htItem); |
75afdc2d | 316 | |
cc87a04c VZ |
317 | if ( !wasSelected ) |
318 | { | |
319 | // need to clear the selection which TreeView_SelectItem() gave | |
320 | // us | |
321 | UnselectItem(hwndTV, htItem); | |
75afdc2d | 322 | } |
cc87a04c | 323 | //else: was selected, still selected - ok |
75afdc2d | 324 | } |
cc87a04c | 325 | else // reset focus |
75afdc2d | 326 | { |
cc87a04c | 327 | bool wasFocusSelected = IsItemSelected(hwndTV, htFocus); |
75afdc2d | 328 | |
cc87a04c VZ |
329 | // just clear the focus |
330 | TreeView_SelectItem(hwndTV, 0); | |
75afdc2d | 331 | |
cc87a04c VZ |
332 | if ( wasFocusSelected ) |
333 | { | |
334 | // restore the selection state | |
335 | SelectItem(hwndTV, htFocus); | |
75afdc2d | 336 | } |
75afdc2d | 337 | } |
cc87a04c VZ |
338 | |
339 | return true; | |
75afdc2d VZ |
340 | } |
341 | ||
08b7c251 VZ |
342 | // ---------------------------------------------------------------------------- |
343 | // private classes | |
344 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 345 | |
08b7c251 | 346 | // a convenient wrapper around TV_ITEM struct which adds a ctor |
f3ef286f | 347 | #ifdef __VISUALC__ |
3f7bc32b | 348 | #pragma warning( disable : 4097 ) // inheriting from typedef |
f3ef286f JS |
349 | #endif |
350 | ||
08b7c251 VZ |
351 | struct wxTreeViewItem : public TV_ITEM |
352 | { | |
9dfbf520 VZ |
353 | wxTreeViewItem(const wxTreeItemId& item, // the item handle |
354 | UINT mask_, // fields which are valid | |
355 | UINT stateMask_ = 0) // for TVIF_STATE only | |
08b7c251 | 356 | { |
a9c1265f VZ |
357 | wxZeroMemory(*this); |
358 | ||
9dfbf520 VZ |
359 | // hItem member is always valid |
360 | mask = mask_ | TVIF_HANDLE; | |
08b7c251 | 361 | stateMask = stateMask_; |
3f7bc32b | 362 | hItem = HITEM(item); |
08b7c251 VZ |
363 | } |
364 | }; | |
f3ef286f | 365 | |
4523ebb3 VZ |
366 | // ---------------------------------------------------------------------------- |
367 | // This class is our userdata/lParam for the TV_ITEMs stored in the treeview. | |
368 | // | |
369 | // We need this for a couple of reasons: | |
370 | // | |
371 | // 1) This class is needed for support of different images: the Win32 common | |
372 | // control natively supports only 2 images (the normal one and another for the | |
373 | // selected state). We wish to provide support for 2 more of them for folder | |
374 | // items (i.e. those which have children): for expanded state and for expanded | |
375 | // selected state. For this we use this structure to store the additional items | |
376 | // images. | |
377 | // | |
378 | // 2) This class is also needed to hold the HITEM so that we can sort | |
379 | // it correctly in the MSW sort callback. | |
380 | // | |
381 | // In addition it makes other workarounds such as this easier and helps | |
382 | // simplify the code. | |
383 | // ---------------------------------------------------------------------------- | |
384 | ||
385 | class wxTreeItemParam | |
386 | { | |
387 | public: | |
388 | wxTreeItemParam() | |
4523ebb3 | 389 | { |
20f4b566 VZ |
390 | m_data = NULL; |
391 | ||
4523ebb3 VZ |
392 | for ( size_t n = 0; n < WXSIZEOF(m_images); n++ ) |
393 | { | |
394 | m_images[n] = -1; | |
395 | } | |
396 | } | |
397 | ||
398 | // dtor deletes the associated data as well | |
399 | virtual ~wxTreeItemParam() { delete m_data; } | |
400 | ||
401 | // accessors | |
402 | // get the real data associated with the item | |
403 | wxTreeItemData *GetData() const { return m_data; } | |
404 | // change it | |
405 | void SetData(wxTreeItemData *data) { m_data = data; } | |
406 | ||
407 | // do we have such image? | |
408 | bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; } | |
1b182562 VZ |
409 | // get image, falling back to the other images if this one is not |
410 | // specified | |
411 | int GetImage(wxTreeItemIcon which) const | |
412 | { | |
413 | int image = m_images[which]; | |
414 | if ( image == -1 ) | |
415 | { | |
416 | switch ( which ) | |
417 | { | |
418 | case wxTreeItemIcon_SelectedExpanded: | |
419 | image = GetImage(wxTreeItemIcon_Expanded); | |
420 | if ( image != -1 ) | |
421 | break; | |
422 | //else: fall through | |
423 | ||
424 | case wxTreeItemIcon_Selected: | |
425 | case wxTreeItemIcon_Expanded: | |
426 | image = GetImage(wxTreeItemIcon_Normal); | |
427 | break; | |
428 | ||
429 | case wxTreeItemIcon_Normal: | |
430 | // no fallback | |
431 | break; | |
432 | ||
433 | default: | |
9a83f860 | 434 | wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") ); |
1b182562 VZ |
435 | } |
436 | } | |
437 | ||
438 | return image; | |
439 | } | |
440 | // change the given image | |
4523ebb3 VZ |
441 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } |
442 | ||
443 | // get item | |
444 | const wxTreeItemId& GetItem() const { return m_item; } | |
445 | // set item | |
446 | void SetItem(const wxTreeItemId& item) { m_item = item; } | |
447 | ||
448 | protected: | |
449 | // all the images associated with the item | |
450 | int m_images[wxTreeItemIcon_Max]; | |
451 | ||
452 | // item for sort callbacks | |
453 | wxTreeItemId m_item; | |
454 | ||
455 | // the real client data | |
456 | wxTreeItemData *m_data; | |
457 | ||
c0c133e1 | 458 | wxDECLARE_NO_COPY_CLASS(wxTreeItemParam); |
4523ebb3 VZ |
459 | }; |
460 | ||
a9c1265f VZ |
461 | // wxVirutalNode is used in place of a single root when 'hidden' root is |
462 | // specified. | |
efa38350 | 463 | class wxVirtualNode : public wxTreeViewItem |
a9c1265f VZ |
464 | { |
465 | public: | |
4523ebb3 | 466 | wxVirtualNode(wxTreeItemParam *param) |
efa38350 | 467 | : wxTreeViewItem(TVI_ROOT, 0) |
a9c1265f | 468 | { |
4523ebb3 | 469 | m_param = param; |
a9c1265f VZ |
470 | } |
471 | ||
472 | ~wxVirtualNode() | |
473 | { | |
4523ebb3 | 474 | delete m_param; |
a9c1265f VZ |
475 | } |
476 | ||
4523ebb3 VZ |
477 | wxTreeItemParam *GetParam() const { return m_param; } |
478 | void SetParam(wxTreeItemParam *param) { delete m_param; m_param = param; } | |
a9c1265f VZ |
479 | |
480 | private: | |
4523ebb3 | 481 | wxTreeItemParam *m_param; |
22f3361e | 482 | |
c0c133e1 | 483 | wxDECLARE_NO_COPY_CLASS(wxVirtualNode); |
a9c1265f VZ |
484 | }; |
485 | ||
f3ef286f | 486 | #ifdef __VISUALC__ |
197dd9af | 487 | #pragma warning( default : 4097 ) |
f3ef286f | 488 | #endif |
2bda0e17 | 489 | |
a9c1265f VZ |
490 | // a macro to get the virtual root, returns NULL if none |
491 | #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot) | |
492 | ||
04cd30de | 493 | // returns true if the item is the virtual root |
a9c1265f VZ |
494 | #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT) |
495 | ||
9dfbf520 | 496 | // a class which encapsulates the tree traversal logic: it vists all (unless |
04cd30de | 497 | // OnVisit() returns false) items under the given one |
9dfbf520 VZ |
498 | class wxTreeTraversal |
499 | { | |
500 | public: | |
501 | wxTreeTraversal(const wxTreeCtrl *tree) | |
502 | { | |
503 | m_tree = tree; | |
504 | } | |
505 | ||
cc11ded1 VZ |
506 | // give it a virtual dtor: not really needed as the class is never used |
507 | // polymorphically and not even allocated on heap at all, but this is safer | |
508 | // (in case it ever is) and silences the compiler warnings for now | |
509 | virtual ~wxTreeTraversal() { } | |
510 | ||
9dfbf520 | 511 | // do traverse the tree: visit all items (recursively by default) under the |
04cd30de RL |
512 | // given one; return true if all items were traversed or false if the |
513 | // traversal was aborted because OnVisit returned false | |
514 | bool DoTraverse(const wxTreeItemId& root, bool recursively = true); | |
9dfbf520 VZ |
515 | |
516 | // override this function to do whatever is needed for each item, return | |
04cd30de | 517 | // false to stop traversing |
9dfbf520 VZ |
518 | virtual bool OnVisit(const wxTreeItemId& item) = 0; |
519 | ||
520 | protected: | |
521 | const wxTreeCtrl *GetTree() const { return m_tree; } | |
522 | ||
523 | private: | |
524 | bool Traverse(const wxTreeItemId& root, bool recursively); | |
525 | ||
526 | const wxTreeCtrl *m_tree; | |
22f3361e | 527 | |
c0c133e1 | 528 | wxDECLARE_NO_COPY_CLASS(wxTreeTraversal); |
9dfbf520 VZ |
529 | }; |
530 | ||
74b31181 VZ |
531 | // internal class for getting the selected items |
532 | class TraverseSelections : public wxTreeTraversal | |
533 | { | |
534 | public: | |
535 | TraverseSelections(const wxTreeCtrl *tree, | |
536 | wxArrayTreeItemIds& selections) | |
537 | : wxTreeTraversal(tree), m_selections(selections) | |
538 | { | |
539 | m_selections.Empty(); | |
540 | ||
b9e9b40c JS |
541 | if (tree->GetCount() > 0) |
542 | DoTraverse(tree->GetRootItem()); | |
74b31181 VZ |
543 | } |
544 | ||
545 | virtual bool OnVisit(const wxTreeItemId& item) | |
546 | { | |
0228081f VZ |
547 | const wxTreeCtrl * const tree = GetTree(); |
548 | ||
a9c1265f | 549 | // can't visit a virtual node. |
0228081f | 550 | if ( (tree->GetRootItem() == item) && tree->HasFlag(wxTR_HIDE_ROOT) ) |
a9c1265f | 551 | { |
04cd30de | 552 | return true; |
a9c1265f VZ |
553 | } |
554 | ||
0228081f | 555 | if ( ::IsItemSelected(GetHwndOf(tree), HITEM(item)) ) |
74b31181 VZ |
556 | { |
557 | m_selections.Add(item); | |
558 | } | |
559 | ||
04cd30de | 560 | return true; |
74b31181 VZ |
561 | } |
562 | ||
e47c4d48 VZ |
563 | size_t GetCount() const { return m_selections.GetCount(); } |
564 | ||
74b31181 VZ |
565 | private: |
566 | wxArrayTreeItemIds& m_selections; | |
fc7a2a60 | 567 | |
c0c133e1 | 568 | wxDECLARE_NO_COPY_CLASS(TraverseSelections); |
74b31181 VZ |
569 | }; |
570 | ||
571 | // internal class for counting tree items | |
572 | class TraverseCounter : public wxTreeTraversal | |
573 | { | |
574 | public: | |
575 | TraverseCounter(const wxTreeCtrl *tree, | |
576 | const wxTreeItemId& root, | |
577 | bool recursively) | |
578 | : wxTreeTraversal(tree) | |
579 | { | |
580 | m_count = 0; | |
581 | ||
582 | DoTraverse(root, recursively); | |
583 | } | |
584 | ||
33ac7e6f | 585 | virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item)) |
74b31181 VZ |
586 | { |
587 | m_count++; | |
588 | ||
04cd30de | 589 | return true; |
74b31181 VZ |
590 | } |
591 | ||
592 | size_t GetCount() const { return m_count; } | |
593 | ||
594 | private: | |
595 | size_t m_count; | |
fc7a2a60 | 596 | |
c0c133e1 | 597 | wxDECLARE_NO_COPY_CLASS(TraverseCounter); |
74b31181 VZ |
598 | }; |
599 | ||
23f681ec | 600 | // ---------------------------------------------------------------------------- |
3f7bc32b | 601 | // wxWin macros |
08b7c251 VZ |
602 | // ---------------------------------------------------------------------------- |
603 | ||
08b7c251 | 604 | // ---------------------------------------------------------------------------- |
deb1de30 | 605 | // constants |
08b7c251 VZ |
606 | // ---------------------------------------------------------------------------- |
607 | ||
deb1de30 VZ |
608 | // indices in gs_expandEvents table below |
609 | enum | |
610 | { | |
611 | IDX_COLLAPSE, | |
612 | IDX_EXPAND, | |
613 | IDX_WHAT_MAX | |
614 | }; | |
615 | ||
616 | enum | |
617 | { | |
618 | IDX_DONE, | |
619 | IDX_DOING, | |
620 | IDX_HOW_MAX | |
621 | }; | |
622 | ||
623 | // handy table for sending events - it has to be initialized during run-time | |
624 | // now so can't be const any more | |
625 | static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX]; | |
626 | ||
627 | /* | |
628 | but logically it's a const table with the following entries: | |
629 | = | |
2bda0e17 | 630 | { |
08b7c251 VZ |
631 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, |
632 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } | |
633 | }; | |
deb1de30 | 634 | */ |
08b7c251 VZ |
635 | |
636 | // ============================================================================ | |
637 | // implementation | |
638 | // ============================================================================ | |
639 | ||
9dfbf520 VZ |
640 | // ---------------------------------------------------------------------------- |
641 | // tree traversal | |
642 | // ---------------------------------------------------------------------------- | |
643 | ||
644 | bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively) | |
645 | { | |
646 | if ( !OnVisit(root) ) | |
04cd30de | 647 | return false; |
9dfbf520 VZ |
648 | |
649 | return Traverse(root, recursively); | |
650 | } | |
651 | ||
652 | bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively) | |
653 | { | |
ee4b2721 | 654 | wxTreeItemIdValue cookie; |
9dfbf520 VZ |
655 | wxTreeItemId child = m_tree->GetFirstChild(root, cookie); |
656 | while ( child.IsOk() ) | |
657 | { | |
658 | // depth first traversal | |
04cd30de RL |
659 | if ( recursively && !Traverse(child, true) ) |
660 | return false; | |
9dfbf520 VZ |
661 | |
662 | if ( !OnVisit(child) ) | |
04cd30de | 663 | return false; |
9dfbf520 VZ |
664 | |
665 | child = m_tree->GetNextChild(root, cookie); | |
666 | } | |
667 | ||
04cd30de | 668 | return true; |
9dfbf520 VZ |
669 | } |
670 | ||
08b7c251 VZ |
671 | // ---------------------------------------------------------------------------- |
672 | // construction and destruction | |
673 | // ---------------------------------------------------------------------------- | |
674 | ||
675 | void wxTreeCtrl::Init() | |
676 | { | |
08b7c251 | 677 | m_textCtrl = NULL; |
04cd30de | 678 | m_hasAnyAttr = false; |
68d9be05 | 679 | #if wxUSE_DRAGIMAGE |
23f681ec | 680 | m_dragImage = NULL; |
68d9be05 | 681 | #endif |
a9c1265f | 682 | m_pVirtualRoot = NULL; |
c7d9c476 VZ |
683 | m_dragStarted = false; |
684 | m_focusLost = true; | |
7dac12ef | 685 | m_changingSelection = false; |
c7d9c476 | 686 | m_triggerStateImageClick = false; |
d301c440 | 687 | m_mouseUpDeselect = false; |
9ab5ef13 | 688 | |
deb1de30 VZ |
689 | // initialize the global array of events now as it can't be done statically |
690 | // with the wxEVT_XXX values being allocated during run-time only | |
691 | gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED; | |
692 | gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING; | |
693 | gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED; | |
694 | gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING; | |
2bda0e17 KB |
695 | } |
696 | ||
9dfbf520 VZ |
697 | bool wxTreeCtrl::Create(wxWindow *parent, |
698 | wxWindowID id, | |
699 | const wxPoint& pos, | |
700 | const wxSize& size, | |
701 | long style, | |
702 | const wxValidator& validator, | |
08b7c251 | 703 | const wxString& name) |
2bda0e17 | 704 | { |
08b7c251 | 705 | Init(); |
2bda0e17 | 706 | |
7699361c JS |
707 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) |
708 | style |= wxBORDER_SUNKEN; | |
709 | ||
9dfbf520 | 710 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
04cd30de | 711 | return false; |
2bda0e17 | 712 | |
823b140b | 713 | WXDWORD exStyle = 0; |
7699361c JS |
714 | DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle); |
715 | wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS; | |
2bda0e17 | 716 | |
c7d9c476 | 717 | if ( !(m_windowStyle & wxTR_NO_LINES) ) |
63da7df7 | 718 | wstyle |= TVS_HASLINES; |
08b7c251 VZ |
719 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) |
720 | wstyle |= TVS_HASBUTTONS; | |
2bda0e17 | 721 | |
08b7c251 VZ |
722 | if ( m_windowStyle & wxTR_EDIT_LABELS ) |
723 | wstyle |= TVS_EDITLABELS; | |
2bda0e17 | 724 | |
08b7c251 VZ |
725 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) |
726 | wstyle |= TVS_LINESATROOT; | |
deb1de30 | 727 | |
c6f4913a | 728 | if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT ) |
deb1de30 | 729 | { |
2a1f999f | 730 | if ( wxApp::GetComCtl32Version() >= 471 ) |
c6f4913a VS |
731 | wstyle |= TVS_FULLROWSELECT; |
732 | } | |
733 | ||
f3f71703 | 734 | #if !defined(__WXWINCE__) && defined(TVS_INFOTIP) |
156194e1 JS |
735 | // Need so that TVN_GETINFOTIP messages will be sent |
736 | wstyle |= TVS_INFOTIP; | |
676d6550 | 737 | #endif |
5e7718a2 | 738 | |
08b7c251 | 739 | // Create the tree control. |
8779cca3 | 740 | if ( !MSWCreateControl(WC_TREEVIEW, wstyle, pos, size) ) |
04cd30de | 741 | return false; |
9dfbf520 | 742 | |
f6bcfd97 | 743 | #if wxUSE_COMCTL32_SAFELY |
a756f210 | 744 | wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 BP |
745 | wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
746 | #elif 1 | |
a756f210 | 747 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 | 748 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
fbdcff4a JS |
749 | #else |
750 | // This works around a bug in the Windows tree control whereby for some versions | |
751 | // of comctrl32, setting any colour actually draws the background in black. | |
752 | // This will initialise the background to the system colour. | |
f6bcfd97 BP |
753 | // THIS FIX NOW REVERTED since it caused problems on _other_ systems. |
754 | // Assume the user has an updated comctl32.dll. | |
fbdcff4a | 755 | ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1); |
a756f210 | 756 | wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 | 757 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
fbdcff4a JS |
758 | #endif |
759 | ||
9a63feff VZ |
760 | wxSetCCUnicodeFormat(GetHwnd()); |
761 | ||
04cd30de | 762 | return true; |
2bda0e17 KB |
763 | } |
764 | ||
08b7c251 | 765 | wxTreeCtrl::~wxTreeCtrl() |
2bda0e17 | 766 | { |
696e1ea0 VZ |
767 | // delete any attributes |
768 | if ( m_hasAnyAttr ) | |
769 | { | |
ee4b2721 | 770 | WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs); |
696e1ea0 VZ |
771 | |
772 | // prevent TVN_DELETEITEM handler from deleting the attributes again! | |
04cd30de | 773 | m_hasAnyAttr = false; |
696e1ea0 VZ |
774 | } |
775 | ||
08b7c251 | 776 | DeleteTextCtrl(); |
2bda0e17 | 777 | |
08b7c251 | 778 | // delete user data to prevent memory leaks |
a9c1265f | 779 | // also deletes hidden root node storage. |
08b7c251 | 780 | DeleteAllItems(); |
2bda0e17 KB |
781 | } |
782 | ||
08b7c251 VZ |
783 | // ---------------------------------------------------------------------------- |
784 | // accessors | |
785 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 786 | |
39c7a53c VZ |
787 | /* static */ wxVisualAttributes |
788 | wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant) | |
789 | { | |
790 | wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant); | |
791 | ||
792 | // common controls have their own default font | |
793 | attrs.font = wxGetCCDefaultFont(); | |
794 | ||
795 | return attrs; | |
796 | } | |
797 | ||
798 | ||
08b7c251 | 799 | // simple wrappers which add error checking in debug mode |
2bda0e17 | 800 | |
4523ebb3 | 801 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem *tvItem) const |
2bda0e17 | 802 | { |
04cd30de | 803 | wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false, |
9a83f860 | 804 | wxT("can't retrieve virtual root item") ); |
a9c1265f | 805 | |
d220ae32 | 806 | if ( !TreeView_GetItem(GetHwnd(), tvItem) ) |
2bda0e17 | 807 | { |
f6bcfd97 | 808 | wxLogLastError(wxT("TreeView_GetItem")); |
08b7c251 | 809 | |
04cd30de | 810 | return false; |
08b7c251 VZ |
811 | } |
812 | ||
04cd30de | 813 | return true; |
2bda0e17 KB |
814 | } |
815 | ||
4523ebb3 | 816 | void wxTreeCtrl::DoSetItem(wxTreeViewItem *tvItem) |
2bda0e17 | 817 | { |
5fe302ef BW |
818 | TreeItemUnlocker unlocker(tvItem->hItem); |
819 | ||
d220ae32 | 820 | if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 ) |
2bda0e17 | 821 | { |
f6bcfd97 | 822 | wxLogLastError(wxT("TreeView_SetItem")); |
08b7c251 | 823 | } |
2bda0e17 KB |
824 | } |
825 | ||
027d45e8 | 826 | unsigned int wxTreeCtrl::GetCount() const |
2bda0e17 | 827 | { |
027d45e8 | 828 | return (unsigned int)TreeView_GetCount(GetHwnd()); |
2bda0e17 KB |
829 | } |
830 | ||
08b7c251 | 831 | unsigned int wxTreeCtrl::GetIndent() const |
2bda0e17 | 832 | { |
d220ae32 | 833 | return TreeView_GetIndent(GetHwnd()); |
2bda0e17 KB |
834 | } |
835 | ||
08b7c251 | 836 | void wxTreeCtrl::SetIndent(unsigned int indent) |
2bda0e17 | 837 | { |
d220ae32 | 838 | TreeView_SetIndent(GetHwnd(), indent); |
2bda0e17 KB |
839 | } |
840 | ||
08b7c251 | 841 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) |
2bda0e17 | 842 | { |
08b7c251 | 843 | // no error return |
8ecd5de2 WS |
844 | (void) TreeView_SetImageList(GetHwnd(), |
845 | imageList ? imageList->GetHIMAGELIST() : 0, | |
846 | which); | |
2bda0e17 KB |
847 | } |
848 | ||
08b7c251 | 849 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
2bda0e17 | 850 | { |
a9c1265f VZ |
851 | if (m_ownsImageListNormal) |
852 | delete m_imageListNormal; | |
853 | ||
08b7c251 | 854 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); |
04cd30de | 855 | m_ownsImageListNormal = false; |
2bda0e17 KB |
856 | } |
857 | ||
08b7c251 | 858 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
2bda0e17 | 859 | { |
0377efa2 | 860 | if (m_ownsImageListState) delete m_imageListState; |
08b7c251 | 861 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); |
04cd30de | 862 | m_ownsImageListState = false; |
0377efa2 VS |
863 | } |
864 | ||
33961d59 RR |
865 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, |
866 | bool recursively) const | |
867 | { | |
05b2a432 | 868 | wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") ); |
23fd5130 | 869 | |
05b2a432 | 870 | TraverseCounter counter(this, item, recursively); |
73974df1 | 871 | return counter.GetCount() - 1; |
23fd5130 VZ |
872 | } |
873 | ||
bb448552 VZ |
874 | // ---------------------------------------------------------------------------- |
875 | // control colours | |
876 | // ---------------------------------------------------------------------------- | |
877 | ||
878 | bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour) | |
879 | { | |
f6bcfd97 | 880 | #if !wxUSE_COMCTL32_SAFELY |
bb448552 | 881 | if ( !wxWindowBase::SetBackgroundColour(colour) ) |
04cd30de | 882 | return false; |
bb448552 | 883 | |
bfbb0b4c | 884 | ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel()); |
f6bcfd97 | 885 | #endif |
bb448552 | 886 | |
04cd30de | 887 | return true; |
bb448552 VZ |
888 | } |
889 | ||
890 | bool wxTreeCtrl::SetForegroundColour(const wxColour &colour) | |
891 | { | |
f6bcfd97 | 892 | #if !wxUSE_COMCTL32_SAFELY |
bb448552 | 893 | if ( !wxWindowBase::SetForegroundColour(colour) ) |
04cd30de | 894 | return false; |
bb448552 | 895 | |
bfbb0b4c | 896 | ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel()); |
f6bcfd97 | 897 | #endif |
bb448552 | 898 | |
04cd30de | 899 | return true; |
bb448552 VZ |
900 | } |
901 | ||
08b7c251 VZ |
902 | // ---------------------------------------------------------------------------- |
903 | // Item access | |
904 | // ---------------------------------------------------------------------------- | |
905 | ||
0228081f VZ |
906 | bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId& item) const |
907 | { | |
908 | return HITEM(item) == TVI_ROOT && HasFlag(wxTR_HIDE_ROOT); | |
909 | } | |
910 | ||
08b7c251 | 911 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const |
2bda0e17 | 912 | { |
f31a4098 | 913 | wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") ); |
05b2a432 | 914 | |
837e5743 | 915 | wxChar buf[512]; // the size is arbitrary... |
02ce7b72 | 916 | |
08b7c251 VZ |
917 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
918 | tvItem.pszText = buf; | |
919 | tvItem.cchTextMax = WXSIZEOF(buf); | |
920 | if ( !DoGetItem(&tvItem) ) | |
921 | { | |
922 | // don't return some garbage which was on stack, but an empty string | |
223d09f6 | 923 | buf[0] = wxT('\0'); |
08b7c251 | 924 | } |
2bda0e17 | 925 | |
08b7c251 VZ |
926 | return wxString(buf); |
927 | } | |
2bda0e17 | 928 | |
08b7c251 VZ |
929 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
930 | { | |
05b2a432 RD |
931 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
932 | ||
a9c1265f VZ |
933 | if ( IS_VIRTUAL_ROOT(item) ) |
934 | return; | |
935 | ||
08b7c251 | 936 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
c9f78968 | 937 | tvItem.pszText = (wxChar *)text.wx_str(); // conversion is ok |
08b7c251 | 938 | DoSetItem(&tvItem); |
44fbc477 VZ |
939 | |
940 | // when setting the text of the item being edited, the text control should | |
941 | // be updated to reflect the new text as well, otherwise calling | |
942 | // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect | |
943 | // | |
944 | // don't use GetEditControl() here because m_textCtrl is not set yet | |
945 | HWND hwndEdit = TreeView_GetEditControl(GetHwnd()); | |
946 | if ( hwndEdit ) | |
947 | { | |
1a4088e1 | 948 | if ( item == m_idEdited ) |
44fbc477 | 949 | { |
e0a050e3 | 950 | ::SetWindowText(hwndEdit, text.wx_str()); |
44fbc477 VZ |
951 | } |
952 | } | |
08b7c251 | 953 | } |
2bda0e17 | 954 | |
4523ebb3 VZ |
955 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item, |
956 | wxTreeItemIcon which) const | |
74b31181 | 957 | { |
4523ebb3 VZ |
958 | wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") ); |
959 | ||
0228081f | 960 | if ( IsHiddenRoot(item) ) |
74b31181 | 961 | { |
4523ebb3 | 962 | // no images for hidden root item |
74b31181 VZ |
963 | return -1; |
964 | } | |
965 | ||
4523ebb3 VZ |
966 | wxTreeItemParam *param = GetItemParam(item); |
967 | ||
1b182562 | 968 | return param && param->HasImage(which) ? param->GetImage(which) : -1; |
74b31181 VZ |
969 | } |
970 | ||
4523ebb3 VZ |
971 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image, |
972 | wxTreeItemIcon which) | |
74b31181 | 973 | { |
4523ebb3 VZ |
974 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
975 | wxCHECK_RET( which >= 0 && | |
976 | which < wxTreeItemIcon_Max, | |
977 | wxT("invalid image index")); | |
978 | ||
979 | ||
0228081f | 980 | if ( IsHiddenRoot(item) ) |
74b31181 | 981 | { |
4523ebb3 | 982 | // no images for hidden root item |
74b31181 VZ |
983 | return; |
984 | } | |
985 | ||
4523ebb3 VZ |
986 | wxTreeItemParam *data = GetItemParam(item); |
987 | if ( !data ) | |
988 | return; | |
74b31181 VZ |
989 | |
990 | data->SetImage(image, which); | |
f5bed7a8 VS |
991 | |
992 | RefreshItem(item); | |
74b31181 VZ |
993 | } |
994 | ||
4523ebb3 | 995 | wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const |
2bda0e17 | 996 | { |
05b2a432 RD |
997 | wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") ); |
998 | ||
08b7c251 | 999 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
a9c1265f | 1000 | |
ae322a48 VZ |
1001 | // hidden root may still have data. |
1002 | if ( IS_VIRTUAL_ROOT(item) ) | |
1003 | { | |
1004 | return GET_VIRTUAL_ROOT()->GetParam(); | |
1005 | } | |
1006 | ||
1007 | // visible node. | |
1008 | if ( !DoGetItem(&tvItem) ) | |
08b7c251 VZ |
1009 | { |
1010 | return NULL; | |
1011 | } | |
2bda0e17 | 1012 | |
4523ebb3 | 1013 | return (wxTreeItemParam *)tvItem.lParam; |
2bda0e17 KB |
1014 | } |
1015 | ||
c7d9c476 VZ |
1016 | bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent& event) const |
1017 | { | |
1018 | if ( event.m_item.IsOk() ) | |
1019 | { | |
1020 | event.SetClientObject(GetItemData(event.m_item)); | |
1021 | } | |
1022 | ||
1023 | return HandleWindowEvent(event); | |
1024 | } | |
1025 | ||
4523ebb3 | 1026 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
2bda0e17 | 1027 | { |
4523ebb3 | 1028 | wxTreeItemParam *data = GetItemParam(item); |
05b2a432 | 1029 | |
4523ebb3 VZ |
1030 | return data ? data->GetData() : NULL; |
1031 | } | |
a9c1265f | 1032 | |
4523ebb3 VZ |
1033 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
1034 | { | |
188781db VZ |
1035 | // first, associate this piece of data with this item |
1036 | if ( data ) | |
1037 | { | |
1038 | data->SetId(item); | |
1039 | } | |
1040 | ||
4523ebb3 | 1041 | wxTreeItemParam *param = GetItemParam(item); |
74b31181 | 1042 | |
4523ebb3 | 1043 | wxCHECK_RET( param, wxT("failed to change tree items data") ); |
74b31181 | 1044 | |
4523ebb3 | 1045 | param->SetData(data); |
08b7c251 | 1046 | } |
2bda0e17 | 1047 | |
3a5a2f56 VZ |
1048 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
1049 | { | |
05b2a432 RD |
1050 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1051 | ||
a9c1265f VZ |
1052 | if ( IS_VIRTUAL_ROOT(item) ) |
1053 | return; | |
1054 | ||
3a5a2f56 VZ |
1055 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
1056 | tvItem.cChildren = (int)has; | |
1057 | DoSetItem(&tvItem); | |
1058 | } | |
1059 | ||
add28c55 VZ |
1060 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
1061 | { | |
05b2a432 RD |
1062 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1063 | ||
a9c1265f VZ |
1064 | if ( IS_VIRTUAL_ROOT(item) ) |
1065 | return; | |
1066 | ||
add28c55 VZ |
1067 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); |
1068 | tvItem.state = bold ? TVIS_BOLD : 0; | |
1069 | DoSetItem(&tvItem); | |
1070 | } | |
1071 | ||
58a8ab88 JS |
1072 | void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight) |
1073 | { | |
a9c1265f VZ |
1074 | if ( IS_VIRTUAL_ROOT(item) ) |
1075 | return; | |
1076 | ||
58a8ab88 JS |
1077 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED); |
1078 | tvItem.state = highlight ? TVIS_DROPHILITED : 0; | |
1079 | DoSetItem(&tvItem); | |
1080 | } | |
1081 | ||
d00407b2 VZ |
1082 | void wxTreeCtrl::RefreshItem(const wxTreeItemId& item) |
1083 | { | |
a9c1265f VZ |
1084 | if ( IS_VIRTUAL_ROOT(item) ) |
1085 | return; | |
1086 | ||
d00407b2 VZ |
1087 | wxRect rect; |
1088 | if ( GetBoundingRect(item, rect) ) | |
1089 | { | |
1090 | RefreshRect(rect); | |
1091 | } | |
1092 | } | |
1093 | ||
2b5f62a0 VZ |
1094 | wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const |
1095 | { | |
05b2a432 | 1096 | wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") ); |
2b5f62a0 | 1097 | |
05b2a432 | 1098 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1099 | return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour(); |
2b5f62a0 VZ |
1100 | } |
1101 | ||
1102 | wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const | |
1103 | { | |
05b2a432 | 1104 | wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") ); |
2b5f62a0 | 1105 | |
05b2a432 | 1106 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1107 | return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour(); |
2b5f62a0 VZ |
1108 | } |
1109 | ||
1110 | wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const | |
1111 | { | |
05b2a432 | 1112 | wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") ); |
2b5f62a0 | 1113 | |
05b2a432 | 1114 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1115 | return it == m_attrs.end() ? wxNullFont : it->second->GetFont(); |
2b5f62a0 VZ |
1116 | } |
1117 | ||
696e1ea0 VZ |
1118 | void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item, |
1119 | const wxColour& col) | |
1120 | { | |
05b2a432 RD |
1121 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1122 | ||
ee4b2721 | 1123 | wxTreeItemAttr *attr; |
f888d614 | 1124 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1125 | if ( it == m_attrs.end() ) |
696e1ea0 | 1126 | { |
ee4b2721 VZ |
1127 | m_hasAnyAttr = true; |
1128 | ||
f888d614 | 1129 | m_attrs[item.m_pItem] = |
696e1ea0 | 1130 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1131 | } |
1132 | else | |
1133 | { | |
1134 | attr = it->second; | |
696e1ea0 VZ |
1135 | } |
1136 | ||
1137 | attr->SetTextColour(col); | |
d00407b2 VZ |
1138 | |
1139 | RefreshItem(item); | |
696e1ea0 VZ |
1140 | } |
1141 | ||
1142 | void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, | |
1143 | const wxColour& col) | |
1144 | { | |
05b2a432 RD |
1145 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1146 | ||
ee4b2721 | 1147 | wxTreeItemAttr *attr; |
f888d614 | 1148 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1149 | if ( it == m_attrs.end() ) |
696e1ea0 | 1150 | { |
ee4b2721 VZ |
1151 | m_hasAnyAttr = true; |
1152 | ||
f888d614 | 1153 | m_attrs[item.m_pItem] = |
696e1ea0 | 1154 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1155 | } |
1156 | else // already in the hash | |
1157 | { | |
1158 | attr = it->second; | |
696e1ea0 VZ |
1159 | } |
1160 | ||
1161 | attr->SetBackgroundColour(col); | |
d00407b2 VZ |
1162 | |
1163 | RefreshItem(item); | |
696e1ea0 VZ |
1164 | } |
1165 | ||
1166 | void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) | |
1167 | { | |
05b2a432 RD |
1168 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1169 | ||
ee4b2721 | 1170 | wxTreeItemAttr *attr; |
f888d614 | 1171 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1172 | if ( it == m_attrs.end() ) |
696e1ea0 | 1173 | { |
ee4b2721 VZ |
1174 | m_hasAnyAttr = true; |
1175 | ||
f888d614 | 1176 | m_attrs[item.m_pItem] = |
696e1ea0 | 1177 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1178 | } |
1179 | else // already in the hash | |
1180 | { | |
1181 | attr = it->second; | |
696e1ea0 VZ |
1182 | } |
1183 | ||
1184 | attr->SetFont(font); | |
d00407b2 | 1185 | |
2bd16277 RD |
1186 | // Reset the item's text to ensure that the bounding rect will be adjusted |
1187 | // for the new font. | |
1188 | SetItemText(item, GetItemText(item)); | |
72e61024 | 1189 | |
d00407b2 | 1190 | RefreshItem(item); |
696e1ea0 VZ |
1191 | } |
1192 | ||
08b7c251 VZ |
1193 | // ---------------------------------------------------------------------------- |
1194 | // Item status | |
1195 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1196 | |
08b7c251 VZ |
1197 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const |
1198 | { | |
bfbb0b4c | 1199 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
05b2a432 | 1200 | |
2b5f62a0 VZ |
1201 | if ( item == wxTreeItemId(TVI_ROOT) ) |
1202 | { | |
1203 | // virtual (hidden) root is never visible | |
04cd30de | 1204 | return false; |
2b5f62a0 VZ |
1205 | } |
1206 | ||
add28c55 | 1207 | // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect |
08b7c251 | 1208 | RECT rect; |
955be36c VZ |
1209 | |
1210 | // this ugliness comes directly from MSDN - it *is* the correct way to pass | |
1211 | // the HTREEITEM with TVM_GETITEMRECT | |
ee4b2721 | 1212 | *(HTREEITEM *)&rect = HITEM(item); |
955be36c | 1213 | |
caea927d VZ |
1214 | // true means to get rect for just the text, not the whole line |
1215 | if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT, true, (LPARAM)&rect) ) | |
1216 | { | |
1217 | // if TVM_GETITEMRECT returned false, then the item is definitely not | |
1218 | // visible (because its parent is not expanded) | |
1219 | return false; | |
1220 | } | |
1221 | ||
1222 | // however if it returned true, the item might still be outside the | |
1223 | // currently visible part of the tree, test for it (notice that partly | |
1224 | // visible means visible here) | |
1225 | return rect.bottom > 0 && rect.top < GetClientSize().y; | |
2bda0e17 KB |
1226 | } |
1227 | ||
08b7c251 | 1228 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
2bda0e17 | 1229 | { |
bfbb0b4c | 1230 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
05b2a432 | 1231 | |
fa97ee24 VZ |
1232 | if ( IS_VIRTUAL_ROOT(item) ) |
1233 | { | |
1234 | wxTreeItemIdValue cookie; | |
1235 | return GetFirstChild(item, cookie).IsOk(); | |
1236 | } | |
1237 | ||
08b7c251 | 1238 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
ae322a48 | 1239 | DoGetItem(&tvItem); |
2bda0e17 | 1240 | |
08b7c251 | 1241 | return tvItem.cChildren != 0; |
2bda0e17 KB |
1242 | } |
1243 | ||
08b7c251 | 1244 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
2bda0e17 | 1245 | { |
bfbb0b4c | 1246 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
2bda0e17 | 1247 | |
08b7c251 VZ |
1248 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); |
1249 | DoGetItem(&tvItem); | |
2bda0e17 | 1250 | |
08b7c251 | 1251 | return (tvItem.state & TVIS_EXPANDED) != 0; |
2bda0e17 KB |
1252 | } |
1253 | ||
08b7c251 | 1254 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
2bda0e17 | 1255 | { |
bfbb0b4c | 1256 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
05b2a432 | 1257 | |
08b7c251 VZ |
1258 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); |
1259 | DoGetItem(&tvItem); | |
2bda0e17 | 1260 | |
08b7c251 | 1261 | return (tvItem.state & TVIS_SELECTED) != 0; |
2bda0e17 KB |
1262 | } |
1263 | ||
add28c55 VZ |
1264 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const |
1265 | { | |
bfbb0b4c | 1266 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
05b2a432 | 1267 | |
add28c55 VZ |
1268 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); |
1269 | DoGetItem(&tvItem); | |
1270 | ||
1271 | return (tvItem.state & TVIS_BOLD) != 0; | |
1272 | } | |
1273 | ||
08b7c251 VZ |
1274 | // ---------------------------------------------------------------------------- |
1275 | // navigation | |
1276 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1277 | |
08b7c251 VZ |
1278 | wxTreeItemId wxTreeCtrl::GetRootItem() const |
1279 | { | |
a9c1265f VZ |
1280 | // Root may be real (visible) or virtual (hidden). |
1281 | if ( GET_VIRTUAL_ROOT() ) | |
1282 | return TVI_ROOT; | |
1283 | ||
ee4b2721 | 1284 | return wxTreeItemId(TreeView_GetRoot(GetHwnd())); |
08b7c251 | 1285 | } |
2bda0e17 | 1286 | |
08b7c251 VZ |
1287 | wxTreeItemId wxTreeCtrl::GetSelection() const |
1288 | { | |
c7d9c476 | 1289 | wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE), wxTreeItemId(), |
223d09f6 | 1290 | wxT("this only works with single selection controls") ); |
9dfbf520 | 1291 | |
3c01c595 VZ |
1292 | return GetFocusedItem(); |
1293 | } | |
1294 | ||
1295 | wxTreeItemId wxTreeCtrl::GetFocusedItem() const | |
1296 | { | |
ee4b2721 | 1297 | return wxTreeItemId(TreeView_GetSelection(GetHwnd())); |
2bda0e17 KB |
1298 | } |
1299 | ||
99006e44 | 1300 | wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const |
2bda0e17 | 1301 | { |
05b2a432 RD |
1302 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
1303 | ||
c9b142c9 VZ |
1304 | HTREEITEM hItem; |
1305 | ||
1306 | if ( IS_VIRTUAL_ROOT(item) ) | |
1307 | { | |
1308 | // no parent for the virtual root | |
1309 | hItem = 0; | |
1310 | } | |
1311 | else // normal item | |
a9c1265f | 1312 | { |
c9b142c9 VZ |
1313 | hItem = TreeView_GetParent(GetHwnd(), HITEM(item)); |
1314 | if ( !hItem && HasFlag(wxTR_HIDE_ROOT) ) | |
1315 | { | |
1316 | // the top level items should have the virtual root as their parent | |
1317 | hItem = TVI_ROOT; | |
1318 | } | |
a9c1265f VZ |
1319 | } |
1320 | ||
ee4b2721 | 1321 | return wxTreeItemId(hItem); |
08b7c251 | 1322 | } |
2bda0e17 | 1323 | |
08b7c251 | 1324 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
ee4b2721 | 1325 | wxTreeItemIdValue& cookie) const |
08b7c251 | 1326 | { |
05b2a432 RD |
1327 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
1328 | ||
08b7c251 | 1329 | // remember the last child returned in 'cookie' |
ee4b2721 VZ |
1330 | cookie = TreeView_GetChild(GetHwnd(), HITEM(item)); |
1331 | ||
1332 | return wxTreeItemId(cookie); | |
1333 | } | |
1334 | ||
1335 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), | |
1336 | wxTreeItemIdValue& cookie) const | |
1337 | { | |
44d60c0b WS |
1338 | wxTreeItemId fromCookie(cookie); |
1339 | ||
1340 | HTREEITEM hitem = HITEM(fromCookie); | |
1341 | ||
1342 | hitem = TreeView_GetNextSibling(GetHwnd(), hitem); | |
1343 | ||
1344 | wxTreeItemId item(hitem); | |
1345 | ||
ee4b2721 VZ |
1346 | cookie = item.m_pItem; |
1347 | ||
1348 | return item; | |
1349 | } | |
2bda0e17 | 1350 | |
978f38c2 VZ |
1351 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
1352 | { | |
05b2a432 RD |
1353 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
1354 | ||
978f38c2 | 1355 | // can this be done more efficiently? |
ee4b2721 | 1356 | wxTreeItemIdValue cookie; |
978f38c2 VZ |
1357 | |
1358 | wxTreeItemId childLast, | |
2165ad93 | 1359 | child = GetFirstChild(item, cookie); |
978f38c2 VZ |
1360 | while ( child.IsOk() ) |
1361 | { | |
1362 | childLast = child; | |
2165ad93 | 1363 | child = GetNextChild(item, cookie); |
978f38c2 VZ |
1364 | } |
1365 | ||
1366 | return childLast; | |
1367 | } | |
1368 | ||
08b7c251 VZ |
1369 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
1370 | { | |
05b2a432 | 1371 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
ee4b2721 | 1372 | return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item))); |
2bda0e17 KB |
1373 | } |
1374 | ||
08b7c251 | 1375 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
2bda0e17 | 1376 | { |
05b2a432 | 1377 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
ee4b2721 | 1378 | return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item))); |
2bda0e17 KB |
1379 | } |
1380 | ||
08b7c251 | 1381 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
2bda0e17 | 1382 | { |
ee4b2721 | 1383 | return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd())); |
2bda0e17 KB |
1384 | } |
1385 | ||
08b7c251 | 1386 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
2bda0e17 | 1387 | { |
05b2a432 | 1388 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
f6bcfd97 | 1389 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!")); |
02ce7b72 | 1390 | |
f73eddd2 VZ |
1391 | wxTreeItemId next(TreeView_GetNextVisible(GetHwnd(), HITEM(item))); |
1392 | if ( next.IsOk() && !IsVisible(next) ) | |
1393 | { | |
1394 | // Win32 considers that any non-collapsed item is visible while we want | |
1395 | // to return only really visible items | |
1396 | next.Unset(); | |
1397 | } | |
1398 | ||
1399 | return next; | |
08b7c251 | 1400 | } |
02ce7b72 | 1401 | |
08b7c251 VZ |
1402 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
1403 | { | |
05b2a432 | 1404 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
f6bcfd97 | 1405 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!")); |
02ce7b72 | 1406 | |
f73eddd2 VZ |
1407 | wxTreeItemId prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item))); |
1408 | if ( prev.IsOk() && !IsVisible(prev) ) | |
1409 | { | |
1410 | // just as above, Win32 function will happily return the previous item | |
1411 | // in the tree for the first visible item too | |
1412 | prev.Unset(); | |
1413 | } | |
1414 | ||
1415 | return prev; | |
08b7c251 | 1416 | } |
02ce7b72 | 1417 | |
9dfbf520 VZ |
1418 | // ---------------------------------------------------------------------------- |
1419 | // multiple selections emulation | |
1420 | // ---------------------------------------------------------------------------- | |
1421 | ||
33961d59 RR |
1422 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const |
1423 | { | |
1424 | TraverseSelections selector(this, selections); | |
9dfbf520 | 1425 | |
e47c4d48 | 1426 | return selector.GetCount(); |
9dfbf520 VZ |
1427 | } |
1428 | ||
08b7c251 VZ |
1429 | // ---------------------------------------------------------------------------- |
1430 | // Usual operations | |
1431 | // ---------------------------------------------------------------------------- | |
02ce7b72 | 1432 | |
8cee4a30 VZ |
1433 | wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent, |
1434 | const wxTreeItemId& hInsertAfter, | |
1435 | const wxString& text, | |
1436 | int image, int selectedImage, | |
1437 | wxTreeItemData *data) | |
08b7c251 | 1438 | { |
0e935169 VZ |
1439 | wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()), |
1440 | wxTreeItemId(), | |
9a83f860 | 1441 | wxT("can't have more than one root in the tree") ); |
0e935169 | 1442 | |
08b7c251 | 1443 | TV_INSERTSTRUCT tvIns; |
3f7bc32b VZ |
1444 | tvIns.hParent = HITEM(parent); |
1445 | tvIns.hInsertAfter = HITEM(hInsertAfter); | |
58a8ab88 | 1446 | |
74b31181 VZ |
1447 | // this is how we insert the item as the first child: supply a NULL |
1448 | // hInsertAfter | |
1449 | if ( !tvIns.hInsertAfter ) | |
58a8ab88 JS |
1450 | { |
1451 | tvIns.hInsertAfter = TVI_FIRST; | |
1452 | } | |
1453 | ||
08b7c251 | 1454 | UINT mask = 0; |
44d60c0b | 1455 | if ( !text.empty() ) |
08b7c251 VZ |
1456 | { |
1457 | mask |= TVIF_TEXT; | |
c9f78968 | 1458 | tvIns.item.pszText = (wxChar *)text.wx_str(); // cast is ok |
08b7c251 | 1459 | } |
f6bcfd97 BP |
1460 | else |
1461 | { | |
1462 | tvIns.item.pszText = NULL; | |
1463 | tvIns.item.cchTextMax = 0; | |
1464 | } | |
02ce7b72 | 1465 | |
1b182562 VZ |
1466 | // create the param which will store the other item parameters |
1467 | wxTreeItemParam *param = new wxTreeItemParam; | |
1468 | ||
1469 | // we return the images on demand as they depend on whether the item is | |
1470 | // expanded or collapsed too in our case | |
4523ebb3 VZ |
1471 | mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE; |
1472 | tvIns.item.iImage = I_IMAGECALLBACK; | |
1473 | tvIns.item.iSelectedImage = I_IMAGECALLBACK; | |
3a5a2f56 | 1474 | |
4523ebb3 | 1475 | param->SetImage(image, wxTreeItemIcon_Normal); |
1b182562 | 1476 | param->SetImage(selectedImage, wxTreeItemIcon_Selected); |
02ce7b72 | 1477 | |
4523ebb3 VZ |
1478 | mask |= TVIF_PARAM; |
1479 | tvIns.item.lParam = (LPARAM)param; | |
08b7c251 | 1480 | tvIns.item.mask = mask; |
02ce7b72 | 1481 | |
4a36036e VZ |
1482 | // don't use the hack below for the children of hidden root: this results |
1483 | // in a crash inside comctl32.dll when we call TreeView_GetItemRect() | |
1484 | const bool firstChild = !IsHiddenRoot(parent) && | |
1485 | !TreeView_GetChild(GetHwnd(), HITEM(parent)); | |
7e45c159 | 1486 | |
1b182562 | 1487 | HTREEITEM id = TreeView_InsertItem(GetHwnd(), &tvIns); |
08b7c251 VZ |
1488 | if ( id == 0 ) |
1489 | { | |
f6bcfd97 | 1490 | wxLogLastError(wxT("TreeView_InsertItem")); |
08b7c251 | 1491 | } |
02ce7b72 | 1492 | |
7e45c159 VZ |
1493 | // apparently some Windows versions (2000 and XP are reported to do this) |
1494 | // sometimes don't refresh the tree after adding the first child and so we | |
1495 | // need this to make the "[+]" appear | |
1496 | if ( firstChild ) | |
1497 | { | |
1498 | RECT rect; | |
1499 | TreeView_GetItemRect(GetHwnd(), HITEM(parent), &rect, FALSE); | |
1500 | ::InvalidateRect(GetHwnd(), &rect, FALSE); | |
1501 | } | |
1502 | ||
4523ebb3 VZ |
1503 | // associate the application tree item with Win32 tree item handle |
1504 | param->SetItem(id); | |
1505 | ||
1506 | // setup wxTreeItemData | |
fd3f686c VZ |
1507 | if ( data != NULL ) |
1508 | { | |
4523ebb3 | 1509 | param->SetData(data); |
ee4b2721 | 1510 | data->SetId(id); |
fd3f686c VZ |
1511 | } |
1512 | ||
ee4b2721 | 1513 | return wxTreeItemId(id); |
2bda0e17 KB |
1514 | } |
1515 | ||
08b7c251 VZ |
1516 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
1517 | int image, int selectedImage, | |
1518 | wxTreeItemData *data) | |
2bda0e17 | 1519 | { |
0228081f | 1520 | if ( HasFlag(wxTR_HIDE_ROOT) ) |
a9c1265f | 1521 | { |
9a83f860 | 1522 | wxASSERT_MSG( !m_pVirtualRoot, wxT("tree can have only a single root") ); |
2a69b4e8 | 1523 | |
a9c1265f | 1524 | // create a virtual root item, the parent for all the others |
4523ebb3 VZ |
1525 | wxTreeItemParam *param = new wxTreeItemParam; |
1526 | param->SetData(data); | |
1527 | ||
1528 | m_pVirtualRoot = new wxVirtualNode(param); | |
a9c1265f VZ |
1529 | |
1530 | return TVI_ROOT; | |
1531 | } | |
1532 | ||
8cee4a30 VZ |
1533 | return DoInsertAfter(wxTreeItemId(), wxTreeItemId(), |
1534 | text, image, selectedImage, data); | |
2bda0e17 KB |
1535 | } |
1536 | ||
8cee4a30 VZ |
1537 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, |
1538 | size_t index, | |
1539 | const wxString& text, | |
1540 | int image, int selectedImage, | |
1541 | wxTreeItemData *data) | |
2ef31e80 | 1542 | { |
8cee4a30 VZ |
1543 | wxTreeItemId idPrev; |
1544 | if ( index == (size_t)-1 ) | |
2ef31e80 | 1545 | { |
8cee4a30 VZ |
1546 | // special value: append to the end |
1547 | idPrev = TVI_LAST; | |
2ef31e80 | 1548 | } |
8cee4a30 VZ |
1549 | else // find the item from index |
1550 | { | |
1551 | wxTreeItemIdValue cookie; | |
1552 | wxTreeItemId idCur = GetFirstChild(parent, cookie); | |
1553 | while ( index != 0 && idCur.IsOk() ) | |
1554 | { | |
1555 | index--; | |
2ef31e80 | 1556 | |
8cee4a30 VZ |
1557 | idPrev = idCur; |
1558 | idCur = GetNextChild(parent, cookie); | |
1559 | } | |
2ef31e80 | 1560 | |
8cee4a30 VZ |
1561 | // assert, not check: if the index is invalid, we will append the item |
1562 | // to the end | |
9a83f860 | 1563 | wxASSERT_MSG( index == 0, wxT("bad index in wxTreeCtrl::InsertItem") ); |
8cee4a30 | 1564 | } |
2ef31e80 | 1565 | |
8cee4a30 | 1566 | return DoInsertAfter(parent, idPrev, text, image, selectedImage, data); |
2bda0e17 KB |
1567 | } |
1568 | ||
08b7c251 | 1569 | void wxTreeCtrl::Delete(const wxTreeItemId& item) |
2bda0e17 | 1570 | { |
396f85ba BW |
1571 | // unlock tree selections on vista, without this the |
1572 | // tree ctrl will eventually crash after item deletion | |
1573 | TreeItemUnlocker unlock_all; | |
1574 | ||
c7d9c476 VZ |
1575 | if ( HasFlag(wxTR_MULTIPLE) ) |
1576 | { | |
1577 | bool selected = IsSelected(item); | |
1578 | wxTreeItemId next; | |
1579 | ||
1580 | if ( selected ) | |
1581 | { | |
1582 | next = TreeView_GetNextVisible(GetHwnd(), HITEM(item)); | |
1583 | ||
1584 | if ( !next.IsOk() ) | |
1585 | { | |
1586 | next = TreeView_GetPrevVisible(GetHwnd(), HITEM(item)); | |
1587 | } | |
1588 | } | |
1589 | ||
c7d9c476 | 1590 | { |
7dac12ef VZ |
1591 | TempSetter set(m_changingSelection); |
1592 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) ) | |
1593 | { | |
1594 | wxLogLastError(wxT("TreeView_DeleteItem")); | |
1595 | return; | |
1596 | } | |
c7d9c476 VZ |
1597 | } |
1598 | ||
1599 | if ( !selected ) | |
1600 | { | |
1601 | return; | |
1602 | } | |
1603 | ||
f19259fc VZ |
1604 | if ( item == m_htSelStart ) |
1605 | m_htSelStart.Unset(); | |
1606 | ||
1607 | if ( item == m_htClickedItem ) | |
1608 | m_htClickedItem.Unset(); | |
1609 | ||
c7d9c476 VZ |
1610 | if ( next.IsOk() ) |
1611 | { | |
1612 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next); | |
1613 | ||
1614 | if ( IsTreeEventAllowed(changingEvent) ) | |
1615 | { | |
1616 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next); | |
1617 | (void)HandleTreeEvent(changedEvent); | |
1618 | } | |
1619 | else | |
1620 | { | |
7dac12ef VZ |
1621 | DoUnselectItem(next); |
1622 | ClearFocusedItem(); | |
c7d9c476 VZ |
1623 | } |
1624 | } | |
1625 | } | |
1626 | else | |
bbcdf8bc | 1627 | { |
c7d9c476 VZ |
1628 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) ) |
1629 | { | |
1630 | wxLogLastError(wxT("TreeView_DeleteItem")); | |
1631 | } | |
bbcdf8bc | 1632 | } |
bbcdf8bc JS |
1633 | } |
1634 | ||
23fd5130 VZ |
1635 | // delete all children (but don't delete the item itself) |
1636 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item) | |
1637 | { | |
396f85ba BW |
1638 | // unlock tree selections on vista for the duration of this call |
1639 | TreeItemUnlocker unlock_all; | |
1640 | ||
ee4b2721 | 1641 | wxTreeItemIdValue cookie; |
23fd5130 | 1642 | |
ee4b2721 | 1643 | wxArrayTreeItemIds children; |
23fd5130 VZ |
1644 | wxTreeItemId child = GetFirstChild(item, cookie); |
1645 | while ( child.IsOk() ) | |
1646 | { | |
ee4b2721 | 1647 | children.Add(child); |
23fd5130 VZ |
1648 | |
1649 | child = GetNextChild(item, cookie); | |
1650 | } | |
1651 | ||
1652 | size_t nCount = children.Count(); | |
1653 | for ( size_t n = 0; n < nCount; n++ ) | |
1654 | { | |
c7d9c476 | 1655 | Delete(children[n]); |
23fd5130 VZ |
1656 | } |
1657 | } | |
1658 | ||
08b7c251 | 1659 | void wxTreeCtrl::DeleteAllItems() |
bbcdf8bc | 1660 | { |
396f85ba BW |
1661 | // unlock tree selections on vista for the duration of this call |
1662 | TreeItemUnlocker unlock_all; | |
1663 | ||
f19259fc VZ |
1664 | // invalidate all the items we store as they're going to become invalid |
1665 | m_htSelStart = | |
1666 | m_htClickedItem = wxTreeItemId(); | |
1667 | ||
bfedf621 VZ |
1668 | // delete the "virtual" root item. |
1669 | if ( GET_VIRTUAL_ROOT() ) | |
1670 | { | |
1671 | delete GET_VIRTUAL_ROOT(); | |
1672 | m_pVirtualRoot = NULL; | |
1673 | } | |
1674 | ||
1675 | // and all the real items | |
a9c1265f | 1676 | |
d220ae32 | 1677 | if ( !TreeView_DeleteAllItems(GetHwnd()) ) |
bbcdf8bc | 1678 | { |
f6bcfd97 | 1679 | wxLogLastError(wxT("TreeView_DeleteAllItems")); |
bbcdf8bc | 1680 | } |
2bda0e17 KB |
1681 | } |
1682 | ||
08b7c251 | 1683 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) |
2bda0e17 | 1684 | { |
dd3646fd VZ |
1685 | wxASSERT_MSG( flag == TVE_COLLAPSE || |
1686 | flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) || | |
1687 | flag == TVE_EXPAND || | |
1688 | flag == TVE_TOGGLE, | |
223d09f6 | 1689 | wxT("Unknown flag in wxTreeCtrl::DoExpand") ); |
08b7c251 | 1690 | |
a9c1265f | 1691 | // A hidden root can be neither expanded nor collapsed. |
0228081f | 1692 | wxCHECK_RET( !IsHiddenRoot(item), |
637b7e4f | 1693 | wxT("Can't expand/collapse hidden root node!") ); |
a9c1265f | 1694 | |
08b7c251 | 1695 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must |
d220ae32 VZ |
1696 | // emulate them. This behaviour has changed slightly with comctl32.dll |
1697 | // v 4.70 - now it does send them but only the first time. To maintain | |
1698 | // compatible behaviour and also in order to not have surprises with the | |
1699 | // future versions, don't rely on this and still do everything ourselves. | |
1700 | // To avoid that the messages be sent twice when the item is expanded for | |
1701 | // the first time we must clear TVIS_EXPANDEDONCE style manually. | |
1702 | ||
1703 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE); | |
1704 | tvItem.state = 0; | |
1705 | DoSetItem(&tvItem); | |
1706 | ||
c7d9c476 VZ |
1707 | if ( IsExpanded(item) ) |
1708 | { | |
1709 | wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, | |
1710 | this, wxTreeItemId(item)); | |
1711 | ||
1712 | if ( !IsTreeEventAllowed(event) ) | |
1713 | return; | |
1714 | } | |
1715 | ||
1716 | if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) ) | |
08b7c251 | 1717 | { |
c7d9c476 VZ |
1718 | if ( IsExpanded(item) ) |
1719 | return; | |
1720 | ||
1721 | wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED, this, item); | |
1722 | (void)HandleTreeEvent(event); | |
08b7c251 | 1723 | } |
d220ae32 | 1724 | //else: change didn't took place, so do nothing at all |
2bda0e17 KB |
1725 | } |
1726 | ||
08b7c251 | 1727 | void wxTreeCtrl::Expand(const wxTreeItemId& item) |
2bda0e17 | 1728 | { |
08b7c251 | 1729 | DoExpand(item, TVE_EXPAND); |
2bda0e17 | 1730 | } |
2bda0e17 | 1731 | |
08b7c251 | 1732 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) |
2bda0e17 | 1733 | { |
08b7c251 | 1734 | DoExpand(item, TVE_COLLAPSE); |
2bda0e17 KB |
1735 | } |
1736 | ||
08b7c251 | 1737 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
2bda0e17 | 1738 | { |
dd3646fd | 1739 | DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET); |
2bda0e17 KB |
1740 | } |
1741 | ||
08b7c251 | 1742 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) |
2bda0e17 | 1743 | { |
08b7c251 | 1744 | DoExpand(item, TVE_TOGGLE); |
2bda0e17 KB |
1745 | } |
1746 | ||
08b7c251 | 1747 | void wxTreeCtrl::Unselect() |
2bda0e17 | 1748 | { |
c7d9c476 | 1749 | wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE), |
3f7bc32b | 1750 | wxT("doesn't make sense, may be you want UnselectAll()?") ); |
9dfbf520 | 1751 | |
c7d9c476 VZ |
1752 | // the current focus |
1753 | HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd()); | |
1754 | ||
1755 | if ( !htFocus ) | |
1756 | { | |
1757 | return; | |
1758 | } | |
1759 | ||
1760 | if ( HasFlag(wxTR_MULTIPLE) ) | |
1761 | { | |
1762 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
1763 | this, wxTreeItemId()); | |
1764 | changingEvent.m_itemOld = htFocus; | |
1765 | ||
1766 | if ( IsTreeEventAllowed(changingEvent) ) | |
1767 | { | |
7dac12ef | 1768 | ClearFocusedItem(); |
c7d9c476 VZ |
1769 | |
1770 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
1771 | this, wxTreeItemId()); | |
1772 | changedEvent.m_itemOld = htFocus; | |
1773 | (void)HandleTreeEvent(changedEvent); | |
1774 | } | |
1775 | } | |
1776 | else | |
1777 | { | |
7dac12ef | 1778 | ClearFocusedItem(); |
c7d9c476 VZ |
1779 | } |
1780 | } | |
1781 | ||
1782 | void wxTreeCtrl::DoUnselectAll() | |
1783 | { | |
1784 | wxArrayTreeItemIds selections; | |
1785 | size_t count = GetSelections(selections); | |
1786 | ||
1787 | for ( size_t n = 0; n < count; n++ ) | |
1788 | { | |
7dac12ef | 1789 | DoUnselectItem(selections[n]); |
c7d9c476 VZ |
1790 | } |
1791 | ||
1792 | m_htSelStart.Unset(); | |
08b7c251 | 1793 | } |
02ce7b72 | 1794 | |
9dfbf520 | 1795 | void wxTreeCtrl::UnselectAll() |
08b7c251 | 1796 | { |
c7d9c476 | 1797 | if ( HasFlag(wxTR_MULTIPLE) ) |
2bda0e17 | 1798 | { |
c7d9c476 VZ |
1799 | HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd()); |
1800 | if ( !htFocus ) return; | |
1801 | ||
1802 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this); | |
1803 | changingEvent.m_itemOld = htFocus; | |
1804 | ||
1805 | if ( IsTreeEventAllowed(changingEvent) ) | |
d220ae32 | 1806 | { |
c7d9c476 | 1807 | DoUnselectAll(); |
ba8a1961 | 1808 | |
c7d9c476 VZ |
1809 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this); |
1810 | changedEvent.m_itemOld = htFocus; | |
1811 | (void)HandleTreeEvent(changedEvent); | |
1812 | } | |
9dfbf520 | 1813 | } |
75afdc2d | 1814 | else |
9dfbf520 | 1815 | { |
9dfbf520 VZ |
1816 | Unselect(); |
1817 | } | |
1818 | } | |
1819 | ||
5cb3a695 VZ |
1820 | void wxTreeCtrl::DoSelectChildren(const wxTreeItemId& parent) |
1821 | { | |
1822 | DoUnselectAll(); | |
1823 | ||
1824 | wxTreeItemIdValue cookie; | |
1825 | wxTreeItemId child = GetFirstChild(parent, cookie); | |
1826 | while ( child.IsOk() ) | |
1827 | { | |
1828 | DoSelectItem(child, true); | |
1829 | child = GetNextChild(child, cookie); | |
1830 | } | |
1831 | } | |
1832 | ||
1833 | void wxTreeCtrl::SelectChildren(const wxTreeItemId& parent) | |
1834 | { | |
1835 | wxCHECK_RET( HasFlag(wxTR_MULTIPLE), | |
1836 | "this only works with multiple selection controls" ); | |
1837 | ||
1838 | HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd()); | |
1839 | ||
1840 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this); | |
1841 | changingEvent.m_itemOld = htFocus; | |
1842 | ||
1843 | if ( IsTreeEventAllowed(changingEvent) ) | |
1844 | { | |
1845 | DoSelectChildren(parent); | |
1846 | ||
1847 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this); | |
1848 | changedEvent.m_itemOld = htFocus; | |
1849 | (void)HandleTreeEvent(changedEvent); | |
1850 | } | |
1851 | } | |
1852 | ||
7dac12ef VZ |
1853 | void wxTreeCtrl::DoSelectItem(const wxTreeItemId& item, bool select) |
1854 | { | |
1855 | TempSetter set(m_changingSelection); | |
1856 | ||
1857 | ::SelectItem(GetHwnd(), HITEM(item), select); | |
1858 | } | |
1859 | ||
3e9af289 | 1860 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select) |
9dfbf520 | 1861 | { |
9a83f860 | 1862 | wxCHECK_RET( !IsHiddenRoot(item), wxT("can't select hidden root item") ); |
8a536fa3 | 1863 | |
86646693 | 1864 | if ( select == IsSelected(item) ) |
c7d9c476 | 1865 | { |
86646693 | 1866 | // nothing to do, the item is already in the requested state |
c7d9c476 VZ |
1867 | return; |
1868 | } | |
9dfbf520 | 1869 | |
c7d9c476 | 1870 | if ( HasFlag(wxTR_MULTIPLE) ) |
cc87a04c | 1871 | { |
c7d9c476 VZ |
1872 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item); |
1873 | ||
1874 | if ( IsTreeEventAllowed(changingEvent) ) | |
d220ae32 | 1875 | { |
c7d9c476 | 1876 | HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd()); |
7dac12ef | 1877 | DoSelectItem(item, select); |
c7d9c476 VZ |
1878 | |
1879 | if ( !htFocus ) | |
7549148b | 1880 | { |
7dac12ef | 1881 | SetFocusedItem(item); |
7549148b | 1882 | } |
c7d9c476 VZ |
1883 | |
1884 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
1885 | this, item); | |
1886 | (void)HandleTreeEvent(changedEvent); | |
cc87a04c | 1887 | } |
c7d9c476 | 1888 | } |
86646693 | 1889 | else // single selection |
c7d9c476 | 1890 | { |
86646693 VZ |
1891 | wxTreeItemId itemOld, itemNew; |
1892 | if ( select ) | |
1893 | { | |
1894 | itemOld = GetSelection(); | |
1895 | itemNew = item; | |
1896 | } | |
1897 | else // deselecting the currently selected item | |
1898 | { | |
1899 | itemOld = item; | |
1900 | // leave itemNew invalid | |
1901 | } | |
c7d9c476 | 1902 | |
942f40ca VZ |
1903 | // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events |
1904 | // when we call TreeView_SelectItem() but apparently some old ones did | |
1905 | // not so send the events ourselves and ignore those generated by | |
1906 | // TreeView_SelectItem() if m_changingSelection is set. | |
86646693 VZ |
1907 | wxTreeEvent |
1908 | changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, itemNew); | |
1909 | changingEvent.SetOldItem(itemOld); | |
c7d9c476 VZ |
1910 | |
1911 | if ( IsTreeEventAllowed(changingEvent) ) | |
cc87a04c | 1912 | { |
1da2783c VZ |
1913 | TempSetter set(m_changingSelection); |
1914 | ||
86646693 | 1915 | if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew)) ) |
7549148b VZ |
1916 | { |
1917 | wxLogLastError(wxT("TreeView_SelectItem")); | |
7549148b | 1918 | } |
c7d9c476 VZ |
1919 | else // ok |
1920 | { | |
1da2783c | 1921 | ::SetFocus(GetHwnd(), HITEM(item)); |
7549148b | 1922 | |
c7d9c476 | 1923 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, |
86646693 VZ |
1924 | this, itemNew); |
1925 | changedEvent.SetOldItem(itemOld); | |
c7d9c476 VZ |
1926 | (void)HandleTreeEvent(changedEvent); |
1927 | } | |
1928 | } | |
1929 | //else: program vetoed the change | |
2bda0e17 | 1930 | } |
08b7c251 | 1931 | } |
2bda0e17 | 1932 | |
08b7c251 VZ |
1933 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
1934 | { | |
9a83f860 | 1935 | wxCHECK_RET( !IsHiddenRoot(item), wxT("can't show hidden root item") ); |
0228081f | 1936 | |
08b7c251 | 1937 | // no error return |
3f7bc32b | 1938 | TreeView_EnsureVisible(GetHwnd(), HITEM(item)); |
08b7c251 VZ |
1939 | } |
1940 | ||
1941 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) | |
1942 | { | |
3f7bc32b | 1943 | if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) ) |
2bda0e17 | 1944 | { |
f6bcfd97 | 1945 | wxLogLastError(wxT("TreeView_SelectSetFirstVisible")); |
2bda0e17 | 1946 | } |
08b7c251 VZ |
1947 | } |
1948 | ||
44fbc477 | 1949 | wxTextCtrl *wxTreeCtrl::GetEditControl() const |
08b7c251 VZ |
1950 | { |
1951 | return m_textCtrl; | |
1952 | } | |
1953 | ||
1954 | void wxTreeCtrl::DeleteTextCtrl() | |
1955 | { | |
1956 | if ( m_textCtrl ) | |
2bda0e17 | 1957 | { |
b6a3d6ad VZ |
1958 | // the HWND corresponding to this control is deleted by the tree |
1959 | // control itself and we don't know when exactly this happens, so check | |
1960 | // if the window still exists before calling UnsubclassWin() | |
1961 | if ( !::IsWindow(GetHwndOf(m_textCtrl)) ) | |
1962 | { | |
1963 | m_textCtrl->SetHWND(0); | |
1964 | } | |
1965 | ||
08b7c251 VZ |
1966 | m_textCtrl->UnsubclassWin(); |
1967 | m_textCtrl->SetHWND(0); | |
5276b0a5 | 1968 | wxDELETE(m_textCtrl); |
1a4088e1 VZ |
1969 | |
1970 | m_idEdited.Unset(); | |
2bda0e17 | 1971 | } |
08b7c251 | 1972 | } |
2bda0e17 | 1973 | |
4523ebb3 VZ |
1974 | wxTextCtrl *wxTreeCtrl::EditLabel(const wxTreeItemId& item, |
1975 | wxClassInfo *textControlClass) | |
08b7c251 VZ |
1976 | { |
1977 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
1978 | ||
b94ae1ea VZ |
1979 | DeleteTextCtrl(); |
1980 | ||
1a4088e1 | 1981 | m_idEdited = item; |
cd6bd270 | 1982 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); |
3f7bc32b | 1983 | HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item)); |
2bda0e17 | 1984 | |
5ea47806 | 1985 | // this is not an error - the TVN_BEGINLABELEDIT handler might have |
04cd30de | 1986 | // returned false |
5ea47806 VZ |
1987 | if ( !hWnd ) |
1988 | { | |
5276b0a5 | 1989 | wxDELETE(m_textCtrl); |
5ea47806 VZ |
1990 | return NULL; |
1991 | } | |
2bda0e17 | 1992 | |
cd6bd270 | 1993 | // textctrl is subclassed in MSWOnNotify |
08b7c251 | 1994 | return m_textCtrl; |
2bda0e17 KB |
1995 | } |
1996 | ||
08b7c251 | 1997 | // End label editing, optionally cancelling the edit |
d35dce3a | 1998 | void wxTreeCtrl::DoEndEditLabel(bool discardChanges) |
2bda0e17 | 1999 | { |
d220ae32 | 2000 | TreeView_EndEditLabelNow(GetHwnd(), discardChanges); |
08b7c251 VZ |
2001 | |
2002 | DeleteTextCtrl(); | |
2bda0e17 KB |
2003 | } |
2004 | ||
be0e5d69 | 2005 | wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const |
2bda0e17 | 2006 | { |
08b7c251 VZ |
2007 | TV_HITTESTINFO hitTestInfo; |
2008 | hitTestInfo.pt.x = (int)point.x; | |
2009 | hitTestInfo.pt.y = (int)point.y; | |
2bda0e17 | 2010 | |
8ecd5de2 | 2011 | (void) TreeView_HitTest(GetHwnd(), &hitTestInfo); |
2bda0e17 | 2012 | |
08b7c251 VZ |
2013 | flags = 0; |
2014 | ||
2015 | // avoid repetition | |
2016 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ | |
2017 | flags |= wxTREE_HITTEST_##flag | |
2018 | ||
2019 | TRANSLATE_FLAG(ABOVE); | |
2020 | TRANSLATE_FLAG(BELOW); | |
2021 | TRANSLATE_FLAG(NOWHERE); | |
2022 | TRANSLATE_FLAG(ONITEMBUTTON); | |
2023 | TRANSLATE_FLAG(ONITEMICON); | |
2024 | TRANSLATE_FLAG(ONITEMINDENT); | |
2025 | TRANSLATE_FLAG(ONITEMLABEL); | |
2026 | TRANSLATE_FLAG(ONITEMRIGHT); | |
2027 | TRANSLATE_FLAG(ONITEMSTATEICON); | |
2028 | TRANSLATE_FLAG(TOLEFT); | |
2029 | TRANSLATE_FLAG(TORIGHT); | |
2bda0e17 | 2030 | |
08b7c251 VZ |
2031 | #undef TRANSLATE_FLAG |
2032 | ||
ee4b2721 | 2033 | return wxTreeItemId(hitTestInfo.hItem); |
08b7c251 VZ |
2034 | } |
2035 | ||
f7c832a7 VZ |
2036 | bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item, |
2037 | wxRect& rect, | |
2038 | bool textOnly) const | |
2039 | { | |
2040 | RECT rc; | |
f2c3db9d RD |
2041 | |
2042 | // Virtual root items have no bounding rectangle | |
2043 | if ( IS_VIRTUAL_ROOT(item) ) | |
2044 | { | |
2045 | return false; | |
2046 | } | |
2047 | ||
3f7bc32b | 2048 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(item), |
f7c832a7 VZ |
2049 | &rc, textOnly) ) |
2050 | { | |
2051 | rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom)); | |
2052 | ||
04cd30de | 2053 | return true; |
f7c832a7 VZ |
2054 | } |
2055 | else | |
2056 | { | |
2057 | // couldn't retrieve rect: for example, item isn't visible | |
04cd30de | 2058 | return false; |
f7c832a7 VZ |
2059 | } |
2060 | } | |
2061 | ||
7dac12ef VZ |
2062 | void wxTreeCtrl::ClearFocusedItem() |
2063 | { | |
2064 | TempSetter set(m_changingSelection); | |
2065 | ||
2066 | if ( !TreeView_SelectItem(GetHwnd(), 0) ) | |
2067 | { | |
2068 | wxLogLastError(wxT("TreeView_SelectItem")); | |
2069 | } | |
2070 | } | |
2071 | ||
2072 | void wxTreeCtrl::SetFocusedItem(const wxTreeItemId& item) | |
2073 | { | |
5708ae18 VZ |
2074 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
2075 | ||
7dac12ef VZ |
2076 | TempSetter set(m_changingSelection); |
2077 | ||
2078 | ::SetFocus(GetHwnd(), HITEM(item)); | |
2079 | } | |
2080 | ||
2081 | void wxTreeCtrl::DoUnselectItem(const wxTreeItemId& item) | |
2082 | { | |
2083 | TempSetter set(m_changingSelection); | |
2084 | ||
2085 | ::UnselectItem(GetHwnd(), HITEM(item)); | |
2086 | } | |
2087 | ||
2088 | void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId& item) | |
2089 | { | |
2090 | TempSetter set(m_changingSelection); | |
2091 | ||
2092 | ::ToggleItemSelection(GetHwnd(), HITEM(item)); | |
2093 | } | |
2094 | ||
23fd5130 VZ |
2095 | // ---------------------------------------------------------------------------- |
2096 | // sorting stuff | |
2097 | // ---------------------------------------------------------------------------- | |
f7c832a7 | 2098 | |
502a2b18 VZ |
2099 | // this is just a tiny namespace which is friend to wxTreeCtrl and so can use |
2100 | // functions such as IsDataIndirect() | |
2101 | class wxTreeSortHelper | |
2102 | { | |
2103 | public: | |
2104 | static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree); | |
2105 | ||
2106 | private: | |
4523ebb3 | 2107 | static wxTreeItemId GetIdFromData(LPARAM lParam) |
502a2b18 | 2108 | { |
4523ebb3 | 2109 | return ((wxTreeItemParam*)lParam)->GetItem(); |
502a2b18 | 2110 | } |
502a2b18 VZ |
2111 | }; |
2112 | ||
2113 | int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1, | |
2114 | LPARAM pItem2, | |
2115 | LPARAM htree) | |
23fd5130 | 2116 | { |
096c9f9b | 2117 | wxCHECK_MSG( pItem1 && pItem2, 0, |
223d09f6 | 2118 | wxT("sorting tree without data doesn't make sense") ); |
096c9f9b | 2119 | |
502a2b18 VZ |
2120 | wxTreeCtrl *tree = (wxTreeCtrl *)htree; |
2121 | ||
4523ebb3 VZ |
2122 | return tree->OnCompareItems(GetIdFromData(pItem1), |
2123 | GetIdFromData(pItem2)); | |
23fd5130 VZ |
2124 | } |
2125 | ||
95aabccc VZ |
2126 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item) |
2127 | { | |
05b2a432 RD |
2128 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
2129 | ||
95aabccc | 2130 | // rely on the fact that TreeView_SortChildren does the same thing as our |
23fd5130 VZ |
2131 | // default behaviour, i.e. sorts items alphabetically and so call it |
2132 | // directly if we're not in derived class (much more efficient!) | |
4523ebb3 VZ |
2133 | // RN: Note that if you find you're code doesn't sort as expected this |
2134 | // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS | |
2135 | // combo for your derived wxTreeCtrl if will sort without | |
2136 | // OnCompareItems | |
23fd5130 | 2137 | if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) ) |
2bda0e17 | 2138 | { |
3f7bc32b | 2139 | TreeView_SortChildren(GetHwnd(), HITEM(item), 0); |
2bda0e17 | 2140 | } |
08b7c251 | 2141 | else |
2bda0e17 | 2142 | { |
62448488 | 2143 | TV_SORTCB tvSort; |
3f7bc32b | 2144 | tvSort.hParent = HITEM(item); |
502a2b18 | 2145 | tvSort.lpfnCompare = wxTreeSortHelper::Compare; |
23fd5130 | 2146 | tvSort.lParam = (LPARAM)this; |
d220ae32 | 2147 | TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */); |
2bda0e17 | 2148 | } |
08b7c251 | 2149 | } |
2bda0e17 | 2150 | |
08b7c251 VZ |
2151 | // ---------------------------------------------------------------------------- |
2152 | // implementation | |
2153 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 2154 | |
90c6edd7 VZ |
2155 | bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG* msg) |
2156 | { | |
2157 | if ( msg->message == WM_KEYDOWN ) | |
2158 | { | |
6719c06a VZ |
2159 | // Only eat VK_RETURN if not being used by the application in |
2160 | // conjunction with modifiers | |
2161 | if ( (msg->wParam == VK_RETURN) && !wxIsAnyModifierDown() ) | |
90c6edd7 VZ |
2162 | { |
2163 | // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED | |
2164 | return false; | |
2165 | } | |
2166 | } | |
2167 | ||
2168 | return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg); | |
2169 | } | |
2170 | ||
0edeeb6d | 2171 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id_) |
08b7c251 | 2172 | { |
0edeeb6d VZ |
2173 | const int id = (signed short)id_; |
2174 | ||
08b7c251 | 2175 | if ( cmd == EN_UPDATE ) |
2bda0e17 | 2176 | { |
08b7c251 VZ |
2177 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); |
2178 | event.SetEventObject( this ); | |
2179 | ProcessCommand(event); | |
2bda0e17 | 2180 | } |
08b7c251 | 2181 | else if ( cmd == EN_KILLFOCUS ) |
2bda0e17 | 2182 | { |
08b7c251 VZ |
2183 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); |
2184 | event.SetEventObject( this ); | |
2185 | ProcessCommand(event); | |
2bda0e17 | 2186 | } |
08b7c251 | 2187 | else |
2bda0e17 | 2188 | { |
08b7c251 | 2189 | // nothing done |
04cd30de | 2190 | return false; |
2bda0e17 | 2191 | } |
08b7c251 VZ |
2192 | |
2193 | // command processed | |
04cd30de | 2194 | return true; |
08b7c251 VZ |
2195 | } |
2196 | ||
3c8cbc12 VZ |
2197 | bool wxTreeCtrl::MSWIsOnItem(unsigned flags) const |
2198 | { | |
2199 | unsigned mask = TVHT_ONITEM; | |
2200 | if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) ) | |
2201 | mask |= TVHT_ONITEMINDENT | TVHT_ONITEMRIGHT; | |
2202 | ||
2203 | return (flags & mask) != 0; | |
2204 | } | |
2205 | ||
25f701e6 | 2206 | bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey) |
c7d9c476 VZ |
2207 | { |
2208 | const bool bCtrl = wxIsCtrlDown(); | |
2209 | const bool bShift = wxIsShiftDown(); | |
2210 | const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd()); | |
2211 | ||
2212 | switch ( vkey ) | |
2213 | { | |
2214 | case VK_RETURN: | |
2215 | case VK_SPACE: | |
2216 | if ( !htSel ) | |
2217 | break; | |
2218 | ||
2219 | if ( vkey != VK_RETURN && bCtrl ) | |
2220 | { | |
2221 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2222 | this, htSel); | |
2223 | changingEvent.m_itemOld = htSel; | |
2224 | ||
2225 | if ( IsTreeEventAllowed(changingEvent) ) | |
2226 | { | |
7dac12ef | 2227 | DoToggleItemSelection(wxTreeItemId(htSel)); |
c7d9c476 VZ |
2228 | |
2229 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
2230 | this, htSel); | |
2231 | changedEvent.m_itemOld = htSel; | |
2232 | (void)HandleTreeEvent(changedEvent); | |
2233 | } | |
2234 | } | |
2235 | else | |
2236 | { | |
2237 | wxArrayTreeItemIds selections; | |
2238 | size_t count = GetSelections(selections); | |
2239 | ||
2240 | if ( count != 1 || HITEM(selections[0]) != htSel ) | |
2241 | { | |
2242 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2243 | this, htSel); | |
2244 | changingEvent.m_itemOld = htSel; | |
2245 | ||
2246 | if ( IsTreeEventAllowed(changingEvent) ) | |
2247 | { | |
2248 | DoUnselectAll(); | |
7dac12ef | 2249 | DoSelectItem(wxTreeItemId(htSel)); |
c7d9c476 VZ |
2250 | |
2251 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
2252 | this, htSel); | |
2253 | changedEvent.m_itemOld = htSel; | |
2254 | (void)HandleTreeEvent(changedEvent); | |
2255 | } | |
2256 | } | |
2257 | } | |
2258 | break; | |
2259 | ||
2260 | case VK_UP: | |
2261 | case VK_DOWN: | |
2262 | if ( !bCtrl && !bShift ) | |
2263 | { | |
2264 | wxArrayTreeItemIds selections; | |
c7d9c476 VZ |
2265 | wxTreeItemId next; |
2266 | ||
7dac12ef | 2267 | if ( htSel ) |
c7d9c476 VZ |
2268 | { |
2269 | next = vkey == VK_UP | |
2270 | ? TreeView_GetPrevVisible(GetHwnd(), htSel) | |
2271 | : TreeView_GetNextVisible(GetHwnd(), htSel); | |
2272 | } | |
2273 | else | |
2274 | { | |
2275 | next = GetRootItem(); | |
2276 | ||
2277 | if ( IsHiddenRoot(next) ) | |
2278 | next = TreeView_GetChild(GetHwnd(), HITEM(next)); | |
c7d9c476 VZ |
2279 | } |
2280 | ||
2281 | if ( !next.IsOk() ) | |
2282 | { | |
2283 | break; | |
2284 | } | |
2285 | ||
2286 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2287 | this, next); | |
2288 | changingEvent.m_itemOld = htSel; | |
2289 | ||
2290 | if ( IsTreeEventAllowed(changingEvent) ) | |
2291 | { | |
2292 | DoUnselectAll(); | |
7dac12ef VZ |
2293 | DoSelectItem(next); |
2294 | SetFocusedItem(next); | |
c7d9c476 VZ |
2295 | |
2296 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
2297 | this, next); | |
2298 | changedEvent.m_itemOld = htSel; | |
2299 | (void)HandleTreeEvent(changedEvent); | |
2300 | } | |
2301 | } | |
2302 | else if ( htSel ) | |
2303 | { | |
2304 | wxTreeItemId next = vkey == VK_UP | |
2305 | ? TreeView_GetPrevVisible(GetHwnd(), htSel) | |
2306 | : TreeView_GetNextVisible(GetHwnd(), htSel); | |
2307 | ||
2308 | if ( !next.IsOk() ) | |
2309 | { | |
2310 | break; | |
2311 | } | |
2312 | ||
2313 | if ( !m_htSelStart ) | |
2314 | { | |
2315 | m_htSelStart = htSel; | |
2316 | } | |
2317 | ||
2318 | if ( bShift && SelectRange(GetHwnd(), HITEM(m_htSelStart), HITEM(next), | |
2319 | SR_UNSELECT_OTHERS | SR_SIMULATE) ) | |
2320 | { | |
2321 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next); | |
2322 | changingEvent.m_itemOld = htSel; | |
2323 | ||
2324 | if ( IsTreeEventAllowed(changingEvent) ) | |
2325 | { | |
2326 | SelectRange(GetHwnd(), HITEM(m_htSelStart), HITEM(next), | |
2327 | SR_UNSELECT_OTHERS); | |
2328 | ||
2329 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next); | |
2330 | changedEvent.m_itemOld = htSel; | |
2331 | (void)HandleTreeEvent(changedEvent); | |
2332 | } | |
2333 | } | |
2334 | ||
7dac12ef | 2335 | SetFocusedItem(next); |
c7d9c476 VZ |
2336 | } |
2337 | break; | |
2338 | ||
2339 | case VK_LEFT: | |
2340 | if ( HasChildren(htSel) && IsExpanded(htSel) ) | |
2341 | { | |
2342 | Collapse(htSel); | |
2343 | } | |
2344 | else | |
2345 | { | |
2346 | wxTreeItemId next = GetItemParent(htSel); | |
2347 | ||
2348 | if ( next.IsOk() && !IsHiddenRoot(next) ) | |
2349 | { | |
2350 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2351 | this, next); | |
2352 | changingEvent.m_itemOld = htSel; | |
2353 | ||
2354 | if ( IsTreeEventAllowed(changingEvent) ) | |
2355 | { | |
2356 | DoUnselectAll(); | |
7dac12ef VZ |
2357 | DoSelectItem(next); |
2358 | SetFocusedItem(next); | |
c7d9c476 VZ |
2359 | |
2360 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
2361 | this, next); | |
2362 | changedEvent.m_itemOld = htSel; | |
2363 | (void)HandleTreeEvent(changedEvent); | |
2364 | } | |
2365 | } | |
2366 | } | |
2367 | break; | |
2368 | ||
2369 | case VK_RIGHT: | |
2370 | if ( !IsVisible(htSel) ) | |
2371 | { | |
2372 | EnsureVisible(htSel); | |
2373 | } | |
2374 | ||
2375 | if ( !HasChildren(htSel) ) | |
2376 | break; | |
2377 | ||
2378 | if ( !IsExpanded(htSel) ) | |
2379 | { | |
2380 | Expand(htSel); | |
2381 | } | |
2382 | else | |
2383 | { | |
2384 | wxTreeItemId next = TreeView_GetChild(GetHwnd(), htSel); | |
2385 | ||
2386 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next); | |
2387 | changingEvent.m_itemOld = htSel; | |
2388 | ||
2389 | if ( IsTreeEventAllowed(changingEvent) ) | |
2390 | { | |
2391 | DoUnselectAll(); | |
7dac12ef VZ |
2392 | DoSelectItem(next); |
2393 | SetFocusedItem(next); | |
c7d9c476 VZ |
2394 | |
2395 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next); | |
2396 | changedEvent.m_itemOld = htSel; | |
2397 | (void)HandleTreeEvent(changedEvent); | |
2398 | } | |
2399 | } | |
2400 | break; | |
2401 | ||
2402 | case VK_HOME: | |
2403 | case VK_END: | |
2404 | { | |
2405 | wxTreeItemId next = GetRootItem(); | |
2406 | ||
2407 | if ( IsHiddenRoot(next) ) | |
2408 | { | |
2409 | next = TreeView_GetChild(GetHwnd(), HITEM(next)); | |
2410 | } | |
2411 | ||
2412 | if ( !next.IsOk() ) | |
2413 | break; | |
2414 | ||
2415 | if ( vkey == VK_END ) | |
2416 | { | |
2417 | for ( ;; ) | |
2418 | { | |
2419 | wxTreeItemId nextTemp = TreeView_GetNextVisible( | |
2420 | GetHwnd(), HITEM(next)); | |
2421 | ||
2422 | if ( !nextTemp.IsOk() ) | |
2423 | break; | |
2424 | ||
2425 | next = nextTemp; | |
2426 | } | |
2427 | } | |
2428 | ||
2429 | if ( htSel == HITEM(next) ) | |
2430 | break; | |
2431 | ||
2432 | if ( bShift ) | |
2433 | { | |
2434 | if ( !m_htSelStart ) | |
2435 | { | |
2436 | m_htSelStart = htSel; | |
2437 | } | |
2438 | ||
2439 | if ( SelectRange(GetHwnd(), | |
2440 | HITEM(m_htSelStart), HITEM(next), | |
2441 | SR_UNSELECT_OTHERS | SR_SIMULATE) ) | |
2442 | { | |
2443 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2444 | this, next); | |
2445 | changingEvent.m_itemOld = htSel; | |
2446 | ||
2447 | if ( IsTreeEventAllowed(changingEvent) ) | |
2448 | { | |
2449 | SelectRange(GetHwnd(), | |
2450 | HITEM(m_htSelStart), HITEM(next), | |
2451 | SR_UNSELECT_OTHERS); | |
7dac12ef | 2452 | SetFocusedItem(next); |
c7d9c476 VZ |
2453 | |
2454 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
2455 | this, next); | |
2456 | changedEvent.m_itemOld = htSel; | |
2457 | (void)HandleTreeEvent(changedEvent); | |
2458 | } | |
2459 | } | |
2460 | } | |
2461 | else // no Shift | |
2462 | { | |
2463 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2464 | this, next); | |
2465 | changingEvent.m_itemOld = htSel; | |
2466 | ||
2467 | if ( IsTreeEventAllowed(changingEvent) ) | |
2468 | { | |
2469 | DoUnselectAll(); | |
7dac12ef VZ |
2470 | DoSelectItem(next); |
2471 | SetFocusedItem(next); | |
c7d9c476 VZ |
2472 | |
2473 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
2474 | this, next); | |
2475 | changedEvent.m_itemOld = htSel; | |
2476 | (void)HandleTreeEvent(changedEvent); | |
2477 | } | |
2478 | } | |
2479 | } | |
2480 | break; | |
2481 | ||
2482 | case VK_PRIOR: | |
2483 | case VK_NEXT: | |
2484 | if ( bCtrl ) | |
2485 | { | |
2486 | wxTreeItemId firstVisible = GetFirstVisibleItem(); | |
2487 | size_t visibleCount = TreeView_GetVisibleCount(GetHwnd()); | |
2488 | wxTreeItemId nextAdjacent = (vkey == VK_PRIOR) ? | |
2489 | TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible)) : | |
2490 | TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible)); | |
2491 | ||
2492 | if ( !nextAdjacent ) | |
2493 | { | |
2494 | break; | |
2495 | } | |
2496 | ||
2497 | wxTreeItemId nextStart = firstVisible; | |
2498 | ||
2499 | for ( size_t n = 1; n < visibleCount; n++ ) | |
2500 | { | |
2501 | wxTreeItemId nextTemp = (vkey == VK_PRIOR) ? | |
2502 | TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart)) : | |
2503 | TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart)); | |
2504 | ||
2505 | if ( nextTemp.IsOk() ) | |
2506 | { | |
2507 | nextStart = nextTemp; | |
2508 | } | |
2509 | else | |
2510 | { | |
2511 | break; | |
2512 | } | |
2513 | } | |
2514 | ||
2515 | EnsureVisible(nextStart); | |
2516 | ||
2517 | if ( vkey == VK_NEXT ) | |
2518 | { | |
2519 | wxTreeItemId nextEnd = nextStart; | |
2520 | ||
2521 | for ( size_t n = 1; n < visibleCount; n++ ) | |
2522 | { | |
2523 | wxTreeItemId nextTemp = | |
2524 | TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd)); | |
2525 | ||
2526 | if ( nextTemp.IsOk() ) | |
2527 | { | |
2528 | nextEnd = nextTemp; | |
2529 | } | |
2530 | else | |
2531 | { | |
2532 | break; | |
2533 | } | |
2534 | } | |
2535 | ||
2536 | EnsureVisible(nextEnd); | |
2537 | } | |
2538 | } | |
2539 | else // no Ctrl | |
2540 | { | |
2541 | size_t visibleCount = TreeView_GetVisibleCount(GetHwnd()); | |
2542 | wxTreeItemId nextAdjacent = (vkey == VK_PRIOR) ? | |
2543 | TreeView_GetPrevVisible(GetHwnd(), htSel) : | |
2544 | TreeView_GetNextVisible(GetHwnd(), htSel); | |
2545 | ||
2546 | if ( !nextAdjacent ) | |
2547 | { | |
2548 | break; | |
2549 | } | |
2550 | ||
2551 | wxTreeItemId next(htSel); | |
2552 | ||
2553 | for ( size_t n = 1; n < visibleCount; n++ ) | |
2554 | { | |
2555 | wxTreeItemId nextTemp = vkey == VK_PRIOR ? | |
2556 | TreeView_GetPrevVisible(GetHwnd(), HITEM(next)) : | |
2557 | TreeView_GetNextVisible(GetHwnd(), HITEM(next)); | |
2558 | ||
2559 | if ( !nextTemp.IsOk() ) | |
2560 | break; | |
2561 | ||
2562 | next = nextTemp; | |
2563 | } | |
2564 | ||
2565 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2566 | this, next); | |
2567 | changingEvent.m_itemOld = htSel; | |
2568 | ||
2569 | if ( IsTreeEventAllowed(changingEvent) ) | |
2570 | { | |
2571 | DoUnselectAll(); | |
2572 | m_htSelStart.Unset(); | |
7dac12ef VZ |
2573 | DoSelectItem(next); |
2574 | SetFocusedItem(next); | |
c7d9c476 VZ |
2575 | |
2576 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
2577 | this, next); | |
2578 | changedEvent.m_itemOld = htSel; | |
2579 | (void)HandleTreeEvent(changedEvent); | |
2580 | } | |
2581 | } | |
2582 | break; | |
2583 | ||
2584 | default: | |
2585 | return false; | |
2586 | } | |
2587 | ||
2588 | return true; | |
2589 | } | |
2590 | ||
aa8166dd VZ |
2591 | bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam, WXLPARAM lParam) |
2592 | { | |
2593 | wxTreeEvent keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN, this); | |
b6885972 | 2594 | keyEvent.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, wParam, lParam); |
aa8166dd VZ |
2595 | |
2596 | bool processed = HandleTreeEvent(keyEvent); | |
2597 | ||
2598 | // generate a separate event for Space/Return | |
2599 | if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() && | |
2600 | ((wParam == VK_SPACE) || (wParam == VK_RETURN)) ) | |
2601 | { | |
2602 | const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd()); | |
2603 | if ( htSel ) | |
2604 | { | |
2605 | wxTreeEvent activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, | |
2606 | this, htSel); | |
2607 | (void)HandleTreeEvent(activatedEvent); | |
2608 | } | |
2609 | } | |
2610 | ||
2611 | return processed; | |
2612 | } | |
2613 | ||
23f681ec VZ |
2614 | // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we |
2615 | // only do it during dragging, minimize wxWin overhead (this is important for | |
2616 | // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly | |
2617 | // instead of passing by wxWin events | |
c7d9c476 VZ |
2618 | WXLRESULT |
2619 | wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
23f681ec | 2620 | { |
04cd30de | 2621 | bool processed = false; |
c140b7e7 | 2622 | WXLRESULT rc = 0; |
2b9a7d4c | 2623 | bool isMultiple = HasFlag(wxTR_MULTIPLE); |
3f7bc32b | 2624 | |
499bbaeb | 2625 | if ( nMsg == WM_CONTEXTMENU ) |
30f5e34f | 2626 | { |
147964ca KH |
2627 | int x = GET_X_LPARAM(lParam), |
2628 | y = GET_Y_LPARAM(lParam); | |
8f6dc819 VZ |
2629 | |
2630 | // the item for which the menu should be shown | |
2631 | wxTreeItemId item; | |
2632 | ||
2633 | // the position where the menu should be shown in client coordinates | |
2634 | // (so that it can be passed directly to PopupMenu()) | |
2635 | wxPoint pt; | |
2636 | ||
2637 | if ( x == -1 || y == -1 ) | |
147964ca | 2638 | { |
8f6dc819 VZ |
2639 | // this means that the event was generated from keyboard (e.g. with |
2640 | // Shift-F10 or special Windows menu key) | |
2641 | // | |
2642 | // use the Explorer standard of putting the menu at the left edge | |
2643 | // of the text, in the vertical middle of the text | |
2644 | item = wxTreeItemId(TreeView_GetSelection(GetHwnd())); | |
2645 | if ( item.IsOk() ) | |
2646 | { | |
2647 | // Use the bounding rectangle of only the text part | |
2648 | wxRect rect; | |
2649 | GetBoundingRect(item, rect, true); | |
2650 | pt = wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight() / 2); | |
2651 | } | |
147964ca | 2652 | } |
8f6dc819 | 2653 | else // event from mouse, use mouse position |
147964ca | 2654 | { |
8f6dc819 VZ |
2655 | pt = ScreenToClient(wxPoint(x, y)); |
2656 | ||
2657 | TV_HITTESTINFO tvhti; | |
2658 | tvhti.pt.x = pt.x; | |
2659 | tvhti.pt.y = pt.y; | |
c7d9c476 | 2660 | |
8f6dc819 VZ |
2661 | if ( TreeView_HitTest(GetHwnd(), &tvhti) ) |
2662 | item = wxTreeItemId(tvhti.hItem); | |
147964ca KH |
2663 | } |
2664 | ||
8f6dc819 | 2665 | // create the event |
22993989 VZ |
2666 | if ( item.IsOk() ) |
2667 | { | |
2668 | wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this, item); | |
8f6dc819 | 2669 | |
22993989 | 2670 | event.m_pointDrag = pt; |
8f6dc819 | 2671 | |
22993989 VZ |
2672 | if ( HandleTreeEvent(event) ) |
2673 | processed = true; | |
2674 | //else: continue with generating wxEVT_CONTEXT_MENU in base class code | |
2675 | } | |
30f5e34f | 2676 | } |
2b9a7d4c | 2677 | else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) ) |
23f681ec | 2678 | { |
7bb8798c VZ |
2679 | // we only process mouse messages here and these parameters have the |
2680 | // same meaning for all of them | |
3f7bc32b VZ |
2681 | int x = GET_X_LPARAM(lParam), |
2682 | y = GET_Y_LPARAM(lParam); | |
8ecd5de2 | 2683 | |
e3ad5702 JS |
2684 | TV_HITTESTINFO tvht; |
2685 | tvht.pt.x = x; | |
2686 | tvht.pt.y = y; | |
8ecd5de2 | 2687 | |
c7d9c476 | 2688 | HTREEITEM htOldItem = TreeView_GetSelection(GetHwnd()); |
8854a202 | 2689 | HTREEITEM htItem = TreeView_HitTest(GetHwnd(), &tvht); |
3f7bc32b | 2690 | |
23f681ec VZ |
2691 | switch ( nMsg ) |
2692 | { | |
3f7bc32b | 2693 | case WM_LBUTTONDOWN: |
c7d9c476 VZ |
2694 | if ( !isMultiple ) |
2695 | break; | |
2696 | ||
c7d9c476 VZ |
2697 | m_htClickedItem.Unset(); |
2698 | ||
3c8cbc12 | 2699 | if ( !MSWIsOnItem(tvht.flags) ) |
23f681ec | 2700 | { |
f2fec40d | 2701 | if ( tvht.flags & TVHT_ONITEMBUTTON ) |
c7d9c476 | 2702 | { |
f2fec40d VZ |
2703 | // either it's going to be handled by user code or |
2704 | // we're going to use it ourselves to toggle the | |
2705 | // branch, in either case don't pass it to the base | |
2706 | // class which would generate another mouse click event | |
2707 | // for it even though it's already handled here | |
2708 | processed = true; | |
2709 | SetFocus(); | |
2710 | ||
2711 | if ( !HandleMouseEvent(nMsg, x, y, wParam) ) | |
c7d9c476 VZ |
2712 | { |
2713 | if ( !IsExpanded(htItem) ) | |
2714 | { | |
2715 | Expand(htItem); | |
2716 | } | |
2717 | else | |
2718 | { | |
2719 | Collapse(htItem); | |
2720 | } | |
2721 | } | |
2722 | } | |
8ecd5de2 | 2723 | |
7dac12ef | 2724 | m_focusLost = false; |
c7d9c476 VZ |
2725 | break; |
2726 | } | |
2727 | ||
7dac12ef | 2728 | processed = true; |
c7d9c476 VZ |
2729 | SetFocus(); |
2730 | m_htClickedItem = (WXHTREEITEM) htItem; | |
2731 | m_ptClick = wxPoint(x, y); | |
2732 | ||
2733 | if ( wParam & MK_CONTROL ) | |
2734 | { | |
2735 | if ( HandleMouseEvent(nMsg, x, y, wParam) ) | |
3f7bc32b | 2736 | { |
c7d9c476 VZ |
2737 | m_htClickedItem.Unset(); |
2738 | break; | |
2739 | } | |
23f681ec | 2740 | |
c7d9c476 VZ |
2741 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, |
2742 | this, htItem); | |
2743 | changingEvent.m_itemOld = htOldItem; | |
2744 | ||
2745 | if ( IsTreeEventAllowed(changingEvent) ) | |
2746 | { | |
3f7bc32b | 2747 | // toggle selected state |
7dac12ef | 2748 | DoToggleItemSelection(wxTreeItemId(htItem)); |
3f7bc32b | 2749 | |
7dac12ef | 2750 | SetFocusedItem(wxTreeItemId(htItem)); |
3f7bc32b VZ |
2751 | |
2752 | // reset on any click without Shift | |
f888d614 | 2753 | m_htSelStart.Unset(); |
3f7bc32b | 2754 | |
c7d9c476 VZ |
2755 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, |
2756 | this, htItem); | |
2757 | changedEvent.m_itemOld = htOldItem; | |
2758 | (void)HandleTreeEvent(changedEvent); | |
3f7bc32b | 2759 | } |
c7d9c476 VZ |
2760 | } |
2761 | else if ( wParam & MK_SHIFT ) | |
2762 | { | |
2763 | if ( HandleMouseEvent(nMsg, x, y, wParam) ) | |
3f7bc32b | 2764 | { |
c7d9c476 VZ |
2765 | m_htClickedItem.Unset(); |
2766 | break; | |
2767 | } | |
3f7bc32b | 2768 | |
c7d9c476 VZ |
2769 | int srFlags = 0; |
2770 | bool willChange = true; | |
3f7bc32b | 2771 | |
c7d9c476 VZ |
2772 | if ( !(wParam & MK_CONTROL) ) |
2773 | { | |
2774 | srFlags |= SR_UNSELECT_OTHERS; | |
2775 | } | |
23f681ec | 2776 | |
c7d9c476 VZ |
2777 | if ( !m_htSelStart ) |
2778 | { | |
2779 | // take the focused item | |
2780 | m_htSelStart = htOldItem; | |
3f7bc32b | 2781 | } |
c7d9c476 | 2782 | else |
3f7bc32b | 2783 | { |
c7d9c476 VZ |
2784 | willChange = SelectRange(GetHwnd(), HITEM(m_htSelStart), |
2785 | htItem, srFlags | SR_SIMULATE); | |
2786 | } | |
8ecd5de2 | 2787 | |
c7d9c476 VZ |
2788 | if ( willChange ) |
2789 | { | |
2790 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2791 | this, htItem); | |
2792 | changingEvent.m_itemOld = htOldItem; | |
5e7718a2 | 2793 | |
c7d9c476 | 2794 | if ( IsTreeEventAllowed(changingEvent) ) |
df4ac4c7 | 2795 | { |
c7d9c476 VZ |
2796 | // this selects all items between the starting one |
2797 | // and the current | |
2798 | if ( m_htSelStart ) | |
2799 | { | |
2800 | SelectRange(GetHwnd(), HITEM(m_htSelStart), | |
2801 | htItem, srFlags); | |
2802 | } | |
2803 | else | |
35cf1ec6 | 2804 | { |
7dac12ef | 2805 | DoSelectItem(wxTreeItemId(htItem)); |
c7d9c476 | 2806 | } |
35cf1ec6 | 2807 | |
7dac12ef | 2808 | SetFocusedItem(wxTreeItemId(htItem)); |
35cf1ec6 | 2809 | |
c7d9c476 VZ |
2810 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, |
2811 | this, htItem); | |
2812 | changedEvent.m_itemOld = htOldItem; | |
2813 | (void)HandleTreeEvent(changedEvent); | |
2814 | } | |
2815 | } | |
2816 | } | |
2817 | else // normal click | |
2818 | { | |
2819 | // avoid doing anything if we click on the only | |
2820 | // currently selected item | |
2821 | ||
2822 | wxArrayTreeItemIds selections; | |
2823 | size_t count = GetSelections(selections); | |
2824 | ||
2825 | if ( count == 0 || | |
2826 | count > 1 || | |
2827 | HITEM(selections[0]) != htItem ) | |
2828 | { | |
2829 | if ( HandleMouseEvent(nMsg, x, y, wParam) ) | |
2830 | { | |
2831 | m_htClickedItem.Unset(); | |
2832 | break; | |
2833 | } | |
2834 | ||
2835 | // clear the previously selected items, if the user | |
2836 | // clicked outside of the present selection, otherwise, | |
2837 | // perform the deselection on mouse-up, this allows | |
2838 | // multiple drag and drop to work. | |
c7d9c476 VZ |
2839 | if ( !IsItemSelected(GetHwnd(), htItem)) |
2840 | { | |
2841 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, | |
2842 | this, htItem); | |
2843 | changingEvent.m_itemOld = htOldItem; | |
2844 | ||
2845 | if ( IsTreeEventAllowed(changingEvent) ) | |
2846 | { | |
2847 | DoUnselectAll(); | |
7dac12ef VZ |
2848 | DoSelectItem(wxTreeItemId(htItem)); |
2849 | SetFocusedItem(wxTreeItemId(htItem)); | |
c7d9c476 VZ |
2850 | |
2851 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
2852 | this, htItem); | |
2853 | changedEvent.m_itemOld = htOldItem; | |
2854 | (void)HandleTreeEvent(changedEvent); | |
35cf1ec6 | 2855 | } |
c7d9c476 VZ |
2856 | } |
2857 | else | |
2858 | { | |
7dac12ef | 2859 | SetFocusedItem(wxTreeItemId(htItem)); |
d301c440 | 2860 | m_mouseUpDeselect = true; |
df4ac4c7 | 2861 | } |
c7d9c476 VZ |
2862 | } |
2863 | else // click on a single selected item | |
2864 | { | |
2865 | // don't interfere with the default processing in | |
2866 | // WM_MOUSEMOVE handler below as the default window | |
2867 | // proc will start the drag itself if we let have | |
2868 | // WM_LBUTTONDOWN | |
2869 | m_htClickedItem.Unset(); | |
2870 | ||
2871 | // prevent in-place editing from starting if focus lost | |
2872 | // since previous click | |
2873 | if ( m_focusLost ) | |
b8e3f1cf | 2874 | { |
7dac12ef VZ |
2875 | ClearFocusedItem(); |
2876 | DoSelectItem(wxTreeItemId(htItem)); | |
2877 | SetFocusedItem(wxTreeItemId(htItem)); | |
2878 | } | |
2879 | else | |
2880 | { | |
2881 | processed = false; | |
b8e3f1cf | 2882 | } |
c7d9c476 VZ |
2883 | } |
2884 | ||
2885 | // reset on any click without Shift | |
2886 | m_htSelStart.Unset(); | |
2887 | } | |
2888 | ||
2889 | m_focusLost = false; | |
2890 | ||
2891 | // we consumed the event so we need to trigger state image | |
2892 | // click if needed | |
2893 | if ( processed ) | |
2894 | { | |
2895 | int htFlags = 0; | |
2896 | wxTreeItemId item = HitTest(wxPoint(x, y), htFlags); | |
2897 | ||
2898 | if ( htFlags & wxTREE_HITTEST_ONITEMSTATEICON ) | |
2899 | { | |
2900 | m_triggerStateImageClick = true; | |
3f7bc32b VZ |
2901 | } |
2902 | } | |
2903 | break; | |
3f7bc32b | 2904 | |
e0bf68d6 | 2905 | case WM_RBUTTONDOWN: |
c7d9c476 VZ |
2906 | if ( !isMultiple ) |
2907 | break; | |
2908 | ||
2909 | processed = true; | |
2910 | SetFocus(); | |
2911 | ||
2912 | if ( HandleMouseEvent(nMsg, x, y, wParam) || !htItem ) | |
2913 | { | |
2914 | break; | |
2915 | } | |
2916 | ||
e0bf68d6 VZ |
2917 | // default handler removes the highlight from the currently |
2918 | // focused item when right mouse button is pressed on another | |
2919 | // one but keeps the remaining items highlighted, which is | |
c7d9c476 VZ |
2920 | // confusing, so override this default behaviour |
2921 | if ( !IsItemSelected(GetHwnd(), htItem) ) | |
e0bf68d6 | 2922 | { |
c7d9c476 VZ |
2923 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, |
2924 | this, htItem); | |
2925 | changingEvent.m_itemOld = htOldItem; | |
2926 | ||
2927 | if ( IsTreeEventAllowed(changingEvent) ) | |
e0bf68d6 | 2928 | { |
c7d9c476 | 2929 | DoUnselectAll(); |
7dac12ef VZ |
2930 | DoSelectItem(wxTreeItemId(htItem)); |
2931 | SetFocusedItem(wxTreeItemId(htItem)); | |
e0bf68d6 | 2932 | |
c7d9c476 VZ |
2933 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, |
2934 | this, htItem); | |
2935 | changedEvent.m_itemOld = htOldItem; | |
2936 | (void)HandleTreeEvent(changedEvent); | |
2937 | } | |
e0bf68d6 | 2938 | } |
c7d9c476 | 2939 | |
e0bf68d6 VZ |
2940 | break; |
2941 | ||
3f7bc32b | 2942 | case WM_MOUSEMOVE: |
68fc69f3 | 2943 | #ifndef __WXWINCE__ |
e3ad5702 JS |
2944 | if ( m_htClickedItem ) |
2945 | { | |
2946 | int cx = abs(m_ptClick.x - x); | |
2947 | int cy = abs(m_ptClick.y - y); | |
2948 | ||
b8e3f1cf VZ |
2949 | if ( cx > ::GetSystemMetrics(SM_CXDRAG) || |
2950 | cy > ::GetSystemMetrics(SM_CYDRAG) ) | |
e3ad5702 | 2951 | { |
b8e3f1cf VZ |
2952 | NM_TREEVIEW tv; |
2953 | wxZeroMemory(tv); | |
e3ad5702 | 2954 | |
b8e3f1cf VZ |
2955 | tv.hdr.hwndFrom = GetHwnd(); |
2956 | tv.hdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID); | |
2957 | tv.hdr.code = TVN_BEGINDRAG; | |
8ecd5de2 | 2958 | |
b8e3f1cf | 2959 | tv.itemNew.hItem = HITEM(m_htClickedItem); |
8ecd5de2 | 2960 | |
8ecd5de2 | 2961 | |
b8e3f1cf VZ |
2962 | TVITEM tviAux; |
2963 | wxZeroMemory(tviAux); | |
8ecd5de2 | 2964 | |
b8e3f1cf VZ |
2965 | tviAux.hItem = HITEM(m_htClickedItem); |
2966 | tviAux.mask = TVIF_STATE | TVIF_PARAM; | |
2967 | tviAux.stateMask = 0xffffffff; | |
2968 | TreeView_GetItem(GetHwnd(), &tviAux); | |
8ecd5de2 | 2969 | |
b8e3f1cf VZ |
2970 | tv.itemNew.state = tviAux.state; |
2971 | tv.itemNew.lParam = tviAux.lParam; | |
2972 | ||
2973 | tv.ptDrag.x = x; | |
2974 | tv.ptDrag.y = y; | |
2975 | ||
2976 | // do it before SendMessage() call below to avoid | |
2977 | // reentrancies here if there is another WM_MOUSEMOVE | |
2978 | // in the queue already | |
e3ad5702 | 2979 | m_htClickedItem.Unset(); |
b8e3f1cf VZ |
2980 | |
2981 | ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY, | |
2982 | tv.hdr.idFrom, (LPARAM)&tv ); | |
2983 | ||
2984 | // don't pass it to the default window proc, it would | |
2985 | // start dragging again | |
2986 | processed = true; | |
e3ad5702 | 2987 | } |
e3ad5702 | 2988 | } |
68fc69f3 | 2989 | #endif // __WXWINCE__ |
8ecd5de2 | 2990 | |
68d9be05 | 2991 | #if wxUSE_DRAGIMAGE |
3f7bc32b VZ |
2992 | if ( m_dragImage ) |
2993 | { | |
afff720b | 2994 | m_dragImage->Move(wxPoint(x, y)); |
3f7bc32b | 2995 | if ( htItem ) |
23f681ec VZ |
2996 | { |
2997 | // highlight the item as target (hiding drag image is | |
2998 | // necessary - otherwise the display will be corrupted) | |
68be9f09 | 2999 | m_dragImage->Hide(); |
3f7bc32b | 3000 | TreeView_SelectDropTarget(GetHwnd(), htItem); |
68be9f09 | 3001 | m_dragImage->Show(); |
23f681ec VZ |
3002 | } |
3003 | } | |
68d9be05 | 3004 | #endif // wxUSE_DRAGIMAGE |
23f681ec VZ |
3005 | break; |
3006 | ||
3007 | case WM_LBUTTONUP: | |
c7d9c476 | 3008 | if ( isMultiple ) |
35cf1ec6 | 3009 | { |
d301c440 | 3010 | // deselect other items if needed |
c7d9c476 | 3011 | if ( htItem ) |
35cf1ec6 | 3012 | { |
d301c440 | 3013 | if ( m_mouseUpDeselect ) |
c7d9c476 | 3014 | { |
d301c440 VZ |
3015 | m_mouseUpDeselect = false; |
3016 | ||
c7d9c476 VZ |
3017 | wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, |
3018 | this, htItem); | |
3019 | changingEvent.m_itemOld = htOldItem; | |
3020 | ||
3021 | if ( IsTreeEventAllowed(changingEvent) ) | |
3022 | { | |
3023 | DoUnselectAll(); | |
7dac12ef VZ |
3024 | DoSelectItem(wxTreeItemId(htItem)); |
3025 | SetFocusedItem(wxTreeItemId(htItem)); | |
c7d9c476 VZ |
3026 | |
3027 | wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, | |
3028 | this, htItem); | |
3029 | changedEvent.m_itemOld = htOldItem; | |
3030 | (void)HandleTreeEvent(changedEvent); | |
3031 | } | |
3032 | } | |
35cf1ec6 | 3033 | } |
c7d9c476 | 3034 | |
e3ad5702 | 3035 | m_htClickedItem.Unset(); |
c7d9c476 VZ |
3036 | |
3037 | if ( m_triggerStateImageClick ) | |
3038 | { | |
3039 | if ( tvht.flags & TVHT_ONITEMSTATEICON ) | |
3040 | { | |
3041 | wxTreeEvent event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK, | |
3042 | this, htItem); | |
3043 | (void)HandleTreeEvent(event); | |
3044 | ||
3045 | m_triggerStateImageClick = false; | |
3046 | processed = true; | |
3047 | } | |
3048 | } | |
3049 | ||
3c8cbc12 | 3050 | if ( !m_dragStarted && MSWIsOnItem(tvht.flags) ) |
c7d9c476 VZ |
3051 | { |
3052 | processed = true; | |
3053 | } | |
35cf1ec6 VZ |
3054 | } |
3055 | ||
3056 | // fall through | |
3057 | ||
23f681ec | 3058 | case WM_RBUTTONUP: |
68d9be05 | 3059 | #if wxUSE_DRAGIMAGE |
3f7bc32b | 3060 | if ( m_dragImage ) |
23f681ec | 3061 | { |
68be9f09 | 3062 | m_dragImage->EndDrag(); |
5276b0a5 | 3063 | wxDELETE(m_dragImage); |
23f681ec VZ |
3064 | |
3065 | // generate the drag end event | |
c7d9c476 VZ |
3066 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, |
3067 | this, htItem); | |
72e61024 | 3068 | event.m_pointDrag = wxPoint(x, y); |
c7d9c476 | 3069 | (void)HandleTreeEvent(event); |
225fe9d6 VZ |
3070 | |
3071 | // if we don't do it, the tree seems to think that 2 items | |
3072 | // are selected simultaneously which is quite weird | |
3073 | TreeView_SelectDropTarget(GetHwnd(), 0); | |
23f681ec | 3074 | } |
68d9be05 | 3075 | #endif // wxUSE_DRAGIMAGE |
c7d9c476 VZ |
3076 | |
3077 | if ( isMultiple && nMsg == WM_RBUTTONUP ) | |
3078 | { | |
3079 | // send NM_RCLICK | |
3080 | NMHDR nmhdr; | |
3081 | nmhdr.hwndFrom = GetHwnd(); | |
3082 | nmhdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID); | |
3083 | nmhdr.code = NM_RCLICK; | |
3084 | ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY, | |
3085 | nmhdr.idFrom, (LPARAM)&nmhdr); | |
3086 | processed = true; | |
3087 | } | |
3088 | ||
3089 | m_dragStarted = false; | |
3090 | ||
23f681ec VZ |
3091 | break; |
3092 | } | |
3093 | } | |
c7d9c476 | 3094 | else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) ) |
3f7bc32b | 3095 | { |
c7d9c476 | 3096 | if ( isMultiple ) |
3f7bc32b | 3097 | { |
c7d9c476 VZ |
3098 | // the tree control greys out the selected item when it loses focus |
3099 | // and paints it as selected again when it regains it, but it won't | |
3100 | // do it for the other items itself - help it | |
3101 | wxArrayTreeItemIds selections; | |
3102 | size_t count = GetSelections(selections); | |
3103 | RECT rect; | |
3104 | ||
3105 | for ( size_t n = 0; n < count; n++ ) | |
3f7bc32b | 3106 | { |
c7d9c476 VZ |
3107 | // TreeView_GetItemRect() will return false if item is not |
3108 | // visible, which may happen perfectly well | |
3109 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]), | |
3110 | &rect, TRUE) ) | |
3111 | { | |
3112 | ::InvalidateRect(GetHwnd(), &rect, FALSE); | |
3113 | } | |
3f7bc32b VZ |
3114 | } |
3115 | } | |
3f7bc32b | 3116 | |
c7d9c476 | 3117 | if ( nMsg == WM_KILLFOCUS ) |
3f7bc32b | 3118 | { |
c7d9c476 VZ |
3119 | m_focusLost = true; |
3120 | } | |
3121 | } | |
3122 | else if ( (nMsg == WM_KEYDOWN || nMsg == WM_SYSKEYDOWN) && isMultiple ) | |
3123 | { | |
aa8166dd VZ |
3124 | // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN |
3125 | // notification but for the keys which can be used to change selection | |
3126 | // we need to do it from here so as to not apply the default behaviour | |
3127 | // if the events are handled by the user code | |
3128 | switch ( wParam ) | |
c7d9c476 | 3129 | { |
aa8166dd VZ |
3130 | case VK_RETURN: |
3131 | case VK_SPACE: | |
3132 | case VK_UP: | |
3133 | case VK_DOWN: | |
3134 | case VK_LEFT: | |
3135 | case VK_RIGHT: | |
3136 | case VK_HOME: | |
3137 | case VK_END: | |
3138 | case VK_PRIOR: | |
3139 | case VK_NEXT: | |
d6d857b1 VZ |
3140 | if ( !HandleKeyDown(wParam, lParam) && |
3141 | !MSWHandleTreeKeyDownEvent(wParam, lParam) ) | |
aa8166dd VZ |
3142 | { |
3143 | // use the key to update the selection if it was left | |
3144 | // unprocessed | |
3145 | MSWHandleSelectionKey(wParam); | |
aa8166dd | 3146 | } |
ba8a1961 | 3147 | |
7dac12ef VZ |
3148 | // pretend that we did process it in any case as we already |
3149 | // generated an event for it | |
3150 | processed = true; | |
3151 | ||
aa8166dd VZ |
3152 | //default: for all the other keys leave processed as false so that |
3153 | // the tree control generates a TVN_KEYDOWN for us | |
c7d9c476 | 3154 | } |
3f7bc32b | 3155 | |
3f7bc32b | 3156 | } |
d35dce3a VZ |
3157 | else if ( nMsg == WM_COMMAND ) |
3158 | { | |
3159 | // if we receive a EN_KILLFOCUS command from the in-place edit control | |
3160 | // used for label editing, make sure to end editing | |
3161 | WORD id, cmd; | |
3162 | WXHWND hwnd; | |
3163 | UnpackCommand(wParam, lParam, &id, &hwnd, &cmd); | |
3164 | ||
3165 | if ( cmd == EN_KILLFOCUS ) | |
3166 | { | |
3167 | if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd ) | |
3168 | { | |
3169 | DoEndEditLabel(); | |
3170 | ||
3171 | processed = true; | |
3172 | } | |
3173 | } | |
3174 | } | |
7bb8798c | 3175 | |
3f7bc32b VZ |
3176 | if ( !processed ) |
3177 | rc = wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
3178 | ||
3179 | return rc; | |
23f681ec VZ |
3180 | } |
3181 | ||
fbd8ac52 VZ |
3182 | WXLRESULT |
3183 | wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
3184 | { | |
068b764a VZ |
3185 | if ( nMsg == WM_CHAR ) |
3186 | { | |
df63b2a4 | 3187 | // don't let the control process Space and Return keys because it |
068b764a VZ |
3188 | // doesn't do anything useful with them anyhow but always beeps |
3189 | // annoyingly when it receives them and there is no way to turn it off | |
3190 | // simply if you just process TREEITEM_ACTIVATED event to which Space | |
3191 | // and Enter presses are mapped in your code | |
3192 | if ( wParam == VK_SPACE || wParam == VK_RETURN ) | |
3193 | return 0; | |
3194 | } | |
c4cd8712 VZ |
3195 | #if wxUSE_DRAGIMAGE |
3196 | else if ( nMsg == WM_KEYDOWN ) | |
3197 | { | |
3198 | if ( wParam == VK_ESCAPE ) | |
3199 | { | |
3200 | if ( m_dragImage ) | |
3201 | { | |
3202 | m_dragImage->EndDrag(); | |
5276b0a5 | 3203 | wxDELETE(m_dragImage); |
c4cd8712 VZ |
3204 | |
3205 | // if we don't do it, the tree seems to think that 2 items | |
3206 | // are selected simultaneously which is quite weird | |
3207 | TreeView_SelectDropTarget(GetHwnd(), 0); | |
3208 | } | |
3209 | } | |
3210 | } | |
3211 | #endif // wxUSE_DRAGIMAGE | |
068b764a | 3212 | |
fbd8ac52 VZ |
3213 | return wxControl::MSWDefWindowProc(nMsg, wParam, lParam); |
3214 | } | |
3215 | ||
08b7c251 | 3216 | // process WM_NOTIFY Windows message |
a23fd0e1 | 3217 | bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
08b7c251 | 3218 | { |
09f277d6 | 3219 | wxTreeEvent event(wxEVT_NULL, this); |
08b7c251 VZ |
3220 | wxEventType eventType = wxEVT_NULL; |
3221 | NMHDR *hdr = (NMHDR *)lParam; | |
3222 | ||
3223 | switch ( hdr->code ) | |
2bda0e17 | 3224 | { |
08b7c251 VZ |
3225 | case TVN_BEGINDRAG: |
3226 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
3227 | // fall through | |
3228 | ||
3229 | case TVN_BEGINRDRAG: | |
3230 | { | |
3231 | if ( eventType == wxEVT_NULL ) | |
3232 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; | |
3233 | //else: left drag, already set above | |
3234 | ||
3235 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
3236 | ||
ee4b2721 | 3237 | event.m_item = tv->itemNew.hItem; |
08b7c251 | 3238 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); |
23f681ec VZ |
3239 | |
3240 | // don't allow dragging by default: the user code must | |
3241 | // explicitly say that it wants to allow it to avoid breaking | |
3242 | // the old apps | |
3243 | event.Veto(); | |
08b7c251 | 3244 | } |
696e1ea0 | 3245 | break; |
08b7c251 VZ |
3246 | |
3247 | case TVN_BEGINLABELEDIT: | |
3248 | { | |
3249 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; | |
3250 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
3251 | ||
646a8a4d VZ |
3252 | // although the user event handler may still veto it, it is |
3253 | // important to set it now so that calls to SetItemText() from | |
3254 | // the event handler would change the text controls contents | |
3255 | m_idEdited = | |
ee4b2721 | 3256 | event.m_item = info->item.hItem; |
5ea47806 | 3257 | event.m_label = info->item.pszText; |
04cd30de | 3258 | event.m_editCancelled = false; |
08b7c251 | 3259 | } |
696e1ea0 | 3260 | break; |
08b7c251 VZ |
3261 | |
3262 | case TVN_DELETEITEM: | |
3263 | { | |
3264 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; | |
3265 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
3266 | ||
ee4b2721 | 3267 | event.m_item = tv->itemOld.hItem; |
696e1ea0 VZ |
3268 | |
3269 | if ( m_hasAnyAttr ) | |
3270 | { | |
ee4b2721 VZ |
3271 | wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem); |
3272 | if ( it != m_attrs.end() ) | |
3273 | { | |
3274 | delete it->second; | |
3275 | m_attrs.erase(it); | |
3276 | } | |
696e1ea0 | 3277 | } |
08b7c251 | 3278 | } |
696e1ea0 | 3279 | break; |
08b7c251 VZ |
3280 | |
3281 | case TVN_ENDLABELEDIT: | |
3282 | { | |
3283 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; | |
3284 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
3285 | ||
ee4b2721 | 3286 | event.m_item = info->item.hItem; |
5ea47806 | 3287 | event.m_label = info->item.pszText; |
06110b00 | 3288 | event.m_editCancelled = info->item.pszText == NULL; |
08b7c251 VZ |
3289 | break; |
3290 | } | |
3291 | ||
676d6550 | 3292 | #ifndef __WXWINCE__ |
156194e1 JS |
3293 | // These *must* not be removed or TVN_GETINFOTIP will |
3294 | // not be processed each time the mouse is moved | |
3295 | // and the tooltip will only ever update once. | |
3296 | case TTN_NEEDTEXTA: | |
3297 | case TTN_NEEDTEXTW: | |
3298 | { | |
3299 | *result = 0; | |
3300 | ||
3301 | break; | |
3302 | } | |
3303 | ||
f3f71703 | 3304 | #ifdef TVN_GETINFOTIP |
156194e1 JS |
3305 | case TVN_GETINFOTIP: |
3306 | { | |
3307 | eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP; | |
3308 | NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam; | |
3309 | ||
3310 | // Which item are we trying to get a tooltip for? | |
bc0aebab | 3311 | event.m_item = info->hItem; |
156194e1 JS |
3312 | |
3313 | break; | |
3314 | } | |
df63b2a4 VZ |
3315 | #endif // TVN_GETINFOTIP |
3316 | #endif // !__WXWINCE__ | |
5e7718a2 | 3317 | |
08b7c251 VZ |
3318 | case TVN_GETDISPINFO: |
3319 | eventType = wxEVT_COMMAND_TREE_GET_INFO; | |
3320 | // fall through | |
3321 | ||
3322 | case TVN_SETDISPINFO: | |
3323 | { | |
3324 | if ( eventType == wxEVT_NULL ) | |
3325 | eventType = wxEVT_COMMAND_TREE_SET_INFO; | |
3326 | //else: get, already set above | |
3327 | ||
3328 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
3329 | ||
ee4b2721 | 3330 | event.m_item = info->item.hItem; |
08b7c251 VZ |
3331 | break; |
3332 | } | |
3333 | ||
3334 | case TVN_ITEMEXPANDING: | |
08b7c251 VZ |
3335 | case TVN_ITEMEXPANDED: |
3336 | { | |
4523ebb3 | 3337 | NM_TREEVIEW *tv = (NM_TREEVIEW*)lParam; |
08b7c251 | 3338 | |
deb1de30 | 3339 | int what; |
08b7c251 VZ |
3340 | switch ( tv->action ) |
3341 | { | |
deb1de30 VZ |
3342 | default: |
3343 | wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action); | |
3344 | // fall through | |
3345 | ||
08b7c251 | 3346 | case TVE_EXPAND: |
deb1de30 | 3347 | what = IDX_EXPAND; |
08b7c251 VZ |
3348 | break; |
3349 | ||
3350 | case TVE_COLLAPSE: | |
deb1de30 | 3351 | what = IDX_COLLAPSE; |
08b7c251 | 3352 | break; |
08b7c251 VZ |
3353 | } |
3354 | ||
2b5f62a0 VZ |
3355 | int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING |
3356 | : IDX_DONE; | |
deb1de30 VZ |
3357 | |
3358 | eventType = gs_expandEvents[what][how]; | |
08b7c251 | 3359 | |
ee4b2721 | 3360 | event.m_item = tv->itemNew.hItem; |
08b7c251 | 3361 | } |
696e1ea0 | 3362 | break; |
08b7c251 VZ |
3363 | |
3364 | case TVN_KEYDOWN: | |
3365 | { | |
08b7c251 VZ |
3366 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; |
3367 | ||
1944ad76 VZ |
3368 | // fabricate the lParam and wParam parameters sufficiently |
3369 | // similar to the ones from a "real" WM_KEYDOWN so that | |
3370 | // CreateKeyEvent() works correctly | |
47e10541 VZ |
3371 | return MSWHandleTreeKeyDownEvent( |
3372 | info->wVKey, (wxIsAltDown() ? KF_ALTDOWN : 0) << 16); | |
08b7c251 VZ |
3373 | } |
3374 | ||
5fe302ef BW |
3375 | |
3376 | // Vista's tree control has introduced some problems with our | |
3377 | // multi-selection tree. When TreeView_SelectItem() is called, | |
3378 | // the wrong items are deselected. | |
20bdddad | 3379 | |
5fe302ef BW |
3380 | // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING |
3381 | // that can be used to regulate this incorrect behavior. The | |
3382 | // following messages will allow only the unlocked item's selection | |
3383 | // state to change | |
396f85ba | 3384 | |
5fe302ef BW |
3385 | case TVN_ITEMCHANGINGA: |
3386 | case TVN_ITEMCHANGINGW: | |
3387 | { | |
3388 | // we only need to handles these in multi-select trees | |
3389 | if ( HasFlag(wxTR_MULTIPLE) ) | |
3390 | { | |
3391 | // get info about the item about to be changed | |
3392 | NMTVITEMCHANGE* info = (NMTVITEMCHANGE*)lParam; | |
396f85ba | 3393 | if (TreeItemUnlocker::IsLocked(info->hItem)) |
5fe302ef BW |
3394 | { |
3395 | // item's state is locked, don't allow the change | |
3396 | // returning 1 will disallow the change | |
20bdddad | 3397 | *result = 1; |
5fe302ef BW |
3398 | return true; |
3399 | } | |
3400 | } | |
3401 | ||
3402 | // allow the state change | |
3403 | } | |
3404 | return false; | |
3405 | ||
5e7718a2 | 3406 | // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of |
69a8b5b4 VS |
3407 | // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore |
3408 | // we have to handle both messages: | |
3409 | case TVN_SELCHANGEDA: | |
3410 | case TVN_SELCHANGEDW: | |
942f40ca | 3411 | if ( !m_changingSelection ) |
c7d9c476 VZ |
3412 | { |
3413 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; | |
3414 | } | |
08b7c251 VZ |
3415 | // fall through |
3416 | ||
69a8b5b4 VS |
3417 | case TVN_SELCHANGINGA: |
3418 | case TVN_SELCHANGINGW: | |
942f40ca | 3419 | if ( !m_changingSelection ) |
08b7c251 VZ |
3420 | { |
3421 | if ( eventType == wxEVT_NULL ) | |
3422 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; | |
3423 | //else: already set above | |
3424 | ||
5e7718a2 | 3425 | if (hdr->code == TVN_SELCHANGINGW || |
69a8b5b4 VS |
3426 | hdr->code == TVN_SELCHANGEDW) |
3427 | { | |
4523ebb3 | 3428 | NM_TREEVIEWW *tv = (NM_TREEVIEWW *)lParam; |
ee4b2721 VZ |
3429 | event.m_item = tv->itemNew.hItem; |
3430 | event.m_itemOld = tv->itemOld.hItem; | |
69a8b5b4 VS |
3431 | } |
3432 | else | |
3433 | { | |
4523ebb3 | 3434 | NM_TREEVIEWA *tv = (NM_TREEVIEWA *)lParam; |
ee4b2721 VZ |
3435 | event.m_item = tv->itemNew.hItem; |
3436 | event.m_itemOld = tv->itemOld.hItem; | |
69a8b5b4 | 3437 | } |
08b7c251 | 3438 | } |
cb776b66 VZ |
3439 | |
3440 | // we receive this message from WM_LBUTTONDOWN handler inside | |
3441 | // comctl32.dll and so before the click is passed to | |
3442 | // DefWindowProc() which sets the focus to the window which was | |
3443 | // clicked and this can lead to unexpected event sequences: for | |
3444 | // example, we may get a "selection change" event from the tree | |
3445 | // before getting a "kill focus" event for the text control which | |
3446 | // had the focus previously, thus breaking user code doing input | |
3447 | // validation | |
3448 | // | |
3449 | // to avoid such surprises, we force the generation of focus events | |
3450 | // now, before we generate the selection change ones | |
1da2783c VZ |
3451 | if ( !m_changingSelection ) |
3452 | SetFocus(); | |
696e1ea0 VZ |
3453 | break; |
3454 | ||
c7d9c476 VZ |
3455 | // instead of explicitly checking for _WIN32_IE, check if the |
3456 | // required symbols are available in the headers | |
5b59df83 | 3457 | #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY |
696e1ea0 VZ |
3458 | case NM_CUSTOMDRAW: |
3459 | { | |
3460 | LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam; | |
3461 | NMCUSTOMDRAW& nmcd = lptvcd->nmcd; | |
d7926e0b | 3462 | switch ( nmcd.dwDrawStage ) |
696e1ea0 VZ |
3463 | { |
3464 | case CDDS_PREPAINT: | |
3465 | // if we've got any items with non standard attributes, | |
3466 | // notify us before painting each item | |
3467 | *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW | |
3468 | : CDRF_DODEFAULT; | |
4754ab16 RR |
3469 | |
3470 | // windows in TreeCtrl use one-based index for item state images, | |
3471 | // 0 indexed image is not being used, we're using zero-based index, | |
3472 | // so we have to add temp image (of zero index) to state image list | |
3473 | // before we draw any item, then after items are drawn we have to | |
3474 | // delete it (in POSTPAINT notify) | |
3475 | if (m_imageListState && m_imageListState->GetImageCount() > 0) | |
3476 | { | |
2ec94d6b | 3477 | typedef BOOL (wxSTDCALL *ImageList_Copy_t) |
c27dce18 VZ |
3478 | (HIMAGELIST, int, HIMAGELIST, int, UINT); |
3479 | static ImageList_Copy_t s_pfnImageList_Copy = NULL; | |
3480 | static bool loaded = false; | |
3481 | ||
3482 | if ( !loaded ) | |
3483 | { | |
9a83f860 | 3484 | wxLoadedDLL dllComCtl32(wxT("comctl32.dll")); |
c27dce18 VZ |
3485 | if ( dllComCtl32.IsLoaded() ) |
3486 | wxDL_INIT_FUNC(s_pfn, ImageList_Copy, dllComCtl32); | |
3487 | } | |
3488 | ||
3489 | if ( !s_pfnImageList_Copy ) | |
3490 | { | |
3491 | // this code is broken with ImageList_Copy() | |
3492 | // but I don't care enough about Win95 support | |
3493 | // to write it now -- if anybody does, please | |
3494 | // do it | |
3495 | wxFAIL_MSG("TODO: implement this for Win95"); | |
3496 | break; | |
3497 | } | |
3498 | ||
3499 | const HIMAGELIST | |
3500 | hImageList = GetHimagelistOf(m_imageListState); | |
4754ab16 RR |
3501 | |
3502 | // add temporary image | |
3503 | int width, height; | |
3504 | m_imageListState->GetSize(0, width, height); | |
3505 | ||
3506 | HBITMAP hbmpTemp = ::CreateBitmap(width, height, 1, 1, NULL); | |
3507 | int index = ::ImageList_Add(hImageList, hbmpTemp, hbmpTemp); | |
3508 | ::DeleteObject(hbmpTemp); | |
3509 | ||
3510 | if ( index != -1 ) | |
3511 | { | |
3512 | // move images to right | |
3513 | for ( int i = index; i > 0; i-- ) | |
c27dce18 VZ |
3514 | { |
3515 | (*s_pfnImageList_Copy)(hImageList, i, | |
3516 | hImageList, i-1, | |
3517 | ILCF_MOVE); | |
3518 | } | |
4754ab16 RR |
3519 | |
3520 | // we must remove the image in POSTPAINT notify | |
3521 | *result |= CDRF_NOTIFYPOSTPAINT; | |
3522 | } | |
4754ab16 RR |
3523 | } |
3524 | break; | |
3525 | ||
3526 | case CDDS_POSTPAINT: | |
3527 | // we are deleting temp image of 0 index, which was | |
3528 | // added before items were drawn (in PREPAINT notify) | |
3529 | if (m_imageListState && m_imageListState->GetImageCount() > 0) | |
3530 | m_imageListState->Remove(0); | |
d7926e0b | 3531 | break; |
696e1ea0 VZ |
3532 | |
3533 | case CDDS_ITEMPREPAINT: | |
3534 | { | |
ee4b2721 VZ |
3535 | wxMapTreeAttr::iterator |
3536 | it = m_attrs.find((void *)nmcd.dwItemSpec); | |
696e1ea0 | 3537 | |
ee4b2721 | 3538 | if ( it == m_attrs.end() ) |
696e1ea0 VZ |
3539 | { |
3540 | // nothing to do for this item | |
d7926e0b VZ |
3541 | *result = CDRF_DODEFAULT; |
3542 | break; | |
696e1ea0 VZ |
3543 | } |
3544 | ||
ee4b2721 VZ |
3545 | wxTreeItemAttr * const attr = it->second; |
3546 | ||
8b713759 VZ |
3547 | wxTreeViewItem tvItem((void *)nmcd.dwItemSpec, |
3548 | TVIF_STATE, TVIS_DROPHILITED); | |
3549 | DoGetItem(&tvItem); | |
3550 | const UINT tvItemState = tvItem.state; | |
3551 | ||
19da49fc | 3552 | // selection colours should override ours, |
8b713759 VZ |
3553 | // otherwise it is too confusing to the user |
3554 | if ( !(nmcd.uItemState & CDIS_SELECTED) && | |
3555 | !(tvItemState & TVIS_DROPHILITED) ) | |
696e1ea0 | 3556 | { |
2076893b | 3557 | wxColour colBack; |
696e1ea0 VZ |
3558 | if ( attr->HasBackgroundColour() ) |
3559 | { | |
3560 | colBack = attr->GetBackgroundColour(); | |
19da49fc | 3561 | lptvcd->clrTextBk = wxColourToRGB(colBack); |
696e1ea0 | 3562 | } |
19da49fc VZ |
3563 | } |
3564 | ||
3565 | // but we still want to keep the special foreground | |
3566 | // colour when we don't have focus (we can't keep | |
3567 | // it when we do, it would usually be unreadable on | |
3568 | // the almost inverted bg colour...) | |
8b713759 VZ |
3569 | if ( ( !(nmcd.uItemState & CDIS_SELECTED) || |
3570 | FindFocus() != this ) && | |
3571 | !(tvItemState & TVIS_DROPHILITED) ) | |
19da49fc VZ |
3572 | { |
3573 | wxColour colText; | |
3574 | if ( attr->HasTextColour() ) | |
696e1ea0 | 3575 | { |
19da49fc VZ |
3576 | colText = attr->GetTextColour(); |
3577 | lptvcd->clrText = wxColourToRGB(colText); | |
696e1ea0 | 3578 | } |
696e1ea0 VZ |
3579 | } |
3580 | ||
19da49fc | 3581 | if ( attr->HasFont() ) |
696e1ea0 | 3582 | { |
19da49fc VZ |
3583 | HFONT hFont = GetHfontOf(attr->GetFont()); |
3584 | ||
696e1ea0 VZ |
3585 | ::SelectObject(nmcd.hdc, hFont); |
3586 | ||
3587 | *result = CDRF_NEWFONT; | |
3588 | } | |
19da49fc | 3589 | else // no specific font |
696e1ea0 VZ |
3590 | { |
3591 | *result = CDRF_DODEFAULT; | |
3592 | } | |
696e1ea0 | 3593 | } |
d7926e0b | 3594 | break; |
696e1ea0 VZ |
3595 | |
3596 | default: | |
3597 | *result = CDRF_DODEFAULT; | |
696e1ea0 VZ |
3598 | } |
3599 | } | |
d7926e0b VZ |
3600 | |
3601 | // we always process it | |
04cd30de | 3602 | return true; |
5b59df83 | 3603 | #endif // have owner drawn support in headers |
08b7c251 | 3604 | |
8a000b6b VZ |
3605 | case NM_CLICK: |
3606 | { | |
3607 | DWORD pos = GetMessagePos(); | |
3608 | POINT point; | |
3609 | point.x = LOWORD(pos); | |
3610 | point.y = HIWORD(pos); | |
3611 | ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1); | |
c7d9c476 VZ |
3612 | int htFlags = 0; |
3613 | wxTreeItemId item = HitTest(wxPoint(point.x, point.y), htFlags); | |
3614 | ||
3615 | if ( htFlags & wxTREE_HITTEST_ONITEMSTATEICON ) | |
8a000b6b VZ |
3616 | { |
3617 | event.m_item = item; | |
3618 | eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK; | |
3619 | } | |
c7d9c476 | 3620 | |
8a000b6b VZ |
3621 | break; |
3622 | } | |
3623 | ||
f6bcfd97 BP |
3624 | case NM_DBLCLK: |
3625 | case NM_RCLICK: | |
3626 | { | |
3627 | TV_HITTESTINFO tvhti; | |
3628 | ::GetCursorPos(&tvhti.pt); | |
3629 | ::ScreenToClient(GetHwnd(), &tvhti.pt); | |
3630 | if ( TreeView_HitTest(GetHwnd(), &tvhti) ) | |
3631 | { | |
3c8cbc12 | 3632 | if ( MSWIsOnItem(tvhti.flags) ) |
f6bcfd97 | 3633 | { |
ee4b2721 | 3634 | event.m_item = tvhti.hItem; |
f6bcfd97 BP |
3635 | eventType = (int)hdr->code == NM_DBLCLK |
3636 | ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED | |
3637 | : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK; | |
8591268f VZ |
3638 | |
3639 | event.m_pointDrag.x = tvhti.pt.x; | |
3640 | event.m_pointDrag.y = tvhti.pt.y; | |
f6bcfd97 BP |
3641 | } |
3642 | ||
3643 | break; | |
3644 | } | |
3645 | } | |
3646 | // fall through | |
3647 | ||
08b7c251 | 3648 | default: |
a23fd0e1 | 3649 | return wxControl::MSWOnNotify(idCtrl, lParam, result); |
2bda0e17 | 3650 | } |
08b7c251 | 3651 | |
08b7c251 VZ |
3652 | event.SetEventType(eventType); |
3653 | ||
c7d9c476 | 3654 | bool processed = HandleTreeEvent(event); |
08b7c251 VZ |
3655 | |
3656 | // post processing | |
5ea47806 | 3657 | switch ( hdr->code ) |
2bda0e17 | 3658 | { |
f6bcfd97 | 3659 | case NM_DBLCLK: |
0e2ad323 VZ |
3660 | // we translate NM_DBLCLK into ACTIVATED event and if the user |
3661 | // handled the activation of the item we shouldn't proceed with | |
3662 | // also using the same double click for toggling the item expanded | |
3663 | // state -- but OTOH do let the user to expand/collapse the item by | |
3664 | // double clicking on it if the activation is not handled specially | |
3665 | *result = processed; | |
f6bcfd97 BP |
3666 | break; |
3667 | ||
df63b2a4 VZ |
3668 | case NM_RCLICK: |
3669 | // prevent tree control from sending WM_CONTEXTMENU to our parent | |
3670 | // (which it does if NM_RCLICK is not handled) because we want to | |
3671 | // send it to the control itself | |
3672 | *result = | |
3673 | processed = true; | |
3674 | ||
3675 | ::SendMessage(GetHwnd(), WM_CONTEXTMENU, | |
3676 | (WPARAM)GetHwnd(), ::GetMessagePos()); | |
3677 | break; | |
3678 | ||
23f681ec VZ |
3679 | case TVN_BEGINDRAG: |
3680 | case TVN_BEGINRDRAG: | |
68d9be05 | 3681 | #if wxUSE_DRAGIMAGE |
23f681ec VZ |
3682 | if ( event.IsAllowed() ) |
3683 | { | |
3684 | // normally this is impossible because the m_dragImage is | |
3685 | // deleted once the drag operation is over | |
9a83f860 | 3686 | wxASSERT_MSG( !m_dragImage, wxT("starting to drag once again?") ); |
23f681ec VZ |
3687 | |
3688 | m_dragImage = new wxDragImage(*this, event.m_item); | |
c47addef | 3689 | m_dragImage->BeginDrag(wxPoint(0,0), this); |
68be9f09 | 3690 | m_dragImage->Show(); |
c7d9c476 VZ |
3691 | |
3692 | m_dragStarted = true; | |
23f681ec | 3693 | } |
68d9be05 | 3694 | #endif // wxUSE_DRAGIMAGE |
23f681ec VZ |
3695 | break; |
3696 | ||
5ea47806 VZ |
3697 | case TVN_DELETEITEM: |
3698 | { | |
77ffb593 | 3699 | // NB: we might process this message using wxWidgets event |
5ea47806 VZ |
3700 | // tables, but due to overhead of wxWin event system we |
3701 | // prefer to do it here ourself (otherwise deleting a tree | |
3702 | // with many items is just too slow) | |
4523ebb3 | 3703 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; |
4523ebb3 VZ |
3704 | |
3705 | wxTreeItemParam *param = | |
3706 | (wxTreeItemParam *)tv->itemOld.lParam; | |
3707 | delete param; | |
08b7c251 | 3708 | |
04cd30de | 3709 | processed = true; // Make sure we don't get called twice |
5ea47806 VZ |
3710 | } |
3711 | break; | |
3712 | ||
3713 | case TVN_BEGINLABELEDIT: | |
04cd30de | 3714 | // return true to cancel label editing |
5ea47806 | 3715 | *result = !event.IsAllowed(); |
646a8a4d | 3716 | |
cd6bd270 | 3717 | // set ES_WANTRETURN ( like we do in BeginLabelEdit ) |
646a8a4d | 3718 | if ( event.IsAllowed() ) |
cd6bd270 MB |
3719 | { |
3720 | HWND hText = TreeView_GetEditControl(GetHwnd()); | |
4523ebb3 | 3721 | if ( hText ) |
cd6bd270 | 3722 | { |
5e7718a2 | 3723 | // MBN: if m_textCtrl already has an HWND, it is a stale |
cd6bd270 MB |
3724 | // pointer from a previous edit (because the user |
3725 | // didn't modify the label before dismissing the control, | |
3726 | // and TVN_ENDLABELEDIT was not sent), so delete it | |
4523ebb3 | 3727 | if ( m_textCtrl && m_textCtrl->GetHWND() ) |
cd6bd270 | 3728 | DeleteTextCtrl(); |
4523ebb3 | 3729 | if ( !m_textCtrl ) |
cd6bd270 MB |
3730 | m_textCtrl = new wxTextCtrl(); |
3731 | m_textCtrl->SetParent(this); | |
3732 | m_textCtrl->SetHWND((WXHWND)hText); | |
3733 | m_textCtrl->SubclassWin((WXHWND)hText); | |
3734 | ||
3735 | // set wxTE_PROCESS_ENTER style for the text control to | |
3736 | // force it to process the Enter presses itself, otherwise | |
3737 | // they could be stolen from it by the dialog | |
3738 | // navigation code | |
3739 | m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | |
3740 | | wxTE_PROCESS_ENTER); | |
3741 | } | |
3742 | } | |
646a8a4d VZ |
3743 | else // we had set m_idEdited before |
3744 | { | |
3745 | m_idEdited.Unset(); | |
3746 | } | |
5ea47806 VZ |
3747 | break; |
3748 | ||
3749 | case TVN_ENDLABELEDIT: | |
04cd30de | 3750 | // return true to set the label to the new string: note that we |
188781db | 3751 | // also must pretend that we did process the message or it is going |
04cd30de | 3752 | // to be passed to DefWindowProc() which will happily return false |
188781db | 3753 | // cancelling the label change |
5ea47806 | 3754 | *result = event.IsAllowed(); |
04cd30de | 3755 | processed = true; |
5ea47806 VZ |
3756 | |
3757 | // ensure that we don't have the text ctrl which is going to be | |
3758 | // deleted any more | |
3759 | DeleteTextCtrl(); | |
3760 | break; | |
3761 | ||
676d6550 | 3762 | #ifndef __WXWINCE__ |
f3f71703 | 3763 | #ifdef TVN_GETINFOTIP |
156194e1 JS |
3764 | case TVN_GETINFOTIP: |
3765 | { | |
3766 | // If the user permitted a tooltip change, change it | |
3767 | if (event.IsAllowed()) | |
3768 | { | |
3769 | SetToolTip(event.m_label); | |
3770 | } | |
3771 | } | |
3772 | break; | |
f3f71703 | 3773 | #endif |
676d6550 | 3774 | #endif |
5e7718a2 | 3775 | |
5ea47806 VZ |
3776 | case TVN_SELCHANGING: |
3777 | case TVN_ITEMEXPANDING: | |
04cd30de | 3778 | // return true to prevent the action from happening |
5ea47806 VZ |
3779 | *result = !event.IsAllowed(); |
3780 | break; | |
3781 | ||
deb1de30 | 3782 | case TVN_ITEMEXPANDED: |
deb1de30 | 3783 | { |
4523ebb3 | 3784 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; |
5188000b | 3785 | const wxTreeItemId id(tv->itemNew.hItem); |
deb1de30 | 3786 | |
5188000b | 3787 | if ( tv->action == TVE_COLLAPSE ) |
bf43d750 | 3788 | { |
5188000b VZ |
3789 | if ( wxApp::GetComCtl32Version() >= 600 ) |
3790 | { | |
3791 | // for some reason the item selection rectangle depends | |
3792 | // on whether it is expanded or collapsed (at least | |
3793 | // with comctl32.dll v6): it is wider (by 3 pixels) in | |
3794 | // the expanded state, so when the item collapses and | |
3795 | // then is deselected the rightmost 3 pixels of the | |
3796 | // previously drawn selection are left on the screen | |
3797 | // | |
3798 | // it's not clear if it's a bug in comctl32.dll or in | |
3799 | // our code (because it does not happen in Explorer but | |
3800 | // OTOH we don't do anything which could result in this | |
3801 | // AFAICS) but we do need to work around it to avoid | |
3802 | // ugly artifacts | |
3803 | RefreshItem(id); | |
3804 | } | |
3805 | } | |
3806 | else // expand | |
3807 | { | |
3808 | // the item is also not refreshed properly after expansion when | |
3809 | // it has an image depending on the expanded/collapsed state: | |
3810 | // again, it's not clear if the bug is in comctl32.dll or our | |
3811 | // code... | |
3812 | int image = GetItemImage(id, wxTreeItemIcon_Expanded); | |
3813 | if ( image != -1 ) | |
3814 | { | |
3815 | RefreshItem(id); | |
3816 | } | |
deb1de30 VZ |
3817 | } |
3818 | } | |
3819 | break; | |
3820 | ||
74b31181 VZ |
3821 | case TVN_GETDISPINFO: |
3822 | // NB: so far the user can't set the image himself anyhow, so do it | |
3823 | // anyway - but this may change later | |
4523ebb3 | 3824 | //if ( /* !processed && */ ) |
74b31181 VZ |
3825 | { |
3826 | wxTreeItemId item = event.m_item; | |
3827 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
4523ebb3 | 3828 | |
1b182562 | 3829 | const wxTreeItemParam * const param = GetItemParam(item); |
4523ebb3 VZ |
3830 | if ( !param ) |
3831 | break; | |
3832 | ||
74b31181 VZ |
3833 | if ( info->item.mask & TVIF_IMAGE ) |
3834 | { | |
3835 | info->item.iImage = | |
4523ebb3 | 3836 | param->GetImage |
74b31181 | 3837 | ( |
74b31181 VZ |
3838 | IsExpanded(item) ? wxTreeItemIcon_Expanded |
3839 | : wxTreeItemIcon_Normal | |
3840 | ); | |
3841 | } | |
3842 | if ( info->item.mask & TVIF_SELECTEDIMAGE ) | |
3843 | { | |
3844 | info->item.iSelectedImage = | |
4523ebb3 | 3845 | param->GetImage |
74b31181 | 3846 | ( |
74b31181 VZ |
3847 | IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded |
3848 | : wxTreeItemIcon_Selected | |
3849 | ); | |
3850 | } | |
deb1de30 | 3851 | } |
74b31181 VZ |
3852 | break; |
3853 | ||
5ea47806 VZ |
3854 | //default: |
3855 | // for the other messages the return value is ignored and there is | |
3856 | // nothing special to do | |
3857 | } | |
fd3f686c | 3858 | return processed; |
2bda0e17 KB |
3859 | } |
3860 | ||
8a000b6b VZ |
3861 | // ---------------------------------------------------------------------------- |
3862 | // State control. | |
3863 | // ---------------------------------------------------------------------------- | |
3864 | ||
3865 | // why do they define INDEXTOSTATEIMAGEMASK but not the inverse? | |
3866 | #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12) | |
3867 | ||
03966fcb | 3868 | int wxTreeCtrl::DoGetItemState(const wxTreeItemId& item) const |
8a000b6b | 3869 | { |
03966fcb | 3870 | wxCHECK_MSG( item.IsOk(), wxTREE_ITEMSTATE_NONE, wxT("invalid tree item") ); |
8a000b6b | 3871 | |
03966fcb RR |
3872 | // receive the desired information |
3873 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
3874 | DoGetItem(&tvItem); | |
8a000b6b | 3875 | |
03966fcb RR |
3876 | // state images are one-based |
3877 | return STATEIMAGEMASKTOINDEX(tvItem.state) - 1; | |
8a000b6b VZ |
3878 | } |
3879 | ||
03966fcb | 3880 | void wxTreeCtrl::DoSetItemState(const wxTreeItemId& item, int state) |
8a000b6b | 3881 | { |
03966fcb RR |
3882 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
3883 | ||
3884 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
8a000b6b | 3885 | |
03966fcb RR |
3886 | // state images are one-based |
3887 | // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1) | |
3888 | tvItem.state = INDEXTOSTATEIMAGEMASK(state + 1); | |
3889 | ||
3890 | DoSetItem(&tvItem); | |
8a000b6b VZ |
3891 | } |
3892 | ||
1e6feb95 | 3893 | #endif // wxUSE_TREECTRL |