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