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