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