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