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