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 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
32 #include "wx/generic/buttonbar.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;
73 bool wxToolbook::Create(wxWindow
*parent
,
80 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
83 // no border for this control
84 style
&= ~wxBORDER_MASK
;
85 style
|= wxBORDER_NONE
;
87 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
88 wxDefaultValidator
, name
) )
91 int orient
= wxTB_HORIZONTAL
;
92 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0)
93 orient
= wxTB_VERTICAL
;
95 // TODO: make more configurable
97 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
98 if (style
& wxBK_BUTTONBAR
)
100 m_bookctrl
= new wxButtonToolBar
103 wxID_TOOLBOOKTOOLBAR
,
106 orient
|wxTB_TEXT
|wxTB_FLAT
|wxNO_BORDER
112 m_bookctrl
= new wxToolBar
115 wxID_TOOLBOOKTOOLBAR
,
118 orient
|wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
|wxNO_BORDER
125 // ----------------------------------------------------------------------------
126 // wxToolbook geometry management
127 // ----------------------------------------------------------------------------
129 wxSize
wxToolbook::GetControllerSize() const
131 const wxSize sizeClient
= GetClientSize(),
132 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
133 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
139 size
.x
= sizeClient
.x
;
140 size
.y
= sizeToolBar
.y
;
142 else // left/right aligned
144 size
.x
= sizeToolBar
.x
;
145 size
.y
= sizeClient
.y
;
151 void wxToolbook::OnSize(wxSizeEvent
& event
)
153 if (m_needsRealizing
)
156 wxBookCtrlBase::OnSize(event
);
159 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
161 // we need to add the size of the list control and the border between
162 const wxSize sizeToolBar
= GetControllerSize();
164 wxSize size
= sizePage
;
167 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
169 else // left/right aligned
171 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
177 // ----------------------------------------------------------------------------
178 // accessing the pages
179 // ----------------------------------------------------------------------------
181 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
183 // Assume tool ids start from 1
184 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
187 tool
->SetLabel(strText
);
194 wxString
wxToolbook::GetPageText(size_t n
) const
196 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
198 return tool
->GetLabel();
200 return wxEmptyString
;
203 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
205 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
210 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
212 wxASSERT( GetImageList() != NULL
);
216 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
219 // Find the image list index for this tool
220 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
221 tool
->SetNormalBitmap(bitmap
);
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 void wxToolbook::SetImageList(wxImageList
*imageList
)
234 wxBookCtrlBase::SetImageList(imageList
);
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 int wxToolbook::GetSelection() const
246 int wxToolbook::SetSelection(size_t n
)
248 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
249 wxT("invalid page index in wxToolbook::SetSelection()") );
251 const int oldSel
= m_selection
;
253 if ( int(n
) != m_selection
)
255 wxToolbookEvent
event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
256 event
.SetSelection(n
);
257 event
.SetOldSelection(m_selection
);
258 event
.SetEventObject(this);
259 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
261 if ( m_selection
!= wxNOT_FOUND
)
262 m_pages
[m_selection
]->Hide();
264 wxWindow
*page
= m_pages
[n
];
265 page
->SetSize(GetPageRect());
268 // change m_selection now to ignore the selection change event
270 GetToolBar()->ToggleTool(n
+ 1, true);
272 // program allows the page change
273 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
274 (void)GetEventHandler()->ProcessEvent(event
);
281 // Not part of the wxBookctrl API, but must be called in OnIdle or
282 // by application to realize the toolbar and select the initial page.
283 void wxToolbook::Realize()
285 if (m_needsRealizing
)
287 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
289 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
290 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
291 GetToolBar()->Realize();
292 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
295 m_needsRealizing
= false;
297 if (m_selection
== -1)
300 if (GetPageCount() > 0)
302 int sel
= m_selection
;
310 void wxToolbook::OnIdle(wxIdleEvent
& event
)
312 if (m_needsRealizing
)
317 // ----------------------------------------------------------------------------
318 // adding/removing the pages
319 // ----------------------------------------------------------------------------
321 bool wxToolbook::InsertPage(size_t n
,
323 const wxString
& text
,
327 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
330 m_needsRealizing
= true;
332 wxASSERT(GetImageList() != NULL
);
337 // TODO: make sure all platforms can convert between icon and bitmap,
338 // and/or test whether the image is a bitmap or an icon.
340 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
342 // On Windows, we can lose information by using GetBitmap, so extract icon instead
343 wxIcon icon
= GetImageList()->GetIcon(imageId
);
345 bitmap
.CopyFromIcon(icon
);
348 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
349 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
351 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
352 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
356 GetToolBar()->ToggleTool(n
, true);
362 InvalidateBestSize();
366 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
368 const size_t page_count
= GetPageCount();
369 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
373 GetToolBar()->DeleteTool(page
+ 1);
375 if (m_selection
>= (int)page
)
377 // force new sel valid if possible
378 int sel
= m_selection
- 1;
381 else if ((page_count
== 2) || (sel
== -1))
384 // force sel invalid if deleting current page - don't try to hide it
385 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
387 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
396 bool wxToolbook::DeleteAllPages()
398 GetToolBar()->ClearTools();
399 return wxBookCtrlBase::DeleteAllPages();
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
408 const int selNew
= event
.GetId() - 1;
410 if ( selNew
== m_selection
)
412 // this event can only come from our own Select(m_selection) below
413 // which we call when the page change is vetoed, so we should simply
418 SetSelection(selNew
);
420 // change wasn't allowed, return to previous state
421 if (m_selection
!= selNew
)
423 GetToolBar()->ToggleTool(m_selection
, false);
427 #endif // wxUSE_TOOLBOOK