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