Compile fix.
[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 IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent, wxNotifyEvent)
48
49 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
50 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING = wxNewEventType();
51 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED = wxNewEventType();
52 #endif
53 const int wxID_TOOLBOOKTOOLBAR = wxNewId();
54
55 BEGIN_EVENT_TABLE(wxToolbook, wxBookCtrlBase)
56 EVT_SIZE(wxToolbook::OnSize)
57 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected)
58 EVT_IDLE(wxToolbook::OnIdle)
59 END_EVENT_TABLE()
60
61 // ============================================================================
62 // wxToolbook implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // wxToolbook creation
67 // ----------------------------------------------------------------------------
68
69 void wxToolbook::Init()
70 {
71 m_selection = wxNOT_FOUND;
72 m_needsRealizing = false;
73 }
74
75 bool wxToolbook::Create(wxWindow *parent,
76 wxWindowID id,
77 const wxPoint& pos,
78 const wxSize& size,
79 long style,
80 const wxString& name)
81 {
82 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
83 style |= wxBK_TOP;
84
85 // no border for this control
86 style &= ~wxBORDER_MASK;
87 style |= wxBORDER_NONE;
88
89 if ( !wxControl::Create(parent, id, pos, size, style,
90 wxDefaultValidator, name) )
91 return false;
92
93 int orient = wxTB_HORIZONTAL;
94 if ( (style & (wxBK_LEFT | wxBK_RIGHT)) != 0)
95 orient = wxTB_VERTICAL;
96
97 // TODO: make more configurable
98
99 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
100 if (style & wxBK_BUTTONBAR)
101 {
102 m_bookctrl = new wxButtonToolBar
103 (
104 this,
105 wxID_TOOLBOOKTOOLBAR,
106 wxDefaultPosition,
107 wxDefaultSize,
108 orient|wxTB_TEXT|wxTB_FLAT|wxNO_BORDER
109 );
110 }
111 else
112 #endif
113 {
114 m_bookctrl = new wxToolBar
115 (
116 this,
117 wxID_TOOLBOOKTOOLBAR,
118 wxDefaultPosition,
119 wxDefaultSize,
120 orient|wxTB_TEXT|wxTB_FLAT|wxTB_NODIVIDER|wxNO_BORDER
121 );
122 }
123
124 return true;
125 }
126
127 // ----------------------------------------------------------------------------
128 // wxToolbook geometry management
129 // ----------------------------------------------------------------------------
130
131 wxSize wxToolbook::GetControllerSize() const
132 {
133 const wxSize sizeClient = GetClientSize(),
134 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
135 sizeToolBar = GetToolBar()->GetSize() + sizeBorder;
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( _T("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 int wxToolbook::DoSetSelection(size_t n, int flags)
249 {
250 wxToolbookEvent event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING, m_windowId);
251 return wxBookCtrlBase::DoSetSelection(n, flags, event);
252 }
253
254 void wxToolbook::UpdateSelectedPage(size_t newsel)
255 {
256 m_selection = newsel;
257 GetToolBar()->ToggleTool(newsel + 1, true);
258 }
259
260 // Not part of the wxBookctrl API, but must be called in OnIdle or
261 // by application to realize the toolbar and select the initial page.
262 void wxToolbook::Realize()
263 {
264 if (m_needsRealizing)
265 {
266 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
267
268 int remap = wxSystemOptions::GetOptionInt(wxT("msw.remap"));
269 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
270 GetToolBar()->Realize();
271 wxSystemOptions::SetOption(wxT("msw.remap"), remap);
272 }
273
274 m_needsRealizing = false;
275
276 if (m_selection == -1)
277 m_selection = 0;
278
279 if (GetPageCount() > 0)
280 {
281 int sel = m_selection;
282 m_selection = -1;
283 SetSelection(sel);
284 }
285
286 DoSize();
287 }
288
289 int wxToolbook::HitTest(const wxPoint& pt, long *flags) const
290 {
291 int pagePos = wxNOT_FOUND;
292
293 if ( flags )
294 *flags = wxBK_HITTEST_NOWHERE;
295
296 // convert from wxToolbook coordinates to wxToolBar ones
297 const wxToolBarBase * const tbar = GetToolBar();
298 const wxPoint tbarPt = tbar->ScreenToClient(ClientToScreen(pt));
299
300 // is the point over the toolbar?
301 if ( wxRect(tbar->GetSize()).Contains(tbarPt) )
302 {
303 const wxToolBarToolBase * const
304 tool = tbar->FindToolForPosition(tbarPt.x, tbarPt.y);
305
306 if ( tool )
307 {
308 pagePos = tbar->GetToolPos(tool->GetId());
309 if ( flags )
310 *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL;
311 }
312 }
313 else // not over the toolbar
314 {
315 if ( flags && GetPageRect().Contains(pt) )
316 *flags |= wxBK_HITTEST_ONPAGE;
317 }
318
319 return pagePos;
320 }
321
322 void wxToolbook::OnIdle(wxIdleEvent& event)
323 {
324 if (m_needsRealizing)
325 Realize();
326 event.Skip();
327 }
328
329 // ----------------------------------------------------------------------------
330 // adding/removing the pages
331 // ----------------------------------------------------------------------------
332
333 bool wxToolbook::InsertPage(size_t n,
334 wxWindow *page,
335 const wxString& text,
336 bool bSelect,
337 int imageId)
338 {
339 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
340 return false;
341
342 m_needsRealizing = true;
343
344 wxASSERT(GetImageList() != NULL);
345
346 if (!GetImageList())
347 return false;
348
349 // TODO: make sure all platforms can convert between icon and bitmap,
350 // and/or test whether the image is a bitmap or an icon.
351 #ifdef __WXMAC__
352 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
353 #else
354 // On Windows, we can lose information by using GetBitmap, so extract icon instead
355 wxIcon icon = GetImageList()->GetIcon(imageId);
356 wxBitmap bitmap;
357 bitmap.CopyFromIcon(icon);
358 #endif
359
360 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
361 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
362
363 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
364 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
365
366 if (bSelect)
367 {
368 GetToolBar()->ToggleTool(n, true);
369 m_selection = n;
370 }
371 else
372 page->Hide();
373
374 InvalidateBestSize();
375 return true;
376 }
377
378 wxWindow *wxToolbook::DoRemovePage(size_t page)
379 {
380 const size_t page_count = GetPageCount();
381 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
382
383 if ( win )
384 {
385 GetToolBar()->DeleteTool(page + 1);
386
387 if (m_selection >= (int)page)
388 {
389 // force new sel valid if possible
390 int sel = m_selection - 1;
391 if (page_count == 1)
392 sel = wxNOT_FOUND;
393 else if ((page_count == 2) || (sel == -1))
394 sel = 0;
395
396 // force sel invalid if deleting current page - don't try to hide it
397 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
398
399 if ((sel != wxNOT_FOUND) && (sel != m_selection))
400 SetSelection(sel);
401 }
402 }
403
404 return win;
405 }
406
407
408 bool wxToolbook::DeleteAllPages()
409 {
410 GetToolBar()->ClearTools();
411 return wxBookCtrlBase::DeleteAllPages();
412 }
413
414 // ----------------------------------------------------------------------------
415 // wxToolbook events
416 // ----------------------------------------------------------------------------
417
418 void wxToolbook::OnToolSelected(wxCommandEvent& event)
419 {
420 const int selNew = event.GetId() - 1;
421
422 if ( selNew == m_selection )
423 {
424 // this event can only come from our own Select(m_selection) below
425 // which we call when the page change is vetoed, so we should simply
426 // ignore it
427 return;
428 }
429
430 SetSelection(selNew);
431
432 // change wasn't allowed, return to previous state
433 if (m_selection != selNew)
434 {
435 GetToolBar()->ToggleTool(m_selection, false);
436 }
437 }
438
439 #endif // wxUSE_TOOLBOOK