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