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 // ----------------------------------------------------------------------------
32 // various wxWidgets macros
33 // ----------------------------------------------------------------------------
35 // check that the page index is valid
36 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 IMPLEMENT_DYNAMIC_CLASS(wxToolbook
, wxBookCtrlBase
)
43 IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent
, wxNotifyEvent
)
45 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
= wxNewEventType();
46 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
= wxNewEventType();
47 const int wxID_TOOLBOOKTOOLBAR
= wxNewId();
49 BEGIN_EVENT_TABLE(wxToolbook
, wxBookCtrlBase
)
50 EVT_SIZE(wxToolbook::OnSize
)
51 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected
)
52 EVT_IDLE(wxToolbook::OnIdle
)
55 // ============================================================================
56 // wxToolbook implementation
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // wxToolbook creation
61 // ----------------------------------------------------------------------------
63 void wxToolbook::Init()
65 m_selection
= wxNOT_FOUND
;
66 m_needsRealizing
= false;
69 bool wxToolbook::Create(wxWindow
*parent
,
76 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
79 // no border for this control
80 style
&= ~wxBORDER_MASK
;
81 style
|= wxBORDER_NONE
;
83 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
84 wxDefaultValidator
, name
) )
87 int orient
= wxTB_HORIZONTAL
;
88 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0)
89 orient
= wxTB_VERTICAL
;
91 // TODO: make more configurable
92 m_bookctrl
= new wxToolBar
98 orient
| wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
104 // ----------------------------------------------------------------------------
105 // wxToolbook geometry management
106 // ----------------------------------------------------------------------------
108 wxSize
wxToolbook::GetControllerSize() const
110 const wxSize sizeClient
= GetClientSize(),
111 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
112 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
118 size
.x
= sizeClient
.x
;
119 size
.y
= sizeToolBar
.y
;
121 else // left/right aligned
123 size
.x
= sizeToolBar
.x
;
124 size
.y
= sizeClient
.y
;
130 void wxToolbook::OnSize(wxSizeEvent
& event
)
132 if (m_needsRealizing
)
135 wxBookCtrlBase::OnSize(event
);
138 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
140 // we need to add the size of the list control and the border between
141 const wxSize sizeToolBar
= GetControllerSize();
143 wxSize size
= sizePage
;
146 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
148 else // left/right aligned
150 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
156 // ----------------------------------------------------------------------------
157 // accessing the pages
158 // ----------------------------------------------------------------------------
160 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
162 // Assume tool ids start from 1
163 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
166 tool
->SetLabel(strText
);
173 wxString
wxToolbook::GetPageText(size_t n
) const
175 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
177 return tool
->GetLabel();
179 return wxEmptyString
;
182 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
184 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
189 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
191 wxASSERT( GetImageList() != NULL
);
195 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
198 // Find the image list index for this tool
199 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
200 tool
->SetNormalBitmap(bitmap
);
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 void wxToolbook::SetImageList(wxImageList
*imageList
)
213 wxBookCtrlBase::SetImageList(imageList
);
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 int wxToolbook::GetSelection() const
225 int wxToolbook::SetSelection(size_t n
)
227 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
228 wxT("invalid page index in wxToolbook::SetSelection()") );
230 const int oldSel
= m_selection
;
232 if ( int(n
) != m_selection
)
234 wxToolbookEvent
event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
235 event
.SetSelection(n
);
236 event
.SetOldSelection(m_selection
);
237 event
.SetEventObject(this);
238 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
240 if ( m_selection
!= wxNOT_FOUND
)
241 m_pages
[m_selection
]->Hide();
243 wxWindow
*page
= m_pages
[n
];
244 page
->SetSize(GetPageRect());
247 // change m_selection now to ignore the selection change event
249 GetToolBar()->ToggleTool(n
+ 1, true);
251 // program allows the page change
252 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
253 (void)GetEventHandler()->ProcessEvent(event
);
260 // Not part of the wxBookctrl API, but must be called in OnIdle or
261 // by application to realize the toolbar and select the initial page.
262 void wxToolbook::Realize()
264 if (m_needsRealizing
)
266 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
268 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
269 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
270 GetToolBar()->Realize();
271 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
274 m_needsRealizing
= false;
276 if (m_selection
== -1)
279 if (GetPageCount() > 0)
281 int sel
= m_selection
;
289 void wxToolbook::OnIdle(wxIdleEvent
& event
)
291 if (m_needsRealizing
)
296 // ----------------------------------------------------------------------------
297 // adding/removing the pages
298 // ----------------------------------------------------------------------------
300 bool wxToolbook::InsertPage(size_t n
,
302 const wxString
& text
,
306 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
309 m_needsRealizing
= true;
311 wxASSERT(GetImageList() != NULL
);
316 // TODO: make sure all platforms can convert between icon and bitmap,
317 // and/or test whether the image is a bitmap or an icon.
319 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
321 // On Windows, we can lose information by using GetBitmap, so extract icon instead
322 wxIcon icon
= GetImageList()->GetIcon(imageId
);
324 bitmap
.CopyFromIcon(icon
);
327 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
328 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
330 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
331 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
335 // GetToolBar()->ToggleTool(n, true);
341 InvalidateBestSize();
345 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
347 const size_t page_count
= GetPageCount();
348 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
352 GetToolBar()->DeleteTool(page
+ 1);
354 if (m_selection
>= (int)page
)
356 // force new sel valid if possible
357 int sel
= m_selection
- 1;
360 else if ((page_count
== 2) || (sel
== -1))
363 // force sel invalid if deleting current page - don't try to hide it
364 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
366 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
375 bool wxToolbook::DeleteAllPages()
377 GetToolBar()->ClearTools();
378 return wxBookCtrlBase::DeleteAllPages();
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
387 const int selNew
= event
.GetId() - 1;
389 if ( selNew
== m_selection
)
391 // this event can only come from our own Select(m_selection) below
392 // which we call when the page change is vetoed, so we should simply
397 SetSelection(selNew
);
399 // change wasn't allowed, return to previous state
400 if (m_selection
!= selNew
)
402 GetToolBar()->ToggleTool(m_selection
, false);
406 #endif // wxUSE_TOOLBOOK