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