]>
Commit | Line | Data |
---|---|---|
b823f5a1 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: treectrl.cpp | |
3 | // Purpose: wxTreeCtrl | |
4 | // Author: Julian Smart | |
08b7c251 | 5 | // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98 |
b823f5a1 JS |
6 | // Created: 1997 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
08b7c251 | 9 | // Licence: wxWindows licence |
b823f5a1 | 10 | ///////////////////////////////////////////////////////////////////////////// |
2bda0e17 | 11 | |
08b7c251 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 19 | #ifdef __GNUG__ |
08b7c251 | 20 | #pragma implementation "treectrl.h" |
2bda0e17 KB |
21 | #endif |
22 | ||
23 | // For compilers that support precompilation, includes "wx.h". | |
24 | #include "wx/wxprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
08b7c251 | 27 | #pragma hdrstop |
2bda0e17 KB |
28 | #endif |
29 | ||
0c589ad0 BM |
30 | #include "wx/window.h" |
31 | #include "wx/msw/private.h" | |
32 | ||
0c589ad0 BM |
33 | // Mingw32 is a bit mental even though this is done in winundef |
34 | #ifdef GetFirstChild | |
d220ae32 | 35 | #undef GetFirstChild |
0c589ad0 | 36 | #endif |
d220ae32 | 37 | |
0c589ad0 | 38 | #ifdef GetNextSibling |
d220ae32 | 39 | #undef GetNextSibling |
2bda0e17 KB |
40 | #endif |
41 | ||
2bda0e17 KB |
42 | #if defined(__WIN95__) |
43 | ||
08b7c251 | 44 | #include "wx/log.h" |
ce3ed50d | 45 | #include "wx/dynarray.h" |
08b7c251 | 46 | #include "wx/imaglist.h" |
53f69f7a | 47 | #include "wx/treectrl.h" |
77cff606 | 48 | #include "wx/settings.h" |
08b7c251 | 49 | |
9a05fd8d | 50 | #ifdef __GNUWIN32__ |
65fd5cb0 | 51 | #ifndef wxUSE_NORLANDER_HEADERS |
9a05fd8d JS |
52 | #include "wx/msw/gnuwin32/extra.h" |
53 | #endif | |
65fd5cb0 | 54 | #endif |
9a05fd8d | 55 | |
65fd5cb0 | 56 | #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS) |
08b7c251 | 57 | #include <commctrl.h> |
2bda0e17 KB |
58 | #endif |
59 | ||
60 | // Bug in headers, sometimes | |
61 | #ifndef TVIS_FOCUSED | |
08b7c251 | 62 | #define TVIS_FOCUSED 0x0001 |
2bda0e17 KB |
63 | #endif |
64 | ||
bb448552 VZ |
65 | #ifndef TV_FIRST |
66 | #define TV_FIRST 0x1100 | |
67 | #endif | |
68 | ||
69 | // old headers might miss these messages (comctl32.dll 4.71+ only) | |
70 | #ifndef TVM_SETBKCOLOR | |
71 | #define TVM_SETBKCOLOR (TV_FIRST + 29) | |
72 | #define TVM_SETTEXTCOLOR (TV_FIRST + 30) | |
73 | #endif | |
74 | ||
08b7c251 VZ |
75 | // ---------------------------------------------------------------------------- |
76 | // private classes | |
77 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 78 | |
08b7c251 | 79 | // a convenient wrapper around TV_ITEM struct which adds a ctor |
f3ef286f | 80 | #ifdef __VISUALC__ |
197dd9af | 81 | #pragma warning( disable : 4097 ) |
f3ef286f JS |
82 | #endif |
83 | ||
08b7c251 VZ |
84 | struct wxTreeViewItem : public TV_ITEM |
85 | { | |
9dfbf520 VZ |
86 | wxTreeViewItem(const wxTreeItemId& item, // the item handle |
87 | UINT mask_, // fields which are valid | |
88 | UINT stateMask_ = 0) // for TVIF_STATE only | |
08b7c251 | 89 | { |
9dfbf520 VZ |
90 | // hItem member is always valid |
91 | mask = mask_ | TVIF_HANDLE; | |
08b7c251 | 92 | stateMask = stateMask_; |
06e38c8e | 93 | hItem = (HTREEITEM) (WXHTREEITEM) item; |
08b7c251 VZ |
94 | } |
95 | }; | |
f3ef286f JS |
96 | |
97 | #ifdef __VISUALC__ | |
197dd9af | 98 | #pragma warning( default : 4097 ) |
f3ef286f | 99 | #endif |
2bda0e17 | 100 | |
9dfbf520 VZ |
101 | // a class which encapsulates the tree traversal logic: it vists all (unless |
102 | // OnVisit() returns FALSE) items under the given one | |
103 | class wxTreeTraversal | |
104 | { | |
105 | public: | |
106 | wxTreeTraversal(const wxTreeCtrl *tree) | |
107 | { | |
108 | m_tree = tree; | |
109 | } | |
110 | ||
111 | // do traverse the tree: visit all items (recursively by default) under the | |
112 | // given one; return TRUE if all items were traversed or FALSE if the | |
113 | // traversal was aborted because OnVisit returned FALSE | |
114 | bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE); | |
115 | ||
116 | // override this function to do whatever is needed for each item, return | |
117 | // FALSE to stop traversing | |
118 | virtual bool OnVisit(const wxTreeItemId& item) = 0; | |
119 | ||
120 | protected: | |
121 | const wxTreeCtrl *GetTree() const { return m_tree; } | |
122 | ||
123 | private: | |
124 | bool Traverse(const wxTreeItemId& root, bool recursively); | |
125 | ||
126 | const wxTreeCtrl *m_tree; | |
127 | }; | |
128 | ||
74b31181 VZ |
129 | // internal class for getting the selected items |
130 | class TraverseSelections : public wxTreeTraversal | |
131 | { | |
132 | public: | |
133 | TraverseSelections(const wxTreeCtrl *tree, | |
134 | wxArrayTreeItemIds& selections) | |
135 | : wxTreeTraversal(tree), m_selections(selections) | |
136 | { | |
137 | m_selections.Empty(); | |
138 | ||
139 | DoTraverse(tree->GetRootItem()); | |
140 | } | |
141 | ||
142 | virtual bool OnVisit(const wxTreeItemId& item) | |
143 | { | |
144 | if ( GetTree()->IsItemChecked(item) ) | |
145 | { | |
146 | m_selections.Add(item); | |
147 | } | |
148 | ||
149 | return TRUE; | |
150 | } | |
151 | ||
e47c4d48 VZ |
152 | size_t GetCount() const { return m_selections.GetCount(); } |
153 | ||
74b31181 VZ |
154 | private: |
155 | wxArrayTreeItemIds& m_selections; | |
156 | }; | |
157 | ||
158 | // internal class for counting tree items | |
159 | class TraverseCounter : public wxTreeTraversal | |
160 | { | |
161 | public: | |
162 | TraverseCounter(const wxTreeCtrl *tree, | |
163 | const wxTreeItemId& root, | |
164 | bool recursively) | |
165 | : wxTreeTraversal(tree) | |
166 | { | |
167 | m_count = 0; | |
168 | ||
169 | DoTraverse(root, recursively); | |
170 | } | |
171 | ||
172 | virtual bool OnVisit(const wxTreeItemId& item) | |
173 | { | |
174 | m_count++; | |
175 | ||
176 | return TRUE; | |
177 | } | |
178 | ||
179 | size_t GetCount() const { return m_count; } | |
180 | ||
181 | private: | |
182 | size_t m_count; | |
183 | }; | |
184 | ||
185 | // ---------------------------------------------------------------------------- | |
186 | // This class is needed for support of different images: the Win32 common | |
187 | // control natively supports only 2 images (the normal one and another for the | |
188 | // selected state). We wish to provide support for 2 more of them for folder | |
189 | // items (i.e. those which have children): for expanded state and for expanded | |
190 | // selected state. For this we use this structure to store the additional items | |
191 | // images. | |
192 | // | |
193 | // There is only one problem with this: when we retrieve the item's data, we | |
194 | // don't know whether we get a pointer to wxTreeItemData or | |
195 | // wxTreeItemIndirectData. So we have to maintain a list of all items which | |
196 | // have indirect data inside the listctrl itself. | |
197 | // ---------------------------------------------------------------------------- | |
696e1ea0 | 198 | |
74b31181 VZ |
199 | class wxTreeItemIndirectData |
200 | { | |
201 | public: | |
202 | // ctor associates this data with the item and the real item data becomes | |
203 | // available through our GetData() method | |
204 | wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item) | |
205 | { | |
206 | for ( size_t n = 0; n < WXSIZEOF(m_images); n++ ) | |
207 | { | |
208 | m_images[n] = -1; | |
209 | } | |
210 | ||
211 | // save the old data | |
212 | m_data = tree->GetItemData(item); | |
213 | ||
214 | // and set ourselves as the new one | |
215 | tree->SetIndirectItemData(item, this); | |
216 | } | |
217 | ||
218 | // dtor deletes the associated data as well | |
219 | ~wxTreeItemIndirectData() { delete m_data; } | |
220 | ||
221 | // accessors | |
222 | // get the real data associated with the item | |
223 | wxTreeItemData *GetData() const { return m_data; } | |
224 | // change it | |
225 | void SetData(wxTreeItemData *data) { m_data = data; } | |
226 | ||
227 | // do we have such image? | |
228 | bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; } | |
229 | // get image | |
230 | int GetImage(wxTreeItemIcon which) const { return m_images[which]; } | |
231 | // change it | |
232 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } | |
233 | ||
234 | private: | |
235 | // all the images associated with the item | |
236 | int m_images[wxTreeItemIcon_Max]; | |
237 | ||
238 | wxTreeItemData *m_data; | |
239 | }; | |
240 | ||
08b7c251 VZ |
241 | // ---------------------------------------------------------------------------- |
242 | // macros | |
243 | // ---------------------------------------------------------------------------- | |
244 | ||
08b7c251 | 245 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) |
2bda0e17 | 246 | |
08b7c251 VZ |
247 | // ---------------------------------------------------------------------------- |
248 | // variables | |
249 | // ---------------------------------------------------------------------------- | |
250 | ||
251 | // handy table for sending events | |
252 | static const wxEventType g_events[2][2] = | |
2bda0e17 | 253 | { |
08b7c251 VZ |
254 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, |
255 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } | |
256 | }; | |
257 | ||
258 | // ============================================================================ | |
259 | // implementation | |
260 | // ============================================================================ | |
261 | ||
9dfbf520 VZ |
262 | // ---------------------------------------------------------------------------- |
263 | // tree traversal | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
266 | bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively) | |
267 | { | |
268 | if ( !OnVisit(root) ) | |
269 | return FALSE; | |
270 | ||
271 | return Traverse(root, recursively); | |
272 | } | |
273 | ||
274 | bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively) | |
275 | { | |
276 | long cookie; | |
277 | wxTreeItemId child = m_tree->GetFirstChild(root, cookie); | |
278 | while ( child.IsOk() ) | |
279 | { | |
280 | // depth first traversal | |
281 | if ( recursively && !Traverse(child, TRUE) ) | |
282 | return FALSE; | |
283 | ||
284 | if ( !OnVisit(child) ) | |
285 | return FALSE; | |
286 | ||
287 | child = m_tree->GetNextChild(root, cookie); | |
288 | } | |
289 | ||
290 | return TRUE; | |
291 | } | |
292 | ||
08b7c251 VZ |
293 | // ---------------------------------------------------------------------------- |
294 | // construction and destruction | |
295 | // ---------------------------------------------------------------------------- | |
296 | ||
297 | void wxTreeCtrl::Init() | |
298 | { | |
299 | m_imageListNormal = NULL; | |
300 | m_imageListState = NULL; | |
301 | m_textCtrl = NULL; | |
696e1ea0 | 302 | m_hasAnyAttr = FALSE; |
2bda0e17 KB |
303 | } |
304 | ||
9dfbf520 VZ |
305 | bool wxTreeCtrl::Create(wxWindow *parent, |
306 | wxWindowID id, | |
307 | const wxPoint& pos, | |
308 | const wxSize& size, | |
309 | long style, | |
310 | const wxValidator& validator, | |
08b7c251 | 311 | const wxString& name) |
2bda0e17 | 312 | { |
08b7c251 | 313 | Init(); |
2bda0e17 | 314 | |
9dfbf520 VZ |
315 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
316 | return FALSE; | |
2bda0e17 | 317 | |
5ea47806 VZ |
318 | DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | |
319 | TVS_HASLINES | TVS_SHOWSELALWAYS; | |
2bda0e17 | 320 | |
08b7c251 VZ |
321 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) |
322 | wstyle |= TVS_HASBUTTONS; | |
2bda0e17 | 323 | |
08b7c251 VZ |
324 | if ( m_windowStyle & wxTR_EDIT_LABELS ) |
325 | wstyle |= TVS_EDITLABELS; | |
2bda0e17 | 326 | |
08b7c251 VZ |
327 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) |
328 | wstyle |= TVS_LINESATROOT; | |
2bda0e17 | 329 | |
a925b006 | 330 | #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined( __WATCOMC__ ) && !defined(wxUSE_NORLANDER_HEADERS) |
9dfbf520 VZ |
331 | // we emulate the multiple selection tree controls by using checkboxes: set |
332 | // up the image list we need for this if we do have multiple selections | |
6474416b | 333 | #if !defined(__VISUALC__) || (__VISUALC__ > 1010) |
9dfbf520 | 334 | if ( m_windowStyle & wxTR_MULTIPLE ) |
10fcf31a | 335 | wstyle |= TVS_CHECKBOXES; |
2996bcde | 336 | #endif |
2899e223 | 337 | #endif |
9dfbf520 | 338 | |
08b7c251 | 339 | // Create the tree control. |
9dfbf520 VZ |
340 | if ( !MSWCreateControl(WC_TREEVIEW, wstyle) ) |
341 | return FALSE; | |
342 | ||
5aeeab14 RD |
343 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); |
344 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); | |
345 | ||
9dfbf520 VZ |
346 | // VZ: this is some experimental code which may be used to get the |
347 | // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71. | |
348 | // AFAIK, the standard DLL does about the same thing anyhow. | |
349 | #if 0 | |
350 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
351 | { | |
352 | wxBitmap bmp; | |
353 | ||
354 | // create the DC compatible with the current screen | |
355 | HDC hdcMem = CreateCompatibleDC(NULL); | |
356 | ||
357 | // create a mono bitmap of the standard size | |
358 | int x = GetSystemMetrics(SM_CXMENUCHECK); | |
359 | int y = GetSystemMetrics(SM_CYMENUCHECK); | |
360 | wxImageList imagelistCheckboxes(x, y, FALSE, 2); | |
361 | HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size | |
362 | 1, // # of color planes | |
363 | 1, // # bits needed for one pixel | |
364 | 0); // array containing colour data | |
365 | SelectObject(hdcMem, hbmpCheck); | |
366 | ||
367 | // then draw a check mark into it | |
368 | RECT rect = { 0, 0, x, y }; | |
369 | if ( !::DrawFrameControl(hdcMem, &rect, | |
370 | DFC_BUTTON, | |
371 | DFCS_BUTTONCHECK | DFCS_CHECKED) ) | |
372 | { | |
223d09f6 | 373 | wxLogLastError(wxT("DrawFrameControl(check)")); |
9dfbf520 VZ |
374 | } |
375 | ||
376 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); | |
377 | imagelistCheckboxes.Add(bmp); | |
378 | ||
379 | if ( !::DrawFrameControl(hdcMem, &rect, | |
380 | DFC_BUTTON, | |
381 | DFCS_BUTTONCHECK) ) | |
382 | { | |
223d09f6 | 383 | wxLogLastError(wxT("DrawFrameControl(uncheck)")); |
9dfbf520 VZ |
384 | } |
385 | ||
386 | bmp.SetHBITMAP((WXHBITMAP)hbmpCheck); | |
387 | imagelistCheckboxes.Add(bmp); | |
388 | ||
389 | // clean up | |
390 | ::DeleteDC(hdcMem); | |
391 | ||
392 | // set the imagelist | |
393 | SetStateImageList(&imagelistCheckboxes); | |
394 | } | |
395 | #endif // 0 | |
396 | ||
397 | SetSize(pos.x, pos.y, size.x, size.y); | |
2bda0e17 | 398 | |
08b7c251 | 399 | return TRUE; |
2bda0e17 KB |
400 | } |
401 | ||
08b7c251 | 402 | wxTreeCtrl::~wxTreeCtrl() |
2bda0e17 | 403 | { |
696e1ea0 VZ |
404 | // delete any attributes |
405 | if ( m_hasAnyAttr ) | |
406 | { | |
407 | for ( wxNode *node = m_attrs.Next(); node; node = m_attrs.Next() ) | |
408 | { | |
409 | delete (wxTreeItemAttr *)node->Data(); | |
410 | } | |
411 | ||
412 | // prevent TVN_DELETEITEM handler from deleting the attributes again! | |
413 | m_hasAnyAttr = FALSE; | |
414 | } | |
415 | ||
08b7c251 | 416 | DeleteTextCtrl(); |
2bda0e17 | 417 | |
08b7c251 VZ |
418 | // delete user data to prevent memory leaks |
419 | DeleteAllItems(); | |
2bda0e17 KB |
420 | } |
421 | ||
08b7c251 VZ |
422 | // ---------------------------------------------------------------------------- |
423 | // accessors | |
424 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 425 | |
08b7c251 | 426 | // simple wrappers which add error checking in debug mode |
2bda0e17 | 427 | |
08b7c251 | 428 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const |
2bda0e17 | 429 | { |
d220ae32 | 430 | if ( !TreeView_GetItem(GetHwnd(), tvItem) ) |
2bda0e17 | 431 | { |
08b7c251 VZ |
432 | wxLogLastError("TreeView_GetItem"); |
433 | ||
434 | return FALSE; | |
435 | } | |
436 | ||
437 | return TRUE; | |
2bda0e17 KB |
438 | } |
439 | ||
08b7c251 | 440 | void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem) |
2bda0e17 | 441 | { |
d220ae32 | 442 | if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 ) |
2bda0e17 | 443 | { |
08b7c251 VZ |
444 | wxLogLastError("TreeView_SetItem"); |
445 | } | |
2bda0e17 KB |
446 | } |
447 | ||
08b7c251 | 448 | size_t wxTreeCtrl::GetCount() const |
2bda0e17 | 449 | { |
d220ae32 | 450 | return (size_t)TreeView_GetCount(GetHwnd()); |
2bda0e17 KB |
451 | } |
452 | ||
08b7c251 | 453 | unsigned int wxTreeCtrl::GetIndent() const |
2bda0e17 | 454 | { |
d220ae32 | 455 | return TreeView_GetIndent(GetHwnd()); |
2bda0e17 KB |
456 | } |
457 | ||
08b7c251 | 458 | void wxTreeCtrl::SetIndent(unsigned int indent) |
2bda0e17 | 459 | { |
d220ae32 | 460 | TreeView_SetIndent(GetHwnd(), indent); |
2bda0e17 KB |
461 | } |
462 | ||
08b7c251 | 463 | wxImageList *wxTreeCtrl::GetImageList() const |
2bda0e17 | 464 | { |
08b7c251 | 465 | return m_imageListNormal; |
2bda0e17 KB |
466 | } |
467 | ||
08b7c251 | 468 | wxImageList *wxTreeCtrl::GetStateImageList() const |
2bda0e17 | 469 | { |
08b7c251 | 470 | return m_imageListNormal; |
2bda0e17 KB |
471 | } |
472 | ||
08b7c251 | 473 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) |
2bda0e17 | 474 | { |
08b7c251 | 475 | // no error return |
d220ae32 | 476 | TreeView_SetImageList(GetHwnd(), |
08b7c251 VZ |
477 | imageList ? imageList->GetHIMAGELIST() : 0, |
478 | which); | |
2bda0e17 KB |
479 | } |
480 | ||
08b7c251 | 481 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
2bda0e17 | 482 | { |
08b7c251 | 483 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); |
2bda0e17 KB |
484 | } |
485 | ||
08b7c251 | 486 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
2bda0e17 | 487 | { |
08b7c251 | 488 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); |
2bda0e17 KB |
489 | } |
490 | ||
33961d59 RR |
491 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, |
492 | bool recursively) const | |
493 | { | |
494 | TraverseCounter counter(this, item, recursively); | |
23fd5130 | 495 | |
73974df1 | 496 | return counter.GetCount() - 1; |
23fd5130 VZ |
497 | } |
498 | ||
bb448552 VZ |
499 | // ---------------------------------------------------------------------------- |
500 | // control colours | |
501 | // ---------------------------------------------------------------------------- | |
502 | ||
503 | bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour) | |
504 | { | |
505 | if ( !wxWindowBase::SetBackgroundColour(colour) ) | |
506 | return FALSE; | |
507 | ||
508 | SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel()); | |
509 | ||
510 | return TRUE; | |
511 | } | |
512 | ||
513 | bool wxTreeCtrl::SetForegroundColour(const wxColour &colour) | |
514 | { | |
515 | if ( !wxWindowBase::SetForegroundColour(colour) ) | |
516 | return FALSE; | |
517 | ||
518 | SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel()); | |
519 | ||
520 | return TRUE; | |
521 | } | |
522 | ||
08b7c251 VZ |
523 | // ---------------------------------------------------------------------------- |
524 | // Item access | |
525 | // ---------------------------------------------------------------------------- | |
526 | ||
527 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const | |
2bda0e17 | 528 | { |
837e5743 | 529 | wxChar buf[512]; // the size is arbitrary... |
02ce7b72 | 530 | |
08b7c251 VZ |
531 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
532 | tvItem.pszText = buf; | |
533 | tvItem.cchTextMax = WXSIZEOF(buf); | |
534 | if ( !DoGetItem(&tvItem) ) | |
535 | { | |
536 | // don't return some garbage which was on stack, but an empty string | |
223d09f6 | 537 | buf[0] = wxT('\0'); |
08b7c251 | 538 | } |
2bda0e17 | 539 | |
08b7c251 VZ |
540 | return wxString(buf); |
541 | } | |
2bda0e17 | 542 | |
08b7c251 VZ |
543 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
544 | { | |
545 | wxTreeViewItem tvItem(item, TVIF_TEXT); | |
837e5743 | 546 | tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok |
08b7c251 VZ |
547 | DoSetItem(&tvItem); |
548 | } | |
2bda0e17 | 549 | |
74b31181 VZ |
550 | int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item, |
551 | wxTreeItemIcon which) const | |
552 | { | |
553 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
554 | if ( !DoGetItem(&tvItem) ) | |
555 | { | |
556 | return -1; | |
557 | } | |
558 | ||
559 | return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which); | |
560 | } | |
561 | ||
562 | void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item, | |
563 | int image, | |
564 | wxTreeItemIcon which) const | |
565 | { | |
566 | wxTreeViewItem tvItem(item, TVIF_PARAM); | |
567 | if ( !DoGetItem(&tvItem) ) | |
568 | { | |
569 | return; | |
570 | } | |
571 | ||
572 | wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam); | |
573 | ||
574 | data->SetImage(image, which); | |
575 | ||
576 | // make sure that we have selected images as well | |
577 | if ( which == wxTreeItemIcon_Normal && | |
578 | !data->HasImage(wxTreeItemIcon_Selected) ) | |
579 | { | |
580 | data->SetImage(image, wxTreeItemIcon_Selected); | |
581 | } | |
582 | ||
583 | if ( which == wxTreeItemIcon_Expanded && | |
584 | !data->HasImage(wxTreeItemIcon_SelectedExpanded) ) | |
585 | { | |
586 | data->SetImage(image, wxTreeItemIcon_SelectedExpanded); | |
587 | } | |
588 | } | |
589 | ||
9dfbf520 VZ |
590 | void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item, |
591 | int image, | |
592 | int imageSel) | |
593 | { | |
594 | wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE); | |
595 | tvItem.iSelectedImage = imageSel; | |
596 | tvItem.iImage = image; | |
597 | DoSetItem(&tvItem); | |
598 | } | |
599 | ||
74b31181 VZ |
600 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item, |
601 | wxTreeItemIcon which) const | |
08b7c251 | 602 | { |
74b31181 VZ |
603 | if ( HasIndirectData(item) ) |
604 | { | |
605 | return DoGetItemImageFromData(item, which); | |
606 | } | |
2bda0e17 | 607 | |
74b31181 VZ |
608 | UINT mask; |
609 | switch ( which ) | |
610 | { | |
611 | default: | |
223d09f6 | 612 | wxFAIL_MSG( wxT("unknown tree item image type") ); |
2bda0e17 | 613 | |
74b31181 VZ |
614 | case wxTreeItemIcon_Normal: |
615 | mask = TVIF_IMAGE; | |
616 | break; | |
2bda0e17 | 617 | |
74b31181 VZ |
618 | case wxTreeItemIcon_Selected: |
619 | mask = TVIF_SELECTEDIMAGE; | |
620 | break; | |
621 | ||
622 | case wxTreeItemIcon_Expanded: | |
623 | case wxTreeItemIcon_SelectedExpanded: | |
624 | return -1; | |
625 | } | |
626 | ||
627 | wxTreeViewItem tvItem(item, mask); | |
08b7c251 | 628 | DoGetItem(&tvItem); |
2bda0e17 | 629 | |
74b31181 | 630 | return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage; |
2bda0e17 KB |
631 | } |
632 | ||
74b31181 VZ |
633 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image, |
634 | wxTreeItemIcon which) | |
2bda0e17 | 635 | { |
74b31181 VZ |
636 | int imageNormal, imageSel; |
637 | switch ( which ) | |
638 | { | |
639 | default: | |
223d09f6 | 640 | wxFAIL_MSG( wxT("unknown tree item image type") ); |
74b31181 VZ |
641 | |
642 | case wxTreeItemIcon_Normal: | |
643 | imageNormal = image; | |
644 | imageSel = GetItemSelectedImage(item); | |
645 | break; | |
646 | ||
647 | case wxTreeItemIcon_Selected: | |
648 | imageNormal = GetItemImage(item); | |
649 | imageSel = image; | |
650 | break; | |
651 | ||
652 | case wxTreeItemIcon_Expanded: | |
653 | case wxTreeItemIcon_SelectedExpanded: | |
654 | if ( !HasIndirectData(item) ) | |
655 | { | |
656 | // we need to get the old images first, because after we create | |
657 | // the wxTreeItemIndirectData GetItemXXXImage() will use it to | |
658 | // get the images | |
659 | imageNormal = GetItemImage(item); | |
660 | imageSel = GetItemSelectedImage(item); | |
661 | ||
662 | // if it doesn't have it yet, add it | |
663 | wxTreeItemIndirectData *data = new | |
664 | wxTreeItemIndirectData(this, item); | |
665 | ||
666 | // copy the data to the new location | |
667 | data->SetImage(imageNormal, wxTreeItemIcon_Normal); | |
668 | data->SetImage(imageSel, wxTreeItemIcon_Selected); | |
669 | } | |
670 | ||
671 | DoSetItemImageFromData(item, image, which); | |
672 | ||
673 | // reset the normal/selected images because we won't use them any | |
674 | // more - now they're stored inside the indirect data | |
675 | imageNormal = | |
676 | imageSel = I_IMAGECALLBACK; | |
677 | break; | |
678 | } | |
679 | ||
9dfbf520 VZ |
680 | // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always |
681 | // change both normal and selected image - otherwise the change simply | |
682 | // doesn't take place! | |
74b31181 | 683 | DoSetItemImages(item, imageNormal, imageSel); |
2bda0e17 KB |
684 | } |
685 | ||
08b7c251 | 686 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
2bda0e17 | 687 | { |
08b7c251 VZ |
688 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
689 | if ( !DoGetItem(&tvItem) ) | |
690 | { | |
691 | return NULL; | |
692 | } | |
2bda0e17 | 693 | |
74b31181 VZ |
694 | if ( HasIndirectData(item) ) |
695 | { | |
696 | return ((wxTreeItemIndirectData *)tvItem.lParam)->GetData(); | |
697 | } | |
698 | else | |
699 | { | |
700 | return (wxTreeItemData *)tvItem.lParam; | |
701 | } | |
2bda0e17 KB |
702 | } |
703 | ||
08b7c251 | 704 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
2bda0e17 | 705 | { |
08b7c251 | 706 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
74b31181 VZ |
707 | |
708 | if ( HasIndirectData(item) ) | |
709 | { | |
710 | if ( DoGetItem(&tvItem) ) | |
711 | { | |
712 | ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data); | |
713 | } | |
714 | else | |
715 | { | |
223d09f6 | 716 | wxFAIL_MSG( wxT("failed to change tree items data") ); |
74b31181 VZ |
717 | } |
718 | } | |
719 | else | |
720 | { | |
721 | tvItem.lParam = (LPARAM)data; | |
722 | DoSetItem(&tvItem); | |
723 | } | |
724 | } | |
725 | ||
726 | void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item, | |
727 | wxTreeItemIndirectData *data) | |
728 | { | |
729 | // this should never happen because it's unnecessary and will probably lead | |
730 | // to crash too because the code elsewhere supposes that the pointer the | |
731 | // wxTreeItemIndirectData has is a real wxItemData and not | |
732 | // wxTreeItemIndirectData as well | |
223d09f6 | 733 | wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") ); |
74b31181 VZ |
734 | |
735 | SetItemData(item, (wxTreeItemData *)data); | |
736 | ||
737 | m_itemsWithIndirectData.Add(item); | |
738 | } | |
739 | ||
740 | bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const | |
741 | { | |
742 | return m_itemsWithIndirectData.Index(item) != wxNOT_FOUND; | |
08b7c251 | 743 | } |
2bda0e17 | 744 | |
3a5a2f56 VZ |
745 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
746 | { | |
747 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); | |
748 | tvItem.cChildren = (int)has; | |
749 | DoSetItem(&tvItem); | |
750 | } | |
751 | ||
add28c55 VZ |
752 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
753 | { | |
754 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
755 | tvItem.state = bold ? TVIS_BOLD : 0; | |
756 | DoSetItem(&tvItem); | |
757 | } | |
758 | ||
58a8ab88 JS |
759 | void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight) |
760 | { | |
761 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED); | |
762 | tvItem.state = highlight ? TVIS_DROPHILITED : 0; | |
763 | DoSetItem(&tvItem); | |
764 | } | |
765 | ||
696e1ea0 VZ |
766 | void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item, |
767 | const wxColour& col) | |
768 | { | |
769 | m_hasAnyAttr = TRUE; | |
770 | ||
771 | long id = (long)(WXHTREEITEM)item; | |
772 | wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id); | |
773 | if ( !attr ) | |
774 | { | |
775 | attr = new wxTreeItemAttr; | |
776 | m_attrs.Put(id, (wxObject *)attr); | |
777 | } | |
778 | ||
779 | attr->SetTextColour(col); | |
780 | } | |
781 | ||
782 | void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, | |
783 | const wxColour& col) | |
784 | { | |
785 | m_hasAnyAttr = TRUE; | |
786 | ||
787 | long id = (long)(WXHTREEITEM)item; | |
788 | wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id); | |
789 | if ( !attr ) | |
790 | { | |
791 | attr = new wxTreeItemAttr; | |
792 | m_attrs.Put(id, (wxObject *)attr); | |
793 | } | |
794 | ||
795 | attr->SetBackgroundColour(col); | |
796 | } | |
797 | ||
798 | void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) | |
799 | { | |
800 | m_hasAnyAttr = TRUE; | |
801 | ||
802 | long id = (long)(WXHTREEITEM)item; | |
803 | wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id); | |
804 | if ( !attr ) | |
805 | { | |
806 | attr = new wxTreeItemAttr; | |
807 | m_attrs.Put(id, (wxObject *)attr); | |
808 | } | |
809 | ||
810 | attr->SetFont(font); | |
811 | } | |
812 | ||
08b7c251 VZ |
813 | // ---------------------------------------------------------------------------- |
814 | // Item status | |
815 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 816 | |
08b7c251 VZ |
817 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const |
818 | { | |
add28c55 | 819 | // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect |
08b7c251 | 820 | RECT rect; |
955be36c VZ |
821 | |
822 | // this ugliness comes directly from MSDN - it *is* the correct way to pass | |
823 | // the HTREEITEM with TVM_GETITEMRECT | |
824 | *(WXHTREEITEM *)&rect = (WXHTREEITEM)item; | |
825 | ||
826 | // FALSE means get item rect for the whole item, not only text | |
1c74a900 | 827 | return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0; |
06e38c8e | 828 | |
2bda0e17 KB |
829 | } |
830 | ||
08b7c251 | 831 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
2bda0e17 | 832 | { |
08b7c251 VZ |
833 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
834 | DoGetItem(&tvItem); | |
2bda0e17 | 835 | |
08b7c251 | 836 | return tvItem.cChildren != 0; |
2bda0e17 KB |
837 | } |
838 | ||
08b7c251 | 839 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
2bda0e17 | 840 | { |
08b7c251 VZ |
841 | // probably not a good idea to put it here |
842 | //wxASSERT( ItemHasChildren(item) ); | |
2bda0e17 | 843 | |
08b7c251 VZ |
844 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); |
845 | DoGetItem(&tvItem); | |
2bda0e17 | 846 | |
08b7c251 | 847 | return (tvItem.state & TVIS_EXPANDED) != 0; |
2bda0e17 KB |
848 | } |
849 | ||
08b7c251 | 850 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
2bda0e17 | 851 | { |
08b7c251 VZ |
852 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); |
853 | DoGetItem(&tvItem); | |
2bda0e17 | 854 | |
08b7c251 | 855 | return (tvItem.state & TVIS_SELECTED) != 0; |
2bda0e17 KB |
856 | } |
857 | ||
add28c55 VZ |
858 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const |
859 | { | |
860 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); | |
861 | DoGetItem(&tvItem); | |
862 | ||
863 | return (tvItem.state & TVIS_BOLD) != 0; | |
864 | } | |
865 | ||
08b7c251 VZ |
866 | // ---------------------------------------------------------------------------- |
867 | // navigation | |
868 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 869 | |
08b7c251 VZ |
870 | wxTreeItemId wxTreeCtrl::GetRootItem() const |
871 | { | |
d220ae32 | 872 | return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd())); |
08b7c251 | 873 | } |
2bda0e17 | 874 | |
08b7c251 VZ |
875 | wxTreeItemId wxTreeCtrl::GetSelection() const |
876 | { | |
9dfbf520 | 877 | wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (WXHTREEITEM)0, |
223d09f6 | 878 | wxT("this only works with single selection controls") ); |
9dfbf520 | 879 | |
d220ae32 | 880 | return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd())); |
2bda0e17 KB |
881 | } |
882 | ||
08b7c251 | 883 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const |
2bda0e17 | 884 | { |
d220ae32 | 885 | return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); |
08b7c251 | 886 | } |
2bda0e17 | 887 | |
08b7c251 | 888 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
06e38c8e | 889 | long& _cookie) const |
08b7c251 VZ |
890 | { |
891 | // remember the last child returned in 'cookie' | |
d220ae32 | 892 | _cookie = (long)TreeView_GetChild(GetHwnd(), (HTREEITEM) (WXHTREEITEM)item); |
2bda0e17 | 893 | |
06e38c8e | 894 | return wxTreeItemId((WXHTREEITEM)_cookie); |
2bda0e17 KB |
895 | } |
896 | ||
08b7c251 | 897 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), |
06e38c8e | 898 | long& _cookie) const |
2bda0e17 | 899 | { |
d220ae32 | 900 | wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(), |
23fd5130 VZ |
901 | (HTREEITEM)(WXHTREEITEM)_cookie)); |
902 | _cookie = (long)l; | |
903 | ||
2e5dddb0 | 904 | return l; |
08b7c251 | 905 | } |
2bda0e17 | 906 | |
978f38c2 VZ |
907 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
908 | { | |
909 | // can this be done more efficiently? | |
910 | long cookie; | |
911 | ||
912 | wxTreeItemId childLast, | |
2165ad93 | 913 | child = GetFirstChild(item, cookie); |
978f38c2 VZ |
914 | while ( child.IsOk() ) |
915 | { | |
916 | childLast = child; | |
2165ad93 | 917 | child = GetNextChild(item, cookie); |
978f38c2 VZ |
918 | } |
919 | ||
920 | return childLast; | |
921 | } | |
922 | ||
08b7c251 VZ |
923 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
924 | { | |
d220ae32 | 925 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); |
2bda0e17 KB |
926 | } |
927 | ||
08b7c251 | 928 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
2bda0e17 | 929 | { |
d220ae32 | 930 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); |
2bda0e17 KB |
931 | } |
932 | ||
08b7c251 | 933 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
2bda0e17 | 934 | { |
d220ae32 | 935 | return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd())); |
2bda0e17 KB |
936 | } |
937 | ||
08b7c251 | 938 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
2bda0e17 | 939 | { |
223d09f6 | 940 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() " |
837e5743 | 941 | "for must be visible itself!")); |
02ce7b72 | 942 | |
d220ae32 | 943 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); |
08b7c251 | 944 | } |
02ce7b72 | 945 | |
08b7c251 VZ |
946 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
947 | { | |
223d09f6 | 948 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() " |
837e5743 | 949 | "for must be visible itself!")); |
02ce7b72 | 950 | |
d220ae32 | 951 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item)); |
08b7c251 | 952 | } |
02ce7b72 | 953 | |
9dfbf520 VZ |
954 | // ---------------------------------------------------------------------------- |
955 | // multiple selections emulation | |
956 | // ---------------------------------------------------------------------------- | |
957 | ||
958 | bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const | |
959 | { | |
960 | // receive the desired information. | |
961 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
962 | DoGetItem(&tvItem); | |
963 | ||
964 | // state image indices are 1 based | |
965 | return ((tvItem.state >> 12) - 1) == 1; | |
966 | } | |
967 | ||
968 | void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check) | |
969 | { | |
970 | // receive the desired information. | |
971 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
972 | ||
973 | // state images are one-based | |
974 | tvItem.state = (check ? 2 : 1) << 12; | |
975 | ||
976 | DoSetItem(&tvItem); | |
977 | } | |
978 | ||
33961d59 RR |
979 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const |
980 | { | |
981 | TraverseSelections selector(this, selections); | |
9dfbf520 | 982 | |
e47c4d48 | 983 | return selector.GetCount(); |
9dfbf520 VZ |
984 | } |
985 | ||
08b7c251 VZ |
986 | // ---------------------------------------------------------------------------- |
987 | // Usual operations | |
988 | // ---------------------------------------------------------------------------- | |
02ce7b72 | 989 | |
08b7c251 VZ |
990 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, |
991 | wxTreeItemId hInsertAfter, | |
992 | const wxString& text, | |
993 | int image, int selectedImage, | |
994 | wxTreeItemData *data) | |
995 | { | |
996 | TV_INSERTSTRUCT tvIns; | |
06e38c8e JS |
997 | tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent; |
998 | tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter; | |
58a8ab88 | 999 | |
74b31181 VZ |
1000 | // this is how we insert the item as the first child: supply a NULL |
1001 | // hInsertAfter | |
1002 | if ( !tvIns.hInsertAfter ) | |
58a8ab88 JS |
1003 | { |
1004 | tvIns.hInsertAfter = TVI_FIRST; | |
1005 | } | |
1006 | ||
08b7c251 VZ |
1007 | UINT mask = 0; |
1008 | if ( !text.IsEmpty() ) | |
1009 | { | |
1010 | mask |= TVIF_TEXT; | |
837e5743 | 1011 | tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok |
08b7c251 | 1012 | } |
02ce7b72 | 1013 | |
08b7c251 VZ |
1014 | if ( image != -1 ) |
1015 | { | |
1016 | mask |= TVIF_IMAGE; | |
1017 | tvIns.item.iImage = image; | |
3a5a2f56 | 1018 | |
6b037754 | 1019 | if ( selectedImage == -1 ) |
3a5a2f56 VZ |
1020 | { |
1021 | // take the same image for selected icon if not specified | |
1022 | selectedImage = image; | |
1023 | } | |
08b7c251 | 1024 | } |
02ce7b72 | 1025 | |
08b7c251 VZ |
1026 | if ( selectedImage != -1 ) |
1027 | { | |
1028 | mask |= TVIF_SELECTEDIMAGE; | |
1029 | tvIns.item.iSelectedImage = selectedImage; | |
1030 | } | |
02ce7b72 | 1031 | |
08b7c251 VZ |
1032 | if ( data != NULL ) |
1033 | { | |
1034 | mask |= TVIF_PARAM; | |
1035 | tvIns.item.lParam = (LPARAM)data; | |
1036 | } | |
02ce7b72 | 1037 | |
08b7c251 | 1038 | tvIns.item.mask = mask; |
02ce7b72 | 1039 | |
d220ae32 | 1040 | HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns); |
08b7c251 VZ |
1041 | if ( id == 0 ) |
1042 | { | |
1043 | wxLogLastError("TreeView_InsertItem"); | |
1044 | } | |
02ce7b72 | 1045 | |
fd3f686c VZ |
1046 | if ( data != NULL ) |
1047 | { | |
1048 | // associate the application tree item with Win32 tree item handle | |
1049 | data->SetId((WXHTREEITEM)id); | |
1050 | } | |
1051 | ||
06e38c8e | 1052 | return wxTreeItemId((WXHTREEITEM)id); |
2bda0e17 KB |
1053 | } |
1054 | ||
08b7c251 VZ |
1055 | // for compatibility only |
1056 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, | |
1057 | const wxString& text, | |
1058 | int image, int selImage, | |
1059 | long insertAfter) | |
2bda0e17 | 1060 | { |
06e38c8e | 1061 | return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text, |
08b7c251 | 1062 | image, selImage, NULL); |
2bda0e17 KB |
1063 | } |
1064 | ||
08b7c251 VZ |
1065 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
1066 | int image, int selectedImage, | |
1067 | wxTreeItemData *data) | |
2bda0e17 | 1068 | { |
06e38c8e | 1069 | return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0, |
08b7c251 | 1070 | text, image, selectedImage, data); |
2bda0e17 KB |
1071 | } |
1072 | ||
08b7c251 VZ |
1073 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, |
1074 | const wxString& text, | |
1075 | int image, int selectedImage, | |
1076 | wxTreeItemData *data) | |
2bda0e17 | 1077 | { |
06e38c8e | 1078 | return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST, |
08b7c251 | 1079 | text, image, selectedImage, data); |
2bda0e17 KB |
1080 | } |
1081 | ||
08b7c251 VZ |
1082 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
1083 | const wxTreeItemId& idPrevious, | |
1084 | const wxString& text, | |
1085 | int image, int selectedImage, | |
1086 | wxTreeItemData *data) | |
2bda0e17 | 1087 | { |
08b7c251 | 1088 | return DoInsertItem(parent, idPrevious, text, image, selectedImage, data); |
2bda0e17 KB |
1089 | } |
1090 | ||
2ef31e80 VZ |
1091 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
1092 | size_t index, | |
1093 | const wxString& text, | |
1094 | int image, int selectedImage, | |
1095 | wxTreeItemData *data) | |
1096 | { | |
1097 | // find the item from index | |
1098 | long cookie; | |
1099 | wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie); | |
1100 | while ( index != 0 && idCur.IsOk() ) | |
1101 | { | |
1102 | index--; | |
1103 | ||
1104 | idPrev = idCur; | |
1105 | idCur = GetNextChild(parent, cookie); | |
1106 | } | |
1107 | ||
1108 | // assert, not check: if the index is invalid, we will append the item | |
1109 | // to the end | |
1110 | wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") ); | |
1111 | ||
1112 | return DoInsertItem(parent, idPrev, text, image, selectedImage, data); | |
1113 | } | |
1114 | ||
08b7c251 VZ |
1115 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent, |
1116 | const wxString& text, | |
1117 | int image, int selectedImage, | |
1118 | wxTreeItemData *data) | |
2bda0e17 | 1119 | { |
06e38c8e | 1120 | return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST, |
08b7c251 | 1121 | text, image, selectedImage, data); |
2bda0e17 KB |
1122 | } |
1123 | ||
08b7c251 | 1124 | void wxTreeCtrl::Delete(const wxTreeItemId& item) |
2bda0e17 | 1125 | { |
d220ae32 | 1126 | if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item) ) |
bbcdf8bc | 1127 | { |
08b7c251 | 1128 | wxLogLastError("TreeView_DeleteItem"); |
bbcdf8bc | 1129 | } |
bbcdf8bc JS |
1130 | } |
1131 | ||
23fd5130 VZ |
1132 | // delete all children (but don't delete the item itself) |
1133 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item) | |
1134 | { | |
1135 | long cookie; | |
1136 | ||
1137 | wxArrayLong children; | |
1138 | wxTreeItemId child = GetFirstChild(item, cookie); | |
1139 | while ( child.IsOk() ) | |
1140 | { | |
1141 | children.Add((long)(WXHTREEITEM)child); | |
1142 | ||
1143 | child = GetNextChild(item, cookie); | |
1144 | } | |
1145 | ||
1146 | size_t nCount = children.Count(); | |
1147 | for ( size_t n = 0; n < nCount; n++ ) | |
1148 | { | |
d220ae32 | 1149 | if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) ) |
23fd5130 VZ |
1150 | { |
1151 | wxLogLastError("TreeView_DeleteItem"); | |
1152 | } | |
1153 | } | |
1154 | } | |
1155 | ||
08b7c251 | 1156 | void wxTreeCtrl::DeleteAllItems() |
bbcdf8bc | 1157 | { |
d220ae32 | 1158 | if ( !TreeView_DeleteAllItems(GetHwnd()) ) |
bbcdf8bc | 1159 | { |
08b7c251 | 1160 | wxLogLastError("TreeView_DeleteAllItems"); |
bbcdf8bc | 1161 | } |
2bda0e17 KB |
1162 | } |
1163 | ||
08b7c251 | 1164 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) |
2bda0e17 | 1165 | { |
dd3646fd VZ |
1166 | wxASSERT_MSG( flag == TVE_COLLAPSE || |
1167 | flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) || | |
1168 | flag == TVE_EXPAND || | |
1169 | flag == TVE_TOGGLE, | |
223d09f6 | 1170 | wxT("Unknown flag in wxTreeCtrl::DoExpand") ); |
08b7c251 VZ |
1171 | |
1172 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must | |
d220ae32 VZ |
1173 | // emulate them. This behaviour has changed slightly with comctl32.dll |
1174 | // v 4.70 - now it does send them but only the first time. To maintain | |
1175 | // compatible behaviour and also in order to not have surprises with the | |
1176 | // future versions, don't rely on this and still do everything ourselves. | |
1177 | // To avoid that the messages be sent twice when the item is expanded for | |
1178 | // the first time we must clear TVIS_EXPANDEDONCE style manually. | |
1179 | ||
1180 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE); | |
1181 | tvItem.state = 0; | |
1182 | DoSetItem(&tvItem); | |
1183 | ||
1184 | if ( TreeView_Expand(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item, flag) != 0 ) | |
08b7c251 VZ |
1185 | { |
1186 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1187 | event.m_item = item; | |
1188 | ||
1189 | bool isExpanded = IsExpanded(item); | |
2bda0e17 | 1190 | |
08b7c251 | 1191 | event.SetEventObject(this); |
2bda0e17 | 1192 | |
d220ae32 | 1193 | // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded |
08b7c251 VZ |
1194 | event.SetEventType(g_events[isExpanded][TRUE]); |
1195 | GetEventHandler()->ProcessEvent(event); | |
2bda0e17 | 1196 | |
08b7c251 VZ |
1197 | event.SetEventType(g_events[isExpanded][FALSE]); |
1198 | GetEventHandler()->ProcessEvent(event); | |
1199 | } | |
d220ae32 | 1200 | //else: change didn't took place, so do nothing at all |
2bda0e17 KB |
1201 | } |
1202 | ||
08b7c251 | 1203 | void wxTreeCtrl::Expand(const wxTreeItemId& item) |
2bda0e17 | 1204 | { |
08b7c251 | 1205 | DoExpand(item, TVE_EXPAND); |
2bda0e17 | 1206 | } |
2bda0e17 | 1207 | |
08b7c251 | 1208 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) |
2bda0e17 | 1209 | { |
08b7c251 | 1210 | DoExpand(item, TVE_COLLAPSE); |
2bda0e17 KB |
1211 | } |
1212 | ||
08b7c251 | 1213 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
2bda0e17 | 1214 | { |
dd3646fd | 1215 | DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET); |
2bda0e17 KB |
1216 | } |
1217 | ||
08b7c251 | 1218 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) |
2bda0e17 | 1219 | { |
08b7c251 | 1220 | DoExpand(item, TVE_TOGGLE); |
2bda0e17 KB |
1221 | } |
1222 | ||
42c5812d UU |
1223 | void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action) |
1224 | { | |
9dfbf520 | 1225 | DoExpand(item, action); |
42c5812d UU |
1226 | } |
1227 | ||
08b7c251 | 1228 | void wxTreeCtrl::Unselect() |
2bda0e17 | 1229 | { |
223d09f6 | 1230 | wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxT("doesn't make sense") ); |
9dfbf520 VZ |
1231 | |
1232 | // just remove the selection | |
06e38c8e | 1233 | SelectItem(wxTreeItemId((WXHTREEITEM) 0)); |
08b7c251 | 1234 | } |
02ce7b72 | 1235 | |
9dfbf520 | 1236 | void wxTreeCtrl::UnselectAll() |
08b7c251 | 1237 | { |
9dfbf520 | 1238 | if ( m_windowStyle & wxTR_MULTIPLE ) |
2bda0e17 | 1239 | { |
9dfbf520 VZ |
1240 | wxArrayTreeItemIds selections; |
1241 | size_t count = GetSelections(selections); | |
1242 | for ( size_t n = 0; n < count; n++ ) | |
d220ae32 | 1243 | { |
9dfbf520 | 1244 | SetItemCheck(selections[n], FALSE); |
d220ae32 | 1245 | } |
9dfbf520 VZ |
1246 | } |
1247 | else | |
1248 | { | |
1249 | // just remove the selection | |
1250 | Unselect(); | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item) | |
1255 | { | |
1256 | if ( m_windowStyle & wxTR_MULTIPLE ) | |
1257 | { | |
1258 | // selecting the item means checking it | |
1259 | SetItemCheck(item); | |
1260 | } | |
1261 | else | |
1262 | { | |
1263 | // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive | |
1264 | // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so | |
1265 | // send them ourselves | |
1266 | ||
1267 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1268 | event.m_item = item; | |
1269 | event.SetEventObject(this); | |
1270 | ||
1271 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING); | |
1272 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
d220ae32 | 1273 | { |
9dfbf520 VZ |
1274 | if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) ) |
1275 | { | |
1276 | wxLogLastError("TreeView_SelectItem"); | |
1277 | } | |
1278 | else | |
1279 | { | |
1280 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); | |
1281 | (void)GetEventHandler()->ProcessEvent(event); | |
1282 | } | |
d220ae32 | 1283 | } |
9dfbf520 | 1284 | //else: program vetoed the change |
2bda0e17 | 1285 | } |
08b7c251 | 1286 | } |
2bda0e17 | 1287 | |
08b7c251 VZ |
1288 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
1289 | { | |
1290 | // no error return | |
d220ae32 | 1291 | TreeView_EnsureVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item); |
08b7c251 VZ |
1292 | } |
1293 | ||
1294 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) | |
1295 | { | |
d220ae32 | 1296 | if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) ) |
2bda0e17 | 1297 | { |
08b7c251 | 1298 | wxLogLastError("TreeView_SelectSetFirstVisible"); |
2bda0e17 | 1299 | } |
08b7c251 VZ |
1300 | } |
1301 | ||
1302 | wxTextCtrl* wxTreeCtrl::GetEditControl() const | |
1303 | { | |
1304 | return m_textCtrl; | |
1305 | } | |
1306 | ||
1307 | void wxTreeCtrl::DeleteTextCtrl() | |
1308 | { | |
1309 | if ( m_textCtrl ) | |
2bda0e17 | 1310 | { |
08b7c251 VZ |
1311 | m_textCtrl->UnsubclassWin(); |
1312 | m_textCtrl->SetHWND(0); | |
1313 | delete m_textCtrl; | |
1314 | m_textCtrl = NULL; | |
2bda0e17 | 1315 | } |
08b7c251 | 1316 | } |
2bda0e17 | 1317 | |
08b7c251 VZ |
1318 | wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, |
1319 | wxClassInfo* textControlClass) | |
1320 | { | |
1321 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
1322 | ||
d220ae32 | 1323 | HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item); |
2bda0e17 | 1324 | |
5ea47806 VZ |
1325 | // this is not an error - the TVN_BEGINLABELEDIT handler might have |
1326 | // returned FALSE | |
1327 | if ( !hWnd ) | |
1328 | { | |
1329 | return NULL; | |
1330 | } | |
2bda0e17 | 1331 | |
08b7c251 | 1332 | DeleteTextCtrl(); |
2bda0e17 | 1333 | |
08b7c251 VZ |
1334 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); |
1335 | m_textCtrl->SetHWND((WXHWND)hWnd); | |
1336 | m_textCtrl->SubclassWin((WXHWND)hWnd); | |
2bda0e17 | 1337 | |
08b7c251 | 1338 | return m_textCtrl; |
2bda0e17 KB |
1339 | } |
1340 | ||
08b7c251 VZ |
1341 | // End label editing, optionally cancelling the edit |
1342 | void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) | |
2bda0e17 | 1343 | { |
d220ae32 | 1344 | TreeView_EndEditLabelNow(GetHwnd(), discardChanges); |
08b7c251 VZ |
1345 | |
1346 | DeleteTextCtrl(); | |
2bda0e17 KB |
1347 | } |
1348 | ||
08b7c251 | 1349 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) |
2bda0e17 | 1350 | { |
08b7c251 VZ |
1351 | TV_HITTESTINFO hitTestInfo; |
1352 | hitTestInfo.pt.x = (int)point.x; | |
1353 | hitTestInfo.pt.y = (int)point.y; | |
2bda0e17 | 1354 | |
d220ae32 | 1355 | TreeView_HitTest(GetHwnd(), &hitTestInfo); |
2bda0e17 | 1356 | |
08b7c251 VZ |
1357 | flags = 0; |
1358 | ||
1359 | // avoid repetition | |
1360 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ | |
1361 | flags |= wxTREE_HITTEST_##flag | |
1362 | ||
1363 | TRANSLATE_FLAG(ABOVE); | |
1364 | TRANSLATE_FLAG(BELOW); | |
1365 | TRANSLATE_FLAG(NOWHERE); | |
1366 | TRANSLATE_FLAG(ONITEMBUTTON); | |
1367 | TRANSLATE_FLAG(ONITEMICON); | |
1368 | TRANSLATE_FLAG(ONITEMINDENT); | |
1369 | TRANSLATE_FLAG(ONITEMLABEL); | |
1370 | TRANSLATE_FLAG(ONITEMRIGHT); | |
1371 | TRANSLATE_FLAG(ONITEMSTATEICON); | |
1372 | TRANSLATE_FLAG(TOLEFT); | |
1373 | TRANSLATE_FLAG(TORIGHT); | |
2bda0e17 | 1374 | |
08b7c251 VZ |
1375 | #undef TRANSLATE_FLAG |
1376 | ||
06e38c8e | 1377 | return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem); |
08b7c251 VZ |
1378 | } |
1379 | ||
f7c832a7 VZ |
1380 | bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item, |
1381 | wxRect& rect, | |
1382 | bool textOnly) const | |
1383 | { | |
1384 | RECT rc; | |
d220ae32 | 1385 | if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item, |
f7c832a7 VZ |
1386 | &rc, textOnly) ) |
1387 | { | |
1388 | rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom)); | |
1389 | ||
1390 | return TRUE; | |
1391 | } | |
1392 | else | |
1393 | { | |
1394 | // couldn't retrieve rect: for example, item isn't visible | |
1395 | return FALSE; | |
1396 | } | |
1397 | } | |
1398 | ||
23fd5130 VZ |
1399 | // ---------------------------------------------------------------------------- |
1400 | // sorting stuff | |
1401 | // ---------------------------------------------------------------------------- | |
f7c832a7 | 1402 | |
23fd5130 VZ |
1403 | static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1, |
1404 | wxTreeItemData *pItem2, | |
1405 | wxTreeCtrl *tree) | |
1406 | { | |
096c9f9b | 1407 | wxCHECK_MSG( pItem1 && pItem2, 0, |
223d09f6 | 1408 | wxT("sorting tree without data doesn't make sense") ); |
096c9f9b | 1409 | |
23fd5130 VZ |
1410 | return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId()); |
1411 | } | |
1412 | ||
95aabccc VZ |
1413 | int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
1414 | const wxTreeItemId& item2) | |
08b7c251 | 1415 | { |
837e5743 | 1416 | return wxStrcmp(GetItemText(item1), GetItemText(item2)); |
95aabccc VZ |
1417 | } |
1418 | ||
1419 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item) | |
1420 | { | |
1421 | // rely on the fact that TreeView_SortChildren does the same thing as our | |
23fd5130 VZ |
1422 | // default behaviour, i.e. sorts items alphabetically and so call it |
1423 | // directly if we're not in derived class (much more efficient!) | |
1424 | if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) ) | |
2bda0e17 | 1425 | { |
d220ae32 | 1426 | TreeView_SortChildren(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item, 0); |
2bda0e17 | 1427 | } |
08b7c251 | 1428 | else |
2bda0e17 | 1429 | { |
62448488 | 1430 | TV_SORTCB tvSort; |
23fd5130 VZ |
1431 | tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item; |
1432 | tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback; | |
1433 | tvSort.lParam = (LPARAM)this; | |
d220ae32 | 1434 | TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */); |
2bda0e17 | 1435 | } |
08b7c251 | 1436 | } |
2bda0e17 | 1437 | |
08b7c251 VZ |
1438 | // ---------------------------------------------------------------------------- |
1439 | // implementation | |
1440 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1441 | |
08b7c251 VZ |
1442 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id) |
1443 | { | |
1444 | if ( cmd == EN_UPDATE ) | |
2bda0e17 | 1445 | { |
08b7c251 VZ |
1446 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); |
1447 | event.SetEventObject( this ); | |
1448 | ProcessCommand(event); | |
2bda0e17 | 1449 | } |
08b7c251 | 1450 | else if ( cmd == EN_KILLFOCUS ) |
2bda0e17 | 1451 | { |
08b7c251 VZ |
1452 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); |
1453 | event.SetEventObject( this ); | |
1454 | ProcessCommand(event); | |
2bda0e17 | 1455 | } |
08b7c251 | 1456 | else |
2bda0e17 | 1457 | { |
08b7c251 VZ |
1458 | // nothing done |
1459 | return FALSE; | |
2bda0e17 | 1460 | } |
08b7c251 VZ |
1461 | |
1462 | // command processed | |
1463 | return TRUE; | |
1464 | } | |
1465 | ||
1466 | // process WM_NOTIFY Windows message | |
a23fd0e1 | 1467 | bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
08b7c251 VZ |
1468 | { |
1469 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
1470 | wxEventType eventType = wxEVT_NULL; | |
1471 | NMHDR *hdr = (NMHDR *)lParam; | |
1472 | ||
1473 | switch ( hdr->code ) | |
2bda0e17 | 1474 | { |
84a6b859 | 1475 | case NM_RCLICK: |
52f13e49 | 1476 | { |
696e1ea0 VZ |
1477 | if ( wxControl::MSWOnNotify(idCtrl, lParam, result) ) |
1478 | return TRUE; | |
1479 | ||
1480 | TV_HITTESTINFO tvhti; | |
1481 | ::GetCursorPos(&(tvhti.pt)); | |
1482 | ::ScreenToClient(GetHwnd(),&(tvhti.pt)); | |
1483 | if ( TreeView_HitTest(GetHwnd(),&tvhti) ) | |
52f13e49 | 1484 | { |
696e1ea0 VZ |
1485 | if( tvhti.flags & TVHT_ONITEM ) |
1486 | { | |
1487 | event.m_item = (WXHTREEITEM) tvhti.hItem; | |
1488 | eventType = wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK; | |
1489 | } | |
52f13e49 VZ |
1490 | } |
1491 | } | |
1492 | break; | |
52f13e49 | 1493 | |
08b7c251 VZ |
1494 | case TVN_BEGINDRAG: |
1495 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
1496 | // fall through | |
1497 | ||
1498 | case TVN_BEGINRDRAG: | |
1499 | { | |
1500 | if ( eventType == wxEVT_NULL ) | |
1501 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; | |
1502 | //else: left drag, already set above | |
1503 | ||
1504 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
1505 | ||
06e38c8e | 1506 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
08b7c251 | 1507 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); |
08b7c251 | 1508 | } |
696e1ea0 | 1509 | break; |
08b7c251 VZ |
1510 | |
1511 | case TVN_BEGINLABELEDIT: | |
1512 | { | |
1513 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; | |
1514 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
1515 | ||
06e38c8e | 1516 | event.m_item = (WXHTREEITEM) info->item.hItem; |
5ea47806 | 1517 | event.m_label = info->item.pszText; |
08b7c251 | 1518 | } |
696e1ea0 | 1519 | break; |
08b7c251 VZ |
1520 | |
1521 | case TVN_DELETEITEM: | |
1522 | { | |
1523 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; | |
1524 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
1525 | ||
696e1ea0 VZ |
1526 | event.m_item = (WXHTREEITEM)tv->itemOld.hItem; |
1527 | ||
1528 | if ( m_hasAnyAttr ) | |
1529 | { | |
1530 | delete (wxTreeItemAttr *)m_attrs. | |
1531 | Delete((long)tv->itemOld.hItem); | |
1532 | } | |
08b7c251 | 1533 | } |
696e1ea0 | 1534 | break; |
08b7c251 VZ |
1535 | |
1536 | case TVN_ENDLABELEDIT: | |
1537 | { | |
1538 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; | |
1539 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
1540 | ||
5ea47806 VZ |
1541 | event.m_item = (WXHTREEITEM)info->item.hItem; |
1542 | event.m_label = info->item.pszText; | |
1ee4ead5 VZ |
1543 | if (info->item.pszText == NULL) |
1544 | return FALSE; | |
08b7c251 VZ |
1545 | break; |
1546 | } | |
1547 | ||
1548 | case TVN_GETDISPINFO: | |
1549 | eventType = wxEVT_COMMAND_TREE_GET_INFO; | |
1550 | // fall through | |
1551 | ||
1552 | case TVN_SETDISPINFO: | |
1553 | { | |
1554 | if ( eventType == wxEVT_NULL ) | |
1555 | eventType = wxEVT_COMMAND_TREE_SET_INFO; | |
1556 | //else: get, already set above | |
1557 | ||
1558 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
1559 | ||
06e38c8e | 1560 | event.m_item = (WXHTREEITEM) info->item.hItem; |
08b7c251 VZ |
1561 | break; |
1562 | } | |
1563 | ||
1564 | case TVN_ITEMEXPANDING: | |
1565 | event.m_code = FALSE; | |
1566 | // fall through | |
1567 | ||
1568 | case TVN_ITEMEXPANDED: | |
1569 | { | |
1570 | NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam; | |
1571 | ||
1572 | bool expand = FALSE; | |
1573 | switch ( tv->action ) | |
1574 | { | |
1575 | case TVE_EXPAND: | |
1576 | expand = TRUE; | |
1577 | break; | |
1578 | ||
1579 | case TVE_COLLAPSE: | |
1580 | expand = FALSE; | |
1581 | break; | |
1582 | ||
1583 | default: | |
223d09f6 | 1584 | wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND " |
837e5743 | 1585 | "message"), tv->action); |
08b7c251 VZ |
1586 | } |
1587 | ||
a17e237f | 1588 | bool ing = ((int)hdr->code == TVN_ITEMEXPANDING); |
08b7c251 VZ |
1589 | eventType = g_events[expand][ing]; |
1590 | ||
06e38c8e | 1591 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
08b7c251 | 1592 | } |
696e1ea0 | 1593 | break; |
08b7c251 VZ |
1594 | |
1595 | case TVN_KEYDOWN: | |
1596 | { | |
1597 | eventType = wxEVT_COMMAND_TREE_KEY_DOWN; | |
1598 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; | |
1599 | ||
1600 | event.m_code = wxCharCodeMSWToWX(info->wVKey); | |
23fd5130 VZ |
1601 | |
1602 | // a separate event for this case | |
1603 | if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN ) | |
1604 | { | |
1605 | wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, | |
1606 | m_windowId); | |
1607 | event2.SetEventObject(this); | |
1608 | ||
1609 | GetEventHandler()->ProcessEvent(event2); | |
1610 | } | |
08b7c251 | 1611 | } |
696e1ea0 | 1612 | break; |
08b7c251 VZ |
1613 | |
1614 | case TVN_SELCHANGED: | |
1615 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; | |
1616 | // fall through | |
1617 | ||
1618 | case TVN_SELCHANGING: | |
1619 | { | |
1620 | if ( eventType == wxEVT_NULL ) | |
1621 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; | |
1622 | //else: already set above | |
1623 | ||
1624 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
1625 | ||
06e38c8e JS |
1626 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
1627 | event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem; | |
08b7c251 | 1628 | } |
696e1ea0 VZ |
1629 | break; |
1630 | ||
6ecfe2ac | 1631 | #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 |
696e1ea0 VZ |
1632 | case NM_CUSTOMDRAW: |
1633 | { | |
1634 | LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam; | |
1635 | NMCUSTOMDRAW& nmcd = lptvcd->nmcd; | |
1636 | switch( nmcd.dwDrawStage ) | |
1637 | { | |
1638 | case CDDS_PREPAINT: | |
1639 | // if we've got any items with non standard attributes, | |
1640 | // notify us before painting each item | |
1641 | *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW | |
1642 | : CDRF_DODEFAULT; | |
1643 | return TRUE; | |
1644 | ||
1645 | case CDDS_ITEMPREPAINT: | |
1646 | { | |
1647 | wxTreeItemAttr *attr = | |
1648 | (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec); | |
1649 | ||
1650 | if ( !attr ) | |
1651 | { | |
1652 | // nothing to do for this item | |
1653 | return CDRF_DODEFAULT; | |
1654 | } | |
1655 | ||
1656 | HFONT hFont; | |
1657 | wxColour colText, colBack; | |
1658 | if ( attr->HasFont() ) | |
1659 | { | |
1660 | wxFont font = attr->GetFont(); | |
1661 | hFont = (HFONT)font.GetResourceHandle(); | |
1662 | } | |
1663 | else | |
1664 | { | |
1665 | hFont = 0; | |
1666 | } | |
1667 | ||
1668 | if ( attr->HasTextColour() ) | |
1669 | { | |
1670 | colText = attr->GetTextColour(); | |
1671 | } | |
1672 | else | |
1673 | { | |
1674 | colText = GetForegroundColour(); | |
1675 | } | |
1676 | ||
1677 | // selection colours should override ours | |
1678 | if ( nmcd.uItemState & CDIS_SELECTED ) | |
1679 | { | |
1680 | DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT); | |
1681 | lptvcd->clrTextBk = clrBk; | |
1682 | ||
1683 | // try to make the text visible | |
1684 | lptvcd->clrText = wxColourToRGB(colText); | |
1685 | lptvcd->clrText |= ~clrBk; | |
1686 | lptvcd->clrText &= 0x00ffffff; | |
1687 | } | |
1688 | else | |
1689 | { | |
1690 | if ( attr->HasBackgroundColour() ) | |
1691 | { | |
1692 | colBack = attr->GetBackgroundColour(); | |
1693 | } | |
1694 | else | |
1695 | { | |
1696 | colBack = GetBackgroundColour(); | |
1697 | } | |
1698 | ||
1699 | lptvcd->clrText = wxColourToRGB(colText); | |
1700 | lptvcd->clrTextBk = wxColourToRGB(colBack); | |
1701 | } | |
1702 | ||
1703 | // note that if we wanted to set colours for | |
1704 | // individual columns (subitems), we would have | |
1705 | // returned CDRF_NOTIFYSUBITEMREDRAW from here | |
1706 | if ( hFont ) | |
1707 | { | |
1708 | ::SelectObject(nmcd.hdc, hFont); | |
1709 | ||
1710 | *result = CDRF_NEWFONT; | |
1711 | } | |
1712 | else | |
1713 | { | |
1714 | *result = CDRF_DODEFAULT; | |
1715 | } | |
1716 | ||
1717 | return TRUE; | |
1718 | } | |
1719 | ||
1720 | default: | |
1721 | *result = CDRF_DODEFAULT; | |
1722 | return TRUE; | |
1723 | } | |
1724 | } | |
1725 | break; | |
6ecfe2ac | 1726 | #endif // _WIN32_IE >= 0x300 |
08b7c251 VZ |
1727 | |
1728 | default: | |
a23fd0e1 | 1729 | return wxControl::MSWOnNotify(idCtrl, lParam, result); |
2bda0e17 | 1730 | } |
08b7c251 VZ |
1731 | |
1732 | event.SetEventObject(this); | |
1733 | event.SetEventType(eventType); | |
1734 | ||
fd3f686c | 1735 | bool processed = GetEventHandler()->ProcessEvent(event); |
08b7c251 VZ |
1736 | |
1737 | // post processing | |
5ea47806 | 1738 | switch ( hdr->code ) |
2bda0e17 | 1739 | { |
5ea47806 VZ |
1740 | case TVN_DELETEITEM: |
1741 | { | |
1742 | // NB: we might process this message using wxWindows event | |
1743 | // tables, but due to overhead of wxWin event system we | |
1744 | // prefer to do it here ourself (otherwise deleting a tree | |
1745 | // with many items is just too slow) | |
1746 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
74b31181 VZ |
1747 | |
1748 | wxTreeItemId item = event.m_item; | |
1749 | if ( HasIndirectData(item) ) | |
1750 | { | |
1751 | wxTreeItemIndirectData *data = (wxTreeItemIndirectData *) | |
1752 | tv->itemOld.lParam; | |
1753 | delete data; // can't be NULL here | |
1754 | ||
1755 | m_itemsWithIndirectData.Remove(item); | |
1756 | } | |
1757 | else | |
1758 | { | |
1759 | wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam; | |
1760 | delete data; // may be NULL, ok | |
1761 | } | |
08b7c251 | 1762 | |
5ea47806 VZ |
1763 | processed = TRUE; // Make sure we don't get called twice |
1764 | } | |
1765 | break; | |
1766 | ||
1767 | case TVN_BEGINLABELEDIT: | |
1768 | // return TRUE to cancel label editing | |
1769 | *result = !event.IsAllowed(); | |
1770 | break; | |
1771 | ||
1772 | case TVN_ENDLABELEDIT: | |
1773 | // return TRUE to set the label to the new string | |
1774 | *result = event.IsAllowed(); | |
1775 | ||
1776 | // ensure that we don't have the text ctrl which is going to be | |
1777 | // deleted any more | |
1778 | DeleteTextCtrl(); | |
1779 | break; | |
1780 | ||
1781 | case TVN_SELCHANGING: | |
1782 | case TVN_ITEMEXPANDING: | |
1783 | // return TRUE to prevent the action from happening | |
1784 | *result = !event.IsAllowed(); | |
1785 | break; | |
1786 | ||
74b31181 VZ |
1787 | case TVN_GETDISPINFO: |
1788 | // NB: so far the user can't set the image himself anyhow, so do it | |
1789 | // anyway - but this may change later | |
1790 | if ( /* !processed && */ 1 ) | |
1791 | { | |
1792 | wxTreeItemId item = event.m_item; | |
1793 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
1794 | if ( info->item.mask & TVIF_IMAGE ) | |
1795 | { | |
1796 | info->item.iImage = | |
1797 | DoGetItemImageFromData | |
1798 | ( | |
1799 | item, | |
1800 | IsExpanded(item) ? wxTreeItemIcon_Expanded | |
1801 | : wxTreeItemIcon_Normal | |
1802 | ); | |
1803 | } | |
1804 | if ( info->item.mask & TVIF_SELECTEDIMAGE ) | |
1805 | { | |
1806 | info->item.iSelectedImage = | |
1807 | DoGetItemImageFromData | |
1808 | ( | |
1809 | item, | |
1810 | IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded | |
1811 | : wxTreeItemIcon_Selected | |
1812 | ); | |
1813 | } | |
1814 | } | |
1815 | break; | |
1816 | ||
5ea47806 VZ |
1817 | //default: |
1818 | // for the other messages the return value is ignored and there is | |
1819 | // nothing special to do | |
1820 | } | |
fd3f686c VZ |
1821 | |
1822 | return processed; | |
2bda0e17 KB |
1823 | } |
1824 | ||
08b7c251 | 1825 | // ---------------------------------------------------------------------------- |
2bda0e17 | 1826 | // Tree event |
08b7c251 VZ |
1827 | // ---------------------------------------------------------------------------- |
1828 | ||
92976ab6 | 1829 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent) |
2bda0e17 | 1830 | |
08b7c251 | 1831 | wxTreeEvent::wxTreeEvent(wxEventType commandType, int id) |
fd3f686c | 1832 | : wxNotifyEvent(commandType, id) |
2bda0e17 | 1833 | { |
08b7c251 VZ |
1834 | m_code = 0; |
1835 | m_itemOld = 0; | |
2bda0e17 KB |
1836 | } |
1837 | ||
08b7c251 | 1838 | #endif // __WIN95__ |
2bda0e17 | 1839 |