1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/buttonbar.cpp
3 // Purpose: wxButtonToolBar implementation
4 // Author: Julian Smart, after Robert Roebling, Vadim Zeitlin, SciTech
8 // Copyright: (c) Julian Smart, Robert Roebling, Vadim Zeitlin,
9 // SciTech Software, Inc.
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
28 #if wxUSE_TOOLBAR && wxUSE_BMPBUTTON
35 #include "wx/generic/buttonbar.h"
40 // ----------------------------------------------------------------------------
41 // wxButtonToolBarTool: our implementation of wxToolBarToolBase
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxButtonToolBarTool
: public wxToolBarToolBase
47 wxButtonToolBarTool(wxButtonToolBar
*tbar
,
49 const wxString
& label
,
50 const wxBitmap
& bmpNormal
,
51 const wxBitmap
& bmpDisabled
,
54 const wxString
& shortHelp
,
55 const wxString
& longHelp
)
56 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
57 clientData
, shortHelp
, longHelp
)
59 m_x
= m_y
= wxDefaultCoord
;
66 wxButtonToolBarTool(wxButtonToolBar
*tbar
, wxControl
*control
)
67 : wxToolBarToolBase(tbar
, control
)
69 m_x
= m_y
= wxDefaultCoord
;
75 wxBitmapButton
* GetButton() const { return m_button
; }
76 void SetButton(wxBitmapButton
* button
) { m_button
= button
; }
79 // the tool position (for controls)
86 // the control representing the button
87 wxBitmapButton
* m_button
;
90 // ============================================================================
91 // wxButtonToolBar implementation
92 // ============================================================================
94 IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar
, wxControl
)
96 BEGIN_EVENT_TABLE(wxButtonToolBar
, wxControl
)
97 EVT_BUTTON(wxID_ANY
, wxButtonToolBar::OnCommand
)
100 // ----------------------------------------------------------------------------
101 // wxButtonToolBar creation
102 // ----------------------------------------------------------------------------
104 void wxButtonToolBar::Init()
107 m_needsLayout
= false;
109 // unknown widths for the tools and separators
110 m_widthSeparator
= wxDefaultCoord
;
116 bool wxButtonToolBar::Create(wxWindow
*parent
,
121 const wxString
& name
)
123 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
124 wxDefaultValidator
, name
) )
132 wxButtonToolBar::~wxButtonToolBar()
136 // ----------------------------------------------------------------------------
137 // wxButtonToolBar tool-related methods
138 // ----------------------------------------------------------------------------
140 wxToolBarToolBase
*wxButtonToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
142 // check the "other" direction first: it must be inside the toolbar or we
143 // don't risk finding anything
146 if ( x
< 0 || x
> m_maxWidth
)
149 // we always use x, even for a vertical toolbar, this makes the code
155 if ( y
< 0 || y
> m_maxHeight
)
159 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
161 node
= node
->GetNext() )
163 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
164 wxRect rectTool
= GetToolRect(tool
);
166 wxCoord startTool
, endTool
;
167 GetRectLimits(rectTool
, &startTool
, &endTool
);
169 if ( x
>= startTool
&& x
<= endTool
)
171 // don't return the separators from here, they don't accept any
173 return tool
->IsSeparator() ? NULL
: tool
;
180 void wxButtonToolBar::GetRectLimits(const wxRect
& rect
,
184 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
188 *start
= rect
.GetTop();
189 *end
= rect
.GetBottom();
193 *start
= rect
.GetLeft();
194 *end
= rect
.GetRight();
199 void wxButtonToolBar::SetToolShortHelp(int id
, const wxString
& help
)
201 wxToolBarToolBase
*tool
= FindById(id
);
203 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
205 // TODO: set tooltip/short help
206 tool
->SetShortHelp(help
);
209 bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos
),
210 wxToolBarToolBase
* WXUNUSED(tool
))
215 bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
216 wxToolBarToolBase
* WXUNUSED(tool
))
222 void wxButtonToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(enable
))
227 void wxButtonToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
232 void wxButtonToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
237 wxToolBarToolBase
*wxButtonToolBar::CreateTool(int id
,
238 const wxString
& label
,
239 const wxBitmap
& bmpNormal
,
240 const wxBitmap
& bmpDisabled
,
242 wxObject
*clientData
,
243 const wxString
& shortHelp
,
244 const wxString
& longHelp
)
246 return new wxButtonToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
247 clientData
, shortHelp
, longHelp
);
250 wxToolBarToolBase
*wxButtonToolBar::CreateTool(wxControl
*control
)
252 return new wxButtonToolBarTool(this, control
);
255 // ----------------------------------------------------------------------------
256 // wxButtonToolBar geometry
257 // ----------------------------------------------------------------------------
259 wxRect
wxButtonToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
261 const wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*)toolBase
;
265 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
267 // ensure that we always have the valid tool position
270 wxConstCast(this, wxButtonToolBar
)->DoLayout();
273 rect
.x
= tool
->m_x
- m_xMargin
;
274 rect
.y
= tool
->m_y
- m_yMargin
;
278 if (tool
->IsButton())
280 rect
.width
= m_defaultWidth
;
281 rect
.height
= m_defaultHeight
;
282 if (tool
->GetButton())
283 rect
.SetSize(tool
->GetButton()->GetSize());
285 else if (tool
->IsSeparator())
287 rect
.width
= m_defaultWidth
;
288 rect
.height
= m_widthSeparator
;
292 rect
.width
= tool
->m_width
;
293 rect
.height
= tool
->m_height
;
298 if (tool
->IsButton())
300 rect
.width
= m_defaultWidth
;
301 rect
.height
= m_defaultHeight
;
302 if (tool
->GetButton())
303 rect
.SetSize(tool
->GetButton()->GetSize());
305 else if (tool
->IsSeparator())
307 rect
.width
= m_widthSeparator
;
308 rect
.height
= m_defaultHeight
;
312 rect
.width
= tool
->m_width
;
313 rect
.height
= tool
->m_height
;
317 rect
.width
+= 2*m_xMargin
;
318 rect
.height
+= 2*m_yMargin
;
323 bool wxButtonToolBar::Realize()
325 if ( !wxToolBarBase::Realize() )
328 m_needsLayout
= true;
331 SetBestSize(wxSize(m_maxWidth
, m_maxHeight
));
336 void wxButtonToolBar::DoLayout()
338 m_needsLayout
= false;
340 wxCoord x
= m_xMargin
,
345 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
346 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
347 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
349 // calculate the positions of all elements
350 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
352 node
= node
->GetNext() )
354 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
359 if (tool
->IsButton())
361 if (!tool
->GetButton())
363 wxBitmapButton
* bmpButton
= new wxBitmapButton(this, tool
->GetId(), tool
->GetNormalBitmap(), wxPoint(tool
->m_x
, tool
->m_y
), wxDefaultSize
,
366 tool
->SetButton(bmpButton
);
370 tool
->GetButton()->Move(wxPoint(tool
->m_x
, tool
->m_y
));
374 if (tool
->GetButton())
376 wxSize sz
= tool
->GetButton()->GetSize();
379 maxHeight
= wxMax(maxHeight
, sz
.y
);
382 *pCur
+= (w
+ GetToolPacking());
384 else if (tool
->IsSeparator())
386 *pCur
+= m_widthSeparator
;
388 else if (!IsVertical()) // horizontal control
390 wxControl
*control
= tool
->GetControl();
391 wxSize size
= control
->GetSize();
392 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
393 tool
->m_width
= size
.x
;
394 tool
->m_height
= size
.y
;
396 *pCur
+= tool
->m_width
;
398 maxHeight
= wxMax(maxHeight
, size
.y
);
403 // calculate the total toolbar size
404 m_maxWidth
= x
+ 2*m_xMargin
;
405 m_maxHeight
= maxHeight
+ 2*m_yMargin
;
408 wxSize
wxButtonToolBar::DoGetBestClientSize() const
410 return wxSize(m_maxWidth
, m_maxHeight
);
413 // receives button commands
414 void wxButtonToolBar::OnCommand(wxCommandEvent
& event
)
416 wxButtonToolBarTool
* tool
= (wxButtonToolBarTool
*) FindById(event
.GetId());
423 // TODO: handle toggle items
424 OnLeftClick(event
.GetId(), false);
427 #endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON