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