]> git.saurik.com Git - wxWidgets.git/blame - src/msw/treectrl.cpp
Removed floor() references
[wxWidgets.git] / src / msw / treectrl.cpp
CommitLineData
b823f5a1
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.cpp
3// Purpose: wxTreeCtrl
4// Author: Julian Smart
08b7c251 5// Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
b823f5a1
JS
6// Created: 1997
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
08b7c251 9// Licence: wxWindows licence
b823f5a1 10/////////////////////////////////////////////////////////////////////////////
2bda0e17 11
08b7c251
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
2bda0e17 19#ifdef __GNUG__
08b7c251 20 #pragma implementation "treectrl.h"
2bda0e17
KB
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
08b7c251 27 #pragma hdrstop
2bda0e17
KB
28#endif
29
0c589ad0
BM
30#include "wx/window.h"
31#include "wx/msw/private.h"
32
2bda0e17 33#ifndef WX_PRECOMP
0c589ad0
BM
34 #include "wx/settings.h"
35#endif
36
37// Mingw32 is a bit mental even though this is done in winundef
38#ifdef GetFirstChild
39#undef GetFirstChild
40#endif
41#ifdef GetNextSibling
42#undef GetNextSibling
2bda0e17
KB
43#endif
44
2bda0e17
KB
45#if defined(__WIN95__)
46
08b7c251 47#include "wx/log.h"
ce3ed50d 48#include "wx/dynarray.h"
08b7c251 49#include "wx/imaglist.h"
ce3ed50d 50#include "wx/msw/treectrl.h"
08b7c251 51
9a05fd8d
JS
52#ifdef __GNUWIN32__
53#include "wx/msw/gnuwin32/extra.h"
54#endif
55
57c208c5 56#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
08b7c251 57 #include <commctrl.h>
2bda0e17
KB
58#endif
59
60// Bug in headers, sometimes
61#ifndef TVIS_FOCUSED
08b7c251 62 #define TVIS_FOCUSED 0x0001
2bda0e17
KB
63#endif
64
08b7c251
VZ
65// ----------------------------------------------------------------------------
66// private classes
67// ----------------------------------------------------------------------------
2bda0e17 68
08b7c251
VZ
69// a convenient wrapper around TV_ITEM struct which adds a ctor
70struct wxTreeViewItem : public TV_ITEM
71{
72 wxTreeViewItem(const wxTreeItemId& item,
73 UINT mask_, UINT stateMask_ = 0)
74 {
75 mask = mask_;
76 stateMask = stateMask_;
06e38c8e 77 hItem = (HTREEITEM) (WXHTREEITEM) item;
08b7c251
VZ
78 }
79};
2bda0e17 80
08b7c251
VZ
81// ----------------------------------------------------------------------------
82// macros
83// ----------------------------------------------------------------------------
84
85#if !USE_SHARED_LIBRARY
86 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
2bda0e17
KB
87#endif
88
08b7c251 89// hide the ugly cast (of course, the macro is _quite_ ugly too...)
06e38c8e 90#define wxhWnd ((HWND)m_hWnd)
08b7c251
VZ
91
92// ----------------------------------------------------------------------------
93// variables
94// ----------------------------------------------------------------------------
95
96// handy table for sending events
97static const wxEventType g_events[2][2] =
2bda0e17 98{
08b7c251
VZ
99 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
100 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
101};
102
103// ============================================================================
104// implementation
105// ============================================================================
106
107// ----------------------------------------------------------------------------
108// construction and destruction
109// ----------------------------------------------------------------------------
110
111void wxTreeCtrl::Init()
112{
113 m_imageListNormal = NULL;
114 m_imageListState = NULL;
115 m_textCtrl = NULL;
2bda0e17
KB
116}
117
08b7c251
VZ
118bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
119 const wxPoint& pos, const wxSize& size,
120 long style, const wxValidator& validator,
121 const wxString& name)
2bda0e17 122{
08b7c251 123 Init();
2bda0e17 124
08b7c251 125 wxSystemSettings settings;
2bda0e17 126
08b7c251
VZ
127 SetName(name);
128 SetValidator(validator);
2bda0e17 129
08b7c251 130 m_windowStyle = style;
2bda0e17 131
08b7c251 132 SetParent(parent);
2bda0e17 133
08b7c251 134 m_windowId = (id == -1) ? NewControlId() : id;
2bda0e17 135
f5ee2e5f
JS
136 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_SHOWSELALWAYS ;
137
2bda0e17 138
08b7c251
VZ
139 bool want3D;
140 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
2bda0e17 141
08b7c251
VZ
142 // Even with extended styles, need to combine with WS_BORDER
143 // for them to look right.
144 if ( want3D || wxStyleHasBorder(m_windowStyle) )
145 {
146 wstyle |= WS_BORDER;
147 }
2bda0e17 148
08b7c251
VZ
149 if ( m_windowStyle & wxTR_HAS_BUTTONS )
150 wstyle |= TVS_HASBUTTONS;
2bda0e17 151
08b7c251
VZ
152 if ( m_windowStyle & wxTR_EDIT_LABELS )
153 wstyle |= TVS_EDITLABELS;
2bda0e17 154
08b7c251
VZ
155 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
156 wstyle |= TVS_LINESATROOT;
2bda0e17 157
08b7c251
VZ
158 // Create the tree control.
159 m_hWnd = (WXHWND)::CreateWindowEx
160 (
161 exStyle,
162 WC_TREEVIEW,
163 "",
164 wstyle,
165 pos.x, pos.y, size.x, size.y,
166 (HWND)parent->GetHWND(),
167 (HMENU)m_windowId,
168 wxGetInstance(),
169 NULL
170 );
7798a18e 171
08b7c251 172 wxCHECK_MSG( m_hWnd, FALSE, "Failed to create tree ctrl" );
2bda0e17 173
08b7c251
VZ
174 if ( parent )
175 parent->AddChild(this);
2bda0e17 176
08b7c251 177 SubclassWin(m_hWnd);
2bda0e17 178
08b7c251 179 return TRUE;
2bda0e17
KB
180}
181
08b7c251 182wxTreeCtrl::~wxTreeCtrl()
2bda0e17 183{
08b7c251 184 DeleteTextCtrl();
2bda0e17 185
08b7c251
VZ
186 // delete user data to prevent memory leaks
187 DeleteAllItems();
2bda0e17
KB
188}
189
08b7c251
VZ
190// ----------------------------------------------------------------------------
191// accessors
192// ----------------------------------------------------------------------------
2bda0e17 193
08b7c251 194// simple wrappers which add error checking in debug mode
2bda0e17 195
08b7c251 196bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
2bda0e17 197{
06e38c8e 198 if ( !TreeView_GetItem(wxhWnd, tvItem) )
2bda0e17 199 {
08b7c251
VZ
200 wxLogLastError("TreeView_GetItem");
201
202 return FALSE;
203 }
204
205 return TRUE;
2bda0e17
KB
206}
207
08b7c251 208void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
2bda0e17 209{
06e38c8e 210 if ( TreeView_SetItem(wxhWnd, tvItem) == -1 )
2bda0e17 211 {
08b7c251
VZ
212 wxLogLastError("TreeView_SetItem");
213 }
2bda0e17
KB
214}
215
08b7c251 216size_t wxTreeCtrl::GetCount() const
2bda0e17 217{
06e38c8e 218 return (size_t)TreeView_GetCount(wxhWnd);
2bda0e17
KB
219}
220
08b7c251 221unsigned int wxTreeCtrl::GetIndent() const
2bda0e17 222{
06e38c8e 223 return TreeView_GetIndent(wxhWnd);
2bda0e17
KB
224}
225
08b7c251 226void wxTreeCtrl::SetIndent(unsigned int indent)
2bda0e17 227{
06e38c8e 228 TreeView_SetIndent(wxhWnd, indent);
2bda0e17
KB
229}
230
08b7c251 231wxImageList *wxTreeCtrl::GetImageList() const
2bda0e17 232{
08b7c251 233 return m_imageListNormal;
2bda0e17
KB
234}
235
08b7c251 236wxImageList *wxTreeCtrl::GetStateImageList() const
2bda0e17 237{
08b7c251 238 return m_imageListNormal;
2bda0e17
KB
239}
240
08b7c251 241void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
2bda0e17 242{
08b7c251 243 // no error return
06e38c8e 244 TreeView_SetImageList(wxhWnd,
08b7c251
VZ
245 imageList ? imageList->GetHIMAGELIST() : 0,
246 which);
2bda0e17
KB
247}
248
08b7c251 249void wxTreeCtrl::SetImageList(wxImageList *imageList)
2bda0e17 250{
08b7c251 251 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
2bda0e17
KB
252}
253
08b7c251 254void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
2bda0e17 255{
08b7c251 256 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
2bda0e17
KB
257}
258
23fd5130
VZ
259size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
260{
261 long cookie;
262
263 size_t result = 0;
264
265 wxArrayLong children;
266 wxTreeItemId child = GetFirstChild(item, cookie);
267 while ( child.IsOk() )
268 {
269 if ( recursively )
270 {
271 // recursive call
272 result += GetChildrenCount(child, TRUE);
273 }
274
275 // add the child to the result in any case
276 result++;
277
278 child = GetNextChild(item, cookie);
279 }
280
281 return result;
282}
283
08b7c251
VZ
284// ----------------------------------------------------------------------------
285// Item access
286// ----------------------------------------------------------------------------
287
288wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
2bda0e17 289{
08b7c251 290 char buf[512]; // the size is arbitrary...
02ce7b72 291
08b7c251
VZ
292 wxTreeViewItem tvItem(item, TVIF_TEXT);
293 tvItem.pszText = buf;
294 tvItem.cchTextMax = WXSIZEOF(buf);
295 if ( !DoGetItem(&tvItem) )
296 {
297 // don't return some garbage which was on stack, but an empty string
298 buf[0] = '\0';
299 }
2bda0e17 300
08b7c251
VZ
301 return wxString(buf);
302}
2bda0e17 303
08b7c251
VZ
304void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
305{
306 wxTreeViewItem tvItem(item, TVIF_TEXT);
307 tvItem.pszText = (char *)text.c_str(); // conversion is ok
308 DoSetItem(&tvItem);
309}
2bda0e17 310
08b7c251
VZ
311int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
312{
313 wxTreeViewItem tvItem(item, TVIF_IMAGE);
314 DoGetItem(&tvItem);
2bda0e17 315
08b7c251
VZ
316 return tvItem.iImage;
317}
2bda0e17 318
08b7c251
VZ
319void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
320{
321 wxTreeViewItem tvItem(item, TVIF_IMAGE);
322 tvItem.iImage = image;
323 DoSetItem(&tvItem);
324}
2bda0e17 325
08b7c251
VZ
326int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
327{
328 wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE);
329 DoGetItem(&tvItem);
2bda0e17 330
08b7c251 331 return tvItem.iSelectedImage;
2bda0e17
KB
332}
333
08b7c251 334void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
2bda0e17 335{
08b7c251
VZ
336 wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE);
337 tvItem.iSelectedImage = image;
338 DoSetItem(&tvItem);
2bda0e17
KB
339}
340
08b7c251 341wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
2bda0e17 342{
08b7c251
VZ
343 wxTreeViewItem tvItem(item, TVIF_PARAM);
344 if ( !DoGetItem(&tvItem) )
345 {
346 return NULL;
347 }
2bda0e17 348
3a5a2f56 349 return (wxTreeItemData *)tvItem.lParam;
2bda0e17
KB
350}
351
08b7c251 352void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
2bda0e17 353{
08b7c251
VZ
354 wxTreeViewItem tvItem(item, TVIF_PARAM);
355 tvItem.lParam = (LPARAM)data;
356 DoSetItem(&tvItem);
357}
2bda0e17 358
3a5a2f56
VZ
359void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
360{
361 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
362 tvItem.cChildren = (int)has;
363 DoSetItem(&tvItem);
364}
365
add28c55
VZ
366void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
367{
368 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
369 tvItem.state = bold ? TVIS_BOLD : 0;
370 DoSetItem(&tvItem);
371}
372
58a8ab88
JS
373void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
374{
375 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
376 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
377 DoSetItem(&tvItem);
378}
379
08b7c251
VZ
380// ----------------------------------------------------------------------------
381// Item status
382// ----------------------------------------------------------------------------
2bda0e17 383
08b7c251
VZ
384bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
385{
add28c55 386 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
08b7c251 387 RECT rect;
add28c55 388 return SendMessage(wxhWnd, TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
06e38c8e 389
2bda0e17
KB
390}
391
08b7c251 392bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
2bda0e17 393{
08b7c251
VZ
394 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
395 DoGetItem(&tvItem);
2bda0e17 396
08b7c251 397 return tvItem.cChildren != 0;
2bda0e17
KB
398}
399
08b7c251 400bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
2bda0e17 401{
08b7c251
VZ
402 // probably not a good idea to put it here
403 //wxASSERT( ItemHasChildren(item) );
2bda0e17 404
08b7c251
VZ
405 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
406 DoGetItem(&tvItem);
2bda0e17 407
08b7c251 408 return (tvItem.state & TVIS_EXPANDED) != 0;
2bda0e17
KB
409}
410
08b7c251 411bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
2bda0e17 412{
08b7c251
VZ
413 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
414 DoGetItem(&tvItem);
2bda0e17 415
08b7c251 416 return (tvItem.state & TVIS_SELECTED) != 0;
2bda0e17
KB
417}
418
add28c55
VZ
419bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
420{
421 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
422 DoGetItem(&tvItem);
423
424 return (tvItem.state & TVIS_BOLD) != 0;
425}
426
08b7c251
VZ
427// ----------------------------------------------------------------------------
428// navigation
429// ----------------------------------------------------------------------------
2bda0e17 430
08b7c251
VZ
431wxTreeItemId wxTreeCtrl::GetRootItem() const
432{
a3168196 433 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(wxhWnd));
08b7c251 434}
2bda0e17 435
08b7c251
VZ
436wxTreeItemId wxTreeCtrl::GetSelection() const
437{
a3168196 438 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(wxhWnd));
2bda0e17
KB
439}
440
08b7c251 441wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
2bda0e17 442{
06e38c8e 443 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
08b7c251 444}
2bda0e17 445
08b7c251 446wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
06e38c8e 447 long& _cookie) const
08b7c251
VZ
448{
449 // remember the last child returned in 'cookie'
06e38c8e 450 _cookie = (long)TreeView_GetChild(wxhWnd, (HTREEITEM) (WXHTREEITEM)item);
2bda0e17 451
06e38c8e 452 return wxTreeItemId((WXHTREEITEM)_cookie);
2bda0e17
KB
453}
454
08b7c251 455wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
06e38c8e 456 long& _cookie) const
2bda0e17 457{
23fd5130
VZ
458 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(wxhWnd,
459 (HTREEITEM)(WXHTREEITEM)_cookie));
460 _cookie = (long)l;
461
2e5dddb0 462 return l;
08b7c251 463}
2bda0e17 464
978f38c2
VZ
465wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
466{
467 // can this be done more efficiently?
468 long cookie;
469
470 wxTreeItemId childLast,
2165ad93 471 child = GetFirstChild(item, cookie);
978f38c2
VZ
472 while ( child.IsOk() )
473 {
474 childLast = child;
2165ad93 475 child = GetNextChild(item, cookie);
978f38c2
VZ
476 }
477
478 return childLast;
479}
480
08b7c251
VZ
481wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
482{
a3168196 483 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
2bda0e17
KB
484}
485
08b7c251 486wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
2bda0e17 487{
a3168196 488 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
2bda0e17
KB
489}
490
08b7c251 491wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
2bda0e17 492{
a3168196 493 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(wxhWnd));
2bda0e17
KB
494}
495
08b7c251 496wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
2bda0e17 497{
08b7c251
VZ
498 wxASSERT_MSG( IsVisible(item), "The item you call GetNextVisible() "
499 "for must be visible itself!");
02ce7b72 500
a3168196 501 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
08b7c251 502}
02ce7b72 503
08b7c251
VZ
504wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
505{
506 wxASSERT_MSG( IsVisible(item), "The item you call GetPrevVisible() "
507 "for must be visible itself!");
02ce7b72 508
a3168196 509 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
08b7c251 510}
02ce7b72 511
08b7c251
VZ
512// ----------------------------------------------------------------------------
513// Usual operations
514// ----------------------------------------------------------------------------
02ce7b72 515
08b7c251
VZ
516wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
517 wxTreeItemId hInsertAfter,
518 const wxString& text,
519 int image, int selectedImage,
520 wxTreeItemData *data)
521{
522 TV_INSERTSTRUCT tvIns;
06e38c8e
JS
523 tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent;
524 tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter;
58a8ab88
JS
525
526 // This is how we insert the item as the first child: supply a NULL hInsertAfter
527 if (tvIns.hInsertAfter == (HTREEITEM) 0)
528 {
529 tvIns.hInsertAfter = TVI_FIRST;
530 }
531
08b7c251
VZ
532 UINT mask = 0;
533 if ( !text.IsEmpty() )
534 {
535 mask |= TVIF_TEXT;
536 tvIns.item.pszText = (char *)text.c_str(); // cast is ok
537 }
02ce7b72 538
08b7c251
VZ
539 if ( image != -1 )
540 {
541 mask |= TVIF_IMAGE;
542 tvIns.item.iImage = image;
3a5a2f56 543
6b037754 544 if ( selectedImage == -1 )
3a5a2f56
VZ
545 {
546 // take the same image for selected icon if not specified
547 selectedImage = image;
548 }
08b7c251 549 }
02ce7b72 550
08b7c251
VZ
551 if ( selectedImage != -1 )
552 {
553 mask |= TVIF_SELECTEDIMAGE;
554 tvIns.item.iSelectedImage = selectedImage;
555 }
02ce7b72 556
08b7c251
VZ
557 if ( data != NULL )
558 {
559 mask |= TVIF_PARAM;
560 tvIns.item.lParam = (LPARAM)data;
561 }
02ce7b72 562
08b7c251 563 tvIns.item.mask = mask;
02ce7b72 564
4fabb575 565 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(wxhWnd, &tvIns);
08b7c251
VZ
566 if ( id == 0 )
567 {
568 wxLogLastError("TreeView_InsertItem");
569 }
02ce7b72 570
fd3f686c
VZ
571 if ( data != NULL )
572 {
573 // associate the application tree item with Win32 tree item handle
574 data->SetId((WXHTREEITEM)id);
575 }
576
06e38c8e 577 return wxTreeItemId((WXHTREEITEM)id);
2bda0e17
KB
578}
579
08b7c251
VZ
580// for compatibility only
581wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
582 const wxString& text,
583 int image, int selImage,
584 long insertAfter)
2bda0e17 585{
06e38c8e 586 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
08b7c251 587 image, selImage, NULL);
2bda0e17
KB
588}
589
08b7c251
VZ
590wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
591 int image, int selectedImage,
592 wxTreeItemData *data)
2bda0e17 593{
06e38c8e 594 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
08b7c251 595 text, image, selectedImage, data);
2bda0e17
KB
596}
597
08b7c251
VZ
598wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
599 const wxString& text,
600 int image, int selectedImage,
601 wxTreeItemData *data)
2bda0e17 602{
06e38c8e 603 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
08b7c251 604 text, image, selectedImage, data);
2bda0e17
KB
605}
606
08b7c251
VZ
607wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
608 const wxTreeItemId& idPrevious,
609 const wxString& text,
610 int image, int selectedImage,
611 wxTreeItemData *data)
2bda0e17 612{
08b7c251 613 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
2bda0e17
KB
614}
615
08b7c251
VZ
616wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
617 const wxString& text,
618 int image, int selectedImage,
619 wxTreeItemData *data)
2bda0e17 620{
06e38c8e 621 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
08b7c251 622 text, image, selectedImage, data);
2bda0e17
KB
623}
624
08b7c251 625void wxTreeCtrl::Delete(const wxTreeItemId& item)
2bda0e17 626{
06e38c8e 627 if ( !TreeView_DeleteItem(wxhWnd, (HTREEITEM)(WXHTREEITEM)item) )
bbcdf8bc 628 {
08b7c251 629 wxLogLastError("TreeView_DeleteItem");
bbcdf8bc 630 }
bbcdf8bc
JS
631}
632
23fd5130
VZ
633// delete all children (but don't delete the item itself)
634void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
635{
636 long cookie;
637
638 wxArrayLong children;
639 wxTreeItemId child = GetFirstChild(item, cookie);
640 while ( child.IsOk() )
641 {
642 children.Add((long)(WXHTREEITEM)child);
643
644 child = GetNextChild(item, cookie);
645 }
646
647 size_t nCount = children.Count();
648 for ( size_t n = 0; n < nCount; n++ )
649 {
650 if ( !TreeView_DeleteItem(wxhWnd, (HTREEITEM)children[n]) )
651 {
652 wxLogLastError("TreeView_DeleteItem");
653 }
654 }
655}
656
08b7c251 657void wxTreeCtrl::DeleteAllItems()
bbcdf8bc 658{
06e38c8e 659 if ( !TreeView_DeleteAllItems(wxhWnd) )
bbcdf8bc 660 {
08b7c251 661 wxLogLastError("TreeView_DeleteAllItems");
bbcdf8bc 662 }
2bda0e17
KB
663}
664
08b7c251 665void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
2bda0e17 666{
dd3646fd
VZ
667 wxASSERT_MSG( flag == TVE_COLLAPSE ||
668 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
669 flag == TVE_EXPAND ||
670 flag == TVE_TOGGLE,
08b7c251
VZ
671 "Unknown flag in wxTreeCtrl::DoExpand" );
672
673 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
674 // emulate them
06e38c8e 675 if ( TreeView_Expand(wxhWnd, (HTREEITEM) (WXHTREEITEM) item, flag) != 0 )
08b7c251
VZ
676 {
677 wxTreeEvent event(wxEVT_NULL, m_windowId);
678 event.m_item = item;
679
680 bool isExpanded = IsExpanded(item);
2bda0e17 681
08b7c251 682 event.SetEventObject(this);
2bda0e17 683
08b7c251
VZ
684 // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded
685 event.SetEventType(g_events[isExpanded][TRUE]);
686 GetEventHandler()->ProcessEvent(event);
2bda0e17 687
08b7c251
VZ
688 event.SetEventType(g_events[isExpanded][FALSE]);
689 GetEventHandler()->ProcessEvent(event);
690 }
691 else
692 {
693 // I wonder if it really ever happens...
694 wxLogDebug("TreeView_Expand: change didn't took place.");
695 }
2bda0e17
KB
696}
697
08b7c251 698void wxTreeCtrl::Expand(const wxTreeItemId& item)
2bda0e17 699{
08b7c251 700 DoExpand(item, TVE_EXPAND);
2bda0e17 701}
2bda0e17 702
08b7c251 703void wxTreeCtrl::Collapse(const wxTreeItemId& item)
2bda0e17 704{
08b7c251 705 DoExpand(item, TVE_COLLAPSE);
2bda0e17
KB
706}
707
08b7c251 708void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
2bda0e17 709{
dd3646fd 710 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
2bda0e17
KB
711}
712
08b7c251 713void wxTreeCtrl::Toggle(const wxTreeItemId& item)
2bda0e17 714{
08b7c251 715 DoExpand(item, TVE_TOGGLE);
2bda0e17
KB
716}
717
42c5812d
UU
718void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
719{
720 DoExpand(item, action);
721}
722
08b7c251 723void wxTreeCtrl::Unselect()
2bda0e17 724{
06e38c8e 725 SelectItem(wxTreeItemId((WXHTREEITEM) 0));
08b7c251 726}
02ce7b72 727
08b7c251
VZ
728void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
729{
06e38c8e 730 if ( !TreeView_SelectItem(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) )
2bda0e17 731 {
08b7c251 732 wxLogLastError("TreeView_SelectItem");
2bda0e17 733 }
08b7c251 734}
2bda0e17 735
08b7c251
VZ
736void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
737{
738 // no error return
06e38c8e 739 TreeView_EnsureVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item);
08b7c251
VZ
740}
741
742void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
743{
06e38c8e 744 if ( !TreeView_SelectSetFirstVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) )
2bda0e17 745 {
08b7c251 746 wxLogLastError("TreeView_SelectSetFirstVisible");
2bda0e17 747 }
08b7c251
VZ
748}
749
750wxTextCtrl* wxTreeCtrl::GetEditControl() const
751{
752 return m_textCtrl;
753}
754
755void wxTreeCtrl::DeleteTextCtrl()
756{
757 if ( m_textCtrl )
2bda0e17 758 {
08b7c251
VZ
759 m_textCtrl->UnsubclassWin();
760 m_textCtrl->SetHWND(0);
761 delete m_textCtrl;
762 m_textCtrl = NULL;
2bda0e17 763 }
08b7c251 764}
2bda0e17 765
08b7c251
VZ
766wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
767 wxClassInfo* textControlClass)
768{
769 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
770
06e38c8e 771 HWND hWnd = (HWND) TreeView_EditLabel(wxhWnd, (HTREEITEM) (WXHTREEITEM) item);
2bda0e17 772
08b7c251 773 wxCHECK_MSG( hWnd, NULL, "Can't edit tree ctrl label" );
2bda0e17 774
08b7c251 775 DeleteTextCtrl();
2bda0e17 776
08b7c251
VZ
777 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
778 m_textCtrl->SetHWND((WXHWND)hWnd);
779 m_textCtrl->SubclassWin((WXHWND)hWnd);
2bda0e17 780
08b7c251 781 return m_textCtrl;
2bda0e17
KB
782}
783
08b7c251
VZ
784// End label editing, optionally cancelling the edit
785void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges)
2bda0e17 786{
06e38c8e 787 TreeView_EndEditLabelNow(wxhWnd, discardChanges);
08b7c251
VZ
788
789 DeleteTextCtrl();
2bda0e17
KB
790}
791
08b7c251 792wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
2bda0e17 793{
08b7c251
VZ
794 TV_HITTESTINFO hitTestInfo;
795 hitTestInfo.pt.x = (int)point.x;
796 hitTestInfo.pt.y = (int)point.y;
2bda0e17 797
06e38c8e 798 TreeView_HitTest(wxhWnd, &hitTestInfo);
2bda0e17 799
08b7c251
VZ
800 flags = 0;
801
802 // avoid repetition
803 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
804 flags |= wxTREE_HITTEST_##flag
805
806 TRANSLATE_FLAG(ABOVE);
807 TRANSLATE_FLAG(BELOW);
808 TRANSLATE_FLAG(NOWHERE);
809 TRANSLATE_FLAG(ONITEMBUTTON);
810 TRANSLATE_FLAG(ONITEMICON);
811 TRANSLATE_FLAG(ONITEMINDENT);
812 TRANSLATE_FLAG(ONITEMLABEL);
813 TRANSLATE_FLAG(ONITEMRIGHT);
814 TRANSLATE_FLAG(ONITEMSTATEICON);
815 TRANSLATE_FLAG(TOLEFT);
816 TRANSLATE_FLAG(TORIGHT);
2bda0e17 817
08b7c251
VZ
818 #undef TRANSLATE_FLAG
819
06e38c8e 820 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
08b7c251
VZ
821}
822
f7c832a7
VZ
823bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
824 wxRect& rect,
825 bool textOnly) const
826{
827 RECT rc;
828 if ( TreeView_GetItemRect(wxhWnd, (HTREEITEM)(WXHTREEITEM)item,
829 &rc, textOnly) )
830 {
831 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
832
833 return TRUE;
834 }
835 else
836 {
837 // couldn't retrieve rect: for example, item isn't visible
838 return FALSE;
839 }
840}
841
23fd5130
VZ
842// ----------------------------------------------------------------------------
843// sorting stuff
844// ----------------------------------------------------------------------------
f7c832a7 845
23fd5130
VZ
846static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1,
847 wxTreeItemData *pItem2,
848 wxTreeCtrl *tree)
849{
096c9f9b
VZ
850 wxCHECK_MSG( pItem1 && pItem2, 0,
851 _T("sorting tree without data doesn't make sense") );
852
23fd5130
VZ
853 return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId());
854}
855
95aabccc
VZ
856int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
857 const wxTreeItemId& item2)
08b7c251 858{
95aabccc
VZ
859 return strcmp(GetItemText(item1), GetItemText(item2));
860}
861
862void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
863{
864 // rely on the fact that TreeView_SortChildren does the same thing as our
23fd5130
VZ
865 // default behaviour, i.e. sorts items alphabetically and so call it
866 // directly if we're not in derived class (much more efficient!)
867 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2bda0e17 868 {
23fd5130 869 TreeView_SortChildren(wxhWnd, (HTREEITEM)(WXHTREEITEM)item, 0);
2bda0e17 870 }
08b7c251 871 else
2bda0e17 872 {
62448488 873 TV_SORTCB tvSort;
23fd5130
VZ
874 tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item;
875 tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback;
876 tvSort.lParam = (LPARAM)this;
877 TreeView_SortChildrenCB(wxhWnd, &tvSort, 0 /* reserved */);
2bda0e17 878 }
08b7c251 879}
2bda0e17 880
08b7c251
VZ
881// ----------------------------------------------------------------------------
882// implementation
883// ----------------------------------------------------------------------------
2bda0e17 884
08b7c251
VZ
885bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
886{
887 if ( cmd == EN_UPDATE )
2bda0e17 888 {
08b7c251
VZ
889 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
890 event.SetEventObject( this );
891 ProcessCommand(event);
2bda0e17 892 }
08b7c251 893 else if ( cmd == EN_KILLFOCUS )
2bda0e17 894 {
08b7c251
VZ
895 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
896 event.SetEventObject( this );
897 ProcessCommand(event);
2bda0e17 898 }
08b7c251 899 else
2bda0e17 900 {
08b7c251
VZ
901 // nothing done
902 return FALSE;
2bda0e17 903 }
08b7c251
VZ
904
905 // command processed
906 return TRUE;
907}
908
909// process WM_NOTIFY Windows message
a23fd0e1 910bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
08b7c251
VZ
911{
912 wxTreeEvent event(wxEVT_NULL, m_windowId);
913 wxEventType eventType = wxEVT_NULL;
914 NMHDR *hdr = (NMHDR *)lParam;
915
916 switch ( hdr->code )
2bda0e17 917 {
08b7c251
VZ
918 case TVN_BEGINDRAG:
919 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
920 // fall through
921
922 case TVN_BEGINRDRAG:
923 {
924 if ( eventType == wxEVT_NULL )
925 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
926 //else: left drag, already set above
927
928 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
929
06e38c8e 930 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
08b7c251
VZ
931 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
932 break;
933 }
934
935 case TVN_BEGINLABELEDIT:
936 {
937 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
938 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
939
06e38c8e 940 event.m_item = (WXHTREEITEM) info->item.hItem;
08b7c251
VZ
941 break;
942 }
943
944 case TVN_DELETEITEM:
945 {
946 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
947 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
948
06e38c8e 949 event.m_item = (WXHTREEITEM) tv->itemOld.hItem;
08b7c251
VZ
950 break;
951 }
952
953 case TVN_ENDLABELEDIT:
954 {
955 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
956 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
957
06e38c8e 958 event.m_item = (WXHTREEITEM) info->item.hItem;
08b7c251
VZ
959 break;
960 }
961
962 case TVN_GETDISPINFO:
963 eventType = wxEVT_COMMAND_TREE_GET_INFO;
964 // fall through
965
966 case TVN_SETDISPINFO:
967 {
968 if ( eventType == wxEVT_NULL )
969 eventType = wxEVT_COMMAND_TREE_SET_INFO;
970 //else: get, already set above
971
972 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
973
06e38c8e 974 event.m_item = (WXHTREEITEM) info->item.hItem;
08b7c251
VZ
975 break;
976 }
977
978 case TVN_ITEMEXPANDING:
979 event.m_code = FALSE;
980 // fall through
981
982 case TVN_ITEMEXPANDED:
983 {
984 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
985
986 bool expand = FALSE;
987 switch ( tv->action )
988 {
989 case TVE_EXPAND:
990 expand = TRUE;
991 break;
992
993 case TVE_COLLAPSE:
994 expand = FALSE;
995 break;
996
997 default:
998 wxLogDebug("unexpected code %d in TVN_ITEMEXPAND "
999 "message", tv->action);
1000 }
1001
06e38c8e 1002 bool ing = (hdr->code == TVN_ITEMEXPANDING);
08b7c251
VZ
1003 eventType = g_events[expand][ing];
1004
06e38c8e 1005 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
08b7c251
VZ
1006 break;
1007 }
1008
1009 case TVN_KEYDOWN:
1010 {
1011 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
1012 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
1013
1014 event.m_code = wxCharCodeMSWToWX(info->wVKey);
23fd5130
VZ
1015
1016 // a separate event for this case
1017 if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN )
1018 {
1019 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
1020 m_windowId);
1021 event2.SetEventObject(this);
1022
1023 GetEventHandler()->ProcessEvent(event2);
1024 }
08b7c251
VZ
1025 break;
1026 }
1027
1028 case TVN_SELCHANGED:
1029 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
1030 // fall through
1031
1032 case TVN_SELCHANGING:
1033 {
1034 if ( eventType == wxEVT_NULL )
1035 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
1036 //else: already set above
1037
1038 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1039
06e38c8e
JS
1040 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1041 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
08b7c251
VZ
1042 break;
1043 }
1044
1045 default:
a23fd0e1 1046 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2bda0e17 1047 }
08b7c251
VZ
1048
1049 event.SetEventObject(this);
1050 event.SetEventType(eventType);
1051
fd3f686c 1052 bool processed = GetEventHandler()->ProcessEvent(event);
08b7c251
VZ
1053
1054 // post processing
fd3f686c 1055 if ( hdr->code == TVN_DELETEITEM )
2bda0e17 1056 {
08b7c251
VZ
1057 // NB: we might process this message using wxWindows event tables, but
1058 // due to overhead of wxWin event system we prefer to do it here
1059 // (otherwise deleting a tree with many items is just too slow)
fd3f686c
VZ
1060 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1061 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
1062 delete data; // may be NULL, ok
8162b848 1063 processed = TRUE; // Make sure we don't get called twice
2bda0e17 1064 }
08b7c251 1065
fd3f686c
VZ
1066 *result = !event.IsAllowed();
1067
1068 return processed;
2bda0e17
KB
1069}
1070
08b7c251 1071// ----------------------------------------------------------------------------
2bda0e17 1072// Tree event
08b7c251
VZ
1073// ----------------------------------------------------------------------------
1074
92976ab6 1075IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
2bda0e17 1076
08b7c251 1077wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
fd3f686c 1078 : wxNotifyEvent(commandType, id)
2bda0e17 1079{
08b7c251
VZ
1080 m_code = 0;
1081 m_itemOld = 0;
2bda0e17
KB
1082}
1083
08b7c251 1084#endif // __WIN95__
2bda0e17 1085