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_TOOLBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
49 wxDEFINE_EVENT( wxEVT_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_needsRealizing
= false;
70 bool wxToolbook::Create(wxWindow
*parent
,
77 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
80 // no border for this control
81 style
&= ~wxBORDER_MASK
;
82 style
|= wxBORDER_NONE
;
84 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
85 wxDefaultValidator
, name
) )
88 int tbFlags
= wxTB_TEXT
| wxTB_FLAT
| wxBORDER_NONE
;
89 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0 )
90 tbFlags
|= wxTB_VERTICAL
;
92 tbFlags
|= wxTB_HORIZONTAL
;
94 if ( style
& wxTBK_HORZ_LAYOUT
)
95 tbFlags
|= wxTB_HORZ_LAYOUT
;
97 // TODO: make more configurable
99 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
100 if (style
& wxTBK_BUTTONBAR
)
102 m_bookctrl
= new wxButtonToolBar
114 m_bookctrl
= new wxToolBar
120 tbFlags
| wxTB_NODIVIDER
127 // ----------------------------------------------------------------------------
128 // wxToolbook geometry management
129 // ----------------------------------------------------------------------------
131 void wxToolbook::OnSize(wxSizeEvent
& event
)
133 if (m_needsRealizing
)
136 wxBookCtrlBase::OnSize(event
);
139 // ----------------------------------------------------------------------------
140 // accessing the pages
141 // ----------------------------------------------------------------------------
143 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
145 // Assume tool ids start from 1
146 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
149 tool
->SetLabel(strText
);
156 wxString
wxToolbook::GetPageText(size_t n
) const
158 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
160 return tool
->GetLabel();
162 return wxEmptyString
;
165 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
167 wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") );
172 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
174 wxASSERT( GetImageList() != NULL
);
178 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
181 // Find the image list index for this tool
182 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
183 tool
->SetNormalBitmap(bitmap
);
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 void wxToolbook::SetImageList(wxImageList
*imageList
)
196 wxBookCtrlBase::SetImageList(imageList
);
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 wxBookCtrlEvent
* wxToolbook::CreatePageChangingEvent() const
205 return new wxBookCtrlEvent(wxEVT_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
208 void wxToolbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
210 event
.SetEventType(wxEVT_TOOLBOOK_PAGE_CHANGED
);
213 void wxToolbook::UpdateSelectedPage(size_t newsel
)
215 m_selection
= newsel
;
216 GetToolBar()->ToggleTool(newsel
+ 1, true);
219 // Not part of the wxBookctrl API, but must be called in OnIdle or
220 // by application to realize the toolbar and select the initial page.
221 void wxToolbook::Realize()
223 if (m_needsRealizing
)
225 m_needsRealizing
= false;
227 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
229 GetToolBar()->Realize();
232 if (m_selection
== wxNOT_FOUND
)
235 if (GetPageCount() > 0)
237 int sel
= m_selection
;
238 m_selection
= wxNOT_FOUND
;
245 int wxToolbook::HitTest(const wxPoint
& pt
, long *flags
) const
247 int pagePos
= wxNOT_FOUND
;
250 *flags
= wxBK_HITTEST_NOWHERE
;
252 // convert from wxToolbook coordinates to wxToolBar ones
253 const wxToolBarBase
* const tbar
= GetToolBar();
254 const wxPoint tbarPt
= tbar
->ScreenToClient(ClientToScreen(pt
));
256 // is the point over the toolbar?
257 if ( wxRect(tbar
->GetSize()).Contains(tbarPt
) )
259 const wxToolBarToolBase
* const
260 tool
= tbar
->FindToolForPosition(tbarPt
.x
, tbarPt
.y
);
264 pagePos
= tbar
->GetToolPos(tool
->GetId());
266 *flags
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
;
269 else // not over the toolbar
271 if ( flags
&& GetPageRect().Contains(pt
) )
272 *flags
|= wxBK_HITTEST_ONPAGE
;
278 void wxToolbook::OnIdle(wxIdleEvent
& event
)
280 if (m_needsRealizing
)
285 // ----------------------------------------------------------------------------
286 // adding/removing the pages
287 // ----------------------------------------------------------------------------
289 bool wxToolbook::InsertPage(size_t n
,
291 const wxString
& text
,
295 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
298 m_needsRealizing
= true;
300 wxASSERT(GetImageList() != NULL
);
305 // TODO: make sure all platforms can convert between icon and bitmap,
306 // and/or test whether the image is a bitmap or an icon.
308 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
310 // On Windows, we can lose information by using GetBitmap, so extract icon instead
311 wxIcon icon
= GetImageList()->GetIcon(imageId
);
313 bitmap
.CopyFromIcon(icon
);
316 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
317 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
319 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
320 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
324 GetToolBar()->ToggleTool(n
, true);
330 InvalidateBestSize();
334 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
336 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
340 GetToolBar()->DeleteTool(page
+ 1);
342 DoSetSelectionAfterRemoval(page
);
349 bool wxToolbook::DeleteAllPages()
351 GetToolBar()->ClearTools();
352 return wxBookCtrlBase::DeleteAllPages();
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
361 const int selNew
= event
.GetId() - 1;
363 if ( selNew
== m_selection
)
365 // this event can only come from our own Select(m_selection) below
366 // which we call when the page change is vetoed, so we should simply
371 SetSelection(selNew
);
373 // change wasn't allowed, return to previous state
374 if (m_selection
!= selNew
)
376 GetToolBar()->ToggleTool(m_selection
, false);
380 #endif // wxUSE_TOOLBOOK