wxButtonToolBar only useful on Mac right now
[wxWidgets.git] / src / generic / toolbkg.cpp
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
7 // RCS-ID: $Id$
8 // Copyright: (c) 2006 Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_TOOLBOOK
20
21 #ifndef WX_PRECOMP
22 #include "wx/icon.h"
23 #include "wx/settings.h"
24 #include "wx/toolbar.h"
25 #endif
26
27 #include "wx/imaglist.h"
28 #include "wx/sysopt.h"
29 #include "wx/toolbook.h"
30
31 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
32 #include "wx/generic/buttonbar.h"
33 #endif
34
35 // ----------------------------------------------------------------------------
36 // various wxWidgets macros
37 // ----------------------------------------------------------------------------
38
39 // check that the page index is valid
40 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41
42 // ----------------------------------------------------------------------------
43 // event table
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_DYNAMIC_CLASS(wxToolbook, wxBookCtrlBase)
47 IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent, wxNotifyEvent)
48
49 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING = wxNewEventType();
50 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED = wxNewEventType();
51 const int wxID_TOOLBOOKTOOLBAR = wxNewId();
52
53 BEGIN_EVENT_TABLE(wxToolbook, wxBookCtrlBase)
54 EVT_SIZE(wxToolbook::OnSize)
55 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected)
56 EVT_IDLE(wxToolbook::OnIdle)
57 END_EVENT_TABLE()
58
59 // ============================================================================
60 // wxToolbook implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // wxToolbook creation
65 // ----------------------------------------------------------------------------
66
67 void wxToolbook::Init()
68 {
69 m_selection = wxNOT_FOUND;
70 m_needsRealizing = false;
71 }
72
73 bool wxToolbook::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxPoint& pos,
76 const wxSize& size,
77 long style,
78 const wxString& name)
79 {
80 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
81 style |= wxBK_TOP;
82
83 // no border for this control
84 style &= ~wxBORDER_MASK;
85 style |= wxBORDER_NONE;
86
87 if ( !wxControl::Create(parent, id, pos, size, style,
88 wxDefaultValidator, name) )
89 return false;
90
91 int orient = wxTB_HORIZONTAL;
92 if ( (style & (wxBK_LEFT | wxBK_RIGHT)) != 0)
93 orient = wxTB_VERTICAL;
94
95 // TODO: make more configurable
96
97 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
98 if (style & wxBK_BUTTONBAR)
99 {
100 m_bookctrl = new wxButtonToolBar
101 (
102 this,
103 wxID_TOOLBOOKTOOLBAR,
104 wxDefaultPosition,
105 wxDefaultSize,
106 orient|wxTB_TEXT|wxTB_FLAT|wxTB_NODIVIDER|wxNO_BORDER
107 );
108 }
109 else
110 #endif
111 {
112 m_bookctrl = new wxToolBar
113 (
114 this,
115 wxID_TOOLBOOKTOOLBAR,
116 wxDefaultPosition,
117 wxDefaultSize,
118 orient|wxTB_TEXT|wxTB_FLAT|wxTB_NODIVIDER|wxNO_BORDER
119 );
120 }
121
122 return true;
123 }
124
125 // ----------------------------------------------------------------------------
126 // wxToolbook geometry management
127 // ----------------------------------------------------------------------------
128
129 wxSize wxToolbook::GetControllerSize() const
130 {
131 const wxSize sizeClient = GetClientSize(),
132 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
133 sizeToolBar = GetToolBar()->GetSize() + sizeBorder;
134
135 wxSize size;
136
137 if ( IsVertical() )
138 {
139 size.x = sizeClient.x;
140 size.y = sizeToolBar.y;
141 }
142 else // left/right aligned
143 {
144 size.x = sizeToolBar.x;
145 size.y = sizeClient.y;
146 }
147
148 return size;
149 }
150
151 void wxToolbook::OnSize(wxSizeEvent& event)
152 {
153 if (m_needsRealizing)
154 Realize();
155
156 wxBookCtrlBase::OnSize(event);
157 }
158
159 wxSize wxToolbook::CalcSizeFromPage(const wxSize& sizePage) const
160 {
161 // we need to add the size of the list control and the border between
162 const wxSize sizeToolBar = GetControllerSize();
163
164 wxSize size = sizePage;
165 if ( IsVertical() )
166 {
167 size.y += sizeToolBar.y + GetInternalBorder();
168 }
169 else // left/right aligned
170 {
171 size.x += sizeToolBar.x + GetInternalBorder();
172 }
173
174 return size;
175 }
176
177 // ----------------------------------------------------------------------------
178 // accessing the pages
179 // ----------------------------------------------------------------------------
180
181 bool wxToolbook::SetPageText(size_t n, const wxString& strText)
182 {
183 // Assume tool ids start from 1
184 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
185 if (tool)
186 {
187 tool->SetLabel(strText);
188 return true;
189 }
190 else
191 return false;
192 }
193
194 wxString wxToolbook::GetPageText(size_t n) const
195 {
196 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
197 if (tool)
198 return tool->GetLabel();
199 else
200 return wxEmptyString;
201 }
202
203 int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const
204 {
205 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
206
207 return wxNOT_FOUND;
208 }
209
210 bool wxToolbook::SetPageImage(size_t n, int imageId)
211 {
212 wxASSERT( GetImageList() != NULL );
213 if (!GetImageList())
214 return false;
215
216 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
217 if (tool)
218 {
219 // Find the image list index for this tool
220 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
221 tool->SetNormalBitmap(bitmap);
222 return true;
223 }
224 else
225 return false;
226 }
227
228 // ----------------------------------------------------------------------------
229 // image list stuff
230 // ----------------------------------------------------------------------------
231
232 void wxToolbook::SetImageList(wxImageList *imageList)
233 {
234 wxBookCtrlBase::SetImageList(imageList);
235 }
236
237 // ----------------------------------------------------------------------------
238 // selection
239 // ----------------------------------------------------------------------------
240
241 int wxToolbook::GetSelection() const
242 {
243 return m_selection;
244 }
245
246 int wxToolbook::SetSelection(size_t n)
247 {
248 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
249 wxT("invalid page index in wxToolbook::SetSelection()") );
250
251 const int oldSel = m_selection;
252
253 if ( int(n) != m_selection )
254 {
255 wxToolbookEvent event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING, m_windowId);
256 event.SetSelection(n);
257 event.SetOldSelection(m_selection);
258 event.SetEventObject(this);
259 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
260 {
261 if ( m_selection != wxNOT_FOUND )
262 m_pages[m_selection]->Hide();
263
264 wxWindow *page = m_pages[n];
265 page->SetSize(GetPageRect());
266 page->Show();
267
268 // change m_selection now to ignore the selection change event
269 m_selection = n;
270 GetToolBar()->ToggleTool(n + 1, true);
271
272 // program allows the page change
273 event.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED);
274 (void)GetEventHandler()->ProcessEvent(event);
275 }
276 }
277
278 return oldSel;
279 }
280
281 // Not part of the wxBookctrl API, but must be called in OnIdle or
282 // by application to realize the toolbar and select the initial page.
283 void wxToolbook::Realize()
284 {
285 if (m_needsRealizing)
286 {
287 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
288
289 int remap = wxSystemOptions::GetOptionInt(wxT("msw.remap"));
290 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
291 GetToolBar()->Realize();
292 wxSystemOptions::SetOption(wxT("msw.remap"), remap);
293 }
294
295 m_needsRealizing = false;
296
297 if (m_selection == -1)
298 m_selection = 0;
299
300 if (GetPageCount() > 0)
301 {
302 int sel = m_selection;
303 m_selection = -1;
304 SetSelection(sel);
305 }
306
307 DoSize();
308 }
309
310 void wxToolbook::OnIdle(wxIdleEvent& event)
311 {
312 if (m_needsRealizing)
313 Realize();
314 event.Skip();
315 }
316
317 // ----------------------------------------------------------------------------
318 // adding/removing the pages
319 // ----------------------------------------------------------------------------
320
321 bool wxToolbook::InsertPage(size_t n,
322 wxWindow *page,
323 const wxString& text,
324 bool bSelect,
325 int imageId)
326 {
327 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
328 return false;
329
330 m_needsRealizing = true;
331
332 wxASSERT(GetImageList() != NULL);
333
334 if (!GetImageList())
335 return false;
336
337 // TODO: make sure all platforms can convert between icon and bitmap,
338 // and/or test whether the image is a bitmap or an icon.
339 #ifdef __WXMAC__
340 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
341 #else
342 // On Windows, we can lose information by using GetBitmap, so extract icon instead
343 wxIcon icon = GetImageList()->GetIcon(imageId);
344 wxBitmap bitmap;
345 bitmap.CopyFromIcon(icon);
346 #endif
347
348 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
349 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
350
351 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
352 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
353
354 if (bSelect)
355 {
356 // GetToolBar()->ToggleTool(n, true);
357 m_selection = n;
358 }
359 else
360 page->Hide();
361
362 InvalidateBestSize();
363 return true;
364 }
365
366 wxWindow *wxToolbook::DoRemovePage(size_t page)
367 {
368 const size_t page_count = GetPageCount();
369 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
370
371 if ( win )
372 {
373 GetToolBar()->DeleteTool(page + 1);
374
375 if (m_selection >= (int)page)
376 {
377 // force new sel valid if possible
378 int sel = m_selection - 1;
379 if (page_count == 1)
380 sel = wxNOT_FOUND;
381 else if ((page_count == 2) || (sel == -1))
382 sel = 0;
383
384 // force sel invalid if deleting current page - don't try to hide it
385 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
386
387 if ((sel != wxNOT_FOUND) && (sel != m_selection))
388 SetSelection(sel);
389 }
390 }
391
392 return win;
393 }
394
395
396 bool wxToolbook::DeleteAllPages()
397 {
398 GetToolBar()->ClearTools();
399 return wxBookCtrlBase::DeleteAllPages();
400 }
401
402 // ----------------------------------------------------------------------------
403 // wxToolbook events
404 // ----------------------------------------------------------------------------
405
406 void wxToolbook::OnToolSelected(wxCommandEvent& event)
407 {
408 const int selNew = event.GetId() - 1;
409
410 if ( selNew == m_selection )
411 {
412 // this event can only come from our own Select(m_selection) below
413 // which we call when the page change is vetoed, so we should simply
414 // ignore it
415 return;
416 }
417
418 SetSelection(selNew);
419
420 // change wasn't allowed, return to previous state
421 if (m_selection != selNew)
422 {
423 GetToolBar()->ToggleTool(m_selection, false);
424 }
425 }
426
427 #endif // wxUSE_TOOLBOOK