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