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 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
= wxNewEventType();
50 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
= wxNewEventType();
52 BEGIN_EVENT_TABLE(wxToolbook
, wxBookCtrlBase
)
53 EVT_SIZE(wxToolbook::OnSize
)
54 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected
)
55 EVT_IDLE(wxToolbook::OnIdle
)
58 // ============================================================================
59 // wxToolbook implementation
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // wxToolbook creation
64 // ----------------------------------------------------------------------------
66 void wxToolbook::Init()
68 m_selection
= wxNOT_FOUND
;
69 m_needsRealizing
= false;
72 bool wxToolbook::Create(wxWindow
*parent
,
79 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
82 // no border for this control
83 style
&= ~wxBORDER_MASK
;
84 style
|= wxBORDER_NONE
;
86 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
87 wxDefaultValidator
, name
) )
90 int tbFlags
= wxTB_TEXT
| wxTB_FLAT
| wxBORDER_NONE
;
91 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0 )
92 tbFlags
|= wxTB_VERTICAL
;
94 tbFlags
|= wxTB_HORIZONTAL
;
96 if ( style
& wxTBK_HORZ_LAYOUT
)
97 tbFlags
|= wxTB_HORZ_LAYOUT
;
99 // TODO: make more configurable
101 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
102 if (style
& wxTBK_BUTTONBAR
)
104 m_bookctrl
= new wxButtonToolBar
116 m_bookctrl
= new wxToolBar
122 tbFlags
| wxTB_NODIVIDER
129 // ----------------------------------------------------------------------------
130 // wxToolbook geometry management
131 // ----------------------------------------------------------------------------
133 wxSize
wxToolbook::GetControllerSize() const
135 const wxSize sizeClient
= GetClientSize(),
136 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
137 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
143 size
.x
= sizeClient
.x
;
144 size
.y
= sizeToolBar
.y
;
146 else // left/right aligned
148 size
.x
= sizeToolBar
.x
;
149 size
.y
= sizeClient
.y
;
155 void wxToolbook::OnSize(wxSizeEvent
& event
)
157 if (m_needsRealizing
)
160 wxBookCtrlBase::OnSize(event
);
163 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
165 // we need to add the size of the list control and the border between
166 const wxSize sizeToolBar
= GetControllerSize();
168 wxSize size
= sizePage
;
171 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
173 else // left/right aligned
175 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
181 // ----------------------------------------------------------------------------
182 // accessing the pages
183 // ----------------------------------------------------------------------------
185 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
187 // Assume tool ids start from 1
188 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
191 tool
->SetLabel(strText
);
198 wxString
wxToolbook::GetPageText(size_t n
) const
200 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
202 return tool
->GetLabel();
204 return wxEmptyString
;
207 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
209 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
214 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
216 wxASSERT( GetImageList() != NULL
);
220 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
223 // Find the image list index for this tool
224 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
225 tool
->SetNormalBitmap(bitmap
);
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 void wxToolbook::SetImageList(wxImageList
*imageList
)
238 wxBookCtrlBase::SetImageList(imageList
);
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 int wxToolbook::GetSelection() const
250 wxBookCtrlBaseEvent
* wxToolbook::CreatePageChangingEvent() const
252 return new wxToolbookEvent(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
255 void wxToolbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
257 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
260 void wxToolbook::UpdateSelectedPage(size_t newsel
)
262 m_selection
= newsel
;
263 GetToolBar()->ToggleTool(newsel
+ 1, true);
266 // Not part of the wxBookctrl API, but must be called in OnIdle or
267 // by application to realize the toolbar and select the initial page.
268 void wxToolbook::Realize()
270 if (m_needsRealizing
)
272 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
274 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
275 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
276 GetToolBar()->Realize();
277 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
280 m_needsRealizing
= false;
282 if (m_selection
== -1)
285 if (GetPageCount() > 0)
287 int sel
= m_selection
;
295 int wxToolbook::HitTest(const wxPoint
& pt
, long *flags
) const
297 int pagePos
= wxNOT_FOUND
;
300 *flags
= wxBK_HITTEST_NOWHERE
;
302 // convert from wxToolbook coordinates to wxToolBar ones
303 const wxToolBarBase
* const tbar
= GetToolBar();
304 const wxPoint tbarPt
= tbar
->ScreenToClient(ClientToScreen(pt
));
306 // is the point over the toolbar?
307 if ( wxRect(tbar
->GetSize()).Contains(tbarPt
) )
309 const wxToolBarToolBase
* const
310 tool
= tbar
->FindToolForPosition(tbarPt
.x
, tbarPt
.y
);
314 pagePos
= tbar
->GetToolPos(tool
->GetId());
316 *flags
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
;
319 else // not over the toolbar
321 if ( flags
&& GetPageRect().Contains(pt
) )
322 *flags
|= wxBK_HITTEST_ONPAGE
;
328 void wxToolbook::OnIdle(wxIdleEvent
& event
)
330 if (m_needsRealizing
)
335 // ----------------------------------------------------------------------------
336 // adding/removing the pages
337 // ----------------------------------------------------------------------------
339 bool wxToolbook::InsertPage(size_t n
,
341 const wxString
& text
,
345 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
348 m_needsRealizing
= true;
350 wxASSERT(GetImageList() != NULL
);
355 // TODO: make sure all platforms can convert between icon and bitmap,
356 // and/or test whether the image is a bitmap or an icon.
358 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
360 // On Windows, we can lose information by using GetBitmap, so extract icon instead
361 wxIcon icon
= GetImageList()->GetIcon(imageId
);
363 bitmap
.CopyFromIcon(icon
);
366 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
367 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
369 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
370 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
374 GetToolBar()->ToggleTool(n
, true);
380 InvalidateBestSize();
384 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
386 const size_t page_count
= GetPageCount();
387 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
391 GetToolBar()->DeleteTool(page
+ 1);
393 if (m_selection
>= (int)page
)
395 // force new sel valid if possible
396 int sel
= m_selection
- 1;
399 else if ((page_count
== 2) || (sel
== -1))
402 // force sel invalid if deleting current page - don't try to hide it
403 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
405 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
414 bool wxToolbook::DeleteAllPages()
416 GetToolBar()->ClearTools();
417 return wxBookCtrlBase::DeleteAllPages();
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
426 const int selNew
= event
.GetId() - 1;
428 if ( selNew
== m_selection
)
430 // this event can only come from our own Select(m_selection) below
431 // which we call when the page change is vetoed, so we should simply
436 SetSelection(selNew
);
438 // change wasn't allowed, return to previous state
439 if (m_selection
!= selNew
)
441 GetToolBar()->ToggleTool(m_selection
, false);
445 #endif // wxUSE_TOOLBOOK