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 // Currently, only for Mac as a toolbar replacement.
29 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
36 #include "wx/generic/buttonbar.h"
41 // ----------------------------------------------------------------------------
42 // wxButtonToolBarTool: our implementation of wxToolBarToolBase
43 // ----------------------------------------------------------------------------
45 class WXDLLEXPORT wxButtonToolBarTool
: public wxToolBarToolBase
48 wxButtonToolBarTool(wxButtonToolBar
*tbar
,
50 const wxString
& label
,
51 const wxBitmap
& bmpNormal
,
52 const wxBitmap
& bmpDisabled
,
55 const wxString
& shortHelp
,
56 const wxString
& longHelp
)
57 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
58 clientData
, shortHelp
, longHelp
)
60 m_x
= m_y
= wxDefaultCoord
;
67 wxButtonToolBarTool(wxButtonToolBar
*tbar
, wxControl
*control
)
68 : wxToolBarToolBase(tbar
, control
)
70 m_x
= m_y
= wxDefaultCoord
;
76 wxBitmapButton
* GetButton() const { return m_button
; }
77 void SetButton(wxBitmapButton
* button
) { m_button
= button
; }
80 // the tool position (for controls)
87 // the control representing the button
88 wxBitmapButton
* m_button
;
91 // ============================================================================
92 // wxButtonToolBar implementation
93 // ============================================================================
95 IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar
, wxControl
)
97 BEGIN_EVENT_TABLE(wxButtonToolBar
, wxControl
)
98 EVT_BUTTON(wxID_ANY
, wxButtonToolBar::OnCommand
)
101 // ----------------------------------------------------------------------------
102 // wxButtonToolBar creation
103 // ----------------------------------------------------------------------------
105 void wxButtonToolBar::Init()
108 m_needsLayout
= false;
110 // unknown widths for the tools and separators
111 m_widthSeparator
= wxDefaultCoord
;
117 bool wxButtonToolBar::Create(wxWindow
*parent
,
122 const wxString
& name
)
124 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
125 wxDefaultValidator
, name
) )
133 wxButtonToolBar::~wxButtonToolBar()
137 // ----------------------------------------------------------------------------
138 // wxButtonToolBar tool-related methods
139 // ----------------------------------------------------------------------------
141 wxToolBarToolBase
*wxButtonToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
143 // check the "other" direction first: it must be inside the toolbar or we
144 // don't risk finding anything
147 if ( x
< 0 || x
> m_maxWidth
)
150 // we always use x, even for a vertical toolbar, this makes the code
156 if ( y
< 0 || y
> m_maxHeight
)
160 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
162 node
= node
->GetNext() )
164 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
165 wxRect rectTool
= GetToolRect(tool
);
167 wxCoord startTool
, endTool
;
168 GetRectLimits(rectTool
, &startTool
, &endTool
);
170 if ( x
>= startTool
&& x
<= endTool
)
172 // don't return the separators from here, they don't accept any
174 return tool
->IsSeparator() ? NULL
: tool
;
181 void wxButtonToolBar::GetRectLimits(const wxRect
& rect
,
185 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
189 *start
= rect
.GetTop();
190 *end
= rect
.GetBottom();
194 *start
= rect
.GetLeft();
195 *end
= rect
.GetRight();
200 void wxButtonToolBar::SetToolShortHelp(int id
, const wxString
& help
)
202 wxToolBarToolBase
*tool
= FindById(id
);
204 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
206 // TODO: set tooltip/short help
207 tool
->SetShortHelp(help
);
210 bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos
),
211 wxToolBarToolBase
* WXUNUSED(tool
))
216 bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
217 wxToolBarToolBase
* WXUNUSED(tool
))
223 void wxButtonToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(enable
))
228 void wxButtonToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
233 void wxButtonToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
238 wxToolBarToolBase
*wxButtonToolBar::CreateTool(int id
,
239 const wxString
& label
,
240 const wxBitmap
& bmpNormal
,
241 const wxBitmap
& bmpDisabled
,
243 wxObject
*clientData
,
244 const wxString
& shortHelp
,
245 const wxString
& longHelp
)
247 return new wxButtonToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
248 clientData
, shortHelp
, longHelp
);
251 wxToolBarToolBase
*wxButtonToolBar::CreateTool(wxControl
*control
)
253 return new wxButtonToolBarTool(this, control
);
256 // ----------------------------------------------------------------------------
257 // wxButtonToolBar geometry
258 // ----------------------------------------------------------------------------
260 wxRect
wxButtonToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
262 const wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*)toolBase
;
266 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
268 // ensure that we always have the valid tool position
271 wxConstCast(this, wxButtonToolBar
)->DoLayout();
274 rect
.x
= tool
->m_x
- m_xMargin
;
275 rect
.y
= tool
->m_y
- m_yMargin
;
279 if (tool
->IsButton())
281 rect
.width
= m_defaultWidth
;
282 rect
.height
= m_defaultHeight
;
283 if (tool
->GetButton())
284 rect
.SetSize(tool
->GetButton()->GetSize());
286 else if (tool
->IsSeparator())
288 rect
.width
= m_defaultWidth
;
289 rect
.height
= m_widthSeparator
;
293 rect
.width
= tool
->m_width
;
294 rect
.height
= tool
->m_height
;
299 if (tool
->IsButton())
301 rect
.width
= m_defaultWidth
;
302 rect
.height
= m_defaultHeight
;
303 if (tool
->GetButton())
304 rect
.SetSize(tool
->GetButton()->GetSize());
306 else if (tool
->IsSeparator())
308 rect
.width
= m_widthSeparator
;
309 rect
.height
= m_defaultHeight
;
313 rect
.width
= tool
->m_width
;
314 rect
.height
= tool
->m_height
;
318 rect
.width
+= 2*m_xMargin
;
319 rect
.height
+= 2*m_yMargin
;
324 bool wxButtonToolBar::Realize()
326 if ( !wxToolBarBase::Realize() )
329 m_needsLayout
= true;
332 SetBestSize(wxSize(m_maxWidth
, m_maxHeight
));
337 void wxButtonToolBar::DoLayout()
339 m_needsLayout
= false;
341 wxCoord x
= m_xMargin
,
346 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
347 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
348 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
350 // calculate the positions of all elements
351 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
353 node
= node
->GetNext() )
355 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
360 if (tool
->IsButton())
362 if (!tool
->GetButton())
364 wxBitmapButton
* bmpButton
= new wxBitmapButton(this, tool
->GetId(), tool
->GetNormalBitmap(), wxPoint(tool
->m_x
, tool
->m_y
), wxDefaultSize
,
367 tool
->SetButton(bmpButton
);
371 tool
->GetButton()->Move(wxPoint(tool
->m_x
, tool
->m_y
));
375 if (tool
->GetButton())
377 wxSize sz
= tool
->GetButton()->GetSize();
380 maxHeight
= wxMax(maxHeight
, sz
.y
);
383 *pCur
+= (w
+ GetToolPacking());
385 else if (tool
->IsSeparator())
387 *pCur
+= m_widthSeparator
;
389 else if (!IsVertical()) // horizontal control
391 wxControl
*control
= tool
->GetControl();
392 wxSize size
= control
->GetSize();
393 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
394 tool
->m_width
= size
.x
;
395 tool
->m_height
= size
.y
;
397 *pCur
+= tool
->m_width
;
399 maxHeight
= wxMax(maxHeight
, size
.y
);
404 // calculate the total toolbar size
405 m_maxWidth
= x
+ 2*m_xMargin
;
406 m_maxHeight
= maxHeight
+ 2*m_yMargin
;
409 wxSize
wxButtonToolBar::DoGetBestClientSize() const
411 return wxSize(m_maxWidth
, m_maxHeight
);
414 // receives button commands
415 void wxButtonToolBar::OnCommand(wxCommandEvent
& event
)
417 wxButtonToolBarTool
* tool
= (wxButtonToolBarTool
*) FindById(event
.GetId());
424 // TODO: handle toggle items
425 OnLeftClick(event
.GetId(), false);
428 #endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON