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