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