]>
Commit | Line | Data |
---|---|---|
3c55b365 JS |
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 | ||
3c55b365 JS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
34d6780e | 19 | #if wxUSE_TOOLBOOK |
3c55b365 | 20 | |
28bfd0a1 DS |
21 | #ifndef WX_PRECOMP |
22 | #include "wx/icon.h" | |
6037dd26 DS |
23 | #include "wx/settings.h" |
24 | #include "wx/toolbar.h" | |
28bfd0a1 DS |
25 | #endif |
26 | ||
6037dd26 DS |
27 | #include "wx/imaglist.h" |
28 | #include "wx/sysopt.h" | |
29 | #include "wx/toolbook.h" | |
b887dc7b JS |
30 | |
31 | #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON | |
64d3ed17 | 32 | #include "wx/generic/buttonbar.h" |
b887dc7b | 33 | #endif |
6037dd26 | 34 | |
3c55b365 JS |
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) | |
3c55b365 | 47 | |
9b11752c VZ |
48 | wxDEFINE_EVENT( wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING, wxBookCtrlEvent ); |
49 | wxDEFINE_EVENT( wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED, wxBookCtrlEvent ); | |
3c55b365 JS |
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 | ||
c2794afd | 71 | bool wxToolbook::Create(wxWindow *parent, |
3c55b365 JS |
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 ) | |
3c55b365 | 79 | style |= wxBK_TOP; |
3c55b365 JS |
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 | ||
050d159c VZ |
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; | |
c2794afd | 97 | |
917cd965 | 98 | // TODO: make more configurable |
9804d540 | 99 | |
b887dc7b | 100 | #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON |
e3507dd8 | 101 | if (style & wxTBK_BUTTONBAR) |
64d3ed17 JS |
102 | { |
103 | m_bookctrl = new wxButtonToolBar | |
104 | ( | |
105 | this, | |
447325a4 | 106 | wxID_ANY, |
64d3ed17 JS |
107 | wxDefaultPosition, |
108 | wxDefaultSize, | |
050d159c | 109 | tbFlags |
64d3ed17 JS |
110 | ); |
111 | } | |
112 | else | |
b887dc7b | 113 | #endif |
64d3ed17 JS |
114 | { |
115 | m_bookctrl = new wxToolBar | |
3c55b365 JS |
116 | ( |
117 | this, | |
447325a4 | 118 | wxID_ANY, |
3c55b365 JS |
119 | wxDefaultPosition, |
120 | wxDefaultSize, | |
050d159c | 121 | tbFlags | wxTB_NODIVIDER |
3c55b365 | 122 | ); |
64d3ed17 | 123 | } |
3c55b365 JS |
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 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), | |
136 | sizeToolBar = GetToolBar()->GetSize() + sizeBorder; | |
137 | ||
138 | wxSize size; | |
139 | ||
140 | if ( IsVertical() ) | |
141 | { | |
142 | size.x = sizeClient.x; | |
143 | size.y = sizeToolBar.y; | |
144 | } | |
145 | else // left/right aligned | |
146 | { | |
147 | size.x = sizeToolBar.x; | |
148 | size.y = sizeClient.y; | |
149 | } | |
150 | ||
151 | return size; | |
152 | } | |
153 | ||
154 | void wxToolbook::OnSize(wxSizeEvent& event) | |
155 | { | |
156 | if (m_needsRealizing) | |
157 | Realize(); | |
c2794afd | 158 | |
3c55b365 JS |
159 | wxBookCtrlBase::OnSize(event); |
160 | } | |
161 | ||
162 | wxSize wxToolbook::CalcSizeFromPage(const wxSize& sizePage) const | |
163 | { | |
164 | // we need to add the size of the list control and the border between | |
165 | const wxSize sizeToolBar = GetControllerSize(); | |
166 | ||
167 | wxSize size = sizePage; | |
168 | if ( IsVertical() ) | |
169 | { | |
170 | size.y += sizeToolBar.y + GetInternalBorder(); | |
171 | } | |
172 | else // left/right aligned | |
173 | { | |
174 | size.x += sizeToolBar.x + GetInternalBorder(); | |
175 | } | |
176 | ||
177 | return size; | |
178 | } | |
179 | ||
3c55b365 JS |
180 | // ---------------------------------------------------------------------------- |
181 | // accessing the pages | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | bool wxToolbook::SetPageText(size_t n, const wxString& strText) | |
185 | { | |
186 | // Assume tool ids start from 1 | |
c2794afd | 187 | wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1); |
3c55b365 JS |
188 | if (tool) |
189 | { | |
190 | tool->SetLabel(strText); | |
191 | return true; | |
192 | } | |
193 | else | |
194 | return false; | |
195 | } | |
196 | ||
197 | wxString wxToolbook::GetPageText(size_t n) const | |
198 | { | |
c2794afd | 199 | wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1); |
3c55b365 | 200 | if (tool) |
3c55b365 | 201 | return tool->GetLabel(); |
3c55b365 JS |
202 | else |
203 | return wxEmptyString; | |
204 | } | |
205 | ||
206 | int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const | |
207 | { | |
9a83f860 | 208 | wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") ); |
3c55b365 JS |
209 | |
210 | return wxNOT_FOUND; | |
211 | } | |
212 | ||
213 | bool wxToolbook::SetPageImage(size_t n, int imageId) | |
214 | { | |
215 | wxASSERT( GetImageList() != NULL ); | |
216 | if (!GetImageList()) | |
217 | return false; | |
c2794afd DS |
218 | |
219 | wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1); | |
3c55b365 JS |
220 | if (tool) |
221 | { | |
222 | // Find the image list index for this tool | |
223 | wxBitmap bitmap = GetImageList()->GetBitmap(imageId); | |
224 | tool->SetNormalBitmap(bitmap); | |
225 | return true; | |
226 | } | |
227 | else | |
228 | return false; | |
229 | } | |
230 | ||
231 | // ---------------------------------------------------------------------------- | |
232 | // image list stuff | |
233 | // ---------------------------------------------------------------------------- | |
234 | ||
235 | void wxToolbook::SetImageList(wxImageList *imageList) | |
236 | { | |
237 | wxBookCtrlBase::SetImageList(imageList); | |
238 | } | |
239 | ||
240 | // ---------------------------------------------------------------------------- | |
241 | // selection | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | int wxToolbook::GetSelection() const | |
245 | { | |
246 | return m_selection; | |
247 | } | |
248 | ||
3e97a905 | 249 | wxBookCtrlEvent* wxToolbook::CreatePageChangingEvent() const |
3c55b365 | 250 | { |
3e97a905 | 251 | return new wxBookCtrlEvent(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING, m_windowId); |
deb325e3 VZ |
252 | } |
253 | ||
3e97a905 | 254 | void wxToolbook::MakeChangedEvent(wxBookCtrlEvent &event) |
deb325e3 VZ |
255 | { |
256 | event.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED); | |
3c55b365 JS |
257 | } |
258 | ||
8de043bb RR |
259 | void wxToolbook::UpdateSelectedPage(size_t newsel) |
260 | { | |
261 | m_selection = newsel; | |
262 | GetToolBar()->ToggleTool(newsel + 1, true); | |
263 | } | |
264 | ||
3c55b365 JS |
265 | // Not part of the wxBookctrl API, but must be called in OnIdle or |
266 | // by application to realize the toolbar and select the initial page. | |
267 | void wxToolbook::Realize() | |
268 | { | |
269 | if (m_needsRealizing) | |
270 | { | |
1c54c4ed VZ |
271 | m_needsRealizing = false; |
272 | ||
3c55b365 | 273 | GetToolBar()->SetToolBitmapSize(m_maxBitmapSize); |
a16c6fdc | 274 | |
3c55b365 | 275 | GetToolBar()->Realize(); |
3c55b365 | 276 | } |
c2794afd | 277 | |
3c55b365 JS |
278 | if (m_selection == -1) |
279 | m_selection = 0; | |
280 | ||
281 | if (GetPageCount() > 0) | |
282 | { | |
283 | int sel = m_selection; | |
284 | m_selection = -1; | |
285 | SetSelection(sel); | |
286 | } | |
c2794afd | 287 | |
3c55b365 JS |
288 | DoSize(); |
289 | } | |
290 | ||
d0a84b63 VZ |
291 | int wxToolbook::HitTest(const wxPoint& pt, long *flags) const |
292 | { | |
293 | int pagePos = wxNOT_FOUND; | |
294 | ||
295 | if ( flags ) | |
9804d540 | 296 | *flags = wxBK_HITTEST_NOWHERE; |
d0a84b63 VZ |
297 | |
298 | // convert from wxToolbook coordinates to wxToolBar ones | |
299 | const wxToolBarBase * const tbar = GetToolBar(); | |
300 | const wxPoint tbarPt = tbar->ScreenToClient(ClientToScreen(pt)); | |
301 | ||
302 | // is the point over the toolbar? | |
22a35096 | 303 | if ( wxRect(tbar->GetSize()).Contains(tbarPt) ) |
d0a84b63 VZ |
304 | { |
305 | const wxToolBarToolBase * const | |
306 | tool = tbar->FindToolForPosition(tbarPt.x, tbarPt.y); | |
307 | ||
308 | if ( tool ) | |
309 | { | |
310 | pagePos = tbar->GetToolPos(tool->GetId()); | |
311 | if ( flags ) | |
9804d540 | 312 | *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL; |
d0a84b63 VZ |
313 | } |
314 | } | |
315 | else // not over the toolbar | |
316 | { | |
22a35096 | 317 | if ( flags && GetPageRect().Contains(pt) ) |
9804d540 | 318 | *flags |= wxBK_HITTEST_ONPAGE; |
d0a84b63 VZ |
319 | } |
320 | ||
321 | return pagePos; | |
322 | } | |
323 | ||
3c55b365 JS |
324 | void wxToolbook::OnIdle(wxIdleEvent& event) |
325 | { | |
326 | if (m_needsRealizing) | |
327 | Realize(); | |
328 | event.Skip(); | |
329 | } | |
330 | ||
331 | // ---------------------------------------------------------------------------- | |
332 | // adding/removing the pages | |
333 | // ---------------------------------------------------------------------------- | |
334 | ||
c2794afd | 335 | bool wxToolbook::InsertPage(size_t n, |
3c55b365 JS |
336 | wxWindow *page, |
337 | const wxString& text, | |
338 | bool bSelect, | |
339 | int imageId) | |
340 | { | |
341 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) | |
342 | return false; | |
343 | ||
344 | m_needsRealizing = true; | |
c2794afd | 345 | |
3c55b365 | 346 | wxASSERT(GetImageList() != NULL); |
c2794afd | 347 | |
3c55b365 JS |
348 | if (!GetImageList()) |
349 | return false; | |
350 | ||
556bf2c3 JS |
351 | // TODO: make sure all platforms can convert between icon and bitmap, |
352 | // and/or test whether the image is a bitmap or an icon. | |
353 | #ifdef __WXMAC__ | |
354 | wxBitmap bitmap = GetImageList()->GetBitmap(imageId); | |
355 | #else | |
3c55b365 JS |
356 | // On Windows, we can lose information by using GetBitmap, so extract icon instead |
357 | wxIcon icon = GetImageList()->GetIcon(imageId); | |
358 | wxBitmap bitmap; | |
359 | bitmap.CopyFromIcon(icon); | |
556bf2c3 | 360 | #endif |
c2794afd | 361 | |
3c55b365 JS |
362 | m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x); |
363 | m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y); | |
c2794afd | 364 | |
3c55b365 | 365 | GetToolBar()->SetToolBitmapSize(m_maxBitmapSize); |
c2794afd | 366 | GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text); |
3c55b365 JS |
367 | |
368 | if (bSelect) | |
369 | { | |
77631b1d | 370 | GetToolBar()->ToggleTool(n, true); |
3c55b365 JS |
371 | m_selection = n; |
372 | } | |
373 | else | |
374 | page->Hide(); | |
375 | ||
376 | InvalidateBestSize(); | |
377 | return true; | |
378 | } | |
379 | ||
380 | wxWindow *wxToolbook::DoRemovePage(size_t page) | |
381 | { | |
382 | const size_t page_count = GetPageCount(); | |
383 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); | |
384 | ||
385 | if ( win ) | |
386 | { | |
c2794afd | 387 | GetToolBar()->DeleteTool(page + 1); |
3c55b365 JS |
388 | |
389 | if (m_selection >= (int)page) | |
390 | { | |
391 | // force new sel valid if possible | |
392 | int sel = m_selection - 1; | |
393 | if (page_count == 1) | |
394 | sel = wxNOT_FOUND; | |
395 | else if ((page_count == 2) || (sel == -1)) | |
396 | sel = 0; | |
397 | ||
398 | // force sel invalid if deleting current page - don't try to hide it | |
399 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; | |
400 | ||
401 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
402 | SetSelection(sel); | |
403 | } | |
404 | } | |
405 | ||
406 | return win; | |
407 | } | |
408 | ||
409 | ||
410 | bool wxToolbook::DeleteAllPages() | |
411 | { | |
412 | GetToolBar()->ClearTools(); | |
413 | return wxBookCtrlBase::DeleteAllPages(); | |
414 | } | |
415 | ||
416 | // ---------------------------------------------------------------------------- | |
417 | // wxToolbook events | |
418 | // ---------------------------------------------------------------------------- | |
419 | ||
420 | void wxToolbook::OnToolSelected(wxCommandEvent& event) | |
421 | { | |
c2794afd | 422 | const int selNew = event.GetId() - 1; |
3c55b365 JS |
423 | |
424 | if ( selNew == m_selection ) | |
425 | { | |
426 | // this event can only come from our own Select(m_selection) below | |
427 | // which we call when the page change is vetoed, so we should simply | |
428 | // ignore it | |
429 | return; | |
430 | } | |
431 | ||
432 | SetSelection(selNew); | |
433 | ||
434 | // change wasn't allowed, return to previous state | |
435 | if (m_selection != selNew) | |
28bfd0a1 | 436 | { |
3c55b365 | 437 | GetToolBar()->ToggleTool(m_selection, false); |
28bfd0a1 | 438 | } |
3c55b365 JS |
439 | } |
440 | ||
441 | #endif // wxUSE_TOOLBOOK |