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"
30 #include "wx/generic/buttonbar.h"
32 // ----------------------------------------------------------------------------
33 // various wxWidgets macros
34 // ----------------------------------------------------------------------------
36 // check that the page index is valid
37 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 IMPLEMENT_DYNAMIC_CLASS(wxToolbook
, wxBookCtrlBase
)
44 IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent
, wxNotifyEvent
)
46 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
= wxNewEventType();
47 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
= wxNewEventType();
48 const int wxID_TOOLBOOKTOOLBAR
= wxNewId();
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_selection
= wxNOT_FOUND
;
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 orient
= wxTB_HORIZONTAL
;
89 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0)
90 orient
= wxTB_VERTICAL
;
92 // TODO: make more configurable
94 if (style
& wxBK_BUTTONBAR
)
96 m_bookctrl
= new wxButtonToolBar
102 orient
|wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
|wxNO_BORDER
107 m_bookctrl
= new wxToolBar
110 wxID_TOOLBOOKTOOLBAR
,
113 orient
| wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
120 // ----------------------------------------------------------------------------
121 // wxToolbook geometry management
122 // ----------------------------------------------------------------------------
124 wxSize
wxToolbook::GetControllerSize() const
126 const wxSize sizeClient
= GetClientSize(),
127 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
128 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
134 size
.x
= sizeClient
.x
;
135 size
.y
= sizeToolBar
.y
;
137 else // left/right aligned
139 size
.x
= sizeToolBar
.x
;
140 size
.y
= sizeClient
.y
;
146 void wxToolbook::OnSize(wxSizeEvent
& event
)
148 if (m_needsRealizing
)
151 wxBookCtrlBase::OnSize(event
);
154 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
156 // we need to add the size of the list control and the border between
157 const wxSize sizeToolBar
= GetControllerSize();
159 wxSize size
= sizePage
;
162 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
164 else // left/right aligned
166 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
172 // ----------------------------------------------------------------------------
173 // accessing the pages
174 // ----------------------------------------------------------------------------
176 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
178 // Assume tool ids start from 1
179 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
182 tool
->SetLabel(strText
);
189 wxString
wxToolbook::GetPageText(size_t n
) const
191 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
193 return tool
->GetLabel();
195 return wxEmptyString
;
198 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
200 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
205 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
207 wxASSERT( GetImageList() != NULL
);
211 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
214 // Find the image list index for this tool
215 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
216 tool
->SetNormalBitmap(bitmap
);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 void wxToolbook::SetImageList(wxImageList
*imageList
)
229 wxBookCtrlBase::SetImageList(imageList
);
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 int wxToolbook::GetSelection() const
241 int wxToolbook::SetSelection(size_t n
)
243 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
244 wxT("invalid page index in wxToolbook::SetSelection()") );
246 const int oldSel
= m_selection
;
248 if ( int(n
) != m_selection
)
250 wxToolbookEvent
event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
251 event
.SetSelection(n
);
252 event
.SetOldSelection(m_selection
);
253 event
.SetEventObject(this);
254 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
256 if ( m_selection
!= wxNOT_FOUND
)
257 m_pages
[m_selection
]->Hide();
259 wxWindow
*page
= m_pages
[n
];
260 page
->SetSize(GetPageRect());
263 // change m_selection now to ignore the selection change event
265 GetToolBar()->ToggleTool(n
+ 1, true);
267 // program allows the page change
268 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
269 (void)GetEventHandler()->ProcessEvent(event
);
276 // Not part of the wxBookctrl API, but must be called in OnIdle or
277 // by application to realize the toolbar and select the initial page.
278 void wxToolbook::Realize()
280 if (m_needsRealizing
)
282 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
284 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
285 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
286 GetToolBar()->Realize();
287 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
290 m_needsRealizing
= false;
292 if (m_selection
== -1)
295 if (GetPageCount() > 0)
297 int sel
= m_selection
;
305 void wxToolbook::OnIdle(wxIdleEvent
& event
)
307 if (m_needsRealizing
)
312 // ----------------------------------------------------------------------------
313 // adding/removing the pages
314 // ----------------------------------------------------------------------------
316 bool wxToolbook::InsertPage(size_t n
,
318 const wxString
& text
,
322 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
325 m_needsRealizing
= true;
327 wxASSERT(GetImageList() != NULL
);
332 // TODO: make sure all platforms can convert between icon and bitmap,
333 // and/or test whether the image is a bitmap or an icon.
335 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
337 // On Windows, we can lose information by using GetBitmap, so extract icon instead
338 wxIcon icon
= GetImageList()->GetIcon(imageId
);
340 bitmap
.CopyFromIcon(icon
);
343 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
344 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
346 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
347 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
351 // GetToolBar()->ToggleTool(n, true);
357 InvalidateBestSize();
361 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
363 const size_t page_count
= GetPageCount();
364 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
368 GetToolBar()->DeleteTool(page
+ 1);
370 if (m_selection
>= (int)page
)
372 // force new sel valid if possible
373 int sel
= m_selection
- 1;
376 else if ((page_count
== 2) || (sel
== -1))
379 // force sel invalid if deleting current page - don't try to hide it
380 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
382 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
391 bool wxToolbook::DeleteAllPages()
393 GetToolBar()->ClearTools();
394 return wxBookCtrlBase::DeleteAllPages();
397 // ----------------------------------------------------------------------------
399 // ----------------------------------------------------------------------------
401 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
403 const int selNew
= event
.GetId() - 1;
405 if ( selNew
== m_selection
)
407 // this event can only come from our own Select(m_selection) below
408 // which we call when the page change is vetoed, so we should simply
413 SetSelection(selNew
);
415 // change wasn't allowed, return to previous state
416 if (m_selection
!= selNew
)
418 GetToolBar()->ToggleTool(m_selection
, false);
422 #endif // wxUSE_TOOLBOOK