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
) )
94 // TODO: make configurable
95 m_bookctrl
= new wxToolBar
101 wxTB_HORIZONTAL
|wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
107 // ----------------------------------------------------------------------------
108 // wxToolbook geometry management
109 // ----------------------------------------------------------------------------
111 wxSize
wxToolbook::GetControllerSize() const
113 const wxSize sizeClient
= GetClientSize(),
114 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
115 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
121 size
.x
= sizeClient
.x
;
122 size
.y
= sizeToolBar
.y
;
124 else // left/right aligned
126 size
.x
= sizeToolBar
.x
;
127 size
.y
= sizeClient
.y
;
133 void wxToolbook::OnSize(wxSizeEvent
& event
)
135 if (m_needsRealizing
)
138 wxBookCtrlBase::OnSize(event
);
141 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
143 // we need to add the size of the list control and the border between
144 const wxSize sizeToolBar
= GetControllerSize();
146 wxSize size
= sizePage
;
149 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
151 else // left/right aligned
153 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
160 // ----------------------------------------------------------------------------
161 // accessing the pages
162 // ----------------------------------------------------------------------------
164 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
166 // Assume tool ids start from 1
167 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
170 tool
->SetLabel(strText
);
177 wxString
wxToolbook::GetPageText(size_t n
) const
179 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
182 return tool
->GetLabel();
185 return wxEmptyString
;
188 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
190 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
195 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
197 wxASSERT( GetImageList() != NULL
);
201 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+1);
204 // Find the image list index for this tool
205 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
206 tool
->SetNormalBitmap(bitmap
);
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 void wxToolbook::SetImageList(wxImageList
*imageList
)
219 wxBookCtrlBase::SetImageList(imageList
);
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
226 int wxToolbook::GetSelection() const
231 int wxToolbook::SetSelection(size_t n
)
233 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
234 wxT("invalid page index in wxToolbook::SetSelection()") );
236 const int oldSel
= m_selection
;
238 if ( int(n
) != m_selection
)
240 wxToolbookEvent
event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
241 event
.SetSelection(n
);
242 event
.SetOldSelection(m_selection
);
243 event
.SetEventObject(this);
244 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
246 if ( m_selection
!= wxNOT_FOUND
)
247 m_pages
[m_selection
]->Hide();
249 wxWindow
*page
= m_pages
[n
];
250 page
->SetSize(GetPageRect());
253 // change m_selection now to ignore the selection change event
255 GetToolBar()->ToggleTool(n
+1, true);
257 // program allows the page change
258 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
259 (void)GetEventHandler()->ProcessEvent(event
);
266 // Not part of the wxBookctrl API, but must be called in OnIdle or
267 // by application to realize the toolbar and select the initial page.
268 void wxToolbook::Realize()
270 if (m_needsRealizing
)
272 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
274 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
275 GetToolBar()->Realize();
276 wxSystemOptions::SetOption(wxT("msw.remap"), 1);
279 m_needsRealizing
= false;
281 if (m_selection
== -1)
284 if (GetPageCount() > 0)
286 int sel
= m_selection
;
294 void wxToolbook::OnIdle(wxIdleEvent
& event
)
296 if (m_needsRealizing
)
301 // ----------------------------------------------------------------------------
302 // adding/removing the pages
303 // ----------------------------------------------------------------------------
306 wxToolbook::InsertPage(size_t n
,
308 const wxString
& text
,
312 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
315 m_needsRealizing
= true;
317 wxASSERT(GetImageList() != NULL
);
322 // On Windows, we can lose information by using GetBitmap, so extract icon instead
323 wxIcon icon
= GetImageList()->GetIcon(imageId
);
325 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