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