1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/toolbkg.cpp
3 // Purpose: generic implementation of wxToolbook
4 // Author: Julian Smart
8 // Copyright: (c) 2006 Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/settings.h"
24 #include "wx/toolbar.h"
27 #include "wx/imaglist.h"
28 #include "wx/sysopt.h"
29 #include "wx/toolbook.h"
31 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
32 #include "wx/generic/buttonbar.h"
35 // ----------------------------------------------------------------------------
36 // various wxWidgets macros
37 // ----------------------------------------------------------------------------
39 // check that the page index is valid
40 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxToolbook
, wxBookCtrlBase
)
47 IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent
, wxNotifyEvent
)
49 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
50 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
= wxNewEventType();
51 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
= wxNewEventType();
54 BEGIN_EVENT_TABLE(wxToolbook
, wxBookCtrlBase
)
55 EVT_SIZE(wxToolbook::OnSize
)
56 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected
)
57 EVT_IDLE(wxToolbook::OnIdle
)
60 // ============================================================================
61 // wxToolbook implementation
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxToolbook creation
66 // ----------------------------------------------------------------------------
68 void wxToolbook::Init()
70 m_selection
= wxNOT_FOUND
;
71 m_needsRealizing
= false;
74 bool wxToolbook::Create(wxWindow
*parent
,
81 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
84 // no border for this control
85 style
&= ~wxBORDER_MASK
;
86 style
|= wxBORDER_NONE
;
88 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
89 wxDefaultValidator
, name
) )
92 int orient
= wxTB_HORIZONTAL
;
93 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0)
94 orient
= wxTB_VERTICAL
;
96 // TODO: make more configurable
98 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
99 if (style
& wxBK_BUTTONBAR
)
101 m_bookctrl
= new wxButtonToolBar
107 orient
|wxTB_TEXT
|wxTB_FLAT
|wxNO_BORDER
113 m_bookctrl
= new wxToolBar
119 orient
|wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
|wxNO_BORDER
126 // ----------------------------------------------------------------------------
127 // wxToolbook geometry management
128 // ----------------------------------------------------------------------------
130 wxSize
wxToolbook::GetControllerSize() const
132 const wxSize sizeClient
= GetClientSize(),
133 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
134 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
140 size
.x
= sizeClient
.x
;
141 size
.y
= sizeToolBar
.y
;
143 else // left/right aligned
145 size
.x
= sizeToolBar
.x
;
146 size
.y
= sizeClient
.y
;
152 void wxToolbook::OnSize(wxSizeEvent
& event
)
154 if (m_needsRealizing
)
157 wxBookCtrlBase::OnSize(event
);
160 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
162 // we need to add the size of the list control and the border between
163 const wxSize sizeToolBar
= GetControllerSize();
165 wxSize size
= sizePage
;
168 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
170 else // left/right aligned
172 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
178 // ----------------------------------------------------------------------------
179 // accessing the pages
180 // ----------------------------------------------------------------------------
182 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
184 // Assume tool ids start from 1
185 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
188 tool
->SetLabel(strText
);
195 wxString
wxToolbook::GetPageText(size_t n
) const
197 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
199 return tool
->GetLabel();
201 return wxEmptyString
;
204 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
206 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
211 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
213 wxASSERT( GetImageList() != NULL
);
217 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
220 // Find the image list index for this tool
221 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
222 tool
->SetNormalBitmap(bitmap
);
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 void wxToolbook::SetImageList(wxImageList
*imageList
)
235 wxBookCtrlBase::SetImageList(imageList
);
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 int wxToolbook::GetSelection() const
247 wxBookCtrlBaseEvent
* wxToolbook::CreatePageChangingEvent() const
249 return new wxToolbookEvent(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
252 void wxToolbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
254 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
257 void wxToolbook::UpdateSelectedPage(size_t newsel
)
259 m_selection
= newsel
;
260 GetToolBar()->ToggleTool(newsel
+ 1, true);
263 // Not part of the wxBookctrl API, but must be called in OnIdle or
264 // by application to realize the toolbar and select the initial page.
265 void wxToolbook::Realize()
267 if (m_needsRealizing
)
269 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
271 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
272 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
273 GetToolBar()->Realize();
274 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
277 m_needsRealizing
= false;
279 if (m_selection
== -1)
282 if (GetPageCount() > 0)
284 int sel
= m_selection
;
292 int wxToolbook::HitTest(const wxPoint
& pt
, long *flags
) const
294 int pagePos
= wxNOT_FOUND
;
297 *flags
= wxBK_HITTEST_NOWHERE
;
299 // convert from wxToolbook coordinates to wxToolBar ones
300 const wxToolBarBase
* const tbar
= GetToolBar();
301 const wxPoint tbarPt
= tbar
->ScreenToClient(ClientToScreen(pt
));
303 // is the point over the toolbar?
304 if ( wxRect(tbar
->GetSize()).Contains(tbarPt
) )
306 const wxToolBarToolBase
* const
307 tool
= tbar
->FindToolForPosition(tbarPt
.x
, tbarPt
.y
);
311 pagePos
= tbar
->GetToolPos(tool
->GetId());
313 *flags
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
;
316 else // not over the toolbar
318 if ( flags
&& GetPageRect().Contains(pt
) )
319 *flags
|= wxBK_HITTEST_ONPAGE
;
325 void wxToolbook::OnIdle(wxIdleEvent
& event
)
327 if (m_needsRealizing
)
332 // ----------------------------------------------------------------------------
333 // adding/removing the pages
334 // ----------------------------------------------------------------------------
336 bool wxToolbook::InsertPage(size_t n
,
338 const wxString
& text
,
342 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
345 m_needsRealizing
= true;
347 wxASSERT(GetImageList() != NULL
);
352 // TODO: make sure all platforms can convert between icon and bitmap,
353 // and/or test whether the image is a bitmap or an icon.
355 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
357 // On Windows, we can lose information by using GetBitmap, so extract icon instead
358 wxIcon icon
= GetImageList()->GetIcon(imageId
);
360 bitmap
.CopyFromIcon(icon
);
363 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
364 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
366 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
367 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
371 GetToolBar()->ToggleTool(n
, true);
377 InvalidateBestSize();
381 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
383 const size_t page_count
= GetPageCount();
384 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
388 GetToolBar()->DeleteTool(page
+ 1);
390 if (m_selection
>= (int)page
)
392 // force new sel valid if possible
393 int sel
= m_selection
- 1;
396 else if ((page_count
== 2) || (sel
== -1))
399 // force sel invalid if deleting current page - don't try to hide it
400 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
402 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
411 bool wxToolbook::DeleteAllPages()
413 GetToolBar()->ClearTools();
414 return wxBookCtrlBase::DeleteAllPages();
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
423 const int selNew
= event
.GetId() - 1;
425 if ( selNew
== m_selection
)
427 // this event can only come from our own Select(m_selection) below
428 // which we call when the page change is vetoed, so we should simply
433 SetSelection(selNew
);
435 // change wasn't allowed, return to previous state
436 if (m_selection
!= selNew
)
438 GetToolBar()->ToggleTool(m_selection
, false);
442 #endif // wxUSE_TOOLBOOK