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