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