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 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/imaglist.h"
30 #include "wx/toolbar.h"
31 #include "wx/toolbook.h"
32 #include "wx/settings.h"
33 #include "wx/sysopt.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();
51 const int wxID_TOOLBOOKTOOLBAR
= wxNewId();
53 BEGIN_EVENT_TABLE(wxToolbook
, wxBookCtrlBase
)
54 EVT_SIZE(wxToolbook::OnSize
)
55 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected
)
56 EVT_IDLE(wxToolbook::OnIdle
)
59 // ============================================================================
60 // wxToolbook implementation
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxToolbook creation
65 // ----------------------------------------------------------------------------
67 void wxToolbook::Init()
69 m_selection
= wxNOT_FOUND
;
70 m_needsRealizing
= false;
74 wxToolbook::Create(wxWindow
*parent
,
81 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
86 // no border for this control
87 style
&= ~wxBORDER_MASK
;
88 style
|= wxBORDER_NONE
;
90 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
91 wxDefaultValidator
, name
) )
95 int orient
= wxTB_HORIZONTAL
;
96 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0)
97 orient
= wxTB_VERTICAL
;
99 // TODO: make more configurable
100 m_bookctrl
= new wxToolBar
103 wxID_TOOLBOOKTOOLBAR
,
106 orient
| wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
112 // ----------------------------------------------------------------------------
113 // wxToolbook geometry management
114 // ----------------------------------------------------------------------------
116 wxSize
wxToolbook::GetControllerSize() const
118 const wxSize sizeClient
= GetClientSize(),
119 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
120 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
126 size
.x
= sizeClient
.x
;
127 size
.y
= sizeToolBar
.y
;
129 else // left/right aligned
131 size
.x
= sizeToolBar
.x
;
132 size
.y
= sizeClient
.y
;
138 void wxToolbook::OnSize(wxSizeEvent
& event
)
140 if (m_needsRealizing
)
143 wxBookCtrlBase::OnSize(event
);
146 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
148 // we need to add the size of the list control and the border between
149 const wxSize sizeToolBar
= GetControllerSize();
151 wxSize size
= sizePage
;
154 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
156 else // left/right aligned
158 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
165 // ----------------------------------------------------------------------------
166 // accessing the pages
167 // ----------------------------------------------------------------------------
169 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
171 // Assume tool ids start from 1
172 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
175 tool
->SetLabel(strText
);
182 wxString
wxToolbook::GetPageText(size_t n
) const
184 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
187 return tool
->GetLabel();
190 return wxEmptyString
;
193 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
195 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
200 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
202 wxASSERT( GetImageList() != NULL
);
206 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
209 // Find the image list index for this tool
210 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
211 tool
->SetNormalBitmap(bitmap
);
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 void wxToolbook::SetImageList(wxImageList
*imageList
)
224 wxBookCtrlBase::SetImageList(imageList
);
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 int wxToolbook::GetSelection() const
236 int wxToolbook::SetSelection(size_t n
)
238 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
239 wxT("invalid page index in wxToolbook::SetSelection()") );
241 const int oldSel
= m_selection
;
243 if ( int(n
) != m_selection
)
245 wxToolbookEvent
event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
246 event
.SetSelection(n
);
247 event
.SetOldSelection(m_selection
);
248 event
.SetEventObject(this);
249 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
251 if ( m_selection
!= wxNOT_FOUND
)
252 m_pages
[m_selection
]->Hide();
254 wxWindow
*page
= m_pages
[n
];
255 page
->SetSize(GetPageRect());
258 // change m_selection now to ignore the selection change event
260 GetToolBar()->ToggleTool(n
+1, true);
262 // program allows the page change
263 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
264 (void)GetEventHandler()->ProcessEvent(event
);
271 // Not part of the wxBookctrl API, but must be called in OnIdle or
272 // by application to realize the toolbar and select the initial page.
273 void wxToolbook::Realize()
275 if (m_needsRealizing
)
277 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
279 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
280 GetToolBar()->Realize();
281 wxSystemOptions::SetOption(wxT("msw.remap"), 1);
284 m_needsRealizing
= false;
286 if (m_selection
== -1)
289 if (GetPageCount() > 0)
291 int sel
= m_selection
;
299 void wxToolbook::OnIdle(wxIdleEvent
& event
)
301 if (m_needsRealizing
)
306 // ----------------------------------------------------------------------------
307 // adding/removing the pages
308 // ----------------------------------------------------------------------------
311 wxToolbook::InsertPage(size_t n
,
313 const wxString
& text
,
317 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
320 m_needsRealizing
= true;
322 wxASSERT(GetImageList() != NULL
);
327 // TODO: make sure all platforms can convert between icon and bitmap,
328 // and/or test whether the image is a bitmap or an icon.
330 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
332 // On Windows, we can lose information by using GetBitmap, so extract icon instead
333 wxIcon icon
= GetImageList()->GetIcon(imageId
);
335 bitmap
.CopyFromIcon(icon
);
338 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
339 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
341 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
342 GetToolBar()->AddRadioTool(n
+1, text
, bitmap
, wxNullBitmap
, text
);
346 // GetToolBar()->ToggleTool(n, true);
352 InvalidateBestSize();
356 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
358 const size_t page_count
= GetPageCount();
359 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
363 GetToolBar()->DeleteTool(page
+1);
365 if (m_selection
>= (int)page
)
367 // force new sel valid if possible
368 int sel
= m_selection
- 1;
371 else if ((page_count
== 2) || (sel
== -1))
374 // force sel invalid if deleting current page - don't try to hide it
375 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
377 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
386 bool wxToolbook::DeleteAllPages()
388 GetToolBar()->ClearTools();
389 return wxBookCtrlBase::DeleteAllPages();
392 // ----------------------------------------------------------------------------
394 // ----------------------------------------------------------------------------
396 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
398 const int selNew
= event
.GetId() -1;
400 if ( selNew
== m_selection
)
402 // this event can only come from our own Select(m_selection) below
403 // which we call when the page change is vetoed, so we should simply
408 SetSelection(selNew
);
410 // change wasn't allowed, return to previous state
411 if (m_selection
!= selNew
)
413 GetToolBar()->ToggleTool(m_selection
, false);
417 #endif // wxUSE_TOOLBOOK