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 void wxToolbook::OnSize(wxSizeEvent
& event
)
134 if (m_needsRealizing
)
137 wxBookCtrlBase::OnSize(event
);
140 // ----------------------------------------------------------------------------
141 // accessing the pages
142 // ----------------------------------------------------------------------------
144 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
146 // Assume tool ids start from 1
147 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
150 tool
->SetLabel(strText
);
157 wxString
wxToolbook::GetPageText(size_t n
) const
159 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
161 return tool
->GetLabel();
163 return wxEmptyString
;
166 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
168 wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") );
173 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
175 wxASSERT( GetImageList() != NULL
);
179 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
182 // Find the image list index for this tool
183 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
184 tool
->SetNormalBitmap(bitmap
);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void wxToolbook::SetImageList(wxImageList
*imageList
)
197 wxBookCtrlBase::SetImageList(imageList
);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 int wxToolbook::GetSelection() const
209 wxBookCtrlEvent
* wxToolbook::CreatePageChangingEvent() const
211 return new wxBookCtrlEvent(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
214 void wxToolbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
216 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
219 void wxToolbook::UpdateSelectedPage(size_t newsel
)
221 m_selection
= newsel
;
222 GetToolBar()->ToggleTool(newsel
+ 1, true);
225 // Not part of the wxBookctrl API, but must be called in OnIdle or
226 // by application to realize the toolbar and select the initial page.
227 void wxToolbook::Realize()
229 if (m_needsRealizing
)
231 m_needsRealizing
= false;
233 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
235 GetToolBar()->Realize();
238 if (m_selection
== -1)
241 if (GetPageCount() > 0)
243 int sel
= m_selection
;
251 int wxToolbook::HitTest(const wxPoint
& pt
, long *flags
) const
253 int pagePos
= wxNOT_FOUND
;
256 *flags
= wxBK_HITTEST_NOWHERE
;
258 // convert from wxToolbook coordinates to wxToolBar ones
259 const wxToolBarBase
* const tbar
= GetToolBar();
260 const wxPoint tbarPt
= tbar
->ScreenToClient(ClientToScreen(pt
));
262 // is the point over the toolbar?
263 if ( wxRect(tbar
->GetSize()).Contains(tbarPt
) )
265 const wxToolBarToolBase
* const
266 tool
= tbar
->FindToolForPosition(tbarPt
.x
, tbarPt
.y
);
270 pagePos
= tbar
->GetToolPos(tool
->GetId());
272 *flags
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
;
275 else // not over the toolbar
277 if ( flags
&& GetPageRect().Contains(pt
) )
278 *flags
|= wxBK_HITTEST_ONPAGE
;
284 void wxToolbook::OnIdle(wxIdleEvent
& event
)
286 if (m_needsRealizing
)
291 // ----------------------------------------------------------------------------
292 // adding/removing the pages
293 // ----------------------------------------------------------------------------
295 bool wxToolbook::InsertPage(size_t n
,
297 const wxString
& text
,
301 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
304 m_needsRealizing
= true;
306 wxASSERT(GetImageList() != NULL
);
311 // TODO: make sure all platforms can convert between icon and bitmap,
312 // and/or test whether the image is a bitmap or an icon.
314 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
316 // On Windows, we can lose information by using GetBitmap, so extract icon instead
317 wxIcon icon
= GetImageList()->GetIcon(imageId
);
319 bitmap
.CopyFromIcon(icon
);
322 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
323 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
325 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
326 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
330 GetToolBar()->ToggleTool(n
, true);
336 InvalidateBestSize();
340 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
342 const size_t page_count
= GetPageCount();
343 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
347 GetToolBar()->DeleteTool(page
+ 1);
349 if (m_selection
>= (int)page
)
351 // force new sel valid if possible
352 int sel
= m_selection
- 1;
355 else if ((page_count
== 2) || (sel
== -1))
358 // force sel invalid if deleting current page - don't try to hide it
359 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
361 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
370 bool wxToolbook::DeleteAllPages()
372 GetToolBar()->ClearTools();
373 return wxBookCtrlBase::DeleteAllPages();
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
382 const int selNew
= event
.GetId() - 1;
384 if ( selNew
== m_selection
)
386 // this event can only come from our own Select(m_selection) below
387 // which we call when the page change is vetoed, so we should simply
392 SetSelection(selNew
);
394 // change wasn't allowed, return to previous state
395 if (m_selection
!= selNew
)
397 GetToolBar()->ToggleTool(m_selection
, false);
401 #endif // wxUSE_TOOLBOOK