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/toolbar.h"
30 #include "wx/toolbook.h"
31 #include "wx/settings.h"
32 #include "wx/sysopt.h"
34 // ----------------------------------------------------------------------------
35 // various wxWidgets macros
36 // ----------------------------------------------------------------------------
38 // check that the page index is valid
39 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(wxToolbook
, wxBookCtrlBase
)
46 IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent
, wxNotifyEvent
)
48 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
= wxNewEventType();
49 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
= wxNewEventType();
50 const int wxID_TOOLBOOKTOOLBAR
= wxNewId();
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;
73 wxToolbook::Create(wxWindow
*parent
,
80 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
85 // no border for this control
86 style
&= ~wxBORDER_MASK
;
87 style
|= wxBORDER_NONE
;
89 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
90 wxDefaultValidator
, name
) )
93 // TODO: make configurable
94 m_bookctrl
= new wxToolBar
100 wxTB_HORIZONTAL
|wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
106 // ----------------------------------------------------------------------------
107 // wxToolbook geometry management
108 // ----------------------------------------------------------------------------
110 wxSize
wxToolbook::GetControllerSize() const
112 const wxSize sizeClient
= GetClientSize(),
113 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
114 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
120 size
.x
= sizeClient
.x
;
121 size
.y
= sizeToolBar
.y
;
123 else // left/right aligned
125 size
.x
= sizeToolBar
.x
;
126 size
.y
= sizeClient
.y
;
132 void wxToolbook::OnSize(wxSizeEvent
& event
)
134 if (m_needsRealizing
)
137 wxBookCtrlBase::OnSize(event
);
140 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
142 // we need to add the size of the list control and the border between
143 const wxSize sizeToolBar
= GetControllerSize();
145 wxSize size
= sizePage
;
148 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
150 else // left/right aligned
152 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
159 // ----------------------------------------------------------------------------
160 // accessing the pages
161 // ----------------------------------------------------------------------------
163 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
165 // Assume tool ids start from 1
166 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
169 tool
->SetLabel(strText
);
176 wxString
wxToolbook::GetPageText(size_t n
) const
178 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
181 return tool
->GetLabel();
184 return wxEmptyString
;
187 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
189 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
194 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
196 wxASSERT( GetImageList() != NULL
);
200 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
203 // Find the image list index for this tool
204 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
205 tool
->SetNormalBitmap(bitmap
);
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 void wxToolbook::SetImageList(wxImageList
*imageList
)
218 wxBookCtrlBase::SetImageList(imageList
);
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 int wxToolbook::GetSelection() const
230 int wxToolbook::SetSelection(size_t n
)
232 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
233 wxT("invalid page index in wxToolbook::SetSelection()") );
235 const int oldSel
= m_selection
;
237 if ( int(n
) != m_selection
)
239 wxToolbookEvent
event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
240 event
.SetSelection(n
);
241 event
.SetOldSelection(m_selection
);
242 event
.SetEventObject(this);
243 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
245 if ( m_selection
!= wxNOT_FOUND
)
246 m_pages
[m_selection
]->Hide();
248 wxWindow
*page
= m_pages
[n
];
249 page
->SetSize(GetPageRect());
252 // change m_selection now to ignore the selection change event
254 GetToolBar()->ToggleTool(n
+1, true);
256 // program allows the page change
257 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
258 (void)GetEventHandler()->ProcessEvent(event
);
265 // Not part of the wxBookctrl API, but must be called in OnIdle or
266 // by application to realize the toolbar and select the initial page.
267 void wxToolbook::Realize()
269 if (m_needsRealizing
)
271 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
273 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
274 GetToolBar()->Realize();
275 wxSystemOptions::SetOption(wxT("msw.remap"), 1);
278 m_needsRealizing
= false;
280 if (m_selection
== -1)
283 if (GetPageCount() > 0)
285 int sel
= m_selection
;
293 void wxToolbook::OnIdle(wxIdleEvent
& event
)
295 if (m_needsRealizing
)
300 // ----------------------------------------------------------------------------
301 // adding/removing the pages
302 // ----------------------------------------------------------------------------
305 wxToolbook::InsertPage(size_t n
,
307 const wxString
& text
,
311 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
314 m_needsRealizing
= true;
316 wxASSERT(GetImageList() != NULL
);
321 // On Windows, we can lose information by using GetBitmap, so extract icon instead
322 wxIcon icon
= GetImageList()->GetIcon(imageId
);
324 bitmap
.CopyFromIcon(icon
);
326 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
327 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
329 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
330 GetToolBar()->AddRadioTool(n
+1, text
, bitmap
, wxNullBitmap
, text
);
334 // GetToolBar()->ToggleTool(n, true);
340 InvalidateBestSize();
344 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
346 const size_t page_count
= GetPageCount();
347 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
351 GetToolBar()->DeleteTool(page
+1);
353 if (m_selection
>= (int)page
)
355 // force new sel valid if possible
356 int sel
= m_selection
- 1;
359 else if ((page_count
== 2) || (sel
== -1))
362 // force sel invalid if deleting current page - don't try to hide it
363 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
365 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
374 bool wxToolbook::DeleteAllPages()
376 GetToolBar()->ClearTools();
377 return wxBookCtrlBase::DeleteAllPages();
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
386 const int selNew
= event
.GetId() -1;
388 if ( selNew
== m_selection
)
390 // this event can only come from our own Select(m_selection) below
391 // which we call when the page change is vetoed, so we should simply
396 SetSelection(selNew
);
398 // change wasn't allowed, return to previous state
399 if (m_selection
!= selNew
)
401 GetToolBar()->ToggleTool(m_selection
, false);
405 #endif // wxUSE_TOOLBOOK