Correct controller size determination in wxToolbook.
[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 wxSize wxToolbook::GetControllerSize() const
133 {
134 const wxSize sizeClient = GetClientSize(),
135 sizeToolBar = GetToolBar()->GetBestSize();
136
137 wxSize size;
138
139 if ( IsVertical() )
140 {
141 size.x = sizeClient.x;
142 size.y = sizeToolBar.y;
143 }
144 else // left/right aligned
145 {
146 size.x = sizeToolBar.x;
147 size.y = sizeClient.y;
148 }
149
150 return size;
151 }
152
153 void wxToolbook::OnSize(wxSizeEvent& event)
154 {
155 if (m_needsRealizing)
156 Realize();
157
158 wxBookCtrlBase::OnSize(event);
159 }
160
161 wxSize wxToolbook::CalcSizeFromPage(const wxSize& sizePage) const
162 {
163 // we need to add the size of the list control and the border between
164 const wxSize sizeToolBar = GetControllerSize();
165
166 wxSize size = sizePage;
167 if ( IsVertical() )
168 {
169 size.y += sizeToolBar.y + GetInternalBorder();
170 }
171 else // left/right aligned
172 {
173 size.x += sizeToolBar.x + GetInternalBorder();
174 }
175
176 return size;
177 }
178
179 // ----------------------------------------------------------------------------
180 // accessing the pages
181 // ----------------------------------------------------------------------------
182
183 bool wxToolbook::SetPageText(size_t n, const wxString& strText)
184 {
185 // Assume tool ids start from 1
186 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
187 if (tool)
188 {
189 tool->SetLabel(strText);
190 return true;
191 }
192 else
193 return false;
194 }
195
196 wxString wxToolbook::GetPageText(size_t n) const
197 {
198 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
199 if (tool)
200 return tool->GetLabel();
201 else
202 return wxEmptyString;
203 }
204
205 int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const
206 {
207 wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") );
208
209 return wxNOT_FOUND;
210 }
211
212 bool wxToolbook::SetPageImage(size_t n, int imageId)
213 {
214 wxASSERT( GetImageList() != NULL );
215 if (!GetImageList())
216 return false;
217
218 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
219 if (tool)
220 {
221 // Find the image list index for this tool
222 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
223 tool->SetNormalBitmap(bitmap);
224 return true;
225 }
226 else
227 return false;
228 }
229
230 // ----------------------------------------------------------------------------
231 // image list stuff
232 // ----------------------------------------------------------------------------
233
234 void wxToolbook::SetImageList(wxImageList *imageList)
235 {
236 wxBookCtrlBase::SetImageList(imageList);
237 }
238
239 // ----------------------------------------------------------------------------
240 // selection
241 // ----------------------------------------------------------------------------
242
243 int wxToolbook::GetSelection() const
244 {
245 return m_selection;
246 }
247
248 wxBookCtrlEvent* wxToolbook::CreatePageChangingEvent() const
249 {
250 return new wxBookCtrlEvent(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING, m_windowId);
251 }
252
253 void wxToolbook::MakeChangedEvent(wxBookCtrlEvent &event)
254 {
255 event.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED);
256 }
257
258 void wxToolbook::UpdateSelectedPage(size_t newsel)
259 {
260 m_selection = newsel;
261 GetToolBar()->ToggleTool(newsel + 1, true);
262 }
263
264 // Not part of the wxBookctrl API, but must be called in OnIdle or
265 // by application to realize the toolbar and select the initial page.
266 void wxToolbook::Realize()
267 {
268 if (m_needsRealizing)
269 {
270 m_needsRealizing = false;
271
272 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
273
274 GetToolBar()->Realize();
275 }
276
277 if (m_selection == -1)
278 m_selection = 0;
279
280 if (GetPageCount() > 0)
281 {
282 int sel = m_selection;
283 m_selection = -1;
284 SetSelection(sel);
285 }
286
287 DoSize();
288 }
289
290 int wxToolbook::HitTest(const wxPoint& pt, long *flags) const
291 {
292 int pagePos = wxNOT_FOUND;
293
294 if ( flags )
295 *flags = wxBK_HITTEST_NOWHERE;
296
297 // convert from wxToolbook coordinates to wxToolBar ones
298 const wxToolBarBase * const tbar = GetToolBar();
299 const wxPoint tbarPt = tbar->ScreenToClient(ClientToScreen(pt));
300
301 // is the point over the toolbar?
302 if ( wxRect(tbar->GetSize()).Contains(tbarPt) )
303 {
304 const wxToolBarToolBase * const
305 tool = tbar->FindToolForPosition(tbarPt.x, tbarPt.y);
306
307 if ( tool )
308 {
309 pagePos = tbar->GetToolPos(tool->GetId());
310 if ( flags )
311 *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL;
312 }
313 }
314 else // not over the toolbar
315 {
316 if ( flags && GetPageRect().Contains(pt) )
317 *flags |= wxBK_HITTEST_ONPAGE;
318 }
319
320 return pagePos;
321 }
322
323 void wxToolbook::OnIdle(wxIdleEvent& event)
324 {
325 if (m_needsRealizing)
326 Realize();
327 event.Skip();
328 }
329
330 // ----------------------------------------------------------------------------
331 // adding/removing the pages
332 // ----------------------------------------------------------------------------
333
334 bool wxToolbook::InsertPage(size_t n,
335 wxWindow *page,
336 const wxString& text,
337 bool bSelect,
338 int imageId)
339 {
340 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
341 return false;
342
343 m_needsRealizing = true;
344
345 wxASSERT(GetImageList() != NULL);
346
347 if (!GetImageList())
348 return false;
349
350 // TODO: make sure all platforms can convert between icon and bitmap,
351 // and/or test whether the image is a bitmap or an icon.
352 #ifdef __WXMAC__
353 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
354 #else
355 // On Windows, we can lose information by using GetBitmap, so extract icon instead
356 wxIcon icon = GetImageList()->GetIcon(imageId);
357 wxBitmap bitmap;
358 bitmap.CopyFromIcon(icon);
359 #endif
360
361 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
362 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
363
364 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
365 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
366
367 if (bSelect)
368 {
369 GetToolBar()->ToggleTool(n, true);
370 m_selection = n;
371 }
372 else
373 page->Hide();
374
375 InvalidateBestSize();
376 return true;
377 }
378
379 wxWindow *wxToolbook::DoRemovePage(size_t page)
380 {
381 const size_t page_count = GetPageCount();
382 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
383
384 if ( win )
385 {
386 GetToolBar()->DeleteTool(page + 1);
387
388 if (m_selection >= (int)page)
389 {
390 // force new sel valid if possible
391 int sel = m_selection - 1;
392 if (page_count == 1)
393 sel = wxNOT_FOUND;
394 else if ((page_count == 2) || (sel == -1))
395 sel = 0;
396
397 // force sel invalid if deleting current page - don't try to hide it
398 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
399
400 if ((sel != wxNOT_FOUND) && (sel != m_selection))
401 SetSelection(sel);
402 }
403 }
404
405 return win;
406 }
407
408
409 bool wxToolbook::DeleteAllPages()
410 {
411 GetToolBar()->ClearTools();
412 return wxBookCtrlBase::DeleteAllPages();
413 }
414
415 // ----------------------------------------------------------------------------
416 // wxToolbook events
417 // ----------------------------------------------------------------------------
418
419 void wxToolbook::OnToolSelected(wxCommandEvent& event)
420 {
421 const int selNew = event.GetId() - 1;
422
423 if ( selNew == m_selection )
424 {
425 // this event can only come from our own Select(m_selection) below
426 // which we call when the page change is vetoed, so we should simply
427 // ignore it
428 return;
429 }
430
431 SetSelection(selNew);
432
433 // change wasn't allowed, return to previous state
434 if (m_selection != selNew)
435 {
436 GetToolBar()->ToggleTool(m_selection, false);
437 }
438 }
439
440 #endif // wxUSE_TOOLBOOK