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
)
48 wxDEFINE_EVENT( wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
49 wxDEFINE_EVENT( wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
51 BEGIN_EVENT_TABLE(wxToolbook
, wxBookCtrlBase
)
52 EVT_SIZE(wxToolbook::OnSize
)
53 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected
)
54 EVT_IDLE(wxToolbook::OnIdle
)
57 // ============================================================================
58 // wxToolbook implementation
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // wxToolbook creation
63 // ----------------------------------------------------------------------------
65 void wxToolbook::Init()
67 m_selection
= wxNOT_FOUND
;
68 m_needsRealizing
= false;
71 bool wxToolbook::Create(wxWindow
*parent
,
78 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
81 // no border for this control
82 style
&= ~wxBORDER_MASK
;
83 style
|= wxBORDER_NONE
;
85 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
86 wxDefaultValidator
, name
) )
89 int tbFlags
= wxTB_TEXT
| wxTB_FLAT
| wxBORDER_NONE
;
90 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0 )
91 tbFlags
|= wxTB_VERTICAL
;
93 tbFlags
|= wxTB_HORIZONTAL
;
95 if ( style
& wxTBK_HORZ_LAYOUT
)
96 tbFlags
|= wxTB_HORZ_LAYOUT
;
98 // TODO: make more configurable
100 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
101 if (style
& wxTBK_BUTTONBAR
)
103 m_bookctrl
= new wxButtonToolBar
115 m_bookctrl
= new wxToolBar
121 tbFlags
| wxTB_NODIVIDER
128 // ----------------------------------------------------------------------------
129 // wxToolbook geometry management
130 // ----------------------------------------------------------------------------
132 wxSize
wxToolbook::GetControllerSize() const
134 const wxSize sizeClient
= GetClientSize(),
135 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
136 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
142 size
.x
= sizeClient
.x
;
143 size
.y
= sizeToolBar
.y
;
145 else // left/right aligned
147 size
.x
= sizeToolBar
.x
;
148 size
.y
= sizeClient
.y
;
154 void wxToolbook::OnSize(wxSizeEvent
& event
)
156 if (m_needsRealizing
)
159 wxBookCtrlBase::OnSize(event
);
162 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
164 // we need to add the size of the list control and the border between
165 const wxSize sizeToolBar
= GetControllerSize();
167 wxSize size
= sizePage
;
170 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
172 else // left/right aligned
174 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
180 // ----------------------------------------------------------------------------
181 // accessing the pages
182 // ----------------------------------------------------------------------------
184 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
186 // Assume tool ids start from 1
187 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
190 tool
->SetLabel(strText
);
197 wxString
wxToolbook::GetPageText(size_t n
) const
199 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
201 return tool
->GetLabel();
203 return wxEmptyString
;
206 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
208 wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") );
213 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
215 wxASSERT( GetImageList() != NULL
);
219 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
222 // Find the image list index for this tool
223 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
224 tool
->SetNormalBitmap(bitmap
);
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 void wxToolbook::SetImageList(wxImageList
*imageList
)
237 wxBookCtrlBase::SetImageList(imageList
);
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 int wxToolbook::GetSelection() const
249 wxBookCtrlEvent
* wxToolbook::CreatePageChangingEvent() const
251 return new wxBookCtrlEvent(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
254 void wxToolbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
256 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
259 void wxToolbook::UpdateSelectedPage(size_t newsel
)
261 m_selection
= newsel
;
262 GetToolBar()->ToggleTool(newsel
+ 1, true);
265 // Not part of the wxBookctrl API, but must be called in OnIdle or
266 // by application to realize the toolbar and select the initial page.
267 void wxToolbook::Realize()
269 if (m_needsRealizing
)
271 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
273 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
274 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
275 GetToolBar()->Realize();
276 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
279 m_needsRealizing
= false;
281 if (m_selection
== -1)
284 if (GetPageCount() > 0)
286 int sel
= m_selection
;
294 int wxToolbook::HitTest(const wxPoint
& pt
, long *flags
) const
296 int pagePos
= wxNOT_FOUND
;
299 *flags
= wxBK_HITTEST_NOWHERE
;
301 // convert from wxToolbook coordinates to wxToolBar ones
302 const wxToolBarBase
* const tbar
= GetToolBar();
303 const wxPoint tbarPt
= tbar
->ScreenToClient(ClientToScreen(pt
));
305 // is the point over the toolbar?
306 if ( wxRect(tbar
->GetSize()).Contains(tbarPt
) )
308 const wxToolBarToolBase
* const
309 tool
= tbar
->FindToolForPosition(tbarPt
.x
, tbarPt
.y
);
313 pagePos
= tbar
->GetToolPos(tool
->GetId());
315 *flags
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
;
318 else // not over the toolbar
320 if ( flags
&& GetPageRect().Contains(pt
) )
321 *flags
|= wxBK_HITTEST_ONPAGE
;
327 void wxToolbook::OnIdle(wxIdleEvent
& event
)
329 if (m_needsRealizing
)
334 // ----------------------------------------------------------------------------
335 // adding/removing the pages
336 // ----------------------------------------------------------------------------
338 bool wxToolbook::InsertPage(size_t n
,
340 const wxString
& text
,
344 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
347 m_needsRealizing
= true;
349 wxASSERT(GetImageList() != NULL
);
354 // TODO: make sure all platforms can convert between icon and bitmap,
355 // and/or test whether the image is a bitmap or an icon.
357 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
359 // On Windows, we can lose information by using GetBitmap, so extract icon instead
360 wxIcon icon
= GetImageList()->GetIcon(imageId
);
362 bitmap
.CopyFromIcon(icon
);
365 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
366 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
368 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
369 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
373 GetToolBar()->ToggleTool(n
, true);
379 InvalidateBestSize();
383 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
385 const size_t page_count
= GetPageCount();
386 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
390 GetToolBar()->DeleteTool(page
+ 1);
392 if (m_selection
>= (int)page
)
394 // force new sel valid if possible
395 int sel
= m_selection
- 1;
398 else if ((page_count
== 2) || (sel
== -1))
401 // force sel invalid if deleting current page - don't try to hide it
402 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
404 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
413 bool wxToolbook::DeleteAllPages()
415 GetToolBar()->ClearTools();
416 return wxBookCtrlBase::DeleteAllPages();
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
423 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
425 const int selNew
= event
.GetId() - 1;
427 if ( selNew
== m_selection
)
429 // this event can only come from our own Select(m_selection) below
430 // which we call when the page change is vetoed, so we should simply
435 SetSelection(selNew
);
437 // change wasn't allowed, return to previous state
438 if (m_selection
!= selNew
)
440 GetToolBar()->ToggleTool(m_selection
, false);
444 #endif // wxUSE_TOOLBOOK