]>
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 | ||
30 | #ifndef WX_PRECOMP | |
08b7c251 | 31 | #include "wx.h" |
2bda0e17 KB |
32 | #endif |
33 | ||
2bda0e17 KB |
34 | #if defined(__WIN95__) |
35 | ||
08b7c251 VZ |
36 | #include "wx/log.h" |
37 | #include "wx/imaglist.h" | |
38 | ||
2bda0e17 KB |
39 | #include "wx/msw/private.h" |
40 | ||
41 | #ifndef __GNUWIN32__ | |
08b7c251 | 42 | #include <commctrl.h> |
2bda0e17 KB |
43 | #endif |
44 | ||
06e38c8e JS |
45 | #ifdef GetFirstChild |
46 | #undef GetFirstChild | |
47 | #endif | |
48 | ||
49 | #ifdef GetNextChild | |
50 | #undef GetNextChild | |
51 | #endif | |
52 | ||
53 | #ifdef GetNextSibling | |
54 | #undef GetNextSibling | |
55 | #endif | |
56 | ||
08b7c251 VZ |
57 | #include "wx/treectrl.h" |
58 | ||
2bda0e17 KB |
59 | // Bug in headers, sometimes |
60 | #ifndef TVIS_FOCUSED | |
08b7c251 | 61 | #define TVIS_FOCUSED 0x0001 |
2bda0e17 KB |
62 | #endif |
63 | ||
08b7c251 VZ |
64 | // ---------------------------------------------------------------------------- |
65 | // private classes | |
66 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 67 | |
08b7c251 VZ |
68 | // a convenient wrapper around TV_ITEM struct which adds a ctor |
69 | struct wxTreeViewItem : public TV_ITEM | |
70 | { | |
71 | wxTreeViewItem(const wxTreeItemId& item, | |
72 | UINT mask_, UINT stateMask_ = 0) | |
73 | { | |
74 | mask = mask_; | |
75 | stateMask = stateMask_; | |
06e38c8e | 76 | hItem = (HTREEITEM) (WXHTREEITEM) item; |
08b7c251 VZ |
77 | } |
78 | }; | |
2bda0e17 | 79 | |
08b7c251 VZ |
80 | // ---------------------------------------------------------------------------- |
81 | // macros | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | #if !USE_SHARED_LIBRARY | |
85 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) | |
2bda0e17 KB |
86 | #endif |
87 | ||
08b7c251 | 88 | // hide the ugly cast (of course, the macro is _quite_ ugly too...) |
06e38c8e | 89 | #define wxhWnd ((HWND)m_hWnd) |
08b7c251 VZ |
90 | |
91 | // ---------------------------------------------------------------------------- | |
92 | // variables | |
93 | // ---------------------------------------------------------------------------- | |
94 | ||
95 | // handy table for sending events | |
96 | static const wxEventType g_events[2][2] = | |
2bda0e17 | 97 | { |
08b7c251 VZ |
98 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, |
99 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } | |
100 | }; | |
101 | ||
102 | // ============================================================================ | |
103 | // implementation | |
104 | // ============================================================================ | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // construction and destruction | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | void wxTreeCtrl::Init() | |
111 | { | |
112 | m_imageListNormal = NULL; | |
113 | m_imageListState = NULL; | |
114 | m_textCtrl = NULL; | |
2bda0e17 KB |
115 | } |
116 | ||
08b7c251 VZ |
117 | bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, |
118 | const wxPoint& pos, const wxSize& size, | |
119 | long style, const wxValidator& validator, | |
120 | const wxString& name) | |
2bda0e17 | 121 | { |
08b7c251 | 122 | Init(); |
2bda0e17 | 123 | |
08b7c251 | 124 | wxSystemSettings settings; |
2bda0e17 | 125 | |
08b7c251 VZ |
126 | SetName(name); |
127 | SetValidator(validator); | |
2bda0e17 | 128 | |
08b7c251 | 129 | m_windowStyle = style; |
2bda0e17 | 130 | |
08b7c251 | 131 | SetParent(parent); |
2bda0e17 | 132 | |
08b7c251 | 133 | m_windowId = (id == -1) ? NewControlId() : id; |
2bda0e17 | 134 | |
08b7c251 | 135 | DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES; |
2bda0e17 | 136 | |
08b7c251 VZ |
137 | bool want3D; |
138 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; | |
2bda0e17 | 139 | |
08b7c251 VZ |
140 | // Even with extended styles, need to combine with WS_BORDER |
141 | // for them to look right. | |
142 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) | |
143 | { | |
144 | wstyle |= WS_BORDER; | |
145 | } | |
2bda0e17 | 146 | |
08b7c251 VZ |
147 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) |
148 | wstyle |= TVS_HASBUTTONS; | |
2bda0e17 | 149 | |
08b7c251 VZ |
150 | if ( m_windowStyle & wxTR_EDIT_LABELS ) |
151 | wstyle |= TVS_EDITLABELS; | |
2bda0e17 | 152 | |
08b7c251 VZ |
153 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) |
154 | wstyle |= TVS_LINESATROOT; | |
2bda0e17 | 155 | |
08b7c251 VZ |
156 | // Create the tree control. |
157 | m_hWnd = (WXHWND)::CreateWindowEx | |
158 | ( | |
159 | exStyle, | |
160 | WC_TREEVIEW, | |
161 | "", | |
162 | wstyle, | |
163 | pos.x, pos.y, size.x, size.y, | |
164 | (HWND)parent->GetHWND(), | |
165 | (HMENU)m_windowId, | |
166 | wxGetInstance(), | |
167 | NULL | |
168 | ); | |
7798a18e | 169 | |
08b7c251 | 170 | wxCHECK_MSG( m_hWnd, FALSE, "Failed to create tree ctrl" ); |
2bda0e17 | 171 | |
08b7c251 VZ |
172 | if ( parent ) |
173 | parent->AddChild(this); | |
2bda0e17 | 174 | |
08b7c251 | 175 | SubclassWin(m_hWnd); |
2bda0e17 | 176 | |
08b7c251 | 177 | return TRUE; |
2bda0e17 KB |
178 | } |
179 | ||
08b7c251 | 180 | wxTreeCtrl::~wxTreeCtrl() |
2bda0e17 | 181 | { |
08b7c251 | 182 | DeleteTextCtrl(); |
2bda0e17 | 183 | |
08b7c251 VZ |
184 | // delete user data to prevent memory leaks |
185 | DeleteAllItems(); | |
2bda0e17 KB |
186 | } |
187 | ||
08b7c251 VZ |
188 | // ---------------------------------------------------------------------------- |
189 | // accessors | |
190 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 191 | |
08b7c251 | 192 | // simple wrappers which add error checking in debug mode |
2bda0e17 | 193 | |
08b7c251 | 194 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const |
2bda0e17 | 195 | { |
06e38c8e | 196 | if ( !TreeView_GetItem(wxhWnd, tvItem) ) |
2bda0e17 | 197 | { |
08b7c251 VZ |
198 | wxLogLastError("TreeView_GetItem"); |
199 | ||
200 | return FALSE; | |
201 | } | |
202 | ||
203 | return TRUE; | |
2bda0e17 KB |
204 | } |
205 | ||
08b7c251 | 206 | void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem) |
2bda0e17 | 207 | { |
06e38c8e | 208 | if ( TreeView_SetItem(wxhWnd, tvItem) == -1 ) |
2bda0e17 | 209 | { |
08b7c251 VZ |
210 | wxLogLastError("TreeView_SetItem"); |
211 | } | |
2bda0e17 KB |
212 | } |
213 | ||
08b7c251 | 214 | size_t wxTreeCtrl::GetCount() const |
2bda0e17 | 215 | { |
06e38c8e | 216 | return (size_t)TreeView_GetCount(wxhWnd); |
2bda0e17 KB |
217 | } |
218 | ||
08b7c251 | 219 | unsigned int wxTreeCtrl::GetIndent() const |
2bda0e17 | 220 | { |
06e38c8e | 221 | return TreeView_GetIndent(wxhWnd); |
2bda0e17 KB |
222 | } |
223 | ||
08b7c251 | 224 | void wxTreeCtrl::SetIndent(unsigned int indent) |
2bda0e17 | 225 | { |
06e38c8e | 226 | TreeView_SetIndent(wxhWnd, indent); |
2bda0e17 KB |
227 | } |
228 | ||
08b7c251 | 229 | wxImageList *wxTreeCtrl::GetImageList() const |
2bda0e17 | 230 | { |
08b7c251 | 231 | return m_imageListNormal; |
2bda0e17 KB |
232 | } |
233 | ||
08b7c251 | 234 | wxImageList *wxTreeCtrl::GetStateImageList() const |
2bda0e17 | 235 | { |
08b7c251 | 236 | return m_imageListNormal; |
2bda0e17 KB |
237 | } |
238 | ||
08b7c251 | 239 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) |
2bda0e17 | 240 | { |
08b7c251 | 241 | // no error return |
06e38c8e | 242 | TreeView_SetImageList(wxhWnd, |
08b7c251 VZ |
243 | imageList ? imageList->GetHIMAGELIST() : 0, |
244 | which); | |
2bda0e17 KB |
245 | } |
246 | ||
08b7c251 | 247 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
2bda0e17 | 248 | { |
08b7c251 | 249 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); |
2bda0e17 KB |
250 | } |
251 | ||
08b7c251 | 252 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
2bda0e17 | 253 | { |
08b7c251 | 254 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); |
2bda0e17 KB |
255 | } |
256 | ||
08b7c251 VZ |
257 | // ---------------------------------------------------------------------------- |
258 | // Item access | |
259 | // ---------------------------------------------------------------------------- | |
260 | ||
261 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const | |
2bda0e17 | 262 | { |
08b7c251 | 263 | char buf[512]; // the size is arbitrary... |
02ce7b72 | 264 | |
08b7c251 VZ |
265 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
266 | tvItem.pszText = buf; | |
267 | tvItem.cchTextMax = WXSIZEOF(buf); | |
268 | if ( !DoGetItem(&tvItem) ) | |
269 | { | |
270 | // don't return some garbage which was on stack, but an empty string | |
271 | buf[0] = '\0'; | |
272 | } | |
2bda0e17 | 273 | |
08b7c251 VZ |
274 | return wxString(buf); |
275 | } | |
2bda0e17 | 276 | |
08b7c251 VZ |
277 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
278 | { | |
279 | wxTreeViewItem tvItem(item, TVIF_TEXT); | |
280 | tvItem.pszText = (char *)text.c_str(); // conversion is ok | |
281 | DoSetItem(&tvItem); | |
282 | } | |
2bda0e17 | 283 | |
08b7c251 VZ |
284 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const |
285 | { | |
286 | wxTreeViewItem tvItem(item, TVIF_IMAGE); | |
287 | DoGetItem(&tvItem); | |
2bda0e17 | 288 | |
08b7c251 VZ |
289 | return tvItem.iImage; |
290 | } | |
2bda0e17 | 291 | |
08b7c251 VZ |
292 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image) |
293 | { | |
294 | wxTreeViewItem tvItem(item, TVIF_IMAGE); | |
295 | tvItem.iImage = image; | |
296 | DoSetItem(&tvItem); | |
297 | } | |
2bda0e17 | 298 | |
08b7c251 VZ |
299 | int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const |
300 | { | |
301 | wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE); | |
302 | DoGetItem(&tvItem); | |
2bda0e17 | 303 | |
08b7c251 | 304 | return tvItem.iSelectedImage; |
2bda0e17 KB |
305 | } |
306 | ||
08b7c251 | 307 | void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image) |
2bda0e17 | 308 | { |
08b7c251 VZ |
309 | wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE); |
310 | tvItem.iSelectedImage = image; | |
311 | DoSetItem(&tvItem); | |
2bda0e17 KB |
312 | } |
313 | ||
08b7c251 | 314 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
2bda0e17 | 315 | { |
08b7c251 VZ |
316 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
317 | if ( !DoGetItem(&tvItem) ) | |
318 | { | |
319 | return NULL; | |
320 | } | |
2bda0e17 | 321 | |
08b7c251 VZ |
322 | wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam; |
323 | if ( data != NULL ) | |
324 | { | |
325 | // the data object should know about its id | |
326 | data->m_itemId = item; | |
327 | } | |
2bda0e17 | 328 | |
08b7c251 | 329 | return data; |
2bda0e17 KB |
330 | } |
331 | ||
08b7c251 | 332 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
2bda0e17 | 333 | { |
08b7c251 VZ |
334 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
335 | tvItem.lParam = (LPARAM)data; | |
336 | DoSetItem(&tvItem); | |
337 | } | |
2bda0e17 | 338 | |
08b7c251 VZ |
339 | // ---------------------------------------------------------------------------- |
340 | // Item status | |
341 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 342 | |
08b7c251 VZ |
343 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const |
344 | { | |
345 | RECT rect; | |
06e38c8e JS |
346 | // return (TreeView_GetItemRect(wxhWnd, (HTREEITEM) (WXHTREEITEM)item, &rect, FALSE) != 0); |
347 | // Bug in Gnu-Win32 headers, so don't use the macro. | |
348 | return (SendMessage((wxhWnd), TVM_GETITEMRECT, (WPARAM) FALSE, (LPARAM) & rect) != 0); | |
349 | ||
2bda0e17 KB |
350 | } |
351 | ||
08b7c251 | 352 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
2bda0e17 | 353 | { |
08b7c251 VZ |
354 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
355 | DoGetItem(&tvItem); | |
2bda0e17 | 356 | |
08b7c251 | 357 | return tvItem.cChildren != 0; |
2bda0e17 KB |
358 | } |
359 | ||
08b7c251 | 360 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
2bda0e17 | 361 | { |
08b7c251 VZ |
362 | // probably not a good idea to put it here |
363 | //wxASSERT( ItemHasChildren(item) ); | |
2bda0e17 | 364 | |
08b7c251 VZ |
365 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); |
366 | DoGetItem(&tvItem); | |
2bda0e17 | 367 | |
08b7c251 | 368 | return (tvItem.state & TVIS_EXPANDED) != 0; |
2bda0e17 KB |
369 | } |
370 | ||
08b7c251 | 371 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
2bda0e17 | 372 | { |
08b7c251 VZ |
373 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); |
374 | DoGetItem(&tvItem); | |
2bda0e17 | 375 | |
08b7c251 | 376 | return (tvItem.state & TVIS_SELECTED) != 0; |
2bda0e17 KB |
377 | } |
378 | ||
08b7c251 VZ |
379 | // ---------------------------------------------------------------------------- |
380 | // navigation | |
381 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 382 | |
08b7c251 VZ |
383 | wxTreeItemId wxTreeCtrl::GetRootItem() const |
384 | { | |
a3168196 | 385 | return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(wxhWnd)); |
08b7c251 | 386 | } |
2bda0e17 | 387 | |
08b7c251 VZ |
388 | wxTreeItemId wxTreeCtrl::GetSelection() const |
389 | { | |
a3168196 | 390 | return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(wxhWnd)); |
2bda0e17 KB |
391 | } |
392 | ||
08b7c251 | 393 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const |
2bda0e17 | 394 | { |
06e38c8e | 395 | return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); |
08b7c251 | 396 | } |
2bda0e17 | 397 | |
08b7c251 | 398 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
06e38c8e | 399 | long& _cookie) const |
08b7c251 VZ |
400 | { |
401 | // remember the last child returned in 'cookie' | |
06e38c8e | 402 | _cookie = (long)TreeView_GetChild(wxhWnd, (HTREEITEM) (WXHTREEITEM)item); |
2bda0e17 | 403 | |
06e38c8e | 404 | return wxTreeItemId((WXHTREEITEM)_cookie); |
2bda0e17 KB |
405 | } |
406 | ||
08b7c251 | 407 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), |
06e38c8e | 408 | long& _cookie) const |
2bda0e17 | 409 | { |
a3168196 | 410 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(wxhWnd, |
06e38c8e | 411 | (HTREEITEM) (WXHTREEITEM)_cookie)); |
08b7c251 | 412 | } |
2bda0e17 | 413 | |
08b7c251 VZ |
414 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
415 | { | |
a3168196 | 416 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); |
2bda0e17 KB |
417 | } |
418 | ||
08b7c251 | 419 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
2bda0e17 | 420 | { |
a3168196 | 421 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); |
2bda0e17 KB |
422 | } |
423 | ||
08b7c251 | 424 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
2bda0e17 | 425 | { |
a3168196 | 426 | return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(wxhWnd)); |
2bda0e17 KB |
427 | } |
428 | ||
08b7c251 | 429 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
2bda0e17 | 430 | { |
08b7c251 VZ |
431 | wxASSERT_MSG( IsVisible(item), "The item you call GetNextVisible() " |
432 | "for must be visible itself!"); | |
02ce7b72 | 433 | |
a3168196 | 434 | return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); |
08b7c251 | 435 | } |
02ce7b72 | 436 | |
08b7c251 VZ |
437 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
438 | { | |
439 | wxASSERT_MSG( IsVisible(item), "The item you call GetPrevVisible() " | |
440 | "for must be visible itself!"); | |
02ce7b72 | 441 | |
a3168196 | 442 | return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item)); |
08b7c251 | 443 | } |
02ce7b72 | 444 | |
08b7c251 VZ |
445 | // ---------------------------------------------------------------------------- |
446 | // Usual operations | |
447 | // ---------------------------------------------------------------------------- | |
02ce7b72 | 448 | |
08b7c251 VZ |
449 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, |
450 | wxTreeItemId hInsertAfter, | |
451 | const wxString& text, | |
452 | int image, int selectedImage, | |
453 | wxTreeItemData *data) | |
454 | { | |
455 | TV_INSERTSTRUCT tvIns; | |
06e38c8e JS |
456 | tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent; |
457 | tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter; | |
08b7c251 VZ |
458 | UINT mask = 0; |
459 | if ( !text.IsEmpty() ) | |
460 | { | |
461 | mask |= TVIF_TEXT; | |
462 | tvIns.item.pszText = (char *)text.c_str(); // cast is ok | |
463 | } | |
02ce7b72 | 464 | |
08b7c251 VZ |
465 | if ( image != -1 ) |
466 | { | |
467 | mask |= TVIF_IMAGE; | |
468 | tvIns.item.iImage = image; | |
469 | } | |
02ce7b72 | 470 | |
08b7c251 VZ |
471 | if ( selectedImage != -1 ) |
472 | { | |
473 | mask |= TVIF_SELECTEDIMAGE; | |
474 | tvIns.item.iSelectedImage = selectedImage; | |
475 | } | |
02ce7b72 | 476 | |
08b7c251 VZ |
477 | if ( data != NULL ) |
478 | { | |
479 | mask |= TVIF_PARAM; | |
480 | tvIns.item.lParam = (LPARAM)data; | |
481 | } | |
02ce7b72 | 482 | |
08b7c251 | 483 | tvIns.item.mask = mask; |
02ce7b72 | 484 | |
06e38c8e | 485 | HTREEITEM id = (HTREEITEM) TreeView_InsertItem(wxhWnd, &tvIns); |
08b7c251 VZ |
486 | if ( id == 0 ) |
487 | { | |
488 | wxLogLastError("TreeView_InsertItem"); | |
489 | } | |
02ce7b72 | 490 | |
06e38c8e | 491 | return wxTreeItemId((WXHTREEITEM)id); |
2bda0e17 KB |
492 | } |
493 | ||
08b7c251 VZ |
494 | // for compatibility only |
495 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, | |
496 | const wxString& text, | |
497 | int image, int selImage, | |
498 | long insertAfter) | |
2bda0e17 | 499 | { |
06e38c8e | 500 | return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text, |
08b7c251 | 501 | image, selImage, NULL); |
2bda0e17 KB |
502 | } |
503 | ||
08b7c251 VZ |
504 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
505 | int image, int selectedImage, | |
506 | wxTreeItemData *data) | |
2bda0e17 | 507 | { |
06e38c8e | 508 | return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0, |
08b7c251 | 509 | text, image, selectedImage, data); |
2bda0e17 KB |
510 | } |
511 | ||
08b7c251 VZ |
512 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, |
513 | const wxString& text, | |
514 | int image, int selectedImage, | |
515 | wxTreeItemData *data) | |
2bda0e17 | 516 | { |
06e38c8e | 517 | return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST, |
08b7c251 | 518 | text, image, selectedImage, data); |
2bda0e17 KB |
519 | } |
520 | ||
08b7c251 VZ |
521 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
522 | const wxTreeItemId& idPrevious, | |
523 | const wxString& text, | |
524 | int image, int selectedImage, | |
525 | wxTreeItemData *data) | |
2bda0e17 | 526 | { |
08b7c251 | 527 | return DoInsertItem(parent, idPrevious, text, image, selectedImage, data); |
2bda0e17 KB |
528 | } |
529 | ||
08b7c251 VZ |
530 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent, |
531 | const wxString& text, | |
532 | int image, int selectedImage, | |
533 | wxTreeItemData *data) | |
2bda0e17 | 534 | { |
06e38c8e | 535 | return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST, |
08b7c251 | 536 | text, image, selectedImage, data); |
2bda0e17 KB |
537 | } |
538 | ||
08b7c251 | 539 | void wxTreeCtrl::Delete(const wxTreeItemId& item) |
2bda0e17 | 540 | { |
08b7c251 VZ |
541 | wxTreeItemData *data = GetItemData(item); |
542 | delete data; // may be NULL, ok | |
bbcdf8bc | 543 | |
06e38c8e | 544 | if ( !TreeView_DeleteItem(wxhWnd, (HTREEITEM)(WXHTREEITEM)item) ) |
bbcdf8bc | 545 | { |
08b7c251 | 546 | wxLogLastError("TreeView_DeleteItem"); |
bbcdf8bc | 547 | } |
bbcdf8bc JS |
548 | } |
549 | ||
08b7c251 | 550 | void wxTreeCtrl::DeleteAllItems() |
bbcdf8bc | 551 | { |
06e38c8e | 552 | if ( !TreeView_DeleteAllItems(wxhWnd) ) |
bbcdf8bc | 553 | { |
08b7c251 | 554 | wxLogLastError("TreeView_DeleteAllItems"); |
bbcdf8bc | 555 | } |
2bda0e17 KB |
556 | } |
557 | ||
08b7c251 | 558 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) |
2bda0e17 | 559 | { |
08b7c251 VZ |
560 | wxASSERT_MSG( flag == TVE_COLLAPSE || flag == TVE_COLLAPSERESET || |
561 | flag == TVE_EXPAND || flag == TVE_TOGGLE, | |
562 | "Unknown flag in wxTreeCtrl::DoExpand" ); | |
563 | ||
564 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must | |
565 | // emulate them | |
06e38c8e | 566 | if ( TreeView_Expand(wxhWnd, (HTREEITEM) (WXHTREEITEM) item, flag) != 0 ) |
08b7c251 VZ |
567 | { |
568 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
569 | event.m_item = item; | |
570 | ||
571 | bool isExpanded = IsExpanded(item); | |
2bda0e17 | 572 | |
08b7c251 | 573 | event.SetEventObject(this); |
2bda0e17 | 574 | |
08b7c251 VZ |
575 | // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded |
576 | event.SetEventType(g_events[isExpanded][TRUE]); | |
577 | GetEventHandler()->ProcessEvent(event); | |
2bda0e17 | 578 | |
08b7c251 VZ |
579 | event.SetEventType(g_events[isExpanded][FALSE]); |
580 | GetEventHandler()->ProcessEvent(event); | |
581 | } | |
582 | else | |
583 | { | |
584 | // I wonder if it really ever happens... | |
585 | wxLogDebug("TreeView_Expand: change didn't took place."); | |
586 | } | |
2bda0e17 KB |
587 | } |
588 | ||
08b7c251 | 589 | void wxTreeCtrl::Expand(const wxTreeItemId& item) |
2bda0e17 | 590 | { |
08b7c251 | 591 | DoExpand(item, TVE_EXPAND); |
2bda0e17 | 592 | } |
2bda0e17 | 593 | |
08b7c251 | 594 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) |
2bda0e17 | 595 | { |
08b7c251 | 596 | DoExpand(item, TVE_COLLAPSE); |
2bda0e17 KB |
597 | } |
598 | ||
08b7c251 | 599 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
2bda0e17 | 600 | { |
08b7c251 | 601 | DoExpand(item, TVE_COLLAPSERESET); |
2bda0e17 KB |
602 | } |
603 | ||
08b7c251 | 604 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) |
2bda0e17 | 605 | { |
08b7c251 | 606 | DoExpand(item, TVE_TOGGLE); |
2bda0e17 KB |
607 | } |
608 | ||
08b7c251 | 609 | void wxTreeCtrl::Unselect() |
2bda0e17 | 610 | { |
06e38c8e | 611 | SelectItem(wxTreeItemId((WXHTREEITEM) 0)); |
08b7c251 | 612 | } |
02ce7b72 | 613 | |
08b7c251 VZ |
614 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item) |
615 | { | |
06e38c8e | 616 | if ( !TreeView_SelectItem(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) ) |
2bda0e17 | 617 | { |
08b7c251 | 618 | wxLogLastError("TreeView_SelectItem"); |
2bda0e17 | 619 | } |
08b7c251 | 620 | } |
2bda0e17 | 621 | |
08b7c251 VZ |
622 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
623 | { | |
624 | // no error return | |
06e38c8e | 625 | TreeView_EnsureVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item); |
08b7c251 VZ |
626 | } |
627 | ||
628 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) | |
629 | { | |
06e38c8e | 630 | if ( !TreeView_SelectSetFirstVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) ) |
2bda0e17 | 631 | { |
08b7c251 | 632 | wxLogLastError("TreeView_SelectSetFirstVisible"); |
2bda0e17 | 633 | } |
08b7c251 VZ |
634 | } |
635 | ||
636 | wxTextCtrl* wxTreeCtrl::GetEditControl() const | |
637 | { | |
638 | return m_textCtrl; | |
639 | } | |
640 | ||
641 | void wxTreeCtrl::DeleteTextCtrl() | |
642 | { | |
643 | if ( m_textCtrl ) | |
2bda0e17 | 644 | { |
08b7c251 VZ |
645 | m_textCtrl->UnsubclassWin(); |
646 | m_textCtrl->SetHWND(0); | |
647 | delete m_textCtrl; | |
648 | m_textCtrl = NULL; | |
2bda0e17 | 649 | } |
08b7c251 | 650 | } |
2bda0e17 | 651 | |
08b7c251 VZ |
652 | wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, |
653 | wxClassInfo* textControlClass) | |
654 | { | |
655 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
656 | ||
06e38c8e | 657 | HWND hWnd = (HWND) TreeView_EditLabel(wxhWnd, (HTREEITEM) (WXHTREEITEM) item); |
2bda0e17 | 658 | |
08b7c251 | 659 | wxCHECK_MSG( hWnd, NULL, "Can't edit tree ctrl label" ); |
2bda0e17 | 660 | |
08b7c251 | 661 | DeleteTextCtrl(); |
2bda0e17 | 662 | |
08b7c251 VZ |
663 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); |
664 | m_textCtrl->SetHWND((WXHWND)hWnd); | |
665 | m_textCtrl->SubclassWin((WXHWND)hWnd); | |
2bda0e17 | 666 | |
08b7c251 | 667 | return m_textCtrl; |
2bda0e17 KB |
668 | } |
669 | ||
08b7c251 VZ |
670 | // End label editing, optionally cancelling the edit |
671 | void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) | |
2bda0e17 | 672 | { |
06e38c8e | 673 | TreeView_EndEditLabelNow(wxhWnd, discardChanges); |
08b7c251 VZ |
674 | |
675 | DeleteTextCtrl(); | |
2bda0e17 KB |
676 | } |
677 | ||
08b7c251 | 678 | wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) |
2bda0e17 | 679 | { |
08b7c251 VZ |
680 | TV_HITTESTINFO hitTestInfo; |
681 | hitTestInfo.pt.x = (int)point.x; | |
682 | hitTestInfo.pt.y = (int)point.y; | |
2bda0e17 | 683 | |
06e38c8e | 684 | TreeView_HitTest(wxhWnd, &hitTestInfo); |
2bda0e17 | 685 | |
08b7c251 VZ |
686 | flags = 0; |
687 | ||
688 | // avoid repetition | |
689 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ | |
690 | flags |= wxTREE_HITTEST_##flag | |
691 | ||
692 | TRANSLATE_FLAG(ABOVE); | |
693 | TRANSLATE_FLAG(BELOW); | |
694 | TRANSLATE_FLAG(NOWHERE); | |
695 | TRANSLATE_FLAG(ONITEMBUTTON); | |
696 | TRANSLATE_FLAG(ONITEMICON); | |
697 | TRANSLATE_FLAG(ONITEMINDENT); | |
698 | TRANSLATE_FLAG(ONITEMLABEL); | |
699 | TRANSLATE_FLAG(ONITEMRIGHT); | |
700 | TRANSLATE_FLAG(ONITEMSTATEICON); | |
701 | TRANSLATE_FLAG(TOLEFT); | |
702 | TRANSLATE_FLAG(TORIGHT); | |
2bda0e17 | 703 | |
08b7c251 VZ |
704 | #undef TRANSLATE_FLAG |
705 | ||
06e38c8e | 706 | return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem); |
08b7c251 VZ |
707 | } |
708 | ||
709 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item, | |
710 | wxTreeItemCmpFunc *cmpFunction) | |
711 | { | |
712 | if ( cmpFunction == NULL ) | |
2bda0e17 | 713 | { |
06e38c8e | 714 | TreeView_SortChildren(wxhWnd, (HTREEITEM) (WXHTREEITEM) item, 0); |
2bda0e17 | 715 | } |
08b7c251 | 716 | else |
2bda0e17 | 717 | { |
08b7c251 VZ |
718 | // TODO: use TreeView_SortChildrenCB |
719 | wxFAIL_MSG("wxTreeCtrl::SortChildren not implemented"); | |
2bda0e17 | 720 | } |
08b7c251 | 721 | } |
2bda0e17 | 722 | |
08b7c251 VZ |
723 | // ---------------------------------------------------------------------------- |
724 | // implementation | |
725 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 726 | |
08b7c251 VZ |
727 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id) |
728 | { | |
729 | if ( cmd == EN_UPDATE ) | |
2bda0e17 | 730 | { |
08b7c251 VZ |
731 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); |
732 | event.SetEventObject( this ); | |
733 | ProcessCommand(event); | |
2bda0e17 | 734 | } |
08b7c251 | 735 | else if ( cmd == EN_KILLFOCUS ) |
2bda0e17 | 736 | { |
08b7c251 VZ |
737 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); |
738 | event.SetEventObject( this ); | |
739 | ProcessCommand(event); | |
2bda0e17 | 740 | } |
08b7c251 | 741 | else |
2bda0e17 | 742 | { |
08b7c251 VZ |
743 | // nothing done |
744 | return FALSE; | |
2bda0e17 | 745 | } |
08b7c251 VZ |
746 | |
747 | // command processed | |
748 | return TRUE; | |
749 | } | |
750 | ||
751 | // process WM_NOTIFY Windows message | |
752 | bool wxTreeCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam) | |
753 | { | |
754 | wxTreeEvent event(wxEVT_NULL, m_windowId); | |
755 | wxEventType eventType = wxEVT_NULL; | |
756 | NMHDR *hdr = (NMHDR *)lParam; | |
757 | ||
758 | switch ( hdr->code ) | |
2bda0e17 | 759 | { |
08b7c251 VZ |
760 | case TVN_BEGINDRAG: |
761 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
762 | // fall through | |
763 | ||
764 | case TVN_BEGINRDRAG: | |
765 | { | |
766 | if ( eventType == wxEVT_NULL ) | |
767 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; | |
768 | //else: left drag, already set above | |
769 | ||
770 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
771 | ||
06e38c8e | 772 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
08b7c251 VZ |
773 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); |
774 | break; | |
775 | } | |
776 | ||
777 | case TVN_BEGINLABELEDIT: | |
778 | { | |
779 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; | |
780 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
781 | ||
06e38c8e | 782 | event.m_item = (WXHTREEITEM) info->item.hItem; |
08b7c251 VZ |
783 | break; |
784 | } | |
785 | ||
786 | case TVN_DELETEITEM: | |
787 | { | |
788 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; | |
789 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
790 | ||
06e38c8e | 791 | event.m_item = (WXHTREEITEM) tv->itemOld.hItem; |
08b7c251 VZ |
792 | break; |
793 | } | |
794 | ||
795 | case TVN_ENDLABELEDIT: | |
796 | { | |
797 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; | |
798 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
799 | ||
06e38c8e | 800 | event.m_item = (WXHTREEITEM) info->item.hItem; |
08b7c251 VZ |
801 | break; |
802 | } | |
803 | ||
804 | case TVN_GETDISPINFO: | |
805 | eventType = wxEVT_COMMAND_TREE_GET_INFO; | |
806 | // fall through | |
807 | ||
808 | case TVN_SETDISPINFO: | |
809 | { | |
810 | if ( eventType == wxEVT_NULL ) | |
811 | eventType = wxEVT_COMMAND_TREE_SET_INFO; | |
812 | //else: get, already set above | |
813 | ||
814 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
815 | ||
06e38c8e | 816 | event.m_item = (WXHTREEITEM) info->item.hItem; |
08b7c251 VZ |
817 | break; |
818 | } | |
819 | ||
820 | case TVN_ITEMEXPANDING: | |
821 | event.m_code = FALSE; | |
822 | // fall through | |
823 | ||
824 | case TVN_ITEMEXPANDED: | |
825 | { | |
826 | NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam; | |
827 | ||
828 | bool expand = FALSE; | |
829 | switch ( tv->action ) | |
830 | { | |
831 | case TVE_EXPAND: | |
832 | expand = TRUE; | |
833 | break; | |
834 | ||
835 | case TVE_COLLAPSE: | |
836 | expand = FALSE; | |
837 | break; | |
838 | ||
839 | default: | |
840 | wxLogDebug("unexpected code %d in TVN_ITEMEXPAND " | |
841 | "message", tv->action); | |
842 | } | |
843 | ||
06e38c8e | 844 | bool ing = (hdr->code == TVN_ITEMEXPANDING); |
08b7c251 VZ |
845 | eventType = g_events[expand][ing]; |
846 | ||
06e38c8e | 847 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
08b7c251 VZ |
848 | break; |
849 | } | |
850 | ||
851 | case TVN_KEYDOWN: | |
852 | { | |
853 | eventType = wxEVT_COMMAND_TREE_KEY_DOWN; | |
854 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; | |
855 | ||
856 | event.m_code = wxCharCodeMSWToWX(info->wVKey); | |
857 | break; | |
858 | } | |
859 | ||
860 | case TVN_SELCHANGED: | |
861 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; | |
862 | // fall through | |
863 | ||
864 | case TVN_SELCHANGING: | |
865 | { | |
866 | if ( eventType == wxEVT_NULL ) | |
867 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; | |
868 | //else: already set above | |
869 | ||
870 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
871 | ||
06e38c8e JS |
872 | event.m_item = (WXHTREEITEM) tv->itemNew.hItem; |
873 | event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem; | |
08b7c251 VZ |
874 | break; |
875 | } | |
876 | ||
877 | default: | |
878 | return wxControl::MSWNotify(wParam, lParam); | |
2bda0e17 | 879 | } |
08b7c251 VZ |
880 | |
881 | event.SetEventObject(this); | |
882 | event.SetEventType(eventType); | |
883 | ||
884 | bool rc = GetEventHandler()->ProcessEvent(event); | |
885 | ||
886 | // post processing | |
887 | switch ( hdr->code ) | |
2bda0e17 | 888 | { |
08b7c251 VZ |
889 | // NB: we might process this message using wxWindows event tables, but |
890 | // due to overhead of wxWin event system we prefer to do it here | |
891 | // (otherwise deleting a tree with many items is just too slow) | |
892 | case TVN_DELETEITEM: | |
893 | { | |
894 | NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; | |
895 | wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam; | |
896 | delete data; // may be NULL, ok | |
897 | } | |
898 | break; | |
899 | ||
900 | case TVN_ITEMEXPANDING: | |
901 | // if user called Veto(), don't allow expansion/collapse by | |
902 | // returning TRUE from here | |
903 | rc = event.m_code != 0; | |
904 | break; | |
2bda0e17 | 905 | } |
08b7c251 VZ |
906 | |
907 | return rc; | |
2bda0e17 KB |
908 | } |
909 | ||
08b7c251 | 910 | // ---------------------------------------------------------------------------- |
2bda0e17 | 911 | // Tree event |
08b7c251 VZ |
912 | // ---------------------------------------------------------------------------- |
913 | ||
2bda0e17 KB |
914 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxCommandEvent) |
915 | ||
08b7c251 VZ |
916 | wxTreeEvent::wxTreeEvent(wxEventType commandType, int id) |
917 | : wxCommandEvent(commandType, id) | |
2bda0e17 | 918 | { |
08b7c251 VZ |
919 | m_code = 0; |
920 | m_itemOld = 0; | |
2bda0e17 KB |
921 | } |
922 | ||
08b7c251 | 923 | #endif // __WIN95__ |
2bda0e17 | 924 |