]> git.saurik.com Git - wxWidgets.git/blame - src/generic/toolbkg.cpp
cleanup - reformatting (pt 2)
[wxWidgets.git] / src / generic / toolbkg.cpp
CommitLineData
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"
30
3c55b365
JS
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
42IMPLEMENT_DYNAMIC_CLASS(wxToolbook, wxBookCtrlBase)
43IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent, wxNotifyEvent)
44
45const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING = wxNewEventType();
46const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED = wxNewEventType();
47const int wxID_TOOLBOOKTOOLBAR = wxNewId();
48
49BEGIN_EVENT_TABLE(wxToolbook, wxBookCtrlBase)
50 EVT_SIZE(wxToolbook::OnSize)
51 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected)
52 EVT_IDLE(wxToolbook::OnIdle)
53END_EVENT_TABLE()
54
55// ============================================================================
56// wxToolbook implementation
57// ============================================================================
58
59// ----------------------------------------------------------------------------
60// wxToolbook creation
61// ----------------------------------------------------------------------------
62
63void wxToolbook::Init()
64{
65 m_selection = wxNOT_FOUND;
66 m_needsRealizing = false;
67}
68
c2794afd 69bool wxToolbook::Create(wxWindow *parent,
3c55b365
JS
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 )
3c55b365 77 style |= wxBK_TOP;
3c55b365
JS
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
917cd965
RD
87 int orient = wxTB_HORIZONTAL;
88 if ( (style & (wxBK_LEFT | wxBK_RIGHT)) != 0)
89 orient = wxTB_VERTICAL;
c2794afd 90
917cd965 91 // TODO: make more configurable
3c55b365
JS
92 m_bookctrl = new wxToolBar
93 (
94 this,
95 wxID_TOOLBOOKTOOLBAR,
96 wxDefaultPosition,
97 wxDefaultSize,
917cd965 98 orient | wxTB_TEXT|wxTB_FLAT|wxTB_NODIVIDER
3c55b365
JS
99 );
100
101 return true;
102}
103
104// ----------------------------------------------------------------------------
105// wxToolbook geometry management
106// ----------------------------------------------------------------------------
107
108wxSize 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
130void wxToolbook::OnSize(wxSizeEvent& event)
131{
132 if (m_needsRealizing)
133 Realize();
c2794afd 134
3c55b365
JS
135 wxBookCtrlBase::OnSize(event);
136}
137
138wxSize 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
3c55b365
JS
156// ----------------------------------------------------------------------------
157// accessing the pages
158// ----------------------------------------------------------------------------
159
160bool wxToolbook::SetPageText(size_t n, const wxString& strText)
161{
162 // Assume tool ids start from 1
c2794afd 163 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365
JS
164 if (tool)
165 {
166 tool->SetLabel(strText);
167 return true;
168 }
169 else
170 return false;
171}
172
173wxString wxToolbook::GetPageText(size_t n) const
174{
c2794afd 175 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365 176 if (tool)
3c55b365 177 return tool->GetLabel();
3c55b365
JS
178 else
179 return wxEmptyString;
180}
181
182int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const
183{
184 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
185
186 return wxNOT_FOUND;
187}
188
189bool wxToolbook::SetPageImage(size_t n, int imageId)
190{
191 wxASSERT( GetImageList() != NULL );
192 if (!GetImageList())
193 return false;
c2794afd
DS
194
195 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365
JS
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
211void wxToolbook::SetImageList(wxImageList *imageList)
212{
213 wxBookCtrlBase::SetImageList(imageList);
214}
215
216// ----------------------------------------------------------------------------
217// selection
218// ----------------------------------------------------------------------------
219
220int wxToolbook::GetSelection() const
221{
222 return m_selection;
223}
224
225int 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;
c2794afd 249 GetToolBar()->ToggleTool(n + 1, true);
3c55b365
JS
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.
262void wxToolbook::Realize()
263{
264 if (m_needsRealizing)
265 {
266 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
a16c6fdc
RD
267
268 int remap = wxSystemOptions::GetOptionInt(wxT("msw.remap"));
3c55b365
JS
269 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
270 GetToolBar()->Realize();
a16c6fdc 271 wxSystemOptions::SetOption(wxT("msw.remap"), remap);
3c55b365 272 }
c2794afd 273
3c55b365 274 m_needsRealizing = false;
c2794afd 275
3c55b365
JS
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 }
c2794afd 285
3c55b365
JS
286 DoSize();
287}
288
289void wxToolbook::OnIdle(wxIdleEvent& event)
290{
291 if (m_needsRealizing)
292 Realize();
293 event.Skip();
294}
295
296// ----------------------------------------------------------------------------
297// adding/removing the pages
298// ----------------------------------------------------------------------------
299
c2794afd 300bool wxToolbook::InsertPage(size_t n,
3c55b365
JS
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;
c2794afd 310
3c55b365 311 wxASSERT(GetImageList() != NULL);
c2794afd 312
3c55b365
JS
313 if (!GetImageList())
314 return false;
315
556bf2c3
JS
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
3c55b365
JS
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);
556bf2c3 325#endif
c2794afd 326
3c55b365
JS
327 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
328 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
c2794afd 329
3c55b365 330 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
c2794afd 331 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
3c55b365
JS
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
345wxWindow *wxToolbook::DoRemovePage(size_t page)
346{
347 const size_t page_count = GetPageCount();
348 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
349
350 if ( win )
351 {
c2794afd 352 GetToolBar()->DeleteTool(page + 1);
3c55b365
JS
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
375bool wxToolbook::DeleteAllPages()
376{
377 GetToolBar()->ClearTools();
378 return wxBookCtrlBase::DeleteAllPages();
379}
380
381// ----------------------------------------------------------------------------
382// wxToolbook events
383// ----------------------------------------------------------------------------
384
385void wxToolbook::OnToolSelected(wxCommandEvent& event)
386{
c2794afd 387 const int selNew = event.GetId() - 1;
3c55b365
JS
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)
28bfd0a1 401 {
3c55b365 402 GetToolBar()->ToggleTool(m_selection, false);
28bfd0a1 403 }
3c55b365
JS
404}
405
406#endif // wxUSE_TOOLBOOK