1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/toolbkg.cpp
3 // Purpose: generic implementation of wxToolbook
4 // Author: Julian Smart
7 // Copyright: (c) 2006 Julian Smart
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
22 #include "wx/settings.h"
23 #include "wx/toolbar.h"
26 #include "wx/imaglist.h"
27 #include "wx/sysopt.h"
28 #include "wx/toolbook.h"
30 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
31 #include "wx/generic/buttonbar.h"
34 // ----------------------------------------------------------------------------
35 // various wxWidgets macros
36 // ----------------------------------------------------------------------------
38 // check that the page index is valid
39 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(wxToolbook
, wxBookCtrlBase
)
47 wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
48 wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
50 BEGIN_EVENT_TABLE(wxToolbook
, wxBookCtrlBase
)
51 EVT_SIZE(wxToolbook::OnSize
)
52 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected
)
53 EVT_IDLE(wxToolbook::OnIdle
)
56 // ============================================================================
57 // wxToolbook implementation
58 // ============================================================================
60 // ----------------------------------------------------------------------------
61 // wxToolbook creation
62 // ----------------------------------------------------------------------------
64 void wxToolbook::Init()
66 m_needsRealizing
= false;
69 bool wxToolbook::Create(wxWindow
*parent
,
76 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
79 // no border for this control
80 style
&= ~wxBORDER_MASK
;
81 style
|= wxBORDER_NONE
;
83 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
84 wxDefaultValidator
, name
) )
87 int tbFlags
= wxTB_TEXT
| wxTB_FLAT
| wxBORDER_NONE
;
88 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0 )
89 tbFlags
|= wxTB_VERTICAL
;
91 tbFlags
|= wxTB_HORIZONTAL
;
93 if ( style
& wxTBK_HORZ_LAYOUT
)
94 tbFlags
|= wxTB_HORZ_LAYOUT
;
96 // TODO: make more configurable
98 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
99 if (style
& wxTBK_BUTTONBAR
)
101 m_bookctrl
= new wxButtonToolBar
113 m_bookctrl
= new wxToolBar
119 tbFlags
| wxTB_NODIVIDER
126 // ----------------------------------------------------------------------------
127 // wxToolbook geometry management
128 // ----------------------------------------------------------------------------
130 void wxToolbook::OnSize(wxSizeEvent
& event
)
132 if (m_needsRealizing
)
135 wxBookCtrlBase::OnSize(event
);
138 // ----------------------------------------------------------------------------
139 // accessing the pages
140 // ----------------------------------------------------------------------------
142 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
144 // Assume tool ids start from 1
145 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
148 tool
->SetLabel(strText
);
155 wxString
wxToolbook::GetPageText(size_t n
) const
157 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
159 return tool
->GetLabel();
161 return wxEmptyString
;
164 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
166 wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") );
171 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
173 wxASSERT( GetImageList() != NULL
);
177 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
180 // Find the image list index for this tool
181 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
182 tool
->SetNormalBitmap(bitmap
);
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 void wxToolbook::SetImageList(wxImageList
*imageList
)
195 wxBookCtrlBase::SetImageList(imageList
);
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
202 wxBookCtrlEvent
* wxToolbook::CreatePageChangingEvent() const
204 return new wxBookCtrlEvent(wxEVT_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
207 void wxToolbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
209 event
.SetEventType(wxEVT_TOOLBOOK_PAGE_CHANGED
);
212 void wxToolbook::UpdateSelectedPage(size_t newsel
)
214 m_selection
= newsel
;
215 GetToolBar()->ToggleTool(newsel
+ 1, true);
218 // Not part of the wxBookctrl API, but must be called in OnIdle or
219 // by application to realize the toolbar and select the initial page.
220 void wxToolbook::Realize()
222 if (m_needsRealizing
)
224 m_needsRealizing
= false;
226 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
228 GetToolBar()->Realize();
231 if (m_selection
== wxNOT_FOUND
)
234 if (GetPageCount() > 0)
236 int sel
= m_selection
;
237 m_selection
= wxNOT_FOUND
;
244 int wxToolbook::HitTest(const wxPoint
& pt
, long *flags
) const
246 int pagePos
= wxNOT_FOUND
;
249 *flags
= wxBK_HITTEST_NOWHERE
;
251 // convert from wxToolbook coordinates to wxToolBar ones
252 const wxToolBarBase
* const tbar
= GetToolBar();
253 const wxPoint tbarPt
= tbar
->ScreenToClient(ClientToScreen(pt
));
255 // is the point over the toolbar?
256 if ( wxRect(tbar
->GetSize()).Contains(tbarPt
) )
258 const wxToolBarToolBase
* const
259 tool
= tbar
->FindToolForPosition(tbarPt
.x
, tbarPt
.y
);
263 pagePos
= tbar
->GetToolPos(tool
->GetId());
265 *flags
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
;
268 else // not over the toolbar
270 if ( flags
&& GetPageRect().Contains(pt
) )
271 *flags
|= wxBK_HITTEST_ONPAGE
;
277 void wxToolbook::OnIdle(wxIdleEvent
& event
)
279 if (m_needsRealizing
)
284 // ----------------------------------------------------------------------------
285 // adding/removing the pages
286 // ----------------------------------------------------------------------------
288 bool wxToolbook::InsertPage(size_t n
,
290 const wxString
& text
,
294 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
297 m_needsRealizing
= true;
299 wxASSERT(GetImageList() != NULL
);
304 // TODO: make sure all platforms can convert between icon and bitmap,
305 // and/or test whether the image is a bitmap or an icon.
307 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
309 // On Windows, we can lose information by using GetBitmap, so extract icon instead
310 wxIcon icon
= GetImageList()->GetIcon(imageId
);
312 bitmap
.CopyFromIcon(icon
);
315 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
316 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
318 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
319 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
323 GetToolBar()->ToggleTool(n
, true);
329 InvalidateBestSize();
333 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
335 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
339 GetToolBar()->DeleteTool(page
+ 1);
341 DoSetSelectionAfterRemoval(page
);
348 bool wxToolbook::DeleteAllPages()
350 GetToolBar()->ClearTools();
351 return wxBookCtrlBase::DeleteAllPages();
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
360 const int selNew
= event
.GetId() - 1;
362 if ( selNew
== m_selection
)
364 // this event can only come from our own Select(m_selection) below
365 // which we call when the page change is vetoed, so we should simply
370 SetSelection(selNew
);
372 // change wasn't allowed, return to previous state
373 if (m_selection
!= selNew
)
375 GetToolBar()->ToggleTool(m_selection
, false);
379 #endif // wxUSE_TOOLBOOK