]> git.saurik.com Git - wxWidgets.git/blame - src/generic/toolbkg.cpp
Don't crash in wxGridCellAutoWrapStringRenderer when the column is hidden.
[wxWidgets.git] / src / generic / toolbkg.cpp
CommitLineData
3c55b365
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/toolbkg.cpp
3// Purpose: generic implementation of wxToolbook
4// Author: Julian Smart
5// Modified by:
6// Created: 2006-01-29
3c55b365
JS
7// Copyright: (c) 2006 Julian Smart
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
3c55b365
JS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
34d6780e 18#if wxUSE_TOOLBOOK
3c55b365 19
28bfd0a1
DS
20#ifndef WX_PRECOMP
21 #include "wx/icon.h"
6037dd26
DS
22 #include "wx/settings.h"
23 #include "wx/toolbar.h"
28bfd0a1
DS
24#endif
25
6037dd26
DS
26#include "wx/imaglist.h"
27#include "wx/sysopt.h"
28#include "wx/toolbook.h"
b887dc7b
JS
29
30#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
64d3ed17 31#include "wx/generic/buttonbar.h"
b887dc7b 32#endif
6037dd26 33
3c55b365
JS
34// ----------------------------------------------------------------------------
35// various wxWidgets macros
36// ----------------------------------------------------------------------------
37
38// check that the page index is valid
39#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
40
41// ----------------------------------------------------------------------------
42// event table
43// ----------------------------------------------------------------------------
44
45IMPLEMENT_DYNAMIC_CLASS(wxToolbook, wxBookCtrlBase)
3c55b365 46
ce7fe42e
VZ
47wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGING, wxBookCtrlEvent );
48wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGED, wxBookCtrlEvent );
3c55b365
JS
49
50BEGIN_EVENT_TABLE(wxToolbook, wxBookCtrlBase)
51 EVT_SIZE(wxToolbook::OnSize)
52 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected)
53 EVT_IDLE(wxToolbook::OnIdle)
54END_EVENT_TABLE()
55
56// ============================================================================
57// wxToolbook implementation
58// ============================================================================
59
60// ----------------------------------------------------------------------------
61// wxToolbook creation
62// ----------------------------------------------------------------------------
63
64void wxToolbook::Init()
65{
3c55b365
JS
66 m_needsRealizing = false;
67}
68
c2794afd 69bool wxToolbook::Create(wxWindow *parent,
3c55b365
JS
70 wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75{
76 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
3c55b365 77 style |= wxBK_TOP;
3c55b365
JS
78
79 // no border for this control
80 style &= ~wxBORDER_MASK;
81 style |= wxBORDER_NONE;
82
83 if ( !wxControl::Create(parent, id, pos, size, style,
84 wxDefaultValidator, name) )
85 return false;
86
050d159c
VZ
87 int tbFlags = wxTB_TEXT | wxTB_FLAT | wxBORDER_NONE;
88 if ( (style & (wxBK_LEFT | wxBK_RIGHT)) != 0 )
89 tbFlags |= wxTB_VERTICAL;
90 else
91 tbFlags |= wxTB_HORIZONTAL;
92
93 if ( style & wxTBK_HORZ_LAYOUT )
94 tbFlags |= wxTB_HORZ_LAYOUT;
c2794afd 95
917cd965 96 // TODO: make more configurable
9804d540 97
b887dc7b 98#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
e3507dd8 99 if (style & wxTBK_BUTTONBAR)
64d3ed17
JS
100 {
101 m_bookctrl = new wxButtonToolBar
102 (
103 this,
447325a4 104 wxID_ANY,
64d3ed17
JS
105 wxDefaultPosition,
106 wxDefaultSize,
050d159c 107 tbFlags
64d3ed17
JS
108 );
109 }
110 else
b887dc7b 111#endif
64d3ed17
JS
112 {
113 m_bookctrl = new wxToolBar
3c55b365
JS
114 (
115 this,
447325a4 116 wxID_ANY,
3c55b365
JS
117 wxDefaultPosition,
118 wxDefaultSize,
050d159c 119 tbFlags | wxTB_NODIVIDER
3c55b365 120 );
64d3ed17 121 }
3c55b365
JS
122
123 return true;
124}
125
126// ----------------------------------------------------------------------------
127// wxToolbook geometry management
128// ----------------------------------------------------------------------------
129
3c55b365
JS
130void wxToolbook::OnSize(wxSizeEvent& event)
131{
132 if (m_needsRealizing)
133 Realize();
c2794afd 134
3c55b365
JS
135 wxBookCtrlBase::OnSize(event);
136}
137
3c55b365
JS
138// ----------------------------------------------------------------------------
139// accessing the pages
140// ----------------------------------------------------------------------------
141
142bool wxToolbook::SetPageText(size_t n, const wxString& strText)
143{
144 // Assume tool ids start from 1
c2794afd 145 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365
JS
146 if (tool)
147 {
148 tool->SetLabel(strText);
149 return true;
150 }
151 else
152 return false;
153}
154
155wxString wxToolbook::GetPageText(size_t n) const
156{
c2794afd 157 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365 158 if (tool)
3c55b365 159 return tool->GetLabel();
3c55b365
JS
160 else
161 return wxEmptyString;
162}
163
164int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const
165{
9a83f860 166 wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") );
3c55b365
JS
167
168 return wxNOT_FOUND;
169}
170
171bool wxToolbook::SetPageImage(size_t n, int imageId)
172{
173 wxASSERT( GetImageList() != NULL );
174 if (!GetImageList())
175 return false;
c2794afd
DS
176
177 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365
JS
178 if (tool)
179 {
180 // Find the image list index for this tool
181 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
182 tool->SetNormalBitmap(bitmap);
183 return true;
184 }
185 else
186 return false;
187}
188
189// ----------------------------------------------------------------------------
190// image list stuff
191// ----------------------------------------------------------------------------
192
193void wxToolbook::SetImageList(wxImageList *imageList)
194{
195 wxBookCtrlBase::SetImageList(imageList);
196}
197
198// ----------------------------------------------------------------------------
199// selection
200// ----------------------------------------------------------------------------
201
3e97a905 202wxBookCtrlEvent* wxToolbook::CreatePageChangingEvent() const
3c55b365 203{
ce7fe42e 204 return new wxBookCtrlEvent(wxEVT_TOOLBOOK_PAGE_CHANGING, m_windowId);
deb325e3
VZ
205}
206
3e97a905 207void wxToolbook::MakeChangedEvent(wxBookCtrlEvent &event)
deb325e3 208{
ce7fe42e 209 event.SetEventType(wxEVT_TOOLBOOK_PAGE_CHANGED);
3c55b365
JS
210}
211
8de043bb
RR
212void wxToolbook::UpdateSelectedPage(size_t newsel)
213{
214 m_selection = newsel;
215 GetToolBar()->ToggleTool(newsel + 1, true);
216}
217
3c55b365
JS
218// Not part of the wxBookctrl API, but must be called in OnIdle or
219// by application to realize the toolbar and select the initial page.
220void wxToolbook::Realize()
221{
222 if (m_needsRealizing)
223 {
1c54c4ed
VZ
224 m_needsRealizing = false;
225
3c55b365 226 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
a16c6fdc 227
3c55b365 228 GetToolBar()->Realize();
3c55b365 229 }
c2794afd 230
7e837615 231 if (m_selection == wxNOT_FOUND)
3c55b365
JS
232 m_selection = 0;
233
234 if (GetPageCount() > 0)
235 {
236 int sel = m_selection;
7e837615 237 m_selection = wxNOT_FOUND;
3c55b365
JS
238 SetSelection(sel);
239 }
c2794afd 240
3c55b365
JS
241 DoSize();
242}
243
d0a84b63
VZ
244int wxToolbook::HitTest(const wxPoint& pt, long *flags) const
245{
246 int pagePos = wxNOT_FOUND;
247
248 if ( flags )
9804d540 249 *flags = wxBK_HITTEST_NOWHERE;
d0a84b63
VZ
250
251 // convert from wxToolbook coordinates to wxToolBar ones
252 const wxToolBarBase * const tbar = GetToolBar();
253 const wxPoint tbarPt = tbar->ScreenToClient(ClientToScreen(pt));
254
255 // is the point over the toolbar?
22a35096 256 if ( wxRect(tbar->GetSize()).Contains(tbarPt) )
d0a84b63
VZ
257 {
258 const wxToolBarToolBase * const
259 tool = tbar->FindToolForPosition(tbarPt.x, tbarPt.y);
260
261 if ( tool )
262 {
263 pagePos = tbar->GetToolPos(tool->GetId());
264 if ( flags )
9804d540 265 *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL;
d0a84b63
VZ
266 }
267 }
268 else // not over the toolbar
269 {
22a35096 270 if ( flags && GetPageRect().Contains(pt) )
9804d540 271 *flags |= wxBK_HITTEST_ONPAGE;
d0a84b63
VZ
272 }
273
274 return pagePos;
275}
276
3c55b365
JS
277void wxToolbook::OnIdle(wxIdleEvent& event)
278{
279 if (m_needsRealizing)
280 Realize();
281 event.Skip();
282}
283
284// ----------------------------------------------------------------------------
285// adding/removing the pages
286// ----------------------------------------------------------------------------
287
c2794afd 288bool wxToolbook::InsertPage(size_t n,
3c55b365
JS
289 wxWindow *page,
290 const wxString& text,
291 bool bSelect,
292 int imageId)
293{
294 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
295 return false;
296
297 m_needsRealizing = true;
c2794afd 298
3c55b365 299 wxASSERT(GetImageList() != NULL);
c2794afd 300
3c55b365
JS
301 if (!GetImageList())
302 return false;
303
556bf2c3
JS
304 // TODO: make sure all platforms can convert between icon and bitmap,
305 // and/or test whether the image is a bitmap or an icon.
306#ifdef __WXMAC__
307 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
308#else
3c55b365
JS
309 // On Windows, we can lose information by using GetBitmap, so extract icon instead
310 wxIcon icon = GetImageList()->GetIcon(imageId);
311 wxBitmap bitmap;
312 bitmap.CopyFromIcon(icon);
556bf2c3 313#endif
c2794afd 314
3c55b365
JS
315 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
316 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
c2794afd 317
3c55b365 318 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
c2794afd 319 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
3c55b365
JS
320
321 if (bSelect)
322 {
77631b1d 323 GetToolBar()->ToggleTool(n, true);
3c55b365
JS
324 m_selection = n;
325 }
326 else
327 page->Hide();
328
329 InvalidateBestSize();
330 return true;
331}
332
333wxWindow *wxToolbook::DoRemovePage(size_t page)
334{
3c55b365
JS
335 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
336
337 if ( win )
338 {
c2794afd 339 GetToolBar()->DeleteTool(page + 1);
3c55b365 340
4f9ccec5 341 DoSetSelectionAfterRemoval(page);
3c55b365
JS
342 }
343
344 return win;
345}
346
347
348bool wxToolbook::DeleteAllPages()
349{
350 GetToolBar()->ClearTools();
351 return wxBookCtrlBase::DeleteAllPages();
352}
353
354// ----------------------------------------------------------------------------
355// wxToolbook events
356// ----------------------------------------------------------------------------
357
358void wxToolbook::OnToolSelected(wxCommandEvent& event)
359{
c2794afd 360 const int selNew = event.GetId() - 1;
3c55b365
JS
361
362 if ( selNew == m_selection )
363 {
364 // this event can only come from our own Select(m_selection) below
365 // which we call when the page change is vetoed, so we should simply
366 // ignore it
367 return;
368 }
369
370 SetSelection(selNew);
371
372 // change wasn't allowed, return to previous state
373 if (m_selection != selNew)
28bfd0a1 374 {
3c55b365 375 GetToolBar()->ToggleTool(m_selection, false);
28bfd0a1 376 }
3c55b365
JS
377}
378
379#endif // wxUSE_TOOLBOOK