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"
21 #include "wx/imaglist.h"
23 #include "wx/toolbar.h"
24 #include "wx/toolbook.h"
25 #include "wx/settings.h"
26 #include "wx/sysopt.h"
28 // ----------------------------------------------------------------------------
29 // various wxWidgets macros
30 // ----------------------------------------------------------------------------
32 // check that the page index is valid
33 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 IMPLEMENT_DYNAMIC_CLASS(wxToolbook
, wxBookCtrlBase
)
40 IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent
, wxNotifyEvent
)
42 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
= wxNewEventType();
43 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
= wxNewEventType();
44 const int wxID_TOOLBOOKTOOLBAR
= wxNewId();
46 BEGIN_EVENT_TABLE(wxToolbook
, wxBookCtrlBase
)
47 EVT_SIZE(wxToolbook::OnSize
)
48 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected
)
49 EVT_IDLE(wxToolbook::OnIdle
)
52 // ============================================================================
53 // wxToolbook implementation
54 // ============================================================================
56 // ----------------------------------------------------------------------------
57 // wxToolbook creation
58 // ----------------------------------------------------------------------------
60 void wxToolbook::Init()
62 m_selection
= wxNOT_FOUND
;
63 m_needsRealizing
= false;
66 bool wxToolbook::Create(wxWindow
*parent
,
73 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
76 // no border for this control
77 style
&= ~wxBORDER_MASK
;
78 style
|= wxBORDER_NONE
;
80 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
81 wxDefaultValidator
, name
) )
84 int orient
= wxTB_HORIZONTAL
;
85 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0)
86 orient
= wxTB_VERTICAL
;
88 // TODO: make more configurable
89 m_bookctrl
= new wxToolBar
95 orient
| wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
101 // ----------------------------------------------------------------------------
102 // wxToolbook geometry management
103 // ----------------------------------------------------------------------------
105 wxSize
wxToolbook::GetControllerSize() const
107 const wxSize sizeClient
= GetClientSize(),
108 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
109 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
115 size
.x
= sizeClient
.x
;
116 size
.y
= sizeToolBar
.y
;
118 else // left/right aligned
120 size
.x
= sizeToolBar
.x
;
121 size
.y
= sizeClient
.y
;
127 void wxToolbook::OnSize(wxSizeEvent
& event
)
129 if (m_needsRealizing
)
132 wxBookCtrlBase::OnSize(event
);
135 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
137 // we need to add the size of the list control and the border between
138 const wxSize sizeToolBar
= GetControllerSize();
140 wxSize size
= sizePage
;
143 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
145 else // left/right aligned
147 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
153 // ----------------------------------------------------------------------------
154 // accessing the pages
155 // ----------------------------------------------------------------------------
157 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
159 // Assume tool ids start from 1
160 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
163 tool
->SetLabel(strText
);
170 wxString
wxToolbook::GetPageText(size_t n
) const
172 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
174 return tool
->GetLabel();
176 return wxEmptyString
;
179 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
181 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
186 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
188 wxASSERT( GetImageList() != NULL
);
192 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
195 // Find the image list index for this tool
196 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
197 tool
->SetNormalBitmap(bitmap
);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 void wxToolbook::SetImageList(wxImageList
*imageList
)
210 wxBookCtrlBase::SetImageList(imageList
);
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 int wxToolbook::GetSelection() const
222 int wxToolbook::SetSelection(size_t n
)
224 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
225 wxT("invalid page index in wxToolbook::SetSelection()") );
227 const int oldSel
= m_selection
;
229 if ( int(n
) != m_selection
)
231 wxToolbookEvent
event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
232 event
.SetSelection(n
);
233 event
.SetOldSelection(m_selection
);
234 event
.SetEventObject(this);
235 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
237 if ( m_selection
!= wxNOT_FOUND
)
238 m_pages
[m_selection
]->Hide();
240 wxWindow
*page
= m_pages
[n
];
241 page
->SetSize(GetPageRect());
244 // change m_selection now to ignore the selection change event
246 GetToolBar()->ToggleTool(n
+ 1, true);
248 // program allows the page change
249 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
250 (void)GetEventHandler()->ProcessEvent(event
);
257 // Not part of the wxBookctrl API, but must be called in OnIdle or
258 // by application to realize the toolbar and select the initial page.
259 void wxToolbook::Realize()
261 if (m_needsRealizing
)
263 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
265 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
266 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
267 GetToolBar()->Realize();
268 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
271 m_needsRealizing
= false;
273 if (m_selection
== -1)
276 if (GetPageCount() > 0)
278 int sel
= m_selection
;
286 void wxToolbook::OnIdle(wxIdleEvent
& event
)
288 if (m_needsRealizing
)
293 // ----------------------------------------------------------------------------
294 // adding/removing the pages
295 // ----------------------------------------------------------------------------
297 bool wxToolbook::InsertPage(size_t n
,
299 const wxString
& text
,
303 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
306 m_needsRealizing
= true;
308 wxASSERT(GetImageList() != NULL
);
313 // TODO: make sure all platforms can convert between icon and bitmap,
314 // and/or test whether the image is a bitmap or an icon.
316 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
318 // On Windows, we can lose information by using GetBitmap, so extract icon instead
319 wxIcon icon
= GetImageList()->GetIcon(imageId
);
321 bitmap
.CopyFromIcon(icon
);
324 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
325 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
327 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
328 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
332 // GetToolBar()->ToggleTool(n, true);
338 InvalidateBestSize();
342 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
344 const size_t page_count
= GetPageCount();
345 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
349 GetToolBar()->DeleteTool(page
+ 1);
351 if (m_selection
>= (int)page
)
353 // force new sel valid if possible
354 int sel
= m_selection
- 1;
357 else if ((page_count
== 2) || (sel
== -1))
360 // force sel invalid if deleting current page - don't try to hide it
361 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
363 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
372 bool wxToolbook::DeleteAllPages()
374 GetToolBar()->ClearTools();
375 return wxBookCtrlBase::DeleteAllPages();
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
384 const int selNew
= event
.GetId() - 1;
386 if ( selNew
== m_selection
)
388 // this event can only come from our own Select(m_selection) below
389 // which we call when the page change is vetoed, so we should simply
394 SetSelection(selNew
);
396 // change wasn't allowed, return to previous state
397 if (m_selection
!= selNew
)
398 GetToolBar()->ToggleTool(m_selection
, false);
401 #endif // wxUSE_TOOLBOOK