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 orient
= wxTB_HORIZONTAL
;
91 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0)
92 orient
= wxTB_VERTICAL
;
94 // TODO: make more configurable
96 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
97 if (style
& wxBK_BUTTONBAR
)
99 m_bookctrl
= new wxButtonToolBar
105 orient
|wxTB_TEXT
|wxTB_FLAT
|wxNO_BORDER
111 m_bookctrl
= new wxToolBar
117 orient
|wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
|wxNO_BORDER
124 // ----------------------------------------------------------------------------
125 // wxToolbook geometry management
126 // ----------------------------------------------------------------------------
128 wxSize
wxToolbook::GetControllerSize() const
130 const wxSize sizeClient
= GetClientSize(),
131 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
132 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
138 size
.x
= sizeClient
.x
;
139 size
.y
= sizeToolBar
.y
;
141 else // left/right aligned
143 size
.x
= sizeToolBar
.x
;
144 size
.y
= sizeClient
.y
;
150 void wxToolbook::OnSize(wxSizeEvent
& event
)
152 if (m_needsRealizing
)
155 wxBookCtrlBase::OnSize(event
);
158 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
160 // we need to add the size of the list control and the border between
161 const wxSize sizeToolBar
= GetControllerSize();
163 wxSize size
= sizePage
;
166 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
168 else // left/right aligned
170 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
176 // ----------------------------------------------------------------------------
177 // accessing the pages
178 // ----------------------------------------------------------------------------
180 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
182 // Assume tool ids start from 1
183 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
186 tool
->SetLabel(strText
);
193 wxString
wxToolbook::GetPageText(size_t n
) const
195 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
197 return tool
->GetLabel();
199 return wxEmptyString
;
202 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
204 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
209 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
211 wxASSERT( GetImageList() != NULL
);
215 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
218 // Find the image list index for this tool
219 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
220 tool
->SetNormalBitmap(bitmap
);
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 void wxToolbook::SetImageList(wxImageList
*imageList
)
233 wxBookCtrlBase::SetImageList(imageList
);
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 int wxToolbook::GetSelection() const
245 wxBookCtrlBaseEvent
* wxToolbook::CreatePageChangingEvent() const
247 return new wxToolbookEvent(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
250 void wxToolbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
252 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
255 void wxToolbook::UpdateSelectedPage(size_t newsel
)
257 m_selection
= newsel
;
258 GetToolBar()->ToggleTool(newsel
+ 1, true);
261 // Not part of the wxBookctrl API, but must be called in OnIdle or
262 // by application to realize the toolbar and select the initial page.
263 void wxToolbook::Realize()
265 if (m_needsRealizing
)
267 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
269 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
270 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
271 GetToolBar()->Realize();
272 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
275 m_needsRealizing
= false;
277 if (m_selection
== -1)
280 if (GetPageCount() > 0)
282 int sel
= m_selection
;
290 int wxToolbook::HitTest(const wxPoint
& pt
, long *flags
) const
292 int pagePos
= wxNOT_FOUND
;
295 *flags
= wxBK_HITTEST_NOWHERE
;
297 // convert from wxToolbook coordinates to wxToolBar ones
298 const wxToolBarBase
* const tbar
= GetToolBar();
299 const wxPoint tbarPt
= tbar
->ScreenToClient(ClientToScreen(pt
));
301 // is the point over the toolbar?
302 if ( wxRect(tbar
->GetSize()).Contains(tbarPt
) )
304 const wxToolBarToolBase
* const
305 tool
= tbar
->FindToolForPosition(tbarPt
.x
, tbarPt
.y
);
309 pagePos
= tbar
->GetToolPos(tool
->GetId());
311 *flags
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
;
314 else // not over the toolbar
316 if ( flags
&& GetPageRect().Contains(pt
) )
317 *flags
|= wxBK_HITTEST_ONPAGE
;
323 void wxToolbook::OnIdle(wxIdleEvent
& event
)
325 if (m_needsRealizing
)
330 // ----------------------------------------------------------------------------
331 // adding/removing the pages
332 // ----------------------------------------------------------------------------
334 bool wxToolbook::InsertPage(size_t n
,
336 const wxString
& text
,
340 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
343 m_needsRealizing
= true;
345 wxASSERT(GetImageList() != NULL
);
350 // TODO: make sure all platforms can convert between icon and bitmap,
351 // and/or test whether the image is a bitmap or an icon.
353 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
355 // On Windows, we can lose information by using GetBitmap, so extract icon instead
356 wxIcon icon
= GetImageList()->GetIcon(imageId
);
358 bitmap
.CopyFromIcon(icon
);
361 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
362 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
364 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
365 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
369 GetToolBar()->ToggleTool(n
, true);
375 InvalidateBestSize();
379 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
381 const size_t page_count
= GetPageCount();
382 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
386 GetToolBar()->DeleteTool(page
+ 1);
388 if (m_selection
>= (int)page
)
390 // force new sel valid if possible
391 int sel
= m_selection
- 1;
394 else if ((page_count
== 2) || (sel
== -1))
397 // force sel invalid if deleting current page - don't try to hide it
398 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
400 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
409 bool wxToolbook::DeleteAllPages()
411 GetToolBar()->ClearTools();
412 return wxBookCtrlBase::DeleteAllPages();
415 // ----------------------------------------------------------------------------
417 // ----------------------------------------------------------------------------
419 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
421 const int selNew
= event
.GetId() - 1;
423 if ( selNew
== m_selection
)
425 // this event can only come from our own Select(m_selection) below
426 // which we call when the page change is vetoed, so we should simply
431 SetSelection(selNew
);
433 // change wasn't allowed, return to previous state
434 if (m_selection
!= selNew
)
436 GetToolBar()->ToggleTool(m_selection
, false);
440 #endif // wxUSE_TOOLBOOK