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 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
50 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
= wxNewEventType();
51 const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
= wxNewEventType();
53 const int wxID_TOOLBOOKTOOLBAR
= wxNewId();
55 BEGIN_EVENT_TABLE(wxToolbook
, wxBookCtrlBase
)
56 EVT_SIZE(wxToolbook::OnSize
)
57 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected
)
58 EVT_IDLE(wxToolbook::OnIdle
)
61 // ============================================================================
62 // wxToolbook implementation
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // wxToolbook creation
67 // ----------------------------------------------------------------------------
69 void wxToolbook::Init()
71 m_selection
= wxNOT_FOUND
;
72 m_needsRealizing
= false;
75 bool wxToolbook::Create(wxWindow
*parent
,
82 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 int orient
= wxTB_HORIZONTAL
;
94 if ( (style
& (wxBK_LEFT
| wxBK_RIGHT
)) != 0)
95 orient
= wxTB_VERTICAL
;
97 // TODO: make more configurable
99 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
100 if (style
& wxBK_BUTTONBAR
)
102 m_bookctrl
= new wxButtonToolBar
105 wxID_TOOLBOOKTOOLBAR
,
108 orient
|wxTB_TEXT
|wxTB_FLAT
|wxNO_BORDER
114 m_bookctrl
= new wxToolBar
117 wxID_TOOLBOOKTOOLBAR
,
120 orient
|wxTB_TEXT
|wxTB_FLAT
|wxTB_NODIVIDER
|wxNO_BORDER
127 // ----------------------------------------------------------------------------
128 // wxToolbook geometry management
129 // ----------------------------------------------------------------------------
131 wxSize
wxToolbook::GetControllerSize() const
133 const wxSize sizeClient
= GetClientSize(),
134 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
135 sizeToolBar
= GetToolBar()->GetSize() + sizeBorder
;
141 size
.x
= sizeClient
.x
;
142 size
.y
= sizeToolBar
.y
;
144 else // left/right aligned
146 size
.x
= sizeToolBar
.x
;
147 size
.y
= sizeClient
.y
;
153 void wxToolbook::OnSize(wxSizeEvent
& event
)
155 if (m_needsRealizing
)
158 wxBookCtrlBase::OnSize(event
);
161 wxSize
wxToolbook::CalcSizeFromPage(const wxSize
& sizePage
) const
163 // we need to add the size of the list control and the border between
164 const wxSize sizeToolBar
= GetControllerSize();
166 wxSize size
= sizePage
;
169 size
.y
+= sizeToolBar
.y
+ GetInternalBorder();
171 else // left/right aligned
173 size
.x
+= sizeToolBar
.x
+ GetInternalBorder();
179 // ----------------------------------------------------------------------------
180 // accessing the pages
181 // ----------------------------------------------------------------------------
183 bool wxToolbook::SetPageText(size_t n
, const wxString
& strText
)
185 // Assume tool ids start from 1
186 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
189 tool
->SetLabel(strText
);
196 wxString
wxToolbook::GetPageText(size_t n
) const
198 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
200 return tool
->GetLabel();
202 return wxEmptyString
;
205 int wxToolbook::GetPageImage(size_t WXUNUSED(n
)) const
207 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
212 bool wxToolbook::SetPageImage(size_t n
, int imageId
)
214 wxASSERT( GetImageList() != NULL
);
218 wxToolBarToolBase
* tool
= GetToolBar()->FindById(n
+ 1);
221 // Find the image list index for this tool
222 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
223 tool
->SetNormalBitmap(bitmap
);
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 void wxToolbook::SetImageList(wxImageList
*imageList
)
236 wxBookCtrlBase::SetImageList(imageList
);
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 int wxToolbook::GetSelection() const
248 int wxToolbook::SetSelection(size_t n
)
250 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
251 wxT("invalid page index in wxToolbook::SetSelection()") );
253 const int oldSel
= m_selection
;
255 if ( int(n
) != m_selection
)
257 wxToolbookEvent
event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
, m_windowId
);
258 event
.SetSelection(n
);
259 event
.SetOldSelection(m_selection
);
260 event
.SetEventObject(this);
261 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
263 if ( m_selection
!= wxNOT_FOUND
)
264 m_pages
[m_selection
]->Hide();
266 wxWindow
*page
= m_pages
[n
];
267 page
->SetSize(GetPageRect());
270 // change m_selection now to ignore the selection change event
272 GetToolBar()->ToggleTool(n
+ 1, true);
274 // program allows the page change
275 event
.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
);
276 (void)GetEventHandler()->ProcessEvent(event
);
283 // Not part of the wxBookctrl API, but must be called in OnIdle or
284 // by application to realize the toolbar and select the initial page.
285 void wxToolbook::Realize()
287 if (m_needsRealizing
)
289 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
291 int remap
= wxSystemOptions::GetOptionInt(wxT("msw.remap"));
292 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
293 GetToolBar()->Realize();
294 wxSystemOptions::SetOption(wxT("msw.remap"), remap
);
297 m_needsRealizing
= false;
299 if (m_selection
== -1)
302 if (GetPageCount() > 0)
304 int sel
= m_selection
;
312 int wxToolbook::HitTest(const wxPoint
& pt
, long *flags
) const
314 int pagePos
= wxNOT_FOUND
;
317 *flags
= wxBK_HITTEST_NOWHERE
;
319 // convert from wxToolbook coordinates to wxToolBar ones
320 const wxToolBarBase
* const tbar
= GetToolBar();
321 const wxPoint tbarPt
= tbar
->ScreenToClient(ClientToScreen(pt
));
323 // is the point over the toolbar?
324 if ( wxRect(tbar
->GetSize()).Inside(tbarPt
) )
326 const wxToolBarToolBase
* const
327 tool
= tbar
->FindToolForPosition(tbarPt
.x
, tbarPt
.y
);
331 pagePos
= tbar
->GetToolPos(tool
->GetId());
333 *flags
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
;
336 else // not over the toolbar
338 if ( flags
&& GetPageRect().Inside(pt
) )
339 *flags
|= wxBK_HITTEST_ONPAGE
;
345 void wxToolbook::OnIdle(wxIdleEvent
& event
)
347 if (m_needsRealizing
)
352 // ----------------------------------------------------------------------------
353 // adding/removing the pages
354 // ----------------------------------------------------------------------------
356 bool wxToolbook::InsertPage(size_t n
,
358 const wxString
& text
,
362 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
365 m_needsRealizing
= true;
367 wxASSERT(GetImageList() != NULL
);
372 // TODO: make sure all platforms can convert between icon and bitmap,
373 // and/or test whether the image is a bitmap or an icon.
375 wxBitmap bitmap
= GetImageList()->GetBitmap(imageId
);
377 // On Windows, we can lose information by using GetBitmap, so extract icon instead
378 wxIcon icon
= GetImageList()->GetIcon(imageId
);
380 bitmap
.CopyFromIcon(icon
);
383 m_maxBitmapSize
.x
= wxMax(bitmap
.GetWidth(), m_maxBitmapSize
.x
);
384 m_maxBitmapSize
.y
= wxMax(bitmap
.GetHeight(), m_maxBitmapSize
.y
);
386 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize
);
387 GetToolBar()->AddRadioTool(n
+ 1, text
, bitmap
, wxNullBitmap
, text
);
391 GetToolBar()->ToggleTool(n
, true);
397 InvalidateBestSize();
401 wxWindow
*wxToolbook::DoRemovePage(size_t page
)
403 const size_t page_count
= GetPageCount();
404 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
408 GetToolBar()->DeleteTool(page
+ 1);
410 if (m_selection
>= (int)page
)
412 // force new sel valid if possible
413 int sel
= m_selection
- 1;
416 else if ((page_count
== 2) || (sel
== -1))
419 // force sel invalid if deleting current page - don't try to hide it
420 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
422 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
431 bool wxToolbook::DeleteAllPages()
433 GetToolBar()->ClearTools();
434 return wxBookCtrlBase::DeleteAllPages();
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 void wxToolbook::OnToolSelected(wxCommandEvent
& event
)
443 const int selNew
= event
.GetId() - 1;
445 if ( selNew
== m_selection
)
447 // this event can only come from our own Select(m_selection) below
448 // which we call when the page change is vetoed, so we should simply
453 SetSelection(selNew
);
455 // change wasn't allowed, return to previous state
456 if (m_selection
!= selNew
)
458 GetToolBar()->ToggleTool(m_selection
, false);
462 #endif // wxUSE_TOOLBOOK